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;
2583 {
2585
2586 if (WALK(opstep->exprs))
2587 return true;
2588 }
2589 break;
2591 /* no expression subnodes */
2592 break;
2593 case T_JoinExpr:
2594 {
2595 JoinExpr *join = (JoinExpr *) node;
2596
2597 if (WALK(join->larg))
2598 return true;
2599 if (WALK(join->rarg))
2600 return true;
2601 if (WALK(join->quals))
2602 return true;
2603
2604 /*
2605 * alias clause, using list are deemed uninteresting.
2606 */
2607 }
2608 break;
2609 case T_SetOperationStmt:
2610 {
2611 SetOperationStmt *setop = (SetOperationStmt *) node;
2612
2613 if (WALK(setop->larg))
2614 return true;
2615 if (WALK(setop->rarg))
2616 return true;
2617
2618 /* groupClauses are deemed uninteresting */
2619 }
2620 break;
2621 case T_IndexClause:
2622 {
2623 IndexClause *iclause = (IndexClause *) node;
2624
2625 if (WALK(iclause->rinfo))
2626 return true;
2627 if (LIST_WALK(iclause->indexquals))
2628 return true;
2629 }
2630 break;
2631 case T_PlaceHolderVar:
2632 return WALK(((PlaceHolderVar *) node)->phexpr);
2633 case T_InferenceElem:
2634 return WALK(((InferenceElem *) node)->expr);
2635 case T_ReturningExpr:
2636 return WALK(((ReturningExpr *) node)->retexpr);
2637 case T_AppendRelInfo:
2638 {
2640
2641 if (LIST_WALK(appinfo->translated_vars))
2642 return true;
2643 }
2644 break;
2645 case T_PlaceHolderInfo:
2646 return WALK(((PlaceHolderInfo *) node)->ph_var);
2647 case T_RangeTblFunction:
2648 return WALK(((RangeTblFunction *) node)->funcexpr);
2650 {
2652
2653 if (LIST_WALK(tsc->args))
2654 return true;
2655 if (WALK(tsc->repeatable))
2656 return true;
2657 }
2658 break;
2659 case T_TableFunc:
2660 {
2661 TableFunc *tf = (TableFunc *) node;
2662
2663 if (WALK(tf->ns_uris))
2664 return true;
2665 if (WALK(tf->docexpr))
2666 return true;
2667 if (WALK(tf->rowexpr))
2668 return true;
2669 if (WALK(tf->colexprs))
2670 return true;
2671 if (WALK(tf->coldefexprs))
2672 return true;
2673 if (WALK(tf->colvalexprs))
2674 return true;
2675 if (WALK(tf->passingvalexprs))
2676 return true;
2677 }
2678 break;
2680 {
2682
2683 if (WALK(gep->subexpr))
2684 return true;
2685 if (WALK(gep->whereClause))
2686 return true;
2687 }
2688 break;
2689 case T_GraphPattern:
2690 {
2691 GraphPattern *gp = (GraphPattern *) node;
2692
2693 if (LIST_WALK(gp->path_pattern_list))
2694 return true;
2695 if (WALK(gp->whereClause))
2696 return true;
2697 }
2698 break;
2699 default:
2700 elog(ERROR, "unrecognized node type: %d",
2701 (int) nodeTag(node));
2702 break;
2703 }
2704 return false;
2705
2706 /* The WALK() macro can be re-used below, but LIST_WALK() not so much */
2707#undef LIST_WALK
2708}
2709
2710/*
2711 * query_tree_walker --- initiate a walk of a Query's expressions
2712 *
2713 * This routine exists just to reduce the number of places that need to know
2714 * where all the expression subtrees of a Query are. Note it can be used
2715 * for starting a walk at top level of a Query regardless of whether the
2716 * walker intends to descend into subqueries. It is also useful for
2717 * descending into subqueries within a walker.
2718 *
2719 * Some callers want to suppress visitation of certain items in the sub-Query,
2720 * typically because they need to process them specially, or don't actually
2721 * want to recurse into subqueries. This is supported by the flags argument,
2722 * which is the bitwise OR of flag values to add or suppress visitation of
2723 * indicated items. (More flag bits may be added as needed.)
2724 */
2725bool
2728 void *context,
2729 int flags)
2730{
2731 Assert(query != NULL && IsA(query, Query));
2732
2733 /*
2734 * We don't walk any utilityStmt here. However, we can't easily assert
2735 * that it is absent, since there are at least two code paths by which
2736 * action statements from CREATE RULE end up here, and NOTIFY is allowed
2737 * in a rule action.
2738 */
2739
2740 if (WALK(query->targetList))
2741 return true;
2742 if (WALK(query->withCheckOptions))
2743 return true;
2744 if (WALK(query->onConflict))
2745 return true;
2746 if (WALK(query->mergeActionList))
2747 return true;
2748 if (WALK(query->mergeJoinCondition))
2749 return true;
2750 if (WALK(query->returningList))
2751 return true;
2752 if (WALK(query->jointree))
2753 return true;
2754 if (WALK(query->setOperations))
2755 return true;
2756 if (WALK(query->havingQual))
2757 return true;
2758 if (WALK(query->limitOffset))
2759 return true;
2760 if (WALK(query->limitCount))
2761 return true;
2762
2763 /*
2764 * Most callers aren't interested in SortGroupClause nodes since those
2765 * don't contain actual expressions. However they do contain OIDs which
2766 * may be needed by dependency walkers etc.
2767 */
2768 if ((flags & QTW_EXAMINE_SORTGROUP))
2769 {
2770 if (WALK(query->groupClause))
2771 return true;
2772 if (WALK(query->windowClause))
2773 return true;
2774 if (WALK(query->sortClause))
2775 return true;
2776 if (WALK(query->distinctClause))
2777 return true;
2778 }
2779 else
2780 {
2781 /*
2782 * But we need to walk the expressions under WindowClause nodes even
2783 * if we're not interested in SortGroupClause nodes.
2784 */
2785 ListCell *lc;
2786
2787 foreach(lc, query->windowClause)
2788 {
2790
2791 if (WALK(wc->startOffset))
2792 return true;
2793 if (WALK(wc->endOffset))
2794 return true;
2795 }
2796 }
2797
2798 /*
2799 * groupingSets and rowMarks are not walked:
2800 *
2801 * groupingSets contain only ressortgrouprefs (integers) which are
2802 * meaningless without the corresponding groupClause or tlist.
2803 * Accordingly, any walker that needs to care about them needs to handle
2804 * them itself in its Query processing.
2805 *
2806 * rowMarks is not walked because it contains only rangetable indexes (and
2807 * flags etc.) and therefore should be handled at Query level similarly.
2808 */
2809
2810 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2811 {
2812 if (WALK(query->cteList))
2813 return true;
2814 }
2815 if (!(flags & QTW_IGNORE_RANGE_TABLE))
2816 {
2817 if (range_table_walker(query->rtable, walker, context, flags))
2818 return true;
2819 }
2820 return false;
2821}
2822
2823/*
2824 * range_table_walker is just the part of query_tree_walker that scans
2825 * a query's rangetable. This is split out since it can be useful on
2826 * its own.
2827 */
2828bool
2831 void *context,
2832 int flags)
2833{
2834 ListCell *rt;
2835
2836 foreach(rt, rtable)
2837 {
2839
2840 if (range_table_entry_walker(rte, walker, context, flags))
2841 return true;
2842 }
2843 return false;
2844}
2845
2846/*
2847 * Some callers even want to scan the expressions in individual RTEs.
2848 */
2849bool
2852 void *context,
2853 int flags)
2854{
2855 /*
2856 * Walkers might need to examine the RTE node itself either before or
2857 * after visiting its contents (or, conceivably, both). Note that if you
2858 * specify neither flag, the walker won't be called on the RTE at all.
2859 */
2860 if (flags & QTW_EXAMINE_RTES_BEFORE)
2861 if (WALK(rte))
2862 return true;
2863
2864 switch (rte->rtekind)
2865 {
2866 case RTE_RELATION:
2867 if (WALK(rte->tablesample))
2868 return true;
2869 break;
2870 case RTE_SUBQUERY:
2871 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2872 if (WALK(rte->subquery))
2873 return true;
2874 break;
2875 case RTE_JOIN:
2876 if (!(flags & QTW_IGNORE_JOINALIASES))
2877 if (WALK(rte->joinaliasvars))
2878 return true;
2879 break;
2880 case RTE_FUNCTION:
2881 if (WALK(rte->functions))
2882 return true;
2883 break;
2884 case RTE_TABLEFUNC:
2885 if (WALK(rte->tablefunc))
2886 return true;
2887 break;
2888 case RTE_VALUES:
2889 if (WALK(rte->values_lists))
2890 return true;
2891 break;
2892 case RTE_GRAPH_TABLE:
2893 if (WALK(rte->graph_pattern))
2894 return true;
2895 if (WALK(rte->graph_table_columns))
2896 return true;
2897 break;
2898 case RTE_CTE:
2900 case RTE_RESULT:
2901 /* nothing to do */
2902 break;
2903 case RTE_GROUP:
2904 if (!(flags & QTW_IGNORE_GROUPEXPRS))
2905 if (WALK(rte->groupexprs))
2906 return true;
2907 break;
2908 }
2909
2910 if (WALK(rte->securityQuals))
2911 return true;
2912
2913 if (flags & QTW_EXAMINE_RTES_AFTER)
2914 if (WALK(rte))
2915 return true;
2916
2917 return false;
2918}
2919
2920
2921/*
2922 * expression_tree_mutator() is designed to support routines that make a
2923 * modified copy of an expression tree, with some nodes being added,
2924 * removed, or replaced by new subtrees. The original tree is (normally)
2925 * not changed. Each recursion level is responsible for returning a copy of
2926 * (or appropriately modified substitute for) the subtree it is handed.
2927 * A mutator routine should look like this:
2928 *
2929 * Node * my_mutator (Node *node, my_struct *context)
2930 * {
2931 * if (node == NULL)
2932 * return NULL;
2933 * // check for nodes that special work is required for, eg:
2934 * if (IsA(node, Var))
2935 * {
2936 * ... create and return modified copy of Var node
2937 * }
2938 * else if (IsA(node, ...))
2939 * {
2940 * ... do special transformations of other node types
2941 * }
2942 * // for any node type not specially processed, do:
2943 * return expression_tree_mutator(node, my_mutator, context);
2944 * }
2945 *
2946 * The "context" argument points to a struct that holds whatever context
2947 * information the mutator routine needs --- it can be used to return extra
2948 * data gathered by the mutator, too. This argument is not touched by
2949 * expression_tree_mutator, but it is passed down to recursive sub-invocations
2950 * of my_mutator. The tree walk is started from a setup routine that
2951 * fills in the appropriate context struct, calls my_mutator with the
2952 * top-level node of the tree, and does any required post-processing.
2953 *
2954 * Each level of recursion must return an appropriately modified Node.
2955 * If expression_tree_mutator() is called, it will make an exact copy
2956 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
2957 * of that Node. In this way, my_mutator() has full control over the
2958 * copying process but need not directly deal with expression trees
2959 * that it has no interest in.
2960 *
2961 * Just as for expression_tree_walker, the node types handled by
2962 * expression_tree_mutator include all those normally found in target lists
2963 * and qualifier clauses during the planning stage.
2964 *
2965 * expression_tree_mutator will handle SubLink nodes by recursing normally
2966 * into the "testexpr" subtree (which is an expression belonging to the outer
2967 * plan). It will also call the mutator on the sub-Query node; however, when
2968 * expression_tree_mutator itself is called on a Query node, it does nothing
2969 * and returns the unmodified Query node. The net effect is that unless the
2970 * mutator does something special at a Query node, sub-selects will not be
2971 * visited or modified; the original sub-select will be linked to by the new
2972 * SubLink node. Mutators that want to descend into sub-selects will usually
2973 * do so by recognizing Query nodes and calling query_tree_mutator (below).
2974 *
2975 * expression_tree_mutator will handle a SubPlan node by recursing into the
2976 * "testexpr" and the "args" list (which belong to the outer plan), but it
2977 * will simply copy the link to the inner plan, since that's typically what
2978 * expression tree mutators want. A mutator that wants to modify the subplan
2979 * can force appropriate behavior by recognizing SubPlan expression nodes
2980 * and doing the right thing.
2981 */
2982
2983Node *
2986 void *context)
2987{
2988 /*
2989 * The mutator has already decided not to modify the current node, but we
2990 * must call the mutator for any sub-nodes.
2991 */
2992
2993#define FLATCOPY(newnode, node, nodetype) \
2994 ( (newnode) = palloc_object(nodetype), \
2995 memcpy((newnode), (node), sizeof(nodetype)) )
2996
2997#define MUTATE(newfield, oldfield, fieldtype) \
2998 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
2999
3000 if (node == NULL)
3001 return NULL;
3002
3003 /* Guard against stack overflow due to overly complex expressions */
3005
3006 switch (nodeTag(node))
3007 {
3008 /*
3009 * Primitive node types with no expression subnodes. Var and
3010 * Const are frequent enough to deserve special cases, the others
3011 * we just use copyObject for.
3012 */
3013 case T_Var:
3014 {
3015 Var *var = (Var *) node;
3016 Var *newnode;
3017
3018 FLATCOPY(newnode, var, Var);
3019 /* Assume we need not copy the varnullingrels bitmapset */
3020 return (Node *) newnode;
3021 }
3022 break;
3023 case T_Const:
3024 {
3025 Const *oldnode = (Const *) node;
3026 Const *newnode;
3027
3029 /* XXX we don't bother with datumCopy; should we? */
3030 return (Node *) newnode;
3031 }
3032 break;
3033 case T_Param:
3034 case T_CaseTestExpr:
3035 case T_SQLValueFunction:
3036 case T_JsonFormat:
3038 case T_SetToDefault:
3039 case T_CurrentOfExpr:
3040 case T_NextValueExpr:
3041 case T_RangeTblRef:
3042 case T_SortGroupClause:
3043 case T_CTESearchClause:
3044 case T_MergeSupportFunc:
3045 return copyObject(node);
3046 case T_WithCheckOption:
3047 {
3050
3052 MUTATE(newnode->qual, wco->qual, Node *);
3053 return (Node *) newnode;
3054 }
3055 case T_Aggref:
3056 {
3057 Aggref *aggref = (Aggref *) node;
3058 Aggref *newnode;
3059
3060 FLATCOPY(newnode, aggref, Aggref);
3061 /* assume mutation doesn't change types of arguments */
3062 newnode->aggargtypes = list_copy(aggref->aggargtypes);
3063 MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
3064 MUTATE(newnode->args, aggref->args, List *);
3065 MUTATE(newnode->aggorder, aggref->aggorder, List *);
3066 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
3067 MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
3068 return (Node *) newnode;
3069 }
3070 break;
3071 case T_GroupingFunc:
3072 {
3075
3077 MUTATE(newnode->args, grouping->args, List *);
3078
3079 /*
3080 * We assume here that mutating the arguments does not change
3081 * the semantics, i.e. that the arguments are not mutated in a
3082 * way that makes them semantically different from their
3083 * previously matching expressions in the GROUP BY clause.
3084 *
3085 * If a mutator somehow wanted to do this, it would have to
3086 * handle the refs and cols lists itself as appropriate.
3087 */
3088 newnode->refs = list_copy(grouping->refs);
3089 newnode->cols = list_copy(grouping->cols);
3090
3091 return (Node *) newnode;
3092 }
3093 break;
3094 case T_WindowFunc:
3095 {
3096 WindowFunc *wfunc = (WindowFunc *) node;
3098
3099 FLATCOPY(newnode, wfunc, WindowFunc);
3100 MUTATE(newnode->args, wfunc->args, List *);
3101 MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
3102 return (Node *) newnode;
3103 }
3104 break;
3106 {
3109
3111 MUTATE(newnode->arg, wfuncrc->arg, Expr *);
3112 return (Node *) newnode;
3113 }
3114 break;
3115 case T_SubscriptingRef:
3116 {
3117 SubscriptingRef *sbsref = (SubscriptingRef *) node;
3119
3121 MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
3122 List *);
3123 MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
3124 List *);
3125 MUTATE(newnode->refexpr, sbsref->refexpr,
3126 Expr *);
3127 MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
3128 Expr *);
3129
3130 return (Node *) newnode;
3131 }
3132 break;
3133 case T_FuncExpr:
3134 {
3135 FuncExpr *expr = (FuncExpr *) node;
3137
3138 FLATCOPY(newnode, expr, FuncExpr);
3139 MUTATE(newnode->args, expr->args, List *);
3140 return (Node *) newnode;
3141 }
3142 break;
3143 case T_NamedArgExpr:
3144 {
3145 NamedArgExpr *nexpr = (NamedArgExpr *) node;
3147
3149 MUTATE(newnode->arg, nexpr->arg, Expr *);
3150 return (Node *) newnode;
3151 }
3152 break;
3153 case T_OpExpr:
3154 {
3155 OpExpr *expr = (OpExpr *) node;
3156 OpExpr *newnode;
3157
3158 FLATCOPY(newnode, expr, OpExpr);
3159 MUTATE(newnode->args, expr->args, List *);
3160 return (Node *) newnode;
3161 }
3162 break;
3163 case T_DistinctExpr:
3164 {
3165 DistinctExpr *expr = (DistinctExpr *) node;
3167
3169 MUTATE(newnode->args, expr->args, List *);
3170 return (Node *) newnode;
3171 }
3172 break;
3173 case T_NullIfExpr:
3174 {
3175 NullIfExpr *expr = (NullIfExpr *) node;
3177
3178 FLATCOPY(newnode, expr, NullIfExpr);
3179 MUTATE(newnode->args, expr->args, List *);
3180 return (Node *) newnode;
3181 }
3182 break;
3184 {
3185 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3187
3189 MUTATE(newnode->args, expr->args, List *);
3190 return (Node *) newnode;
3191 }
3192 break;
3193 case T_BoolExpr:
3194 {
3195 BoolExpr *expr = (BoolExpr *) node;
3197
3198 FLATCOPY(newnode, expr, BoolExpr);
3199 MUTATE(newnode->args, expr->args, List *);
3200 return (Node *) newnode;
3201 }
3202 break;
3203 case T_SubLink:
3204 {
3205 SubLink *sublink = (SubLink *) node;
3207
3209 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
3210
3211 /*
3212 * Also invoke the mutator on the sublink's Query node, so it
3213 * can recurse into the sub-query if it wants to.
3214 */
3215 MUTATE(newnode->subselect, sublink->subselect, Node *);
3216 return (Node *) newnode;
3217 }
3218 break;
3219 case T_SubPlan:
3220 {
3221 SubPlan *subplan = (SubPlan *) node;
3223
3224 FLATCOPY(newnode, subplan, SubPlan);
3225 /* transform testexpr */
3226 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
3227 /* transform args list (params to be passed to subplan) */
3228 MUTATE(newnode->args, subplan->args, List *);
3229 /* but not the sub-Plan itself, which is referenced as-is */
3230 return (Node *) newnode;
3231 }
3232 break;
3234 {
3237
3239 MUTATE(newnode->subplans, asplan->subplans, List *);
3240 return (Node *) newnode;
3241 }
3242 break;
3243 case T_FieldSelect:
3244 {
3245 FieldSelect *fselect = (FieldSelect *) node;
3247
3249 MUTATE(newnode->arg, fselect->arg, Expr *);
3250 return (Node *) newnode;
3251 }
3252 break;
3253 case T_FieldStore:
3254 {
3255 FieldStore *fstore = (FieldStore *) node;
3257
3258 FLATCOPY(newnode, fstore, FieldStore);
3259 MUTATE(newnode->arg, fstore->arg, Expr *);
3260 MUTATE(newnode->newvals, fstore->newvals, List *);
3261 newnode->fieldnums = list_copy(fstore->fieldnums);
3262 return (Node *) newnode;
3263 }
3264 break;
3265 case T_RelabelType:
3266 {
3267 RelabelType *relabel = (RelabelType *) node;
3269
3271 MUTATE(newnode->arg, relabel->arg, Expr *);
3272 return (Node *) newnode;
3273 }
3274 break;
3275 case T_CoerceViaIO:
3276 {
3277 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
3279
3280 FLATCOPY(newnode, iocoerce, CoerceViaIO);
3281 MUTATE(newnode->arg, iocoerce->arg, Expr *);
3282 return (Node *) newnode;
3283 }
3284 break;
3285 case T_ArrayCoerceExpr:
3286 {
3289
3291 MUTATE(newnode->arg, acoerce->arg, Expr *);
3292 MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
3293 return (Node *) newnode;
3294 }
3295 break;
3297 {
3300
3302 MUTATE(newnode->arg, convexpr->arg, Expr *);
3303 return (Node *) newnode;
3304 }
3305 break;
3306 case T_CollateExpr:
3307 {
3308 CollateExpr *collate = (CollateExpr *) node;
3310
3311 FLATCOPY(newnode, collate, CollateExpr);
3312 MUTATE(newnode->arg, collate->arg, Expr *);
3313 return (Node *) newnode;
3314 }
3315 break;
3316 case T_CaseExpr:
3317 {
3318 CaseExpr *caseexpr = (CaseExpr *) node;
3320
3322 MUTATE(newnode->arg, caseexpr->arg, Expr *);
3323 MUTATE(newnode->args, caseexpr->args, List *);
3324 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
3325 return (Node *) newnode;
3326 }
3327 break;
3328 case T_CaseWhen:
3329 {
3330 CaseWhen *casewhen = (CaseWhen *) node;
3332
3334 MUTATE(newnode->expr, casewhen->expr, Expr *);
3335 MUTATE(newnode->result, casewhen->result, Expr *);
3336 return (Node *) newnode;
3337 }
3338 break;
3339 case T_ArrayExpr:
3340 {
3341 ArrayExpr *arrayexpr = (ArrayExpr *) node;
3343
3344 FLATCOPY(newnode, arrayexpr, ArrayExpr);
3345 MUTATE(newnode->elements, arrayexpr->elements, List *);
3346 return (Node *) newnode;
3347 }
3348 break;
3349 case T_RowExpr:
3350 {
3351 RowExpr *rowexpr = (RowExpr *) node;
3353
3354 FLATCOPY(newnode, rowexpr, RowExpr);
3355 MUTATE(newnode->args, rowexpr->args, List *);
3356 /* Assume colnames needn't be duplicated */
3357 return (Node *) newnode;
3358 }
3359 break;
3360 case T_RowCompareExpr:
3361 {
3364
3366 MUTATE(newnode->largs, rcexpr->largs, List *);
3367 MUTATE(newnode->rargs, rcexpr->rargs, List *);
3368 return (Node *) newnode;
3369 }
3370 break;
3371 case T_CoalesceExpr:
3372 {
3375
3377 MUTATE(newnode->args, coalesceexpr->args, List *);
3378 return (Node *) newnode;
3379 }
3380 break;
3381 case T_MinMaxExpr:
3382 {
3383 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3385
3387 MUTATE(newnode->args, minmaxexpr->args, List *);
3388 return (Node *) newnode;
3389 }
3390 break;
3391 case T_XmlExpr:
3392 {
3393 XmlExpr *xexpr = (XmlExpr *) node;
3395
3396 FLATCOPY(newnode, xexpr, XmlExpr);
3397 MUTATE(newnode->named_args, xexpr->named_args, List *);
3398 /* assume mutator does not care about arg_names */
3399 MUTATE(newnode->args, xexpr->args, List *);
3400 return (Node *) newnode;
3401 }
3402 break;
3403 case T_JsonReturning:
3404 {
3405 JsonReturning *jr = (JsonReturning *) node;
3407
3409 MUTATE(newnode->format, jr->format, JsonFormat *);
3410
3411 return (Node *) newnode;
3412 }
3413 case T_JsonValueExpr:
3414 {
3415 JsonValueExpr *jve = (JsonValueExpr *) node;
3417
3419 MUTATE(newnode->raw_expr, jve->raw_expr, Expr *);
3420 MUTATE(newnode->formatted_expr, jve->formatted_expr, Expr *);
3421 MUTATE(newnode->format, jve->format, JsonFormat *);
3422
3423 return (Node *) newnode;
3424 }
3426 {
3429
3431 MUTATE(newnode->args, jce->args, List *);
3432 MUTATE(newnode->func, jce->func, Expr *);
3433 MUTATE(newnode->coercion, jce->coercion, Expr *);
3434 MUTATE(newnode->returning, jce->returning, JsonReturning *);
3435
3436 return (Node *) newnode;
3437 }
3438 case T_JsonIsPredicate:
3439 {
3440 JsonIsPredicate *pred = (JsonIsPredicate *) node;
3442
3444 MUTATE(newnode->expr, pred->expr, Node *);
3445 MUTATE(newnode->format, pred->format, JsonFormat *);
3446
3447 return (Node *) newnode;
3448 }
3449 case T_JsonExpr:
3450 {
3451 JsonExpr *jexpr = (JsonExpr *) node;
3453
3455 MUTATE(newnode->formatted_expr, jexpr->formatted_expr, Node *);
3456 MUTATE(newnode->path_spec, jexpr->path_spec, Node *);
3457 MUTATE(newnode->passing_values, jexpr->passing_values, List *);
3458 /* assume mutator does not care about passing_names */
3459 MUTATE(newnode->on_empty, jexpr->on_empty, JsonBehavior *);
3460 MUTATE(newnode->on_error, jexpr->on_error, JsonBehavior *);
3461 return (Node *) newnode;
3462 }
3463 break;
3464 case T_JsonBehavior:
3465 {
3466 JsonBehavior *behavior = (JsonBehavior *) node;
3468
3469 FLATCOPY(newnode, behavior, JsonBehavior);
3470 MUTATE(newnode->expr, behavior->expr, Node *);
3471 return (Node *) newnode;
3472 }
3473 break;
3474 case T_NullTest:
3475 {
3476 NullTest *ntest = (NullTest *) node;
3478
3480 MUTATE(newnode->arg, ntest->arg, Expr *);
3481 return (Node *) newnode;
3482 }
3483 break;
3484 case T_BooleanTest:
3485 {
3486 BooleanTest *btest = (BooleanTest *) node;
3488
3490 MUTATE(newnode->arg, btest->arg, Expr *);
3491 return (Node *) newnode;
3492 }
3493 break;
3494 case T_CoerceToDomain:
3495 {
3498
3500 MUTATE(newnode->arg, ctest->arg, Expr *);
3501 return (Node *) newnode;
3502 }
3503 break;
3504 case T_ReturningExpr:
3505 {
3506 ReturningExpr *rexpr = (ReturningExpr *) node;
3508
3510 MUTATE(newnode->retexpr, rexpr->retexpr, Expr *);
3511 return (Node *) newnode;
3512 }
3513 break;
3514 case T_TargetEntry:
3515 {
3518
3520 MUTATE(newnode->expr, targetentry->expr, Expr *);
3521 return (Node *) newnode;
3522 }
3523 break;
3524 case T_Query:
3525 /* Do nothing with a sub-Query, per discussion above */
3526 return node;
3527 case T_WindowClause:
3528 {
3529 WindowClause *wc = (WindowClause *) node;
3531
3533 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
3534 MUTATE(newnode->orderClause, wc->orderClause, List *);
3535 MUTATE(newnode->startOffset, wc->startOffset, Node *);
3536 MUTATE(newnode->endOffset, wc->endOffset, Node *);
3537 return (Node *) newnode;
3538 }
3539 break;
3540 case T_CTECycleClause:
3541 {
3542 CTECycleClause *cc = (CTECycleClause *) node;
3544
3546 MUTATE(newnode->cycle_mark_value, cc->cycle_mark_value, Node *);
3547 MUTATE(newnode->cycle_mark_default, cc->cycle_mark_default, Node *);
3548 return (Node *) newnode;
3549 }
3550 break;
3551 case T_CommonTableExpr:
3552 {
3553 CommonTableExpr *cte = (CommonTableExpr *) node;
3555
3557
3558 /*
3559 * Also invoke the mutator on the CTE's Query node, so it can
3560 * recurse into the sub-query if it wants to.
3561 */
3562 MUTATE(newnode->ctequery, cte->ctequery, Node *);
3563
3564 MUTATE(newnode->search_clause, cte->search_clause, CTESearchClause *);
3565 MUTATE(newnode->cycle_clause, cte->cycle_clause, CTECycleClause *);
3566
3567 return (Node *) newnode;
3568 }
3569 break;
3571 {
3574
3576 MUTATE(newnode->listdatums, pbs->listdatums, List *);
3577 MUTATE(newnode->lowerdatums, pbs->lowerdatums, List *);
3578 MUTATE(newnode->upperdatums, pbs->upperdatums, List *);
3579 return (Node *) newnode;
3580 }
3581 break;
3583 {
3586
3588 MUTATE(newnode->value, prd->value, Node *);
3589 return (Node *) newnode;
3590 }
3591 break;
3592 case T_List:
3593 {
3594 /*
3595 * We assume the mutator isn't interested in the list nodes
3596 * per se, so just invoke it on each list element. NOTE: this
3597 * would fail badly on a list with integer elements!
3598 */
3600 ListCell *temp;
3601
3602 resultlist = NIL;
3603 foreach(temp, (List *) node)
3604 {
3606 mutator((Node *) lfirst(temp),
3607 context));
3608 }
3609 return (Node *) resultlist;
3610 }
3611 break;
3612 case T_FromExpr:
3613 {
3614 FromExpr *from = (FromExpr *) node;
3616
3617 FLATCOPY(newnode, from, FromExpr);
3618 MUTATE(newnode->fromlist, from->fromlist, List *);
3619 MUTATE(newnode->quals, from->quals, Node *);
3620 return (Node *) newnode;
3621 }
3622 break;
3623 case T_OnConflictExpr:
3624 {
3625 OnConflictExpr *oc = (OnConflictExpr *) node;
3627
3629 MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
3630 MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
3631 MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
3632 MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
3633 MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
3634
3635 return (Node *) newnode;
3636 }
3637 break;
3638 case T_MergeAction:
3639 {
3640 MergeAction *action = (MergeAction *) node;
3642
3643 FLATCOPY(newnode, action, MergeAction);
3644 MUTATE(newnode->qual, action->qual, Node *);
3645 MUTATE(newnode->targetList, action->targetList, List *);
3646
3647 return (Node *) newnode;
3648 }
3649 break;
3651 {
3654
3656 MUTATE(newnode->exprs, opstep->exprs, List *);
3657
3658 return (Node *) newnode;
3659 }
3660 break;
3662 /* no expression sub-nodes */
3663 return copyObject(node);
3664 case T_JoinExpr:
3665 {
3666 JoinExpr *join = (JoinExpr *) node;
3668
3669 FLATCOPY(newnode, join, JoinExpr);
3670 MUTATE(newnode->larg, join->larg, Node *);
3671 MUTATE(newnode->rarg, join->rarg, Node *);
3672 MUTATE(newnode->quals, join->quals, Node *);
3673 /* We do not mutate alias or using by default */
3674 return (Node *) newnode;
3675 }
3676 break;
3677 case T_SetOperationStmt:
3678 {
3679 SetOperationStmt *setop = (SetOperationStmt *) node;
3681
3683 MUTATE(newnode->larg, setop->larg, Node *);
3684 MUTATE(newnode->rarg, setop->rarg, Node *);
3685 /* We do not mutate groupClauses by default */
3686 return (Node *) newnode;
3687 }
3688 break;
3689 case T_IndexClause:
3690 {
3691 IndexClause *iclause = (IndexClause *) node;
3693
3695 MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
3696 MUTATE(newnode->indexquals, iclause->indexquals, List *);
3697 return (Node *) newnode;
3698 }
3699 break;
3700 case T_PlaceHolderVar:
3701 {
3702 PlaceHolderVar *phv = (PlaceHolderVar *) node;
3704
3706 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
3707 /* Assume we need not copy the relids bitmapsets */
3708 return (Node *) newnode;
3709 }
3710 break;
3711 case T_InferenceElem:
3712 {
3715
3717 MUTATE(newnode->expr, newnode->expr, Node *);
3718 return (Node *) newnode;
3719 }
3720 break;
3721 case T_AppendRelInfo:
3722 {
3725
3727 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
3728 /* Assume nothing need be done with parent_colnos[] */
3729 return (Node *) newnode;
3730 }
3731 break;
3732 case T_PlaceHolderInfo:
3733 {
3736
3738 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
3739 /* Assume we need not copy the relids bitmapsets */
3740 return (Node *) newnode;
3741 }
3742 break;
3743 case T_RangeTblFunction:
3744 {
3745 RangeTblFunction *rtfunc = (RangeTblFunction *) node;
3747
3749 MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
3750 /* Assume we need not copy the coldef info lists */
3751 return (Node *) newnode;
3752 }
3753 break;
3755 {
3758
3760 MUTATE(newnode->args, tsc->args, List *);
3761 MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
3762 return (Node *) newnode;
3763 }
3764 break;
3765 case T_TableFunc:
3766 {
3767 TableFunc *tf = (TableFunc *) node;
3769
3771 MUTATE(newnode->ns_uris, tf->ns_uris, List *);
3772 MUTATE(newnode->docexpr, tf->docexpr, Node *);
3773 MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
3774 MUTATE(newnode->colexprs, tf->colexprs, List *);
3775 MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
3776 MUTATE(newnode->colvalexprs, tf->colvalexprs, List *);
3777 MUTATE(newnode->passingvalexprs, tf->passingvalexprs, List *);
3778 return (Node *) newnode;
3779 }
3780 break;
3781 default:
3782 elog(ERROR, "unrecognized node type: %d",
3783 (int) nodeTag(node));
3784 break;
3785 }
3786 /* can't get here, but keep compiler happy */
3787 return NULL;
3788}
3789
3790
3791/*
3792 * query_tree_mutator --- initiate modification of a Query's expressions
3793 *
3794 * This routine exists just to reduce the number of places that need to know
3795 * where all the expression subtrees of a Query are. Note it can be used
3796 * for starting a walk at top level of a Query regardless of whether the
3797 * mutator intends to descend into subqueries. It is also useful for
3798 * descending into subqueries within a mutator.
3799 *
3800 * Some callers want to suppress mutating of certain items in the Query,
3801 * typically because they need to process them specially, or don't actually
3802 * want to recurse into subqueries. This is supported by the flags argument,
3803 * which is the bitwise OR of flag values to suppress mutating of
3804 * indicated items. (More flag bits may be added as needed.)
3805 *
3806 * Normally the top-level Query node itself is copied, but some callers want
3807 * it to be modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.
3808 * All modified substructure is safely copied in any case.
3809 */
3810Query *
3813 void *context,
3814 int flags)
3815{
3816 Assert(query != NULL && IsA(query, Query));
3817
3818 if (!(flags & QTW_DONT_COPY_QUERY))
3819 {
3820 Query *newquery;
3821
3822 FLATCOPY(newquery, query, Query);
3823 query = newquery;
3824 }
3825
3826 MUTATE(query->targetList, query->targetList, List *);
3827 MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
3828 MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
3829 MUTATE(query->mergeActionList, query->mergeActionList, List *);
3831 MUTATE(query->returningList, query->returningList, List *);
3832 MUTATE(query->jointree, query->jointree, FromExpr *);
3833 MUTATE(query->setOperations, query->setOperations, Node *);
3834 MUTATE(query->havingQual, query->havingQual, Node *);
3835 MUTATE(query->limitOffset, query->limitOffset, Node *);
3836 MUTATE(query->limitCount, query->limitCount, Node *);
3837
3838 /*
3839 * Most callers aren't interested in SortGroupClause nodes since those
3840 * don't contain actual expressions. However they do contain OIDs, which
3841 * may be of interest to some mutators.
3842 */
3843
3844 if ((flags & QTW_EXAMINE_SORTGROUP))
3845 {
3846 MUTATE(query->groupClause, query->groupClause, List *);
3847 MUTATE(query->windowClause, query->windowClause, List *);
3848 MUTATE(query->sortClause, query->sortClause, List *);
3849 MUTATE(query->distinctClause, query->distinctClause, List *);
3850 }
3851 else
3852 {
3853 /*
3854 * But we need to mutate the expressions under WindowClause nodes even
3855 * if we're not interested in SortGroupClause nodes.
3856 */
3858 ListCell *temp;
3859
3860 resultlist = NIL;
3861 foreach(temp, query->windowClause)
3862 {
3865
3867 MUTATE(newnode->startOffset, wc->startOffset, Node *);
3868 MUTATE(newnode->endOffset, wc->endOffset, Node *);
3869
3871 }
3872 query->windowClause = resultlist;
3873 }
3874
3875 /*
3876 * groupingSets and rowMarks are not mutated:
3877 *
3878 * groupingSets contain only ressortgroup refs (integers) which are
3879 * meaningless without the groupClause or tlist. Accordingly, any mutator
3880 * that needs to care about them needs to handle them itself in its Query
3881 * processing.
3882 *
3883 * rowMarks contains only rangetable indexes (and flags etc.) and
3884 * therefore should be handled at Query level similarly.
3885 */
3886
3887 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
3888 MUTATE(query->cteList, query->cteList, List *);
3889 else /* else copy CTE list as-is */
3890 query->cteList = copyObject(query->cteList);
3891 query->rtable = range_table_mutator(query->rtable,
3892 mutator, context, flags);
3893 return query;
3894}
3895
3896/*
3897 * range_table_mutator is just the part of query_tree_mutator that processes
3898 * a query's rangetable. This is split out since it can be useful on
3899 * its own.
3900 */
3901List *
3904 void *context,
3905 int flags)
3906{
3907 List *newrt = NIL;
3908 ListCell *rt;
3909
3910 foreach(rt, rtable)
3911 {
3914
3916 switch (rte->rtekind)
3917 {
3918 case RTE_RELATION:
3919 MUTATE(newrte->tablesample, rte->tablesample,
3921 /* we don't bother to copy eref, aliases, etc; OK? */
3922 break;
3923 case RTE_SUBQUERY:
3924 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3925 MUTATE(newrte->subquery, rte->subquery, Query *);
3926 else
3927 {
3928 /* else, copy RT subqueries as-is */
3929 newrte->subquery = copyObject(rte->subquery);
3930 }
3931 break;
3932 case RTE_JOIN:
3933 if (!(flags & QTW_IGNORE_JOINALIASES))
3934 MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
3935 else
3936 {
3937 /* else, copy join aliases as-is */
3938 newrte->joinaliasvars = copyObject(rte->joinaliasvars);
3939 }
3940 break;
3941 case RTE_FUNCTION:
3942 MUTATE(newrte->functions, rte->functions, List *);
3943 break;
3944 case RTE_TABLEFUNC:
3945 MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
3946 break;
3947 case RTE_VALUES:
3948 MUTATE(newrte->values_lists, rte->values_lists, List *);
3949 break;
3950 case RTE_GRAPH_TABLE:
3951 MUTATE(newrte->graph_pattern, rte->graph_pattern, GraphPattern *);
3952 MUTATE(newrte->graph_table_columns, rte->graph_table_columns, List *);
3953 break;
3954 case RTE_CTE:
3956 case RTE_RESULT:
3957 /* nothing to do */
3958 break;
3959 case RTE_GROUP:
3960 if (!(flags & QTW_IGNORE_GROUPEXPRS))
3961 MUTATE(newrte->groupexprs, rte->groupexprs, List *);
3962 else
3963 {
3964 /* else, copy grouping exprs as-is */
3965 newrte->groupexprs = copyObject(rte->groupexprs);
3966 }
3967 break;
3968 }
3969 MUTATE(newrte->securityQuals, rte->securityQuals, List *);
3971 }
3972 return newrt;
3973}
3974
3975/*
3976 * query_or_expression_tree_walker --- hybrid form
3977 *
3978 * This routine will invoke query_tree_walker if called on a Query node,
3979 * else will invoke the walker directly. This is a useful way of starting
3980 * the recursion when the walker's normal change of state is not appropriate
3981 * for the outermost Query node.
3982 */
3983bool
3986 void *context,
3987 int flags)
3988{
3989 if (node && IsA(node, Query))
3990 return query_tree_walker((Query *) node,
3991 walker,
3992 context,
3993 flags);
3994 else
3995 return WALK(node);
3996}
3997
3998/*
3999 * query_or_expression_tree_mutator --- hybrid form
4000 *
4001 * This routine will invoke query_tree_mutator if called on a Query node,
4002 * else will invoke the mutator directly. This is a useful way of starting
4003 * the recursion when the mutator's normal change of state is not appropriate
4004 * for the outermost Query node.
4005 */
4006Node *
4009 void *context,
4010 int flags)
4011{
4012 if (node && IsA(node, Query))
4013 return (Node *) query_tree_mutator((Query *) node,
4014 mutator,
4015 context,
4016 flags);
4017 else
4018 return mutator(node, context);
4019}
4020
4021
4022/*
4023 * raw_expression_tree_walker --- walk raw parse trees
4024 *
4025 * This has exactly the same API as expression_tree_walker, but instead of
4026 * walking post-analysis parse trees, it knows how to walk the node types
4027 * found in raw grammar output. (There is not currently any need for a
4028 * combined walker, so we keep them separate in the name of efficiency.)
4029 * Unlike expression_tree_walker, there is no special rule about query
4030 * boundaries: we descend to everything that's possibly interesting.
4031 *
4032 * Currently, the node type coverage here extends only to DML statements
4033 * (SELECT/INSERT/UPDATE/DELETE/MERGE) and nodes that can appear in them,
4034 * because this is used mainly during analysis of CTEs, and only DML
4035 * statements can appear in CTEs.
4036 */
4037bool
4040 void *context)
4041{
4042 ListCell *temp;
4043
4044 /*
4045 * The walker has already visited the current node, and so we need only
4046 * recurse into any sub-nodes it has.
4047 */
4048 if (node == NULL)
4049 return false;
4050
4051 /* Guard against stack overflow due to overly complex expressions */
4053
4054 switch (nodeTag(node))
4055 {
4056 case T_JsonFormat:
4057 case T_SetToDefault:
4058 case T_CurrentOfExpr:
4059 case T_SQLValueFunction:
4060 case T_Integer:
4061 case T_Float:
4062 case T_Boolean:
4063 case T_String:
4064 case T_BitString:
4065 case T_ParamRef:
4066 case T_A_Const:
4067 case T_A_Star:
4068 case T_MergeSupportFunc:
4069 case T_ReturningOption:
4070 /* primitive node types with no subnodes */
4071 break;
4072 case T_Alias:
4073 /* we assume the colnames list isn't interesting */
4074 break;
4075 case T_RangeVar:
4076 return WALK(((RangeVar *) node)->alias);
4077 case T_GroupingFunc:
4078 return WALK(((GroupingFunc *) node)->args);
4079 case T_SubLink:
4080 {
4081 SubLink *sublink = (SubLink *) node;
4082
4083 if (WALK(sublink->testexpr))
4084 return true;
4085 /* we assume the operName is not interesting */
4086 if (WALK(sublink->subselect))
4087 return true;
4088 }
4089 break;
4090 case T_CaseExpr:
4091 {
4092 CaseExpr *caseexpr = (CaseExpr *) node;
4093
4094 if (WALK(caseexpr->arg))
4095 return true;
4096 /* we assume walker doesn't care about CaseWhens, either */
4097 foreach(temp, caseexpr->args)
4098 {
4100
4101 if (WALK(when->expr))
4102 return true;
4103 if (WALK(when->result))
4104 return true;
4105 }
4106 if (WALK(caseexpr->defresult))
4107 return true;
4108 }
4109 break;
4110 case T_RowExpr:
4111 /* Assume colnames isn't interesting */
4112 return WALK(((RowExpr *) node)->args);
4113 case T_CoalesceExpr:
4114 return WALK(((CoalesceExpr *) node)->args);
4115 case T_MinMaxExpr:
4116 return WALK(((MinMaxExpr *) node)->args);
4117 case T_XmlExpr:
4118 {
4119 XmlExpr *xexpr = (XmlExpr *) node;
4120
4121 if (WALK(xexpr->named_args))
4122 return true;
4123 /* we assume walker doesn't care about arg_names */
4124 if (WALK(xexpr->args))
4125 return true;
4126 }
4127 break;
4128 case T_JsonReturning:
4129 return WALK(((JsonReturning *) node)->format);
4130 case T_JsonValueExpr:
4131 {
4132 JsonValueExpr *jve = (JsonValueExpr *) node;
4133
4134 if (WALK(jve->raw_expr))
4135 return true;
4136 if (WALK(jve->formatted_expr))
4137 return true;
4138 if (WALK(jve->format))
4139 return true;
4140 }
4141 break;
4142 case T_JsonParseExpr:
4143 {
4144 JsonParseExpr *jpe = (JsonParseExpr *) node;
4145
4146 if (WALK(jpe->expr))
4147 return true;
4148 if (WALK(jpe->output))
4149 return true;
4150 }
4151 break;
4152 case T_JsonScalarExpr:
4153 {
4154 JsonScalarExpr *jse = (JsonScalarExpr *) node;
4155
4156 if (WALK(jse->expr))
4157 return true;
4158 if (WALK(jse->output))
4159 return true;
4160 }
4161 break;
4163 {
4165
4166 if (WALK(jse->expr))
4167 return true;
4168 if (WALK(jse->output))
4169 return true;
4170 }
4171 break;
4173 {
4175
4176 if (WALK(ctor->args))
4177 return true;
4178 if (WALK(ctor->func))
4179 return true;
4180 if (WALK(ctor->coercion))
4181 return true;
4182 if (WALK(ctor->returning))
4183 return true;
4184 }
4185 break;
4186 case T_JsonIsPredicate:
4187 return WALK(((JsonIsPredicate *) node)->expr);
4188 case T_JsonArgument:
4189 return WALK(((JsonArgument *) node)->val);
4190 case T_JsonFuncExpr:
4191 {
4192 JsonFuncExpr *jfe = (JsonFuncExpr *) node;
4193
4194 if (WALK(jfe->context_item))
4195 return true;
4196 if (WALK(jfe->pathspec))
4197 return true;
4198 if (WALK(jfe->passing))
4199 return true;
4200 if (WALK(jfe->output))
4201 return true;
4202 if (WALK(jfe->on_empty))
4203 return true;
4204 if (WALK(jfe->on_error))
4205 return true;
4206 }
4207 break;
4208 case T_JsonBehavior:
4209 {
4210 JsonBehavior *jb = (JsonBehavior *) node;
4211
4212 if (WALK(jb->expr))
4213 return true;
4214 }
4215 break;
4216 case T_JsonTable:
4217 {
4218 JsonTable *jt = (JsonTable *) node;
4219
4220 if (WALK(jt->context_item))
4221 return true;
4222 if (WALK(jt->pathspec))
4223 return true;
4224 if (WALK(jt->passing))
4225 return true;
4226 if (WALK(jt->columns))
4227 return true;
4228 if (WALK(jt->on_error))
4229 return true;
4230 }
4231 break;
4232 case T_JsonTableColumn:
4233 {
4235
4236 if (WALK(jtc->typeName))
4237 return true;
4238 if (WALK(jtc->on_empty))
4239 return true;
4240 if (WALK(jtc->on_error))
4241 return true;
4242 if (WALK(jtc->columns))
4243 return true;
4244 }
4245 break;
4247 return WALK(((JsonTablePathSpec *) node)->string);
4248 case T_NullTest:
4249 return WALK(((NullTest *) node)->arg);
4250 case T_BooleanTest:
4251 return WALK(((BooleanTest *) node)->arg);
4252 case T_JoinExpr:
4253 {
4254 JoinExpr *join = (JoinExpr *) node;
4255
4256 if (WALK(join->larg))
4257 return true;
4258 if (WALK(join->rarg))
4259 return true;
4260 if (WALK(join->quals))
4261 return true;
4262 if (WALK(join->alias))
4263 return true;
4264 /* using list is deemed uninteresting */
4265 }
4266 break;
4267 case T_IntoClause:
4268 {
4269 IntoClause *into = (IntoClause *) node;
4270
4271 if (WALK(into->rel))
4272 return true;
4273 /* colNames, options are deemed uninteresting */
4274 /* viewQuery should be null in raw parsetree, but check it */
4275 if (WALK(into->viewQuery))
4276 return true;
4277 }
4278 break;
4279 case T_List:
4280 foreach(temp, (List *) node)
4281 {
4282 if (WALK((Node *) lfirst(temp)))
4283 return true;
4284 }
4285 break;
4286 case T_InsertStmt:
4287 {
4288 InsertStmt *stmt = (InsertStmt *) node;
4289
4290 if (WALK(stmt->relation))
4291 return true;
4292 if (WALK(stmt->cols))
4293 return true;
4294 if (WALK(stmt->selectStmt))
4295 return true;
4296 if (WALK(stmt->onConflictClause))
4297 return true;
4298 if (WALK(stmt->returningClause))
4299 return true;
4300 if (WALK(stmt->withClause))
4301 return true;
4302 }
4303 break;
4304 case T_DeleteStmt:
4305 {
4306 DeleteStmt *stmt = (DeleteStmt *) node;
4307
4308 if (WALK(stmt->relation))
4309 return true;
4310 if (WALK(stmt->usingClause))
4311 return true;
4312 if (WALK(stmt->whereClause))
4313 return true;
4314 if (WALK(stmt->returningClause))
4315 return true;
4316 if (WALK(stmt->withClause))
4317 return true;
4318 }
4319 break;
4320 case T_UpdateStmt:
4321 {
4322 UpdateStmt *stmt = (UpdateStmt *) node;
4323
4324 if (WALK(stmt->relation))
4325 return true;
4326 if (WALK(stmt->targetList))
4327 return true;
4328 if (WALK(stmt->whereClause))
4329 return true;
4330 if (WALK(stmt->fromClause))
4331 return true;
4332 if (WALK(stmt->returningClause))
4333 return true;
4334 if (WALK(stmt->withClause))
4335 return true;
4336 }
4337 break;
4338 case T_MergeStmt:
4339 {
4340 MergeStmt *stmt = (MergeStmt *) node;
4341
4342 if (WALK(stmt->relation))
4343 return true;
4344 if (WALK(stmt->sourceRelation))
4345 return true;
4346 if (WALK(stmt->joinCondition))
4347 return true;
4348 if (WALK(stmt->mergeWhenClauses))
4349 return true;
4350 if (WALK(stmt->returningClause))
4351 return true;
4352 if (WALK(stmt->withClause))
4353 return true;
4354 }
4355 break;
4356 case T_MergeWhenClause:
4357 {
4359
4360 if (WALK(mergeWhenClause->condition))
4361 return true;
4362 if (WALK(mergeWhenClause->targetList))
4363 return true;
4364 if (WALK(mergeWhenClause->values))
4365 return true;
4366 }
4367 break;
4368 case T_ReturningClause:
4369 {
4370 ReturningClause *returning = (ReturningClause *) node;
4371
4372 if (WALK(returning->options))
4373 return true;
4374 if (WALK(returning->exprs))
4375 return true;
4376 }
4377 break;
4378 case T_SelectStmt:
4379 {
4380 SelectStmt *stmt = (SelectStmt *) node;
4381
4382 if (WALK(stmt->distinctClause))
4383 return true;
4384 if (WALK(stmt->intoClause))
4385 return true;
4386 if (WALK(stmt->targetList))
4387 return true;
4388 if (WALK(stmt->fromClause))
4389 return true;
4390 if (WALK(stmt->whereClause))
4391 return true;
4392 if (WALK(stmt->groupClause))
4393 return true;
4394 if (WALK(stmt->havingClause))
4395 return true;
4396 if (WALK(stmt->windowClause))
4397 return true;
4398 if (WALK(stmt->valuesLists))
4399 return true;
4400 if (WALK(stmt->sortClause))
4401 return true;
4402 if (WALK(stmt->limitOffset))
4403 return true;
4404 if (WALK(stmt->limitCount))
4405 return true;
4406 if (WALK(stmt->lockingClause))
4407 return true;
4408 if (WALK(stmt->withClause))
4409 return true;
4410 if (WALK(stmt->larg))
4411 return true;
4412 if (WALK(stmt->rarg))
4413 return true;
4414 }
4415 break;
4416 case T_PLAssignStmt:
4417 {
4418 PLAssignStmt *stmt = (PLAssignStmt *) node;
4419
4420 if (WALK(stmt->indirection))
4421 return true;
4422 if (WALK(stmt->val))
4423 return true;
4424 }
4425 break;
4426 case T_A_Expr:
4427 {
4428 A_Expr *expr = (A_Expr *) node;
4429
4430 if (WALK(expr->lexpr))
4431 return true;
4432 if (WALK(expr->rexpr))
4433 return true;
4434 /* operator name is deemed uninteresting */
4435 }
4436 break;
4437 case T_BoolExpr:
4438 {
4439 BoolExpr *expr = (BoolExpr *) node;
4440
4441 if (WALK(expr->args))
4442 return true;
4443 }
4444 break;
4445 case T_ColumnRef:
4446 /* we assume the fields contain nothing interesting */
4447 break;
4448 case T_FuncCall:
4449 {
4450 FuncCall *fcall = (FuncCall *) node;
4451
4452 if (WALK(fcall->args))
4453 return true;
4454 if (WALK(fcall->agg_order))
4455 return true;
4456 if (WALK(fcall->agg_filter))
4457 return true;
4458 if (WALK(fcall->over))
4459 return true;
4460 /* function name is deemed uninteresting */
4461 }
4462 break;
4463 case T_NamedArgExpr:
4464 return WALK(((NamedArgExpr *) node)->arg);
4465 case T_A_Indices:
4466 {
4467 A_Indices *indices = (A_Indices *) node;
4468
4469 if (WALK(indices->lidx))
4470 return true;
4471 if (WALK(indices->uidx))
4472 return true;
4473 }
4474 break;
4475 case T_A_Indirection:
4476 {
4477 A_Indirection *indir = (A_Indirection *) node;
4478
4479 if (WALK(indir->arg))
4480 return true;
4481 if (WALK(indir->indirection))
4482 return true;
4483 }
4484 break;
4485 case T_A_ArrayExpr:
4486 return WALK(((A_ArrayExpr *) node)->elements);
4487 case T_ResTarget:
4488 {
4489 ResTarget *rt = (ResTarget *) node;
4490
4491 if (WALK(rt->indirection))
4492 return true;
4493 if (WALK(rt->val))
4494 return true;
4495 }
4496 break;
4497 case T_MultiAssignRef:
4498 return WALK(((MultiAssignRef *) node)->source);
4499 case T_TypeCast:
4500 {
4501 TypeCast *tc = (TypeCast *) node;
4502
4503 if (WALK(tc->arg))
4504 return true;
4505 if (WALK(tc->typeName))
4506 return true;
4507 }
4508 break;
4509 case T_CollateClause:
4510 return WALK(((CollateClause *) node)->arg);
4511 case T_SortBy:
4512 return WALK(((SortBy *) node)->node);
4513 case T_WindowDef:
4514 {
4515 WindowDef *wd = (WindowDef *) node;
4516
4517 if (WALK(wd->partitionClause))
4518 return true;
4519 if (WALK(wd->orderClause))
4520 return true;
4521 if (WALK(wd->startOffset))
4522 return true;
4523 if (WALK(wd->endOffset))
4524 return true;
4525 }
4526 break;
4527 case T_RangeSubselect:
4528 {
4529 RangeSubselect *rs = (RangeSubselect *) node;
4530
4531 if (WALK(rs->subquery))
4532 return true;
4533 if (WALK(rs->alias))
4534 return true;
4535 }
4536 break;
4537 case T_RangeFunction:
4538 {
4539 RangeFunction *rf = (RangeFunction *) node;
4540
4541 if (WALK(rf->functions))
4542 return true;
4543 if (WALK(rf->alias))
4544 return true;
4545 if (WALK(rf->coldeflist))
4546 return true;
4547 }
4548 break;
4549 case T_RangeTableSample:
4550 {
4552
4553 if (WALK(rts->relation))
4554 return true;
4555 /* method name is deemed uninteresting */
4556 if (WALK(rts->args))
4557 return true;
4558 if (WALK(rts->repeatable))
4559 return true;
4560 }
4561 break;
4562 case T_RangeTableFunc:
4563 {
4564 RangeTableFunc *rtf = (RangeTableFunc *) node;
4565
4566 if (WALK(rtf->docexpr))
4567 return true;
4568 if (WALK(rtf->rowexpr))
4569 return true;
4570 if (WALK(rtf->namespaces))
4571 return true;
4572 if (WALK(rtf->columns))
4573 return true;
4574 if (WALK(rtf->alias))
4575 return true;
4576 }
4577 break;
4579 {
4581
4582 if (WALK(rtfc->colexpr))
4583 return true;
4584 if (WALK(rtfc->coldefexpr))
4585 return true;
4586 }
4587 break;
4588 case T_RangeGraphTable:
4589 {
4591
4592 if (WALK(rgt->graph_pattern))
4593 return true;
4594 if (WALK(rgt->columns))
4595 return true;
4596 if (WALK(rgt->alias))
4597 return true;
4598 }
4599 break;
4600 case T_TypeName:
4601 {
4602 TypeName *tn = (TypeName *) node;
4603
4604 if (WALK(tn->typmods))
4605 return true;
4606 if (WALK(tn->arrayBounds))
4607 return true;
4608 /* type name itself is deemed uninteresting */
4609 }
4610 break;
4611 case T_ColumnDef:
4612 {
4613 ColumnDef *coldef = (ColumnDef *) node;
4614
4615 if (WALK(coldef->typeName))
4616 return true;
4617 if (WALK(coldef->raw_default))
4618 return true;
4619 if (WALK(coldef->collClause))
4620 return true;
4621 /* for now, constraints are ignored */
4622 }
4623 break;
4624 case T_IndexElem:
4625 {
4626 IndexElem *indelem = (IndexElem *) node;
4627
4628 if (WALK(indelem->expr))
4629 return true;
4630 /* collation and opclass names are deemed uninteresting */
4631 }
4632 break;
4633 case T_GroupingSet:
4634 return WALK(((GroupingSet *) node)->content);
4635 case T_LockingClause:
4636 return WALK(((LockingClause *) node)->lockedRels);
4637 case T_XmlSerialize:
4638 {
4639 XmlSerialize *xs = (XmlSerialize *) node;
4640
4641 if (WALK(xs->expr))
4642 return true;
4643 if (WALK(xs->typeName))
4644 return true;
4645 }
4646 break;
4647 case T_WithClause:
4648 return WALK(((WithClause *) node)->ctes);
4649 case T_InferClause:
4650 {
4651 InferClause *stmt = (InferClause *) node;
4652
4653 if (WALK(stmt->indexElems))
4654 return true;
4655 if (WALK(stmt->whereClause))
4656 return true;
4657 }
4658 break;
4659 case T_OnConflictClause:
4660 {
4662
4663 if (WALK(stmt->infer))
4664 return true;
4665 if (WALK(stmt->targetList))
4666 return true;
4667 if (WALK(stmt->whereClause))
4668 return true;
4669 }
4670 break;
4671 case T_CommonTableExpr:
4672 /* search_clause and cycle_clause are not interesting here */
4673 return WALK(((CommonTableExpr *) node)->ctequery);
4674 case T_JsonOutput:
4675 {
4676 JsonOutput *out = (JsonOutput *) node;
4677
4678 if (WALK(out->typeName))
4679 return true;
4680 if (WALK(out->returning))
4681 return true;
4682 }
4683 break;
4684 case T_JsonKeyValue:
4685 {
4686 JsonKeyValue *jkv = (JsonKeyValue *) node;
4687
4688 if (WALK(jkv->key))
4689 return true;
4690 if (WALK(jkv->value))
4691 return true;
4692 }
4693 break;
4695 {
4697
4698 if (WALK(joc->output))
4699 return true;
4700 if (WALK(joc->exprs))
4701 return true;
4702 }
4703 break;
4705 {
4707
4708 if (WALK(jac->output))
4709 return true;
4710 if (WALK(jac->exprs))
4711 return true;
4712 }
4713 break;
4715 {
4717
4718 if (WALK(ctor->output))
4719 return true;
4720 if (WALK(ctor->agg_order))
4721 return true;
4722 if (WALK(ctor->agg_filter))
4723 return true;
4724 if (WALK(ctor->over))
4725 return true;
4726 }
4727 break;
4728 case T_JsonObjectAgg:
4729 {
4730 JsonObjectAgg *joa = (JsonObjectAgg *) node;
4731
4732 if (WALK(joa->constructor))
4733 return true;
4734 if (WALK(joa->arg))
4735 return true;
4736 }
4737 break;
4738 case T_JsonArrayAgg:
4739 {
4740 JsonArrayAgg *jaa = (JsonArrayAgg *) node;
4741
4742 if (WALK(jaa->constructor))
4743 return true;
4744 if (WALK(jaa->arg))
4745 return true;
4746 }
4747 break;
4749 {
4751
4752 if (WALK(jaqc->output))
4753 return true;
4754 if (WALK(jaqc->query))
4755 return true;
4756 }
4757 break;
4759 {
4761
4762 if (WALK(gep->subexpr))
4763 return true;
4764 if (WALK(gep->whereClause))
4765 return true;
4766 }
4767 break;
4768 case T_GraphPattern:
4769 {
4770 GraphPattern *gp = (GraphPattern *) node;
4771
4772 if (WALK(gp->path_pattern_list))
4773 return true;
4774 if (WALK(gp->whereClause))
4775 return true;
4776 }
4777 break;
4778 default:
4779 elog(ERROR, "unrecognized node type: %d",
4780 (int) nodeTag(node));
4781 break;
4782 }
4783 return false;
4784}
4785
4786/*
4787 * planstate_tree_walker --- walk plan state trees
4788 *
4789 * The walker has already visited the current node, and so we need only
4790 * recurse into any sub-nodes it has.
4791 */
4792bool
4795 void *context)
4796{
4797 Plan *plan = planstate->plan;
4798 ListCell *lc;
4799
4800 /* We don't need implicit coercions to Node here */
4801#define PSWALK(n) walker(n, context)
4802
4803 /* Guard against stack overflow due to overly complex plan trees */
4805
4806 /* initPlan-s */
4807 if (planstate_walk_subplans(planstate->initPlan, walker, context))
4808 return true;
4809
4810 /* lefttree */
4811 if (outerPlanState(planstate))
4812 {
4813 if (PSWALK(outerPlanState(planstate)))
4814 return true;
4815 }
4816
4817 /* righttree */
4818 if (innerPlanState(planstate))
4819 {
4820 if (PSWALK(innerPlanState(planstate)))
4821 return true;
4822 }
4823
4824 /* special child plans */
4825 switch (nodeTag(plan))
4826 {
4827 case T_Append:
4828 if (planstate_walk_members(((AppendState *) planstate)->appendplans,
4829 ((AppendState *) planstate)->as_nplans,
4830 walker, context))
4831 return true;
4832 break;
4833 case T_MergeAppend:
4834 if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
4835 ((MergeAppendState *) planstate)->ms_nplans,
4836 walker, context))
4837 return true;
4838 break;
4839 case T_BitmapAnd:
4840 if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
4841 ((BitmapAndState *) planstate)->nplans,
4842 walker, context))
4843 return true;
4844 break;
4845 case T_BitmapOr:
4846 if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
4847 ((BitmapOrState *) planstate)->nplans,
4848 walker, context))
4849 return true;
4850 break;
4851 case T_SubqueryScan:
4852 if (PSWALK(((SubqueryScanState *) planstate)->subplan))
4853 return true;
4854 break;
4855 case T_CustomScan:
4856 foreach(lc, ((CustomScanState *) planstate)->custom_ps)
4857 {
4858 if (PSWALK(lfirst(lc)))
4859 return true;
4860 }
4861 break;
4862 default:
4863 break;
4864 }
4865
4866 /* subPlan-s */
4867 if (planstate_walk_subplans(planstate->subPlan, walker, context))
4868 return true;
4869
4870 return false;
4871}
4872
4873/*
4874 * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
4875 */
4876static bool
4879 void *context)
4880{
4881 ListCell *lc;
4882
4883 foreach(lc, plans)
4884 {
4886
4887 if (PSWALK(sps->planstate))
4888 return true;
4889 }
4890
4891 return false;
4892}
4893
4894/*
4895 * Walk the constituent plans of a ModifyTable, Append, MergeAppend,
4896 * BitmapAnd, or BitmapOr node.
4897 */
4898static bool
4901 void *context)
4902{
4903 for (int j = 0; j < nplans; j++)
4904 {
4905 if (PSWALK(planstates[j]))
4906 return true;
4907 }
4908
4909 return false;
4910}
#define Min(x, y)
Definition c.h:1093
#define Assert(condition)
Definition c.h:945
int32_t int32
Definition c.h:614
#define OidIsValid(objectId)
Definition c.h:860
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define outerPlanState(node)
Definition execnodes.h:1273
#define innerPlanState(node)
Definition execnodes.h:1272
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:2726
#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:4038
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:2850
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:4877
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:3984
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:2829
Node * query_or_expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *context, int flags)
Definition nodeFuncs.c:4007
Query * query_tree_mutator_impl(Query *query, tree_mutator_callback mutator, void *context, int flags)
Definition nodeFuncs.c:3811
#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:3902
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:4793
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:2984
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:4899
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:414
#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:161
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:1613
@ IS_XMLSERIALIZE
Definition primnodes.h:1612
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:353
Node * rexpr
Definition parsenodes.h:354
Node * uidx
Definition parsenodes.h:487
Node * lidx
Definition parsenodes.h:486
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:1276
List * args
Definition primnodes.h:973
Node * cycle_mark_default
Node * cycle_mark_value
Expr * defresult
Definition primnodes.h:1349
List * args
Definition primnodes.h:1348
Expr * result
Definition primnodes.h:1360
ParseLoc location
Definition primnodes.h:2063
ParseLoc location
Definition primnodes.h:1248
Oid consttype
Definition primnodes.h:330
List * newvals
Definition primnodes.h:1194
Expr * arg
Definition primnodes.h:1193
Node * quals
Definition primnodes.h:2384
List * fromlist
Definition primnodes.h:2383
Node * agg_filter
Definition parsenodes.h:454
List * agg_order
Definition parsenodes.h:453
List * args
Definition parsenodes.h:452
struct WindowDef * over
Definition parsenodes.h:455
Oid funcid
Definition primnodes.h:783
List * args
Definition primnodes.h:801
RangeVar * rel
Definition primnodes.h:164
Node * quals
Definition primnodes.h:2364
Node * larg
Definition primnodes.h:2357
Node * rarg
Definition primnodes.h:2358
Node * formatted_expr
Definition primnodes.h:1850
ParseLoc location
Definition primnodes.h:1886
Oid collation
Definition primnodes.h:1883
JsonFormat * format
Definition primnodes.h:1762
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:2402
List * onConflictSet
Definition primnodes.h:2411
List * exclRelTlist
Definition primnodes.h:2416
Node * onConflictWhere
Definition primnodes.h:2414
Node * arbiterWhere
Definition primnodes.h:2404
Oid opno
Definition primnodes.h:851
List * args
Definition primnodes.h:869
ParseLoc location
Definition primnodes.h:872
Plan * plan
Definition execnodes.h:1177
List * subPlan
Definition execnodes.h:1204
List * initPlan
Definition execnodes.h:1202
Node * mergeJoinCondition
Definition parsenodes.h:196
Node * limitCount
Definition parsenodes.h:231
FromExpr * jointree
Definition parsenodes.h:182
List * returningList
Definition parsenodes.h:214
Node * setOperations
Definition parsenodes.h:236
List * cteList
Definition parsenodes.h:173
OnConflictExpr * onConflict
Definition parsenodes.h:203
List * groupClause
Definition parsenodes.h:216
Node * havingQual
Definition parsenodes.h:222
List * rtable
Definition parsenodes.h:175
Node * limitOffset
Definition parsenodes.h:230
List * mergeActionList
Definition parsenodes.h:185
List * windowClause
Definition parsenodes.h:224
List * targetList
Definition parsenodes.h:198
List * distinctClause
Definition parsenodes.h:226
List * sortClause
Definition parsenodes.h:228
ParseLoc location
Definition primnodes.h:1226
List * args
Definition primnodes.h:1449
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:1634
ParseLoc location
Definition primnodes.h:1643
List * named_args
Definition primnodes.h:1630
Definition type.h:89
const char * type