📄 tlist.c
字号:
/*------------------------------------------------------------------------- * * tlist.c * Target list manipulation routines * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $PostgreSQL: pgsql/src/backend/optimizer/util/tlist.c,v 1.70 2005/10/15 02:49:21 momjian Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include "nodes/makefuncs.h"#include "optimizer/tlist.h"#include "optimizer/var.h"#include "parser/parse_expr.h"/***************************************************************************** * Target list creation and searching utilities *****************************************************************************//* * tlist_member * Finds the (first) member of the given tlist whose expression is * equal() to the given expression. Result is NULL if no such member. */TargetEntry *tlist_member(Node *node, List *targetlist){ ListCell *temp; foreach(temp, targetlist) { TargetEntry *tlentry = (TargetEntry *) lfirst(temp); if (equal(node, tlentry->expr)) return tlentry; } return NULL;}/* * flatten_tlist * Create a target list that only contains unique variables. * * Note that Vars with varlevelsup > 0 are not included in the output * tlist. We expect that those will eventually be replaced with Params, * but that probably has not happened at the time this routine is called. * * 'tlist' is the current target list * * Returns the "flattened" new target list. * * The result is entirely new structure sharing no nodes with the original. * Copying the Var nodes is probably overkill, but be safe for now. */List *flatten_tlist(List *tlist){ List *vlist = pull_var_clause((Node *) tlist, false); List *new_tlist; new_tlist = add_to_flat_tlist(NIL, vlist); list_free(vlist); return new_tlist;}/* * add_to_flat_tlist * Add more vars to a flattened tlist (if they're not already in it) * * 'tlist' is the flattened tlist * 'vars' is a list of var nodes * * Returns the extended tlist. */List *add_to_flat_tlist(List *tlist, List *vars){ int next_resno = list_length(tlist) + 1; ListCell *v; foreach(v, vars) { Var *var = (Var *) lfirst(v); if (!tlist_member((Node *) var, tlist)) { TargetEntry *tle; tle = makeTargetEntry(copyObject(var), /* copy needed?? */ next_resno++, NULL, false); tlist = lappend(tlist, tle); } } return tlist;}/* * get_sortgroupclause_tle * Find the targetlist entry matching the given SortClause * (or GroupClause) by ressortgroupref, and return it. * * Because GroupClause is typedef'd as SortClause, either kind of * node can be passed without casting. */TargetEntry *get_sortgroupclause_tle(SortClause *sortClause, List *targetList){ Index refnumber = sortClause->tleSortGroupRef; ListCell *l; foreach(l, targetList) { TargetEntry *tle = (TargetEntry *) lfirst(l); if (tle->ressortgroupref == refnumber) return tle; } elog(ERROR, "ORDER/GROUP BY expression not found in targetlist"); return NULL; /* keep compiler quiet */}/* * get_sortgroupclause_expr * Find the targetlist entry matching the given SortClause * (or GroupClause) by ressortgroupref, and return its expression. * * Because GroupClause is typedef'd as SortClause, either kind of * node can be passed without casting. */Node *get_sortgroupclause_expr(SortClause *sortClause, List *targetList){ TargetEntry *tle = get_sortgroupclause_tle(sortClause, targetList); return (Node *) tle->expr;}/* * get_sortgrouplist_exprs * Given a list of SortClauses (or GroupClauses), build a list * of the referenced targetlist expressions. */List *get_sortgrouplist_exprs(List *sortClauses, List *targetList){ List *result = NIL; ListCell *l; foreach(l, sortClauses) { SortClause *sortcl = (SortClause *) lfirst(l); Node *sortexpr; sortexpr = get_sortgroupclause_expr(sortcl, targetList); result = lappend(result, sortexpr); } return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -