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