1 /*-------------------------------------------------------------------------
4 * Var node manipulation routines
6 * Note: for most purposes, PlaceHolderVar is considered a Var too,
7 * even if its contained expression is variable-free. Also, CurrentOfExpr
8 * is treated as a Var for purposes of determining whether an expression
12 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
17 * $PostgreSQL: pgsql/src/backend/optimizer/util/var.c,v 1.81 2008/10/21 20:42:53 tgl Exp $
19 *-------------------------------------------------------------------------
23 #include "access/sysattr.h"
24 #include "nodes/nodeFuncs.h"
25 #include "optimizer/prep.h"
26 #include "optimizer/var.h"
27 #include "parser/parsetree.h"
28 #include "rewrite/rewriteManip.h"
35 } pull_varnos_context;
41 } locate_var_of_level_context;
48 } locate_var_of_relation_context;
54 } find_minimum_var_level_context;
59 bool includePlaceHolderVars;
60 } pull_var_clause_context;
66 } flatten_join_alias_vars_context;
68 static bool pull_varnos_walker(Node *node,
69 pull_varnos_context *context);
70 static bool pull_varattnos_walker(Node *node, Bitmapset **varattnos);
71 static bool contain_var_clause_walker(Node *node, void *context);
72 static bool contain_vars_of_level_walker(Node *node, int *sublevels_up);
73 static bool locate_var_of_level_walker(Node *node,
74 locate_var_of_level_context *context);
75 static bool locate_var_of_relation_walker(Node *node,
76 locate_var_of_relation_context *context);
77 static bool find_minimum_var_level_walker(Node *node,
78 find_minimum_var_level_context *context);
79 static bool pull_var_clause_walker(Node *node,
80 pull_var_clause_context *context);
81 static Node *flatten_join_alias_vars_mutator(Node *node,
82 flatten_join_alias_vars_context *context);
83 static Relids alias_relid_set(PlannerInfo *root, Relids relids);
88 * Create a set of all the distinct varnos present in a parsetree.
89 * Only varnos that reference level-zero rtable entries are considered.
91 * NOTE: this is used on not-yet-planned expressions. It may therefore find
92 * bare SubLinks, and if so it needs to recurse into them to look for uplevel
93 * references to the desired rtable level! But when we find a completed
94 * SubPlan, we only need to look at the parameters passed to the subplan.
97 pull_varnos(Node *node)
99 pull_varnos_context context;
101 context.varnos = NULL;
102 context.sublevels_up = 0;
105 * Must be prepared to start with a Query or a bare expression tree; if
106 * it's a Query, we don't want to increment sublevels_up.
108 query_or_expression_tree_walker(node,
113 return context.varnos;
117 pull_varnos_walker(Node *node, pull_varnos_context *context)
123 Var *var = (Var *) node;
125 if (var->varlevelsup == context->sublevels_up)
126 context->varnos = bms_add_member(context->varnos, var->varno);
129 if (IsA(node, CurrentOfExpr))
131 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
133 if (context->sublevels_up == 0)
134 context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
137 if (IsA(node, PlaceHolderVar))
140 * Normally, we can just take the varnos in the contained expression.
141 * But if it is variable-free, use the PHV's syntactic relids.
143 PlaceHolderVar *phv = (PlaceHolderVar *) node;
144 pull_varnos_context subcontext;
146 subcontext.varnos = NULL;
147 subcontext.sublevels_up = context->sublevels_up;
148 (void) pull_varnos_walker((Node *) phv->phexpr, &subcontext);
150 if (bms_is_empty(subcontext.varnos) &&
151 phv->phlevelsup == context->sublevels_up)
152 context->varnos = bms_add_members(context->varnos, phv->phrels);
154 context->varnos = bms_join(context->varnos, subcontext.varnos);
157 if (IsA(node, Query))
159 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
162 context->sublevels_up++;
163 result = query_tree_walker((Query *) node, pull_varnos_walker,
164 (void *) context, 0);
165 context->sublevels_up--;
168 return expression_tree_walker(node, pull_varnos_walker,
175 * Find all the distinct attribute numbers present in an expression tree,
176 * and add them to the initial contents of *varattnos.
177 * Only Vars that reference RTE 1 of rtable level zero are considered.
179 * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
180 * we can include system attributes (e.g., OID) in the bitmap representation.
182 * Currently, this does not support subqueries nor expressions containing
183 * references to multiple tables; not needed since it's only applied to
184 * index expressions and predicates.
187 pull_varattnos(Node *node, Bitmapset **varattnos)
189 (void) pull_varattnos_walker(node, varattnos);
193 pull_varattnos_walker(Node *node, Bitmapset **varattnos)
199 Var *var = (Var *) node;
201 Assert(var->varno == 1);
202 *varattnos = bms_add_member(*varattnos,
203 var->varattno - FirstLowInvalidHeapAttributeNumber);
206 /* Should not find a subquery or subplan */
207 Assert(!IsA(node, Query));
208 Assert(!IsA(node, SubPlan));
210 return expression_tree_walker(node, pull_varattnos_walker,
217 * Recursively scan a clause to discover whether it contains any Var nodes
218 * (of the current query level).
220 * Returns true if any varnode found.
222 * Does not examine subqueries, therefore must only be used after reduction
223 * of sublinks to subplans!
226 contain_var_clause(Node *node)
228 return contain_var_clause_walker(node, NULL);
232 contain_var_clause_walker(Node *node, void *context)
238 if (((Var *) node)->varlevelsup == 0)
239 return true; /* abort the tree traversal and return true */
242 if (IsA(node, CurrentOfExpr))
244 if (IsA(node, PlaceHolderVar))
246 if (((PlaceHolderVar *) node)->phlevelsup == 0)
247 return true; /* abort the tree traversal and return true */
248 /* else fall through to check the contained expr */
250 return expression_tree_walker(node, contain_var_clause_walker, context);
255 * contain_vars_of_level
256 * Recursively scan a clause to discover whether it contains any Var nodes
257 * of the specified query level.
259 * Returns true if any such Var found.
261 * Will recurse into sublinks. Also, may be invoked directly on a Query.
264 contain_vars_of_level(Node *node, int levelsup)
266 int sublevels_up = levelsup;
268 return query_or_expression_tree_walker(node,
269 contain_vars_of_level_walker,
270 (void *) &sublevels_up,
275 contain_vars_of_level_walker(Node *node, int *sublevels_up)
281 if (((Var *) node)->varlevelsup == *sublevels_up)
282 return true; /* abort tree traversal and return true */
285 if (IsA(node, CurrentOfExpr))
287 if (*sublevels_up == 0)
291 if (IsA(node, PlaceHolderVar))
293 if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
294 return true; /* abort the tree traversal and return true */
295 /* else fall through to check the contained expr */
297 if (IsA(node, Query))
299 /* Recurse into subselects */
303 result = query_tree_walker((Query *) node,
304 contain_vars_of_level_walker,
305 (void *) sublevels_up,
310 return expression_tree_walker(node,
311 contain_vars_of_level_walker,
312 (void *) sublevels_up);
317 * locate_var_of_level
318 * Find the parse location of any Var of the specified query level.
320 * Returns -1 if no such Var is in the querytree, or if they all have
321 * unknown parse location. (The former case is probably caller error,
322 * but we don't bother to distinguish it from the latter case.)
324 * Will recurse into sublinks. Also, may be invoked directly on a Query.
326 * Note: it might seem appropriate to merge this functionality into
327 * contain_vars_of_level, but that would complicate that function's API.
328 * Currently, the only uses of this function are for error reporting,
329 * and so shaving cycles probably isn't very important.
332 locate_var_of_level(Node *node, int levelsup)
334 locate_var_of_level_context context;
336 context.var_location = -1; /* in case we find nothing */
337 context.sublevels_up = levelsup;
339 (void) query_or_expression_tree_walker(node,
340 locate_var_of_level_walker,
344 return context.var_location;
348 locate_var_of_level_walker(Node *node,
349 locate_var_of_level_context *context)
355 Var *var = (Var *) node;
357 if (var->varlevelsup == context->sublevels_up &&
360 context->var_location = var->location;
361 return true; /* abort tree traversal and return true */
365 if (IsA(node, CurrentOfExpr))
367 /* since CurrentOfExpr doesn't carry location, nothing we can do */
370 /* No extra code needed for PlaceHolderVar; just look in contained expr */
371 if (IsA(node, Query))
373 /* Recurse into subselects */
376 context->sublevels_up++;
377 result = query_tree_walker((Query *) node,
378 locate_var_of_level_walker,
381 context->sublevels_up--;
384 return expression_tree_walker(node,
385 locate_var_of_level_walker,
391 * locate_var_of_relation
392 * Find the parse location of any Var of the specified relation.
394 * Returns -1 if no such Var is in the querytree, or if they all have
395 * unknown parse location.
397 * Will recurse into sublinks. Also, may be invoked directly on a Query.
400 locate_var_of_relation(Node *node, int relid, int levelsup)
402 locate_var_of_relation_context context;
404 context.var_location = -1; /* in case we find nothing */
405 context.relid = relid;
406 context.sublevels_up = levelsup;
408 (void) query_or_expression_tree_walker(node,
409 locate_var_of_relation_walker,
413 return context.var_location;
417 locate_var_of_relation_walker(Node *node,
418 locate_var_of_relation_context *context)
424 Var *var = (Var *) node;
426 if (var->varno == context->relid &&
427 var->varlevelsup == context->sublevels_up &&
430 context->var_location = var->location;
431 return true; /* abort tree traversal and return true */
435 if (IsA(node, CurrentOfExpr))
437 /* since CurrentOfExpr doesn't carry location, nothing we can do */
440 /* No extra code needed for PlaceHolderVar; just look in contained expr */
441 if (IsA(node, Query))
443 /* Recurse into subselects */
446 context->sublevels_up++;
447 result = query_tree_walker((Query *) node,
448 locate_var_of_relation_walker,
451 context->sublevels_up--;
454 return expression_tree_walker(node,
455 locate_var_of_relation_walker,
461 * find_minimum_var_level
462 * Recursively scan a clause to find the lowest variable level it
463 * contains --- for example, zero is returned if there are any local
464 * variables, one if there are no local variables but there are
465 * one-level-up outer references, etc. Subqueries are scanned to see
466 * if they possess relevant outer references. (But any local variables
467 * within subqueries are not relevant.)
469 * -1 is returned if the clause has no variables at all.
471 * Will recurse into sublinks. Also, may be invoked directly on a Query.
474 find_minimum_var_level(Node *node)
476 find_minimum_var_level_context context;
478 context.min_varlevel = -1; /* signifies nothing found yet */
479 context.sublevels_up = 0;
481 (void) query_or_expression_tree_walker(node,
482 find_minimum_var_level_walker,
486 return context.min_varlevel;
490 find_minimum_var_level_walker(Node *node,
491 find_minimum_var_level_context *context)
497 int varlevelsup = ((Var *) node)->varlevelsup;
499 /* convert levelsup to frame of reference of original query */
500 varlevelsup -= context->sublevels_up;
501 /* ignore local vars of subqueries */
502 if (varlevelsup >= 0)
504 if (context->min_varlevel < 0 ||
505 context->min_varlevel > varlevelsup)
507 context->min_varlevel = varlevelsup;
510 * As soon as we find a local variable, we can abort the tree
511 * traversal, since min_varlevel is then certainly 0.
513 if (varlevelsup == 0)
518 if (IsA(node, CurrentOfExpr))
522 /* convert levelsup to frame of reference of original query */
523 varlevelsup -= context->sublevels_up;
524 /* ignore local vars of subqueries */
525 if (varlevelsup >= 0)
527 if (context->min_varlevel < 0 ||
528 context->min_varlevel > varlevelsup)
530 context->min_varlevel = varlevelsup;
533 * As soon as we find a local variable, we can abort the tree
534 * traversal, since min_varlevel is then certainly 0.
536 if (varlevelsup == 0)
543 * An Aggref must be treated like a Var of its level. Normally we'd get
544 * the same result from looking at the Vars in the aggregate's argument,
545 * but this fails in the case of a Var-less aggregate call (COUNT(*)).
547 if (IsA(node, Aggref))
549 int agglevelsup = ((Aggref *) node)->agglevelsup;
551 /* convert levelsup to frame of reference of original query */
552 agglevelsup -= context->sublevels_up;
553 /* ignore local aggs of subqueries */
554 if (agglevelsup >= 0)
556 if (context->min_varlevel < 0 ||
557 context->min_varlevel > agglevelsup)
559 context->min_varlevel = agglevelsup;
562 * As soon as we find a local aggregate, we can abort the tree
563 * traversal, since min_varlevel is then certainly 0.
565 if (agglevelsup == 0)
570 /* Likewise, make sure PlaceHolderVar is treated correctly */
571 if (IsA(node, PlaceHolderVar))
573 int phlevelsup = ((PlaceHolderVar *) node)->phlevelsup;
575 /* convert levelsup to frame of reference of original query */
576 phlevelsup -= context->sublevels_up;
577 /* ignore local vars of subqueries */
580 if (context->min_varlevel < 0 ||
581 context->min_varlevel > phlevelsup)
583 context->min_varlevel = phlevelsup;
586 * As soon as we find a local variable, we can abort the tree
587 * traversal, since min_varlevel is then certainly 0.
594 if (IsA(node, Query))
596 /* Recurse into subselects */
599 context->sublevels_up++;
600 result = query_tree_walker((Query *) node,
601 find_minimum_var_level_walker,
604 context->sublevels_up--;
607 return expression_tree_walker(node,
608 find_minimum_var_level_walker,
615 * Recursively pulls all Var nodes from an expression clause.
617 * PlaceHolderVars are included too, if includePlaceHolderVars is true.
618 * If it isn't true, an error is thrown if any are found.
619 * Note that Vars within a PHV's expression are *not* included.
621 * CurrentOfExpr nodes are *not* included.
623 * Upper-level vars (with varlevelsup > 0) are not included.
624 * (These probably represent errors too, but we don't complain.)
626 * Returns list of nodes found. Note the nodes themselves are not
627 * copied, only referenced.
629 * Does not examine subqueries, therefore must only be used after reduction
630 * of sublinks to subplans!
633 pull_var_clause(Node *node, bool includePlaceHolderVars)
635 pull_var_clause_context context;
637 context.varlist = NIL;
638 context.includePlaceHolderVars = includePlaceHolderVars;
640 pull_var_clause_walker(node, &context);
641 return context.varlist;
645 pull_var_clause_walker(Node *node, pull_var_clause_context *context)
651 if (((Var *) node)->varlevelsup == 0)
652 context->varlist = lappend(context->varlist, node);
655 if (IsA(node, PlaceHolderVar))
657 if (!context->includePlaceHolderVars)
658 elog(ERROR, "PlaceHolderVar found where not expected");
659 if (((PlaceHolderVar *) node)->phlevelsup == 0)
660 context->varlist = lappend(context->varlist, node);
661 /* we do NOT descend into the contained expression */
664 return expression_tree_walker(node, pull_var_clause_walker,
670 * flatten_join_alias_vars
671 * Replace Vars that reference JOIN outputs with references to the original
672 * relation variables instead. This allows quals involving such vars to be
673 * pushed down. Whole-row Vars that reference JOIN relations are expanded
674 * into RowExpr constructs that name the individual output Vars. This
675 * is necessary since we will not scan the JOIN as a base relation, which
676 * is the only way that the executor can directly handle whole-row Vars.
678 * This also adjusts relid sets found in some expression node types to
679 * substitute the contained base rels for any join relid.
681 * NOTE: this is used on not-yet-planned expressions. We do not expect it
682 * to be applied directly to a Query node.
685 flatten_join_alias_vars(PlannerInfo *root, Node *node)
687 flatten_join_alias_vars_context context;
690 context.sublevels_up = 0;
692 return flatten_join_alias_vars_mutator(node, &context);
696 flatten_join_alias_vars_mutator(Node *node,
697 flatten_join_alias_vars_context *context)
703 Var *var = (Var *) node;
707 /* No change unless Var belongs to a JOIN of the target level */
708 if (var->varlevelsup != context->sublevels_up)
709 return node; /* no need to copy, really */
710 rte = rt_fetch(var->varno, context->root->parse->rtable);
711 if (rte->rtekind != RTE_JOIN)
713 if (var->varattno == InvalidAttrNumber)
715 /* Must expand whole-row reference */
722 foreach(l, rte->joinaliasvars)
724 newvar = (Node *) lfirst(l);
726 /* Ignore dropped columns */
727 if (IsA(newvar, Const))
731 * If we are expanding an alias carried down from an upper
732 * query, must adjust its varlevelsup fields.
734 if (context->sublevels_up != 0)
736 newvar = copyObject(newvar);
737 IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
739 /* Recurse in case join input is itself a join */
740 newvar = flatten_join_alias_vars_mutator(newvar, context);
741 fields = lappend(fields, newvar);
743 rowexpr = makeNode(RowExpr);
744 rowexpr->args = fields;
745 rowexpr->row_typeid = var->vartype;
746 rowexpr->row_format = COERCE_IMPLICIT_CAST;
747 rowexpr->colnames = NIL;
748 rowexpr->location = -1;
750 return (Node *) rowexpr;
753 /* Expand join alias reference */
754 Assert(var->varattno > 0);
755 newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
758 * If we are expanding an alias carried down from an upper query, must
759 * adjust its varlevelsup fields.
761 if (context->sublevels_up != 0)
763 newvar = copyObject(newvar);
764 IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
766 /* Recurse in case join input is itself a join */
767 return flatten_join_alias_vars_mutator(newvar, context);
769 if (IsA(node, FlattenedSubLink))
771 /* Copy the FlattenedSubLink node with correct mutation of subnodes */
772 FlattenedSubLink *fslink;
774 fslink = (FlattenedSubLink *) expression_tree_mutator(node,
775 flatten_join_alias_vars_mutator,
777 /* now fix FlattenedSubLink's relid sets */
778 if (context->sublevels_up == 0)
780 fslink->lefthand = alias_relid_set(context->root,
782 fslink->righthand = alias_relid_set(context->root,
785 return (Node *) fslink;
787 if (IsA(node, PlaceHolderVar))
789 /* Copy the PlaceHolderVar node with correct mutation of subnodes */
792 phv = (PlaceHolderVar *) expression_tree_mutator(node,
793 flatten_join_alias_vars_mutator,
795 /* now fix PlaceHolderVar's relid sets */
796 if (phv->phlevelsup == context->sublevels_up)
798 phv->phrels = alias_relid_set(context->root,
803 if (IsA(node, PlaceHolderInfo))
805 /* Copy the PlaceHolderInfo node with correct mutation of subnodes */
806 PlaceHolderInfo *phinfo;
808 phinfo = (PlaceHolderInfo *) expression_tree_mutator(node,
809 flatten_join_alias_vars_mutator,
811 /* now fix PlaceHolderInfo's relid sets */
812 if (context->sublevels_up == 0)
814 phinfo->ph_eval_at = alias_relid_set(context->root,
816 phinfo->ph_needed = alias_relid_set(context->root,
819 return (Node *) phinfo;
822 if (IsA(node, Query))
824 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
827 context->sublevels_up++;
828 newnode = query_tree_mutator((Query *) node,
829 flatten_join_alias_vars_mutator,
831 QTW_IGNORE_JOINALIASES);
832 context->sublevels_up--;
833 return (Node *) newnode;
835 /* Already-planned tree not supported */
836 Assert(!IsA(node, SubPlan));
838 return expression_tree_mutator(node, flatten_join_alias_vars_mutator,
843 * alias_relid_set: in a set of RT indexes, replace joins by their
844 * underlying base relids
847 alias_relid_set(PlannerInfo *root, Relids relids)
849 Relids result = NULL;
853 tmprelids = bms_copy(relids);
854 while ((rtindex = bms_first_member(tmprelids)) >= 0)
856 RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
858 if (rte->rtekind == RTE_JOIN)
859 result = bms_join(result, get_relids_for_join(root, rtindex));
861 result = bms_add_member(result, rtindex);