PostgreSQL Source Code  git master
var.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * var.c
4  * Var node manipulation routines
5  *
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
9  * contains variables.
10  *
11  *
12  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  *
16  * IDENTIFICATION
17  * src/backend/optimizer/util/var.c
18  *
19  *-------------------------------------------------------------------------
20  */
21 #include "postgres.h"
22 
23 #include "access/sysattr.h"
24 #include "nodes/nodeFuncs.h"
25 #include "optimizer/optimizer.h"
26 #include "optimizer/placeholder.h"
27 #include "optimizer/prep.h"
28 #include "parser/parsetree.h"
29 #include "rewrite/rewriteManip.h"
30 
31 
32 typedef struct
33 {
38 
39 typedef struct
40 {
44 
45 typedef struct
46 {
50 
51 typedef struct
52 {
56 
57 typedef struct
58 {
60  int flags;
62 
63 typedef struct
64 {
65  Query *query; /* outer Query */
67  bool possible_sublink; /* could aliases include a SubLink? */
68  bool inserted_sublink; /* have we inserted a SubLink? */
70 
71 static bool pull_varnos_walker(Node *node,
72  pull_varnos_context *context);
73 static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context);
74 static bool pull_vars_walker(Node *node, pull_vars_context *context);
75 static bool contain_var_clause_walker(Node *node, void *context);
76 static bool contain_vars_of_level_walker(Node *node, int *sublevels_up);
77 static bool locate_var_of_level_walker(Node *node,
79 static bool pull_var_clause_walker(Node *node,
80  pull_var_clause_context *context);
83 static Relids alias_relid_set(Query *query, Relids relids);
84 
85 
86 /*
87  * pull_varnos
88  * Create a set of all the distinct varnos present in a parsetree.
89  * Only varnos that reference level-zero rtable entries are considered.
90  *
91  * "root" can be passed as NULL if it is not necessary to process
92  * PlaceHolderVars.
93  *
94  * NOTE: this is used on not-yet-planned expressions. It may therefore find
95  * bare SubLinks, and if so it needs to recurse into them to look for uplevel
96  * references to the desired rtable level! But when we find a completed
97  * SubPlan, we only need to look at the parameters passed to the subplan.
98  */
99 Relids
101 {
102  pull_varnos_context context;
103 
104  context.varnos = NULL;
105  context.root = root;
106  context.sublevels_up = 0;
107 
108  /*
109  * Must be prepared to start with a Query or a bare expression tree; if
110  * it's a Query, we don't want to increment sublevels_up.
111  */
114  (void *) &context,
115  0);
116 
117  return context.varnos;
118 }
119 
120 /*
121  * pull_varnos_of_level
122  * Create a set of all the distinct varnos present in a parsetree.
123  * Only Vars of the specified level are considered.
124  */
125 Relids
126 pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
127 {
128  pull_varnos_context context;
129 
130  context.varnos = NULL;
131  context.root = root;
132  context.sublevels_up = levelsup;
133 
134  /*
135  * Must be prepared to start with a Query or a bare expression tree; if
136  * it's a Query, we don't want to increment sublevels_up.
137  */
140  (void *) &context,
141  0);
142 
143  return context.varnos;
144 }
145 
146 static bool
148 {
149  if (node == NULL)
150  return false;
151  if (IsA(node, Var))
152  {
153  Var *var = (Var *) node;
154 
155  if (var->varlevelsup == context->sublevels_up)
156  context->varnos = bms_add_member(context->varnos, var->varno);
157  return false;
158  }
159  if (IsA(node, CurrentOfExpr))
160  {
161  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
162 
163  if (context->sublevels_up == 0)
164  context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
165  return false;
166  }
167  if (IsA(node, PlaceHolderVar))
168  {
169  PlaceHolderVar *phv = (PlaceHolderVar *) node;
170 
171  /*
172  * If a PlaceHolderVar is not of the target query level, ignore it,
173  * instead recursing into its expression to see if it contains any
174  * vars that are of the target level. We'll also do that when the
175  * caller doesn't pass a "root" pointer. (We probably shouldn't see
176  * PlaceHolderVars at all in such cases, but if we do, this is a
177  * reasonable behavior.)
178  */
179  if (phv->phlevelsup == context->sublevels_up &&
180  context->root != NULL)
181  {
182  /*
183  * Ideally, the PHV's contribution to context->varnos is its
184  * ph_eval_at set. However, this code can be invoked before
185  * that's been computed. If we cannot find a PlaceHolderInfo,
186  * fall back to the conservative assumption that the PHV will be
187  * evaluated at its syntactic level (phv->phrels).
188  *
189  * There is a second hazard: this code is also used to examine
190  * qual clauses during deconstruct_jointree, when we may have a
191  * PlaceHolderInfo but its ph_eval_at value is not yet final, so
192  * that theoretically we could obtain a relid set that's smaller
193  * than we'd see later on. That should never happen though,
194  * because we deconstruct the jointree working upwards. Any outer
195  * join that forces delay of evaluation of a given qual clause
196  * will be processed before we examine that clause here, so the
197  * ph_eval_at value should have been updated to include it.
198  *
199  * Another problem is that a PlaceHolderVar can appear in quals or
200  * tlists that have been translated for use in a child appendrel.
201  * Typically such a PHV is a parameter expression sourced by some
202  * other relation, so that the translation from parent appendrel
203  * to child doesn't change its phrels, and we should still take
204  * ph_eval_at at face value. But in corner cases, the PHV's
205  * original phrels can include the parent appendrel itself, in
206  * which case the translated PHV will have the child appendrel in
207  * phrels, and we must translate ph_eval_at to match.
208  */
209  PlaceHolderInfo *phinfo = NULL;
210 
211  if (phv->phlevelsup == 0)
212  {
213  ListCell *lc;
214 
215  foreach(lc, context->root->placeholder_list)
216  {
217  phinfo = (PlaceHolderInfo *) lfirst(lc);
218  if (phinfo->phid == phv->phid)
219  break;
220  phinfo = NULL;
221  }
222  }
223  if (phinfo == NULL)
224  {
225  /* No PlaceHolderInfo yet, use phrels */
226  context->varnos = bms_add_members(context->varnos,
227  phv->phrels);
228  }
229  else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
230  {
231  /* Normal case: use ph_eval_at */
232  context->varnos = bms_add_members(context->varnos,
233  phinfo->ph_eval_at);
234  }
235  else
236  {
237  /* Translated PlaceHolderVar: translate ph_eval_at to match */
238  Relids newevalat,
239  delta;
240 
241  /* remove what was removed from phv->phrels ... */
242  delta = bms_difference(phinfo->ph_var->phrels, phv->phrels);
243  newevalat = bms_difference(phinfo->ph_eval_at, delta);
244  /* ... then if that was in fact part of ph_eval_at ... */
245  if (!bms_equal(newevalat, phinfo->ph_eval_at))
246  {
247  /* ... add what was added */
248  delta = bms_difference(phv->phrels, phinfo->ph_var->phrels);
249  newevalat = bms_join(newevalat, delta);
250  }
251  context->varnos = bms_join(context->varnos,
252  newevalat);
253  }
254  return false; /* don't recurse into expression */
255  }
256  }
257  else if (IsA(node, Query))
258  {
259  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
260  bool result;
261 
262  context->sublevels_up++;
263  result = query_tree_walker((Query *) node, pull_varnos_walker,
264  (void *) context, 0);
265  context->sublevels_up--;
266  return result;
267  }
269  (void *) context);
270 }
271 
272 
273 /*
274  * pull_varattnos
275  * Find all the distinct attribute numbers present in an expression tree,
276  * and add them to the initial contents of *varattnos.
277  * Only Vars of the given varno and rtable level zero are considered.
278  *
279  * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
280  * we can include system attributes (e.g., OID) in the bitmap representation.
281  *
282  * Currently, this does not support unplanned subqueries; that is not needed
283  * for current uses. It will handle already-planned SubPlan nodes, though,
284  * looking into only the "testexpr" and the "args" list. (The subplan cannot
285  * contain any other references to Vars of the current level.)
286  */
287 void
288 pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
289 {
290  pull_varattnos_context context;
291 
292  context.varattnos = *varattnos;
293  context.varno = varno;
294 
295  (void) pull_varattnos_walker(node, &context);
296 
297  *varattnos = context.varattnos;
298 }
299 
300 static bool
302 {
303  if (node == NULL)
304  return false;
305  if (IsA(node, Var))
306  {
307  Var *var = (Var *) node;
308 
309  if (var->varno == context->varno && var->varlevelsup == 0)
310  context->varattnos =
311  bms_add_member(context->varattnos,
313  return false;
314  }
315 
316  /* Should not find an unplanned subquery */
317  Assert(!IsA(node, Query));
318 
320  (void *) context);
321 }
322 
323 
324 /*
325  * pull_vars_of_level
326  * Create a list of all Vars (and PlaceHolderVars) referencing the
327  * specified query level in the given parsetree.
328  *
329  * Caution: the Vars are not copied, only linked into the list.
330  */
331 List *
332 pull_vars_of_level(Node *node, int levelsup)
333 {
334  pull_vars_context context;
335 
336  context.vars = NIL;
337  context.sublevels_up = levelsup;
338 
339  /*
340  * Must be prepared to start with a Query or a bare expression tree; if
341  * it's a Query, we don't want to increment sublevels_up.
342  */
345  (void *) &context,
346  0);
347 
348  return context.vars;
349 }
350 
351 static bool
353 {
354  if (node == NULL)
355  return false;
356  if (IsA(node, Var))
357  {
358  Var *var = (Var *) node;
359 
360  if (var->varlevelsup == context->sublevels_up)
361  context->vars = lappend(context->vars, var);
362  return false;
363  }
364  if (IsA(node, PlaceHolderVar))
365  {
366  PlaceHolderVar *phv = (PlaceHolderVar *) node;
367 
368  if (phv->phlevelsup == context->sublevels_up)
369  context->vars = lappend(context->vars, phv);
370  /* we don't want to look into the contained expression */
371  return false;
372  }
373  if (IsA(node, Query))
374  {
375  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
376  bool result;
377 
378  context->sublevels_up++;
379  result = query_tree_walker((Query *) node, pull_vars_walker,
380  (void *) context, 0);
381  context->sublevels_up--;
382  return result;
383  }
385  (void *) context);
386 }
387 
388 
389 /*
390  * contain_var_clause
391  * Recursively scan a clause to discover whether it contains any Var nodes
392  * (of the current query level).
393  *
394  * Returns true if any varnode found.
395  *
396  * Does not examine subqueries, therefore must only be used after reduction
397  * of sublinks to subplans!
398  */
399 bool
401 {
402  return contain_var_clause_walker(node, NULL);
403 }
404 
405 static bool
406 contain_var_clause_walker(Node *node, void *context)
407 {
408  if (node == NULL)
409  return false;
410  if (IsA(node, Var))
411  {
412  if (((Var *) node)->varlevelsup == 0)
413  return true; /* abort the tree traversal and return true */
414  return false;
415  }
416  if (IsA(node, CurrentOfExpr))
417  return true;
418  if (IsA(node, PlaceHolderVar))
419  {
420  if (((PlaceHolderVar *) node)->phlevelsup == 0)
421  return true; /* abort the tree traversal and return true */
422  /* else fall through to check the contained expr */
423  }
424  return expression_tree_walker(node, contain_var_clause_walker, context);
425 }
426 
427 
428 /*
429  * contain_vars_of_level
430  * Recursively scan a clause to discover whether it contains any Var nodes
431  * of the specified query level.
432  *
433  * Returns true if any such Var found.
434  *
435  * Will recurse into sublinks. Also, may be invoked directly on a Query.
436  */
437 bool
438 contain_vars_of_level(Node *node, int levelsup)
439 {
440  int sublevels_up = levelsup;
441 
444  (void *) &sublevels_up,
445  0);
446 }
447 
448 static bool
449 contain_vars_of_level_walker(Node *node, int *sublevels_up)
450 {
451  if (node == NULL)
452  return false;
453  if (IsA(node, Var))
454  {
455  if (((Var *) node)->varlevelsup == *sublevels_up)
456  return true; /* abort tree traversal and return true */
457  return false;
458  }
459  if (IsA(node, CurrentOfExpr))
460  {
461  if (*sublevels_up == 0)
462  return true;
463  return false;
464  }
465  if (IsA(node, PlaceHolderVar))
466  {
467  if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
468  return true; /* abort the tree traversal and return true */
469  /* else fall through to check the contained expr */
470  }
471  if (IsA(node, Query))
472  {
473  /* Recurse into subselects */
474  bool result;
475 
476  (*sublevels_up)++;
477  result = query_tree_walker((Query *) node,
479  (void *) sublevels_up,
480  0);
481  (*sublevels_up)--;
482  return result;
483  }
484  return expression_tree_walker(node,
486  (void *) sublevels_up);
487 }
488 
489 
490 /*
491  * locate_var_of_level
492  * Find the parse location of any Var of the specified query level.
493  *
494  * Returns -1 if no such Var is in the querytree, or if they all have
495  * unknown parse location. (The former case is probably caller error,
496  * but we don't bother to distinguish it from the latter case.)
497  *
498  * Will recurse into sublinks. Also, may be invoked directly on a Query.
499  *
500  * Note: it might seem appropriate to merge this functionality into
501  * contain_vars_of_level, but that would complicate that function's API.
502  * Currently, the only uses of this function are for error reporting,
503  * and so shaving cycles probably isn't very important.
504  */
505 int
506 locate_var_of_level(Node *node, int levelsup)
507 {
509 
510  context.var_location = -1; /* in case we find nothing */
511  context.sublevels_up = levelsup;
512 
515  (void *) &context,
516  0);
517 
518  return context.var_location;
519 }
520 
521 static bool
524 {
525  if (node == NULL)
526  return false;
527  if (IsA(node, Var))
528  {
529  Var *var = (Var *) node;
530 
531  if (var->varlevelsup == context->sublevels_up &&
532  var->location >= 0)
533  {
534  context->var_location = var->location;
535  return true; /* abort tree traversal and return true */
536  }
537  return false;
538  }
539  if (IsA(node, CurrentOfExpr))
540  {
541  /* since CurrentOfExpr doesn't carry location, nothing we can do */
542  return false;
543  }
544  /* No extra code needed for PlaceHolderVar; just look in contained expr */
545  if (IsA(node, Query))
546  {
547  /* Recurse into subselects */
548  bool result;
549 
550  context->sublevels_up++;
551  result = query_tree_walker((Query *) node,
553  (void *) context,
554  0);
555  context->sublevels_up--;
556  return result;
557  }
558  return expression_tree_walker(node,
560  (void *) context);
561 }
562 
563 
564 /*
565  * pull_var_clause
566  * Recursively pulls all Var nodes from an expression clause.
567  *
568  * Aggrefs are handled according to these bits in 'flags':
569  * PVC_INCLUDE_AGGREGATES include Aggrefs in output list
570  * PVC_RECURSE_AGGREGATES recurse into Aggref arguments
571  * neither flag throw error if Aggref found
572  * Vars within an Aggref's expression are included in the result only
573  * when PVC_RECURSE_AGGREGATES is specified.
574  *
575  * WindowFuncs are handled according to these bits in 'flags':
576  * PVC_INCLUDE_WINDOWFUNCS include WindowFuncs in output list
577  * PVC_RECURSE_WINDOWFUNCS recurse into WindowFunc arguments
578  * neither flag throw error if WindowFunc found
579  * Vars within a WindowFunc's expression are included in the result only
580  * when PVC_RECURSE_WINDOWFUNCS is specified.
581  *
582  * PlaceHolderVars are handled according to these bits in 'flags':
583  * PVC_INCLUDE_PLACEHOLDERS include PlaceHolderVars in output list
584  * PVC_RECURSE_PLACEHOLDERS recurse into PlaceHolderVar arguments
585  * neither flag throw error if PlaceHolderVar found
586  * Vars within a PHV's expression are included in the result only
587  * when PVC_RECURSE_PLACEHOLDERS is specified.
588  *
589  * GroupingFuncs are treated exactly like Aggrefs, and so do not need
590  * their own flag bits.
591  *
592  * CurrentOfExpr nodes are ignored in all cases.
593  *
594  * Upper-level vars (with varlevelsup > 0) should not be seen here,
595  * likewise for upper-level Aggrefs and PlaceHolderVars.
596  *
597  * Returns list of nodes found. Note the nodes themselves are not
598  * copied, only referenced.
599  *
600  * Does not examine subqueries, therefore must only be used after reduction
601  * of sublinks to subplans!
602  */
603 List *
604 pull_var_clause(Node *node, int flags)
605 {
606  pull_var_clause_context context;
607 
608  /* Assert that caller has not specified inconsistent flags */
615 
616  context.varlist = NIL;
617  context.flags = flags;
618 
619  pull_var_clause_walker(node, &context);
620  return context.varlist;
621 }
622 
623 static bool
625 {
626  if (node == NULL)
627  return false;
628  if (IsA(node, Var))
629  {
630  if (((Var *) node)->varlevelsup != 0)
631  elog(ERROR, "Upper-level Var found where not expected");
632  context->varlist = lappend(context->varlist, node);
633  return false;
634  }
635  else if (IsA(node, Aggref))
636  {
637  if (((Aggref *) node)->agglevelsup != 0)
638  elog(ERROR, "Upper-level Aggref found where not expected");
639  if (context->flags & PVC_INCLUDE_AGGREGATES)
640  {
641  context->varlist = lappend(context->varlist, node);
642  /* we do NOT descend into the contained expression */
643  return false;
644  }
645  else if (context->flags & PVC_RECURSE_AGGREGATES)
646  {
647  /* fall through to recurse into the aggregate's arguments */
648  }
649  else
650  elog(ERROR, "Aggref found where not expected");
651  }
652  else if (IsA(node, GroupingFunc))
653  {
654  if (((GroupingFunc *) node)->agglevelsup != 0)
655  elog(ERROR, "Upper-level GROUPING found where not expected");
656  if (context->flags & PVC_INCLUDE_AGGREGATES)
657  {
658  context->varlist = lappend(context->varlist, node);
659  /* we do NOT descend into the contained expression */
660  return false;
661  }
662  else if (context->flags & PVC_RECURSE_AGGREGATES)
663  {
664  /* fall through to recurse into the GroupingFunc's arguments */
665  }
666  else
667  elog(ERROR, "GROUPING found where not expected");
668  }
669  else if (IsA(node, WindowFunc))
670  {
671  /* WindowFuncs have no levelsup field to check ... */
672  if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
673  {
674  context->varlist = lappend(context->varlist, node);
675  /* we do NOT descend into the contained expressions */
676  return false;
677  }
678  else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
679  {
680  /* fall through to recurse into the windowfunc's arguments */
681  }
682  else
683  elog(ERROR, "WindowFunc found where not expected");
684  }
685  else if (IsA(node, PlaceHolderVar))
686  {
687  if (((PlaceHolderVar *) node)->phlevelsup != 0)
688  elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
689  if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
690  {
691  context->varlist = lappend(context->varlist, node);
692  /* we do NOT descend into the contained expression */
693  return false;
694  }
695  else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
696  {
697  /* fall through to recurse into the placeholder's expression */
698  }
699  else
700  elog(ERROR, "PlaceHolderVar found where not expected");
701  }
703  (void *) context);
704 }
705 
706 
707 /*
708  * flatten_join_alias_vars
709  * Replace Vars that reference JOIN outputs with references to the original
710  * relation variables instead. This allows quals involving such vars to be
711  * pushed down. Whole-row Vars that reference JOIN relations are expanded
712  * into RowExpr constructs that name the individual output Vars. This
713  * is necessary since we will not scan the JOIN as a base relation, which
714  * is the only way that the executor can directly handle whole-row Vars.
715  *
716  * This also adjusts relid sets found in some expression node types to
717  * substitute the contained base rels for any join relid.
718  *
719  * If a JOIN contains sub-selects that have been flattened, its join alias
720  * entries might now be arbitrary expressions, not just Vars. This affects
721  * this function in one important way: we might find ourselves inserting
722  * SubLink expressions into subqueries, and we must make sure that their
723  * Query.hasSubLinks fields get set to true if so. If there are any
724  * SubLinks in the join alias lists, the outer Query should already have
725  * hasSubLinks = true, so this is only relevant to un-flattened subqueries.
726  *
727  * NOTE: this is used on not-yet-planned expressions. We do not expect it
728  * to be applied directly to the whole Query, so if we see a Query to start
729  * with, we do want to increment sublevels_up (this occurs for LATERAL
730  * subqueries).
731  */
732 Node *
734 {
736 
737  context.query = query;
738  context.sublevels_up = 0;
739  /* flag whether join aliases could possibly contain SubLinks */
740  context.possible_sublink = query->hasSubLinks;
741  /* if hasSubLinks is already true, no need to work hard */
742  context.inserted_sublink = query->hasSubLinks;
743 
744  return flatten_join_alias_vars_mutator(node, &context);
745 }
746 
747 static Node *
750 {
751  if (node == NULL)
752  return NULL;
753  if (IsA(node, Var))
754  {
755  Var *var = (Var *) node;
756  RangeTblEntry *rte;
757  Node *newvar;
758 
759  /* No change unless Var belongs to a JOIN of the target level */
760  if (var->varlevelsup != context->sublevels_up)
761  return node; /* no need to copy, really */
762  rte = rt_fetch(var->varno, context->query->rtable);
763  if (rte->rtekind != RTE_JOIN)
764  return node;
765  if (var->varattno == InvalidAttrNumber)
766  {
767  /* Must expand whole-row reference */
768  RowExpr *rowexpr;
769  List *fields = NIL;
770  List *colnames = NIL;
771  ListCell *lv;
772  ListCell *ln;
773 
775  forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
776  {
777  newvar = (Node *) lfirst(lv);
778  /* Ignore dropped columns */
779  if (newvar == NULL)
780  continue;
781  newvar = copyObject(newvar);
782 
783  /*
784  * If we are expanding an alias carried down from an upper
785  * query, must adjust its varlevelsup fields.
786  */
787  if (context->sublevels_up != 0)
788  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
789  /* Preserve original Var's location, if possible */
790  if (IsA(newvar, Var))
791  ((Var *) newvar)->location = var->location;
792  /* Recurse in case join input is itself a join */
793  /* (also takes care of setting inserted_sublink if needed) */
794  newvar = flatten_join_alias_vars_mutator(newvar, context);
795  fields = lappend(fields, newvar);
796  /* We need the names of non-dropped columns, too */
797  colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
798  }
799  rowexpr = makeNode(RowExpr);
800  rowexpr->args = fields;
801  rowexpr->row_typeid = var->vartype;
802  rowexpr->row_format = COERCE_IMPLICIT_CAST;
803  /* vartype will always be RECORDOID, so we always need colnames */
804  rowexpr->colnames = colnames;
805  rowexpr->location = var->location;
806 
807  return (Node *) rowexpr;
808  }
809 
810  /* Expand join alias reference */
811  Assert(var->varattno > 0);
812  newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
813  Assert(newvar != NULL);
814  newvar = copyObject(newvar);
815 
816  /*
817  * If we are expanding an alias carried down from an upper query, must
818  * adjust its varlevelsup fields.
819  */
820  if (context->sublevels_up != 0)
821  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
822 
823  /* Preserve original Var's location, if possible */
824  if (IsA(newvar, Var))
825  ((Var *) newvar)->location = var->location;
826 
827  /* Recurse in case join input is itself a join */
828  newvar = flatten_join_alias_vars_mutator(newvar, context);
829 
830  /* Detect if we are adding a sublink to query */
831  if (context->possible_sublink && !context->inserted_sublink)
832  context->inserted_sublink = checkExprHasSubLink(newvar);
833 
834  return newvar;
835  }
836  if (IsA(node, PlaceHolderVar))
837  {
838  /* Copy the PlaceHolderVar node with correct mutation of subnodes */
839  PlaceHolderVar *phv;
840 
843  (void *) context);
844  /* now fix PlaceHolderVar's relid sets */
845  if (phv->phlevelsup == context->sublevels_up)
846  {
847  phv->phrels = alias_relid_set(context->query,
848  phv->phrels);
849  }
850  return (Node *) phv;
851  }
852 
853  if (IsA(node, Query))
854  {
855  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
856  Query *newnode;
857  bool save_inserted_sublink;
858 
859  context->sublevels_up++;
860  save_inserted_sublink = context->inserted_sublink;
861  context->inserted_sublink = ((Query *) node)->hasSubLinks;
862  newnode = query_tree_mutator((Query *) node,
864  (void *) context,
866  newnode->hasSubLinks |= context->inserted_sublink;
867  context->inserted_sublink = save_inserted_sublink;
868  context->sublevels_up--;
869  return (Node *) newnode;
870  }
871  /* Already-planned tree not supported */
872  Assert(!IsA(node, SubPlan));
873  /* Shouldn't need to handle these planner auxiliary nodes here */
874  Assert(!IsA(node, SpecialJoinInfo));
875  Assert(!IsA(node, PlaceHolderInfo));
876  Assert(!IsA(node, MinMaxAggInfo));
877 
879  (void *) context);
880 }
881 
882 /*
883  * alias_relid_set: in a set of RT indexes, replace joins by their
884  * underlying base relids
885  */
886 static Relids
887 alias_relid_set(Query *query, Relids relids)
888 {
889  Relids result = NULL;
890  int rtindex;
891 
892  rtindex = -1;
893  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
894  {
895  RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
896 
897  if (rte->rtekind == RTE_JOIN)
898  result = bms_join(result, get_relids_for_join(query, rtindex));
899  else
900  result = bms_add_member(result, rtindex);
901  }
902  return result;
903 }
#define InvalidAttrNumber
Definition: attnum.h:23
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:951
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1045
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:795
unsigned int Index
Definition: c.h:549
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:336
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3810
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2570
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3648
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:2015
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2815
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
#define copyObject(obj)
Definition: nodes.h:689
#define makeNode(_type_)
Definition: nodes.h:621
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:189
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:193
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:191
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:190
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:192
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:188
@ RTE_JOIN
Definition: parsenodes.h:1000
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
Relids get_relids_for_join(Query *query, int joinrelid)
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:494
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
List * colnames
Definition: primnodes.h:43
Definition: pg_list.h:51
Definition: nodes.h:574
Relids ph_eval_at
Definition: pathnodes.h:2433
PlaceHolderVar * ph_var
Definition: pathnodes.h:2432
Index phlevelsup
Definition: pathnodes.h:2204
List * placeholder_list
Definition: pathnodes.h:291
List * rtable
Definition: parsenodes.h:148
bool hasSubLinks
Definition: parsenodes.h:137
Alias * eref
Definition: parsenodes.h:1161
List * joinaliasvars
Definition: parsenodes.h:1090
RTEKind rtekind
Definition: parsenodes.h:1015
Oid row_typeid
Definition: primnodes.h:1076
int location
Definition: primnodes.h:1092
List * args
Definition: primnodes.h:1075
CoercionForm row_format
Definition: primnodes.h:1090
List * colnames
Definition: primnodes.h:1091
Definition: primnodes.h:196
Oid vartype
Definition: primnodes.h:202
AttrNumber varattno
Definition: primnodes.h:200
int varno
Definition: primnodes.h:198
Index varlevelsup
Definition: primnodes.h:205
int location
Definition: primnodes.h:210
Bitmapset * varattnos
Definition: var.c:41
PlannerInfo * root
Definition: var.c:35
int sublevels_up
Definition: var.c:36
Relids varnos
Definition: var.c:34
int sublevels_up
Definition: var.c:48
List * vars
Definition: var.c:47
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context)
Definition: var.c:301
List * pull_vars_of_level(Node *node, int levelsup)
Definition: var.c:332
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:438
List * pull_var_clause(Node *node, int flags)
Definition: var.c:604
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:748
bool contain_var_clause(Node *node)
Definition: var.c:400
Relids pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
Definition: var.c:126
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:100
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:147
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:624
static Relids alias_relid_set(Query *query, Relids relids)
Definition: var.c:887
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up)
Definition: var.c:449
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:288
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:522
int locate_var_of_level(Node *node, int levelsup)
Definition: var.c:506
Node * flatten_join_alias_vars(Query *query, Node *node)
Definition: var.c:733
static bool contain_var_clause_walker(Node *node, void *context)
Definition: var.c:406
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:352