Branch data 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
114 : 319492 : pull_varnos(PlannerInfo *root, Node *node)
115 : : {
116 : 319492 : pull_varnos_context context;
117 : :
118 : 319492 : context.varnos = NULL;
119 : 319492 : context.root = root;
120 : 319492 : 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 : : */
126 : 319492 : query_or_expression_tree_walker(node,
127 : : pull_varnos_walker,
128 : : &context,
129 : : 0);
130 : :
131 : 638984 : return context.varnos;
132 : 319492 : }
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
140 : 1364 : pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
141 : : {
142 : 1364 : pull_varnos_context context;
143 : :
144 : 1364 : context.varnos = NULL;
145 : 1364 : context.root = root;
146 : 1364 : 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 : 1364 : query_or_expression_tree_walker(node,
153 : : pull_varnos_walker,
154 : : &context,
155 : : 0);
156 : :
157 : 2728 : return context.varnos;
158 : 1364 : }
159 : :
160 : : static bool
161 : 587537 : pull_varnos_walker(Node *node, pull_varnos_context *context)
162 : : {
163 [ + + ]: 587537 : if (node == NULL)
164 : 31792 : return false;
165 [ + + ]: 555745 : if (IsA(node, Var))
166 : : {
167 : 263800 : Var *var = (Var *) node;
168 : :
169 [ + + ]: 263800 : if (var->varlevelsup == context->sublevels_up)
170 : : {
171 : 261967 : context->varnos = bms_add_member(context->varnos, var->varno);
172 : 523934 : context->varnos = bms_add_members(context->varnos,
173 : 261967 : var->varnullingrels);
174 : 261967 : }
175 : 263800 : return false;
176 : 263800 : }
177 [ + + ]: 291945 : if (IsA(node, CurrentOfExpr))
178 : : {
179 : 130 : CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
180 : :
181 [ - + ]: 130 : if (context->sublevels_up == 0)
182 : 130 : context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
183 : 130 : return false;
184 : 130 : }
185 [ + + ]: 291815 : if (IsA(node, PlaceHolderVar))
186 : : {
187 : 844 : 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 : : */
197 [ + - - + ]: 844 : if (phv->phlevelsup == context->sublevels_up &&
198 : 844 : 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 : : */
217 : 844 : PlaceHolderInfo *phinfo = NULL;
218 : :
219 [ + + ]: 844 : if (phv->phlevelsup == 0)
220 : : {
221 [ + + ]: 828 : if (phv->phid < context->root->placeholder_array_size)
222 : 606 : phinfo = context->root->placeholder_array[phv->phid];
223 : 828 : }
224 [ + + ]: 844 : if (phinfo == NULL)
225 : : {
226 : : /* No PlaceHolderInfo yet, use phrels */
227 : 480 : context->varnos = bms_add_members(context->varnos,
228 : 240 : phv->phrels);
229 : 240 : }
230 [ + + ]: 604 : else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
231 : : {
232 : : /* Normal case: use ph_eval_at */
233 : 1134 : context->varnos = bms_add_members(context->varnos,
234 : 567 : phinfo->ph_eval_at);
235 : 567 : }
236 : : else
237 : : {
238 : : /* Translated PlaceHolderVar: translate ph_eval_at to match */
239 : 37 : Relids newevalat,
240 : : delta;
241 : :
242 : : /* remove what was removed from phv->phrels ... */
243 : 37 : delta = bms_difference(phinfo->ph_var->phrels, phv->phrels);
244 : 37 : newevalat = bms_difference(phinfo->ph_eval_at, delta);
245 : : /* ... then if that was in fact part of ph_eval_at ... */
246 [ + + ]: 37 : if (!bms_equal(newevalat, phinfo->ph_eval_at))
247 : : {
248 : : /* ... add what was added */
249 : 28 : delta = bms_difference(phv->phrels, phinfo->ph_var->phrels);
250 : 28 : newevalat = bms_join(newevalat, delta);
251 : 28 : }
252 : 74 : context->varnos = bms_join(context->varnos,
253 : 37 : newevalat);
254 : 37 : }
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 : : */
261 : 1688 : context->varnos = bms_add_members(context->varnos,
262 : 844 : phv->phnullingrels);
263 : 844 : return false; /* don't recurse into expression */
264 : 844 : }
265 [ - + - ]: 844 : }
266 [ + + ]: 290971 : else if (IsA(node, Query))
267 : : {
268 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
269 : 1060 : bool result;
270 : :
271 : 1060 : context->sublevels_up++;
272 : 1060 : result = query_tree_walker((Query *) node, pull_varnos_walker,
273 : : context, 0);
274 : 1060 : context->sublevels_up--;
275 : 1060 : return result;
276 : 1060 : }
277 : 289911 : return expression_tree_walker(node, pull_varnos_walker, context);
278 : 587537 : }
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
296 : 175287 : pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
297 : : {
298 : 175287 : pull_varattnos_context context;
299 : :
300 : 175287 : context.varattnos = *varattnos;
301 : 175287 : context.varno = varno;
302 : :
303 : 175287 : (void) pull_varattnos_walker(node, &context);
304 : :
305 : 175287 : *varattnos = context.varattnos;
306 : 175287 : }
307 : :
308 : : static bool
309 : 604469 : pull_varattnos_walker(Node *node, pull_varattnos_context *context)
310 : : {
311 [ + + ]: 604469 : if (node == NULL)
312 : 7401 : return false;
313 [ + + ]: 597068 : if (IsA(node, Var))
314 : : {
315 : 343043 : Var *var = (Var *) node;
316 : :
317 [ + + - + ]: 343043 : if (var->varno == context->varno && var->varlevelsup == 0)
318 : 342978 : context->varattnos =
319 : 685956 : bms_add_member(context->varattnos,
320 : 342978 : var->varattno - FirstLowInvalidHeapAttributeNumber);
321 : 343043 : return false;
322 : 343043 : }
323 : :
324 : : /* Should not find an unplanned subquery */
325 [ + - ]: 254025 : Assert(!IsA(node, Query));
326 : :
327 : 254025 : return expression_tree_walker(node, pull_varattnos_walker, context);
328 : 604469 : }
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 *
339 : 282 : pull_vars_of_level(Node *node, int levelsup)
340 : : {
341 : 282 : pull_vars_context context;
342 : :
343 : 282 : context.vars = NIL;
344 : 282 : 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 : 282 : query_or_expression_tree_walker(node,
351 : : pull_vars_walker,
352 : : &context,
353 : : 0);
354 : :
355 : 564 : return context.vars;
356 : 282 : }
357 : :
358 : : static bool
359 : 6557 : pull_vars_walker(Node *node, pull_vars_context *context)
360 : : {
361 [ + + ]: 6557 : if (node == NULL)
362 : 2470 : return false;
363 [ + + ]: 4087 : if (IsA(node, Var))
364 : : {
365 : 948 : Var *var = (Var *) node;
366 : :
367 [ + + ]: 948 : if (var->varlevelsup == context->sublevels_up)
368 : 389 : context->vars = lappend(context->vars, var);
369 : 948 : return false;
370 : 948 : }
371 [ + + ]: 3139 : if (IsA(node, PlaceHolderVar))
372 : : {
373 : 23 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
374 : :
375 [ - + ]: 23 : if (phv->phlevelsup == context->sublevels_up)
376 : 23 : context->vars = lappend(context->vars, phv);
377 : : /* we don't want to look into the contained expression */
378 : 23 : return false;
379 : 23 : }
380 [ + + ]: 3116 : if (IsA(node, Query))
381 : : {
382 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
383 : 58 : bool result;
384 : :
385 : 58 : context->sublevels_up++;
386 : 58 : result = query_tree_walker((Query *) node, pull_vars_walker,
387 : : context, 0);
388 : 58 : context->sublevels_up--;
389 : 58 : return result;
390 : 58 : }
391 : 3058 : return expression_tree_walker(node, pull_vars_walker, context);
392 : 6557 : }
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
406 : 5591 : contain_var_clause(Node *node)
407 : : {
408 : 5591 : return contain_var_clause_walker(node, NULL);
409 : : }
410 : :
411 : : static bool
412 : 6645 : contain_var_clause_walker(Node *node, void *context)
413 : : {
414 [ + + ]: 6645 : if (node == NULL)
415 : 18 : return false;
416 [ + + ]: 6627 : if (IsA(node, Var))
417 : : {
418 [ - + ]: 446 : if (((Var *) node)->varlevelsup == 0)
419 : 446 : return true; /* abort the tree traversal and return true */
420 : 0 : return false;
421 : : }
422 [ - + ]: 6181 : if (IsA(node, CurrentOfExpr))
423 : 0 : return true;
424 [ + + ]: 6181 : if (IsA(node, PlaceHolderVar))
425 : : {
426 [ - + ]: 2 : if (((PlaceHolderVar *) node)->phlevelsup == 0)
427 : 2 : return true; /* abort the tree traversal and return true */
428 : : /* else fall through to check the contained expr */
429 : 0 : }
430 : 6179 : return expression_tree_walker(node, contain_var_clause_walker, context);
431 : 6645 : }
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
444 : 14966 : contain_vars_of_level(Node *node, int levelsup)
445 : : {
446 : 14966 : int sublevels_up = levelsup;
447 : :
448 : 29932 : return query_or_expression_tree_walker(node,
449 : : contain_vars_of_level_walker,
450 : : &sublevels_up,
451 : : 0);
452 : 14966 : }
453 : :
454 : : static bool
455 : 48397 : contain_vars_of_level_walker(Node *node, int *sublevels_up)
456 : : {
457 [ + + ]: 48397 : if (node == NULL)
458 : 9094 : return false;
459 [ + + ]: 39303 : if (IsA(node, Var))
460 : : {
461 [ + + ]: 8144 : if (((Var *) node)->varlevelsup == *sublevels_up)
462 : 5208 : return true; /* abort tree traversal and return true */
463 : 2936 : return false;
464 : : }
465 [ + + ]: 31159 : if (IsA(node, CurrentOfExpr))
466 : : {
467 [ - + ]: 30 : if (*sublevels_up == 0)
468 : 30 : return true;
469 : 0 : return false;
470 : : }
471 [ + + ]: 31129 : if (IsA(node, PlaceHolderVar))
472 : : {
473 [ + + ]: 14 : if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
474 : 13 : return true; /* abort the tree traversal and return true */
475 : : /* else fall through to check the contained expr */
476 : 1 : }
477 [ + + ]: 31116 : if (IsA(node, Query))
478 : : {
479 : : /* Recurse into subselects */
480 : 44 : bool result;
481 : :
482 : 44 : (*sublevels_up)++;
483 : 44 : result = query_tree_walker((Query *) node,
484 : : contain_vars_of_level_walker,
485 : : sublevels_up,
486 : : 0);
487 : 44 : (*sublevels_up)--;
488 : 44 : return result;
489 : 44 : }
490 : 31072 : return expression_tree_walker(node,
491 : : contain_vars_of_level_walker,
492 : : sublevels_up);
493 : 48397 : }
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
511 : 0 : contain_vars_returning_old_or_new(Node *node)
512 : : {
513 : 0 : return contain_vars_returning_old_or_new_walker(node, NULL);
514 : : }
515 : :
516 : : static bool
517 : 0 : contain_vars_returning_old_or_new_walker(Node *node, void *context)
518 : : {
519 [ # # ]: 0 : if (node == NULL)
520 : 0 : return false;
521 [ # # ]: 0 : if (IsA(node, Var))
522 : : {
523 [ # # # # ]: 0 : if (((Var *) node)->varlevelsup == 0 &&
524 : 0 : ((Var *) node)->varreturningtype != VAR_RETURNING_DEFAULT)
525 : 0 : return true; /* abort the tree traversal and return true */
526 : 0 : return false;
527 : : }
528 [ # # ]: 0 : if (IsA(node, ReturningExpr))
529 : : {
530 [ # # ]: 0 : if (((ReturningExpr *) node)->retlevelsup == 0)
531 : 0 : return true; /* abort the tree traversal and return true */
532 : 0 : return false;
533 : : }
534 : 0 : return expression_tree_walker(node, contain_vars_returning_old_or_new_walker,
535 : : context);
536 : 0 : }
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
555 : 2 : locate_var_of_level(Node *node, int levelsup)
556 : : {
557 : 2 : locate_var_of_level_context context;
558 : :
559 : 2 : context.var_location = -1; /* in case we find nothing */
560 : 2 : context.sublevels_up = levelsup;
561 : :
562 : 2 : (void) query_or_expression_tree_walker(node,
563 : : locate_var_of_level_walker,
564 : : &context,
565 : : 0);
566 : :
567 : 4 : return context.var_location;
568 : 2 : }
569 : :
570 : : static bool
571 : 5 : locate_var_of_level_walker(Node *node,
572 : : locate_var_of_level_context *context)
573 : : {
574 [ + - ]: 5 : if (node == NULL)
575 : 0 : return false;
576 [ + + ]: 5 : if (IsA(node, Var))
577 : : {
578 : 2 : Var *var = (Var *) node;
579 : :
580 [ + - - + ]: 2 : if (var->varlevelsup == context->sublevels_up &&
581 : 2 : var->location >= 0)
582 : : {
583 : 2 : context->var_location = var->location;
584 : 2 : return true; /* abort tree traversal and return true */
585 : : }
586 : 0 : return false;
587 : 2 : }
588 [ - + ]: 3 : if (IsA(node, CurrentOfExpr))
589 : : {
590 : : /* since CurrentOfExpr doesn't carry location, nothing we can do */
591 : 0 : return false;
592 : : }
593 : : /* No extra code needed for PlaceHolderVar; just look in contained expr */
594 [ - + ]: 3 : if (IsA(node, Query))
595 : : {
596 : : /* Recurse into subselects */
597 : 0 : bool result;
598 : :
599 : 0 : context->sublevels_up++;
600 : 0 : result = query_tree_walker((Query *) node,
601 : : locate_var_of_level_walker,
602 : : context,
603 : : 0);
604 : 0 : context->sublevels_up--;
605 : 0 : return result;
606 : 0 : }
607 : 3 : return expression_tree_walker(node,
608 : : locate_var_of_level_walker,
609 : : context);
610 : 5 : }
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 *
653 : 85342 : pull_var_clause(Node *node, int flags)
654 : : {
655 : 85342 : pull_var_clause_context context;
656 : :
657 : : /* Assert that caller has not specified inconsistent flags */
658 [ + - ]: 85342 : Assert((flags & (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES))
659 : : != (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES));
660 [ + - ]: 85342 : Assert((flags & (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS))
661 : : != (PVC_INCLUDE_WINDOWFUNCS | PVC_RECURSE_WINDOWFUNCS));
662 [ + - ]: 85342 : Assert((flags & (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS))
663 : : != (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS));
664 : :
665 : 85342 : context.varlist = NIL;
666 : 85342 : context.flags = flags;
667 : :
668 : 85342 : pull_var_clause_walker(node, &context);
669 : 170684 : return context.varlist;
670 : 85342 : }
671 : :
672 : : static bool
673 : 502113 : pull_var_clause_walker(Node *node, pull_var_clause_context *context)
674 : : {
675 [ + + ]: 502113 : if (node == NULL)
676 : 24423 : return false;
677 [ + + ]: 477690 : if (IsA(node, Var))
678 : : {
679 [ + - ]: 177012 : if (((Var *) node)->varlevelsup != 0)
680 [ # # # # ]: 0 : elog(ERROR, "Upper-level Var found where not expected");
681 : 177012 : context->varlist = lappend(context->varlist, node);
682 : 177012 : return false;
683 : : }
684 [ + + ]: 300678 : else if (IsA(node, Aggref))
685 : : {
686 [ + - ]: 13132 : if (((Aggref *) node)->agglevelsup != 0)
687 [ # # # # ]: 0 : elog(ERROR, "Upper-level Aggref found where not expected");
688 [ + + ]: 13132 : if (context->flags & PVC_INCLUDE_AGGREGATES)
689 : : {
690 : 1208 : context->varlist = lappend(context->varlist, node);
691 : : /* we do NOT descend into the contained expression */
692 : 1208 : return false;
693 : : }
694 [ + - ]: 11924 : else if (context->flags & PVC_RECURSE_AGGREGATES)
695 : : {
696 : : /* fall through to recurse into the aggregate's arguments */
697 : 11924 : }
698 : : else
699 [ # # # # ]: 0 : elog(ERROR, "Aggref found where not expected");
700 : 11924 : }
701 [ + + ]: 287546 : else if (IsA(node, GroupingFunc))
702 : : {
703 [ + - ]: 105 : if (((GroupingFunc *) node)->agglevelsup != 0)
704 [ # # # # ]: 0 : elog(ERROR, "Upper-level GROUPING found where not expected");
705 [ - + ]: 105 : if (context->flags & PVC_INCLUDE_AGGREGATES)
706 : : {
707 : 0 : context->varlist = lappend(context->varlist, node);
708 : : /* we do NOT descend into the contained expression */
709 : 0 : return false;
710 : : }
711 [ + - ]: 105 : else if (context->flags & PVC_RECURSE_AGGREGATES)
712 : : {
713 : : /* fall through to recurse into the GroupingFunc's arguments */
714 : 105 : }
715 : : else
716 [ # # # # ]: 0 : elog(ERROR, "GROUPING found where not expected");
717 : 105 : }
718 [ + + ]: 287441 : else if (IsA(node, WindowFunc))
719 : : {
720 : : /* WindowFuncs have no levelsup field to check ... */
721 [ + + ]: 1221 : if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
722 : : {
723 : 2 : context->varlist = lappend(context->varlist, node);
724 : : /* we do NOT descend into the contained expressions */
725 : 2 : return false;
726 : : }
727 [ + - ]: 1219 : else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
728 : : {
729 : : /* fall through to recurse into the windowfunc's arguments */
730 : 1219 : }
731 : : else
732 [ # # # # ]: 0 : elog(ERROR, "WindowFunc found where not expected");
733 : 1219 : }
734 [ + + ]: 286220 : else if (IsA(node, PlaceHolderVar))
735 : : {
736 [ + - ]: 1032 : if (((PlaceHolderVar *) node)->phlevelsup != 0)
737 [ # # # # ]: 0 : elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
738 [ + + ]: 1032 : if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
739 : : {
740 : 768 : context->varlist = lappend(context->varlist, node);
741 : : /* we do NOT descend into the contained expression */
742 : 768 : return false;
743 : : }
744 [ + - ]: 264 : else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
745 : : {
746 : : /* fall through to recurse into the placeholder's expression */
747 : 264 : }
748 : : else
749 [ # # # # ]: 0 : elog(ERROR, "PlaceHolderVar found where not expected");
750 : 264 : }
751 : 298700 : return expression_tree_walker(node, pull_var_clause_walker, context);
752 : 502113 : }
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 : : * NOTE: this is also used by the parser, to expand join alias Vars before
781 : : * checking GROUP BY validity. For that use-case, root will be NULL, which
782 : : * is why we have to pass the Query separately. We need the root itself only
783 : : * for making PlaceHolderVars. We can avoid making PlaceHolderVars in the
784 : : * parser's usage because it won't be dealing with arbitrary expressions:
785 : : * so long as adjust_standard_join_alias_expression can handle everything
786 : : * the parser would make as a join alias expression, we're OK.
787 : : */
788 : : Node *
789 : 24876 : flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
790 : : {
791 : 24876 : flatten_join_alias_vars_context context;
792 : :
793 : : /*
794 : : * We do not expect this to be applied to the whole Query, only to
795 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
796 : : * it's okay to immediately increment sublevels_up.
797 : : */
798 [ + - ]: 24876 : Assert(node != (Node *) query);
799 : :
800 : 24876 : context.root = root;
801 : 24876 : context.query = query;
802 : 24876 : context.sublevels_up = 0;
803 : : /* flag whether join aliases could possibly contain SubLinks */
804 : 24876 : context.possible_sublink = query->hasSubLinks;
805 : : /* if hasSubLinks is already true, no need to work hard */
806 : 24876 : context.inserted_sublink = query->hasSubLinks;
807 : :
808 : 49752 : return flatten_join_alias_vars_mutator(node, &context);
809 : 24876 : }
810 : :
811 : : static Node *
812 : 319342 : flatten_join_alias_vars_mutator(Node *node,
813 : : flatten_join_alias_vars_context *context)
814 : : {
815 [ + + ]: 319342 : if (node == NULL)
816 : 28670 : return NULL;
817 [ + + ]: 290672 : if (IsA(node, Var))
818 : : {
819 : 83582 : Var *var = (Var *) node;
820 : 83582 : RangeTblEntry *rte;
821 : 83582 : Node *newvar;
822 : :
823 : : /* No change unless Var belongs to a JOIN of the target level */
824 [ + + ]: 83582 : if (var->varlevelsup != context->sublevels_up)
825 : 4864 : return node; /* no need to copy, really */
826 : 78718 : rte = rt_fetch(var->varno, context->query->rtable);
827 [ + + ]: 78718 : if (rte->rtekind != RTE_JOIN)
828 : 78637 : return node;
829 [ + + ]: 81 : if (var->varattno == InvalidAttrNumber)
830 : : {
831 : : /* Must expand whole-row reference */
832 : 1 : RowExpr *rowexpr;
833 : 1 : List *fields = NIL;
834 : 1 : List *colnames = NIL;
835 : 1 : ListCell *lv;
836 : 1 : ListCell *ln;
837 : :
838 [ + - ]: 1 : Assert(list_length(rte->joinaliasvars) == list_length(rte->eref->colnames));
839 [ + - + + : 8 : forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
+ - + + +
+ + + ]
840 : : {
841 : 7 : newvar = (Node *) lfirst(lv);
842 : : /* Ignore dropped columns */
843 [ + - ]: 7 : if (newvar == NULL)
844 : 0 : continue;
845 : 7 : newvar = copyObject(newvar);
846 : :
847 : : /*
848 : : * If we are expanding an alias carried down from an upper
849 : : * query, must adjust its varlevelsup fields.
850 : : */
851 [ + - ]: 7 : if (context->sublevels_up != 0)
852 : 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
853 : : /* Preserve original Var's location, if possible */
854 [ - + ]: 7 : if (IsA(newvar, Var))
855 : 7 : ((Var *) newvar)->location = var->location;
856 : : /* Recurse in case join input is itself a join */
857 : : /* (also takes care of setting inserted_sublink if needed) */
858 : 7 : newvar = flatten_join_alias_vars_mutator(newvar, context);
859 : 7 : fields = lappend(fields, newvar);
860 : : /* We need the names of non-dropped columns, too */
861 : 7 : colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
862 : 7 : }
863 : 1 : rowexpr = makeNode(RowExpr);
864 : 1 : rowexpr->args = fields;
865 : 1 : rowexpr->row_typeid = var->vartype;
866 : 1 : rowexpr->row_format = COERCE_IMPLICIT_CAST;
867 : : /* vartype will always be RECORDOID, so we always need colnames */
868 : 1 : rowexpr->colnames = colnames;
869 : 1 : rowexpr->location = var->location;
870 : :
871 : : /* Lastly, add any varnullingrels to the replacement expression */
872 : 2 : return add_nullingrels_if_needed(context->root, (Node *) rowexpr,
873 : 1 : var);
874 : 1 : }
875 : :
876 : : /* Expand join alias reference */
877 [ + - ]: 80 : Assert(var->varattno > 0);
878 : 80 : newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
879 [ + - ]: 80 : Assert(newvar != NULL);
880 : 80 : newvar = copyObject(newvar);
881 : :
882 : : /*
883 : : * If we are expanding an alias carried down from an upper query, must
884 : : * adjust its varlevelsup fields.
885 : : */
886 [ + - ]: 80 : if (context->sublevels_up != 0)
887 : 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
888 : :
889 : : /* Preserve original Var's location, if possible */
890 [ + - ]: 80 : if (IsA(newvar, Var))
891 : 0 : ((Var *) newvar)->location = var->location;
892 : :
893 : : /* Recurse in case join input is itself a join */
894 : 80 : newvar = flatten_join_alias_vars_mutator(newvar, context);
895 : :
896 : : /* Detect if we are adding a sublink to query */
897 [ - + # # ]: 80 : if (context->possible_sublink && !context->inserted_sublink)
898 : 0 : context->inserted_sublink = checkExprHasSubLink(newvar);
899 : :
900 : : /* Lastly, add any varnullingrels to the replacement expression */
901 : 80 : return add_nullingrels_if_needed(context->root, newvar, var);
902 : 83582 : }
903 [ + + ]: 207090 : if (IsA(node, PlaceHolderVar))
904 : : {
905 : : /* Copy the PlaceHolderVar node with correct mutation of subnodes */
906 : 317 : PlaceHolderVar *phv;
907 : :
908 : 317 : phv = (PlaceHolderVar *) expression_tree_mutator(node,
909 : : flatten_join_alias_vars_mutator,
910 : : context);
911 : : /* now fix PlaceHolderVar's relid sets */
912 [ + + ]: 317 : if (phv->phlevelsup == context->sublevels_up)
913 : : {
914 : 602 : phv->phrels = alias_relid_set(context->query,
915 : 301 : phv->phrels);
916 : : /* we *don't* change phnullingrels */
917 : 301 : }
918 : 317 : return (Node *) phv;
919 : 317 : }
920 : :
921 [ + + ]: 206773 : if (IsA(node, Query))
922 : : {
923 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
924 : 1376 : Query *newnode;
925 : 1376 : bool save_inserted_sublink;
926 : :
927 : 1376 : context->sublevels_up++;
928 : 1376 : save_inserted_sublink = context->inserted_sublink;
929 : 1376 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
930 : 1376 : newnode = query_tree_mutator((Query *) node,
931 : : flatten_join_alias_vars_mutator,
932 : : context,
933 : : QTW_IGNORE_JOINALIASES);
934 : 1376 : newnode->hasSubLinks |= context->inserted_sublink;
935 : 1376 : context->inserted_sublink = save_inserted_sublink;
936 : 1376 : context->sublevels_up--;
937 : 1376 : return (Node *) newnode;
938 : 1376 : }
939 : : /* Already-planned tree not supported */
940 [ + - ]: 205397 : Assert(!IsA(node, SubPlan));
941 [ + - ]: 205397 : Assert(!IsA(node, AlternativeSubPlan));
942 : : /* Shouldn't need to handle these planner auxiliary nodes here */
943 [ + - ]: 205397 : Assert(!IsA(node, SpecialJoinInfo));
944 [ + - ]: 205397 : Assert(!IsA(node, PlaceHolderInfo));
945 [ + - ]: 205397 : Assert(!IsA(node, MinMaxAggInfo));
946 : :
947 : 205397 : return expression_tree_mutator(node, flatten_join_alias_vars_mutator, context);
948 : 319342 : }
949 : :
950 : : /*
951 : : * flatten_group_exprs
952 : : * Replace Vars that reference GROUP outputs with the underlying grouping
953 : : * expressions.
954 : : *
955 : : * We have to preserve any varnullingrels info attached to the group Vars we're
956 : : * replacing. If the replacement expression is a Var or PlaceHolderVar or
957 : : * constructed from those, we can just add the varnullingrels bits to the
958 : : * existing nullingrels field(s); otherwise we have to add a PlaceHolderVar
959 : : * wrapper.
960 : : *
961 : : * NOTE: this is also used by ruleutils.c, to deparse one query parsetree back
962 : : * to source text, and by check_output_expressions() to check for unsafe
963 : : * pushdowns. For these use-cases, root will be NULL, which is why we have to
964 : : * pass the Query separately. We need the root itself only for preserving
965 : : * varnullingrels. We can avoid preserving varnullingrels in the ruleutils.c's
966 : : * usage because it does not make any difference to the deparsed source text.
967 : : * We can also avoid it in check_output_expressions() because that function
968 : : * uses the expanded expressions solely to check for volatile or set-returning
969 : : * functions, which is independent of the Vars' nullingrels.
970 : : */
971 : : Node *
972 : 1542 : flatten_group_exprs(PlannerInfo *root, Query *query, Node *node)
973 : : {
974 : 1542 : flatten_join_alias_vars_context context;
975 : :
976 : : /*
977 : : * We do not expect this to be applied to the whole Query, only to
978 : : * expressions or LATERAL subqueries. Hence, if the top node is a Query,
979 : : * it's okay to immediately increment sublevels_up.
980 : : */
981 [ + - ]: 1542 : Assert(node != (Node *) query);
982 : :
983 : 1542 : context.root = root;
984 : 1542 : context.query = query;
985 : 1542 : context.sublevels_up = 0;
986 : : /* flag whether grouping expressions could possibly contain SubLinks */
987 : 1542 : context.possible_sublink = query->hasSubLinks;
988 : : /* if hasSubLinks is already true, no need to work hard */
989 : 1542 : context.inserted_sublink = query->hasSubLinks;
990 : :
991 : 3084 : return flatten_group_exprs_mutator(node, &context);
992 : 1542 : }
993 : :
994 : : static Node *
995 : 15637 : flatten_group_exprs_mutator(Node *node,
996 : : flatten_join_alias_vars_context *context)
997 : : {
998 [ + + ]: 15637 : if (node == NULL)
999 : 1759 : return NULL;
1000 [ + + ]: 13878 : if (IsA(node, Var))
1001 : : {
1002 : 2709 : Var *var = (Var *) node;
1003 : 2709 : RangeTblEntry *rte;
1004 : 2709 : Node *newvar;
1005 : :
1006 : : /* No change unless Var belongs to the GROUP of the target level */
1007 [ - + ]: 2709 : if (var->varlevelsup != context->sublevels_up)
1008 : 0 : return node; /* no need to copy, really */
1009 : 2709 : rte = rt_fetch(var->varno, context->query->rtable);
1010 [ + + ]: 2709 : if (rte->rtekind != RTE_GROUP)
1011 : 24 : return node;
1012 : :
1013 : : /* Expand group exprs reference */
1014 [ + - ]: 2685 : Assert(var->varattno > 0);
1015 : 2685 : newvar = (Node *) list_nth(rte->groupexprs, var->varattno - 1);
1016 [ + - ]: 2685 : Assert(newvar != NULL);
1017 : 2685 : newvar = copyObject(newvar);
1018 : :
1019 : : /*
1020 : : * If we are expanding an expr carried down from an upper query, must
1021 : : * adjust its varlevelsup fields.
1022 : : */
1023 [ + - ]: 2685 : if (context->sublevels_up != 0)
1024 : 0 : IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
1025 : :
1026 : : /* Preserve original Var's location, if possible */
1027 [ + + ]: 2685 : if (IsA(newvar, Var))
1028 : 2294 : ((Var *) newvar)->location = var->location;
1029 : :
1030 : : /* Detect if we are adding a sublink to query */
1031 [ + + + - ]: 2685 : if (context->possible_sublink && !context->inserted_sublink)
1032 : 0 : context->inserted_sublink = checkExprHasSubLink(newvar);
1033 : :
1034 : : /* Lastly, add any varnullingrels to the replacement expression */
1035 : 2685 : return mark_nullable_by_grouping(context->root, newvar, var);
1036 : 2709 : }
1037 : :
1038 [ + + ]: 11169 : if (IsA(node, Aggref))
1039 : : {
1040 : 1079 : Aggref *agg = (Aggref *) node;
1041 : :
1042 [ + - ]: 1079 : if ((int) agg->agglevelsup == context->sublevels_up)
1043 : : {
1044 : : /*
1045 : : * If we find an aggregate call of the original level, do not
1046 : : * recurse into its normal arguments, ORDER BY arguments, or
1047 : : * filter; there are no grouped vars there. But we should check
1048 : : * direct arguments as though they weren't in an aggregate.
1049 : : */
1050 : 1079 : agg = copyObject(agg);
1051 : 1079 : agg->aggdirectargs = (List *)
1052 : 1079 : flatten_group_exprs_mutator((Node *) agg->aggdirectargs, context);
1053 : :
1054 : 1079 : return (Node *) agg;
1055 : : }
1056 : :
1057 : : /*
1058 : : * We can skip recursing into aggregates of higher levels altogether,
1059 : : * since they could not possibly contain Vars of concern to us (see
1060 : : * transformAggregateCall). We do need to look at aggregates of lower
1061 : : * levels, however.
1062 : : */
1063 [ # # ]: 0 : if ((int) agg->agglevelsup > context->sublevels_up)
1064 : 0 : return node;
1065 [ + - ]: 1079 : }
1066 : :
1067 [ + + ]: 10090 : if (IsA(node, GroupingFunc))
1068 : : {
1069 : 58 : GroupingFunc *grp = (GroupingFunc *) node;
1070 : :
1071 : : /*
1072 : : * If we find a GroupingFunc node of the original or higher level, do
1073 : : * not recurse into its arguments; there are no grouped vars there.
1074 : : */
1075 [ + - ]: 58 : if ((int) grp->agglevelsup >= context->sublevels_up)
1076 : 58 : return node;
1077 [ + - ]: 58 : }
1078 : :
1079 [ - + ]: 10032 : if (IsA(node, Query))
1080 : : {
1081 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1082 : 0 : Query *newnode;
1083 : 0 : bool save_inserted_sublink;
1084 : :
1085 : 0 : context->sublevels_up++;
1086 : 0 : save_inserted_sublink = context->inserted_sublink;
1087 : 0 : context->inserted_sublink = ((Query *) node)->hasSubLinks;
1088 : 0 : newnode = query_tree_mutator((Query *) node,
1089 : : flatten_group_exprs_mutator,
1090 : : context,
1091 : : QTW_IGNORE_GROUPEXPRS);
1092 : 0 : newnode->hasSubLinks |= context->inserted_sublink;
1093 : 0 : context->inserted_sublink = save_inserted_sublink;
1094 : 0 : context->sublevels_up--;
1095 : 0 : return (Node *) newnode;
1096 : 0 : }
1097 : :
1098 : 10032 : return expression_tree_mutator(node, flatten_group_exprs_mutator,
1099 : : context);
1100 : 15637 : }
1101 : :
1102 : : /*
1103 : : * Add oldvar's varnullingrels, if any, to a flattened grouping expression.
1104 : : * The newnode has been copied, so we can modify it freely.
1105 : : */
1106 : : static Node *
1107 : 2685 : mark_nullable_by_grouping(PlannerInfo *root, Node *newnode, Var *oldvar)
1108 : : {
1109 : 2685 : Relids relids;
1110 : :
1111 [ + + ]: 2685 : if (root == NULL)
1112 : 1129 : return newnode;
1113 [ + + ]: 1556 : if (oldvar->varnullingrels == NULL)
1114 : 1230 : return newnode; /* nothing to do */
1115 : :
1116 [ + - ]: 326 : Assert(bms_equal(oldvar->varnullingrels,
1117 : : bms_make_singleton(root->group_rtindex)));
1118 : :
1119 : 326 : relids = pull_varnos_of_level(root, newnode, oldvar->varlevelsup);
1120 : :
1121 [ + + ]: 326 : if (!bms_is_empty(relids))
1122 : : {
1123 : : /*
1124 : : * If the newnode is not variable-free, we set the nullingrels of Vars
1125 : : * or PHVs that are contained in the expression. This is not really
1126 : : * 'correct' in theory, because it is the whole expression that can be
1127 : : * nullable by grouping sets, not its individual vars. But it works
1128 : : * in practice, because what we need is that the expression can be
1129 : : * somehow distinguished from the same expression in ECs, and marking
1130 : : * its vars is sufficient for this purpose.
1131 : : */
1132 : 636 : newnode = add_nulling_relids(newnode,
1133 : 318 : relids,
1134 : 318 : oldvar->varnullingrels);
1135 : 318 : }
1136 : : else /* variable-free? */
1137 : : {
1138 : : /*
1139 : : * If the newnode is variable-free and does not contain volatile
1140 : : * functions or set-returning functions, it can be treated as a member
1141 : : * of EC that is redundant. So wrap it in a new PlaceHolderVar to
1142 : : * carry the nullingrels. Otherwise we do not bother to make any
1143 : : * changes.
1144 : : *
1145 : : * Aggregate functions and window functions are not allowed in
1146 : : * grouping expressions.
1147 : : */
1148 [ + - ]: 8 : Assert(!contain_agg_clause(newnode));
1149 [ + - ]: 8 : Assert(!contain_window_function(newnode));
1150 : :
1151 [ + - + + ]: 8 : if (!contain_volatile_functions(newnode) &&
1152 : 8 : !expression_returns_set(newnode))
1153 : : {
1154 : 5 : PlaceHolderVar *newphv;
1155 : 5 : Relids phrels;
1156 : :
1157 : 5 : phrels = get_relids_in_jointree((Node *) root->parse->jointree,
1158 : : true, false);
1159 [ + - ]: 5 : Assert(!bms_is_empty(phrels));
1160 : :
1161 : 5 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1162 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1163 : 5 : newphv->phlevelsup = oldvar->varlevelsup;
1164 : 5 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1165 : 5 : newnode = (Node *) newphv;
1166 : 5 : }
1167 : : }
1168 : :
1169 : 326 : return newnode;
1170 : 2685 : }
1171 : :
1172 : : /*
1173 : : * Add oldvar's varnullingrels, if any, to a flattened join alias expression.
1174 : : * The newnode has been copied, so we can modify it freely.
1175 : : */
1176 : : static Node *
1177 : 81 : add_nullingrels_if_needed(PlannerInfo *root, Node *newnode, Var *oldvar)
1178 : : {
1179 [ + + ]: 81 : if (oldvar->varnullingrels == NULL)
1180 : 57 : return newnode; /* nothing to do */
1181 : : /* If possible, do it by adding to existing nullingrel fields */
1182 [ + + ]: 24 : if (is_standard_join_alias_expression(newnode, oldvar))
1183 : 22 : adjust_standard_join_alias_expression(newnode, oldvar);
1184 [ + - ]: 2 : else if (root)
1185 : : {
1186 : : /*
1187 : : * We can insert a PlaceHolderVar to carry the nullingrels. However,
1188 : : * deciding where to evaluate the PHV is slightly tricky. We first
1189 : : * try to evaluate it at the natural semantic level of the new
1190 : : * expression; but if that expression is variable-free, fall back to
1191 : : * evaluating it at the join that the oldvar is an alias Var for.
1192 : : */
1193 : 2 : PlaceHolderVar *newphv;
1194 : 2 : Index levelsup = oldvar->varlevelsup;
1195 : 2 : Relids phrels = pull_varnos_of_level(root, newnode, levelsup);
1196 : :
1197 [ - + ]: 2 : if (bms_is_empty(phrels)) /* variable-free? */
1198 : : {
1199 [ + - ]: 2 : if (levelsup != 0) /* this won't work otherwise */
1200 [ # # # # ]: 0 : elog(ERROR, "unsupported join alias expression");
1201 : 2 : phrels = get_relids_for_join(root->parse, oldvar->varno);
1202 : : /* If it's an outer join, eval below not above the join */
1203 : 2 : phrels = bms_del_member(phrels, oldvar->varno);
1204 [ + - ]: 2 : Assert(!bms_is_empty(phrels));
1205 : 2 : }
1206 : 2 : newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
1207 : : /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1208 : 2 : newphv->phlevelsup = levelsup;
1209 : 2 : newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1210 : 2 : newnode = (Node *) newphv;
1211 : 2 : }
1212 : : else
1213 : : {
1214 : : /* ooops, we're missing support for something the parser can make */
1215 [ # # # # ]: 0 : elog(ERROR, "unsupported join alias expression");
1216 : : }
1217 : 24 : return newnode;
1218 : 81 : }
1219 : :
1220 : : /*
1221 : : * Check to see if we can insert nullingrels into this join alias expression
1222 : : * without use of a separate PlaceHolderVar.
1223 : : *
1224 : : * This will handle Vars, PlaceHolderVars, and implicit-coercion and COALESCE
1225 : : * expressions built from those. This coverage needs to handle anything
1226 : : * that the parser would put into joinaliasvars.
1227 : : */
1228 : : static bool
1229 : 74 : is_standard_join_alias_expression(Node *newnode, Var *oldvar)
1230 : : {
1231 [ + - ]: 74 : if (newnode == NULL)
1232 : 0 : return false;
1233 [ + + - + ]: 74 : if (IsA(newnode, Var) &&
1234 : 44 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1235 : 44 : return true;
1236 [ - + # # ]: 30 : else if (IsA(newnode, PlaceHolderVar) &&
1237 : 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1238 : 0 : return true;
1239 [ + + ]: 30 : else if (IsA(newnode, FuncExpr))
1240 : : {
1241 : 4 : FuncExpr *fexpr = (FuncExpr *) newnode;
1242 : :
1243 : : /*
1244 : : * We need to assume that the function wouldn't produce non-NULL from
1245 : : * NULL, which is reasonable for implicit coercions but otherwise not
1246 : : * so much. (Looking at its strictness is likely overkill, and anyway
1247 : : * it would cause us to fail if someone forgot to mark an implicit
1248 : : * coercion as strict.)
1249 : : */
1250 [ + - - + ]: 4 : if (fexpr->funcformat != COERCE_IMPLICIT_CAST ||
1251 : 4 : fexpr->args == NIL)
1252 : 0 : return false;
1253 : :
1254 : : /*
1255 : : * Examine only the first argument --- coercions might have additional
1256 : : * arguments that are constants.
1257 : : */
1258 : 4 : return is_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1259 : 4 : }
1260 [ + + ]: 26 : else if (IsA(newnode, RelabelType))
1261 : : {
1262 : 2 : RelabelType *relabel = (RelabelType *) newnode;
1263 : :
1264 : : /* This definitely won't produce non-NULL from NULL */
1265 : 2 : return is_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1266 : 2 : }
1267 [ - + ]: 24 : else if (IsA(newnode, CoerceViaIO))
1268 : : {
1269 : 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1270 : :
1271 : : /* This definitely won't produce non-NULL from NULL */
1272 : 0 : return is_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1273 : 0 : }
1274 [ - + ]: 24 : else if (IsA(newnode, ArrayCoerceExpr))
1275 : : {
1276 : 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1277 : :
1278 : : /* This definitely won't produce non-NULL from NULL (at array level) */
1279 : 0 : return is_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1280 : 0 : }
1281 [ + + ]: 24 : else if (IsA(newnode, CoalesceExpr))
1282 : : {
1283 : 22 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1284 : 22 : ListCell *lc;
1285 : :
1286 [ + - ]: 22 : Assert(cexpr->args != NIL);
1287 [ + - + + : 66 : foreach(lc, cexpr->args)
+ + - + ]
1288 : : {
1289 [ - + ]: 44 : if (!is_standard_join_alias_expression(lfirst(lc), oldvar))
1290 : 0 : return false;
1291 : 44 : }
1292 : 22 : return true;
1293 : 22 : }
1294 : : else
1295 : 2 : return false;
1296 : 74 : }
1297 : :
1298 : : /*
1299 : : * Insert nullingrels into an expression accepted by
1300 : : * is_standard_join_alias_expression.
1301 : : */
1302 : : static void
1303 : 70 : adjust_standard_join_alias_expression(Node *newnode, Var *oldvar)
1304 : : {
1305 [ + + - + ]: 70 : if (IsA(newnode, Var) &&
1306 : 44 : ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1307 : : {
1308 : 44 : Var *newvar = (Var *) newnode;
1309 : :
1310 : 88 : newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
1311 : 44 : oldvar->varnullingrels);
1312 : 44 : }
1313 [ - + # # ]: 26 : else if (IsA(newnode, PlaceHolderVar) &&
1314 : 0 : ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1315 : : {
1316 : 0 : PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
1317 : :
1318 : 0 : newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
1319 : 0 : oldvar->varnullingrels);
1320 : 0 : }
1321 [ + + ]: 26 : else if (IsA(newnode, FuncExpr))
1322 : : {
1323 : 2 : FuncExpr *fexpr = (FuncExpr *) newnode;
1324 : :
1325 : 2 : adjust_standard_join_alias_expression(linitial(fexpr->args), oldvar);
1326 : 2 : }
1327 [ + + ]: 24 : else if (IsA(newnode, RelabelType))
1328 : : {
1329 : 2 : RelabelType *relabel = (RelabelType *) newnode;
1330 : :
1331 : 2 : adjust_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1332 : 2 : }
1333 [ - + ]: 22 : else if (IsA(newnode, CoerceViaIO))
1334 : : {
1335 : 0 : CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1336 : :
1337 : 0 : adjust_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1338 : 0 : }
1339 [ - + ]: 22 : else if (IsA(newnode, ArrayCoerceExpr))
1340 : : {
1341 : 0 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1342 : :
1343 : 0 : adjust_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1344 : 0 : }
1345 [ + - ]: 22 : else if (IsA(newnode, CoalesceExpr))
1346 : : {
1347 : 22 : CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1348 : 22 : ListCell *lc;
1349 : :
1350 [ + - ]: 22 : Assert(cexpr->args != NIL);
1351 [ + - + + : 66 : foreach(lc, cexpr->args)
+ + ]
1352 : : {
1353 : 44 : adjust_standard_join_alias_expression(lfirst(lc), oldvar);
1354 : 44 : }
1355 : 22 : }
1356 : : else
1357 : 0 : Assert(false);
1358 : 70 : }
1359 : :
1360 : : /*
1361 : : * alias_relid_set: in a set of RT indexes, replace joins by their
1362 : : * underlying base+OJ relids
1363 : : */
1364 : : static Relids
1365 : 301 : alias_relid_set(Query *query, Relids relids)
1366 : : {
1367 : 301 : Relids result = NULL;
1368 : 301 : int rtindex;
1369 : :
1370 : 301 : rtindex = -1;
1371 [ + + ]: 765 : while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1372 : : {
1373 : 464 : RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
1374 : :
1375 [ + + ]: 464 : if (rte->rtekind == RTE_JOIN)
1376 : 57 : result = bms_join(result, get_relids_for_join(query, rtindex));
1377 : : else
1378 : 407 : result = bms_add_member(result, rtindex);
1379 : 464 : }
1380 : 602 : return result;
1381 : 301 : }
|