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
2068 tgl@sss.pgh.pa.us 114 :CBC 2669172 : pull_varnos(PlannerInfo *root, Node *node)
115 : : {
116 : : pull_varnos_context context;
117 : :
8625 118 : 2669172 : context.varnos = NULL;
2068 119 : 2669172 : context.root = root;
9504 120 : 2669172 : 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 : : */
8647 126 : 2669172 : query_or_expression_tree_walker(node,
127 : : pull_varnos_walker,
128 : : &context,
129 : : 0);
130 : :
8625 131 : 2669172 : 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
2068 140 : 7960 : pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
141 : : {
142 : : pull_varnos_context context;
143 : :
5157 144 : 7960 : context.varnos = NULL;
2068 145 : 7960 : context.root = root;
5157 146 : 7960 : 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 : 7960 : query_or_expression_tree_walker(node,
153 : : pull_varnos_walker,
154 : : &context,
155 : : 0);
156 : :
157 : 7960 : return context.varnos;
158 : : }
159 : :
160 : : static bool
9504 161 : 4752210 : pull_varnos_walker(Node *node, pull_varnos_context *context)
162 : : {
9955 163 [ + + ]: 4752210 : if (node == NULL)
164 : 184421 : return false;
165 [ + + ]: 4567789 : if (IsA(node, Var))
166 : : {
9657 bruce@momjian.us 167 : 2287409 : Var *var = (Var *) node;
168 : :
8625 tgl@sss.pgh.pa.us 169 [ + + ]: 2287409 : if (var->varlevelsup == context->sublevels_up)
170 : : {
171 : 2276784 : context->varnos = bms_add_member(context->varnos, var->varno);
1329 172 : 2276784 : context->varnos = bms_add_members(context->varnos,
173 : 2276784 : var->varnullingrels);
174 : : }
9955 175 : 2287409 : return false;
176 : : }
7041 177 [ + + ]: 2280380 : 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 : : }
6543 185 [ + + ]: 2279700 : if (IsA(node, PlaceHolderVar))
186 : : {
187 : 5586 : 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 : : */
1715 197 [ + - ]: 5586 : if (phv->phlevelsup == context->sublevels_up &&
198 [ + - ]: 5586 : 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 : : */
2068 217 : 5586 : PlaceHolderInfo *phinfo = NULL;
218 : :
219 [ + + ]: 5586 : if (phv->phlevelsup == 0)
220 : : {
1495 221 [ + + ]: 5506 : if (phv->phid < context->root->placeholder_array_size)
222 : 4238 : phinfo = context->root->placeholder_array[phv->phid];
223 : : }
1829 224 [ + + ]: 5586 : 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 [ + + ]: 4228 : else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
231 : : {
232 : : /* Normal case: use ph_eval_at */
2068 233 : 3868 : context->varnos = bms_add_members(context->varnos,
234 : 3868 : 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 ... */
1829 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 : : */
1329 261 : 11172 : context->varnos = bms_add_members(context->varnos,
262 : 5586 : phv->phnullingrels);
2068 263 : 5586 : return false; /* don't recurse into expression */
264 : : }
265 : : }
266 [ + + ]: 2274114 : else if (IsA(node, Query))
267 : : {
268 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
269 : : bool result;
270 : :
9504 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 : : }
661 peter@eisentraut.org 277 : 2268149 : 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
5462 tgl@sss.pgh.pa.us 296 : 1255459 : pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
297 : : {
298 : : pull_varattnos_context context;
299 : :
300 : 1255459 : context.varattnos = *varattnos;
301 : 1255459 : context.varno = varno;
302 : :
303 : 1255459 : (void) pull_varattnos_walker(node, &context);
304 : :
305 : 1255459 : *varattnos = context.varattnos;
6940 306 : 1255459 : }
307 : :
308 : : static bool
5462 309 : 4195717 : pull_varattnos_walker(Node *node, pull_varattnos_context *context)
310 : : {
6940 311 [ + + ]: 4195717 : if (node == NULL)
312 : 54480 : return false;
313 [ + + ]: 4141237 : if (IsA(node, Var))
314 : : {
315 : 2384662 : Var *var = (Var *) node;
316 : :
5462 317 [ + + + - ]: 2384662 : if (var->varno == context->varno && var->varlevelsup == 0)
318 : 2384331 : context->varattnos =
319 : 2384331 : bms_add_member(context->varattnos,
3378 320 : 2384331 : var->varattno - FirstLowInvalidHeapAttributeNumber);
6940 321 : 2384662 : return false;
322 : : }
323 : :
324 : : /* Should not find an unplanned subquery */
325 [ - + ]: 1756575 : Assert(!IsA(node, Query));
326 : :
661 peter@eisentraut.org 327 : 1756575 : 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 *
5157 tgl@sss.pgh.pa.us 339 : 6397 : pull_vars_of_level(Node *node, int levelsup)
340 : : {
341 : : pull_vars_context context;
342 : :
343 : 6397 : context.vars = NIL;
344 : 6397 : 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 : 6397 : query_or_expression_tree_walker(node,
351 : : pull_vars_walker,
352 : : &context,
353 : : 0);
354 : :
355 : 6397 : return context.vars;
356 : : }
357 : :
358 : : static bool
359 : 116124 : pull_vars_walker(Node *node, pull_vars_context *context)
360 : : {
361 [ + + ]: 116124 : if (node == NULL)
362 : 29055 : return false;
363 [ + + ]: 87069 : if (IsA(node, Var))
364 : : {
365 : 18146 : Var *var = (Var *) node;
366 : :
367 [ + + ]: 18146 : if (var->varlevelsup == context->sublevels_up)
368 : 9147 : context->vars = lappend(context->vars, var);
369 : 18146 : return false;
370 : : }
5146 371 [ + + ]: 68923 : if (IsA(node, PlaceHolderVar))
372 : : {
373 : 203 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
374 : :
375 [ + - ]: 203 : if (phv->phlevelsup == context->sublevels_up)
376 : 203 : context->vars = lappend(context->vars, phv);
377 : : /* we don't want to look into the contained expression */
378 : 203 : return false;
379 : : }
5157 380 [ + + ]: 68720 : if (IsA(node, Query))
381 : : {
382 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
383 : : bool result;
384 : :
385 : 400 : context->sublevels_up++;
386 : 400 : result = query_tree_walker((Query *) node, pull_vars_walker,
387 : : context, 0);
388 : 400 : context->sublevels_up--;
389 : 400 : return result;
390 : : }
661 peter@eisentraut.org 391 : 68320 : 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
9286 tgl@sss.pgh.pa.us 406 : 26682 : contain_var_clause(Node *node)
407 : : {
408 : 26682 : return contain_var_clause_walker(node, NULL);
409 : : }
410 : :
411 : : static bool
9955 412 : 32584 : contain_var_clause_walker(Node *node, void *context)
413 : : {
414 [ + + ]: 32584 : if (node == NULL)
415 : 84 : return false;
416 [ + + ]: 32500 : if (IsA(node, Var))
417 : : {
9887 418 [ + - ]: 2239 : if (((Var *) node)->varlevelsup == 0)
7645 bruce@momjian.us 419 : 2239 : return true; /* abort the tree traversal and return true */
9887 tgl@sss.pgh.pa.us 420 :UBC 0 : return false;
421 : : }
7041 tgl@sss.pgh.pa.us 422 [ - + ]:CBC 30261 : if (IsA(node, CurrentOfExpr))
7041 tgl@sss.pgh.pa.us 423 :UBC 0 : return true;
6543 tgl@sss.pgh.pa.us 424 [ - + ]:CBC 30261 : if (IsA(node, PlaceHolderVar))
425 : : {
6543 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 : : }
9955 tgl@sss.pgh.pa.us 430 :CBC 30261 : 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
8644 444 : 124445 : contain_vars_of_level(Node *node, int levelsup)
445 : : {
8448 bruce@momjian.us 446 : 124445 : int sublevels_up = levelsup;
447 : :
8644 tgl@sss.pgh.pa.us 448 : 124445 : return query_or_expression_tree_walker(node,
449 : : contain_vars_of_level_walker,
450 : : &sublevels_up,
451 : : 0);
452 : : }
453 : :
454 : : static bool
455 : 492414 : contain_vars_of_level_walker(Node *node, int *sublevels_up)
456 : : {
457 [ + + ]: 492414 : if (node == NULL)
458 : 144877 : return false;
459 [ + + ]: 347537 : if (IsA(node, Var))
460 : : {
461 [ + + ]: 61764 : if (((Var *) node)->varlevelsup == *sublevels_up)
462 : 37887 : return true; /* abort tree traversal and return true */
7041 463 : 23877 : return false;
464 : : }
465 [ + + ]: 285773 : if (IsA(node, CurrentOfExpr))
466 : : {
467 [ + - ]: 152 : if (*sublevels_up == 0)
468 : 152 : return true;
7041 tgl@sss.pgh.pa.us 469 :UBC 0 : return false;
470 : : }
6543 tgl@sss.pgh.pa.us 471 [ + + ]:CBC 285621 : 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 : : }
8644 477 [ + + ]: 285481 : if (IsA(node, Query))
478 : : {
479 : : /* Recurse into subselects */
480 : : bool result;
481 : :
482 : 536 : (*sublevels_up)++;
483 : 536 : result = query_tree_walker((Query *) node,
484 : : contain_vars_of_level_walker,
485 : : sublevels_up,
486 : : 0);
487 : 536 : (*sublevels_up)--;
488 : 536 : return result;
489 : : }
490 : 284945 : 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
612 dean.a.rasheed@gmail 511 : 207 : contain_vars_returning_old_or_new(Node *node)
512 : : {
513 : 207 : return contain_vars_returning_old_or_new_walker(node, NULL);
514 : : }
515 : :
516 : : static bool
517 : 643 : contain_vars_returning_old_or_new_walker(Node *node, void *context)
518 : : {
519 [ + + ]: 643 : if (node == NULL)
520 : 145 : 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 : : {
612 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 : : }
612 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
6593 tgl@sss.pgh.pa.us 555 : 8 : locate_var_of_level(Node *node, int levelsup)
556 : : {
557 : : locate_var_of_level_context context;
558 : :
6310 bruce@momjian.us 559 : 8 : context.var_location = -1; /* in case we find nothing */
6593 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 : : {
8644 574 [ - + ]: 20 : if (node == NULL)
8644 tgl@sss.pgh.pa.us 575 :UBC 0 : return false;
8644 tgl@sss.pgh.pa.us 576 [ + + ]:CBC 20 : if (IsA(node, Var))
577 : : {
6310 bruce@momjian.us 578 : 8 : Var *var = (Var *) node;
579 : :
6593 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;
8644 584 : 8 : return true; /* abort tree traversal and return true */
585 : : }
6593 tgl@sss.pgh.pa.us 586 :UBC 0 : return false;
587 : : }
6593 tgl@sss.pgh.pa.us 588 [ - + ]:CBC 12 : if (IsA(node, CurrentOfExpr))
589 : : {
590 : : /* since CurrentOfExpr doesn't carry location, nothing we can do */
6593 tgl@sss.pgh.pa.us 591 :UBC 0 : return false;
592 : : }
593 : : /* No extra code needed for PlaceHolderVar; just look in contained expr */
8644 tgl@sss.pgh.pa.us 594 [ - + ]:CBC 12 : if (IsA(node, Query))
595 : : {
596 : : /* Recurse into subselects */
597 : : bool result;
598 : :
6593 tgl@sss.pgh.pa.us 599 :UBC 0 : context->sublevels_up++;
8644 600 : 0 : result = query_tree_walker((Query *) node,
601 : : locate_var_of_level_walker,
602 : : context,
603 : : 0);
6593 604 : 0 : context->sublevels_up--;
8644 605 : 0 : return result;
606 : : }
8644 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 *
3846 653 : 666104 : 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 [ - + ]: 666104 : Assert((flags & (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES))
659 : : != (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES));
660 [ - + ]: 666104 : Assert((flags & (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS))
661 : : != (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS));
662 [ - + ]: 666104 : Assert((flags & (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS))
663 : : != (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS));
664 : :
8146 665 : 666104 : context.varlist = NIL;
3846 666 : 666104 : context.flags = flags;
667 : :
9286 668 : 666104 : pull_var_clause_walker(node, &context);
8146 669 : 666104 : return context.varlist;
670 : : }
671 : :
672 : : static bool
9887 673 : 3982308 : pull_var_clause_walker(Node *node, pull_var_clause_context *context)
674 : : {
9955 675 [ + + ]: 3982308 : if (node == NULL)
676 : 173791 : return false;
677 [ + + ]: 3808517 : if (IsA(node, Var))
678 : : {
5549 679 [ - + ]: 1444429 : if (((Var *) node)->varlevelsup != 0)
5549 tgl@sss.pgh.pa.us 680 [ # # ]:UBC 0 : elog(ERROR, "Upper-level Var found where not expected");
5549 tgl@sss.pgh.pa.us 681 :CBC 1444429 : context->varlist = lappend(context->varlist, node);
9955 682 : 1444429 : return false;
683 : : }
5549 684 [ + + ]: 2364088 : else if (IsA(node, Aggref))
685 : : {
686 [ - + ]: 87680 : if (((Aggref *) node)->agglevelsup != 0)
5549 tgl@sss.pgh.pa.us 687 [ # # ]:UBC 0 : elog(ERROR, "Upper-level Aggref found where not expected");
3846 tgl@sss.pgh.pa.us 688 [ + + ]:CBC 87680 : if (context->flags & PVC_INCLUDE_AGGREGATES)
689 : : {
690 : 6795 : context->varlist = lappend(context->varlist, node);
691 : : /* we do NOT descend into the contained expression */
692 : 6795 : return false;
693 : : }
694 [ - + ]: 80885 : else if (context->flags & PVC_RECURSE_AGGREGATES)
695 : : {
696 : : /* fall through to recurse into the aggregate's arguments */
697 : : }
698 : : else
3846 tgl@sss.pgh.pa.us 699 [ # # ]:UBC 0 : elog(ERROR, "Aggref found where not expected");
700 : : }
4145 andres@anarazel.de 701 [ + + ]:CBC 2276408 : else if (IsA(node, GroupingFunc))
702 : : {
703 [ - + ]: 557 : if (((GroupingFunc *) node)->agglevelsup != 0)
4145 andres@anarazel.de 704 [ # # ]:UBC 0 : elog(ERROR, "Upper-level GROUPING found where not expected");
3846 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
3846 tgl@sss.pgh.pa.us 716 [ # # ]:UBC 0 : elog(ERROR, "GROUPING found where not expected");
717 : : }
3846 tgl@sss.pgh.pa.us 718 [ + + ]:CBC 2275851 : 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
3846 tgl@sss.pgh.pa.us 732 [ # # ]:UBC 0 : elog(ERROR, "WindowFunc found where not expected");
733 : : }
5549 tgl@sss.pgh.pa.us 734 [ + + ]:CBC 2269260 : else if (IsA(node, PlaceHolderVar))
735 : : {
736 [ - + ]: 6723 : if (((PlaceHolderVar *) node)->phlevelsup != 0)
5549 tgl@sss.pgh.pa.us 737 [ # # ]:UBC 0 : elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
3846 tgl@sss.pgh.pa.us 738 [ + + ]:CBC 6723 : if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
739 : : {
740 : 5047 : context->varlist = lappend(context->varlist, node);
741 : : /* we do NOT descend into the contained expression */
742 : 5047 : return false;
743 : : }
744 [ - + ]: 1676 : else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
745 : : {
746 : : /* fall through to recurse into the placeholder's expression */
747 : : }
748 : : else
3846 tgl@sss.pgh.pa.us 749 [ # # ]:UBC 0 : elog(ERROR, "PlaceHolderVar found where not expected");
750 : : }
661 peter@eisentraut.org 751 :CBC 2352180 : 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 : : Node *
1329 tgl@sss.pgh.pa.us 781 : 194756 : flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
782 : : {
783 : : flatten_join_alias_vars_context context;
784 : :
785 : : /*
786 : : * We do not expect this to be applied to the whole Query, only to
787 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
788 : : * it's okay to immediately increment sublevels_up.
789 : : */
790 [ - + ]: 194756 : Assert(node != (Node *) query);
791 : :
792 : 194756 : context.root = root;
2791 793 : 194756 : context.query = query;
8649 794 : 194756 : context.sublevels_up = 0;
795 : : /* flag whether join aliases could possibly contain SubLinks */
198 796 : 194756 : context.possible_sublink = query->hasSubLinks;
797 : : /* if hasSubLinks is already true, no need to work hard */
798 : 194756 : context.inserted_sublink = query->hasSubLinks;
799 : :
800 : 194756 : return flatten_join_alias_vars_mutator(node, &context);
801 : : }
802 : :
803 : : /*
804 : : * flatten_join_alias_for_parser
805 : : *
806 : : * This variant of flatten_join_alias_vars is used by the parser, to expand
807 : : * join alias Vars before checking GROUP BY validity. In that case we lack
808 : : * a root structure. Fortunately, we'd only need the root for making
809 : : * PlaceHolderVars. We can avoid making PlaceHolderVars in the parser's
810 : : * usage because it won't be dealing with arbitrary expressions: so long as
811 : : * adjust_standard_join_alias_expression can handle everything the parser
812 : : * would make as a join alias expression, we're OK.
813 : : *
814 : : * The "node" might be part of a sub-query of the Query whose join alias
815 : : * Vars are to be expanded. "sublevels_up" indicates how far below the
816 : : * given query we are starting.
817 : : */
818 : : Node *
819 : 3956 : flatten_join_alias_for_parser(Query *query, Node *node, int sublevels_up)
820 : : {
821 : : flatten_join_alias_vars_context context;
822 : :
823 : : /*
824 : : * We do not expect this to be applied to the whole Query, only to
825 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
826 : : * it's okay to immediately increment sublevels_up.
827 : : */
828 [ - + ]: 3956 : Assert(node != (Node *) query);
829 : :
830 : 3956 : context.root = NULL;
831 : 3956 : context.query = query;
832 : 3956 : context.sublevels_up = sublevels_up;
833 : : /* flag whether join aliases could possibly contain SubLinks */
2791 834 : 3956 : context.possible_sublink = query->hasSubLinks;
835 : : /* if hasSubLinks is already true, no need to work hard */
836 : 3956 : context.inserted_sublink = query->hasSubLinks;
837 : :
8958 838 : 3956 : return flatten_join_alias_vars_mutator(node, &context);
839 : : }
840 : :
841 : : static Node *
842 : 2766025 : flatten_join_alias_vars_mutator(Node *node,
843 : : flatten_join_alias_vars_context *context)
844 : : {
845 [ + + ]: 2766025 : if (node == NULL)
846 : 232001 : return NULL;
847 [ + + ]: 2534024 : if (IsA(node, Var))
848 : : {
8782 bruce@momjian.us 849 : 751530 : Var *var = (Var *) node;
850 : : RangeTblEntry *rte;
851 : : Node *newvar;
852 : :
853 : : /* No change unless Var belongs to a JOIN of the target level */
8649 tgl@sss.pgh.pa.us 854 [ + + ]: 751530 : if (var->varlevelsup != context->sublevels_up)
8958 855 : 39983 : return node; /* no need to copy, really */
2791 856 : 711547 : rte = rt_fetch(var->varno, context->query->rtable);
8958 857 [ + + ]: 711547 : if (rte->rtekind != RTE_JOIN)
8911 858 : 710973 : return node;
8168 859 [ + + ]: 574 : if (var->varattno == InvalidAttrNumber)
860 : : {
861 : : /* Must expand whole-row reference */
862 : : RowExpr *rowexpr;
8057 bruce@momjian.us 863 : 5 : List *fields = NIL;
5332 tgl@sss.pgh.pa.us 864 : 5 : List *colnames = NIL;
865 : : ListCell *lv;
866 : : ListCell *ln;
867 : :
868 [ - + ]: 5 : Assert(list_length(rte->joinaliasvars) == list_length(rte->eref->colnames));
869 [ + - + + : 40 : forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
+ - + + +
+ + - +
+ ]
870 : : {
871 : 35 : newvar = (Node *) lfirst(lv);
872 : : /* Ignore dropped columns */
4807 873 [ - + ]: 35 : if (newvar == NULL)
8067 tgl@sss.pgh.pa.us 874 :UBC 0 : continue;
5437 tgl@sss.pgh.pa.us 875 :CBC 35 : newvar = copyObject(newvar);
876 : :
877 : : /*
878 : : * If we are expanding an alias carried down from an upper
879 : : * query, must adjust its varlevelsup fields.
880 : : */
8168 881 [ - + ]: 35 : if (context->sublevels_up != 0)
8168 tgl@sss.pgh.pa.us 882 :UBC 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
883 : : /* Preserve original Var's location, if possible */
5437 tgl@sss.pgh.pa.us 884 [ + - ]:CBC 35 : if (IsA(newvar, Var))
885 : 35 : ((Var *) newvar)->location = var->location;
886 : : /* Recurse in case join input is itself a join */
887 : : /* (also takes care of setting inserted_sublink if needed) */
8168 888 : 35 : newvar = flatten_join_alias_vars_mutator(newvar, context);
889 : 35 : fields = lappend(fields, newvar);
890 : : /* We need the names of non-dropped columns, too */
5332 891 : 35 : colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
892 : : }
8168 893 : 5 : rowexpr = makeNode(RowExpr);
894 : 5 : rowexpr->args = fields;
895 : 5 : rowexpr->row_typeid = var->vartype;
896 : 5 : rowexpr->row_format = COERCE_IMPLICIT_CAST;
897 : : /* vartype will always be RECORDOID, so we always need colnames */
5332 898 : 5 : rowexpr->colnames = colnames;
5437 899 : 5 : rowexpr->location = var->location;
900 : :
901 : : /* Lastly, add any varnullingrels to the replacement expression */
1329 902 : 5 : return add_nullingrels_if_needed(context->root, (Node *) rowexpr,
903 : : var);
904 : : }
905 : :
906 : : /* Expand join alias reference */
8911 907 [ - + ]: 569 : Assert(var->varattno > 0);
8148 neilc@samurai.com 908 : 569 : newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
4807 tgl@sss.pgh.pa.us 909 [ - + ]: 569 : Assert(newvar != NULL);
5437 910 : 569 : newvar = copyObject(newvar);
911 : :
912 : : /*
913 : : * If we are expanding an alias carried down from an upper query, must
914 : : * adjust its varlevelsup fields.
915 : : */
8627 916 [ + + ]: 569 : if (context->sublevels_up != 0)
917 : 38 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
918 : :
919 : : /* Preserve original Var's location, if possible */
5437 920 [ - + ]: 569 : if (IsA(newvar, Var))
5437 tgl@sss.pgh.pa.us 921 :UBC 0 : ((Var *) newvar)->location = var->location;
922 : :
923 : : /* Recurse in case join input is itself a join */
5918 tgl@sss.pgh.pa.us 924 :CBC 569 : newvar = flatten_join_alias_vars_mutator(newvar, context);
925 : :
926 : : /* Detect if we are adding a sublink to query */
927 [ + + + + ]: 569 : if (context->possible_sublink && !context->inserted_sublink)
928 : 30 : context->inserted_sublink = checkExprHasSubLink(newvar);
929 : :
930 : : /* Lastly, add any varnullingrels to the replacement expression */
1329 931 : 569 : return add_nullingrels_if_needed(context->root, newvar, var);
932 : : }
6543 933 [ + + ]: 1782494 : if (IsA(node, PlaceHolderVar))
934 : : {
6 rguo@postgresql.org 935 : 2087 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
936 : :
937 : : /*
938 : : * A PHV above the target level can't contain join aliases of the
939 : : * target level and has no relid sets of ours to fix, so return it
940 : : * unchanged.
941 : : */
942 [ + + ]: 2087 : if (phv->phlevelsup > context->sublevels_up)
943 : 95 : return node; /* no need to copy, really */
944 : :
945 : : /*
946 : : * A pushed-down copy of a target-level PHV is preprocessed by
947 : : * preprocess_subquery_phvs, which handles its target-level aliases
948 : : * and may leave SubPlans in it; so don't recurse into it. Any other
949 : : * PHV may still hold target-level aliases, so recurse.
950 : : */
951 [ + - ]: 1992 : if (phv->phlevelsup == context->sublevels_up &&
952 [ + + ]: 1992 : context->sublevels_up > 0)
953 : 195 : {
954 : 195 : PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
955 : :
956 : 195 : memcpy(newphv, phv, sizeof(PlaceHolderVar));
957 : 195 : phv = newphv;
958 : : }
959 : : else
960 : 1797 : phv = (PlaceHolderVar *) expression_tree_mutator(node,
961 : : flatten_join_alias_vars_mutator,
962 : : context);
963 : : /* now fix PlaceHolderVar's relid sets */
6543 tgl@sss.pgh.pa.us 964 [ + - ]: 1992 : if (phv->phlevelsup == context->sublevels_up)
965 : : {
2791 966 : 1992 : phv->phrels = alias_relid_set(context->query,
967 : : phv->phrels);
968 : : /* we *don't* change phnullingrels */
969 : : }
6543 970 : 1992 : return (Node *) phv;
971 : : }
972 : :
8649 973 [ + + ]: 1780407 : if (IsA(node, Query))
974 : : {
975 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
976 : : Query *newnode;
977 : : bool save_inserted_sublink;
978 : :
979 : 11871 : context->sublevels_up++;
5918 980 : 11871 : save_inserted_sublink = context->inserted_sublink;
981 : 11871 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
8647 982 : 11871 : newnode = query_tree_mutator((Query *) node,
983 : : flatten_join_alias_vars_mutator,
984 : : context,
985 : : QTW_IGNORE_JOINALIASES);
5918 986 : 11871 : newnode->hasSubLinks |= context->inserted_sublink;
987 : 11871 : context->inserted_sublink = save_inserted_sublink;
8649 988 : 11871 : context->sublevels_up--;
989 : 11871 : return (Node *) newnode;
990 : : }
991 : : /* Already-planned tree not supported */
6603 992 [ - + ]: 1768536 : Assert(!IsA(node, SubPlan));
740 rguo@postgresql.org 993 [ - + ]: 1768536 : Assert(!IsA(node, AlternativeSubPlan));
994 : : /* Shouldn't need to handle these planner auxiliary nodes here */
6542 tgl@sss.pgh.pa.us 995 [ - + ]: 1768536 : Assert(!IsA(node, SpecialJoinInfo));
996 [ - + ]: 1768536 : Assert(!IsA(node, PlaceHolderInfo));
5799 997 [ - + ]: 1768536 : Assert(!IsA(node, MinMaxAggInfo));
998 : :
661 peter@eisentraut.org 999 : 1768536 : return expression_tree_mutator(node, flatten_join_alias_vars_mutator, context);
1000 : : }
1001 : :
1002 : : /*
1003 : : * flatten_group_exprs
1004 : : * Replace Vars that reference GROUP outputs with the underlying grouping
1005 : : * expressions.
1006 : : *
1007 : : * We have to preserve any varnullingrels info attached to the group Vars we're
1008 : : * replacing. If the replacement expression is a Var or PlaceHolderVar or
1009 : : * constructed from those, we can just add the varnullingrels bits to the
1010 : : * existing nullingrels field(s); otherwise we have to add a PlaceHolderVar
1011 : : * wrapper.
1012 : : *
1013 : : * NOTE: root may be passed as NULL, which is why we have to pass the Query
1014 : : * separately. We need the root itself only for preserving varnullingrels.
1015 : : * Callers can safely pass NULL if preserving varnullingrels is unnecessary for
1016 : : * their specific use case (e.g., deparsing source text, or scanning for
1017 : : * volatile functions), or if it is already guaranteed that the query cannot
1018 : : * contain grouping sets.
1019 : : */
1020 : : Node *
740 rguo@postgresql.org 1021 : 9328 : flatten_group_exprs(PlannerInfo *root, Query *query, Node *node)
1022 : : {
1023 : : flatten_join_alias_vars_context context;
1024 : :
1025 : : /*
1026 : : * We do not expect this to be applied to the whole Query, only to
1027 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
1028 : : * it's okay to immediately increment sublevels_up.
1029 : : */
1030 [ - + ]: 9328 : Assert(node != (Node *) query);
1031 : :
1032 : 9328 : context.root = root;
1033 : 9328 : context.query = query;
1034 : 9328 : context.sublevels_up = 0;
1035 : : /* flag whether grouping expressions could possibly contain SubLinks */
1036 : 9328 : context.possible_sublink = query->hasSubLinks;
1037 : : /* if hasSubLinks is already true, no need to work hard */
1038 : 9328 : context.inserted_sublink = query->hasSubLinks;
1039 : :
1040 : 9328 : return flatten_group_exprs_mutator(node, &context);
1041 : : }
1042 : :
1043 : : static Node *
1044 : 89881 : flatten_group_exprs_mutator(Node *node,
1045 : : flatten_join_alias_vars_context *context)
1046 : : {
1047 [ + + ]: 89881 : if (node == NULL)
1048 : 10388 : return NULL;
1049 [ + + ]: 79493 : if (IsA(node, Var))
1050 : : {
1051 : 15407 : Var *var = (Var *) node;
1052 : : RangeTblEntry *rte;
1053 : : Node *newvar;
1054 : :
1055 : : /* No change unless Var belongs to the GROUP of the target level */
1056 [ - + ]: 15407 : if (var->varlevelsup != context->sublevels_up)
740 rguo@postgresql.org 1057 :UBC 0 : return node; /* no need to copy, really */
740 rguo@postgresql.org 1058 :CBC 15407 : rte = rt_fetch(var->varno, context->query->rtable);
1059 [ + + ]: 15407 : if (rte->rtekind != RTE_GROUP)
1060 : 199 : return node;
1061 : :
1062 : : /* Expand group exprs reference */
1063 [ - + ]: 15208 : Assert(var->varattno > 0);
1064 : 15208 : newvar = (Node *) list_nth(rte->groupexprs, var->varattno - 1);
1065 [ - + ]: 15208 : Assert(newvar != NULL);
1066 : 15208 : newvar = copyObject(newvar);
1067 : :
1068 : : /*
1069 : : * If we are expanding an expr carried down from an upper query, must
1070 : : * adjust its varlevelsup fields.
1071 : : */
1072 [ - + ]: 15208 : if (context->sublevels_up != 0)
740 rguo@postgresql.org 1073 :UBC 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
1074 : :
1075 : : /* Preserve original Var's location, if possible */
740 rguo@postgresql.org 1076 [ + + ]:CBC 15208 : if (IsA(newvar, Var))
1077 : 12998 : ((Var *) newvar)->location = var->location;
1078 : :
1079 : : /* Detect if we are adding a sublink to query */
1080 [ + + - + ]: 15208 : if (context->possible_sublink && !context->inserted_sublink)
740 rguo@postgresql.org 1081 :UBC 0 : context->inserted_sublink = checkExprHasSubLink(newvar);
1082 : :
1083 : : /* Lastly, add any varnullingrels to the replacement expression */
740 rguo@postgresql.org 1084 :CBC 15208 : return mark_nullable_by_grouping(context->root, newvar, var);
1085 : : }
1086 : :
1087 [ + + ]: 64086 : if (IsA(node, Aggref))
1088 : : {
1089 : 6228 : Aggref *agg = (Aggref *) node;
1090 : :
1091 [ + - ]: 6228 : if ((int) agg->agglevelsup == context->sublevels_up)
1092 : : {
1093 : : /*
1094 : : * If we find an aggregate call of the original level, do not
1095 : : * recurse into its normal arguments, ORDER BY arguments, or
1096 : : * filter; there are no grouped vars there. But we should check
1097 : : * direct arguments as though they weren't in an aggregate.
1098 : : */
1099 : 6228 : agg = copyObject(agg);
1100 : 6228 : agg->aggdirectargs = (List *)
1101 : 6228 : flatten_group_exprs_mutator((Node *) agg->aggdirectargs, context);
1102 : :
1103 : 6228 : return (Node *) agg;
1104 : : }
1105 : :
1106 : : /*
1107 : : * We can skip recursing into aggregates of higher levels altogether,
1108 : : * since they could not possibly contain Vars of concern to us (see
1109 : : * transformAggregateCall). We do need to look at aggregates of lower
1110 : : * levels, however.
1111 : : */
740 rguo@postgresql.org 1112 [ # # ]:UBC 0 : if ((int) agg->agglevelsup > context->sublevels_up)
1113 : 0 : return node;
1114 : : }
1115 : :
740 rguo@postgresql.org 1116 [ + + ]:CBC 57858 : if (IsA(node, GroupingFunc))
1117 : : {
1118 : 307 : GroupingFunc *grp = (GroupingFunc *) node;
1119 : :
1120 : : /*
1121 : : * If we find a GroupingFunc node of the original or higher level, do
1122 : : * not recurse into its arguments; there are no grouped vars there.
1123 : : */
1124 [ + - ]: 307 : if ((int) grp->agglevelsup >= context->sublevels_up)
1125 : 307 : return node;
1126 : : }
1127 : :
1128 [ - + ]: 57551 : if (IsA(node, Query))
1129 : : {
1130 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1131 : : Query *newnode;
1132 : : bool save_inserted_sublink;
1133 : :
740 rguo@postgresql.org 1134 :UBC 0 : context->sublevels_up++;
1135 : 0 : save_inserted_sublink = context->inserted_sublink;
1136 : 0 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
1137 : 0 : newnode = query_tree_mutator((Query *) node,
1138 : : flatten_group_exprs_mutator,
1139 : : context,
1140 : : QTW_IGNORE_GROUPEXPRS);
1141 : 0 : newnode->hasSubLinks |= context->inserted_sublink;
1142 : 0 : context->inserted_sublink = save_inserted_sublink;
1143 : 0 : context->sublevels_up--;
1144 : 0 : return (Node *) newnode;
1145 : : }
1146 : :
740 rguo@postgresql.org 1147 :CBC 57551 : return expression_tree_mutator(node, flatten_group_exprs_mutator,
1148 : : context);
1149 : : }
1150 : :
1151 : : /*
1152 : : * Add oldvar's varnullingrels, if any, to a flattened grouping expression.
1153 : : * The newnode has been copied, so we can modify it freely.
1154 : : */
1155 : : static Node *
1156 : 15208 : mark_nullable_by_grouping(PlannerInfo *root, Node *newnode, Var *oldvar)
1157 : : {
1158 : : Relids relids;
1159 : :
1160 [ + + ]: 15208 : if (root == NULL)
1161 : 6229 : return newnode;
1162 [ + + ]: 8979 : if (oldvar->varnullingrels == NULL)
1163 : 7192 : return newnode; /* nothing to do */
1164 : :
1165 [ - + ]: 1787 : Assert(bms_equal(oldvar->varnullingrels,
1166 : : bms_make_singleton(root->group_rtindex)));
1167 : :
1168 : 1787 : relids = pull_varnos_of_level(root, newnode, oldvar->varlevelsup);
1169 : :
1170 [ + + ]: 1787 : if (!bms_is_empty(relids))
1171 : : {
1172 : : /*
1173 : : * If the newnode is not variable-free, we set the nullingrels of Vars
1174 : : * or PHVs that are contained in the expression. This is not really
1175 : : * 'correct' in theory, because it is the whole expression that can be
1176 : : * nullable by grouping sets, not its individual vars. But it works
1177 : : * in practice, because what we need is that the expression can be
1178 : : * somehow distinguished from the same expression in ECs, and marking
1179 : : * its vars is sufficient for this purpose.
1180 : : */
1181 : 1747 : newnode = add_nulling_relids(newnode,
1182 : : relids,
1183 : 1747 : oldvar->varnullingrels);
1184 : : }
1185 : : else /* variable-free? */
1186 : : {
1187 : : /*
1188 : : * If the newnode is variable-free and does not contain volatile
1189 : : * functions or set-returning functions, it can be treated as a member
1190 : : * of EC that is redundant. So wrap it in a new PlaceHolderVar to
1191 : : * carry the nullingrels. Otherwise we do not bother to make any
1192 : : * changes.
1193 : : *
1194 : : * Aggregate functions and window functions are not allowed in
1195 : : * grouping expressions.
1196 : : */
1197 [ - + ]: 40 : Assert(!contain_agg_clause(newnode));
1198 [ - + ]: 40 : Assert(!contain_window_function(newnode));
1199 : :
1200 [ + - ]: 40 : if (!contain_volatile_functions(newnode) &&
1201 [ + + ]: 40 : !expression_returns_set(newnode))
1202 : : {
1203 : : PlaceHolderVar *newphv;
1204 : : Relids phrels;
1205 : :
1206 : 25 : phrels = get_relids_in_jointree((Node *) root->parse->jointree,
1207 : : true, false);
1208 [ - + ]: 25 : Assert(!bms_is_empty(phrels));
1209 : :
1210 : 25 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1211 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1212 : 25 : newphv->phlevelsup = oldvar->varlevelsup;
1213 : 25 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1214 : 25 : newnode = (Node *) newphv;
1215 : : }
1216 : : }
1217 : :
1218 : 1787 : return newnode;
1219 : : }
1220 : :
1221 : : /*
1222 : : * Add oldvar's varnullingrels, if any, to a flattened join alias expression.
1223 : : * The newnode has been copied, so we can modify it freely.
1224 : : */
1225 : : static Node *
1329 tgl@sss.pgh.pa.us 1226 : 574 : add_nullingrels_if_needed(PlannerInfo *root, Node *newnode, Var *oldvar)
1227 : : {
1228 [ + + ]: 574 : if (oldvar->varnullingrels == NULL)
1229 : 448 : return newnode; /* nothing to do */
1230 : : /* If possible, do it by adding to existing nullingrel fields */
1231 [ + + ]: 126 : if (is_standard_join_alias_expression(newnode, oldvar))
1232 : 116 : adjust_standard_join_alias_expression(newnode, oldvar);
1233 [ + - ]: 10 : else if (root)
1234 : : {
1235 : : /*
1236 : : * We can insert a PlaceHolderVar to carry the nullingrels. However,
1237 : : * deciding where to evaluate the PHV is slightly tricky. We first
1238 : : * try to evaluate it at the natural semantic level of the new
1239 : : * expression; but if that expression is variable-free, fall back to
1240 : : * evaluating it at the join that the oldvar is an alias Var for.
1241 : : */
1242 : : PlaceHolderVar *newphv;
1243 : 10 : Index levelsup = oldvar->varlevelsup;
1244 : 10 : Relids phrels = pull_varnos_of_level(root, newnode, levelsup);
1245 : :
1246 [ + - ]: 10 : if (bms_is_empty(phrels)) /* variable-free? */
1247 : : {
1248 [ - + ]: 10 : if (levelsup != 0) /* this won't work otherwise */
1329 tgl@sss.pgh.pa.us 1249 [ # # ]:UBC 0 : elog(ERROR, "unsupported join alias expression");
1329 tgl@sss.pgh.pa.us 1250 :CBC 10 : phrels = get_relids_for_join(root->parse, oldvar->varno);
1251 : : /* If it's an outer join, eval below not above the join */
1252 : 10 : phrels = bms_del_member(phrels, oldvar->varno);
1253 [ - + ]: 10 : Assert(!bms_is_empty(phrels));
1254 : : }
1255 : 10 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1256 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1257 : 10 : newphv->phlevelsup = levelsup;
1258 : 10 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1259 : 10 : newnode = (Node *) newphv;
1260 : : }
1261 : : else
1262 : : {
1263 : : /* ooops, we're missing support for something the parser can make */
1329 tgl@sss.pgh.pa.us 1264 [ # # ]:UBC 0 : elog(ERROR, "unsupported join alias expression");
1265 : : }
1329 tgl@sss.pgh.pa.us 1266 :CBC 126 : return newnode;
1267 : : }
1268 : :
1269 : : /*
1270 : : * Check to see if we can insert nullingrels into this join alias expression
1271 : : * without use of a separate PlaceHolderVar.
1272 : : *
1273 : : * This will handle Vars, PlaceHolderVars, and implicit-coercion and COALESCE
1274 : : * expressions built from those. This coverage needs to handle anything
1275 : : * that the parser would put into joinaliasvars.
1276 : : */
1277 : : static bool
1278 : 382 : is_standard_join_alias_expression(Node *newnode, Var *oldvar)
1279 : : {
1280 [ - + ]: 382 : if (newnode == NULL)
1329 tgl@sss.pgh.pa.us 1281 :UBC 0 : return false;
1329 tgl@sss.pgh.pa.us 1282 [ + + ]:CBC 382 : if (IsA(newnode, Var) &&
1283 [ + - ]: 226 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1284 : 226 : return true;
1285 [ - + ]: 156 : else if (IsA(newnode, PlaceHolderVar) &&
1329 tgl@sss.pgh.pa.us 1286 [ # # ]:UBC 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1287 : 0 : return true;
1329 tgl@sss.pgh.pa.us 1288 [ + + ]:CBC 156 : else if (IsA(newnode, FuncExpr))
1289 : : {
1290 : 23 : FuncExpr *fexpr = (FuncExpr *) newnode;
1291 : :
1292 : : /*
1293 : : * We need to assume that the function wouldn't produce non-NULL from
1294 : : * NULL, which is reasonable for implicit coercions but otherwise not
1295 : : * so much. (Looking at its strictness is likely overkill, and anyway
1296 : : * it would cause us to fail if someone forgot to mark an implicit
1297 : : * coercion as strict.)
1298 : : */
1299 [ + - ]: 23 : if (fexpr->funcformat != COERCE_IMPLICIT_CAST ||
1300 [ - + ]: 23 : fexpr->args == NIL)
1329 tgl@sss.pgh.pa.us 1301 :UBC 0 : return false;
1302 : :
1303 : : /*
1304 : : * Examine only the first argument --- coercions might have additional
1305 : : * arguments that are constants.
1306 : : */
1329 tgl@sss.pgh.pa.us 1307 :CBC 23 : return is_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1308 : : }
1309 [ + + ]: 133 : else if (IsA(newnode, RelabelType))
1310 : : {
1311 : 13 : RelabelType *relabel = (RelabelType *) newnode;
1312 : :
1313 : : /* This definitely won't produce non-NULL from NULL */
1314 : 13 : return is_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1315 : : }
1316 [ - + ]: 120 : else if (IsA(newnode, CoerceViaIO))
1317 : : {
1329 tgl@sss.pgh.pa.us 1318 :UBC 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1319 : :
1320 : : /* This definitely won't produce non-NULL from NULL */
1321 : 0 : return is_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1322 : : }
1329 tgl@sss.pgh.pa.us 1323 [ - + ]:CBC 120 : else if (IsA(newnode, ArrayCoerceExpr))
1324 : : {
1329 tgl@sss.pgh.pa.us 1325 :UBC 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1326 : :
1327 : : /* This definitely won't produce non-NULL from NULL (at array level) */
1328 : 0 : return is_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1329 : : }
1329 tgl@sss.pgh.pa.us 1330 [ + + ]:CBC 120 : else if (IsA(newnode, CoalesceExpr))
1331 : : {
1332 : 110 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1333 : : ListCell *lc;
1334 : :
1335 [ - + ]: 110 : Assert(cexpr->args != NIL);
1336 [ + - + + : 330 : foreach(lc, cexpr->args)
+ + ]
1337 : : {
1338 [ - + ]: 220 : if (!is_standard_join_alias_expression(lfirst(lc), oldvar))
1329 tgl@sss.pgh.pa.us 1339 :UBC 0 : return false;
1340 : : }
1329 tgl@sss.pgh.pa.us 1341 :CBC 110 : return true;
1342 : : }
1343 : : else
1344 : 10 : return false;
1345 : : }
1346 : :
1347 : : /*
1348 : : * Insert nullingrels into an expression accepted by
1349 : : * is_standard_join_alias_expression.
1350 : : */
1351 : : static void
1352 : 362 : adjust_standard_join_alias_expression(Node *newnode, Var *oldvar)
1353 : : {
1354 [ + + ]: 362 : if (IsA(newnode, Var) &&
1355 [ + - ]: 226 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1356 : 226 : {
1357 : 226 : Var *newvar = (Var *) newnode;
1358 : :
1359 : 226 : newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
1360 : 226 : oldvar->varnullingrels);
1361 : : }
1362 [ - + ]: 136 : else if (IsA(newnode, PlaceHolderVar) &&
1329 tgl@sss.pgh.pa.us 1363 [ # # ]:UBC 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1364 : 0 : {
1365 : 0 : PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
1366 : :
1367 : 0 : newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
1368 : 0 : oldvar->varnullingrels);
1369 : : }
1329 tgl@sss.pgh.pa.us 1370 [ + + ]:CBC 136 : else if (IsA(newnode, FuncExpr))
1371 : : {
1372 : 13 : FuncExpr *fexpr = (FuncExpr *) newnode;
1373 : :
1374 : 13 : adjust_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1375 : : }
1376 [ + + ]: 123 : else if (IsA(newnode, RelabelType))
1377 : : {
1378 : 13 : RelabelType *relabel = (RelabelType *) newnode;
1379 : :
1380 : 13 : adjust_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1381 : : }
1382 [ - + ]: 110 : else if (IsA(newnode, CoerceViaIO))
1383 : : {
1329 tgl@sss.pgh.pa.us 1384 :UBC 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1385 : :
1386 : 0 : adjust_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1387 : : }
1329 tgl@sss.pgh.pa.us 1388 [ - + ]:CBC 110 : else if (IsA(newnode, ArrayCoerceExpr))
1389 : : {
1329 tgl@sss.pgh.pa.us 1390 :UBC 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1391 : :
1392 : 0 : adjust_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1393 : : }
1329 tgl@sss.pgh.pa.us 1394 [ + - ]:CBC 110 : else if (IsA(newnode, CoalesceExpr))
1395 : : {
1396 : 110 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1397 : : ListCell *lc;
1398 : :
1399 [ - + ]: 110 : Assert(cexpr->args != NIL);
1400 [ + - + + : 330 : foreach(lc, cexpr->args)
+ + ]
1401 : : {
1402 : 220 : adjust_standard_join_alias_expression(lfirst(lc), oldvar);
1403 : : }
1404 : : }
1405 : : else
1329 tgl@sss.pgh.pa.us 1406 :UBC 0 : Assert(false);
1329 tgl@sss.pgh.pa.us 1407 :CBC 362 : }
1408 : :
1409 : : /*
1410 : : * alias_relid_set: in a set of RT indexes, replace joins by their
1411 : : * underlying base+OJ relids
1412 : : */
1413 : : static Relids
2791 1414 : 1992 : alias_relid_set(Query *query, Relids relids)
1415 : : {
8625 1416 : 1992 : Relids result = NULL;
1417 : : int rtindex;
1418 : :
4314 1419 : 1992 : rtindex = -1;
1420 [ + + ]: 5124 : while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1421 : : {
2791 1422 : 3132 : RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
1423 : :
8644 1424 [ + + ]: 3132 : if (rte->rtekind == RTE_JOIN)
2791 1425 : 437 : result = bms_join(result, get_relids_for_join(query, rtindex));
1426 : : else
8625 1427 : 2695 : result = bms_add_member(result, rtindex);
1428 : : }
8644 1429 : 1992 : return result;
1430 : : }
|