Age Owner Branch data TLA Line data Source code
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-2026, 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/clauses.h"
26 : : #include "optimizer/optimizer.h"
27 : : #include "optimizer/placeholder.h"
28 : : #include "optimizer/prep.h"
29 : : #include "parser/parsetree.h"
30 : : #include "rewrite/rewriteManip.h"
31 : :
32 : :
33 : : typedef struct
34 : : {
35 : : Relids varnos;
36 : : PlannerInfo *root;
37 : : int sublevels_up;
38 : : } pull_varnos_context;
39 : :
40 : : typedef struct
41 : : {
42 : : Bitmapset *varattnos;
43 : : Index varno;
44 : : } pull_varattnos_context;
45 : :
46 : : typedef struct
47 : : {
48 : : List *vars;
49 : : int sublevels_up;
50 : : } pull_vars_context;
51 : :
52 : : typedef struct
53 : : {
54 : : int var_location;
55 : : int sublevels_up;
56 : : } locate_var_of_level_context;
57 : :
58 : : typedef struct
59 : : {
60 : : List *varlist;
61 : : int flags;
62 : : } pull_var_clause_context;
63 : :
64 : : typedef struct
65 : : {
66 : : PlannerInfo *root; /* could be NULL! */
67 : : Query *query; /* outer Query */
68 : : int sublevels_up;
69 : : bool possible_sublink; /* could aliases include a SubLink? */
70 : : bool inserted_sublink; /* have we inserted a SubLink? */
71 : : } flatten_join_alias_vars_context;
72 : :
73 : : static bool pull_varnos_walker(Node *node,
74 : : pull_varnos_context *context);
75 : : static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context);
76 : : static bool pull_vars_walker(Node *node, pull_vars_context *context);
77 : : static bool contain_var_clause_walker(Node *node, void *context);
78 : : static bool contain_vars_of_level_walker(Node *node, int *sublevels_up);
79 : : static bool contain_vars_returning_old_or_new_walker(Node *node, void *context);
80 : : static bool locate_var_of_level_walker(Node *node,
81 : : locate_var_of_level_context *context);
82 : : static bool pull_var_clause_walker(Node *node,
83 : : pull_var_clause_context *context);
84 : : static Node *flatten_join_alias_vars_mutator(Node *node,
85 : : flatten_join_alias_vars_context *context);
86 : : static Node *flatten_group_exprs_mutator(Node *node,
87 : : flatten_join_alias_vars_context *context);
88 : : static Node *mark_nullable_by_grouping(PlannerInfo *root, Node *newnode,
89 : : Var *oldvar);
90 : : static Node *add_nullingrels_if_needed(PlannerInfo *root, Node *newnode,
91 : : Var *oldvar);
92 : : static bool is_standard_join_alias_expression(Node *newnode, Var *oldvar);
93 : : static void adjust_standard_join_alias_expression(Node *newnode, Var *oldvar);
94 : : static Relids alias_relid_set(Query *query, Relids relids);
95 : :
96 : :
97 : : /*
98 : : * pull_varnos
99 : : * Create a set of all the distinct varnos present in a parsetree.
100 : : * Only varnos that reference level-zero rtable entries are considered.
101 : : *
102 : : * The result includes outer-join relids mentioned in Var.varnullingrels and
103 : : * PlaceHolderVar.phnullingrels fields in the parsetree.
104 : : *
105 : : * "root" can be passed as NULL if it is not necessary to process
106 : : * PlaceHolderVars.
107 : : *
108 : : * NOTE: this is used on not-yet-planned expressions. It may therefore find
109 : : * bare SubLinks, and if so it needs to recurse into them to look for uplevel
110 : : * references to the desired rtable level! But when we find a completed
111 : : * SubPlan, we only need to look at the parameters passed to the subplan.
112 : : */
113 : : Relids
2090 tgl@sss.pgh.pa.us 114 :CBC 2689822 : pull_varnos(PlannerInfo *root, Node *node)
115 : : {
116 : : pull_varnos_context context;
117 : :
8647 118 : 2689822 : context.varnos = NULL;
2090 119 : 2689822 : context.root = root;
9526 120 : 2689822 : context.sublevels_up = 0;
121 : :
122 : : /*
123 : : * Must be prepared to start with a Query or a bare expression tree; if
124 : : * it's a Query, we don't want to increment sublevels_up.
125 : : */
8669 126 : 2689822 : query_or_expression_tree_walker(node,
127 : : pull_varnos_walker,
128 : : &context,
129 : : 0);
130 : :
8647 131 : 2689822 : return context.varnos;
132 : : }
133 : :
134 : : /*
135 : : * pull_varnos_of_level
136 : : * Create a set of all the distinct varnos present in a parsetree.
137 : : * Only Vars of the specified level are considered.
138 : : */
139 : : Relids
2090 140 : 8321 : pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
141 : : {
142 : : pull_varnos_context context;
143 : :
5179 144 : 8321 : context.varnos = NULL;
2090 145 : 8321 : context.root = root;
5179 146 : 8321 : context.sublevels_up = levelsup;
147 : :
148 : : /*
149 : : * Must be prepared to start with a Query or a bare expression tree; if
150 : : * it's a Query, we don't want to increment sublevels_up.
151 : : */
152 : 8321 : query_or_expression_tree_walker(node,
153 : : pull_varnos_walker,
154 : : &context,
155 : : 0);
156 : :
157 : 8321 : return context.varnos;
158 : : }
159 : :
160 : : static bool
9526 161 : 4785009 : pull_varnos_walker(Node *node, pull_varnos_context *context)
162 : : {
9977 163 [ + + ]: 4785009 : if (node == NULL)
164 : 185384 : return false;
165 [ + + ]: 4599625 : if (IsA(node, Var))
166 : : {
9679 bruce@momjian.us 167 : 2306631 : Var *var = (Var *) node;
168 : :
8647 tgl@sss.pgh.pa.us 169 [ + + ]: 2306631 : if (var->varlevelsup == context->sublevels_up)
170 : : {
171 : 2295920 : context->varnos = bms_add_member(context->varnos, var->varno);
1351 172 : 2295920 : context->varnos = bms_add_members(context->varnos,
173 : 2295920 : var->varnullingrels);
174 : : }
9977 175 : 2306631 : return false;
176 : : }
7063 177 [ + + ]: 2292994 : if (IsA(node, CurrentOfExpr))
178 : : {
179 : 680 : CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
180 : :
181 [ + - ]: 680 : if (context->sublevels_up == 0)
182 : 680 : context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
183 : 680 : return false;
184 : : }
6565 185 [ + + ]: 2292314 : if (IsA(node, PlaceHolderVar))
186 : : {
187 : 5744 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
188 : :
189 : : /*
190 : : * If a PlaceHolderVar is not of the target query level, ignore it,
191 : : * instead recursing into its expression to see if it contains any
192 : : * vars that are of the target level. We'll also do that when the
193 : : * caller doesn't pass a "root" pointer. (We probably shouldn't see
194 : : * PlaceHolderVars at all in such cases, but if we do, this is a
195 : : * reasonable behavior.)
196 : : */
1737 197 [ + + ]: 5744 : if (phv->phlevelsup == context->sublevels_up &&
198 [ + - ]: 5724 : context->root != NULL)
199 : : {
200 : : /*
201 : : * Ideally, the PHV's contribution to context->varnos is its
202 : : * ph_eval_at set. However, this code can be invoked before
203 : : * that's been computed. If we cannot find a PlaceHolderInfo,
204 : : * fall back to the conservative assumption that the PHV will be
205 : : * evaluated at its syntactic level (phv->phrels).
206 : : *
207 : : * Another problem is that a PlaceHolderVar can appear in quals or
208 : : * tlists that have been translated for use in a child appendrel.
209 : : * Typically such a PHV is a parameter expression sourced by some
210 : : * other relation, so that the translation from parent appendrel
211 : : * to child doesn't change its phrels, and we should still take
212 : : * ph_eval_at at face value. But in corner cases, the PHV's
213 : : * original phrels can include the parent appendrel itself, in
214 : : * which case the translated PHV will have the child appendrel in
215 : : * phrels, and we must translate ph_eval_at to match.
216 : : */
2090 217 : 5724 : PlaceHolderInfo *phinfo = NULL;
218 : :
219 [ + + ]: 5724 : if (phv->phlevelsup == 0)
220 : : {
1517 221 [ + + ]: 5644 : if (phv->phid < context->root->placeholder_array_size)
222 : 4376 : phinfo = context->root->placeholder_array[phv->phid];
223 : : }
1851 224 [ + + ]: 5724 : if (phinfo == NULL)
225 : : {
226 : : /* No PlaceHolderInfo yet, use phrels */
227 : 1358 : context->varnos = bms_add_members(context->varnos,
228 : 1358 : phv->phrels);
229 : : }
230 [ + + ]: 4366 : else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
231 : : {
232 : : /* Normal case: use ph_eval_at */
2090 233 : 4006 : context->varnos = bms_add_members(context->varnos,
234 : 4006 : phinfo->ph_eval_at);
235 : : }
236 : : else
237 : : {
238 : : /* Translated PlaceHolderVar: translate ph_eval_at to match */
239 : : Relids newevalat,
240 : : delta;
241 : :
242 : : /* remove what was removed from phv->phrels ... */
1851 243 : 360 : delta = bms_difference(phinfo->ph_var->phrels, phv->phrels);
244 : 360 : newevalat = bms_difference(phinfo->ph_eval_at, delta);
245 : : /* ... then if that was in fact part of ph_eval_at ... */
246 [ + - ]: 360 : if (!bms_equal(newevalat, phinfo->ph_eval_at))
247 : : {
248 : : /* ... add what was added */
249 : 360 : delta = bms_difference(phv->phrels, phinfo->ph_var->phrels);
250 : 360 : newevalat = bms_join(newevalat, delta);
251 : : }
252 : 360 : context->varnos = bms_join(context->varnos,
253 : : newevalat);
254 : : }
255 : :
256 : : /*
257 : : * In all three cases, include phnullingrels in the result. We
258 : : * don't worry about possibly needing to translate it, because
259 : : * appendrels only translate varnos of baserels, not outer joins.
260 : : */
1351 261 : 11448 : context->varnos = bms_add_members(context->varnos,
262 : 5724 : phv->phnullingrels);
2090 263 : 5724 : return false; /* don't recurse into expression */
264 : : }
265 : : }
266 [ + + ]: 2286570 : else if (IsA(node, Query))
267 : : {
268 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
269 : : bool result;
270 : :
9526 271 : 5965 : context->sublevels_up++;
272 : 5965 : result = query_tree_walker((Query *) node, pull_varnos_walker,
273 : : context, 0);
274 : 5965 : context->sublevels_up--;
275 : 5965 : return result;
276 : : }
683 peter@eisentraut.org 277 : 2280625 : return expression_tree_walker(node, pull_varnos_walker, context);
278 : : }
279 : :
280 : :
281 : : /*
282 : : * pull_varattnos
283 : : * Find all the distinct attribute numbers present in an expression tree,
284 : : * and add them to the initial contents of *varattnos.
285 : : * Only Vars of the given varno and rtable level zero are considered.
286 : : *
287 : : * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
288 : : * we can include system attributes (e.g., OID) in the bitmap representation.
289 : : *
290 : : * Currently, this does not support unplanned subqueries; that is not needed
291 : : * for current uses. It will handle already-planned SubPlan nodes, though,
292 : : * looking into only the "testexpr" and the "args" list. (The subplan cannot
293 : : * contain any other references to Vars of the current level.)
294 : : */
295 : : void
5484 tgl@sss.pgh.pa.us 296 : 1262190 : pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
297 : : {
298 : : pull_varattnos_context context;
299 : :
300 : 1262190 : context.varattnos = *varattnos;
301 : 1262190 : context.varno = varno;
302 : :
303 : 1262190 : (void) pull_varattnos_walker(node, &context);
304 : :
305 : 1262190 : *varattnos = context.varattnos;
6962 306 : 1262190 : }
307 : :
308 : : static bool
5484 309 : 4218300 : pull_varattnos_walker(Node *node, pull_varattnos_context *context)
310 : : {
6962 311 [ + + ]: 4218300 : if (node == NULL)
312 : 54691 : return false;
313 [ + + ]: 4163609 : if (IsA(node, Var))
314 : : {
315 : 2397829 : Var *var = (Var *) node;
316 : :
5484 317 [ + + + - ]: 2397829 : if (var->varno == context->varno && var->varlevelsup == 0)
318 : 2397498 : context->varattnos =
319 : 2397498 : bms_add_member(context->varattnos,
3400 320 : 2397498 : var->varattno - FirstLowInvalidHeapAttributeNumber);
6962 321 : 2397829 : return false;
322 : : }
323 : :
324 : : /* Should not find an unplanned subquery */
325 [ - + ]: 1765780 : Assert(!IsA(node, Query));
326 : :
683 peter@eisentraut.org 327 : 1765780 : return expression_tree_walker(node, pull_varattnos_walker, context);
328 : : }
329 : :
330 : :
331 : : /*
332 : : * pull_vars_of_level
333 : : * Create a list of all Vars (and PlaceHolderVars) referencing the
334 : : * specified query level in the given parsetree.
335 : : *
336 : : * Caution: the Vars are not copied, only linked into the list.
337 : : */
338 : : List *
5179 tgl@sss.pgh.pa.us 339 : 6518 : pull_vars_of_level(Node *node, int levelsup)
340 : : {
341 : : pull_vars_context context;
342 : :
343 : 6518 : context.vars = NIL;
344 : 6518 : context.sublevels_up = levelsup;
345 : :
346 : : /*
347 : : * Must be prepared to start with a Query or a bare expression tree; if
348 : : * it's a Query, we don't want to increment sublevels_up.
349 : : */
350 : 6518 : query_or_expression_tree_walker(node,
351 : : pull_vars_walker,
352 : : &context,
353 : : 0);
354 : :
355 : 6518 : return context.vars;
356 : : }
357 : :
358 : : static bool
359 : 119455 : pull_vars_walker(Node *node, pull_vars_context *context)
360 : : {
361 [ + + ]: 119455 : if (node == NULL)
362 : 30670 : return false;
363 [ + + ]: 88785 : if (IsA(node, Var))
364 : : {
365 : 18538 : Var *var = (Var *) node;
366 : :
367 [ + + ]: 18538 : if (var->varlevelsup == context->sublevels_up)
368 : 9459 : context->vars = lappend(context->vars, var);
369 : 18538 : return false;
370 : : }
5168 371 [ + + ]: 70247 : if (IsA(node, PlaceHolderVar))
372 : : {
373 : 243 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
374 : :
375 [ + - ]: 243 : if (phv->phlevelsup == context->sublevels_up)
376 : 243 : context->vars = lappend(context->vars, phv);
377 : : /* we don't want to look into the contained expression */
378 : 243 : return false;
379 : : }
5179 380 [ + + ]: 70004 : if (IsA(node, Query))
381 : : {
382 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
383 : : bool result;
384 : :
385 : 480 : context->sublevels_up++;
386 : 480 : result = query_tree_walker((Query *) node, pull_vars_walker,
387 : : context, 0);
388 : 480 : context->sublevels_up--;
389 : 480 : return result;
390 : : }
683 peter@eisentraut.org 391 : 69524 : return expression_tree_walker(node, pull_vars_walker, context);
392 : : }
393 : :
394 : :
395 : : /*
396 : : * contain_var_clause
397 : : * Recursively scan a clause to discover whether it contains any Var nodes
398 : : * (of the current query level).
399 : : *
400 : : * Returns true if any varnode found.
401 : : *
402 : : * Does not examine subqueries, therefore must only be used after reduction
403 : : * of sublinks to subplans!
404 : : */
405 : : bool
9308 tgl@sss.pgh.pa.us 406 : 26771 : contain_var_clause(Node *node)
407 : : {
408 : 26771 : return contain_var_clause_walker(node, NULL);
409 : : }
410 : :
411 : : static bool
9977 412 : 32678 : contain_var_clause_walker(Node *node, void *context)
413 : : {
414 [ + + ]: 32678 : if (node == NULL)
415 : 84 : return false;
416 [ + + ]: 32594 : if (IsA(node, Var))
417 : : {
9909 418 [ + - ]: 2239 : if (((Var *) node)->varlevelsup == 0)
7667 bruce@momjian.us 419 : 2239 : return true; /* abort the tree traversal and return true */
9909 tgl@sss.pgh.pa.us 420 :UBC 0 : return false;
421 : : }
7063 tgl@sss.pgh.pa.us 422 [ - + ]:CBC 30355 : if (IsA(node, CurrentOfExpr))
7063 tgl@sss.pgh.pa.us 423 :UBC 0 : return true;
6565 tgl@sss.pgh.pa.us 424 [ - + ]:CBC 30355 : if (IsA(node, PlaceHolderVar))
425 : : {
6565 tgl@sss.pgh.pa.us 426 [ # # ]:UBC 0 : if (((PlaceHolderVar *) node)->phlevelsup == 0)
427 : 0 : return true; /* abort the tree traversal and return true */
428 : : /* else fall through to check the contained expr */
429 : : }
9977 tgl@sss.pgh.pa.us 430 :CBC 30355 : return expression_tree_walker(node, contain_var_clause_walker, context);
431 : : }
432 : :
433 : :
434 : : /*
435 : : * contain_vars_of_level
436 : : * Recursively scan a clause to discover whether it contains any Var nodes
437 : : * of the specified query level.
438 : : *
439 : : * Returns true if any such Var found.
440 : : *
441 : : * Will recurse into sublinks. Also, may be invoked directly on a Query.
442 : : */
443 : : bool
8666 444 : 124988 : contain_vars_of_level(Node *node, int levelsup)
445 : : {
8470 bruce@momjian.us 446 : 124988 : int sublevels_up = levelsup;
447 : :
8666 tgl@sss.pgh.pa.us 448 : 124988 : return query_or_expression_tree_walker(node,
449 : : contain_vars_of_level_walker,
450 : : &sublevels_up,
451 : : 0);
452 : : }
453 : :
454 : : static bool
455 : 493541 : contain_vars_of_level_walker(Node *node, int *sublevels_up)
456 : : {
457 [ + + ]: 493541 : if (node == NULL)
458 : 145046 : return false;
459 [ + + ]: 348495 : if (IsA(node, Var))
460 : : {
461 [ + + ]: 61885 : if (((Var *) node)->varlevelsup == *sublevels_up)
462 : 37988 : return true; /* abort tree traversal and return true */
7063 463 : 23897 : return false;
464 : : }
465 [ + + ]: 286610 : if (IsA(node, CurrentOfExpr))
466 : : {
467 [ + - ]: 152 : if (*sublevels_up == 0)
468 : 152 : return true;
7063 tgl@sss.pgh.pa.us 469 :UBC 0 : return false;
470 : : }
6565 tgl@sss.pgh.pa.us 471 [ + + ]:CBC 286458 : if (IsA(node, PlaceHolderVar))
472 : : {
473 [ + + ]: 145 : if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
474 : 140 : return true; /* abort the tree traversal and return true */
475 : : /* else fall through to check the contained expr */
476 : : }
8666 477 [ + + ]: 286318 : if (IsA(node, Query))
478 : : {
479 : : /* Recurse into subselects */
480 : : bool result;
481 : :
482 : 554 : (*sublevels_up)++;
483 : 554 : result = query_tree_walker((Query *) node,
484 : : contain_vars_of_level_walker,
485 : : sublevels_up,
486 : : 0);
487 : 554 : (*sublevels_up)--;
488 : 554 : return result;
489 : : }
490 : 285764 : return expression_tree_walker(node,
491 : : contain_vars_of_level_walker,
492 : : sublevels_up);
493 : : }
494 : :
495 : :
496 : : /*
497 : : * contain_vars_returning_old_or_new
498 : : * Recursively scan a clause to discover whether it contains any Var nodes
499 : : * (of the current query level) whose varreturningtype is VAR_RETURNING_OLD
500 : : * or VAR_RETURNING_NEW.
501 : : *
502 : : * Returns true if any found.
503 : : *
504 : : * Any ReturningExprs are also detected --- if an OLD/NEW Var was rewritten,
505 : : * we still regard this as a clause that returns OLD/NEW values.
506 : : *
507 : : * Does not examine subqueries, therefore must only be used after reduction
508 : : * of sublinks to subplans!
509 : : */
510 : : bool
634 dean.a.rasheed@gmail 511 : 208 : contain_vars_returning_old_or_new(Node *node)
512 : : {
513 : 208 : return contain_vars_returning_old_or_new_walker(node, NULL);
514 : : }
515 : :
516 : : static bool
517 : 644 : contain_vars_returning_old_or_new_walker(Node *node, void *context)
518 : : {
519 [ + + ]: 644 : if (node == NULL)
520 : 146 : return false;
521 [ + + ]: 498 : if (IsA(node, Var))
522 : : {
523 [ + - ]: 212 : if (((Var *) node)->varlevelsup == 0 &&
524 [ + + ]: 212 : ((Var *) node)->varreturningtype != VAR_RETURNING_DEFAULT)
525 : 7 : return true; /* abort the tree traversal and return true */
526 : 205 : return false;
527 : : }
528 [ - + ]: 286 : if (IsA(node, ReturningExpr))
529 : : {
634 dean.a.rasheed@gmail 530 [ # # ]:UBC 0 : if (((ReturningExpr *) node)->retlevelsup == 0)
531 : 0 : return true; /* abort the tree traversal and return true */
532 : 0 : return false;
533 : : }
634 dean.a.rasheed@gmail 534 :CBC 286 : return expression_tree_walker(node, contain_vars_returning_old_or_new_walker,
535 : : context);
536 : : }
537 : :
538 : :
539 : : /*
540 : : * locate_var_of_level
541 : : * Find the parse location of any Var of the specified query level.
542 : : *
543 : : * Returns -1 if no such Var is in the querytree, or if they all have
544 : : * unknown parse location. (The former case is probably caller error,
545 : : * but we don't bother to distinguish it from the latter case.)
546 : : *
547 : : * Will recurse into sublinks. Also, may be invoked directly on a Query.
548 : : *
549 : : * Note: it might seem appropriate to merge this functionality into
550 : : * contain_vars_of_level, but that would complicate that function's API.
551 : : * Currently, the only uses of this function are for error reporting,
552 : : * and so shaving cycles probably isn't very important.
553 : : */
554 : : int
6615 tgl@sss.pgh.pa.us 555 : 8 : locate_var_of_level(Node *node, int levelsup)
556 : : {
557 : : locate_var_of_level_context context;
558 : :
6332 bruce@momjian.us 559 : 8 : context.var_location = -1; /* in case we find nothing */
6615 tgl@sss.pgh.pa.us 560 : 8 : context.sublevels_up = levelsup;
561 : :
562 : 8 : (void) query_or_expression_tree_walker(node,
563 : : locate_var_of_level_walker,
564 : : &context,
565 : : 0);
566 : :
567 : 8 : return context.var_location;
568 : : }
569 : :
570 : : static bool
571 : 20 : locate_var_of_level_walker(Node *node,
572 : : locate_var_of_level_context *context)
573 : : {
8666 574 [ - + ]: 20 : if (node == NULL)
8666 tgl@sss.pgh.pa.us 575 :UBC 0 : return false;
8666 tgl@sss.pgh.pa.us 576 [ + + ]:CBC 20 : if (IsA(node, Var))
577 : : {
6332 bruce@momjian.us 578 : 8 : Var *var = (Var *) node;
579 : :
6615 tgl@sss.pgh.pa.us 580 [ + - ]: 8 : if (var->varlevelsup == context->sublevels_up &&
581 [ + - ]: 8 : var->location >= 0)
582 : : {
583 : 8 : context->var_location = var->location;
8666 584 : 8 : return true; /* abort tree traversal and return true */
585 : : }
6615 tgl@sss.pgh.pa.us 586 :UBC 0 : return false;
587 : : }
6615 tgl@sss.pgh.pa.us 588 [ - + ]:CBC 12 : if (IsA(node, CurrentOfExpr))
589 : : {
590 : : /* since CurrentOfExpr doesn't carry location, nothing we can do */
6615 tgl@sss.pgh.pa.us 591 :UBC 0 : return false;
592 : : }
593 : : /* No extra code needed for PlaceHolderVar; just look in contained expr */
8666 tgl@sss.pgh.pa.us 594 [ - + ]:CBC 12 : if (IsA(node, Query))
595 : : {
596 : : /* Recurse into subselects */
597 : : bool result;
598 : :
6615 tgl@sss.pgh.pa.us 599 :UBC 0 : context->sublevels_up++;
8666 600 : 0 : result = query_tree_walker((Query *) node,
601 : : locate_var_of_level_walker,
602 : : context,
603 : : 0);
6615 604 : 0 : context->sublevels_up--;
8666 605 : 0 : return result;
606 : : }
8666 tgl@sss.pgh.pa.us 607 :CBC 12 : return expression_tree_walker(node,
608 : : locate_var_of_level_walker,
609 : : context);
610 : : }
611 : :
612 : :
613 : : /*
614 : : * pull_var_clause
615 : : * Recursively pulls all Var nodes from an expression clause.
616 : : *
617 : : * Aggrefs are handled according to these bits in 'flags':
618 : : * PVC_INCLUDE_AGGREGATES include Aggrefs in output list
619 : : * PVC_RECURSE_AGGREGATES recurse into Aggref arguments
620 : : * neither flag throw error if Aggref found
621 : : * Vars within an Aggref's expression are included in the result only
622 : : * when PVC_RECURSE_AGGREGATES is specified.
623 : : *
624 : : * WindowFuncs are handled according to these bits in 'flags':
625 : : * PVC_INCLUDE_WINDOWFUNCS include WindowFuncs in output list
626 : : * PVC_RECURSE_WINDOWFUNCS recurse into WindowFunc arguments
627 : : * neither flag throw error if WindowFunc found
628 : : * Vars within a WindowFunc's expression are included in the result only
629 : : * when PVC_RECURSE_WINDOWFUNCS is specified.
630 : : *
631 : : * PlaceHolderVars are handled according to these bits in 'flags':
632 : : * PVC_INCLUDE_PLACEHOLDERS include PlaceHolderVars in output list
633 : : * PVC_RECURSE_PLACEHOLDERS recurse into PlaceHolderVar arguments
634 : : * neither flag throw error if PlaceHolderVar found
635 : : * Vars within a PHV's expression are included in the result only
636 : : * when PVC_RECURSE_PLACEHOLDERS is specified.
637 : : *
638 : : * GroupingFuncs are treated exactly like Aggrefs, and so do not need
639 : : * their own flag bits.
640 : : *
641 : : * CurrentOfExpr nodes are ignored in all cases.
642 : : *
643 : : * Upper-level vars (with varlevelsup > 0) should not be seen here,
644 : : * likewise for upper-level Aggrefs and PlaceHolderVars.
645 : : *
646 : : * Returns list of nodes found. Note the nodes themselves are not
647 : : * copied, only referenced.
648 : : *
649 : : * Does not examine subqueries, therefore must only be used after reduction
650 : : * of sublinks to subplans!
651 : : */
652 : : List *
3868 653 : 673106 : pull_var_clause(Node *node, int flags)
654 : : {
655 : : pull_var_clause_context context;
656 : :
657 : : /* Assert that caller has not specified inconsistent flags */
658 [ - + ]: 673106 : Assert((flags & (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES))
659 : : != (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES));
660 [ - + ]: 673106 : Assert((flags & (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS))
661 : : != (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS));
662 [ - + ]: 673106 : Assert((flags & (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS))
663 : : != (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS));
664 : :
8168 665 : 673106 : context.varlist = NIL;
3868 666 : 673106 : context.flags = flags;
667 : :
9308 668 : 673106 : pull_var_clause_walker(node, &context);
8168 669 : 673106 : return context.varlist;
670 : : }
671 : :
672 : : static bool
9909 673 : 4015903 : pull_var_clause_walker(Node *node, pull_var_clause_context *context)
674 : : {
9977 675 [ + + ]: 4015903 : if (node == NULL)
676 : 174984 : return false;
677 [ + + ]: 3840919 : if (IsA(node, Var))
678 : : {
5571 679 [ - + ]: 1458790 : if (((Var *) node)->varlevelsup != 0)
5571 tgl@sss.pgh.pa.us 680 [ # # ]:UBC 0 : elog(ERROR, "Upper-level Var found where not expected");
5571 tgl@sss.pgh.pa.us 681 :CBC 1458790 : context->varlist = lappend(context->varlist, node);
9977 682 : 1458790 : return false;
683 : : }
5571 684 [ + + ]: 2382129 : else if (IsA(node, Aggref))
685 : : {
686 [ - + ]: 88157 : if (((Aggref *) node)->agglevelsup != 0)
5571 tgl@sss.pgh.pa.us 687 [ # # ]:UBC 0 : elog(ERROR, "Upper-level Aggref found where not expected");
3868 tgl@sss.pgh.pa.us 688 [ + + ]:CBC 88157 : if (context->flags & PVC_INCLUDE_AGGREGATES)
689 : : {
690 : 6951 : context->varlist = lappend(context->varlist, node);
691 : : /* we do NOT descend into the contained expression */
692 : 6951 : return false;
693 : : }
694 [ - + ]: 81206 : else if (context->flags & PVC_RECURSE_AGGREGATES)
695 : : {
696 : : /* fall through to recurse into the aggregate's arguments */
697 : : }
698 : : else
3868 tgl@sss.pgh.pa.us 699 [ # # ]:UBC 0 : elog(ERROR, "Aggref found where not expected");
700 : : }
4167 andres@anarazel.de 701 [ + + ]:CBC 2293972 : else if (IsA(node, GroupingFunc))
702 : : {
703 [ - + ]: 557 : if (((GroupingFunc *) node)->agglevelsup != 0)
4167 andres@anarazel.de 704 [ # # ]:UBC 0 : elog(ERROR, "Upper-level GROUPING found where not expected");
3868 tgl@sss.pgh.pa.us 705 [ + + ]:CBC 557 : if (context->flags & PVC_INCLUDE_AGGREGATES)
706 : : {
707 : 2 : context->varlist = lappend(context->varlist, node);
708 : : /* we do NOT descend into the contained expression */
709 : 2 : return false;
710 : : }
711 [ - + ]: 555 : else if (context->flags & PVC_RECURSE_AGGREGATES)
712 : : {
713 : : /* fall through to recurse into the GroupingFunc's arguments */
714 : : }
715 : : else
3868 tgl@sss.pgh.pa.us 716 [ # # ]:UBC 0 : elog(ERROR, "GROUPING found where not expected");
717 : : }
3868 tgl@sss.pgh.pa.us 718 [ + + ]:CBC 2293415 : else if (IsA(node, WindowFunc))
719 : : {
720 : : /* WindowFuncs have no levelsup field to check ... */
721 [ + + ]: 6591 : if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
722 : : {
723 : 64 : context->varlist = lappend(context->varlist, node);
724 : : /* we do NOT descend into the contained expressions */
725 : 64 : return false;
726 : : }
727 [ - + ]: 6527 : else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
728 : : {
729 : : /* fall through to recurse into the windowfunc's arguments */
730 : : }
731 : : else
3868 tgl@sss.pgh.pa.us 732 [ # # ]:UBC 0 : elog(ERROR, "WindowFunc found where not expected");
733 : : }
5571 tgl@sss.pgh.pa.us 734 [ + + ]:CBC 2286824 : else if (IsA(node, PlaceHolderVar))
735 : : {
736 [ - + ]: 6996 : if (((PlaceHolderVar *) node)->phlevelsup != 0)
5571 tgl@sss.pgh.pa.us 737 [ # # ]:UBC 0 : elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
3868 tgl@sss.pgh.pa.us 738 [ + + ]:CBC 6996 : if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
739 : : {
740 : 5262 : context->varlist = lappend(context->varlist, node);
741 : : /* we do NOT descend into the contained expression */
742 : 5262 : return false;
743 : : }
744 [ - + ]: 1734 : else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
745 : : {
746 : : /* fall through to recurse into the placeholder's expression */
747 : : }
748 : : else
3868 tgl@sss.pgh.pa.us 749 [ # # ]:UBC 0 : elog(ERROR, "PlaceHolderVar found where not expected");
750 : : }
683 peter@eisentraut.org 751 :CBC 2369850 : return expression_tree_walker(node, pull_var_clause_walker, context);
752 : : }
753 : :
754 : :
755 : : /*
756 : : * flatten_join_alias_vars
757 : : * Replace Vars that reference JOIN outputs with references to the original
758 : : * relation variables instead. This allows quals involving such vars to be
759 : : * pushed down. Whole-row Vars that reference JOIN relations are expanded
760 : : * into RowExpr constructs that name the individual output Vars. This
761 : : * is necessary since we will not scan the JOIN as a base relation, which
762 : : * is the only way that the executor can directly handle whole-row Vars.
763 : : *
764 : : * This also adjusts relid sets found in some expression node types to
765 : : * substitute the contained base+OJ rels for any join relid.
766 : : *
767 : : * If a JOIN contains sub-selects that have been flattened, its join alias
768 : : * entries might now be arbitrary expressions, not just Vars. This affects
769 : : * this function in two important ways. First, we might find ourselves
770 : : * inserting SubLink expressions into subqueries, and we must make sure that
771 : : * their Query.hasSubLinks fields get set to true if so. If there are any
772 : : * SubLinks in the join alias lists, the outer Query should already have
773 : : * hasSubLinks = true, so this is only relevant to un-flattened subqueries.
774 : : * Second, we have to preserve any varnullingrels info attached to the
775 : : * alias Vars we're replacing. If the replacement expression is a Var or
776 : : * PlaceHolderVar or constructed from those, we can just add the
777 : : * varnullingrels bits to the existing nullingrels field(s); otherwise
778 : : * we have to add a PlaceHolderVar wrapper.
779 : : *
780 : : * If root is NULL, nulled whole-row JOIN Vars are left unexpanded, so the
781 : : * result must be flattened again with a root before it can be executed.
782 : : */
783 : : Node *
1351 tgl@sss.pgh.pa.us 784 : 198163 : flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
785 : : {
786 : : flatten_join_alias_vars_context context;
787 : :
788 : : /*
789 : : * We do not expect this to be applied to the whole Query, only to
790 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
791 : : * it's okay to immediately increment sublevels_up.
792 : : */
793 [ - + ]: 198163 : Assert(node != (Node *) query);
794 : : /* add_nullingrels_if_needed relies on this */
5 rguo@postgresql.org 795 [ + + - + ]: 198163 : Assert(root == NULL || query == root->parse);
796 : :
1351 tgl@sss.pgh.pa.us 797 : 198163 : context.root = root;
2813 798 : 198163 : context.query = query;
8671 799 : 198163 : context.sublevels_up = 0;
800 : : /* flag whether join aliases could possibly contain SubLinks */
220 801 : 198163 : context.possible_sublink = query->hasSubLinks;
802 : : /* if hasSubLinks is already true, no need to work hard */
803 : 198163 : context.inserted_sublink = query->hasSubLinks;
804 : :
805 : 198163 : return flatten_join_alias_vars_mutator(node, &context);
806 : : }
807 : :
808 : : /*
809 : : * flatten_join_alias_for_parser
810 : : *
811 : : * This variant of flatten_join_alias_vars is used by the parser, to expand
812 : : * join alias Vars before checking GROUP BY validity. In that case we lack
813 : : * a root structure. Fortunately, we'd only need the root for making
814 : : * PlaceHolderVars. We can avoid making PlaceHolderVars in the parser's
815 : : * usage because it won't be dealing with arbitrary expressions: so long as
816 : : * adjust_standard_join_alias_expression can handle everything the parser
817 : : * would make as a join alias expression, we're OK. (Nulled whole-row join
818 : : * Vars are the exception; those are left unexpanded.)
819 : : *
820 : : * The "node" might be part of a sub-query of the Query whose join alias
821 : : * Vars are to be expanded. "sublevels_up" indicates how far below the
822 : : * given query we are starting.
823 : : */
824 : : Node *
825 : 4106 : flatten_join_alias_for_parser(Query *query, Node *node, int sublevels_up)
826 : : {
827 : : flatten_join_alias_vars_context context;
828 : :
829 : : /*
830 : : * We do not expect this to be applied to the whole Query, only to
831 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
832 : : * it's okay to immediately increment sublevels_up.
833 : : */
834 [ - + ]: 4106 : Assert(node != (Node *) query);
835 : :
836 : 4106 : context.root = NULL;
837 : 4106 : context.query = query;
838 : 4106 : context.sublevels_up = sublevels_up;
839 : : /* flag whether join aliases could possibly contain SubLinks */
2813 840 : 4106 : context.possible_sublink = query->hasSubLinks;
841 : : /* if hasSubLinks is already true, no need to work hard */
842 : 4106 : context.inserted_sublink = query->hasSubLinks;
843 : :
8980 844 : 4106 : return flatten_join_alias_vars_mutator(node, &context);
845 : : }
846 : :
847 : : static Node *
848 : 2804243 : flatten_join_alias_vars_mutator(Node *node,
849 : : flatten_join_alias_vars_context *context)
850 : : {
851 [ + + ]: 2804243 : if (node == NULL)
852 : 237339 : return NULL;
853 [ + + ]: 2566904 : if (IsA(node, Var))
854 : : {
8804 bruce@momjian.us 855 : 761422 : Var *var = (Var *) node;
856 : : RangeTblEntry *rte;
857 : : Node *newvar;
858 : :
859 : : /* No change unless Var belongs to a JOIN of the target level */
8671 tgl@sss.pgh.pa.us 860 [ + + ]: 761422 : if (var->varlevelsup != context->sublevels_up)
8980 861 : 40375 : return node; /* no need to copy, really */
2813 862 : 721047 : rte = rt_fetch(var->varno, context->query->rtable);
8980 863 [ + + ]: 721047 : if (rte->rtekind != RTE_JOIN)
8933 864 : 720290 : return node;
8190 865 [ + + ]: 757 : if (var->varattno == InvalidAttrNumber)
866 : : {
867 : : /* Must expand whole-row reference */
868 : : RowExpr *rowexpr;
8079 bruce@momjian.us 869 : 142 : List *fields = NIL;
5354 tgl@sss.pgh.pa.us 870 : 142 : List *colnames = NIL;
871 : : ListCell *lv;
872 : : ListCell *ln;
873 : :
874 : : /*
875 : : * A nulled whole-row expansion needs a PlaceHolderVar, which we
876 : : * can't make without a root; leave the Var unexpanded.
877 : : */
5 rguo@postgresql.org 878 [ + + + - ]: 142 : if (context->root == NULL && var->varnullingrels != NULL)
879 : 12 : return node;
880 : :
5354 tgl@sss.pgh.pa.us 881 [ - + ]: 130 : Assert(list_length(rte->joinaliasvars) == list_length(rte->eref->colnames));
882 [ + + + + : 415 : forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
+ + + + +
+ + - +
+ ]
883 : : {
884 : 285 : newvar = (Node *) lfirst(lv);
885 : : /* Ignore dropped columns */
4829 886 [ - + ]: 285 : if (newvar == NULL)
8089 tgl@sss.pgh.pa.us 887 :UBC 0 : continue;
5459 tgl@sss.pgh.pa.us 888 :CBC 285 : newvar = copyObject(newvar);
889 : :
890 : : /*
891 : : * If we are expanding an alias carried down from an upper
892 : : * query, must adjust its varlevelsup fields.
893 : : */
8190 894 [ + + ]: 285 : if (context->sublevels_up != 0)
895 : 50 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
896 : : /* Preserve original Var's location, if possible */
5459 897 [ + + ]: 285 : if (IsA(newvar, Var))
898 : 220 : ((Var *) newvar)->location = var->location;
899 : : /* Recurse in case join input is itself a join */
8190 900 : 285 : newvar = flatten_join_alias_vars_mutator(newvar, context);
901 : :
902 : : /* Detect if we are adding a sublink to query */
8 rguo@postgresql.org 903 [ + + + + ]: 285 : if (context->possible_sublink && !context->inserted_sublink)
904 : 35 : context->inserted_sublink = checkExprHasSubLink(newvar);
905 : :
8190 tgl@sss.pgh.pa.us 906 : 285 : fields = lappend(fields, newvar);
907 : : /* We need the names of non-dropped columns, too */
5354 908 : 285 : colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
909 : : }
8190 910 : 130 : rowexpr = makeNode(RowExpr);
911 : 130 : rowexpr->args = fields;
912 : 130 : rowexpr->row_typeid = var->vartype;
913 : 130 : rowexpr->row_format = COERCE_IMPLICIT_CAST;
914 : : /* vartype will always be RECORDOID, so we always need colnames */
5354 915 : 130 : rowexpr->colnames = colnames;
5459 916 : 130 : rowexpr->location = var->location;
917 : :
918 : : /* Lastly, add any varnullingrels to the replacement expression */
1351 919 : 130 : return add_nullingrels_if_needed(context->root, (Node *) rowexpr,
920 : : var);
921 : : }
922 : :
923 : : /* Expand join alias reference */
8933 924 [ - + ]: 615 : Assert(var->varattno > 0);
8170 neilc@samurai.com 925 : 615 : newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
4829 tgl@sss.pgh.pa.us 926 [ - + ]: 615 : Assert(newvar != NULL);
5459 927 : 615 : newvar = copyObject(newvar);
928 : :
929 : : /*
930 : : * If we are expanding an alias carried down from an upper query, must
931 : : * adjust its varlevelsup fields.
932 : : */
8649 933 [ + + ]: 615 : if (context->sublevels_up != 0)
934 : 38 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
935 : :
936 : : /* Preserve original Var's location, if possible */
5459 937 [ - + ]: 615 : if (IsA(newvar, Var))
5459 tgl@sss.pgh.pa.us 938 :UBC 0 : ((Var *) newvar)->location = var->location;
939 : :
940 : : /* Recurse in case join input is itself a join */
5940 tgl@sss.pgh.pa.us 941 :CBC 615 : newvar = flatten_join_alias_vars_mutator(newvar, context);
942 : :
943 : : /* Detect if we are adding a sublink to query */
944 [ + + + + ]: 615 : if (context->possible_sublink && !context->inserted_sublink)
945 : 30 : context->inserted_sublink = checkExprHasSubLink(newvar);
946 : :
947 : : /* Lastly, add any varnullingrels to the replacement expression */
1351 948 : 615 : return add_nullingrels_if_needed(context->root, newvar, var);
949 : : }
6565 950 [ + + ]: 1805482 : if (IsA(node, PlaceHolderVar))
951 : : {
28 rguo@postgresql.org 952 : 2243 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
953 : :
954 : : /*
955 : : * A PHV above the target level can't contain join aliases of the
956 : : * target level and has no relid sets of ours to fix, so return it
957 : : * unchanged.
958 : : */
959 [ + + ]: 2243 : if (phv->phlevelsup > context->sublevels_up)
960 : 100 : return node; /* no need to copy, really */
961 : :
962 : : /*
963 : : * A pushed-down copy of a target-level PHV is preprocessed by
964 : : * preprocess_subquery_phvs, which handles its target-level aliases
965 : : * and may leave SubPlans in it; so don't recurse into it. Any other
966 : : * PHV may still hold target-level aliases, so recurse.
967 : : */
968 [ + - ]: 2143 : if (phv->phlevelsup == context->sublevels_up &&
969 [ + + ]: 2143 : context->sublevels_up > 0)
970 : 200 : {
971 : 200 : PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
972 : :
973 : 200 : memcpy(newphv, phv, sizeof(PlaceHolderVar));
974 : 200 : phv = newphv;
975 : : }
976 : : else
977 : 1943 : phv = (PlaceHolderVar *) expression_tree_mutator(node,
978 : : flatten_join_alias_vars_mutator,
979 : : context);
980 : : /* now fix PlaceHolderVar's relid sets */
6565 tgl@sss.pgh.pa.us 981 [ + - ]: 2143 : if (phv->phlevelsup == context->sublevels_up)
982 : : {
2813 983 : 2143 : phv->phrels = alias_relid_set(context->query,
984 : : phv->phrels);
985 : : /* we *don't* change phnullingrels */
986 : : }
6565 987 : 2143 : return (Node *) phv;
988 : : }
989 : :
8671 990 [ + + ]: 1803239 : if (IsA(node, Query))
991 : : {
992 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
993 : : Query *newnode;
994 : : bool save_inserted_sublink;
995 : :
996 : 12088 : context->sublevels_up++;
5940 997 : 12088 : save_inserted_sublink = context->inserted_sublink;
998 : 12088 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
8669 999 : 12088 : newnode = query_tree_mutator((Query *) node,
1000 : : flatten_join_alias_vars_mutator,
1001 : : context,
1002 : : QTW_IGNORE_JOINALIASES);
5940 1003 : 12088 : newnode->hasSubLinks |= context->inserted_sublink;
1004 : 12088 : context->inserted_sublink = save_inserted_sublink;
8671 1005 : 12088 : context->sublevels_up--;
1006 : 12088 : return (Node *) newnode;
1007 : : }
1008 : : /* Already-planned tree not supported */
6625 1009 [ - + ]: 1791151 : Assert(!IsA(node, SubPlan));
762 rguo@postgresql.org 1010 [ - + ]: 1791151 : Assert(!IsA(node, AlternativeSubPlan));
1011 : : /* Shouldn't need to handle these planner auxiliary nodes here */
6564 tgl@sss.pgh.pa.us 1012 [ - + ]: 1791151 : Assert(!IsA(node, SpecialJoinInfo));
1013 [ - + ]: 1791151 : Assert(!IsA(node, PlaceHolderInfo));
5821 1014 [ - + ]: 1791151 : Assert(!IsA(node, MinMaxAggInfo));
1015 : :
683 peter@eisentraut.org 1016 : 1791151 : return expression_tree_mutator(node, flatten_join_alias_vars_mutator, context);
1017 : : }
1018 : :
1019 : : /*
1020 : : * flatten_group_exprs
1021 : : * Replace Vars that reference GROUP outputs with the underlying grouping
1022 : : * expressions.
1023 : : *
1024 : : * We have to preserve any varnullingrels info attached to the group Vars we're
1025 : : * replacing. If the replacement expression is a Var or PlaceHolderVar or
1026 : : * constructed from those, we can just add the varnullingrels bits to the
1027 : : * existing nullingrels field(s); otherwise we have to add a PlaceHolderVar
1028 : : * wrapper.
1029 : : *
1030 : : * NOTE: root may be passed as NULL, which is why we have to pass the Query
1031 : : * separately. We need the root itself only for preserving varnullingrels.
1032 : : * Callers can safely pass NULL if preserving varnullingrels is unnecessary for
1033 : : * their specific use case (e.g., deparsing source text, or scanning for
1034 : : * volatile functions), or if it is already guaranteed that the query cannot
1035 : : * contain grouping sets.
1036 : : */
1037 : : Node *
762 rguo@postgresql.org 1038 : 9474 : flatten_group_exprs(PlannerInfo *root, Query *query, Node *node)
1039 : : {
1040 : : flatten_join_alias_vars_context context;
1041 : :
1042 : : /*
1043 : : * We do not expect this to be applied to the whole Query, only to
1044 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
1045 : : * it's okay to immediately increment sublevels_up.
1046 : : */
1047 [ - + ]: 9474 : Assert(node != (Node *) query);
1048 : :
1049 : 9474 : context.root = root;
1050 : 9474 : context.query = query;
1051 : 9474 : context.sublevels_up = 0;
1052 : : /* flag whether grouping expressions could possibly contain SubLinks */
1053 : 9474 : context.possible_sublink = query->hasSubLinks;
1054 : : /* if hasSubLinks is already true, no need to work hard */
1055 : 9474 : context.inserted_sublink = query->hasSubLinks;
1056 : :
1057 : 9474 : return flatten_group_exprs_mutator(node, &context);
1058 : : }
1059 : :
1060 : : static Node *
1061 : 90349 : flatten_group_exprs_mutator(Node *node,
1062 : : flatten_join_alias_vars_context *context)
1063 : : {
1064 [ + + ]: 90349 : if (node == NULL)
1065 : 10519 : return NULL;
1066 [ + + ]: 79830 : if (IsA(node, Var))
1067 : : {
1068 : 15481 : Var *var = (Var *) node;
1069 : : RangeTblEntry *rte;
1070 : : Node *newvar;
1071 : :
1072 : : /* No change unless Var belongs to the GROUP of the target level */
1073 [ - + ]: 15481 : if (var->varlevelsup != context->sublevels_up)
762 rguo@postgresql.org 1074 :UBC 0 : return node; /* no need to copy, really */
762 rguo@postgresql.org 1075 :CBC 15481 : rte = rt_fetch(var->varno, context->query->rtable);
1076 [ + + ]: 15481 : if (rte->rtekind != RTE_GROUP)
1077 : 199 : return node;
1078 : :
1079 : : /* Expand group exprs reference */
1080 [ - + ]: 15282 : Assert(var->varattno > 0);
1081 : 15282 : newvar = (Node *) list_nth(rte->groupexprs, var->varattno - 1);
1082 [ - + ]: 15282 : Assert(newvar != NULL);
1083 : 15282 : newvar = copyObject(newvar);
1084 : :
1085 : : /*
1086 : : * If we are expanding an expr carried down from an upper query, must
1087 : : * adjust its varlevelsup fields.
1088 : : */
1089 [ - + ]: 15282 : if (context->sublevels_up != 0)
762 rguo@postgresql.org 1090 :UBC 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
1091 : :
1092 : : /* Preserve original Var's location, if possible */
762 rguo@postgresql.org 1093 [ + + ]:CBC 15282 : if (IsA(newvar, Var))
1094 : 13042 : ((Var *) newvar)->location = var->location;
1095 : :
1096 : : /* Detect if we are adding a sublink to query */
1097 [ + + - + ]: 15282 : if (context->possible_sublink && !context->inserted_sublink)
762 rguo@postgresql.org 1098 :UBC 0 : context->inserted_sublink = checkExprHasSubLink(newvar);
1099 : :
1100 : : /* Lastly, add any varnullingrels to the replacement expression */
762 rguo@postgresql.org 1101 :CBC 15282 : return mark_nullable_by_grouping(context->root, newvar, var);
1102 : : }
1103 : :
1104 [ + + ]: 64349 : if (IsA(node, Aggref))
1105 : : {
1106 : 6286 : Aggref *agg = (Aggref *) node;
1107 : :
1108 [ + - ]: 6286 : if ((int) agg->agglevelsup == context->sublevels_up)
1109 : : {
1110 : : /*
1111 : : * If we find an aggregate call of the original level, do not
1112 : : * recurse into its normal arguments, ORDER BY arguments, or
1113 : : * filter; there are no grouped vars there. But we should check
1114 : : * direct arguments as though they weren't in an aggregate.
1115 : : */
1116 : 6286 : agg = copyObject(agg);
1117 : 6286 : agg->aggdirectargs = (List *)
1118 : 6286 : flatten_group_exprs_mutator((Node *) agg->aggdirectargs, context);
1119 : :
1120 : 6286 : return (Node *) agg;
1121 : : }
1122 : :
1123 : : /*
1124 : : * We can skip recursing into aggregates of higher levels altogether,
1125 : : * since they could not possibly contain Vars of concern to us (see
1126 : : * transformAggregateCall). We do need to look at aggregates of lower
1127 : : * levels, however.
1128 : : */
762 rguo@postgresql.org 1129 [ # # ]:UBC 0 : if ((int) agg->agglevelsup > context->sublevels_up)
1130 : 0 : return node;
1131 : : }
1132 : :
762 rguo@postgresql.org 1133 [ + + ]:CBC 58063 : if (IsA(node, GroupingFunc))
1134 : : {
1135 : 307 : GroupingFunc *grp = (GroupingFunc *) node;
1136 : :
1137 : : /*
1138 : : * If we find a GroupingFunc node of the original or higher level, do
1139 : : * not recurse into its arguments; there are no grouped vars there.
1140 : : */
1141 [ + - ]: 307 : if ((int) grp->agglevelsup >= context->sublevels_up)
1142 : 307 : return node;
1143 : : }
1144 : :
1145 [ - + ]: 57756 : if (IsA(node, Query))
1146 : : {
1147 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1148 : : Query *newnode;
1149 : : bool save_inserted_sublink;
1150 : :
762 rguo@postgresql.org 1151 :UBC 0 : context->sublevels_up++;
1152 : 0 : save_inserted_sublink = context->inserted_sublink;
1153 : 0 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
1154 : 0 : newnode = query_tree_mutator((Query *) node,
1155 : : flatten_group_exprs_mutator,
1156 : : context,
1157 : : QTW_IGNORE_GROUPEXPRS);
1158 : 0 : newnode->hasSubLinks |= context->inserted_sublink;
1159 : 0 : context->inserted_sublink = save_inserted_sublink;
1160 : 0 : context->sublevels_up--;
1161 : 0 : return (Node *) newnode;
1162 : : }
1163 : :
762 rguo@postgresql.org 1164 :CBC 57756 : return expression_tree_mutator(node, flatten_group_exprs_mutator,
1165 : : context);
1166 : : }
1167 : :
1168 : : /*
1169 : : * Add oldvar's varnullingrels, if any, to a flattened grouping expression.
1170 : : * The newnode has been copied, so we can modify it freely.
1171 : : */
1172 : : static Node *
1173 : 15282 : mark_nullable_by_grouping(PlannerInfo *root, Node *newnode, Var *oldvar)
1174 : : {
1175 : : Relids relids;
1176 : :
1177 [ + + ]: 15282 : if (root == NULL)
1178 : 6229 : return newnode;
1179 [ + + ]: 9053 : if (oldvar->varnullingrels == NULL)
1180 : 7266 : return newnode; /* nothing to do */
1181 : :
1182 [ - + ]: 1787 : Assert(bms_equal(oldvar->varnullingrels,
1183 : : bms_make_singleton(root->group_rtindex)));
1184 : :
1185 : 1787 : relids = pull_varnos_of_level(root, newnode, oldvar->varlevelsup);
1186 : :
1187 [ + + ]: 1787 : if (!bms_is_empty(relids))
1188 : : {
1189 : : /*
1190 : : * If the newnode is not variable-free, we set the nullingrels of Vars
1191 : : * or PHVs that are contained in the expression. This is not really
1192 : : * 'correct' in theory, because it is the whole expression that can be
1193 : : * nullable by grouping sets, not its individual vars. But it works
1194 : : * in practice, because what we need is that the expression can be
1195 : : * somehow distinguished from the same expression in ECs, and marking
1196 : : * its vars is sufficient for this purpose.
1197 : : */
1198 : 1747 : newnode = add_nulling_relids(newnode,
1199 : : relids,
1200 : 1747 : oldvar->varnullingrels);
1201 : : }
1202 : : else /* variable-free? */
1203 : : {
1204 : : /*
1205 : : * If the newnode is variable-free and does not contain volatile
1206 : : * functions or set-returning functions, it can be treated as a member
1207 : : * of EC that is redundant. So wrap it in a new PlaceHolderVar to
1208 : : * carry the nullingrels. Otherwise we do not bother to make any
1209 : : * changes.
1210 : : *
1211 : : * Aggregate functions and window functions are not allowed in
1212 : : * grouping expressions.
1213 : : */
1214 [ - + ]: 40 : Assert(!contain_agg_clause(newnode));
1215 [ - + ]: 40 : Assert(!contain_window_function(newnode));
1216 : :
1217 [ + - ]: 40 : if (!contain_volatile_functions(newnode) &&
1218 [ + + ]: 40 : !expression_returns_set(newnode))
1219 : : {
1220 : : PlaceHolderVar *newphv;
1221 : : Relids phrels;
1222 : :
1223 : 25 : phrels = get_relids_in_jointree((Node *) root->parse->jointree,
1224 : : true, false);
1225 [ - + ]: 25 : Assert(!bms_is_empty(phrels));
1226 : :
1227 : 25 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1228 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1229 : 25 : newphv->phlevelsup = oldvar->varlevelsup;
1230 : 25 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1231 : 25 : newnode = (Node *) newphv;
1232 : : }
1233 : : }
1234 : :
1235 : 1787 : return newnode;
1236 : : }
1237 : :
1238 : : /*
1239 : : * Add oldvar's varnullingrels, if any, to a flattened join alias expression.
1240 : : * The newnode has been copied, so we can modify it freely.
1241 : : */
1242 : : static Node *
1351 tgl@sss.pgh.pa.us 1243 : 745 : add_nullingrels_if_needed(PlannerInfo *root, Node *newnode, Var *oldvar)
1244 : : {
1245 [ + + ]: 745 : if (oldvar->varnullingrels == NULL)
1246 : 549 : return newnode; /* nothing to do */
1247 : : /* If possible, do it by adding to existing nullingrel fields */
1248 [ + + ]: 196 : if (is_standard_join_alias_expression(newnode, oldvar))
1249 : 116 : adjust_standard_join_alias_expression(newnode, oldvar);
1250 [ + - ]: 80 : else if (root)
1251 : : {
1252 : : /*
1253 : : * We can insert a PlaceHolderVar to carry the nullingrels. However,
1254 : : * deciding where to evaluate the PHV is slightly tricky. We first
1255 : : * try to evaluate it at the natural semantic level of the new
1256 : : * expression, ignoring any lateral references to rels outside the
1257 : : * join; but if that leaves nothing, fall back to evaluating it at the
1258 : : * join that the oldvar is an alias Var for.
1259 : : */
1260 : : PlaceHolderVar *newphv;
1261 : 80 : Index levelsup = oldvar->varlevelsup;
1262 : : Relids joinrelids;
1263 : : Relids phrels;
1264 : :
1265 : : /* oldvar belongs to root->parse even when levelsup > 0 */
5 rguo@postgresql.org 1266 : 80 : joinrelids = get_relids_for_join(root->parse, oldvar->varno);
1267 : 80 : phrels = pull_varnos_of_level(root, newnode, levelsup);
1268 : 80 : phrels = bms_int_members(phrels, joinrelids);
1269 [ + + ]: 80 : if (bms_is_empty(phrels))
1270 : : {
1271 : : /* Keep the join's own OJ relid: this set spans both its sides */
1272 : 50 : phrels = joinrelids;
1273 : : }
1351 tgl@sss.pgh.pa.us 1274 : 80 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1275 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1276 : 80 : newphv->phlevelsup = levelsup;
1277 : 80 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1278 : 80 : newnode = (Node *) newphv;
1279 : : }
1280 : : else
1281 : : {
1282 : : /* ooops, we're missing support for something the parser can make */
1351 tgl@sss.pgh.pa.us 1283 [ # # ]:UBC 0 : elog(ERROR, "unsupported join alias expression");
1284 : : }
1351 tgl@sss.pgh.pa.us 1285 :CBC 196 : return newnode;
1286 : : }
1287 : :
1288 : : /*
1289 : : * Check to see if we can insert nullingrels into this join alias expression
1290 : : * without use of a separate PlaceHolderVar.
1291 : : *
1292 : : * This will handle Vars, PlaceHolderVars, and implicit-coercion and COALESCE
1293 : : * expressions built from those. This coverage needs to handle anything
1294 : : * that the parser would put into joinaliasvars.
1295 : : */
1296 : : static bool
1297 : 462 : is_standard_join_alias_expression(Node *newnode, Var *oldvar)
1298 : : {
1299 [ - + ]: 462 : if (newnode == NULL)
1351 tgl@sss.pgh.pa.us 1300 :UBC 0 : return false;
1351 tgl@sss.pgh.pa.us 1301 [ + + ]:CBC 462 : if (IsA(newnode, Var) &&
1302 [ + - ]: 226 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1303 : 226 : return true;
1304 [ - + ]: 236 : else if (IsA(newnode, PlaceHolderVar) &&
1351 tgl@sss.pgh.pa.us 1305 [ # # ]:UBC 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1306 : 0 : return true;
1351 tgl@sss.pgh.pa.us 1307 [ + + ]:CBC 236 : else if (IsA(newnode, FuncExpr))
1308 : : {
1309 : 33 : FuncExpr *fexpr = (FuncExpr *) newnode;
1310 : :
1311 : : /*
1312 : : * We need to assume that the function wouldn't produce non-NULL from
1313 : : * NULL, which is reasonable for implicit coercions but otherwise not
1314 : : * so much. (Looking at its strictness is likely overkill, and anyway
1315 : : * it would cause us to fail if someone forgot to mark an implicit
1316 : : * coercion as strict.)
1317 : : */
1318 [ + - ]: 33 : if (fexpr->funcformat != COERCE_IMPLICIT_CAST ||
1319 [ - + ]: 33 : fexpr->args == NIL)
1351 tgl@sss.pgh.pa.us 1320 :UBC 0 : return false;
1321 : :
1322 : : /*
1323 : : * Examine only the first argument --- coercions might have additional
1324 : : * arguments that are constants.
1325 : : */
1351 tgl@sss.pgh.pa.us 1326 :CBC 33 : return is_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1327 : : }
1328 [ + + ]: 203 : else if (IsA(newnode, RelabelType))
1329 : : {
1330 : 13 : RelabelType *relabel = (RelabelType *) newnode;
1331 : :
1332 : : /* This definitely won't produce non-NULL from NULL */
1333 : 13 : return is_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1334 : : }
1335 [ - + ]: 190 : else if (IsA(newnode, CoerceViaIO))
1336 : : {
1351 tgl@sss.pgh.pa.us 1337 :UBC 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1338 : :
1339 : : /* This definitely won't produce non-NULL from NULL */
1340 : 0 : return is_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1341 : : }
1351 tgl@sss.pgh.pa.us 1342 [ - + ]:CBC 190 : else if (IsA(newnode, ArrayCoerceExpr))
1343 : : {
1351 tgl@sss.pgh.pa.us 1344 :UBC 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1345 : :
1346 : : /* This definitely won't produce non-NULL from NULL (at array level) */
1347 : 0 : return is_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1348 : : }
1351 tgl@sss.pgh.pa.us 1349 [ + + ]:CBC 190 : else if (IsA(newnode, CoalesceExpr))
1350 : : {
1351 : 110 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1352 : : ListCell *lc;
1353 : :
1354 [ - + ]: 110 : Assert(cexpr->args != NIL);
1355 [ + - + + : 330 : foreach(lc, cexpr->args)
+ + ]
1356 : : {
1357 [ - + ]: 220 : if (!is_standard_join_alias_expression(lfirst(lc), oldvar))
1351 tgl@sss.pgh.pa.us 1358 :UBC 0 : return false;
1359 : : }
1351 tgl@sss.pgh.pa.us 1360 :CBC 110 : return true;
1361 : : }
1362 : : else
1363 : 80 : return false;
1364 : : }
1365 : :
1366 : : /*
1367 : : * Insert nullingrels into an expression accepted by
1368 : : * is_standard_join_alias_expression.
1369 : : */
1370 : : static void
1371 : 362 : adjust_standard_join_alias_expression(Node *newnode, Var *oldvar)
1372 : : {
1373 [ + + ]: 362 : if (IsA(newnode, Var) &&
1374 [ + - ]: 226 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1375 : 226 : {
1376 : 226 : Var *newvar = (Var *) newnode;
1377 : :
1378 : 226 : newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
1379 : 226 : oldvar->varnullingrels);
1380 : : }
1381 [ - + ]: 136 : else if (IsA(newnode, PlaceHolderVar) &&
1351 tgl@sss.pgh.pa.us 1382 [ # # ]:UBC 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1383 : 0 : {
1384 : 0 : PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
1385 : :
1386 : 0 : newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
1387 : 0 : oldvar->varnullingrels);
1388 : : }
1351 tgl@sss.pgh.pa.us 1389 [ + + ]:CBC 136 : else if (IsA(newnode, FuncExpr))
1390 : : {
1391 : 13 : FuncExpr *fexpr = (FuncExpr *) newnode;
1392 : :
1393 : 13 : adjust_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1394 : : }
1395 [ + + ]: 123 : else if (IsA(newnode, RelabelType))
1396 : : {
1397 : 13 : RelabelType *relabel = (RelabelType *) newnode;
1398 : :
1399 : 13 : adjust_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1400 : : }
1401 [ - + ]: 110 : else if (IsA(newnode, CoerceViaIO))
1402 : : {
1351 tgl@sss.pgh.pa.us 1403 :UBC 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1404 : :
1405 : 0 : adjust_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1406 : : }
1351 tgl@sss.pgh.pa.us 1407 [ - + ]:CBC 110 : else if (IsA(newnode, ArrayCoerceExpr))
1408 : : {
1351 tgl@sss.pgh.pa.us 1409 :UBC 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1410 : :
1411 : 0 : adjust_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1412 : : }
1351 tgl@sss.pgh.pa.us 1413 [ + - ]:CBC 110 : else if (IsA(newnode, CoalesceExpr))
1414 : : {
1415 : 110 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1416 : : ListCell *lc;
1417 : :
1418 [ - + ]: 110 : Assert(cexpr->args != NIL);
1419 [ + - + + : 330 : foreach(lc, cexpr->args)
+ + ]
1420 : : {
1421 : 220 : adjust_standard_join_alias_expression(lfirst(lc), oldvar);
1422 : : }
1423 : : }
1424 : : else
1351 tgl@sss.pgh.pa.us 1425 :UBC 0 : Assert(false);
1351 tgl@sss.pgh.pa.us 1426 :CBC 362 : }
1427 : :
1428 : : /*
1429 : : * alias_relid_set: in a set of RT indexes, replace joins by their
1430 : : * underlying base+OJ relids
1431 : : */
1432 : : static Relids
2813 1433 : 2143 : alias_relid_set(Query *query, Relids relids)
1434 : : {
8647 1435 : 2143 : Relids result = NULL;
1436 : : int rtindex;
1437 : :
4336 1438 : 2143 : rtindex = -1;
1439 [ + + ]: 5436 : while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1440 : : {
2813 1441 : 3293 : RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
1442 : :
8666 1443 [ + + ]: 3293 : if (rte->rtekind == RTE_JOIN)
2813 1444 : 437 : result = bms_join(result, get_relids_for_join(query, rtindex));
1445 : : else
8647 1446 : 2856 : result = bms_add_member(result, rtindex);
1447 : : }
8666 1448 : 2143 : return result;
1449 : : }
|