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
1930 tgl@sss.pgh.pa.us 114 :CBC 2656629 : pull_varnos(PlannerInfo *root, Node *node)
115 : : {
116 : : pull_varnos_context context;
117 : :
8487 118 : 2656629 : context.varnos = NULL;
1930 119 : 2656629 : context.root = root;
9366 120 : 2656629 : 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 : : */
8509 126 : 2656629 : query_or_expression_tree_walker(node,
127 : : pull_varnos_walker,
128 : : &context,
129 : : 0);
130 : :
8487 131 : 2656629 : 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
1930 140 : 7911 : pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
141 : : {
142 : : pull_varnos_context context;
143 : :
5019 144 : 7911 : context.varnos = NULL;
1930 145 : 7911 : context.root = root;
5019 146 : 7911 : 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 : 7911 : query_or_expression_tree_walker(node,
153 : : pull_varnos_walker,
154 : : &context,
155 : : 0);
156 : :
157 : 7911 : return context.varnos;
158 : : }
159 : :
160 : : static bool
9366 161 : 4679023 : pull_varnos_walker(Node *node, pull_varnos_context *context)
162 : : {
9817 163 [ + + ]: 4679023 : if (node == NULL)
164 : 190548 : return false;
165 [ + + ]: 4488475 : if (IsA(node, Var))
166 : : {
9519 bruce@momjian.us 167 : 2243299 : Var *var = (Var *) node;
168 : :
8487 tgl@sss.pgh.pa.us 169 [ + + ]: 2243299 : if (var->varlevelsup == context->sublevels_up)
170 : : {
171 : 2232713 : context->varnos = bms_add_member(context->varnos, var->varno);
1191 172 : 2232713 : context->varnos = bms_add_members(context->varnos,
173 : 2232713 : var->varnullingrels);
174 : : }
9817 175 : 2243299 : return false;
176 : : }
6903 177 [ + + ]: 2245176 : 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 : : }
6405 185 [ + + ]: 2244496 : if (IsA(node, PlaceHolderVar))
186 : : {
187 : 4877 : 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 : : */
1577 197 [ + - ]: 4877 : if (phv->phlevelsup == context->sublevels_up &&
198 [ + - ]: 4877 : 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 : : */
1930 217 : 4877 : PlaceHolderInfo *phinfo = NULL;
218 : :
219 [ + + ]: 4877 : if (phv->phlevelsup == 0)
220 : : {
1357 221 [ + + ]: 4797 : if (phv->phid < context->root->placeholder_array_size)
222 : 3539 : phinfo = context->root->placeholder_array[phv->phid];
223 : : }
1691 224 [ + + ]: 4877 : if (phinfo == NULL)
225 : : {
226 : : /* No PlaceHolderInfo yet, use phrels */
227 : 1348 : context->varnos = bms_add_members(context->varnos,
228 : 1348 : phv->phrels);
229 : : }
230 [ + + ]: 3529 : else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
231 : : {
232 : : /* Normal case: use ph_eval_at */
1930 233 : 3260 : context->varnos = bms_add_members(context->varnos,
234 : 3260 : 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 ... */
1691 243 : 269 : delta = bms_difference(phinfo->ph_var->phrels, phv->phrels);
244 : 269 : newevalat = bms_difference(phinfo->ph_eval_at, delta);
245 : : /* ... then if that was in fact part of ph_eval_at ... */
246 [ + + ]: 269 : if (!bms_equal(newevalat, phinfo->ph_eval_at))
247 : : {
248 : : /* ... add what was added */
249 : 190 : delta = bms_difference(phv->phrels, phinfo->ph_var->phrels);
250 : 190 : newevalat = bms_join(newevalat, delta);
251 : : }
252 : 269 : 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 : : */
1191 261 : 9754 : context->varnos = bms_add_members(context->varnos,
262 : 4877 : phv->phnullingrels);
1930 263 : 4877 : return false; /* don't recurse into expression */
264 : : }
265 : : }
266 [ + + ]: 2239619 : else if (IsA(node, Query))
267 : : {
268 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
269 : : bool result;
270 : :
9366 271 : 5973 : context->sublevels_up++;
272 : 5973 : result = query_tree_walker((Query *) node, pull_varnos_walker,
273 : : context, 0);
274 : 5973 : context->sublevels_up--;
275 : 5973 : return result;
276 : : }
523 peter@eisentraut.org 277 : 2233646 : 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
5324 tgl@sss.pgh.pa.us 296 : 1316274 : pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
297 : : {
298 : : pull_varattnos_context context;
299 : :
300 : 1316274 : context.varattnos = *varattnos;
301 : 1316274 : context.varno = varno;
302 : :
303 : 1316274 : (void) pull_varattnos_walker(node, &context);
304 : :
305 : 1316274 : *varattnos = context.varattnos;
6802 306 : 1316274 : }
307 : :
308 : : static bool
5324 309 : 4454620 : pull_varattnos_walker(Node *node, pull_varattnos_context *context)
310 : : {
6802 311 [ + + ]: 4454620 : if (node == NULL)
312 : 54892 : return false;
313 [ + + ]: 4399728 : if (IsA(node, Var))
314 : : {
315 : 2554538 : Var *var = (Var *) node;
316 : :
5324 317 [ + + + - ]: 2554538 : if (var->varno == context->varno && var->varlevelsup == 0)
318 : 2554207 : context->varattnos =
319 : 2554207 : bms_add_member(context->varattnos,
3240 320 : 2554207 : var->varattno - FirstLowInvalidHeapAttributeNumber);
6802 321 : 2554538 : return false;
322 : : }
323 : :
324 : : /* Should not find an unplanned subquery */
325 [ - + ]: 1845190 : Assert(!IsA(node, Query));
326 : :
523 peter@eisentraut.org 327 : 1845190 : 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 *
5019 tgl@sss.pgh.pa.us 339 : 6911 : pull_vars_of_level(Node *node, int levelsup)
340 : : {
341 : : pull_vars_context context;
342 : :
343 : 6911 : context.vars = NIL;
344 : 6911 : 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 : 6911 : query_or_expression_tree_walker(node,
351 : : pull_vars_walker,
352 : : &context,
353 : : 0);
354 : :
355 : 6911 : return context.vars;
356 : : }
357 : :
358 : : static bool
359 : 156390 : pull_vars_walker(Node *node, pull_vars_context *context)
360 : : {
361 [ + + ]: 156390 : if (node == NULL)
362 : 37080 : return false;
363 [ + + ]: 119310 : if (IsA(node, Var))
364 : : {
365 : 32329 : Var *var = (Var *) node;
366 : :
367 [ + + ]: 32329 : if (var->varlevelsup == context->sublevels_up)
368 : 16324 : context->vars = lappend(context->vars, var);
369 : 32329 : return false;
370 : : }
5008 371 [ + + ]: 86981 : if (IsA(node, PlaceHolderVar))
372 : : {
373 : 113 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
374 : :
375 [ + - ]: 113 : if (phv->phlevelsup == context->sublevels_up)
376 : 113 : context->vars = lappend(context->vars, phv);
377 : : /* we don't want to look into the contained expression */
378 : 113 : return false;
379 : : }
5019 380 [ + + ]: 86868 : if (IsA(node, Query))
381 : : {
382 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
383 : : bool result;
384 : :
385 : 1027 : context->sublevels_up++;
386 : 1027 : result = query_tree_walker((Query *) node, pull_vars_walker,
387 : : context, 0);
388 : 1027 : context->sublevels_up--;
389 : 1027 : return result;
390 : : }
523 peter@eisentraut.org 391 : 85841 : 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
9148 tgl@sss.pgh.pa.us 406 : 29277 : contain_var_clause(Node *node)
407 : : {
408 : 29277 : return contain_var_clause_walker(node, NULL);
409 : : }
410 : :
411 : : static bool
9817 412 : 35342 : contain_var_clause_walker(Node *node, void *context)
413 : : {
414 [ + + ]: 35342 : if (node == NULL)
415 : 84 : return false;
416 [ + + ]: 35258 : if (IsA(node, Var))
417 : : {
9749 418 [ + - ]: 2384 : if (((Var *) node)->varlevelsup == 0)
7507 bruce@momjian.us 419 : 2384 : return true; /* abort the tree traversal and return true */
9749 tgl@sss.pgh.pa.us 420 :UBC 0 : return false;
421 : : }
6903 tgl@sss.pgh.pa.us 422 [ - + ]:CBC 32874 : if (IsA(node, CurrentOfExpr))
6903 tgl@sss.pgh.pa.us 423 :UBC 0 : return true;
6405 tgl@sss.pgh.pa.us 424 [ - + ]:CBC 32874 : if (IsA(node, PlaceHolderVar))
425 : : {
6405 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 : : }
9817 tgl@sss.pgh.pa.us 430 :CBC 32874 : 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
8506 444 : 122731 : contain_vars_of_level(Node *node, int levelsup)
445 : : {
8310 bruce@momjian.us 446 : 122731 : int sublevels_up = levelsup;
447 : :
8506 tgl@sss.pgh.pa.us 448 : 122731 : return query_or_expression_tree_walker(node,
449 : : contain_vars_of_level_walker,
450 : : &sublevels_up,
451 : : 0);
452 : : }
453 : :
454 : : static bool
455 : 451984 : contain_vars_of_level_walker(Node *node, int *sublevels_up)
456 : : {
457 [ + + ]: 451984 : if (node == NULL)
458 : 116321 : return false;
459 [ + + ]: 335663 : if (IsA(node, Var))
460 : : {
461 [ + + ]: 57793 : if (((Var *) node)->varlevelsup == *sublevels_up)
462 : 36009 : return true; /* abort tree traversal and return true */
6903 463 : 21784 : return false;
464 : : }
465 [ + + ]: 277870 : if (IsA(node, CurrentOfExpr))
466 : : {
467 [ + - ]: 152 : if (*sublevels_up == 0)
468 : 152 : return true;
6903 tgl@sss.pgh.pa.us 469 :UBC 0 : return false;
470 : : }
6405 tgl@sss.pgh.pa.us 471 [ + + ]:CBC 277718 : if (IsA(node, PlaceHolderVar))
472 : : {
473 [ + + ]: 120 : if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
474 : 115 : return true; /* abort the tree traversal and return true */
475 : : /* else fall through to check the contained expr */
476 : : }
8506 477 [ + + ]: 277603 : if (IsA(node, Query))
478 : : {
479 : : /* Recurse into subselects */
480 : : bool result;
481 : :
482 : 527 : (*sublevels_up)++;
483 : 527 : result = query_tree_walker((Query *) node,
484 : : contain_vars_of_level_walker,
485 : : sublevels_up,
486 : : 0);
487 : 527 : (*sublevels_up)--;
488 : 527 : return result;
489 : : }
490 : 277076 : 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
474 dean.a.rasheed@gmail 511 : 202 : contain_vars_returning_old_or_new(Node *node)
512 : : {
513 : 202 : return contain_vars_returning_old_or_new_walker(node, NULL);
514 : : }
515 : :
516 : : static bool
517 : 614 : contain_vars_returning_old_or_new_walker(Node *node, void *context)
518 : : {
519 [ + + ]: 614 : if (node == NULL)
520 : 144 : return false;
521 [ + + ]: 470 : if (IsA(node, Var))
522 : : {
523 [ + - ]: 201 : if (((Var *) node)->varlevelsup == 0 &&
524 [ + + ]: 201 : ((Var *) node)->varreturningtype != VAR_RETURNING_DEFAULT)
525 : 7 : return true; /* abort the tree traversal and return true */
526 : 194 : return false;
527 : : }
528 [ - + ]: 269 : if (IsA(node, ReturningExpr))
529 : : {
474 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 : : }
474 dean.a.rasheed@gmail 534 :CBC 269 : 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
6455 tgl@sss.pgh.pa.us 555 : 8 : locate_var_of_level(Node *node, int levelsup)
556 : : {
557 : : locate_var_of_level_context context;
558 : :
6172 bruce@momjian.us 559 : 8 : context.var_location = -1; /* in case we find nothing */
6455 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 : : {
8506 574 [ - + ]: 20 : if (node == NULL)
8506 tgl@sss.pgh.pa.us 575 :UBC 0 : return false;
8506 tgl@sss.pgh.pa.us 576 [ + + ]:CBC 20 : if (IsA(node, Var))
577 : : {
6172 bruce@momjian.us 578 : 8 : Var *var = (Var *) node;
579 : :
6455 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;
8506 584 : 8 : return true; /* abort tree traversal and return true */
585 : : }
6455 tgl@sss.pgh.pa.us 586 :UBC 0 : return false;
587 : : }
6455 tgl@sss.pgh.pa.us 588 [ - + ]:CBC 12 : if (IsA(node, CurrentOfExpr))
589 : : {
590 : : /* since CurrentOfExpr doesn't carry location, nothing we can do */
6455 tgl@sss.pgh.pa.us 591 :UBC 0 : return false;
592 : : }
593 : : /* No extra code needed for PlaceHolderVar; just look in contained expr */
8506 tgl@sss.pgh.pa.us 594 [ - + ]:CBC 12 : if (IsA(node, Query))
595 : : {
596 : : /* Recurse into subselects */
597 : : bool result;
598 : :
6455 tgl@sss.pgh.pa.us 599 :UBC 0 : context->sublevels_up++;
8506 600 : 0 : result = query_tree_walker((Query *) node,
601 : : locate_var_of_level_walker,
602 : : context,
603 : : 0);
6455 604 : 0 : context->sublevels_up--;
8506 605 : 0 : return result;
606 : : }
8506 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 *
3708 653 : 672039 : 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 [ - + ]: 672039 : Assert((flags & (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES))
659 : : != (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES));
660 [ - + ]: 672039 : Assert((flags & (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS))
661 : : != (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS));
662 [ - + ]: 672039 : Assert((flags & (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS))
663 : : != (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS));
664 : :
8008 665 : 672039 : context.varlist = NIL;
3708 666 : 672039 : context.flags = flags;
667 : :
9148 668 : 672039 : pull_var_clause_walker(node, &context);
8008 669 : 672039 : return context.varlist;
670 : : }
671 : :
672 : : static bool
9749 673 : 3900351 : pull_var_clause_walker(Node *node, pull_var_clause_context *context)
674 : : {
9817 675 [ + + ]: 3900351 : if (node == NULL)
676 : 162054 : return false;
677 [ + + ]: 3738297 : if (IsA(node, Var))
678 : : {
5411 679 [ - + ]: 1449664 : if (((Var *) node)->varlevelsup != 0)
5411 tgl@sss.pgh.pa.us 680 [ # # ]:UBC 0 : elog(ERROR, "Upper-level Var found where not expected");
5411 tgl@sss.pgh.pa.us 681 :CBC 1449664 : context->varlist = lappend(context->varlist, node);
9817 682 : 1449664 : return false;
683 : : }
5411 684 [ + + ]: 2288633 : else if (IsA(node, Aggref))
685 : : {
686 [ - + ]: 86629 : if (((Aggref *) node)->agglevelsup != 0)
5411 tgl@sss.pgh.pa.us 687 [ # # ]:UBC 0 : elog(ERROR, "Upper-level Aggref found where not expected");
3708 tgl@sss.pgh.pa.us 688 [ + + ]:CBC 86629 : if (context->flags & PVC_INCLUDE_AGGREGATES)
689 : : {
690 : 6763 : context->varlist = lappend(context->varlist, node);
691 : : /* we do NOT descend into the contained expression */
692 : 6763 : return false;
693 : : }
694 [ - + ]: 79866 : else if (context->flags & PVC_RECURSE_AGGREGATES)
695 : : {
696 : : /* fall through to recurse into the aggregate's arguments */
697 : : }
698 : : else
3708 tgl@sss.pgh.pa.us 699 [ # # ]:UBC 0 : elog(ERROR, "Aggref found where not expected");
700 : : }
4007 andres@anarazel.de 701 [ + + ]:CBC 2202004 : else if (IsA(node, GroupingFunc))
702 : : {
703 [ - + ]: 557 : if (((GroupingFunc *) node)->agglevelsup != 0)
4007 andres@anarazel.de 704 [ # # ]:UBC 0 : elog(ERROR, "Upper-level GROUPING found where not expected");
3708 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
3708 tgl@sss.pgh.pa.us 716 [ # # ]:UBC 0 : elog(ERROR, "GROUPING found where not expected");
717 : : }
3708 tgl@sss.pgh.pa.us 718 [ + + ]:CBC 2201447 : else if (IsA(node, WindowFunc))
719 : : {
720 : : /* WindowFuncs have no levelsup field to check ... */
721 [ + + ]: 6337 : if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
722 : : {
723 : 62 : context->varlist = lappend(context->varlist, node);
724 : : /* we do NOT descend into the contained expressions */
725 : 62 : return false;
726 : : }
727 [ - + ]: 6275 : else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
728 : : {
729 : : /* fall through to recurse into the windowfunc's arguments */
730 : : }
731 : : else
3708 tgl@sss.pgh.pa.us 732 [ # # ]:UBC 0 : elog(ERROR, "WindowFunc found where not expected");
733 : : }
5411 tgl@sss.pgh.pa.us 734 [ + + ]:CBC 2195110 : else if (IsA(node, PlaceHolderVar))
735 : : {
736 [ - + ]: 5897 : if (((PlaceHolderVar *) node)->phlevelsup != 0)
5411 tgl@sss.pgh.pa.us 737 [ # # ]:UBC 0 : elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
3708 tgl@sss.pgh.pa.us 738 [ + + ]:CBC 5897 : if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
739 : : {
740 : 4487 : context->varlist = lappend(context->varlist, node);
741 : : /* we do NOT descend into the contained expression */
742 : 4487 : return false;
743 : : }
744 [ - + ]: 1410 : else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
745 : : {
746 : : /* fall through to recurse into the placeholder's expression */
747 : : }
748 : : else
3708 tgl@sss.pgh.pa.us 749 [ # # ]:UBC 0 : elog(ERROR, "PlaceHolderVar found where not expected");
750 : : }
523 peter@eisentraut.org 751 :CBC 2277319 : 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 *
1191 tgl@sss.pgh.pa.us 781 : 198259 : 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 [ - + ]: 198259 : Assert(node != (Node *) query);
791 : :
792 : 198259 : context.root = root;
2653 793 : 198259 : context.query = query;
8511 794 : 198259 : context.sublevels_up = 0;
795 : : /* flag whether join aliases could possibly contain SubLinks */
60 796 : 198259 : context.possible_sublink = query->hasSubLinks;
797 : : /* if hasSubLinks is already true, no need to work hard */
798 : 198259 : context.inserted_sublink = query->hasSubLinks;
799 : :
800 : 198259 : 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 *
60 tgl@sss.pgh.pa.us 819 :GNC 3827 : 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 [ - + ]: 3827 : Assert(node != (Node *) query);
829 : :
830 : 3827 : context.root = NULL;
831 : 3827 : context.query = query;
832 : 3827 : context.sublevels_up = sublevels_up;
833 : : /* flag whether join aliases could possibly contain SubLinks */
2653 834 : 3827 : context.possible_sublink = query->hasSubLinks;
835 : : /* if hasSubLinks is already true, no need to work hard */
836 : 3827 : context.inserted_sublink = query->hasSubLinks;
837 : :
8820 838 : 3827 : return flatten_join_alias_vars_mutator(node, &context);
839 : : }
840 : :
841 : : static Node *
8820 tgl@sss.pgh.pa.us 842 :CBC 2865026 : flatten_join_alias_vars_mutator(Node *node,
843 : : flatten_join_alias_vars_context *context)
844 : : {
845 [ + + ]: 2865026 : if (node == NULL)
846 : 253608 : return NULL;
847 [ + + ]: 2611418 : if (IsA(node, Var))
848 : : {
8644 bruce@momjian.us 849 : 772475 : Var *var = (Var *) node;
850 : : RangeTblEntry *rte;
851 : : Node *newvar;
852 : :
853 : : /* No change unless Var belongs to a JOIN of the target level */
8511 tgl@sss.pgh.pa.us 854 [ + + ]: 772475 : if (var->varlevelsup != context->sublevels_up)
8820 855 : 43141 : return node; /* no need to copy, really */
2653 856 : 729334 : rte = rt_fetch(var->varno, context->query->rtable);
8820 857 [ + + ]: 729334 : if (rte->rtekind != RTE_JOIN)
8773 858 : 728790 : return node;
8030 859 [ + + ]: 544 : if (var->varattno == InvalidAttrNumber)
860 : : {
861 : : /* Must expand whole-row reference */
862 : : RowExpr *rowexpr;
7919 bruce@momjian.us 863 : 5 : List *fields = NIL;
5194 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 */
4669 873 [ - + ]: 35 : if (newvar == NULL)
7929 tgl@sss.pgh.pa.us 874 :UBC 0 : continue;
5299 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 : : */
8030 881 [ - + ]: 35 : if (context->sublevels_up != 0)
8030 tgl@sss.pgh.pa.us 882 :UBC 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
883 : : /* Preserve original Var's location, if possible */
5299 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) */
8030 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 */
5194 891 : 35 : colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
892 : : }
8030 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 */
5194 898 : 5 : rowexpr->colnames = colnames;
5299 899 : 5 : rowexpr->location = var->location;
900 : :
901 : : /* Lastly, add any varnullingrels to the replacement expression */
1191 902 : 5 : return add_nullingrels_if_needed(context->root, (Node *) rowexpr,
903 : : var);
904 : : }
905 : :
906 : : /* Expand join alias reference */
8773 907 [ - + ]: 539 : Assert(var->varattno > 0);
8010 neilc@samurai.com 908 : 539 : newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
4669 tgl@sss.pgh.pa.us 909 [ - + ]: 539 : Assert(newvar != NULL);
5299 910 : 539 : 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 : : */
8489 916 [ + + ]: 539 : if (context->sublevels_up != 0)
8489 tgl@sss.pgh.pa.us 917 :GBC 28 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
918 : :
919 : : /* Preserve original Var's location, if possible */
5299 tgl@sss.pgh.pa.us 920 [ - + ]:CBC 539 : if (IsA(newvar, Var))
5299 tgl@sss.pgh.pa.us 921 :UBC 0 : ((Var *) newvar)->location = var->location;
922 : :
923 : : /* Recurse in case join input is itself a join */
5780 tgl@sss.pgh.pa.us 924 :CBC 539 : newvar = flatten_join_alias_vars_mutator(newvar, context);
925 : :
926 : : /* Detect if we are adding a sublink to query */
927 [ + + + + ]: 539 : if (context->possible_sublink && !context->inserted_sublink)
5780 tgl@sss.pgh.pa.us 928 :GBC 20 : context->inserted_sublink = checkExprHasSubLink(newvar);
929 : :
930 : : /* Lastly, add any varnullingrels to the replacement expression */
1191 tgl@sss.pgh.pa.us 931 :CBC 539 : return add_nullingrels_if_needed(context->root, newvar, var);
932 : : }
6405 933 [ + + ]: 1838943 : if (IsA(node, PlaceHolderVar))
934 : : {
935 : : /* Copy the PlaceHolderVar node with correct mutation of subnodes */
936 : : PlaceHolderVar *phv;
937 : :
938 : 1850 : phv = (PlaceHolderVar *) expression_tree_mutator(node,
939 : : flatten_join_alias_vars_mutator,
940 : : context);
941 : : /* now fix PlaceHolderVar's relid sets */
942 [ + + ]: 1850 : if (phv->phlevelsup == context->sublevels_up)
943 : : {
2653 944 : 1770 : phv->phrels = alias_relid_set(context->query,
945 : : phv->phrels);
946 : : /* we *don't* change phnullingrels */
947 : : }
6405 948 : 1850 : return (Node *) phv;
949 : : }
950 : :
8511 951 [ + + ]: 1837093 : if (IsA(node, Query))
952 : : {
953 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
954 : : Query *newnode;
955 : : bool save_inserted_sublink;
956 : :
957 : 12581 : context->sublevels_up++;
5780 958 : 12581 : save_inserted_sublink = context->inserted_sublink;
959 : 12581 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
8509 960 : 12581 : newnode = query_tree_mutator((Query *) node,
961 : : flatten_join_alias_vars_mutator,
962 : : context,
963 : : QTW_IGNORE_JOINALIASES);
5780 964 : 12581 : newnode->hasSubLinks |= context->inserted_sublink;
965 : 12581 : context->inserted_sublink = save_inserted_sublink;
8511 966 : 12581 : context->sublevels_up--;
967 : 12581 : return (Node *) newnode;
968 : : }
969 : : /* Already-planned tree not supported */
6465 970 [ - + ]: 1824512 : Assert(!IsA(node, SubPlan));
602 rguo@postgresql.org 971 [ - + ]: 1824512 : Assert(!IsA(node, AlternativeSubPlan));
972 : : /* Shouldn't need to handle these planner auxiliary nodes here */
6404 tgl@sss.pgh.pa.us 973 [ - + ]: 1824512 : Assert(!IsA(node, SpecialJoinInfo));
974 [ - + ]: 1824512 : Assert(!IsA(node, PlaceHolderInfo));
5661 975 [ - + ]: 1824512 : Assert(!IsA(node, MinMaxAggInfo));
976 : :
523 peter@eisentraut.org 977 : 1824512 : return expression_tree_mutator(node, flatten_join_alias_vars_mutator, context);
978 : : }
979 : :
980 : : /*
981 : : * flatten_group_exprs
982 : : * Replace Vars that reference GROUP outputs with the underlying grouping
983 : : * expressions.
984 : : *
985 : : * We have to preserve any varnullingrels info attached to the group Vars we're
986 : : * replacing. If the replacement expression is a Var or PlaceHolderVar or
987 : : * constructed from those, we can just add the varnullingrels bits to the
988 : : * existing nullingrels field(s); otherwise we have to add a PlaceHolderVar
989 : : * wrapper.
990 : : *
991 : : * NOTE: root may be passed as NULL, which is why we have to pass the Query
992 : : * separately. We need the root itself only for preserving varnullingrels.
993 : : * Callers can safely pass NULL if preserving varnullingrels is unnecessary for
994 : : * their specific use case (e.g., deparsing source text, or scanning for
995 : : * volatile functions), or if it is already guaranteed that the query cannot
996 : : * contain grouping sets.
997 : : */
998 : : Node *
602 rguo@postgresql.org 999 : 9042 : flatten_group_exprs(PlannerInfo *root, Query *query, Node *node)
1000 : : {
1001 : : flatten_join_alias_vars_context context;
1002 : :
1003 : : /*
1004 : : * We do not expect this to be applied to the whole Query, only to
1005 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
1006 : : * it's okay to immediately increment sublevels_up.
1007 : : */
1008 [ - + ]: 9042 : Assert(node != (Node *) query);
1009 : :
1010 : 9042 : context.root = root;
1011 : 9042 : context.query = query;
1012 : 9042 : context.sublevels_up = 0;
1013 : : /* flag whether grouping expressions could possibly contain SubLinks */
1014 : 9042 : context.possible_sublink = query->hasSubLinks;
1015 : : /* if hasSubLinks is already true, no need to work hard */
1016 : 9042 : context.inserted_sublink = query->hasSubLinks;
1017 : :
1018 : 9042 : return flatten_group_exprs_mutator(node, &context);
1019 : : }
1020 : :
1021 : : static Node *
1022 : 85340 : flatten_group_exprs_mutator(Node *node,
1023 : : flatten_join_alias_vars_context *context)
1024 : : {
1025 [ + + ]: 85340 : if (node == NULL)
1026 : 10016 : return NULL;
1027 [ + + ]: 75324 : if (IsA(node, Var))
1028 : : {
1029 : 14765 : Var *var = (Var *) node;
1030 : : RangeTblEntry *rte;
1031 : : Node *newvar;
1032 : :
1033 : : /* No change unless Var belongs to the GROUP of the target level */
1034 [ - + ]: 14765 : if (var->varlevelsup != context->sublevels_up)
602 rguo@postgresql.org 1035 :UBC 0 : return node; /* no need to copy, really */
602 rguo@postgresql.org 1036 :CBC 14765 : rte = rt_fetch(var->varno, context->query->rtable);
1037 [ + + ]: 14765 : if (rte->rtekind != RTE_GROUP)
1038 : 195 : return node;
1039 : :
1040 : : /* Expand group exprs reference */
1041 [ - + ]: 14570 : Assert(var->varattno > 0);
1042 : 14570 : newvar = (Node *) list_nth(rte->groupexprs, var->varattno - 1);
1043 [ - + ]: 14570 : Assert(newvar != NULL);
1044 : 14570 : newvar = copyObject(newvar);
1045 : :
1046 : : /*
1047 : : * If we are expanding an expr carried down from an upper query, must
1048 : : * adjust its varlevelsup fields.
1049 : : */
1050 [ - + ]: 14570 : if (context->sublevels_up != 0)
602 rguo@postgresql.org 1051 :UBC 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
1052 : :
1053 : : /* Preserve original Var's location, if possible */
602 rguo@postgresql.org 1054 [ + + ]:CBC 14570 : if (IsA(newvar, Var))
1055 : 12365 : ((Var *) newvar)->location = var->location;
1056 : :
1057 : : /* Detect if we are adding a sublink to query */
1058 [ + + - + ]: 14570 : if (context->possible_sublink && !context->inserted_sublink)
602 rguo@postgresql.org 1059 :UBC 0 : context->inserted_sublink = checkExprHasSubLink(newvar);
1060 : :
1061 : : /* Lastly, add any varnullingrels to the replacement expression */
602 rguo@postgresql.org 1062 :CBC 14570 : return mark_nullable_by_grouping(context->root, newvar, var);
1063 : : }
1064 : :
1065 [ + + ]: 60559 : if (IsA(node, Aggref))
1066 : : {
1067 : 6041 : Aggref *agg = (Aggref *) node;
1068 : :
1069 [ + - ]: 6041 : if ((int) agg->agglevelsup == context->sublevels_up)
1070 : : {
1071 : : /*
1072 : : * If we find an aggregate call of the original level, do not
1073 : : * recurse into its normal arguments, ORDER BY arguments, or
1074 : : * filter; there are no grouped vars there. But we should check
1075 : : * direct arguments as though they weren't in an aggregate.
1076 : : */
1077 : 6041 : agg = copyObject(agg);
1078 : 6041 : agg->aggdirectargs = (List *)
1079 : 6041 : flatten_group_exprs_mutator((Node *) agg->aggdirectargs, context);
1080 : :
1081 : 6041 : return (Node *) agg;
1082 : : }
1083 : :
1084 : : /*
1085 : : * We can skip recursing into aggregates of higher levels altogether,
1086 : : * since they could not possibly contain Vars of concern to us (see
1087 : : * transformAggregateCall). We do need to look at aggregates of lower
1088 : : * levels, however.
1089 : : */
602 rguo@postgresql.org 1090 [ # # ]:UBC 0 : if ((int) agg->agglevelsup > context->sublevels_up)
1091 : 0 : return node;
1092 : : }
1093 : :
602 rguo@postgresql.org 1094 [ + + ]:CBC 54518 : if (IsA(node, GroupingFunc))
1095 : : {
1096 : 307 : GroupingFunc *grp = (GroupingFunc *) node;
1097 : :
1098 : : /*
1099 : : * If we find a GroupingFunc node of the original or higher level, do
1100 : : * not recurse into its arguments; there are no grouped vars there.
1101 : : */
1102 [ + - ]: 307 : if ((int) grp->agglevelsup >= context->sublevels_up)
1103 : 307 : return node;
1104 : : }
1105 : :
1106 [ - + ]: 54211 : if (IsA(node, Query))
1107 : : {
1108 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1109 : : Query *newnode;
1110 : : bool save_inserted_sublink;
1111 : :
602 rguo@postgresql.org 1112 :UBC 0 : context->sublevels_up++;
1113 : 0 : save_inserted_sublink = context->inserted_sublink;
1114 : 0 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
1115 : 0 : newnode = query_tree_mutator((Query *) node,
1116 : : flatten_group_exprs_mutator,
1117 : : context,
1118 : : QTW_IGNORE_GROUPEXPRS);
1119 : 0 : newnode->hasSubLinks |= context->inserted_sublink;
1120 : 0 : context->inserted_sublink = save_inserted_sublink;
1121 : 0 : context->sublevels_up--;
1122 : 0 : return (Node *) newnode;
1123 : : }
1124 : :
602 rguo@postgresql.org 1125 :CBC 54211 : return expression_tree_mutator(node, flatten_group_exprs_mutator,
1126 : : context);
1127 : : }
1128 : :
1129 : : /*
1130 : : * Add oldvar's varnullingrels, if any, to a flattened grouping expression.
1131 : : * The newnode has been copied, so we can modify it freely.
1132 : : */
1133 : : static Node *
1134 : 14570 : mark_nullable_by_grouping(PlannerInfo *root, Node *newnode, Var *oldvar)
1135 : : {
1136 : : Relids relids;
1137 : :
1138 [ + + ]: 14570 : if (root == NULL)
1139 : 5881 : return newnode;
1140 [ + + ]: 8689 : if (oldvar->varnullingrels == NULL)
1141 : 6902 : return newnode; /* nothing to do */
1142 : :
1143 [ - + ]: 1787 : Assert(bms_equal(oldvar->varnullingrels,
1144 : : bms_make_singleton(root->group_rtindex)));
1145 : :
1146 : 1787 : relids = pull_varnos_of_level(root, newnode, oldvar->varlevelsup);
1147 : :
1148 [ + + ]: 1787 : if (!bms_is_empty(relids))
1149 : : {
1150 : : /*
1151 : : * If the newnode is not variable-free, we set the nullingrels of Vars
1152 : : * or PHVs that are contained in the expression. This is not really
1153 : : * 'correct' in theory, because it is the whole expression that can be
1154 : : * nullable by grouping sets, not its individual vars. But it works
1155 : : * in practice, because what we need is that the expression can be
1156 : : * somehow distinguished from the same expression in ECs, and marking
1157 : : * its vars is sufficient for this purpose.
1158 : : */
1159 : 1747 : newnode = add_nulling_relids(newnode,
1160 : : relids,
1161 : 1747 : oldvar->varnullingrels);
1162 : : }
1163 : : else /* variable-free? */
1164 : : {
1165 : : /*
1166 : : * If the newnode is variable-free and does not contain volatile
1167 : : * functions or set-returning functions, it can be treated as a member
1168 : : * of EC that is redundant. So wrap it in a new PlaceHolderVar to
1169 : : * carry the nullingrels. Otherwise we do not bother to make any
1170 : : * changes.
1171 : : *
1172 : : * Aggregate functions and window functions are not allowed in
1173 : : * grouping expressions.
1174 : : */
1175 [ - + ]: 40 : Assert(!contain_agg_clause(newnode));
1176 [ - + ]: 40 : Assert(!contain_window_function(newnode));
1177 : :
1178 [ + - ]: 40 : if (!contain_volatile_functions(newnode) &&
1179 [ + + ]: 40 : !expression_returns_set(newnode))
1180 : : {
1181 : : PlaceHolderVar *newphv;
1182 : : Relids phrels;
1183 : :
1184 : 25 : phrels = get_relids_in_jointree((Node *) root->parse->jointree,
1185 : : true, false);
1186 [ - + ]: 25 : Assert(!bms_is_empty(phrels));
1187 : :
1188 : 25 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1189 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1190 : 25 : newphv->phlevelsup = oldvar->varlevelsup;
1191 : 25 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1192 : 25 : newnode = (Node *) newphv;
1193 : : }
1194 : : }
1195 : :
1196 : 1787 : return newnode;
1197 : : }
1198 : :
1199 : : /*
1200 : : * Add oldvar's varnullingrels, if any, to a flattened join alias expression.
1201 : : * The newnode has been copied, so we can modify it freely.
1202 : : */
1203 : : static Node *
1191 tgl@sss.pgh.pa.us 1204 : 544 : add_nullingrels_if_needed(PlannerInfo *root, Node *newnode, Var *oldvar)
1205 : : {
1206 [ + + ]: 544 : if (oldvar->varnullingrels == NULL)
1207 : 418 : return newnode; /* nothing to do */
1208 : : /* If possible, do it by adding to existing nullingrel fields */
1209 [ + + ]: 126 : if (is_standard_join_alias_expression(newnode, oldvar))
1210 : 116 : adjust_standard_join_alias_expression(newnode, oldvar);
1211 [ + - ]: 10 : else if (root)
1212 : : {
1213 : : /*
1214 : : * We can insert a PlaceHolderVar to carry the nullingrels. However,
1215 : : * deciding where to evaluate the PHV is slightly tricky. We first
1216 : : * try to evaluate it at the natural semantic level of the new
1217 : : * expression; but if that expression is variable-free, fall back to
1218 : : * evaluating it at the join that the oldvar is an alias Var for.
1219 : : */
1220 : : PlaceHolderVar *newphv;
1221 : 10 : Index levelsup = oldvar->varlevelsup;
1222 : 10 : Relids phrels = pull_varnos_of_level(root, newnode, levelsup);
1223 : :
1224 [ + - ]: 10 : if (bms_is_empty(phrels)) /* variable-free? */
1225 : : {
1226 [ - + ]: 10 : if (levelsup != 0) /* this won't work otherwise */
1191 tgl@sss.pgh.pa.us 1227 [ # # ]:UBC 0 : elog(ERROR, "unsupported join alias expression");
1191 tgl@sss.pgh.pa.us 1228 :CBC 10 : phrels = get_relids_for_join(root->parse, oldvar->varno);
1229 : : /* If it's an outer join, eval below not above the join */
1230 : 10 : phrels = bms_del_member(phrels, oldvar->varno);
1231 [ - + ]: 10 : Assert(!bms_is_empty(phrels));
1232 : : }
1233 : 10 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1234 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1235 : 10 : newphv->phlevelsup = levelsup;
1236 : 10 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1237 : 10 : newnode = (Node *) newphv;
1238 : : }
1239 : : else
1240 : : {
1241 : : /* ooops, we're missing support for something the parser can make */
1191 tgl@sss.pgh.pa.us 1242 [ # # ]:UBC 0 : elog(ERROR, "unsupported join alias expression");
1243 : : }
1191 tgl@sss.pgh.pa.us 1244 :CBC 126 : return newnode;
1245 : : }
1246 : :
1247 : : /*
1248 : : * Check to see if we can insert nullingrels into this join alias expression
1249 : : * without use of a separate PlaceHolderVar.
1250 : : *
1251 : : * This will handle Vars, PlaceHolderVars, and implicit-coercion and COALESCE
1252 : : * expressions built from those. This coverage needs to handle anything
1253 : : * that the parser would put into joinaliasvars.
1254 : : */
1255 : : static bool
1256 : 382 : is_standard_join_alias_expression(Node *newnode, Var *oldvar)
1257 : : {
1258 [ - + ]: 382 : if (newnode == NULL)
1191 tgl@sss.pgh.pa.us 1259 :UBC 0 : return false;
1191 tgl@sss.pgh.pa.us 1260 [ + + ]:CBC 382 : if (IsA(newnode, Var) &&
1261 [ + - ]: 226 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1262 : 226 : return true;
1263 [ - + ]: 156 : else if (IsA(newnode, PlaceHolderVar) &&
1191 tgl@sss.pgh.pa.us 1264 [ # # ]:UBC 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1265 : 0 : return true;
1191 tgl@sss.pgh.pa.us 1266 [ + + ]:CBC 156 : else if (IsA(newnode, FuncExpr))
1267 : : {
1268 : 23 : FuncExpr *fexpr = (FuncExpr *) newnode;
1269 : :
1270 : : /*
1271 : : * We need to assume that the function wouldn't produce non-NULL from
1272 : : * NULL, which is reasonable for implicit coercions but otherwise not
1273 : : * so much. (Looking at its strictness is likely overkill, and anyway
1274 : : * it would cause us to fail if someone forgot to mark an implicit
1275 : : * coercion as strict.)
1276 : : */
1277 [ + - ]: 23 : if (fexpr->funcformat != COERCE_IMPLICIT_CAST ||
1278 [ - + ]: 23 : fexpr->args == NIL)
1191 tgl@sss.pgh.pa.us 1279 :UBC 0 : return false;
1280 : :
1281 : : /*
1282 : : * Examine only the first argument --- coercions might have additional
1283 : : * arguments that are constants.
1284 : : */
1191 tgl@sss.pgh.pa.us 1285 :CBC 23 : return is_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1286 : : }
1287 [ + + ]: 133 : else if (IsA(newnode, RelabelType))
1288 : : {
1289 : 13 : RelabelType *relabel = (RelabelType *) newnode;
1290 : :
1291 : : /* This definitely won't produce non-NULL from NULL */
1292 : 13 : return is_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1293 : : }
1294 [ - + ]: 120 : else if (IsA(newnode, CoerceViaIO))
1295 : : {
1191 tgl@sss.pgh.pa.us 1296 :UBC 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1297 : :
1298 : : /* This definitely won't produce non-NULL from NULL */
1299 : 0 : return is_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1300 : : }
1191 tgl@sss.pgh.pa.us 1301 [ - + ]:CBC 120 : else if (IsA(newnode, ArrayCoerceExpr))
1302 : : {
1191 tgl@sss.pgh.pa.us 1303 :UBC 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1304 : :
1305 : : /* This definitely won't produce non-NULL from NULL (at array level) */
1306 : 0 : return is_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1307 : : }
1191 tgl@sss.pgh.pa.us 1308 [ + + ]:CBC 120 : else if (IsA(newnode, CoalesceExpr))
1309 : : {
1310 : 110 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1311 : : ListCell *lc;
1312 : :
1313 [ - + ]: 110 : Assert(cexpr->args != NIL);
1314 [ + - + + : 330 : foreach(lc, cexpr->args)
+ + ]
1315 : : {
1316 [ - + ]: 220 : if (!is_standard_join_alias_expression(lfirst(lc), oldvar))
1191 tgl@sss.pgh.pa.us 1317 :UBC 0 : return false;
1318 : : }
1191 tgl@sss.pgh.pa.us 1319 :CBC 110 : return true;
1320 : : }
1321 : : else
1322 : 10 : return false;
1323 : : }
1324 : :
1325 : : /*
1326 : : * Insert nullingrels into an expression accepted by
1327 : : * is_standard_join_alias_expression.
1328 : : */
1329 : : static void
1330 : 362 : adjust_standard_join_alias_expression(Node *newnode, Var *oldvar)
1331 : : {
1332 [ + + ]: 362 : if (IsA(newnode, Var) &&
1333 [ + - ]: 226 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1334 : 226 : {
1335 : 226 : Var *newvar = (Var *) newnode;
1336 : :
1337 : 226 : newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
1338 : 226 : oldvar->varnullingrels);
1339 : : }
1340 [ - + ]: 136 : else if (IsA(newnode, PlaceHolderVar) &&
1191 tgl@sss.pgh.pa.us 1341 [ # # ]:UBC 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1342 : 0 : {
1343 : 0 : PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
1344 : :
1345 : 0 : newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
1346 : 0 : oldvar->varnullingrels);
1347 : : }
1191 tgl@sss.pgh.pa.us 1348 [ + + ]:CBC 136 : else if (IsA(newnode, FuncExpr))
1349 : : {
1350 : 13 : FuncExpr *fexpr = (FuncExpr *) newnode;
1351 : :
1352 : 13 : adjust_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1353 : : }
1354 [ + + ]: 123 : else if (IsA(newnode, RelabelType))
1355 : : {
1356 : 13 : RelabelType *relabel = (RelabelType *) newnode;
1357 : :
1358 : 13 : adjust_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1359 : : }
1360 [ - + ]: 110 : else if (IsA(newnode, CoerceViaIO))
1361 : : {
1191 tgl@sss.pgh.pa.us 1362 :UBC 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1363 : :
1364 : 0 : adjust_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1365 : : }
1191 tgl@sss.pgh.pa.us 1366 [ - + ]:CBC 110 : else if (IsA(newnode, ArrayCoerceExpr))
1367 : : {
1191 tgl@sss.pgh.pa.us 1368 :UBC 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1369 : :
1370 : 0 : adjust_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1371 : : }
1191 tgl@sss.pgh.pa.us 1372 [ + - ]:CBC 110 : else if (IsA(newnode, CoalesceExpr))
1373 : : {
1374 : 110 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1375 : : ListCell *lc;
1376 : :
1377 [ - + ]: 110 : Assert(cexpr->args != NIL);
1378 [ + - + + : 330 : foreach(lc, cexpr->args)
+ + ]
1379 : : {
1380 : 220 : adjust_standard_join_alias_expression(lfirst(lc), oldvar);
1381 : : }
1382 : : }
1383 : : else
1191 tgl@sss.pgh.pa.us 1384 :UBC 0 : Assert(false);
1191 tgl@sss.pgh.pa.us 1385 :CBC 362 : }
1386 : :
1387 : : /*
1388 : : * alias_relid_set: in a set of RT indexes, replace joins by their
1389 : : * underlying base+OJ relids
1390 : : */
1391 : : static Relids
2653 1392 : 1770 : alias_relid_set(Query *query, Relids relids)
1393 : : {
8487 1394 : 1770 : Relids result = NULL;
1395 : : int rtindex;
1396 : :
4176 1397 : 1770 : rtindex = -1;
1398 [ + + ]: 4400 : while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1399 : : {
2653 1400 : 2630 : RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
1401 : :
8506 1402 [ + + ]: 2630 : if (rte->rtekind == RTE_JOIN)
2653 1403 : 307 : result = bms_join(result, get_relids_for_join(query, rtindex));
1404 : : else
8487 1405 : 2323 : result = bms_add_member(result, rtindex);
1406 : : }
8506 1407 : 1770 : return result;
1408 : : }
|