PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeFuncs.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeFuncs.c
4 * Various general-purpose manipulations of Node trees
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/nodes/nodeFuncs.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
18#include "catalog/pg_type.h"
19#include "miscadmin.h"
20#include "nodes/execnodes.h"
21#include "nodes/nodeFuncs.h"
22#include "nodes/pathnodes.h"
23#include "utils/builtins.h"
24#include "utils/lsyscache.h"
25
26static bool expression_returns_set_walker(Node *node, void *context);
27static int leftmostLoc(int loc1, int loc2);
28static bool fix_opfuncids_walker(Node *node, void *context);
29static bool planstate_walk_subplans(List *plans,
31 void *context);
32static bool planstate_walk_members(PlanState **planstates, int nplans,
34 void *context);
35
36
37/*
38 * exprType -
39 * returns the Oid of the type of the expression's result.
40 */
41Oid
42exprType(const Node *expr)
43{
44 Oid type;
45
46 if (!expr)
47 return InvalidOid;
48
49 switch (nodeTag(expr))
50 {
51 case T_Var:
52 type = ((const Var *) expr)->vartype;
53 break;
54 case T_Const:
55 type = ((const Const *) expr)->consttype;
56 break;
57 case T_Param:
58 type = ((const Param *) expr)->paramtype;
59 break;
60 case T_Aggref:
61 type = ((const Aggref *) expr)->aggtype;
62 break;
63 case T_GroupingFunc:
64 type = INT4OID;
65 break;
66 case T_WindowFunc:
67 type = ((const WindowFunc *) expr)->wintype;
68 break;
70 type = ((const MergeSupportFunc *) expr)->msftype;
71 break;
73 type = ((const SubscriptingRef *) expr)->refrestype;
74 break;
75 case T_FuncExpr:
76 type = ((const FuncExpr *) expr)->funcresulttype;
77 break;
78 case T_NamedArgExpr:
79 type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
80 break;
81 case T_OpExpr:
82 type = ((const OpExpr *) expr)->opresulttype;
83 break;
84 case T_DistinctExpr:
85 type = ((const DistinctExpr *) expr)->opresulttype;
86 break;
87 case T_NullIfExpr:
88 type = ((const NullIfExpr *) expr)->opresulttype;
89 break;
91 type = BOOLOID;
92 break;
93 case T_BoolExpr:
94 type = BOOLOID;
95 break;
96 case T_SubLink:
97 {
98 const SubLink *sublink = (const SubLink *) expr;
99
100 if (sublink->subLinkType == EXPR_SUBLINK ||
101 sublink->subLinkType == ARRAY_SUBLINK)
102 {
103 /* get the type of the subselect's first target column */
104 Query *qtree = (Query *) sublink->subselect;
106
107 if (!qtree || !IsA(qtree, Query))
108 elog(ERROR, "cannot get type for untransformed sublink");
109 tent = linitial_node(TargetEntry, qtree->targetList);
110 Assert(!tent->resjunk);
111 type = exprType((Node *) tent->expr);
112 if (sublink->subLinkType == ARRAY_SUBLINK)
113 {
115 if (!OidIsValid(type))
118 errmsg("could not find array type for data type %s",
119 format_type_be(exprType((Node *) tent->expr)))));
120 }
121 }
122 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
123 {
124 /* MULTIEXPR is always considered to return RECORD */
125 type = RECORDOID;
126 }
127 else
128 {
129 /* for all other sublink types, result is boolean */
130 type = BOOLOID;
131 }
132 }
133 break;
134 case T_SubPlan:
135 {
136 const SubPlan *subplan = (const SubPlan *) expr;
137
138 if (subplan->subLinkType == EXPR_SUBLINK ||
139 subplan->subLinkType == ARRAY_SUBLINK)
140 {
141 /* get the type of the subselect's first target column */
142 type = subplan->firstColType;
143 if (subplan->subLinkType == ARRAY_SUBLINK)
144 {
146 if (!OidIsValid(type))
149 errmsg("could not find array type for data type %s",
150 format_type_be(subplan->firstColType))));
151 }
152 }
153 else if (subplan->subLinkType == MULTIEXPR_SUBLINK)
154 {
155 /* MULTIEXPR is always considered to return RECORD */
156 type = RECORDOID;
157 }
158 else
159 {
160 /* for all other subplan types, result is boolean */
161 type = BOOLOID;
162 }
163 }
164 break;
166 {
167 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
168
169 /* subplans should all return the same thing */
170 type = exprType((Node *) linitial(asplan->subplans));
171 }
172 break;
173 case T_FieldSelect:
174 type = ((const FieldSelect *) expr)->resulttype;
175 break;
176 case T_FieldStore:
177 type = ((const FieldStore *) expr)->resulttype;
178 break;
179 case T_RelabelType:
180 type = ((const RelabelType *) expr)->resulttype;
181 break;
182 case T_CoerceViaIO:
183 type = ((const CoerceViaIO *) expr)->resulttype;
184 break;
186 type = ((const ArrayCoerceExpr *) expr)->resulttype;
187 break;
189 type = ((const ConvertRowtypeExpr *) expr)->resulttype;
190 break;
191 case T_CollateExpr:
192 type = exprType((Node *) ((const CollateExpr *) expr)->arg);
193 break;
194 case T_CaseExpr:
195 type = ((const CaseExpr *) expr)->casetype;
196 break;
197 case T_CaseTestExpr:
198 type = ((const CaseTestExpr *) expr)->typeId;
199 break;
200 case T_ArrayExpr:
201 type = ((const ArrayExpr *) expr)->array_typeid;
202 break;
203 case T_RowExpr:
204 type = ((const RowExpr *) expr)->row_typeid;
205 break;
206 case T_RowCompareExpr:
207 type = BOOLOID;
208 break;
209 case T_CoalesceExpr:
210 type = ((const CoalesceExpr *) expr)->coalescetype;
211 break;
212 case T_MinMaxExpr:
213 type = ((const MinMaxExpr *) expr)->minmaxtype;
214 break;
216 type = ((const SQLValueFunction *) expr)->type;
217 break;
218 case T_XmlExpr:
219 if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
220 type = BOOLOID;
221 else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
222 type = TEXTOID;
223 else
224 type = XMLOID;
225 break;
226 case T_JsonValueExpr:
227 {
228 const JsonValueExpr *jve = (const JsonValueExpr *) expr;
229
230 type = exprType((Node *) jve->formatted_expr);
231 }
232 break;
234 type = ((const JsonConstructorExpr *) expr)->returning->typid;
235 break;
237 type = BOOLOID;
238 break;
239 case T_JsonExpr:
240 {
241 const JsonExpr *jexpr = (const JsonExpr *) expr;
242
243 type = jexpr->returning->typid;
244 break;
245 }
246 case T_JsonBehavior:
247 {
248 const JsonBehavior *behavior = (const JsonBehavior *) expr;
249
250 type = exprType(behavior->expr);
251 break;
252 }
253 case T_NullTest:
254 type = BOOLOID;
255 break;
256 case T_BooleanTest:
257 type = BOOLOID;
258 break;
259 case T_CoerceToDomain:
260 type = ((const CoerceToDomain *) expr)->resulttype;
261 break;
263 type = ((const CoerceToDomainValue *) expr)->typeId;
264 break;
265 case T_SetToDefault:
266 type = ((const SetToDefault *) expr)->typeId;
267 break;
268 case T_CurrentOfExpr:
269 type = BOOLOID;
270 break;
271 case T_NextValueExpr:
272 type = ((const NextValueExpr *) expr)->typeId;
273 break;
274 case T_InferenceElem:
275 {
276 const InferenceElem *n = (const InferenceElem *) expr;
277
278 type = exprType((Node *) n->expr);
279 }
280 break;
281 case T_ReturningExpr:
282 type = exprType((Node *) ((const ReturningExpr *) expr)->retexpr);
283 break;
284 case T_PlaceHolderVar:
285 type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
286 break;
288 type = ((const GraphPropertyRef *) expr)->typeId;
289 break;
290 default:
291 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
292 type = InvalidOid; /* keep compiler quiet */
293 break;
294 }
295 return type;
296}
297
298/*
299 * exprTypmod -
300 * returns the type-specific modifier of the expression's result type,
301 * if it can be determined. In many cases, it can't and we return -1.
302 */
303int32
304exprTypmod(const Node *expr)
305{
306 if (!expr)
307 return -1;
308
309 switch (nodeTag(expr))
310 {
311 case T_Var:
312 return ((const Var *) expr)->vartypmod;
313 case T_Const:
314 return ((const Const *) expr)->consttypmod;
315 case T_Param:
316 return ((const Param *) expr)->paramtypmod;
318 return ((const SubscriptingRef *) expr)->reftypmod;
319 case T_FuncExpr:
320 {
322
323 /* Be smart about length-coercion functions... */
325 return coercedTypmod;
326 }
327 break;
328 case T_NamedArgExpr:
329 return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
330 case T_NullIfExpr:
331 {
332 /*
333 * Result is either first argument or NULL, so we can report
334 * first argument's typmod if known.
335 */
336 const NullIfExpr *nexpr = (const NullIfExpr *) expr;
337
338 return exprTypmod((Node *) linitial(nexpr->args));
339 }
340 break;
341 case T_SubLink:
342 {
343 const SubLink *sublink = (const SubLink *) expr;
344
345 if (sublink->subLinkType == EXPR_SUBLINK ||
346 sublink->subLinkType == ARRAY_SUBLINK)
347 {
348 /* get the typmod of the subselect's first target column */
349 Query *qtree = (Query *) sublink->subselect;
351
352 if (!qtree || !IsA(qtree, Query))
353 elog(ERROR, "cannot get type for untransformed sublink");
354 tent = linitial_node(TargetEntry, qtree->targetList);
355 Assert(!tent->resjunk);
356 return exprTypmod((Node *) tent->expr);
357 /* note we don't need to care if it's an array */
358 }
359 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
360 }
361 break;
362 case T_SubPlan:
363 {
364 const SubPlan *subplan = (const SubPlan *) expr;
365
366 if (subplan->subLinkType == EXPR_SUBLINK ||
367 subplan->subLinkType == ARRAY_SUBLINK)
368 {
369 /* get the typmod of the subselect's first target column */
370 /* note we don't need to care if it's an array */
371 return subplan->firstColTypmod;
372 }
373 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
374 }
375 break;
377 {
378 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
379
380 /* subplans should all return the same thing */
381 return exprTypmod((Node *) linitial(asplan->subplans));
382 }
383 break;
384 case T_FieldSelect:
385 return ((const FieldSelect *) expr)->resulttypmod;
386 case T_RelabelType:
387 return ((const RelabelType *) expr)->resulttypmod;
389 return ((const ArrayCoerceExpr *) expr)->resulttypmod;
390 case T_CollateExpr:
391 return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
392 case T_CaseExpr:
393 {
394 /*
395 * If all the alternatives agree on type/typmod, return that
396 * typmod, else use -1
397 */
398 const CaseExpr *cexpr = (const CaseExpr *) expr;
399 Oid casetype = cexpr->casetype;
400 int32 typmod;
401 ListCell *arg;
402
403 if (!cexpr->defresult)
404 return -1;
405 if (exprType((Node *) cexpr->defresult) != casetype)
406 return -1;
407 typmod = exprTypmod((Node *) cexpr->defresult);
408 if (typmod < 0)
409 return -1; /* no point in trying harder */
410 foreach(arg, cexpr->args)
411 {
413
414 if (exprType((Node *) w->result) != casetype)
415 return -1;
416 if (exprTypmod((Node *) w->result) != typmod)
417 return -1;
418 }
419 return typmod;
420 }
421 break;
422 case T_CaseTestExpr:
423 return ((const CaseTestExpr *) expr)->typeMod;
424 case T_ArrayExpr:
425 {
426 /*
427 * If all the elements agree on type/typmod, return that
428 * typmod, else use -1
429 */
430 const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
432 int32 typmod;
433 ListCell *elem;
434
435 if (arrayexpr->elements == NIL)
436 return -1;
437 typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
438 if (typmod < 0)
439 return -1; /* no point in trying harder */
440 if (arrayexpr->multidims)
441 commontype = arrayexpr->array_typeid;
442 else
443 commontype = arrayexpr->element_typeid;
444 foreach(elem, arrayexpr->elements)
445 {
446 Node *e = (Node *) lfirst(elem);
447
448 if (exprType(e) != commontype)
449 return -1;
450 if (exprTypmod(e) != typmod)
451 return -1;
452 }
453 return typmod;
454 }
455 break;
456 case T_CoalesceExpr:
457 {
458 /*
459 * If all the alternatives agree on type/typmod, return that
460 * typmod, else use -1
461 */
462 const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
463 Oid coalescetype = cexpr->coalescetype;
464 int32 typmod;
465 ListCell *arg;
466
467 if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
468 return -1;
469 typmod = exprTypmod((Node *) linitial(cexpr->args));
470 if (typmod < 0)
471 return -1; /* no point in trying harder */
472 for_each_from(arg, cexpr->args, 1)
473 {
474 Node *e = (Node *) lfirst(arg);
475
476 if (exprType(e) != coalescetype)
477 return -1;
478 if (exprTypmod(e) != typmod)
479 return -1;
480 }
481 return typmod;
482 }
483 break;
484 case T_MinMaxExpr:
485 {
486 /*
487 * If all the alternatives agree on type/typmod, return that
488 * typmod, else use -1
489 */
490 const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
491 Oid minmaxtype = mexpr->minmaxtype;
492 int32 typmod;
493 ListCell *arg;
494
495 if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
496 return -1;
497 typmod = exprTypmod((Node *) linitial(mexpr->args));
498 if (typmod < 0)
499 return -1; /* no point in trying harder */
500 for_each_from(arg, mexpr->args, 1)
501 {
502 Node *e = (Node *) lfirst(arg);
503
504 if (exprType(e) != minmaxtype)
505 return -1;
506 if (exprTypmod(e) != typmod)
507 return -1;
508 }
509 return typmod;
510 }
511 break;
513 return ((const SQLValueFunction *) expr)->typmod;
514 case T_JsonValueExpr:
515 return exprTypmod((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
517 return ((const JsonConstructorExpr *) expr)->returning->typmod;
518 case T_JsonExpr:
519 {
520 const JsonExpr *jexpr = (const JsonExpr *) expr;
521
522 return jexpr->returning->typmod;
523 }
524 break;
525 case T_JsonBehavior:
526 {
527 const JsonBehavior *behavior = (const JsonBehavior *) expr;
528
529 return exprTypmod(behavior->expr);
530 }
531 break;
532 case T_CoerceToDomain:
533 return ((const CoerceToDomain *) expr)->resulttypmod;
535 return ((const CoerceToDomainValue *) expr)->typeMod;
536 case T_SetToDefault:
537 return ((const SetToDefault *) expr)->typeMod;
538 case T_ReturningExpr:
539 return exprTypmod((Node *) ((const ReturningExpr *) expr)->retexpr);
540 case T_PlaceHolderVar:
541 return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
543 return ((const GraphPropertyRef *) expr)->typmod;
544 default:
545 break;
546 }
547 return -1;
548}
549
550/*
551 * exprIsLengthCoercion
552 * Detect whether an expression tree is an application of a datatype's
553 * typmod-coercion function. Optionally extract the result's typmod.
554 *
555 * If coercedTypmod is not NULL, the typmod is stored there if the expression
556 * is a length-coercion function, else -1 is stored there.
557 *
558 * Note that a combined type-and-length coercion will be treated as a
559 * length coercion by this routine.
560 */
561bool
563{
564 if (coercedTypmod != NULL)
565 *coercedTypmod = -1; /* default result on failure */
566
567 /*
568 * Scalar-type length coercions are FuncExprs, array-type length coercions
569 * are ArrayCoerceExprs
570 */
571 if (expr && IsA(expr, FuncExpr))
572 {
573 const FuncExpr *func = (const FuncExpr *) expr;
574 int nargs;
576
577 /*
578 * If it didn't come from a coercion context, reject.
579 */
580 if (func->funcformat != COERCE_EXPLICIT_CAST &&
581 func->funcformat != COERCE_IMPLICIT_CAST)
582 return false;
583
584 /*
585 * If it's not a two-argument or three-argument function with the
586 * second argument being an int4 constant, it can't have been created
587 * from a length coercion (it must be a type coercion, instead).
588 */
589 nargs = list_length(func->args);
591 return false;
592
593 second_arg = (Const *) lsecond(func->args);
594 if (!IsA(second_arg, Const) ||
595 second_arg->consttype != INT4OID ||
596 second_arg->constisnull)
597 return false;
598
599 /*
600 * OK, it is indeed a length-coercion function.
601 */
602 if (coercedTypmod != NULL)
603 *coercedTypmod = DatumGetInt32(second_arg->constvalue);
604
605 return true;
606 }
607
608 if (expr && IsA(expr, ArrayCoerceExpr))
609 {
610 const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
611
612 /* It's not a length coercion unless there's a nondefault typmod */
613 if (acoerce->resulttypmod < 0)
614 return false;
615
616 /*
617 * OK, it is indeed a length-coercion expression.
618 */
619 if (coercedTypmod != NULL)
620 *coercedTypmod = acoerce->resulttypmod;
621
622 return true;
623 }
624
625 return false;
626}
627
628/*
629 * applyRelabelType
630 * Add a RelabelType node if needed to make the expression expose
631 * the specified type, typmod, and collation.
632 *
633 * This is primarily intended to be used during planning. Therefore, it must
634 * maintain the post-eval_const_expressions invariants that there are not
635 * adjacent RelabelTypes, and that the tree is fully const-folded (hence,
636 * we mustn't return a RelabelType atop a Const). If we do find a Const,
637 * we'll modify it in-place if "overwrite_ok" is true; that should only be
638 * passed as true if caller knows the Const is newly generated.
639 */
640Node *
643{
644 /*
645 * If we find stacked RelabelTypes (eg, from foo::int::oid) we can discard
646 * all but the top one, and must do so to ensure that semantically
647 * equivalent expressions are equal().
648 */
649 while (arg && IsA(arg, RelabelType))
650 arg = (Node *) ((RelabelType *) arg)->arg;
651
652 if (arg && IsA(arg, Const))
653 {
654 /* Modify the Const directly to preserve const-flatness. */
655 Const *con = (Const *) arg;
656
657 if (!overwrite_ok)
658 con = copyObject(con);
659 con->consttype = rtype;
660 con->consttypmod = rtypmod;
661 con->constcollid = rcollid;
662 /* We keep the Const's original location. */
663 return (Node *) con;
664 }
665 else if (exprType(arg) == rtype &&
666 exprTypmod(arg) == rtypmod &&
668 {
669 /* Sometimes we find a nest of relabels that net out to nothing. */
670 return arg;
671 }
672 else
673 {
674 /* Nope, gotta have a RelabelType. */
676
677 newrelabel->arg = (Expr *) arg;
678 newrelabel->resulttype = rtype;
679 newrelabel->resulttypmod = rtypmod;
680 newrelabel->resultcollid = rcollid;
681 newrelabel->relabelformat = rformat;
682 newrelabel->location = rlocation;
683 return (Node *) newrelabel;
684 }
685}
686
687/*
688 * relabel_to_typmod
689 * Add a RelabelType node that changes just the typmod of the expression.
690 *
691 * Convenience function for a common usage of applyRelabelType.
692 */
693Node *
695{
696 return applyRelabelType(expr, exprType(expr), typmod, exprCollation(expr),
697 COERCE_EXPLICIT_CAST, -1, false);
698}
699
700/*
701 * strip_implicit_coercions: remove implicit coercions at top level of tree
702 *
703 * This doesn't modify or copy the input expression tree, just return a
704 * pointer to a suitable place within it.
705 *
706 * Note: there isn't any useful thing we can do with a RowExpr here, so
707 * just return it unchanged, even if it's marked as an implicit coercion.
708 */
709Node *
711{
712 if (node == NULL)
713 return NULL;
714 if (IsA(node, FuncExpr))
715 {
716 FuncExpr *f = (FuncExpr *) node;
717
718 if (f->funcformat == COERCE_IMPLICIT_CAST)
720 }
721 else if (IsA(node, RelabelType))
722 {
723 RelabelType *r = (RelabelType *) node;
724
725 if (r->relabelformat == COERCE_IMPLICIT_CAST)
726 return strip_implicit_coercions((Node *) r->arg);
727 }
728 else if (IsA(node, CoerceViaIO))
729 {
730 CoerceViaIO *c = (CoerceViaIO *) node;
731
732 if (c->coerceformat == COERCE_IMPLICIT_CAST)
733 return strip_implicit_coercions((Node *) c->arg);
734 }
735 else if (IsA(node, ArrayCoerceExpr))
736 {
738
739 if (c->coerceformat == COERCE_IMPLICIT_CAST)
740 return strip_implicit_coercions((Node *) c->arg);
741 }
742 else if (IsA(node, ConvertRowtypeExpr))
743 {
745
746 if (c->convertformat == COERCE_IMPLICIT_CAST)
747 return strip_implicit_coercions((Node *) c->arg);
748 }
749 else if (IsA(node, CoerceToDomain))
750 {
751 CoerceToDomain *c = (CoerceToDomain *) node;
752
753 if (c->coercionformat == COERCE_IMPLICIT_CAST)
754 return strip_implicit_coercions((Node *) c->arg);
755 }
756 return node;
757}
758
759/*
760 * expression_returns_set
761 * Test whether an expression returns a set result.
762 *
763 * Because we use expression_tree_walker(), this can also be applied to
764 * whole targetlists; it'll produce true if any one of the tlist items
765 * returns a set.
766 */
767bool
769{
770 return expression_returns_set_walker(clause, NULL);
771}
772
773static bool
775{
776 if (node == NULL)
777 return false;
778 if (IsA(node, FuncExpr))
779 {
780 FuncExpr *expr = (FuncExpr *) node;
781
782 if (expr->funcretset)
783 return true;
784 /* else fall through to check args */
785 }
786 if (IsA(node, OpExpr))
787 {
788 OpExpr *expr = (OpExpr *) node;
789
790 if (expr->opretset)
791 return true;
792 /* else fall through to check args */
793 }
794
795 /*
796 * If you add any more cases that return sets, also fix
797 * expression_returns_set_rows() in clauses.c and IS_SRF_CALL() in
798 * tlist.c.
799 */
800
801 /* Avoid recursion for some cases that parser checks not to return a set */
802 if (IsA(node, Aggref))
803 return false;
804 if (IsA(node, GroupingFunc))
805 return false;
806 if (IsA(node, WindowFunc))
807 return false;
808
810 context);
811}
812
813
814/*
815 * exprCollation -
816 * returns the Oid of the collation of the expression's result.
817 *
818 * Note: expression nodes that can invoke functions generally have an
819 * "inputcollid" field, which is what the function should use as collation.
820 * That is the resolved common collation of the node's inputs. It is often
821 * but not always the same as the result collation; in particular, if the
822 * function produces a non-collatable result type from collatable inputs
823 * or vice versa, the two are different.
824 */
825Oid
826exprCollation(const Node *expr)
827{
828 Oid coll;
829
830 if (!expr)
831 return InvalidOid;
832
833 switch (nodeTag(expr))
834 {
835 case T_Var:
836 coll = ((const Var *) expr)->varcollid;
837 break;
838 case T_Const:
839 coll = ((const Const *) expr)->constcollid;
840 break;
841 case T_Param:
842 coll = ((const Param *) expr)->paramcollid;
843 break;
844 case T_Aggref:
845 coll = ((const Aggref *) expr)->aggcollid;
846 break;
847 case T_GroupingFunc:
849 break;
850 case T_WindowFunc:
851 coll = ((const WindowFunc *) expr)->wincollid;
852 break;
854 coll = ((const MergeSupportFunc *) expr)->msfcollid;
855 break;
857 coll = ((const SubscriptingRef *) expr)->refcollid;
858 break;
859 case T_FuncExpr:
860 coll = ((const FuncExpr *) expr)->funccollid;
861 break;
862 case T_NamedArgExpr:
863 coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
864 break;
865 case T_OpExpr:
866 coll = ((const OpExpr *) expr)->opcollid;
867 break;
868 case T_DistinctExpr:
869 coll = ((const DistinctExpr *) expr)->opcollid;
870 break;
871 case T_NullIfExpr:
872 coll = ((const NullIfExpr *) expr)->opcollid;
873 break;
875 /* ScalarArrayOpExpr's result is boolean ... */
876 coll = InvalidOid; /* ... so it has no collation */
877 break;
878 case T_BoolExpr:
879 /* BoolExpr's result is boolean ... */
880 coll = InvalidOid; /* ... so it has no collation */
881 break;
882 case T_SubLink:
883 {
884 const SubLink *sublink = (const SubLink *) expr;
885
886 if (sublink->subLinkType == EXPR_SUBLINK ||
887 sublink->subLinkType == ARRAY_SUBLINK)
888 {
889 /* get the collation of subselect's first target column */
890 Query *qtree = (Query *) sublink->subselect;
892
893 if (!qtree || !IsA(qtree, Query))
894 elog(ERROR, "cannot get collation for untransformed sublink");
895 tent = linitial_node(TargetEntry, qtree->targetList);
896 Assert(!tent->resjunk);
897 coll = exprCollation((Node *) tent->expr);
898 /* collation doesn't change if it's converted to array */
899 }
900 else
901 {
902 /* otherwise, SubLink's result is RECORD or BOOLEAN */
903 coll = InvalidOid; /* ... so it has no collation */
904 }
905 }
906 break;
907 case T_SubPlan:
908 {
909 const SubPlan *subplan = (const SubPlan *) expr;
910
911 if (subplan->subLinkType == EXPR_SUBLINK ||
912 subplan->subLinkType == ARRAY_SUBLINK)
913 {
914 /* get the collation of subselect's first target column */
915 coll = subplan->firstColCollation;
916 /* collation doesn't change if it's converted to array */
917 }
918 else
919 {
920 /* otherwise, SubPlan's result is RECORD or BOOLEAN */
921 coll = InvalidOid; /* ... so it has no collation */
922 }
923 }
924 break;
926 {
927 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
928
929 /* subplans should all return the same thing */
930 coll = exprCollation((Node *) linitial(asplan->subplans));
931 }
932 break;
933 case T_FieldSelect:
934 coll = ((const FieldSelect *) expr)->resultcollid;
935 break;
936 case T_FieldStore:
937 /* FieldStore's result is composite ... */
938 coll = InvalidOid; /* ... so it has no collation */
939 break;
940 case T_RelabelType:
941 coll = ((const RelabelType *) expr)->resultcollid;
942 break;
943 case T_CoerceViaIO:
944 coll = ((const CoerceViaIO *) expr)->resultcollid;
945 break;
947 coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
948 break;
950 /* ConvertRowtypeExpr's result is composite ... */
951 coll = InvalidOid; /* ... so it has no collation */
952 break;
953 case T_CollateExpr:
954 coll = ((const CollateExpr *) expr)->collOid;
955 break;
956 case T_CaseExpr:
957 coll = ((const CaseExpr *) expr)->casecollid;
958 break;
959 case T_CaseTestExpr:
960 coll = ((const CaseTestExpr *) expr)->collation;
961 break;
962 case T_ArrayExpr:
963 coll = ((const ArrayExpr *) expr)->array_collid;
964 break;
965 case T_RowExpr:
966 /* RowExpr's result is composite ... */
967 coll = InvalidOid; /* ... so it has no collation */
968 break;
969 case T_RowCompareExpr:
970 /* RowCompareExpr's result is boolean ... */
971 coll = InvalidOid; /* ... so it has no collation */
972 break;
973 case T_CoalesceExpr:
974 coll = ((const CoalesceExpr *) expr)->coalescecollid;
975 break;
976 case T_MinMaxExpr:
977 coll = ((const MinMaxExpr *) expr)->minmaxcollid;
978 break;
980 /* Returns either NAME or a non-collatable type */
981 if (((const SQLValueFunction *) expr)->type == NAMEOID)
983 else
985 break;
986 case T_XmlExpr:
987
988 /*
989 * XMLSERIALIZE returns text from non-collatable inputs, so its
990 * collation is always default. The other cases return boolean or
991 * XML, which are non-collatable.
992 */
993 if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
995 else
997 break;
998 case T_JsonValueExpr:
999 coll = exprCollation((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
1000 break;
1002 {
1003 const JsonConstructorExpr *ctor = (const JsonConstructorExpr *) expr;
1004
1005 if (ctor->coercion)
1006 coll = exprCollation((Node *) ctor->coercion);
1007 else
1008 coll = InvalidOid;
1009 }
1010 break;
1011 case T_JsonIsPredicate:
1012 /* IS JSON's result is boolean ... */
1013 coll = InvalidOid; /* ... so it has no collation */
1014 break;
1015 case T_JsonExpr:
1016 {
1017 const JsonExpr *jsexpr = (const JsonExpr *) expr;
1018
1019 coll = jsexpr->collation;
1020 }
1021 break;
1022 case T_JsonBehavior:
1023 {
1024 const JsonBehavior *behavior = (const JsonBehavior *) expr;
1025
1026 if (behavior->expr)
1027 coll = exprCollation(behavior->expr);
1028 else
1029 coll = InvalidOid;
1030 }
1031 break;
1032 case T_NullTest:
1033 /* NullTest's result is boolean ... */
1034 coll = InvalidOid; /* ... so it has no collation */
1035 break;
1036 case T_BooleanTest:
1037 /* BooleanTest's result is boolean ... */
1038 coll = InvalidOid; /* ... so it has no collation */
1039 break;
1040 case T_CoerceToDomain:
1041 coll = ((const CoerceToDomain *) expr)->resultcollid;
1042 break;
1044 coll = ((const CoerceToDomainValue *) expr)->collation;
1045 break;
1046 case T_SetToDefault:
1047 coll = ((const SetToDefault *) expr)->collation;
1048 break;
1049 case T_CurrentOfExpr:
1050 /* CurrentOfExpr's result is boolean ... */
1051 coll = InvalidOid; /* ... so it has no collation */
1052 break;
1053 case T_NextValueExpr:
1054 /* NextValueExpr's result is an integer type ... */
1055 coll = InvalidOid; /* ... so it has no collation */
1056 break;
1057 case T_InferenceElem:
1058 coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
1059 break;
1060 case T_ReturningExpr:
1061 coll = exprCollation((Node *) ((const ReturningExpr *) expr)->retexpr);
1062 break;
1063 case T_PlaceHolderVar:
1064 coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1065 break;
1066 case T_GraphPropertyRef:
1067 coll = ((const GraphPropertyRef *) expr)->collation;
1068 break;
1069 default:
1070 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
1071 coll = InvalidOid; /* keep compiler quiet */
1072 break;
1073 }
1074 return coll;
1075}
1076
1077/*
1078 * exprInputCollation -
1079 * returns the Oid of the collation a function should use, if available.
1080 *
1081 * Result is InvalidOid if the node type doesn't store this information.
1082 */
1083Oid
1085{
1086 Oid coll;
1087
1088 if (!expr)
1089 return InvalidOid;
1090
1091 switch (nodeTag(expr))
1092 {
1093 case T_Aggref:
1094 coll = ((const Aggref *) expr)->inputcollid;
1095 break;
1096 case T_WindowFunc:
1097 coll = ((const WindowFunc *) expr)->inputcollid;
1098 break;
1099 case T_FuncExpr:
1100 coll = ((const FuncExpr *) expr)->inputcollid;
1101 break;
1102 case T_OpExpr:
1103 coll = ((const OpExpr *) expr)->inputcollid;
1104 break;
1105 case T_DistinctExpr:
1106 coll = ((const DistinctExpr *) expr)->inputcollid;
1107 break;
1108 case T_NullIfExpr:
1109 coll = ((const NullIfExpr *) expr)->inputcollid;
1110 break;
1112 coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
1113 break;
1114 case T_MinMaxExpr:
1115 coll = ((const MinMaxExpr *) expr)->inputcollid;
1116 break;
1117 default:
1118 coll = InvalidOid;
1119 break;
1120 }
1121 return coll;
1122}
1123
1124/*
1125 * exprSetCollation -
1126 * Assign collation information to an expression tree node.
1127 *
1128 * Note: since this is only used during parse analysis, we don't need to
1129 * worry about subplans, PlaceHolderVars, or ReturningExprs.
1130 */
1131void
1132exprSetCollation(Node *expr, Oid collation)
1133{
1134 switch (nodeTag(expr))
1135 {
1136 case T_Var:
1137 ((Var *) expr)->varcollid = collation;
1138 break;
1139 case T_Const:
1140 ((Const *) expr)->constcollid = collation;
1141 break;
1142 case T_Param:
1143 ((Param *) expr)->paramcollid = collation;
1144 break;
1145 case T_Aggref:
1146 ((Aggref *) expr)->aggcollid = collation;
1147 break;
1148 case T_GroupingFunc:
1149 Assert(!OidIsValid(collation));
1150 break;
1151 case T_WindowFunc:
1152 ((WindowFunc *) expr)->wincollid = collation;
1153 break;
1154 case T_MergeSupportFunc:
1155 ((MergeSupportFunc *) expr)->msfcollid = collation;
1156 break;
1157 case T_SubscriptingRef:
1158 ((SubscriptingRef *) expr)->refcollid = collation;
1159 break;
1160 case T_FuncExpr:
1161 ((FuncExpr *) expr)->funccollid = collation;
1162 break;
1163 case T_NamedArgExpr:
1164 Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
1165 break;
1166 case T_OpExpr:
1167 ((OpExpr *) expr)->opcollid = collation;
1168 break;
1169 case T_DistinctExpr:
1170 ((DistinctExpr *) expr)->opcollid = collation;
1171 break;
1172 case T_NullIfExpr:
1173 ((NullIfExpr *) expr)->opcollid = collation;
1174 break;
1176 /* ScalarArrayOpExpr's result is boolean ... */
1177 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1178 break;
1179 case T_BoolExpr:
1180 /* BoolExpr's result is boolean ... */
1181 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1182 break;
1183 case T_SubLink:
1184#ifdef USE_ASSERT_CHECKING
1185 {
1186 SubLink *sublink = (SubLink *) expr;
1187
1188 if (sublink->subLinkType == EXPR_SUBLINK ||
1189 sublink->subLinkType == ARRAY_SUBLINK)
1190 {
1191 /* get the collation of subselect's first target column */
1192 Query *qtree = (Query *) sublink->subselect;
1194
1195 if (!qtree || !IsA(qtree, Query))
1196 elog(ERROR, "cannot set collation for untransformed sublink");
1197 tent = linitial_node(TargetEntry, qtree->targetList);
1198 Assert(!tent->resjunk);
1199 Assert(collation == exprCollation((Node *) tent->expr));
1200 }
1201 else
1202 {
1203 /* otherwise, result is RECORD or BOOLEAN */
1204 Assert(!OidIsValid(collation));
1205 }
1206 }
1207#endif /* USE_ASSERT_CHECKING */
1208 break;
1209 case T_FieldSelect:
1210 ((FieldSelect *) expr)->resultcollid = collation;
1211 break;
1212 case T_FieldStore:
1213 /* FieldStore's result is composite ... */
1214 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1215 break;
1216 case T_RelabelType:
1217 ((RelabelType *) expr)->resultcollid = collation;
1218 break;
1219 case T_CoerceViaIO:
1220 ((CoerceViaIO *) expr)->resultcollid = collation;
1221 break;
1222 case T_ArrayCoerceExpr:
1223 ((ArrayCoerceExpr *) expr)->resultcollid = collation;
1224 break;
1226 /* ConvertRowtypeExpr's result is composite ... */
1227 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1228 break;
1229 case T_CaseExpr:
1230 ((CaseExpr *) expr)->casecollid = collation;
1231 break;
1232 case T_ArrayExpr:
1233 ((ArrayExpr *) expr)->array_collid = collation;
1234 break;
1235 case T_RowExpr:
1236 /* RowExpr's result is composite ... */
1237 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1238 break;
1239 case T_RowCompareExpr:
1240 /* RowCompareExpr's result is boolean ... */
1241 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1242 break;
1243 case T_CoalesceExpr:
1244 ((CoalesceExpr *) expr)->coalescecollid = collation;
1245 break;
1246 case T_MinMaxExpr:
1247 ((MinMaxExpr *) expr)->minmaxcollid = collation;
1248 break;
1249 case T_SQLValueFunction:
1250 Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
1251 (collation == C_COLLATION_OID) :
1252 (collation == InvalidOid));
1253 break;
1254 case T_XmlExpr:
1255 Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
1256 (collation == DEFAULT_COLLATION_OID) :
1257 (collation == InvalidOid));
1258 break;
1259 case T_JsonValueExpr:
1260 exprSetCollation((Node *) ((JsonValueExpr *) expr)->formatted_expr,
1261 collation);
1262 break;
1264 {
1266
1267 if (ctor->coercion)
1268 exprSetCollation((Node *) ctor->coercion, collation);
1269 else
1270 Assert(!OidIsValid(collation)); /* result is always a
1271 * json[b] type */
1272 }
1273 break;
1274 case T_JsonIsPredicate:
1275 Assert(!OidIsValid(collation)); /* result is always boolean */
1276 break;
1277 case T_JsonExpr:
1278 {
1279 JsonExpr *jexpr = (JsonExpr *) expr;
1280
1281 jexpr->collation = collation;
1282 }
1283 break;
1284 case T_JsonBehavior:
1285 Assert(((JsonBehavior *) expr)->expr == NULL ||
1286 exprCollation(((JsonBehavior *) expr)->expr) == collation);
1287 break;
1288 case T_NullTest:
1289 /* NullTest's result is boolean ... */
1290 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1291 break;
1292 case T_BooleanTest:
1293 /* BooleanTest's result is boolean ... */
1294 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1295 break;
1296 case T_CoerceToDomain:
1297 ((CoerceToDomain *) expr)->resultcollid = collation;
1298 break;
1300 ((CoerceToDomainValue *) expr)->collation = collation;
1301 break;
1302 case T_SetToDefault:
1303 ((SetToDefault *) expr)->collation = collation;
1304 break;
1305 case T_CurrentOfExpr:
1306 /* CurrentOfExpr's result is boolean ... */
1307 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1308 break;
1309 case T_NextValueExpr:
1310 /* NextValueExpr's result is an integer type ... */
1311 Assert(!OidIsValid(collation)); /* ... so never set a collation */
1312 break;
1313 default:
1314 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
1315 break;
1316 }
1317}
1318
1319/*
1320 * exprSetInputCollation -
1321 * Assign input-collation information to an expression tree node.
1322 *
1323 * This is a no-op for node types that don't store their input collation.
1324 * Note we omit RowCompareExpr, which needs special treatment since it
1325 * contains multiple input collation OIDs.
1326 */
1327void
1329{
1330 switch (nodeTag(expr))
1331 {
1332 case T_Aggref:
1333 ((Aggref *) expr)->inputcollid = inputcollation;
1334 break;
1335 case T_WindowFunc:
1336 ((WindowFunc *) expr)->inputcollid = inputcollation;
1337 break;
1338 case T_FuncExpr:
1339 ((FuncExpr *) expr)->inputcollid = inputcollation;
1340 break;
1341 case T_OpExpr:
1342 ((OpExpr *) expr)->inputcollid = inputcollation;
1343 break;
1344 case T_DistinctExpr:
1345 ((DistinctExpr *) expr)->inputcollid = inputcollation;
1346 break;
1347 case T_NullIfExpr:
1348 ((NullIfExpr *) expr)->inputcollid = inputcollation;
1349 break;
1351 ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
1352 break;
1353 case T_MinMaxExpr:
1354 ((MinMaxExpr *) expr)->inputcollid = inputcollation;
1355 break;
1356 default:
1357 break;
1358 }
1359}
1360
1361
1362/*
1363 * exprLocation -
1364 * returns the parse location of an expression tree, for error reports
1365 *
1366 * -1 is returned if the location can't be determined.
1367 *
1368 * For expressions larger than a single token, the intent here is to
1369 * return the location of the expression's leftmost token, not necessarily
1370 * the topmost Node's location field. For example, an OpExpr's location
1371 * field will point at the operator name, but if it is not a prefix operator
1372 * then we should return the location of the left-hand operand instead.
1373 * The reason is that we want to reference the entire expression not just
1374 * that operator, and pointing to its start seems to be the most natural way.
1375 *
1376 * The location is not perfect --- for example, since the grammar doesn't
1377 * explicitly represent parentheses in the parsetree, given something that
1378 * had been written "(a + b) * c" we are going to point at "a" not "(".
1379 * But it should be plenty good enough for error reporting purposes.
1380 *
1381 * You might think that this code is overly general, for instance why check
1382 * the operands of a FuncExpr node, when the function name can be expected
1383 * to be to the left of them? There are a couple of reasons. The grammar
1384 * sometimes builds expressions that aren't quite what the user wrote;
1385 * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
1386 * pointer is to the right of its leftmost argument. Also, nodes that were
1387 * inserted implicitly by parse analysis (such as FuncExprs for implicit
1388 * coercions) will have location -1, and so we can have odd combinations of
1389 * known and unknown locations in a tree.
1390 */
1391int
1392exprLocation(const Node *expr)
1393{
1394 int loc;
1395
1396 if (expr == NULL)
1397 return -1;
1398 switch (nodeTag(expr))
1399 {
1400 case T_RangeVar:
1401 loc = ((const RangeVar *) expr)->location;
1402 break;
1403 case T_TableFunc:
1404 loc = ((const TableFunc *) expr)->location;
1405 break;
1406 case T_Var:
1407 loc = ((const Var *) expr)->location;
1408 break;
1409 case T_Const:
1410 loc = ((const Const *) expr)->location;
1411 break;
1412 case T_Param:
1413 loc = ((const Param *) expr)->location;
1414 break;
1415 case T_Aggref:
1416 /* function name should always be the first thing */
1417 loc = ((const Aggref *) expr)->location;
1418 break;
1419 case T_GroupingFunc:
1420 loc = ((const GroupingFunc *) expr)->location;
1421 break;
1422 case T_WindowFunc:
1423 /* function name should always be the first thing */
1424 loc = ((const WindowFunc *) expr)->location;
1425 break;
1426 case T_MergeSupportFunc:
1427 loc = ((const MergeSupportFunc *) expr)->location;
1428 break;
1429 case T_SubscriptingRef:
1430 /* just use container argument's location */
1431 loc = exprLocation((Node *) ((const SubscriptingRef *) expr)->refexpr);
1432 break;
1433 case T_FuncExpr:
1434 {
1435 const FuncExpr *fexpr = (const FuncExpr *) expr;
1436
1437 /* consider both function name and leftmost arg */
1438 loc = leftmostLoc(fexpr->location,
1439 exprLocation((Node *) fexpr->args));
1440 }
1441 break;
1442 case T_NamedArgExpr:
1443 {
1444 const NamedArgExpr *na = (const NamedArgExpr *) expr;
1445
1446 /* consider both argument name and value */
1447 loc = leftmostLoc(na->location,
1448 exprLocation((Node *) na->arg));
1449 }
1450 break;
1451 case T_OpExpr:
1452 case T_DistinctExpr: /* struct-equivalent to OpExpr */
1453 case T_NullIfExpr: /* struct-equivalent to OpExpr */
1454 {
1455 const OpExpr *opexpr = (const OpExpr *) expr;
1456
1457 /* consider both operator name and leftmost arg */
1458 loc = leftmostLoc(opexpr->location,
1459 exprLocation((Node *) opexpr->args));
1460 }
1461 break;
1463 {
1464 const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
1465
1466 /* consider both operator name and leftmost arg */
1467 loc = leftmostLoc(saopexpr->location,
1468 exprLocation((Node *) saopexpr->args));
1469 }
1470 break;
1471 case T_BoolExpr:
1472 {
1473 const BoolExpr *bexpr = (const BoolExpr *) expr;
1474
1475 /*
1476 * Same as above, to handle either NOT or AND/OR. We can't
1477 * special-case NOT because of the way that it's used for
1478 * things like IS NOT BETWEEN.
1479 */
1480 loc = leftmostLoc(bexpr->location,
1481 exprLocation((Node *) bexpr->args));
1482 }
1483 break;
1484 case T_SubLink:
1485 {
1486 const SubLink *sublink = (const SubLink *) expr;
1487
1488 /* check the testexpr, if any, and the operator/keyword */
1489 loc = leftmostLoc(exprLocation(sublink->testexpr),
1490 sublink->location);
1491 }
1492 break;
1493 case T_FieldSelect:
1494 /* just use argument's location */
1495 loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
1496 break;
1497 case T_FieldStore:
1498 /* just use argument's location */
1499 loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
1500 break;
1501 case T_RelabelType:
1502 {
1503 const RelabelType *rexpr = (const RelabelType *) expr;
1504
1505 /* Much as above */
1506 loc = leftmostLoc(rexpr->location,
1507 exprLocation((Node *) rexpr->arg));
1508 }
1509 break;
1510 case T_CoerceViaIO:
1511 {
1512 const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
1513
1514 /* Much as above */
1515 loc = leftmostLoc(cexpr->location,
1516 exprLocation((Node *) cexpr->arg));
1517 }
1518 break;
1519 case T_ArrayCoerceExpr:
1520 {
1521 const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
1522
1523 /* Much as above */
1524 loc = leftmostLoc(cexpr->location,
1525 exprLocation((Node *) cexpr->arg));
1526 }
1527 break;
1529 {
1530 const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
1531
1532 /* Much as above */
1533 loc = leftmostLoc(cexpr->location,
1534 exprLocation((Node *) cexpr->arg));
1535 }
1536 break;
1537 case T_CollateExpr:
1538 /* just use argument's location */
1539 loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
1540 break;
1541 case T_CaseExpr:
1542 /* CASE keyword should always be the first thing */
1543 loc = ((const CaseExpr *) expr)->location;
1544 break;
1545 case T_CaseWhen:
1546 /* WHEN keyword should always be the first thing */
1547 loc = ((const CaseWhen *) expr)->location;
1548 break;
1549 case T_ArrayExpr:
1550 /* the location points at ARRAY or [, which must be leftmost */
1551 loc = ((const ArrayExpr *) expr)->location;
1552 break;
1553 case T_RowExpr:
1554 /* the location points at ROW or (, which must be leftmost */
1555 loc = ((const RowExpr *) expr)->location;
1556 break;
1557 case T_RowCompareExpr:
1558 /* just use leftmost argument's location */
1559 loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
1560 break;
1561 case T_CoalesceExpr:
1562 /* COALESCE keyword should always be the first thing */
1563 loc = ((const CoalesceExpr *) expr)->location;
1564 break;
1565 case T_MinMaxExpr:
1566 /* GREATEST/LEAST keyword should always be the first thing */
1567 loc = ((const MinMaxExpr *) expr)->location;
1568 break;
1569 case T_SQLValueFunction:
1570 /* function keyword should always be the first thing */
1571 loc = ((const SQLValueFunction *) expr)->location;
1572 break;
1573 case T_XmlExpr:
1574 {
1575 const XmlExpr *xexpr = (const XmlExpr *) expr;
1576
1577 /* consider both function name and leftmost arg */
1578 loc = leftmostLoc(xexpr->location,
1579 exprLocation((Node *) xexpr->args));
1580 }
1581 break;
1582 case T_JsonFormat:
1583 loc = ((const JsonFormat *) expr)->location;
1584 break;
1585 case T_JsonValueExpr:
1586 loc = exprLocation((Node *) ((const JsonValueExpr *) expr)->raw_expr);
1587 break;
1589 loc = ((const JsonConstructorExpr *) expr)->location;
1590 break;
1591 case T_JsonIsPredicate:
1592 loc = ((const JsonIsPredicate *) expr)->location;
1593 break;
1594 case T_JsonExpr:
1595 {
1596 const JsonExpr *jsexpr = (const JsonExpr *) expr;
1597
1598 /* consider both function name and leftmost arg */
1599 loc = leftmostLoc(jsexpr->location,
1600 exprLocation(jsexpr->formatted_expr));
1601 }
1602 break;
1603 case T_JsonBehavior:
1604 loc = exprLocation(((const JsonBehavior *) expr)->expr);
1605 break;
1606 case T_NullTest:
1607 {
1608 const NullTest *nexpr = (const NullTest *) expr;
1609
1610 /* Much as above */
1611 loc = leftmostLoc(nexpr->location,
1612 exprLocation((Node *) nexpr->arg));
1613 }
1614 break;
1615 case T_BooleanTest:
1616 {
1617 const BooleanTest *bexpr = (const BooleanTest *) expr;
1618
1619 /* Much as above */
1620 loc = leftmostLoc(bexpr->location,
1621 exprLocation((Node *) bexpr->arg));
1622 }
1623 break;
1624 case T_CoerceToDomain:
1625 {
1626 const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
1627
1628 /* Much as above */
1629 loc = leftmostLoc(cexpr->location,
1630 exprLocation((Node *) cexpr->arg));
1631 }
1632 break;
1634 loc = ((const CoerceToDomainValue *) expr)->location;
1635 break;
1636 case T_SetToDefault:
1637 loc = ((const SetToDefault *) expr)->location;
1638 break;
1639 case T_ReturningExpr:
1640 loc = exprLocation((Node *) ((const ReturningExpr *) expr)->retexpr);
1641 break;
1642 case T_TargetEntry:
1643 /* just use argument's location */
1644 loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
1645 break;
1646 case T_IntoClause:
1647 /* use the contained RangeVar's location --- close enough */
1648 loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
1649 break;
1650 case T_List:
1651 {
1652 /* report location of first list member that has a location */
1653 ListCell *lc;
1654
1655 loc = -1; /* just to suppress compiler warning */
1656 foreach(lc, (const List *) expr)
1657 {
1658 loc = exprLocation((Node *) lfirst(lc));
1659 if (loc >= 0)
1660 break;
1661 }
1662 }
1663 break;
1664 case T_A_Expr:
1665 {
1666 const A_Expr *aexpr = (const A_Expr *) expr;
1667
1668 /* use leftmost of operator or left operand (if any) */
1669 /* we assume right operand can't be to left of operator */
1670 loc = leftmostLoc(aexpr->location,
1671 exprLocation(aexpr->lexpr));
1672 }
1673 break;
1674 case T_ColumnRef:
1675 loc = ((const ColumnRef *) expr)->location;
1676 break;
1677 case T_ParamRef:
1678 loc = ((const ParamRef *) expr)->location;
1679 break;
1680 case T_A_Const:
1681 loc = ((const A_Const *) expr)->location;
1682 break;
1683 case T_FuncCall:
1684 {
1685 const FuncCall *fc = (const FuncCall *) expr;
1686
1687 /* consider both function name and leftmost arg */
1688 /* (we assume any ORDER BY nodes must be to right of name) */
1689 loc = leftmostLoc(fc->location,
1690 exprLocation((Node *) fc->args));
1691 }
1692 break;
1693 case T_A_ArrayExpr:
1694 /* the location points at ARRAY or [, which must be leftmost */
1695 loc = ((const A_ArrayExpr *) expr)->location;
1696 break;
1697 case T_ResTarget:
1698 /* we need not examine the contained expression (if any) */
1699 loc = ((const ResTarget *) expr)->location;
1700 break;
1701 case T_MultiAssignRef:
1702 loc = exprLocation(((const MultiAssignRef *) expr)->source);
1703 break;
1704 case T_TypeCast:
1705 {
1706 const TypeCast *tc = (const TypeCast *) expr;
1707
1708 /*
1709 * This could represent CAST(), ::, or TypeName 'literal', so
1710 * any of the components might be leftmost.
1711 */
1712 loc = exprLocation(tc->arg);
1713 loc = leftmostLoc(loc, tc->typeName->location);
1714 loc = leftmostLoc(loc, tc->location);
1715 }
1716 break;
1717 case T_CollateClause:
1718 /* just use argument's location */
1719 loc = exprLocation(((const CollateClause *) expr)->arg);
1720 break;
1721 case T_SortBy:
1722 /* just use argument's location (ignore operator, if any) */
1723 loc = exprLocation(((const SortBy *) expr)->node);
1724 break;
1725 case T_WindowDef:
1726 loc = ((const WindowDef *) expr)->location;
1727 break;
1728 case T_RangeTableSample:
1729 loc = ((const RangeTableSample *) expr)->location;
1730 break;
1731 case T_TypeName:
1732 loc = ((const TypeName *) expr)->location;
1733 break;
1734 case T_ColumnDef:
1735 loc = ((const ColumnDef *) expr)->location;
1736 break;
1737 case T_IndexElem:
1738 loc = ((const IndexElem *) expr)->location;
1739 break;
1740 case T_Constraint:
1741 loc = ((const Constraint *) expr)->location;
1742 break;
1744 loc = ((const FunctionParameter *) expr)->location;
1745 break;
1746 case T_XmlSerialize:
1747 /* XMLSERIALIZE keyword should always be the first thing */
1748 loc = ((const XmlSerialize *) expr)->location;
1749 break;
1750 case T_GroupingSet:
1751 loc = ((const GroupingSet *) expr)->location;
1752 break;
1753 case T_WithClause:
1754 loc = ((const WithClause *) expr)->location;
1755 break;
1756 case T_InferClause:
1757 loc = ((const InferClause *) expr)->location;
1758 break;
1759 case T_OnConflictClause:
1760 loc = ((const OnConflictClause *) expr)->location;
1761 break;
1762 case T_CTESearchClause:
1763 loc = ((const CTESearchClause *) expr)->location;
1764 break;
1765 case T_CTECycleClause:
1766 loc = ((const CTECycleClause *) expr)->location;
1767 break;
1768 case T_CommonTableExpr:
1769 loc = ((const CommonTableExpr *) expr)->location;
1770 break;
1771 case T_JsonKeyValue:
1772 /* just use the key's location */
1773 loc = exprLocation((Node *) ((const JsonKeyValue *) expr)->key);
1774 break;
1776 loc = ((const JsonObjectConstructor *) expr)->location;
1777 break;
1779 loc = ((const JsonArrayConstructor *) expr)->location;
1780 break;
1782 loc = ((const JsonArrayQueryConstructor *) expr)->location;
1783 break;
1785 loc = ((const JsonAggConstructor *) expr)->location;
1786 break;
1787 case T_JsonObjectAgg:
1788 loc = exprLocation((Node *) ((const JsonObjectAgg *) expr)->constructor);
1789 break;
1790 case T_JsonArrayAgg:
1791 loc = exprLocation((Node *) ((const JsonArrayAgg *) expr)->constructor);
1792 break;
1793 case T_PlaceHolderVar:
1794 /* just use argument's location */
1795 loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1796 break;
1797 case T_InferenceElem:
1798 /* just use nested expr's location */
1799 loc = exprLocation((Node *) ((const InferenceElem *) expr)->expr);
1800 break;
1801 case T_PartitionElem:
1802 loc = ((const PartitionElem *) expr)->location;
1803 break;
1804 case T_PartitionSpec:
1805 loc = ((const PartitionSpec *) expr)->location;
1806 break;
1808 loc = ((const PartitionBoundSpec *) expr)->location;
1809 break;
1811 loc = ((const PartitionRangeDatum *) expr)->location;
1812 break;
1813 default:
1814 /* for any other node type it's just unknown... */
1815 loc = -1;
1816 break;
1817 }
1818 return loc;
1819}
1820
1821/*
1822 * leftmostLoc - support for exprLocation
1823 *
1824 * Take the minimum of two parse location values, but ignore unknowns
1825 */
1826static int
1828{
1829 if (loc1 < 0)
1830 return loc2;
1831 else if (loc2 < 0)
1832 return loc1;
1833 else
1834 return Min(loc1, loc2);
1835}
1836
1837
1838/*
1839 * fix_opfuncids
1840 * Calculate opfuncid field from opno for each OpExpr node in given tree.
1841 * The given tree can be anything expression_tree_walker handles.
1842 *
1843 * The argument is modified in-place. (This is OK since we'd want the
1844 * same change for any node, even if it gets visited more than once due to
1845 * shared structure.)
1846 */
1847void
1849{
1850 /* This tree walk requires no special setup, so away we go... */
1852}
1853
1854static bool
1855fix_opfuncids_walker(Node *node, void *context)
1856{
1857 if (node == NULL)
1858 return false;
1859 if (IsA(node, OpExpr))
1860 set_opfuncid((OpExpr *) node);
1861 else if (IsA(node, DistinctExpr))
1862 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1863 else if (IsA(node, NullIfExpr))
1864 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1865 else if (IsA(node, ScalarArrayOpExpr))
1867 return expression_tree_walker(node, fix_opfuncids_walker, context);
1868}
1869
1870/*
1871 * set_opfuncid
1872 * Set the opfuncid (procedure OID) in an OpExpr node,
1873 * if it hasn't been set already.
1874 *
1875 * Because of struct equivalence, this can also be used for
1876 * DistinctExpr and NullIfExpr nodes.
1877 */
1878void
1880{
1881 if (opexpr->opfuncid == InvalidOid)
1882 opexpr->opfuncid = get_opcode(opexpr->opno);
1883}
1884
1885/*
1886 * set_sa_opfuncid
1887 * As above, for ScalarArrayOpExpr nodes.
1888 */
1889void
1891{
1892 if (opexpr->opfuncid == InvalidOid)
1893 opexpr->opfuncid = get_opcode(opexpr->opno);
1894}
1895
1896
1897/*
1898 * check_functions_in_node -
1899 * apply checker() to each function OID contained in given expression node
1900 *
1901 * Returns true if the checker() function does; for nodes representing more
1902 * than one function call, returns true if the checker() function does so
1903 * for any of those functions. Returns false if node does not invoke any
1904 * SQL-visible function. Caller must not pass node == NULL.
1905 *
1906 * This function examines only the given node; it does not recurse into any
1907 * sub-expressions. Callers typically prefer to keep control of the recursion
1908 * for themselves, in case additional checks should be made, or because they
1909 * have special rules about which parts of the tree need to be visited.
1910 *
1911 * Note: we ignore MinMaxExpr, SQLValueFunction, XmlExpr, CoerceToDomain,
1912 * and NextValueExpr nodes, because they do not contain SQL function OIDs.
1913 * However, they can invoke SQL-visible functions, so callers should take
1914 * thought about how to treat them.
1915 */
1916bool
1918 void *context)
1919{
1920 switch (nodeTag(node))
1921 {
1922 case T_Aggref:
1923 {
1924 Aggref *expr = (Aggref *) node;
1925
1926 if (checker(expr->aggfnoid, context))
1927 return true;
1928 }
1929 break;
1930 case T_WindowFunc:
1931 {
1932 WindowFunc *expr = (WindowFunc *) node;
1933
1934 if (checker(expr->winfnoid, context))
1935 return true;
1936 }
1937 break;
1938 case T_FuncExpr:
1939 {
1940 FuncExpr *expr = (FuncExpr *) node;
1941
1942 if (checker(expr->funcid, context))
1943 return true;
1944 }
1945 break;
1946 case T_OpExpr:
1947 case T_DistinctExpr: /* struct-equivalent to OpExpr */
1948 case T_NullIfExpr: /* struct-equivalent to OpExpr */
1949 {
1950 OpExpr *expr = (OpExpr *) node;
1951
1952 /* Set opfuncid if it wasn't set already */
1953 set_opfuncid(expr);
1954 if (checker(expr->opfuncid, context))
1955 return true;
1956 }
1957 break;
1959 {
1960 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1961
1962 set_sa_opfuncid(expr);
1963 if (checker(expr->opfuncid, context))
1964 return true;
1965 }
1966 break;
1967 case T_CoerceViaIO:
1968 {
1969 CoerceViaIO *expr = (CoerceViaIO *) node;
1970 Oid iofunc;
1971 Oid typioparam;
1972 bool typisvarlena;
1973
1974 /* check the result type's input function */
1976 &iofunc, &typioparam);
1977 if (checker(iofunc, context))
1978 return true;
1979 /* check the input type's output function */
1981 &iofunc, &typisvarlena);
1982 if (checker(iofunc, context))
1983 return true;
1984 }
1985 break;
1986 case T_RowCompareExpr:
1987 {
1989 ListCell *opid;
1990
1991 foreach(opid, rcexpr->opnos)
1992 {
1994
1995 if (checker(opfuncid, context))
1996 return true;
1997 }
1998 }
1999 break;
2000 default:
2001 break;
2002 }
2003 return false;
2004}
2005
2006
2007/*
2008 * Standard expression-tree walking support
2009 *
2010 * We used to have near-duplicate code in many different routines that
2011 * understood how to recurse through an expression node tree. That was
2012 * a pain to maintain, and we frequently had bugs due to some particular
2013 * routine neglecting to support a particular node type. In most cases,
2014 * these routines only actually care about certain node types, and don't
2015 * care about other types except insofar as they have to recurse through
2016 * non-primitive node types. Therefore, we now provide generic tree-walking
2017 * logic to consolidate the redundant "boilerplate" code. There are
2018 * two versions: expression_tree_walker() and expression_tree_mutator().
2019 */
2020
2021/*
2022 * expression_tree_walker() is designed to support routines that traverse
2023 * a tree in a read-only fashion (although it will also work for routines
2024 * that modify nodes in-place but never add/delete/replace nodes).
2025 * A walker routine should look like this:
2026 *
2027 * bool my_walker (Node *node, my_struct *context)
2028 * {
2029 * if (node == NULL)
2030 * return false;
2031 * // check for nodes that special work is required for, eg:
2032 * if (IsA(node, Var))
2033 * {
2034 * ... do special actions for Var nodes
2035 * }
2036 * else if (IsA(node, ...))
2037 * {
2038 * ... do special actions for other node types
2039 * }
2040 * // for any node type not specially processed, do:
2041 * return expression_tree_walker(node, my_walker, context);
2042 * }
2043 *
2044 * The "context" argument points to a struct that holds whatever context
2045 * information the walker routine needs --- it can be used to return data
2046 * gathered by the walker, too. This argument is not touched by
2047 * expression_tree_walker, but it is passed down to recursive sub-invocations
2048 * of my_walker. The tree walk is started from a setup routine that
2049 * fills in the appropriate context struct, calls my_walker with the top-level
2050 * node of the tree, and then examines the results.
2051 *
2052 * The walker routine should return "false" to continue the tree walk, or
2053 * "true" to abort the walk and immediately return "true" to the top-level
2054 * caller. This can be used to short-circuit the traversal if the walker
2055 * has found what it came for. "false" is returned to the top-level caller
2056 * iff no invocation of the walker returned "true".
2057 *
2058 * The node types handled by expression_tree_walker include all those
2059 * normally found in target lists and qualifier clauses during the planning
2060 * stage. In particular, it handles List nodes since a cnf-ified qual clause
2061 * will have List structure at the top level, and it handles TargetEntry nodes
2062 * so that a scan of a target list can be handled without additional code.
2063 * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
2064 * handled, so that query jointrees and setOperation trees can be processed
2065 * without additional code.
2066 *
2067 * expression_tree_walker will handle SubLink nodes by recursing normally
2068 * into the "testexpr" subtree (which is an expression belonging to the outer
2069 * plan). It will also call the walker on the sub-Query node; however, when
2070 * expression_tree_walker itself is called on a Query node, it does nothing
2071 * and returns "false". The net effect is that unless the walker does
2072 * something special at a Query node, sub-selects will not be visited during
2073 * an expression tree walk. This is exactly the behavior wanted in many cases
2074 * --- and for those walkers that do want to recurse into sub-selects, special
2075 * behavior is typically needed anyway at the entry to a sub-select (such as
2076 * incrementing a depth counter). A walker that wants to examine sub-selects
2077 * should include code along the lines of:
2078 *
2079 * if (IsA(node, Query))
2080 * {
2081 * adjust context for subquery;
2082 * result = query_tree_walker((Query *) node, my_walker, context,
2083 * 0); // adjust flags as needed
2084 * restore context if needed;
2085 * return result;
2086 * }
2087 *
2088 * query_tree_walker is a convenience routine (see below) that calls the
2089 * walker on all the expression subtrees of the given Query node.
2090 *
2091 * expression_tree_walker will handle SubPlan nodes by recursing normally
2092 * into the "testexpr" and the "args" list (which are expressions belonging to
2093 * the outer plan). It will not touch the completed subplan, however. Since
2094 * there is no link to the original Query, it is not possible to recurse into
2095 * subselects of an already-planned expression tree. This is OK for current
2096 * uses, but may need to be revisited in future.
2097 */
2098
2099bool
2102 void *context)
2103{
2104 ListCell *temp;
2105
2106 /*
2107 * The walker has already visited the current node, and so we need only
2108 * recurse into any sub-nodes it has.
2109 *
2110 * We assume that the walker is not interested in List nodes per se, so
2111 * when we expect a List we just recurse directly to self without
2112 * bothering to call the walker.
2113 */
2114#define WALK(n) walker((Node *) (n), context)
2115
2116#define LIST_WALK(l) expression_tree_walker_impl((Node *) (l), walker, context)
2117
2118 if (node == NULL)
2119 return false;
2120
2121 /* Guard against stack overflow due to overly complex expressions */
2123
2124 switch (nodeTag(node))
2125 {
2126 case T_Var:
2127 case T_Const:
2128 case T_Param:
2129 case T_CaseTestExpr:
2130 case T_SQLValueFunction:
2132 case T_SetToDefault:
2133 case T_CurrentOfExpr:
2134 case T_NextValueExpr:
2135 case T_RangeTblRef:
2136 case T_SortGroupClause:
2137 case T_CTESearchClause:
2138 case T_GraphPropertyRef:
2139 case T_MergeSupportFunc:
2140 /* primitive node types with no expression subnodes */
2141 break;
2142 case T_WithCheckOption:
2143 return WALK(((WithCheckOption *) node)->qual);
2144 case T_Aggref:
2145 {
2146 Aggref *expr = (Aggref *) node;
2147
2148 /* recurse directly on Lists */
2149 if (LIST_WALK(expr->aggdirectargs))
2150 return true;
2151 if (LIST_WALK(expr->args))
2152 return true;
2153 if (LIST_WALK(expr->aggorder))
2154 return true;
2155 if (LIST_WALK(expr->aggdistinct))
2156 return true;
2157 if (WALK(expr->aggfilter))
2158 return true;
2159 }
2160 break;
2161 case T_GroupingFunc:
2162 {
2164
2165 if (LIST_WALK(grouping->args))
2166 return true;
2167 }
2168 break;
2169 case T_WindowFunc:
2170 {
2171 WindowFunc *expr = (WindowFunc *) node;
2172
2173 /* recurse directly on List */
2174 if (LIST_WALK(expr->args))
2175 return true;
2176 if (WALK(expr->aggfilter))
2177 return true;
2178 if (WALK(expr->runCondition))
2179 return true;
2180 }
2181 break;
2183 {
2185
2186 if (WALK(expr->arg))
2187 return true;
2188 }
2189 break;
2190 case T_SubscriptingRef:
2191 {
2192 SubscriptingRef *sbsref = (SubscriptingRef *) node;
2193
2194 /* recurse directly for upper/lower container index lists */
2195 if (LIST_WALK(sbsref->refupperindexpr))
2196 return true;
2197 if (LIST_WALK(sbsref->reflowerindexpr))
2198 return true;
2199 /* walker must see the refexpr and refassgnexpr, however */
2200 if (WALK(sbsref->refexpr))
2201 return true;
2202
2203 if (WALK(sbsref->refassgnexpr))
2204 return true;
2205 }
2206 break;
2207 case T_FuncExpr:
2208 {
2209 FuncExpr *expr = (FuncExpr *) node;
2210
2211 if (LIST_WALK(expr->args))
2212 return true;
2213 }
2214 break;
2215 case T_NamedArgExpr:
2216 return WALK(((NamedArgExpr *) node)->arg);
2217 case T_OpExpr:
2218 case T_DistinctExpr: /* struct-equivalent to OpExpr */
2219 case T_NullIfExpr: /* struct-equivalent to OpExpr */
2220 {
2221 OpExpr *expr = (OpExpr *) node;
2222
2223 if (LIST_WALK(expr->args))
2224 return true;
2225 }
2226 break;
2228 {
2229 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2230
2231 if (LIST_WALK(expr->args))
2232 return true;
2233 }
2234 break;
2235 case T_BoolExpr:
2236 {
2237 BoolExpr *expr = (BoolExpr *) node;
2238
2239 if (LIST_WALK(expr->args))
2240 return true;
2241 }
2242 break;
2243 case T_SubLink:
2244 {
2245 SubLink *sublink = (SubLink *) node;
2246
2247 if (WALK(sublink->testexpr))
2248 return true;
2249
2250 /*
2251 * Also invoke the walker on the sublink's Query node, so it
2252 * can recurse into the sub-query if it wants to.
2253 */
2254 return WALK(sublink->subselect);
2255 }
2256 break;
2257 case T_SubPlan:
2258 {
2259 SubPlan *subplan = (SubPlan *) node;
2260
2261 /* recurse into the testexpr, but not into the Plan */
2262 if (WALK(subplan->testexpr))
2263 return true;
2264 /* also examine args list */
2265 if (LIST_WALK(subplan->args))
2266 return true;
2267 }
2268 break;
2270 return LIST_WALK(((AlternativeSubPlan *) node)->subplans);
2271 case T_FieldSelect:
2272 return WALK(((FieldSelect *) node)->arg);
2273 case T_FieldStore:
2274 {
2275 FieldStore *fstore = (FieldStore *) node;
2276
2277 if (WALK(fstore->arg))
2278 return true;
2279 if (WALK(fstore->newvals))
2280 return true;
2281 }
2282 break;
2283 case T_RelabelType:
2284 return WALK(((RelabelType *) node)->arg);
2285 case T_CoerceViaIO:
2286 return WALK(((CoerceViaIO *) node)->arg);
2287 case T_ArrayCoerceExpr:
2288 {
2290
2291 if (WALK(acoerce->arg))
2292 return true;
2293 if (WALK(acoerce->elemexpr))
2294 return true;
2295 }
2296 break;
2298 return WALK(((ConvertRowtypeExpr *) node)->arg);
2299 case T_CollateExpr:
2300 return WALK(((CollateExpr *) node)->arg);
2301 case T_CaseExpr:
2302 {
2303 CaseExpr *caseexpr = (CaseExpr *) node;
2304
2305 if (WALK(caseexpr->arg))
2306 return true;
2307 /* we assume walker doesn't care about CaseWhens, either */
2308 foreach(temp, caseexpr->args)
2309 {
2311
2312 if (WALK(when->expr))
2313 return true;
2314 if (WALK(when->result))
2315 return true;
2316 }
2317 if (WALK(caseexpr->defresult))
2318 return true;
2319 }
2320 break;
2321 case T_ArrayExpr:
2322 return WALK(((ArrayExpr *) node)->elements);
2323 case T_RowExpr:
2324 /* Assume colnames isn't interesting */
2325 return WALK(((RowExpr *) node)->args);
2326 case T_RowCompareExpr:
2327 {
2329
2330 if (WALK(rcexpr->largs))
2331 return true;
2332 if (WALK(rcexpr->rargs))
2333 return true;
2334 }
2335 break;
2336 case T_CoalesceExpr:
2337 return WALK(((CoalesceExpr *) node)->args);
2338 case T_MinMaxExpr:
2339 return WALK(((MinMaxExpr *) node)->args);
2340 case T_XmlExpr:
2341 {
2342 XmlExpr *xexpr = (XmlExpr *) node;
2343
2344 if (WALK(xexpr->named_args))
2345 return true;
2346 /* we assume walker doesn't care about arg_names */
2347 if (WALK(xexpr->args))
2348 return true;
2349 }
2350 break;
2351 case T_JsonValueExpr:
2352 {
2353 JsonValueExpr *jve = (JsonValueExpr *) node;
2354
2355 if (WALK(jve->raw_expr))
2356 return true;
2357 if (WALK(jve->formatted_expr))
2358 return true;
2359 }
2360 break;
2362 {
2364
2365 if (WALK(ctor->args))
2366 return true;
2367 if (WALK(ctor->func))
2368 return true;
2369 if (WALK(ctor->coercion))
2370 return true;
2371 }
2372 break;
2373 case T_JsonIsPredicate:
2374 return WALK(((JsonIsPredicate *) node)->expr);
2375 case T_JsonExpr:
2376 {
2377 JsonExpr *jexpr = (JsonExpr *) node;
2378
2379 if (WALK(jexpr->formatted_expr))
2380 return true;
2381 if (WALK(jexpr->path_spec))
2382 return true;
2383 if (WALK(jexpr->passing_values))
2384 return true;
2385 /* we assume walker doesn't care about passing_names */
2386 if (WALK(jexpr->on_empty))
2387 return true;
2388 if (WALK(jexpr->on_error))
2389 return true;
2390 }
2391 break;
2392 case T_JsonBehavior:
2393 {
2394 JsonBehavior *behavior = (JsonBehavior *) node;
2395
2396 if (WALK(behavior->expr))
2397 return true;
2398 }
2399 break;
2400 case T_NullTest:
2401 return WALK(((NullTest *) node)->arg);
2402 case T_BooleanTest:
2403 return WALK(((BooleanTest *) node)->arg);
2404 case T_CoerceToDomain:
2405 return WALK(((CoerceToDomain *) node)->arg);
2406 case T_TargetEntry:
2407 return WALK(((TargetEntry *) node)->expr);
2408 case T_Query:
2409 /* Do nothing with a sub-Query, per discussion above */
2410 break;
2411 case T_WindowClause:
2412 {
2413 WindowClause *wc = (WindowClause *) node;
2414
2415 if (WALK(wc->partitionClause))
2416 return true;
2417 if (WALK(wc->orderClause))
2418 return true;
2419 if (WALK(wc->startOffset))
2420 return true;
2421 if (WALK(wc->endOffset))
2422 return true;
2423 }
2424 break;
2425 case T_CTECycleClause:
2426 {
2427 CTECycleClause *cc = (CTECycleClause *) node;
2428
2429 if (WALK(cc->cycle_mark_value))
2430 return true;
2431 if (WALK(cc->cycle_mark_default))
2432 return true;
2433 }
2434 break;
2435 case T_CommonTableExpr:
2436 {
2437 CommonTableExpr *cte = (CommonTableExpr *) node;
2438
2439 /*
2440 * Invoke the walker on the CTE's Query node, so it can
2441 * recurse into the sub-query if it wants to.
2442 */
2443 if (WALK(cte->ctequery))
2444 return true;
2445
2446 if (WALK(cte->search_clause))
2447 return true;
2448 if (WALK(cte->cycle_clause))
2449 return true;
2450 }
2451 break;
2452 case T_JsonKeyValue:
2453 {
2454 JsonKeyValue *kv = (JsonKeyValue *) node;
2455
2456 if (WALK(kv->key))
2457 return true;
2458 if (WALK(kv->value))
2459 return true;
2460 }
2461 break;
2463 {
2465
2466 if (LIST_WALK(ctor->exprs))
2467 return true;
2468 }
2469 break;
2471 {
2473
2474 if (LIST_WALK(ctor->exprs))
2475 return true;
2476 }
2477 break;
2479 {
2481
2482 if (WALK(ctor->query))
2483 return true;
2484 }
2485 break;
2487 {
2489
2490 if (WALK(ctor->agg_filter))
2491 return true;
2492 if (WALK(ctor->agg_order))
2493 return true;
2494 if (WALK(ctor->over))
2495 return true;
2496 }
2497 break;
2498 case T_JsonObjectAgg:
2499 {
2500 JsonObjectAgg *ctor = (JsonObjectAgg *) node;
2501
2502 if (WALK(ctor->constructor))
2503 return true;
2504 if (WALK(ctor->arg))
2505 return true;
2506 }
2507 break;
2508 case T_JsonArrayAgg:
2509 {
2510 JsonArrayAgg *ctor = (JsonArrayAgg *) node;
2511
2512 if (WALK(ctor->constructor))
2513 return true;
2514 if (WALK(ctor->arg))
2515 return true;
2516 }
2517 break;
2518
2520 {
2522
2523 if (WALK(pbs->listdatums))
2524 return true;
2525 if (WALK(pbs->lowerdatums))
2526 return true;
2527 if (WALK(pbs->upperdatums))
2528 return true;
2529 }
2530 break;
2532 {
2534
2535 if (WALK(prd->value))
2536 return true;
2537 }
2538 break;
2539 case T_List:
2540 foreach(temp, (List *) node)
2541 {
2542 if (WALK(lfirst(temp)))
2543 return true;
2544 }
2545 break;
2546 case T_FromExpr:
2547 {
2548 FromExpr *from = (FromExpr *) node;
2549
2550 if (LIST_WALK(from->fromlist))
2551 return true;
2552 if (WALK(from->quals))
2553 return true;
2554 }
2555 break;
2556 case T_OnConflictExpr:
2557 {
2558 OnConflictExpr *onconflict = (OnConflictExpr *) node;
2559
2560 if (WALK(onconflict->arbiterElems))
2561 return true;
2562 if (WALK(onconflict->arbiterWhere))
2563 return true;
2564 if (WALK(onconflict->onConflictSet))
2565 return true;
2566 if (WALK(onconflict->onConflictWhere))
2567 return true;
2568 if (WALK(onconflict->exclRelTlist))
2569 return true;
2570 }
2571 break;
2572 case T_MergeAction:
2573 {
2574 MergeAction *action = (MergeAction *) node;
2575
2576 if (WALK(action->qual))
2577 return true;
2578 if (WALK(action->targetList))
2579 return true;
2580 }
2581 break;
2582 case T_ForPortionOfExpr:
2583 {
2584 ForPortionOfExpr *forPortionOf = (ForPortionOfExpr *) node;
2585
2586 if (WALK(forPortionOf->targetFrom))
2587 return true;
2588 if (WALK(forPortionOf->targetTo))
2589 return true;
2590 if (WALK(forPortionOf->targetRange))
2591 return true;
2592 if (WALK(forPortionOf->overlapsExpr))
2593 return true;
2594 }
2595 break;
2597 {
2599
2600 if (WALK(opstep->exprs))
2601 return true;
2602 }
2603 break;
2605 /* no expression subnodes */
2606 break;
2607 case T_JoinExpr:
2608 {
2609 JoinExpr *join = (JoinExpr *) node;
2610
2611 if (WALK(join->larg))
2612 return true;
2613 if (WALK(join->rarg))
2614 return true;
2615 if (WALK(join->quals))
2616 return true;
2617
2618 /*
2619 * alias clause, using list are deemed uninteresting.
2620 */
2621 }
2622 break;
2623 case T_SetOperationStmt:
2624 {
2625 SetOperationStmt *setop = (SetOperationStmt *) node;
2626
2627 if (WALK(setop->larg))
2628 return true;
2629 if (WALK(setop->rarg))
2630 return true;
2631
2632 /* groupClauses are deemed uninteresting */
2633 }
2634 break;
2635 case T_IndexClause:
2636 {
2637 IndexClause *iclause = (IndexClause *) node;
2638
2639 if (WALK(iclause->rinfo))
2640 return true;
2641 if (LIST_WALK(iclause->indexquals))
2642 return true;
2643 }
2644 break;
2645 case T_PlaceHolderVar:
2646 return WALK(((PlaceHolderVar *) node)->phexpr);
2647 case T_InferenceElem:
2648 return WALK(((InferenceElem *) node)->expr);
2649 case T_ReturningExpr:
2650 return WALK(((ReturningExpr *) node)->retexpr);
2651 case T_AppendRelInfo:
2652 {
2654
2655 if (LIST_WALK(appinfo->translated_vars))
2656 return true;
2657 }
2658 break;
2659 case T_PlaceHolderInfo:
2660 return WALK(((PlaceHolderInfo *) node)->ph_var);
2661 case T_RangeTblFunction:
2662 return WALK(((RangeTblFunction *) node)->funcexpr);
2664 {
2666
2667 if (LIST_WALK(tsc->args))
2668 return true;
2669 if (WALK(tsc->repeatable))
2670 return true;
2671 }
2672 break;
2673 case T_TableFunc:
2674 {
2675 TableFunc *tf = (TableFunc *) node;
2676
2677 if (WALK(tf->ns_uris))
2678 return true;
2679 if (WALK(tf->docexpr))
2680 return true;
2681 if (WALK(tf->rowexpr))
2682 return true;
2683 if (WALK(tf->colexprs))
2684 return true;
2685 if (WALK(tf->coldefexprs))
2686 return true;
2687 if (WALK(tf->colvalexprs))
2688 return true;
2689 if (WALK(tf->passingvalexprs))
2690 return true;
2691 }
2692 break;
2694 {
2696
2697 if (WALK(gep->subexpr))
2698 return true;
2699 if (WALK(gep->whereClause))
2700 return true;
2701 }
2702 break;
2703 case T_GraphPattern:
2704 {
2705 GraphPattern *gp = (GraphPattern *) node;
2706
2707 if (LIST_WALK(gp->path_pattern_list))
2708 return true;
2709 if (WALK(gp->whereClause))
2710 return true;
2711 }
2712 break;
2713 default:
2714 elog(ERROR, "unrecognized node type: %d",
2715 (int) nodeTag(node));
2716 break;
2717 }
2718 return false;
2719
2720 /* The WALK() macro can be re-used below, but LIST_WALK() not so much */
2721#undef LIST_WALK
2722}
2723
2724/*
2725 * query_tree_walker --- initiate a walk of a Query's expressions
2726 *
2727 * This routine exists just to reduce the number of places that need to know
2728 * where all the expression subtrees of a Query are. Note it can be used
2729 * for starting a walk at top level of a Query regardless of whether the
2730 * walker intends to descend into subqueries. It is also useful for
2731 * descending into subqueries within a walker.
2732 *
2733 * Some callers want to suppress visitation of certain items in the sub-Query,
2734 * typically because they need to process them specially, or don't actually
2735 * want to recurse into subqueries. This is supported by the flags argument,
2736 * which is the bitwise OR of flag values to add or suppress visitation of
2737 * indicated items. (More flag bits may be added as needed.)
2738 */
2739bool
2742 void *context,
2743 int flags)
2744{
2745 Assert(query != NULL && IsA(query, Query));
2746
2747 /*
2748 * We don't walk any utilityStmt here. However, we can't easily assert
2749 * that it is absent, since there are at least two code paths by which
2750 * action statements from CREATE RULE end up here, and NOTIFY is allowed
2751 * in a rule action.
2752 */
2753
2754 if (WALK(query->targetList))
2755 return true;
2756 if (WALK(query->withCheckOptions))
2757 return true;
2758 if (WALK(query->onConflict))
2759 return true;
2760 if (WALK(query->mergeActionList))
2761 return true;
2762 if (WALK(query->mergeJoinCondition))
2763 return true;
2764 if (WALK(query->forPortionOf))
2765 return true;
2766 if (WALK(query->returningList))
2767 return true;
2768 if (WALK(query->jointree))
2769 return true;
2770 if (WALK(query->setOperations))
2771 return true;
2772 if (WALK(query->havingQual))
2773 return true;
2774 if (WALK(query->limitOffset))
2775 return true;
2776 if (WALK(query->limitCount))
2777 return true;
2778
2779 /*
2780 * Most callers aren't interested in SortGroupClause nodes since those
2781 * don't contain actual expressions. However they do contain OIDs which
2782 * may be needed by dependency walkers etc.
2783 */
2784 if ((flags & QTW_EXAMINE_SORTGROUP))
2785 {
2786 if (WALK(query->groupClause))
2787 return true;
2788 if (WALK(query->windowClause))
2789 return true;
2790 if (WALK(query->sortClause))
2791 return true;
2792 if (WALK(query->distinctClause))
2793 return true;
2794 }
2795 else
2796 {
2797 /*
2798 * But we need to walk the expressions under WindowClause nodes even
2799 * if we're not interested in SortGroupClause nodes.
2800 */
2801 ListCell *lc;
2802
2803 foreach(lc, query->windowClause)
2804 {
2806
2807 if (WALK(wc->startOffset))
2808 return true;
2809 if (WALK(wc->endOffset))
2810 return true;
2811 }
2812 }
2813
2814 /*
2815 * groupingSets and rowMarks are not walked:
2816 *
2817 * groupingSets contain only ressortgrouprefs (integers) which are
2818 * meaningless without the corresponding groupClause or tlist.
2819 * Accordingly, any walker that needs to care about them needs to handle
2820 * them itself in its Query processing.
2821 *
2822 * rowMarks is not walked because it contains only rangetable indexes (and
2823 * flags etc.) and therefore should be handled at Query level similarly.
2824 */
2825
2826 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2827 {
2828 if (WALK(query->cteList))
2829 return true;
2830 }
2831 if (!(flags & QTW_IGNORE_RANGE_TABLE))
2832 {
2833 if (range_table_walker(query->rtable, walker, context, flags))
2834 return true;
2835 }
2836 return false;
2837}
2838
2839/*
2840 * range_table_walker is just the part of query_tree_walker that scans
2841 * a query's rangetable. This is split out since it can be useful on
2842 * its own.
2843 */
2844bool
2847 void *context,
2848 int flags)
2849{
2850 ListCell *rt;
2851
2852 foreach(rt, rtable)
2853 {
2855
2856 if (range_table_entry_walker(rte, walker, context, flags))
2857 return true;
2858 }
2859 return false;
2860}
2861
2862/*
2863 * Some callers even want to scan the expressions in individual RTEs.
2864 */
2865bool
2868 void *context,
2869 int flags)
2870{
2871 /*
2872 * Walkers might need to examine the RTE node itself either before or
2873 * after visiting its contents (or, conceivably, both). Note that if you
2874 * specify neither flag, the walker won't be called on the RTE at all.
2875 */
2876 if (flags & QTW_EXAMINE_RTES_BEFORE)
2877 if (WALK(rte))
2878 return true;
2879
2880 switch (rte->rtekind)
2881 {
2882 case RTE_RELATION:
2883 if (WALK(rte->tablesample))
2884 return true;
2885 break;
2886 case RTE_SUBQUERY:
2887 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2888 if (WALK(rte->subquery))
2889 return true;
2890 break;
2891 case RTE_JOIN:
2892 if (!(flags & QTW_IGNORE_JOINALIASES))
2893 if (WALK(rte->joinaliasvars))
2894 return true;
2895 break;
2896 case RTE_FUNCTION:
2897 if (WALK(rte->functions))
2898 return true;
2899 break;
2900 case RTE_TABLEFUNC:
2901 if (WALK(rte->tablefunc))
2902 return true;
2903 break;
2904 case RTE_VALUES:
2905 if (WALK(rte->values_lists))
2906 return true;
2907 break;
2908 case RTE_GRAPH_TABLE:
2909 if (WALK(rte->graph_pattern))
2910 return true;
2911 if (WALK(rte->graph_table_columns))
2912 return true;
2913 break;
2914 case RTE_CTE:
2916 case RTE_RESULT:
2917 /* nothing to do */
2918 break;
2919 case RTE_GROUP:
2920 if (!(flags & QTW_IGNORE_GROUPEXPRS))
2921 if (WALK(rte->groupexprs))
2922 return true;
2923 break;
2924 }
2925
2926 if (WALK(rte->securityQuals))
2927 return true;
2928
2929 if (flags & QTW_EXAMINE_RTES_AFTER)
2930 if (WALK(rte))
2931 return true;
2932
2933 return false;
2934}
2935
2936
2937/*
2938 * expression_tree_mutator() is designed to support routines that make a
2939 * modified copy of an expression tree, with some nodes being added,
2940 * removed, or replaced by new subtrees. The original tree is (normally)
2941 * not changed. Each recursion level is responsible for returning a copy of
2942 * (or appropriately modified substitute for) the subtree it is handed.
2943 * A mutator routine should look like this:
2944 *
2945 * Node * my_mutator (Node *node, my_struct *context)
2946 * {
2947 * if (node == NULL)
2948 * return NULL;
2949 * // check for nodes that special work is required for, eg:
2950 * if (IsA(node, Var))
2951 * {
2952 * ... create and return modified copy of Var node
2953 * }
2954 * else if (IsA(node, ...))
2955 * {
2956 * ... do special transformations of other node types
2957 * }
2958 * // for any node type not specially processed, do:
2959 * return expression_tree_mutator(node, my_mutator, context);
2960 * }
2961 *
2962 * The "context" argument points to a struct that holds whatever context
2963 * information the mutator routine needs --- it can be used to return extra
2964 * data gathered by the mutator, too. This argument is not touched by
2965 * expression_tree_mutator, but it is passed down to recursive sub-invocations
2966 * of my_mutator. The tree walk is started from a setup routine that
2967 * fills in the appropriate context struct, calls my_mutator with the
2968 * top-level node of the tree, and does any required post-processing.
2969 *
2970 * Each level of recursion must return an appropriately modified Node.
2971 * If expression_tree_mutator() is called, it will make an exact copy
2972 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
2973 * of that Node. In this way, my_mutator() has full control over the
2974 * copying process but need not directly deal with expression trees
2975 * that it has no interest in.
2976 *
2977 * Just as for expression_tree_walker, the node types handled by
2978 * expression_tree_mutator include all those normally found in target lists
2979 * and qualifier clauses during the planning stage.
2980 *
2981 * expression_tree_mutator will handle SubLink nodes by recursing normally
2982 * into the "testexpr" subtree (which is an expression belonging to the outer
2983 * plan). It will also call the mutator on the sub-Query node; however, when
2984 * expression_tree_mutator itself is called on a Query node, it does nothing
2985 * and returns the unmodified Query node. The net effect is that unless the
2986 * mutator does something special at a Query node, sub-selects will not be
2987 * visited or modified; the original sub-select will be linked to by the new
2988 * SubLink node. Mutators that want to descend into sub-selects will usually
2989 * do so by recognizing Query nodes and calling query_tree_mutator (below).
2990 *
2991 * expression_tree_mutator will handle a SubPlan node by recursing into the
2992 * "testexpr" and the "args" list (which belong to the outer plan), but it
2993 * will simply copy the link to the inner plan, since that's typically what
2994 * expression tree mutators want. A mutator that wants to modify the subplan
2995 * can force appropriate behavior by recognizing SubPlan expression nodes
2996 * and doing the right thing.
2997 */
2998
2999Node *
3002 void *context)
3003{
3004 /*
3005 * The mutator has already decided not to modify the current node, but we
3006 * must call the mutator for any sub-nodes.
3007 */
3008
3009#define FLATCOPY(newnode, node, nodetype) \
3010 ( (newnode) = palloc_object(nodetype), \
3011 memcpy((newnode), (node), sizeof(nodetype)) )
3012
3013#define MUTATE(newfield, oldfield, fieldtype) \
3014 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
3015
3016 if (node == NULL)
3017 return NULL;
3018
3019 /* Guard against stack overflow due to overly complex expressions */
3021
3022 switch (nodeTag(node))
3023 {
3024 /*
3025 * Primitive node types with no expression subnodes. Var and
3026 * Const are frequent enough to deserve special cases, the others
3027 * we just use copyObject for.
3028 */
3029 case T_Var:
3030 {
3031 Var *var = (Var *) node;
3032 Var *newnode;
3033
3034 FLATCOPY(newnode, var, Var);
3035 /* Assume we need not copy the varnullingrels bitmapset */
3036 return (Node *) newnode;
3037 }
3038 break;
3039 case T_Const:
3040 {
3041 Const *oldnode = (Const *) node;
3042 Const *newnode;
3043
3045 /* XXX we don't bother with datumCopy; should we? */
3046 return (Node *) newnode;
3047 }
3048 break;
3049 case T_Param:
3050 case T_CaseTestExpr:
3051 case T_SQLValueFunction:
3052 case T_JsonFormat:
3054 case T_SetToDefault:
3055 case T_CurrentOfExpr:
3056 case T_NextValueExpr:
3057 case T_RangeTblRef:
3058 case T_SortGroupClause:
3059 case T_CTESearchClause:
3060 case T_MergeSupportFunc:
3061 return copyObject(node);
3062 case T_WithCheckOption:
3063 {
3066
3068 MUTATE(newnode->qual, wco->qual, Node *);
3069 return (Node *) newnode;
3070 }
3071 case T_Aggref:
3072 {
3073 Aggref *aggref = (Aggref *) node;
3074 Aggref *newnode;
3075
3076 FLATCOPY(newnode, aggref, Aggref);
3077 /* assume mutation doesn't change types of arguments */
3078 newnode->aggargtypes = list_copy(aggref->aggargtypes);
3079 MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
3080 MUTATE(newnode->args, aggref->args, List *);
3081 MUTATE(newnode->aggorder, aggref->aggorder, List *);
3082 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
3083 MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
3084 return (Node *) newnode;
3085 }
3086 break;
3087 case T_GroupingFunc:
3088 {
3091
3093 MUTATE(newnode->args, grouping->args, List *);
3094
3095 /*
3096 * We assume here that mutating the arguments does not change
3097 * the semantics, i.e. that the arguments are not mutated in a
3098 * way that makes them semantically different from their
3099 * previously matching expressions in the GROUP BY clause.
3100 *
3101 * If a mutator somehow wanted to do this, it would have to
3102 * handle the refs and cols lists itself as appropriate.
3103 */
3104 newnode->refs = list_copy(grouping->refs);
3105 newnode->cols = list_copy(grouping->cols);
3106
3107 return (Node *) newnode;
3108 }
3109 break;
3110 case T_WindowFunc:
3111 {
3112 WindowFunc *wfunc = (WindowFunc *) node;
3114
3115 FLATCOPY(newnode, wfunc, WindowFunc);
3116 MUTATE(newnode->args, wfunc->args, List *);
3117 MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
3118 return (Node *) newnode;
3119 }
3120 break;
3122 {
3125
3127 MUTATE(newnode->arg, wfuncrc->arg, Expr *);
3128 return (Node *) newnode;
3129 }
3130 break;
3131 case T_SubscriptingRef:
3132 {
3133 SubscriptingRef *sbsref = (SubscriptingRef *) node;
3135
3137 MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
3138 List *);
3139 MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
3140 List *);
3141 MUTATE(newnode->refexpr, sbsref->refexpr,
3142 Expr *);
3143 MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
3144 Expr *);
3145
3146 return (Node *) newnode;
3147 }
3148 break;
3149 case T_FuncExpr:
3150 {
3151 FuncExpr *expr = (FuncExpr *) node;
3153
3154 FLATCOPY(newnode, expr, FuncExpr);
3155 MUTATE(newnode->args, expr->args, List *);
3156 return (Node *) newnode;
3157 }
3158 break;
3159 case T_NamedArgExpr:
3160 {
3161 NamedArgExpr *nexpr = (NamedArgExpr *) node;
3163
3165 MUTATE(newnode->arg, nexpr->arg, Expr *);
3166 return (Node *) newnode;
3167 }
3168 break;
3169 case T_OpExpr:
3170 {
3171 OpExpr *expr = (OpExpr *) node;
3172 OpExpr *newnode;
3173
3174 FLATCOPY(newnode, expr, OpExpr);
3175 MUTATE(newnode->args, expr->args, List *);
3176 return (Node *) newnode;
3177 }
3178 break;
3179 case T_DistinctExpr:
3180 {
3181 DistinctExpr *expr = (DistinctExpr *) node;
3183
3185 MUTATE(newnode->args, expr->args, List *);
3186 return (Node *) newnode;
3187 }
3188 break;
3189 case T_NullIfExpr:
3190 {
3191 NullIfExpr *expr = (NullIfExpr *) node;
3193
3194 FLATCOPY(newnode, expr, NullIfExpr);
3195 MUTATE(newnode->args, expr->args, List *);
3196 return (Node *) newnode;
3197 }
3198 break;
3200 {
3201 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3203
3205 MUTATE(newnode->args, expr->args, List *);
3206 return (Node *) newnode;
3207 }
3208 break;
3209 case T_BoolExpr:
3210 {
3211 BoolExpr *expr = (BoolExpr *) node;
3213
3214 FLATCOPY(newnode, expr, BoolExpr);
3215 MUTATE(newnode->args, expr->args, List *);
3216 return (Node *) newnode;
3217 }
3218 break;
3219 case T_SubLink:
3220 {
3221 SubLink *sublink = (SubLink *) node;
3223
3225 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
3226
3227 /*
3228 * Also invoke the mutator on the sublink's Query node, so it
3229 * can recurse into the sub-query if it wants to.
3230 */
3231 MUTATE(newnode->subselect, sublink->subselect, Node *);
3232 return (Node *) newnode;
3233 }
3234 break;
3235 case T_SubPlan:
3236 {
3237 SubPlan *subplan = (SubPlan *) node;
3239
3240 FLATCOPY(newnode, subplan, SubPlan);
3241 /* transform testexpr */
3242 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
3243 /* transform args list (params to be passed to subplan) */
3244 MUTATE(newnode->args, subplan->args, List *);
3245 /* but not the sub-Plan itself, which is referenced as-is */
3246 return (Node *) newnode;
3247 }
3248 break;
3250 {
3253
3255 MUTATE(newnode->subplans, asplan->subplans, List *);
3256 return (Node *) newnode;
3257 }
3258 break;
3259 case T_FieldSelect:
3260 {
3261 FieldSelect *fselect = (FieldSelect *) node;
3263
3265 MUTATE(newnode->arg, fselect->arg, Expr *);
3266 return (Node *) newnode;
3267 }
3268 break;
3269 case T_FieldStore:
3270 {
3271 FieldStore *fstore = (FieldStore *) node;
3273
3274 FLATCOPY(newnode, fstore, FieldStore);
3275 MUTATE(newnode->arg, fstore->arg, Expr *);
3276 MUTATE(newnode->newvals, fstore->newvals, List *);
3277 newnode->fieldnums = list_copy(fstore->fieldnums);
3278 return (Node *) newnode;
3279 }
3280 break;
3281 case T_RelabelType:
3282 {
3283 RelabelType *relabel = (RelabelType *) node;
3285
3287 MUTATE(newnode->arg, relabel->arg, Expr *);
3288 return (Node *) newnode;
3289 }
3290 break;
3291 case T_CoerceViaIO:
3292 {
3293 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
3295
3296 FLATCOPY(newnode, iocoerce, CoerceViaIO);
3297 MUTATE(newnode->arg, iocoerce->arg, Expr *);
3298 return (Node *) newnode;
3299 }
3300 break;
3301 case T_ArrayCoerceExpr:
3302 {
3305
3307 MUTATE(newnode->arg, acoerce->arg, Expr *);
3308 MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
3309 return (Node *) newnode;
3310 }
3311 break;
3313 {
3316
3318 MUTATE(newnode->arg, convexpr->arg, Expr *);
3319 return (Node *) newnode;
3320 }
3321 break;
3322 case T_CollateExpr:
3323 {
3324 CollateExpr *collate = (CollateExpr *) node;
3326
3327 FLATCOPY(newnode, collate, CollateExpr);
3328 MUTATE(newnode->arg, collate->arg, Expr *);
3329 return (Node *) newnode;
3330 }
3331 break;
3332 case T_CaseExpr:
3333 {
3334 CaseExpr *caseexpr = (CaseExpr *) node;
3336
3338 MUTATE(newnode->arg, caseexpr->arg, Expr *);
3339 MUTATE(newnode->args, caseexpr->args, List *);
3340 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
3341 return (Node *) newnode;
3342 }
3343 break;
3344 case T_CaseWhen:
3345 {
3346 CaseWhen *casewhen = (CaseWhen *) node;
3348
3350 MUTATE(newnode->expr, casewhen->expr, Expr *);
3351 MUTATE(newnode->result, casewhen->result, Expr *);
3352 return (Node *) newnode;
3353 }
3354 break;
3355 case T_ArrayExpr:
3356 {
3357 ArrayExpr *arrayexpr = (ArrayExpr *) node;
3359
3360 FLATCOPY(newnode, arrayexpr, ArrayExpr);
3361 MUTATE(newnode->elements, arrayexpr->elements, List *);
3362 return (Node *) newnode;
3363 }
3364 break;
3365 case T_RowExpr:
3366 {
3367 RowExpr *rowexpr = (RowExpr *) node;
3369
3370 FLATCOPY(newnode, rowexpr, RowExpr);
3371 MUTATE(newnode->args, rowexpr->args, List *);
3372 /* Assume colnames needn't be duplicated */
3373 return (Node *) newnode;
3374 }
3375 break;
3376 case T_RowCompareExpr:
3377 {
3380
3382 MUTATE(newnode->largs, rcexpr->largs, List *);
3383 MUTATE(newnode->rargs, rcexpr->rargs, List *);
3384 return (Node *) newnode;
3385 }
3386 break;
3387 case T_CoalesceExpr:
3388 {
3391
3393 MUTATE(newnode->args, coalesceexpr->args, List *);
3394 return (Node *) newnode;
3395 }
3396 break;
3397 case T_MinMaxExpr:
3398 {
3399 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3401
3403 MUTATE(newnode->args, minmaxexpr->args, List *);
3404 return (Node *) newnode;
3405 }
3406 break;
3407 case T_XmlExpr:
3408 {
3409 XmlExpr *xexpr = (XmlExpr *) node;
3411
3412 FLATCOPY(newnode, xexpr, XmlExpr);
3413 MUTATE(newnode->named_args, xexpr->named_args, List *);
3414 /* assume mutator does not care about arg_names */
3415 MUTATE(newnode->args, xexpr->args, List *);
3416 return (Node *) newnode;
3417 }
3418 break;
3419 case T_JsonReturning:
3420 {
3421 JsonReturning *jr = (JsonReturning *) node;
3423
3425 MUTATE(newnode->format, jr->format, JsonFormat *);
3426
3427 return (Node *) newnode;
3428 }
3429 case T_JsonValueExpr:
3430 {
3431 JsonValueExpr *jve = (JsonValueExpr *) node;
3433
3435 MUTATE(newnode->raw_expr, jve->raw_expr, Expr *);
3436 MUTATE(newnode->formatted_expr, jve->formatted_expr, Expr *);
3437 MUTATE(newnode->format, jve->format, JsonFormat *);
3438
3439 return (Node *) newnode;
3440 }
3442 {
3445
3447 MUTATE(newnode->args, jce->args, List *);
3448 MUTATE(newnode->func, jce->func, Expr *);
3449 MUTATE(newnode->coercion, jce->coercion, Expr *);
3450 MUTATE(newnode->returning, jce->returning, JsonReturning *);
3451
3452 return (Node *) newnode;
3453 }
3454 case T_JsonIsPredicate:
3455 {
3456 JsonIsPredicate *pred = (JsonIsPredicate *) node;
3458
3460 MUTATE(newnode->expr, pred->expr, Node *);
3461 MUTATE(newnode->format, pred->format, JsonFormat *);
3462
3463 return (Node *) newnode;
3464 }
3465 case T_JsonExpr:
3466 {
3467 JsonExpr *jexpr = (JsonExpr *) node;
3469
3471 MUTATE(newnode->formatted_expr, jexpr->formatted_expr, Node *);
3472 MUTATE(newnode->path_spec, jexpr->path_spec, Node *);
3473 MUTATE(newnode->passing_values, jexpr->passing_values, List *);
3474 /* assume mutator does not care about passing_names */
3475 MUTATE(newnode->on_empty, jexpr->on_empty, JsonBehavior *);
3476 MUTATE(newnode->on_error, jexpr->on_error, JsonBehavior *);
3477 return (Node *) newnode;
3478 }
3479 break;
3480 case T_JsonBehavior:
3481 {
3482 JsonBehavior *behavior = (JsonBehavior *) node;
3484
3485 FLATCOPY(newnode, behavior, JsonBehavior);
3486 MUTATE(newnode->expr, behavior->expr, Node *);
3487 return (Node *) newnode;
3488 }
3489 break;
3490 case T_NullTest:
3491 {
3492 NullTest *ntest = (NullTest *) node;
3494
3496 MUTATE(newnode->arg, ntest->arg, Expr *);
3497 return (Node *) newnode;
3498 }
3499 break;
3500 case T_BooleanTest:
3501 {
3502 BooleanTest *btest = (BooleanTest *) node;
3504
3506 MUTATE(newnode->arg, btest->arg, Expr *);
3507 return (Node *) newnode;
3508 }
3509 break;
3510 case T_CoerceToDomain:
3511 {
3514
3516 MUTATE(newnode->arg, ctest->arg, Expr *);
3517 return (Node *) newnode;
3518 }
3519 break;
3520 case T_ReturningExpr:
3521 {
3522 ReturningExpr *rexpr = (ReturningExpr *) node;
3524
3526 MUTATE(newnode->retexpr, rexpr->retexpr, Expr *);
3527 return (Node *) newnode;
3528 }
3529 break;
3530 case T_TargetEntry:
3531 {
3534
3536 MUTATE(newnode->expr, targetentry->expr, Expr *);
3537 return (Node *) newnode;
3538 }
3539 break;
3540 case T_Query:
3541 /* Do nothing with a sub-Query, per discussion above */
3542 return node;
3543 case T_WindowClause:
3544 {
3545 WindowClause *wc = (WindowClause *) node;
3547
3549 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
3550 MUTATE(newnode->orderClause, wc->orderClause, List *);
3551 MUTATE(newnode->startOffset, wc->startOffset, Node *);
3552 MUTATE(newnode->endOffset, wc->endOffset, Node *);
3553 return (Node *) newnode;
3554 }
3555 break;
3556 case T_CTECycleClause:
3557 {
3558 CTECycleClause *cc = (CTECycleClause *) node;
3560
3562 MUTATE(newnode->cycle_mark_value, cc->cycle_mark_value, Node *);
3563 MUTATE(newnode->cycle_mark_default, cc->cycle_mark_default, Node *);
3564 return (Node *) newnode;
3565 }
3566 break;
3567 case T_CommonTableExpr:
3568 {
3569 CommonTableExpr *cte = (CommonTableExpr *) node;
3571
3573
3574 /*
3575 * Also invoke the mutator on the CTE's Query node, so it can
3576 * recurse into the sub-query if it wants to.
3577 */
3578 MUTATE(newnode->ctequery, cte->ctequery, Node *);
3579
3580 MUTATE(newnode->search_clause, cte->search_clause, CTESearchClause *);
3581 MUTATE(newnode->cycle_clause, cte->cycle_clause, CTECycleClause *);
3582
3583 return (Node *) newnode;
3584 }
3585 break;
3587 {
3590
3592 MUTATE(newnode->listdatums, pbs->listdatums, List *);
3593 MUTATE(newnode->lowerdatums, pbs->lowerdatums, List *);
3594 MUTATE(newnode->upperdatums, pbs->upperdatums, List *);
3595 return (Node *) newnode;
3596 }
3597 break;
3599 {
3602
3604 MUTATE(newnode->value, prd->value, Node *);
3605 return (Node *) newnode;
3606 }
3607 break;
3608 case T_List:
3609 {
3610 /*
3611 * We assume the mutator isn't interested in the list nodes
3612 * per se, so just invoke it on each list element. NOTE: this
3613 * would fail badly on a list with integer elements!
3614 */
3616 ListCell *temp;
3617
3618 resultlist = NIL;
3619 foreach(temp, (List *) node)
3620 {
3622 mutator((Node *) lfirst(temp),
3623 context));
3624 }
3625 return (Node *) resultlist;
3626 }
3627 break;
3628 case T_FromExpr:
3629 {
3630 FromExpr *from = (FromExpr *) node;
3632
3633 FLATCOPY(newnode, from, FromExpr);
3634 MUTATE(newnode->fromlist, from->fromlist, List *);
3635 MUTATE(newnode->quals, from->quals, Node *);
3636 return (Node *) newnode;
3637 }
3638 break;
3639 case T_OnConflictExpr:
3640 {
3641 OnConflictExpr *oc = (OnConflictExpr *) node;
3643
3645 MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
3646 MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
3647 MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
3648 MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
3649 MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
3650
3651 return (Node *) newnode;
3652 }
3653 break;
3654 case T_MergeAction:
3655 {
3656 MergeAction *action = (MergeAction *) node;
3658
3659 FLATCOPY(newnode, action, MergeAction);
3660 MUTATE(newnode->qual, action->qual, Node *);
3661 MUTATE(newnode->targetList, action->targetList, List *);
3662
3663 return (Node *) newnode;
3664 }
3665 break;
3666 case T_ForPortionOfExpr:
3667 {
3670
3672 MUTATE(newnode->rangeVar, fpo->rangeVar, Var *);
3673 MUTATE(newnode->targetFrom, fpo->targetFrom, Node *);
3674 MUTATE(newnode->targetTo, fpo->targetTo, Node *);
3675 MUTATE(newnode->targetRange, fpo->targetRange, Node *);
3676 MUTATE(newnode->overlapsExpr, fpo->overlapsExpr, Node *);
3677 MUTATE(newnode->rangeTargetList, fpo->rangeTargetList, List *);
3678
3679 return (Node *) newnode;
3680 }
3681 break;
3683 {
3686
3688 MUTATE(newnode->exprs, opstep->exprs, List *);
3689
3690 return (Node *) newnode;
3691 }
3692 break;
3694 /* no expression sub-nodes */
3695 return copyObject(node);
3696 case T_JoinExpr:
3697 {
3698 JoinExpr *join = (JoinExpr *) node;
3700
3701 FLATCOPY(newnode, join, JoinExpr);
3702 MUTATE(newnode->larg, join->larg, Node *);
3703 MUTATE(newnode->rarg, join->rarg, Node *);
3704 MUTATE(newnode->quals, join->quals, Node *);
3705 /* We do not mutate alias or using by default */
3706 return (Node *) newnode;
3707 }
3708 break;
3709 case T_SetOperationStmt:
3710 {
3711 SetOperationStmt *setop = (SetOperationStmt *) node;
3713
3715 MUTATE(newnode->larg, setop->larg, Node *);
3716 MUTATE(newnode->rarg, setop->rarg, Node *);
3717 /* We do not mutate groupClauses by default */
3718 return (Node *) newnode;
3719 }
3720 break;
3721 case T_IndexClause:
3722 {
3723 IndexClause *iclause = (IndexClause *) node;
3725
3727 MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
3728 MUTATE(newnode->indexquals, iclause->indexquals, List *);
3729 return (Node *) newnode;
3730 }
3731 break;
3732 case T_PlaceHolderVar:
3733 {
3734 PlaceHolderVar *phv = (PlaceHolderVar *) node;
3736
3738 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
3739 /* Assume we need not copy the relids bitmapsets */
3740 return (Node *) newnode;
3741 }
3742 break;
3743 case T_InferenceElem:
3744 {
3747
3749 MUTATE(newnode->expr, newnode->expr, Node *);
3750 return (Node *) newnode;
3751 }
3752 break;
3753 case T_AppendRelInfo:
3754 {
3757
3759 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
3760 /* Assume nothing need be done with parent_colnos[] */
3761 return (Node *) newnode;
3762 }
3763 break;
3764 case T_PlaceHolderInfo:
3765 {
3768
3770 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
3771 /* Assume we need not copy the relids bitmapsets */
3772 return (Node *) newnode;
3773 }
3774 break;
3775 case T_RangeTblFunction:
3776 {
3777 RangeTblFunction *rtfunc = (RangeTblFunction *) node;
3779
3781 MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
3782 /* Assume we need not copy the coldef info lists */
3783 return (Node *) newnode;
3784 }
3785 break;
3787 {
3790
3792 MUTATE(newnode->args, tsc->args, List *);
3793 MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
3794 return (Node *) newnode;
3795 }
3796 break;
3797 case T_TableFunc:
3798 {
3799 TableFunc *tf = (TableFunc *) node;
3801
3803 MUTATE(newnode->ns_uris, tf->ns_uris, List *);
3804 MUTATE(newnode->docexpr, tf->docexpr, Node *);
3805 MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
3806 MUTATE(newnode->colexprs, tf->colexprs, List *);
3807 MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
3808 MUTATE(newnode->colvalexprs, tf->colvalexprs, List *);
3809 MUTATE(newnode->passingvalexprs, tf->passingvalexprs, List *);
3810 return (Node *) newnode;
3811 }
3812 break;
3813 default:
3814 elog(ERROR, "unrecognized node type: %d",
3815 (int) nodeTag(node));
3816 break;
3817 }
3818 /* can't get here, but keep compiler happy */
3819 return NULL;
3820}
3821
3822
3823/*
3824 * query_tree_mutator --- initiate modification of a Query's expressions
3825 *
3826 * This routine exists just to reduce the number of places that need to know
3827 * where all the expression subtrees of a Query are. Note it can be used
3828 * for starting a walk at top level of a Query regardless of whether the
3829 * mutator intends to descend into subqueries. It is also useful for
3830 * descending into subqueries within a mutator.
3831 *
3832 * Some callers want to suppress mutating of certain items in the Query,
3833 * typically because they need to process them specially, or don't actually
3834 * want to recurse into subqueries. This is supported by the flags argument,
3835 * which is the bitwise OR of flag values to suppress mutating of
3836 * indicated items. (More flag bits may be added as needed.)
3837 *
3838 * Normally the top-level Query node itself is copied, but some callers want
3839 * it to be modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.
3840 * All modified substructure is safely copied in any case.
3841 */
3842Query *
3845 void *context,
3846 int flags)
3847{
3848 Assert(query != NULL && IsA(query, Query));
3849
3850 if (!(flags & QTW_DONT_COPY_QUERY))
3851 {
3852 Query *newquery;
3853
3854 FLATCOPY(newquery, query, Query);
3855 query = newquery;
3856 }
3857
3858 MUTATE(query->targetList, query->targetList, List *);
3859 MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
3860 MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
3861 MUTATE(query->mergeActionList, query->mergeActionList, List *);
3864 MUTATE(query->returningList, query->returningList, List *);
3865 MUTATE(query->jointree, query->jointree, FromExpr *);
3866 MUTATE(query->setOperations, query->setOperations, Node *);
3867 MUTATE(query->havingQual, query->havingQual, Node *);
3868 MUTATE(query->limitOffset, query->limitOffset, Node *);
3869 MUTATE(query->limitCount, query->limitCount, Node *);
3870
3871 /*
3872 * Most callers aren't interested in SortGroupClause nodes since those
3873 * don't contain actual expressions. However they do contain OIDs, which
3874 * may be of interest to some mutators.
3875 */
3876
3877 if ((flags & QTW_EXAMINE_SORTGROUP))
3878 {
3879 MUTATE(query->groupClause, query->groupClause, List *);
3880 MUTATE(query->windowClause, query->windowClause, List *);
3881 MUTATE(query->sortClause, query->sortClause, List *);
3882 MUTATE(query->distinctClause, query->distinctClause, List *);
3883 }
3884 else
3885 {
3886 /*
3887 * But we need to mutate the expressions under WindowClause nodes even
3888 * if we're not interested in SortGroupClause nodes.
3889 */
3891 ListCell *temp;
3892
3893 resultlist = NIL;
3894 foreach(temp, query->windowClause)
3895 {
3898
3900 MUTATE(newnode->startOffset, wc->startOffset, Node *);
3901 MUTATE(newnode->endOffset, wc->endOffset, Node *);
3902
3904 }
3905 query->windowClause = resultlist;
3906 }
3907
3908 /*
3909 * groupingSets and rowMarks are not mutated:
3910 *
3911 * groupingSets contain only ressortgroup refs (integers) which are
3912 * meaningless without the groupClause or tlist. Accordingly, any mutator
3913 * that needs to care about them needs to handle them itself in its Query
3914 * processing.
3915 *
3916 * rowMarks contains only rangetable indexes (and flags etc.) and
3917 * therefore should be handled at Query level similarly.
3918 */
3919
3920 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
3921 MUTATE(query->cteList, query->cteList, List *);
3922 else /* else copy CTE list as-is */
3923 query->cteList = copyObject(query->cteList);
3924 query->rtable = range_table_mutator(query->rtable,
3925 mutator, context, flags);
3926 return query;
3927}
3928
3929/*
3930 * range_table_mutator is just the part of query_tree_mutator that processes
3931 * a query's rangetable. This is split out since it can be useful on
3932 * its own.
3933 */
3934List *
3937 void *context,
3938 int flags)
3939{
3940 List *newrt = NIL;
3941 ListCell *rt;
3942
3943 foreach(rt, rtable)
3944 {
3947
3949 switch (rte->rtekind)
3950 {
3951 case RTE_RELATION:
3952 MUTATE(newrte->tablesample, rte->tablesample,
3954 /* we don't bother to copy eref, aliases, etc; OK? */
3955 break;
3956 case RTE_SUBQUERY:
3957 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3958 MUTATE(newrte->subquery, rte->subquery, Query *);
3959 else
3960 {
3961 /* else, copy RT subqueries as-is */
3962 newrte->subquery = copyObject(rte->subquery);
3963 }
3964 break;
3965 case RTE_JOIN:
3966 if (!(flags & QTW_IGNORE_JOINALIASES))
3967 MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
3968 else
3969 {
3970 /* else, copy join aliases as-is */
3971 newrte->joinaliasvars = copyObject(rte->joinaliasvars);
3972 }
3973 break;
3974 case RTE_FUNCTION:
3975 MUTATE(newrte->functions, rte->functions, List *);
3976 break;
3977 case RTE_TABLEFUNC:
3978 MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
3979 break;
3980 case RTE_VALUES:
3981 MUTATE(newrte->values_lists, rte->values_lists, List *);
3982 break;
3983 case RTE_GRAPH_TABLE:
3984 MUTATE(newrte->graph_pattern, rte->graph_pattern, GraphPattern *);
3985 MUTATE(newrte->graph_table_columns, rte->graph_table_columns, List *);
3986 break;
3987 case RTE_CTE:
3989 case RTE_RESULT:
3990 /* nothing to do */
3991 break;
3992 case RTE_GROUP:
3993 if (!(flags & QTW_IGNORE_GROUPEXPRS))
3994 MUTATE(newrte->groupexprs, rte->groupexprs, List *);
3995 else
3996 {
3997 /* else, copy grouping exprs as-is */
3998 newrte->groupexprs = copyObject(rte->groupexprs);
3999 }
4000 break;
4001 }
4002 MUTATE(newrte->securityQuals, rte->securityQuals, List *);
4004 }
4005 return newrt;
4006}
4007
4008/*
4009 * query_or_expression_tree_walker --- hybrid form
4010 *
4011 * This routine will invoke query_tree_walker if called on a Query node,
4012 * else will invoke the walker directly. This is a useful way of starting
4013 * the recursion when the walker's normal change of state is not appropriate
4014 * for the outermost Query node.
4015 */
4016bool
4019 void *context,
4020 int flags)
4021{
4022 if (node && IsA(node, Query))
4023 return query_tree_walker((Query *) node,
4024 walker,
4025 context,
4026 flags);
4027 else
4028 return WALK(node);
4029}
4030
4031/*
4032 * query_or_expression_tree_mutator --- hybrid form
4033 *
4034 * This routine will invoke query_tree_mutator if called on a Query node,
4035 * else will invoke the mutator directly. This is a useful way of starting
4036 * the recursion when the mutator's normal change of state is not appropriate
4037 * for the outermost Query node.
4038 */
4039Node *
4042 void *context,
4043 int flags)
4044{
4045 if (node && IsA(node, Query))
4046 return (Node *) query_tree_mutator((Query *) node,
4047 mutator,
4048 context,
4049 flags);
4050 else
4051 return mutator(node, context);
4052}
4053
4054
4055/*
4056 * raw_expression_tree_walker --- walk raw parse trees
4057 *
4058 * This has exactly the same API as expression_tree_walker, but instead of
4059 * walking post-analysis parse trees, it knows how to walk the node types
4060 * found in raw grammar output. (There is not currently any need for a
4061 * combined walker, so we keep them separate in the name of efficiency.)
4062 * Unlike expression_tree_walker, there is no special rule about query
4063 * boundaries: we descend to everything that's possibly interesting.
4064 *
4065 * Currently, the node type coverage here extends only to DML statements
4066 * (SELECT/INSERT/UPDATE/DELETE/MERGE) and nodes that can appear in them,
4067 * because this is used mainly during analysis of CTEs, and only DML
4068 * statements can appear in CTEs.
4069 */
4070bool
4073 void *context)
4074{
4075 ListCell *temp;
4076
4077 /*
4078 * The walker has already visited the current node, and so we need only
4079 * recurse into any sub-nodes it has.
4080 */
4081 if (node == NULL)
4082 return false;
4083
4084 /* Guard against stack overflow due to overly complex expressions */
4086
4087 switch (nodeTag(node))
4088 {
4089 case T_JsonFormat:
4090 case T_SetToDefault:
4091 case T_CurrentOfExpr:
4092 case T_SQLValueFunction:
4093 case T_Integer:
4094 case T_Float:
4095 case T_Boolean:
4096 case T_String:
4097 case T_BitString:
4098 case T_ParamRef:
4099 case T_A_Const:
4100 case T_A_Star:
4101 case T_MergeSupportFunc:
4102 case T_ReturningOption:
4103 /* primitive node types with no subnodes */
4104 break;
4105 case T_Alias:
4106 /* we assume the colnames list isn't interesting */
4107 break;
4108 case T_RangeVar:
4109 return WALK(((RangeVar *) node)->alias);
4110 case T_GroupingFunc:
4111 return WALK(((GroupingFunc *) node)->args);
4112 case T_SubLink:
4113 {
4114 SubLink *sublink = (SubLink *) node;
4115
4116 if (WALK(sublink->testexpr))
4117 return true;
4118 /* we assume the operName is not interesting */
4119 if (WALK(sublink->subselect))
4120 return true;
4121 }
4122 break;
4123 case T_CaseExpr:
4124 {
4125 CaseExpr *caseexpr = (CaseExpr *) node;
4126
4127 if (WALK(caseexpr->arg))
4128 return true;
4129 /* we assume walker doesn't care about CaseWhens, either */
4130 foreach(temp, caseexpr->args)
4131 {
4133
4134 if (WALK(when->expr))
4135 return true;
4136 if (WALK(when->result))
4137 return true;
4138 }
4139 if (WALK(caseexpr->defresult))
4140 return true;
4141 }
4142 break;
4143 case T_RowExpr:
4144 /* Assume colnames isn't interesting */
4145 return WALK(((RowExpr *) node)->args);
4146 case T_CoalesceExpr:
4147 return WALK(((CoalesceExpr *) node)->args);
4148 case T_MinMaxExpr:
4149 return WALK(((MinMaxExpr *) node)->args);
4150 case T_XmlExpr:
4151 {
4152 XmlExpr *xexpr = (XmlExpr *) node;
4153
4154 if (WALK(xexpr->named_args))
4155 return true;
4156 /* we assume walker doesn't care about arg_names */
4157 if (WALK(xexpr->args))
4158 return true;
4159 }
4160 break;
4161 case T_JsonReturning:
4162 return WALK(((JsonReturning *) node)->format);
4163 case T_JsonValueExpr:
4164 {
4165 JsonValueExpr *jve = (JsonValueExpr *) node;
4166
4167 if (WALK(jve->raw_expr))
4168 return true;
4169 if (WALK(jve->formatted_expr))
4170 return true;
4171 if (WALK(jve->format))
4172 return true;
4173 }
4174 break;
4175 case T_JsonParseExpr:
4176 {
4177 JsonParseExpr *jpe = (JsonParseExpr *) node;
4178
4179 if (WALK(jpe->expr))
4180 return true;
4181 if (WALK(jpe->output))
4182 return true;
4183 }
4184 break;
4185 case T_JsonScalarExpr:
4186 {
4187 JsonScalarExpr *jse = (JsonScalarExpr *) node;
4188
4189 if (WALK(jse->expr))
4190 return true;
4191 if (WALK(jse->output))
4192 return true;
4193 }
4194 break;
4196 {
4198
4199 if (WALK(jse->expr))
4200 return true;
4201 if (WALK(jse->output))
4202 return true;
4203 }
4204 break;
4206 {
4208
4209 if (WALK(ctor->args))
4210 return true;
4211 if (WALK(ctor->func))
4212 return true;
4213 if (WALK(ctor->coercion))
4214 return true;
4215 if (WALK(ctor->returning))
4216 return true;
4217 }
4218 break;
4219 case T_JsonIsPredicate:
4220 return WALK(((JsonIsPredicate *) node)->expr);
4221 case T_JsonArgument:
4222 return WALK(((JsonArgument *) node)->val);
4223 case T_JsonFuncExpr:
4224 {
4225 JsonFuncExpr *jfe = (JsonFuncExpr *) node;
4226
4227 if (WALK(jfe->context_item))
4228 return true;
4229 if (WALK(jfe->pathspec))
4230 return true;
4231 if (WALK(jfe->passing))
4232 return true;
4233 if (WALK(jfe->output))
4234 return true;
4235 if (WALK(jfe->on_empty))
4236 return true;
4237 if (WALK(jfe->on_error))
4238 return true;
4239 }
4240 break;
4241 case T_JsonBehavior:
4242 {
4243 JsonBehavior *jb = (JsonBehavior *) node;
4244
4245 if (WALK(jb->expr))
4246 return true;
4247 }
4248 break;
4249 case T_JsonTable:
4250 {
4251 JsonTable *jt = (JsonTable *) node;
4252
4253 if (WALK(jt->context_item))
4254 return true;
4255 if (WALK(jt->pathspec))
4256 return true;
4257 if (WALK(jt->passing))
4258 return true;
4259 if (WALK(jt->columns))
4260 return true;
4261 if (WALK(jt->on_error))
4262 return true;
4263 }
4264 break;
4265 case T_JsonTableColumn:
4266 {
4268
4269 if (WALK(jtc->typeName))
4270 return true;
4271 if (WALK(jtc->on_empty))
4272 return true;
4273 if (WALK(jtc->on_error))
4274 return true;
4275 if (WALK(jtc->columns))
4276 return true;
4277 }
4278 break;
4280 return WALK(((JsonTablePathSpec *) node)->string);
4281 case T_NullTest:
4282 return WALK(((NullTest *) node)->arg);
4283 case T_BooleanTest:
4284 return WALK(((BooleanTest *) node)->arg);
4285 case T_JoinExpr:
4286 {
4287 JoinExpr *join = (JoinExpr *) node;
4288
4289 if (WALK(join->larg))
4290 return true;
4291 if (WALK(join->rarg))
4292 return true;
4293 if (WALK(join->quals))
4294 return true;
4295 if (WALK(join->alias))
4296 return true;
4297 /* using list is deemed uninteresting */
4298 }
4299 break;
4300 case T_IntoClause:
4301 {
4302 IntoClause *into = (IntoClause *) node;
4303
4304 if (WALK(into->rel))
4305 return true;
4306 /* colNames, options are deemed uninteresting */
4307 /* viewQuery should be null in raw parsetree, but check it */
4308 if (WALK(into->viewQuery))
4309 return true;
4310 }
4311 break;
4312 case T_List:
4313 foreach(temp, (List *) node)
4314 {
4315 if (WALK((Node *) lfirst(temp)))
4316 return true;
4317 }
4318 break;
4319 case T_InsertStmt:
4320 {
4321 InsertStmt *stmt = (InsertStmt *) node;
4322
4323 if (WALK(stmt->relation))
4324 return true;
4325 if (WALK(stmt->cols))
4326 return true;
4327 if (WALK(stmt->selectStmt))
4328 return true;
4329 if (WALK(stmt->onConflictClause))
4330 return true;
4331 if (WALK(stmt->returningClause))
4332 return true;
4333 if (WALK(stmt->withClause))
4334 return true;
4335 }
4336 break;
4337 case T_DeleteStmt:
4338 {
4339 DeleteStmt *stmt = (DeleteStmt *) node;
4340
4341 if (WALK(stmt->relation))
4342 return true;
4343 if (WALK(stmt->usingClause))
4344 return true;
4345 if (WALK(stmt->whereClause))
4346 return true;
4347 if (WALK(stmt->returningClause))
4348 return true;
4349 if (WALK(stmt->withClause))
4350 return true;
4351 }
4352 break;
4353 case T_UpdateStmt:
4354 {
4355 UpdateStmt *stmt = (UpdateStmt *) node;
4356
4357 if (WALK(stmt->relation))
4358 return true;
4359 if (WALK(stmt->targetList))
4360 return true;
4361 if (WALK(stmt->whereClause))
4362 return true;
4363 if (WALK(stmt->fromClause))
4364 return true;
4365 if (WALK(stmt->returningClause))
4366 return true;
4367 if (WALK(stmt->withClause))
4368 return true;
4369 }
4370 break;
4371 case T_MergeStmt:
4372 {
4373 MergeStmt *stmt = (MergeStmt *) node;
4374
4375 if (WALK(stmt->relation))
4376 return true;
4377 if (WALK(stmt->sourceRelation))
4378 return true;
4379 if (WALK(stmt->joinCondition))
4380 return true;
4381 if (WALK(stmt->mergeWhenClauses))
4382 return true;
4383 if (WALK(stmt->returningClause))
4384 return true;
4385 if (WALK(stmt->withClause))
4386 return true;
4387 }
4388 break;
4389 case T_MergeWhenClause:
4390 {
4392
4393 if (WALK(mergeWhenClause->condition))
4394 return true;
4395 if (WALK(mergeWhenClause->targetList))
4396 return true;
4397 if (WALK(mergeWhenClause->values))
4398 return true;
4399 }
4400 break;
4401 case T_ReturningClause:
4402 {
4403 ReturningClause *returning = (ReturningClause *) node;
4404
4405 if (WALK(returning->options))
4406 return true;
4407 if (WALK(returning->exprs))
4408 return true;
4409 }
4410 break;
4411 case T_SelectStmt:
4412 {
4413 SelectStmt *stmt = (SelectStmt *) node;
4414
4415 if (WALK(stmt->distinctClause))
4416 return true;
4417 if (WALK(stmt->intoClause))
4418 return true;
4419 if (WALK(stmt->targetList))
4420 return true;
4421 if (WALK(stmt->fromClause))
4422 return true;
4423 if (WALK(stmt->whereClause))
4424 return true;
4425 if (WALK(stmt->groupClause))
4426 return true;
4427 if (WALK(stmt->havingClause))
4428 return true;
4429 if (WALK(stmt->windowClause))
4430 return true;
4431 if (WALK(stmt->valuesLists))
4432 return true;
4433 if (WALK(stmt->sortClause))
4434 return true;
4435 if (WALK(stmt->limitOffset))
4436 return true;
4437 if (WALK(stmt->limitCount))
4438 return true;
4439 if (WALK(stmt->lockingClause))
4440 return true;
4441 if (WALK(stmt->withClause))
4442 return true;
4443 if (WALK(stmt->larg))
4444 return true;
4445 if (WALK(stmt->rarg))
4446 return true;
4447 }
4448 break;
4449 case T_PLAssignStmt:
4450 {
4451 PLAssignStmt *stmt = (PLAssignStmt *) node;
4452
4453 if (WALK(stmt->indirection))
4454 return true;
4455 if (WALK(stmt->val))
4456 return true;
4457 }
4458 break;
4459 case T_A_Expr:
4460 {
4461 A_Expr *expr = (A_Expr *) node;
4462
4463 if (WALK(expr->lexpr))
4464 return true;
4465 if (WALK(expr->rexpr))
4466 return true;
4467 /* operator name is deemed uninteresting */
4468 }
4469 break;
4470 case T_BoolExpr:
4471 {
4472 BoolExpr *expr = (BoolExpr *) node;
4473
4474 if (WALK(expr->args))
4475 return true;
4476 }
4477 break;
4478 case T_ColumnRef:
4479 /* we assume the fields contain nothing interesting */
4480 break;
4481 case T_FuncCall:
4482 {
4483 FuncCall *fcall = (FuncCall *) node;
4484
4485 if (WALK(fcall->args))
4486 return true;
4487 if (WALK(fcall->agg_order))
4488 return true;
4489 if (WALK(fcall->agg_filter))
4490 return true;
4491 if (WALK(fcall->over))
4492 return true;
4493 /* function name is deemed uninteresting */
4494 }
4495 break;
4496 case T_NamedArgExpr:
4497 return WALK(((NamedArgExpr *) node)->arg);
4498 case T_A_Indices:
4499 {
4500 A_Indices *indices = (A_Indices *) node;
4501
4502 if (WALK(indices->lidx))
4503 return true;
4504 if (WALK(indices->uidx))
4505 return true;
4506 }
4507 break;
4508 case T_A_Indirection:
4509 {
4510 A_Indirection *indir = (A_Indirection *) node;
4511
4512 if (WALK(indir->arg))
4513 return true;
4514 if (WALK(indir->indirection))
4515 return true;
4516 }
4517 break;
4518 case T_A_ArrayExpr:
4519 return WALK(((A_ArrayExpr *) node)->elements);
4520 case T_ResTarget:
4521 {
4522 ResTarget *rt = (ResTarget *) node;
4523
4524 if (WALK(rt->indirection))
4525 return true;
4526 if (WALK(rt->val))
4527 return true;
4528 }
4529 break;
4530 case T_MultiAssignRef:
4531 return WALK(((MultiAssignRef *) node)->source);
4532 case T_TypeCast:
4533 {
4534 TypeCast *tc = (TypeCast *) node;
4535
4536 if (WALK(tc->arg))
4537 return true;
4538 if (WALK(tc->typeName))
4539 return true;
4540 }
4541 break;
4542 case T_CollateClause:
4543 return WALK(((CollateClause *) node)->arg);
4544 case T_SortBy:
4545 return WALK(((SortBy *) node)->node);
4546 case T_WindowDef:
4547 {
4548 WindowDef *wd = (WindowDef *) node;
4549
4550 if (WALK(wd->partitionClause))
4551 return true;
4552 if (WALK(wd->orderClause))
4553 return true;
4554 if (WALK(wd->startOffset))
4555 return true;
4556 if (WALK(wd->endOffset))
4557 return true;
4558 }
4559 break;
4560 case T_RangeSubselect:
4561 {
4562 RangeSubselect *rs = (RangeSubselect *) node;
4563
4564 if (WALK(rs->subquery))
4565 return true;
4566 if (WALK(rs->alias))
4567 return true;
4568 }
4569 break;
4570 case T_RangeFunction:
4571 {
4572 RangeFunction *rf = (RangeFunction *) node;
4573
4574 if (WALK(rf->functions))
4575 return true;
4576 if (WALK(rf->alias))
4577 return true;
4578 if (WALK(rf->coldeflist))
4579 return true;
4580 }
4581 break;
4582 case T_RangeTableSample:
4583 {
4585
4586 if (WALK(rts->relation))
4587 return true;
4588 /* method name is deemed uninteresting */
4589 if (WALK(rts->args))
4590 return true;
4591 if (WALK(rts->repeatable))
4592 return true;
4593 }
4594 break;
4595 case T_RangeTableFunc:
4596 {
4597 RangeTableFunc *rtf = (RangeTableFunc *) node;
4598
4599 if (WALK(rtf->docexpr))
4600 return true;
4601 if (WALK(rtf->rowexpr))
4602 return true;
4603 if (WALK(rtf->namespaces))
4604 return true;
4605 if (WALK(rtf->columns))
4606 return true;
4607 if (WALK(rtf->alias))
4608 return true;
4609 }
4610 break;
4612 {
4614
4615 if (WALK(rtfc->colexpr))
4616 return true;
4617 if (WALK(rtfc->coldefexpr))
4618 return true;
4619 }
4620 break;
4621 case T_RangeGraphTable:
4622 {
4624
4625 if (WALK(rgt->graph_pattern))
4626 return true;
4627 if (WALK(rgt->columns))
4628 return true;
4629 if (WALK(rgt->alias))
4630 return true;
4631 }
4632 break;
4633 case T_TypeName:
4634 {
4635 TypeName *tn = (TypeName *) node;
4636
4637 if (WALK(tn->typmods))
4638 return true;
4639 if (WALK(tn->arrayBounds))
4640 return true;
4641 /* type name itself is deemed uninteresting */
4642 }
4643 break;
4644 case T_ColumnDef:
4645 {
4646 ColumnDef *coldef = (ColumnDef *) node;
4647
4648 if (WALK(coldef->typeName))
4649 return true;
4650 if (WALK(coldef->raw_default))
4651 return true;
4652 if (WALK(coldef->collClause))
4653 return true;
4654 /* for now, constraints are ignored */
4655 }
4656 break;
4657 case T_IndexElem:
4658 {
4659 IndexElem *indelem = (IndexElem *) node;
4660
4661 if (WALK(indelem->expr))
4662 return true;
4663 /* collation and opclass names are deemed uninteresting */
4664 }
4665 break;
4666 case T_GroupingSet:
4667 return WALK(((GroupingSet *) node)->content);
4668 case T_LockingClause:
4669 return WALK(((LockingClause *) node)->lockedRels);
4670 case T_XmlSerialize:
4671 {
4672 XmlSerialize *xs = (XmlSerialize *) node;
4673
4674 if (WALK(xs->expr))
4675 return true;
4676 if (WALK(xs->typeName))
4677 return true;
4678 }
4679 break;
4680 case T_WithClause:
4681 return WALK(((WithClause *) node)->ctes);
4682 case T_InferClause:
4683 {
4684 InferClause *stmt = (InferClause *) node;
4685
4686 if (WALK(stmt->indexElems))
4687 return true;
4688 if (WALK(stmt->whereClause))
4689 return true;
4690 }
4691 break;
4692 case T_OnConflictClause:
4693 {
4695
4696 if (WALK(stmt->infer))
4697 return true;
4698 if (WALK(stmt->targetList))
4699 return true;
4700 if (WALK(stmt->whereClause))
4701 return true;
4702 }
4703 break;
4704 case T_CommonTableExpr:
4705 /* search_clause and cycle_clause are not interesting here */
4706 return WALK(((CommonTableExpr *) node)->ctequery);
4707 case T_JsonOutput:
4708 {
4709 JsonOutput *out = (JsonOutput *) node;
4710
4711 if (WALK(out->typeName))
4712 return true;
4713 if (WALK(out->returning))
4714 return true;
4715 }
4716 break;
4717 case T_JsonKeyValue:
4718 {
4719 JsonKeyValue *jkv = (JsonKeyValue *) node;
4720
4721 if (WALK(jkv->key))
4722 return true;
4723 if (WALK(jkv->value))
4724 return true;
4725 }
4726 break;
4728 {
4730
4731 if (WALK(joc->output))
4732 return true;
4733 if (WALK(joc->exprs))
4734 return true;
4735 }
4736 break;
4738 {
4740
4741 if (WALK(jac->output))
4742 return true;
4743 if (WALK(jac->exprs))
4744 return true;
4745 }
4746 break;
4748 {
4750
4751 if (WALK(ctor->output))
4752 return true;
4753 if (WALK(ctor->agg_order))
4754 return true;
4755 if (WALK(ctor->agg_filter))
4756 return true;
4757 if (WALK(ctor->over))
4758 return true;
4759 }
4760 break;
4761 case T_JsonObjectAgg:
4762 {
4763 JsonObjectAgg *joa = (JsonObjectAgg *) node;
4764
4765 if (WALK(joa->constructor))
4766 return true;
4767 if (WALK(joa->arg))
4768 return true;
4769 }
4770 break;
4771 case T_JsonArrayAgg:
4772 {
4773 JsonArrayAgg *jaa = (JsonArrayAgg *) node;
4774
4775 if (WALK(jaa->constructor))
4776 return true;
4777 if (WALK(jaa->arg))
4778 return true;
4779 }
4780 break;
4782 {
4784
4785 if (WALK(jaqc->output))
4786 return true;
4787 if (WALK(jaqc->query))
4788 return true;
4789 }
4790 break;
4792 {
4794
4795 if (WALK(gep->subexpr))
4796 return true;
4797 if (WALK(gep->whereClause))
4798 return true;
4799 }
4800 break;
4801 case T_GraphPattern:
4802 {
4803 GraphPattern *gp = (GraphPattern *) node;
4804
4805 if (WALK(gp->path_pattern_list))
4806 return true;
4807 if (WALK(gp->whereClause))
4808 return true;
4809 }
4810 break;
4811 default:
4812 elog(ERROR, "unrecognized node type: %d",
4813 (int) nodeTag(node));
4814 break;
4815 }
4816 return false;
4817}
4818
4819/*
4820 * planstate_tree_walker --- walk plan state trees
4821 *
4822 * The walker has already visited the current node, and so we need only
4823 * recurse into any sub-nodes it has.
4824 */
4825bool
4828 void *context)
4829{
4830 Plan *plan = planstate->plan;
4831 ListCell *lc;
4832
4833 /* We don't need implicit coercions to Node here */
4834#define PSWALK(n) walker(n, context)
4835
4836 /* Guard against stack overflow due to overly complex plan trees */
4838
4839 /* initPlan-s */
4840 if (planstate_walk_subplans(planstate->initPlan, walker, context))
4841 return true;
4842
4843 /* lefttree */
4844 if (outerPlanState(planstate))
4845 {
4846 if (PSWALK(outerPlanState(planstate)))
4847 return true;
4848 }
4849
4850 /* righttree */
4851 if (innerPlanState(planstate))
4852 {
4853 if (PSWALK(innerPlanState(planstate)))
4854 return true;
4855 }
4856
4857 /* special child plans */
4858 switch (nodeTag(plan))
4859 {
4860 case T_Append:
4861 if (planstate_walk_members(((AppendState *) planstate)->appendplans,
4862 ((AppendState *) planstate)->as_nplans,
4863 walker, context))
4864 return true;
4865 break;
4866 case T_MergeAppend:
4867 if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
4868 ((MergeAppendState *) planstate)->ms_nplans,
4869 walker, context))
4870 return true;
4871 break;
4872 case T_BitmapAnd:
4873 if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
4874 ((BitmapAndState *) planstate)->nplans,
4875 walker, context))
4876 return true;
4877 break;
4878 case T_BitmapOr:
4879 if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
4880 ((BitmapOrState *) planstate)->nplans,
4881 walker, context))
4882 return true;
4883 break;
4884 case T_SubqueryScan:
4885 if (PSWALK(((SubqueryScanState *) planstate)->subplan))
4886 return true;
4887 break;
4888 case T_CustomScan:
4889 foreach(lc, ((CustomScanState *) planstate)->custom_ps)
4890 {
4891 if (PSWALK(lfirst(lc)))
4892 return true;
4893 }
4894 break;
4895 default:
4896 break;
4897 }
4898
4899 /* subPlan-s */
4900 if (planstate_walk_subplans(planstate->subPlan, walker, context))
4901 return true;
4902
4903 return false;
4904}
4905
4906/*
4907 * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
4908 */
4909static bool
4912 void *context)
4913{
4914 ListCell *lc;
4915
4916 foreach(lc, plans)
4917 {
4919
4920 if (PSWALK(sps->planstate))
4921 return true;
4922 }
4923
4924 return false;
4925}
4926
4927/*
4928 * Walk the constituent plans of a ModifyTable, Append, MergeAppend,
4929 * BitmapAnd, or BitmapOr node.
4930 */
4931static bool
4934 void *context)
4935{
4936 for (int j = 0; j < nplans; j++)
4937 {
4938 if (PSWALK(planstates[j]))
4939 return true;
4940 }
4941
4942 return false;
4943}
#define Min(x, y)
Definition c.h:1091
#define Assert(condition)
Definition c.h:943
int32_t int32
Definition c.h:620
#define OidIsValid(objectId)
Definition c.h:858
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
#define outerPlanState(node)
Definition execnodes.h:1299
#define innerPlanState(node)
Definition execnodes.h:1298
char * format_type_be(Oid type_oid)
#define stmt
long val
Definition informix.c:689
int j
Definition isn.c:78
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_copy(const List *oldlist)
Definition list.c:1573
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3129
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1505
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition lsyscache.c:3096
Oid get_promoted_array_type(Oid typid)
Definition lsyscache.c:3033
bool query_tree_walker_impl(Query *query, tree_walker_callback walker, void *context, int flags)
Definition nodeFuncs.c:2740
#define FLATCOPY(newnode, node, nodetype)
#define LIST_WALK(l)
bool raw_expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context)
Definition nodeFuncs.c:4071
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
bool range_table_entry_walker_impl(RangeTblEntry *rte, tree_walker_callback walker, void *context, int flags)
Definition nodeFuncs.c:2866
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition nodeFuncs.c:562
void exprSetCollation(Node *expr, Oid collation)
Definition nodeFuncs.c:1132
Oid exprInputCollation(const Node *expr)
Definition nodeFuncs.c:1084
#define WALK(n)
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
static bool planstate_walk_subplans(List *plans, planstate_tree_walker_callback walker, void *context)
Definition nodeFuncs.c:4910
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition nodeFuncs.c:1917
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
static bool fix_opfuncids_walker(Node *node, void *context)
Definition nodeFuncs.c:1855
void exprSetInputCollation(Node *expr, Oid inputcollation)
Definition nodeFuncs.c:1328
bool query_or_expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context, int flags)
Definition nodeFuncs.c:4017
bool expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context)
Definition nodeFuncs.c:2100
bool range_table_walker_impl(List *rtable, tree_walker_callback walker, void *context, int flags)
Definition nodeFuncs.c:2845
Node * query_or_expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *context, int flags)
Definition nodeFuncs.c:4040
Query * query_tree_mutator_impl(Query *query, tree_mutator_callback mutator, void *context, int flags)
Definition nodeFuncs.c:3843
#define MUTATE(newfield, oldfield, fieldtype)
#define PSWALK(n)
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
Definition nodeFuncs.c:641
static int leftmostLoc(int loc1, int loc2)
Definition nodeFuncs.c:1827
Node * strip_implicit_coercions(Node *node)
Definition nodeFuncs.c:710
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1392
List * range_table_mutator_impl(List *rtable, tree_mutator_callback mutator, void *context, int flags)
Definition nodeFuncs.c:3935
bool expression_returns_set(Node *clause)
Definition nodeFuncs.c:768
bool planstate_tree_walker_impl(PlanState *planstate, planstate_tree_walker_callback walker, void *context)
Definition nodeFuncs.c:4826
void fix_opfuncids(Node *node)
Definition nodeFuncs.c:1848
Node * expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *context)
Definition nodeFuncs.c:3000
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition nodeFuncs.c:694
static bool expression_returns_set_walker(Node *node, void *context)
Definition nodeFuncs.c:774
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition nodeFuncs.c:1890
static bool planstate_walk_members(PlanState **planstates, int nplans, planstate_tree_walker_callback walker, void *context)
Definition nodeFuncs.c:4932
void set_opfuncid(OpExpr *opexpr)
Definition nodeFuncs.c:1879
Node *(* tree_mutator_callback)(Node *node, void *context)
Definition nodeFuncs.h:43
#define QTW_DONT_COPY_QUERY
Definition nodeFuncs.h:29
#define QTW_IGNORE_CTE_SUBQUERIES
Definition nodeFuncs.h:23
#define QTW_IGNORE_RT_SUBQUERIES
Definition nodeFuncs.h:22
#define range_table_walker(rt, w, c, f)
Definition nodeFuncs.h:163
#define query_tree_walker(q, w, c, f)
Definition nodeFuncs.h:158
#define range_table_entry_walker(r, w, c, f)
Definition nodeFuncs.h:168
#define QTW_EXAMINE_RTES_AFTER
Definition nodeFuncs.h:28
#define QTW_EXAMINE_SORTGROUP
Definition nodeFuncs.h:30
bool(* tree_walker_callback)(Node *node, void *context)
Definition nodeFuncs.h:38
#define QTW_IGNORE_GROUPEXPRS
Definition nodeFuncs.h:32
#define expression_tree_walker(n, w, c)
Definition nodeFuncs.h:153
#define query_tree_mutator(q, m, c, f)
Definition nodeFuncs.h:160
bool(* check_function_callback)(Oid func_id, void *context)
Definition nodeFuncs.h:35
bool(* planstate_tree_walker_callback)(PlanState *planstate, void *context)
Definition nodeFuncs.h:39
#define QTW_EXAMINE_RTES_BEFORE
Definition nodeFuncs.h:27
#define range_table_mutator(rt, m, c, f)
Definition nodeFuncs.h:165
#define QTW_IGNORE_RANGE_TABLE
Definition nodeFuncs.h:26
#define QTW_IGNORE_JOINALIASES
Definition nodeFuncs.h:25
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define nodeTag(nodeptr)
Definition nodes.h:139
#define makeNode(_type_)
Definition nodes.h:161
static char * errmsg
@ RTE_JOIN
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_RESULT
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_GROUP
@ RTE_GRAPH_TABLE
@ RTE_RELATION
static char format
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial_node(type, l)
Definition pg_list.h:181
#define NIL
Definition pg_list.h:68
#define for_each_from(cell, lst, N)
Definition pg_list.h:446
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
#define lfirst_oid(lc)
Definition pg_list.h:174
#define plan(x)
Definition pg_regress.c:164
static rewind_source * source
Definition pg_rewind.c:89
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
#define InvalidOid
unsigned int Oid
char * c
e
static int fb(int x)
static int fc(const char *x)
@ ARRAY_SUBLINK
Definition primnodes.h:1036
@ MULTIEXPR_SUBLINK
Definition primnodes.h:1035
@ EXPR_SUBLINK
Definition primnodes.h:1034
@ IS_DOCUMENT
Definition primnodes.h:1614
@ IS_XMLSERIALIZE
Definition primnodes.h:1613
CoercionForm
Definition primnodes.h:766
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCE_EXPLICIT_CAST
Definition primnodes.h:768
void check_stack_depth(void)
Definition stack_depth.c:95
Node * lexpr
Definition parsenodes.h:356
Node * rexpr
Definition parsenodes.h:357
Node * uidx
Definition parsenodes.h:490
Node * lidx
Definition parsenodes.h:489
Oid aggfnoid
Definition primnodes.h:464
List * aggdistinct
Definition primnodes.h:494
List * aggdirectargs
Definition primnodes.h:485
List * args
Definition primnodes.h:488
Expr * aggfilter
Definition primnodes.h:497
List * aggorder
Definition primnodes.h:491
ParseLoc location
Definition primnodes.h:1277
List * args
Definition primnodes.h:973
Node * cycle_mark_default
Node * cycle_mark_value
Expr * defresult
Definition primnodes.h:1350
List * args
Definition primnodes.h:1349
Expr * result
Definition primnodes.h:1361
ParseLoc location
Definition primnodes.h:2064
ParseLoc location
Definition primnodes.h:1249
Oid consttype
Definition primnodes.h:330
List * newvals
Definition primnodes.h:1195
Expr * arg
Definition primnodes.h:1194
Node * quals
Definition primnodes.h:2385
List * fromlist
Definition primnodes.h:2384
Node * agg_filter
Definition parsenodes.h:457
List * agg_order
Definition parsenodes.h:456
List * args
Definition parsenodes.h:455
struct WindowDef * over
Definition parsenodes.h:458
Oid funcid
Definition primnodes.h:783
List * args
Definition primnodes.h:801
RangeVar * rel
Definition primnodes.h:164
Node * quals
Definition primnodes.h:2365
Node * larg
Definition primnodes.h:2358
Node * rarg
Definition primnodes.h:2359
Node * formatted_expr
Definition primnodes.h:1851
ParseLoc location
Definition primnodes.h:1887
Oid collation
Definition primnodes.h:1884
JsonFormat * format
Definition primnodes.h:1763
JsonReturning * returning
TypeName * typeName
JsonBehavior * on_error
List * columns
JsonTablePathSpec * pathspec
List * passing
JsonValueExpr * context_item
Definition pg_list.h:54
Definition nodes.h:135
List * arbiterElems
Definition primnodes.h:2403
List * onConflictSet
Definition primnodes.h:2412
List * exclRelTlist
Definition primnodes.h:2417
Node * onConflictWhere
Definition primnodes.h:2415
Node * arbiterWhere
Definition primnodes.h:2405
Oid opno
Definition primnodes.h:851
List * args
Definition primnodes.h:869
ParseLoc location
Definition primnodes.h:872
Plan * plan
Definition execnodes.h:1201
List * subPlan
Definition execnodes.h:1230
List * initPlan
Definition execnodes.h:1228
Node * mergeJoinCondition
Definition parsenodes.h:199
Node * limitCount
Definition parsenodes.h:234
FromExpr * jointree
Definition parsenodes.h:185
List * returningList
Definition parsenodes.h:217
Node * setOperations
Definition parsenodes.h:239
List * cteList
Definition parsenodes.h:176
OnConflictExpr * onConflict
Definition parsenodes.h:206
ForPortionOfExpr * forPortionOf
Definition parsenodes.h:151
List * groupClause
Definition parsenodes.h:219
Node * havingQual
Definition parsenodes.h:225
List * rtable
Definition parsenodes.h:178
Node * limitOffset
Definition parsenodes.h:233
List * mergeActionList
Definition parsenodes.h:188
List * windowClause
Definition parsenodes.h:227
List * targetList
Definition parsenodes.h:201
List * distinctClause
Definition parsenodes.h:229
List * sortClause
Definition parsenodes.h:231
ParseLoc location
Definition primnodes.h:1227
List * args
Definition primnodes.h:1450
List * args
Definition primnodes.h:1125
Node * testexpr
Definition primnodes.h:1100
int32 firstColTypmod
Definition primnodes.h:1108
Oid firstColCollation
Definition primnodes.h:1109
SubLinkType subLinkType
Definition primnodes.h:1098
Oid firstColType
Definition primnodes.h:1107
Expr * refassgnexpr
Definition primnodes.h:736
List * refupperindexpr
Definition primnodes.h:726
List * reflowerindexpr
Definition primnodes.h:732
Node * docexpr
Definition primnodes.h:121
Node * rowexpr
Definition primnodes.h:123
List * colexprs
Definition primnodes.h:133
Node * startOffset
List * partitionClause
Node * endOffset
List * orderClause
List * args
Definition primnodes.h:606
Expr * aggfilter
Definition primnodes.h:608
List * args
Definition primnodes.h:1635
ParseLoc location
Definition primnodes.h:1644
List * named_args
Definition primnodes.h:1631
Definition type.h:89
const char * type