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 27469892 : exprType(const Node *expr)
43 : {
44 : Oid type;
45 :
46 27469892 : if (!expr)
47 462 : return InvalidOid;
48 :
49 27469430 : switch (nodeTag(expr))
50 : {
51 14292892 : case T_Var:
52 14292892 : type = ((const Var *) expr)->vartype;
53 14292892 : break;
54 4191814 : case T_Const:
55 4191814 : type = ((const Const *) expr)->consttype;
56 4191814 : break;
57 2439328 : case T_Param:
58 2439328 : type = ((const Param *) expr)->paramtype;
59 2439328 : break;
60 270782 : case T_Aggref:
61 270782 : type = ((const Aggref *) expr)->aggtype;
62 270782 : break;
63 2366 : case T_GroupingFunc:
64 2366 : type = INT4OID;
65 2366 : break;
66 18374 : case T_WindowFunc:
67 18374 : type = ((const WindowFunc *) expr)->wintype;
68 18374 : break;
69 894 : case T_MergeSupportFunc:
70 894 : type = ((const MergeSupportFunc *) expr)->msftype;
71 894 : break;
72 125716 : case T_SubscriptingRef:
73 125716 : type = ((const SubscriptingRef *) expr)->refrestype;
74 125716 : break;
75 2082484 : case T_FuncExpr:
76 2082484 : type = ((const FuncExpr *) expr)->funcresulttype;
77 2082484 : break;
78 96592 : case T_NamedArgExpr:
79 96592 : type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
80 96592 : break;
81 1484682 : case T_OpExpr:
82 1484682 : type = ((const OpExpr *) expr)->opresulttype;
83 1484682 : break;
84 2086 : case T_DistinctExpr:
85 2086 : type = ((const DistinctExpr *) expr)->opresulttype;
86 2086 : break;
87 962 : case T_NullIfExpr:
88 962 : type = ((const NullIfExpr *) expr)->opresulttype;
89 962 : break;
90 99460 : case T_ScalarArrayOpExpr:
91 99460 : type = BOOLOID;
92 99460 : break;
93 306462 : case T_BoolExpr:
94 306462 : type = BOOLOID;
95 306462 : break;
96 93634 : case T_SubLink:
97 : {
98 93634 : const SubLink *sublink = (const SubLink *) expr;
99 :
100 93634 : if (sublink->subLinkType == EXPR_SUBLINK ||
101 32594 : sublink->subLinkType == ARRAY_SUBLINK)
102 77354 : {
103 : /* get the type of the subselect's first target column */
104 77354 : Query *qtree = (Query *) sublink->subselect;
105 : TargetEntry *tent;
106 :
107 77354 : if (!qtree || !IsA(qtree, Query))
108 0 : elog(ERROR, "cannot get type for untransformed sublink");
109 77354 : tent = linitial_node(TargetEntry, qtree->targetList);
110 : Assert(!tent->resjunk);
111 77354 : type = exprType((Node *) tent->expr);
112 77354 : if (sublink->subLinkType == ARRAY_SUBLINK)
113 : {
114 16314 : type = get_promoted_array_type(type);
115 16314 : 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 16280 : 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 16142 : type = BOOLOID;
131 : }
132 : }
133 93634 : break;
134 48700 : case T_SubPlan:
135 : {
136 48700 : const SubPlan *subplan = (const SubPlan *) expr;
137 :
138 48700 : if (subplan->subLinkType == EXPR_SUBLINK ||
139 3472 : subplan->subLinkType == ARRAY_SUBLINK)
140 : {
141 : /* get the type of the subselect's first target column */
142 45564 : type = subplan->firstColType;
143 45564 : 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 3136 : 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 2956 : type = BOOLOID;
162 : }
163 : }
164 48700 : break;
165 1192 : case T_AlternativeSubPlan:
166 : {
167 1192 : const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
168 :
169 : /* subplans should all return the same thing */
170 1192 : type = exprType((Node *) linitial(asplan->subplans));
171 : }
172 1192 : break;
173 38566 : case T_FieldSelect:
174 38566 : type = ((const FieldSelect *) expr)->resulttype;
175 38566 : break;
176 1126 : case T_FieldStore:
177 1126 : type = ((const FieldStore *) expr)->resulttype;
178 1126 : break;
179 530762 : case T_RelabelType:
180 530762 : type = ((const RelabelType *) expr)->resulttype;
181 530762 : break;
182 159290 : case T_CoerceViaIO:
183 159290 : type = ((const CoerceViaIO *) expr)->resulttype;
184 159290 : break;
185 16128 : case T_ArrayCoerceExpr:
186 16128 : type = ((const ArrayCoerceExpr *) expr)->resulttype;
187 16128 : break;
188 2436 : case T_ConvertRowtypeExpr:
189 2436 : type = ((const ConvertRowtypeExpr *) expr)->resulttype;
190 2436 : break;
191 9574 : case T_CollateExpr:
192 9574 : type = exprType((Node *) ((const CollateExpr *) expr)->arg);
193 9574 : break;
194 486472 : case T_CaseExpr:
195 486472 : type = ((const CaseExpr *) expr)->casetype;
196 486472 : break;
197 36702 : case T_CaseTestExpr:
198 36702 : type = ((const CaseTestExpr *) expr)->typeId;
199 36702 : break;
200 94058 : case T_ArrayExpr:
201 94058 : type = ((const ArrayExpr *) expr)->array_typeid;
202 94058 : break;
203 15184 : case T_RowExpr:
204 15184 : type = ((const RowExpr *) expr)->row_typeid;
205 15184 : break;
206 408 : case T_RowCompareExpr:
207 408 : type = BOOLOID;
208 408 : break;
209 23794 : case T_CoalesceExpr:
210 23794 : type = ((const CoalesceExpr *) expr)->coalescetype;
211 23794 : break;
212 6054 : case T_MinMaxExpr:
213 6054 : type = ((const MinMaxExpr *) expr)->minmaxtype;
214 6054 : break;
215 11278 : case T_SQLValueFunction:
216 11278 : type = ((const SQLValueFunction *) expr)->type;
217 11278 : break;
218 25462 : case T_XmlExpr:
219 25462 : if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
220 102 : type = BOOLOID;
221 25360 : else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
222 870 : type = TEXTOID;
223 : else
224 24490 : type = XMLOID;
225 25462 : 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 6738 : case T_JsonConstructorExpr:
234 6738 : type = ((const JsonConstructorExpr *) expr)->returning->typid;
235 6738 : 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 40134 : case T_NullTest:
254 40134 : type = BOOLOID;
255 40134 : break;
256 2758 : case T_BooleanTest:
257 2758 : type = BOOLOID;
258 2758 : break;
259 263984 : case T_CoerceToDomain:
260 263984 : type = ((const CoerceToDomain *) expr)->resulttype;
261 263984 : break;
262 1994 : case T_CoerceToDomainValue:
263 1994 : type = ((const CoerceToDomainValue *) expr)->typeId;
264 1994 : break;
265 106342 : case T_SetToDefault:
266 106342 : type = ((const SetToDefault *) expr)->typeId;
267 106342 : break;
268 254 : case T_CurrentOfExpr:
269 254 : type = BOOLOID;
270 254 : break;
271 1498 : case T_NextValueExpr:
272 1498 : type = ((const NextValueExpr *) expr)->typeId;
273 1498 : 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 756 : case T_ReturningExpr:
282 756 : type = exprType((Node *) ((const ReturningExpr *) expr)->retexpr);
283 756 : break;
284 11638 : case T_PlaceHolderVar:
285 11638 : type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
286 11638 : 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 27469430 : 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 9895782 : exprTypmod(const Node *expr)
302 : {
303 9895782 : if (!expr)
304 0 : return -1;
305 :
306 9895782 : switch (nodeTag(expr))
307 : {
308 5703508 : case T_Var:
309 5703508 : return ((const Var *) expr)->vartypmod;
310 1543916 : case T_Const:
311 1543916 : return ((const Const *) expr)->consttypmod;
312 143042 : case T_Param:
313 143042 : return ((const Param *) expr)->paramtypmod;
314 37332 : case T_SubscriptingRef:
315 37332 : return ((const SubscriptingRef *) expr)->reftypmod;
316 1282428 : case T_FuncExpr:
317 : {
318 : int32 coercedTypmod;
319 :
320 : /* Be smart about length-coercion functions... */
321 1282428 : if (exprIsLengthCoercion(expr, &coercedTypmod))
322 22740 : return coercedTypmod;
323 : }
324 1259688 : break;
325 0 : case T_NamedArgExpr:
326 0 : return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
327 248 : 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 248 : const NullIfExpr *nexpr = (const NullIfExpr *) expr;
334 :
335 248 : return exprTypmod((Node *) linitial(nexpr->args));
336 : }
337 : break;
338 6806 : case T_SubLink:
339 : {
340 6806 : const SubLink *sublink = (const SubLink *) expr;
341 :
342 6806 : if (sublink->subLinkType == EXPR_SUBLINK ||
343 712 : sublink->subLinkType == ARRAY_SUBLINK)
344 : {
345 : /* get the typmod of the subselect's first target column */
346 6722 : Query *qtree = (Query *) sublink->subselect;
347 : TargetEntry *tent;
348 :
349 6722 : if (!qtree || !IsA(qtree, Query))
350 0 : elog(ERROR, "cannot get type for untransformed sublink");
351 6722 : tent = linitial_node(TargetEntry, qtree->targetList);
352 : Assert(!tent->resjunk);
353 6722 : 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 84 : break;
359 34024 : case T_SubPlan:
360 : {
361 34024 : const SubPlan *subplan = (const SubPlan *) expr;
362 :
363 34024 : if (subplan->subLinkType == EXPR_SUBLINK ||
364 1886 : 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 32372 : return subplan->firstColTypmod;
369 : }
370 : /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
371 : }
372 1652 : break;
373 602 : case T_AlternativeSubPlan:
374 : {
375 602 : const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
376 :
377 : /* subplans should all return the same thing */
378 602 : return exprTypmod((Node *) linitial(asplan->subplans));
379 : }
380 : break;
381 16136 : case T_FieldSelect:
382 16136 : return ((const FieldSelect *) expr)->resulttypmod;
383 189988 : case T_RelabelType:
384 189988 : return ((const RelabelType *) expr)->resulttypmod;
385 7550 : case T_ArrayCoerceExpr:
386 7550 : return ((const ArrayCoerceExpr *) expr)->resulttypmod;
387 200 : case T_CollateExpr:
388 200 : return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
389 224454 : case T_CaseExpr:
390 : {
391 : /*
392 : * If all the alternatives agree on type/typmod, return that
393 : * typmod, else use -1
394 : */
395 224454 : const CaseExpr *cexpr = (const CaseExpr *) expr;
396 224454 : Oid casetype = cexpr->casetype;
397 : int32 typmod;
398 : ListCell *arg;
399 :
400 224454 : if (!cexpr->defresult)
401 0 : return -1;
402 224454 : if (exprType((Node *) cexpr->defresult) != casetype)
403 0 : return -1;
404 224454 : typmod = exprTypmod((Node *) cexpr->defresult);
405 224454 : if (typmod < 0)
406 224454 : 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 12560 : case T_CaseTestExpr:
420 12560 : return ((const CaseTestExpr *) expr)->typeMod;
421 35066 : case T_ArrayExpr:
422 : {
423 : /*
424 : * If all the elements agree on type/typmod, return that
425 : * typmod, else use -1
426 : */
427 35066 : const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
428 : Oid commontype;
429 : int32 typmod;
430 : ListCell *elem;
431 :
432 35066 : if (arrayexpr->elements == NIL)
433 182 : return -1;
434 34884 : typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
435 34884 : if (typmod < 0)
436 34866 : 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 6482 : case T_CoalesceExpr:
454 : {
455 : /*
456 : * If all the alternatives agree on type/typmod, return that
457 : * typmod, else use -1
458 : */
459 6482 : const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
460 6482 : Oid coalescetype = cexpr->coalescetype;
461 : int32 typmod;
462 : ListCell *arg;
463 :
464 6482 : if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
465 0 : return -1;
466 6482 : typmod = exprTypmod((Node *) linitial(cexpr->args));
467 6482 : if (typmod < 0)
468 6482 : 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 2350 : case T_SQLValueFunction:
510 2350 : return ((const SQLValueFunction *) expr)->typmod;
511 66 : case T_JsonValueExpr:
512 66 : return exprTypmod((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
513 2400 : case T_JsonConstructorExpr:
514 2400 : 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 202004 : case T_CoerceToDomain:
530 202004 : return ((const CoerceToDomain *) expr)->resulttypmod;
531 82 : case T_CoerceToDomainValue:
532 82 : return ((const CoerceToDomainValue *) expr)->typeMod;
533 27844 : case T_SetToDefault:
534 27844 : return ((const SetToDefault *) expr)->typeMod;
535 408 : case T_ReturningExpr:
536 408 : return exprTypmod((Node *) ((const ReturningExpr *) expr)->retexpr);
537 7456 : case T_PlaceHolderVar:
538 7456 : return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
539 402220 : default:
540 402220 : break;
541 : }
542 1663644 : 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 1284018 : exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
558 : {
559 1284018 : if (coercedTypmod != NULL)
560 1284018 : *coercedTypmod = -1; /* default result on failure */
561 :
562 : /*
563 : * Scalar-type length coercions are FuncExprs, array-type length coercions
564 : * are ArrayCoerceExprs
565 : */
566 1284018 : if (expr && IsA(expr, FuncExpr))
567 : {
568 1284018 : 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 1284018 : if (func->funcformat != COERCE_EXPLICIT_CAST &&
576 1249602 : func->funcformat != COERCE_IMPLICIT_CAST)
577 1010990 : 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 273028 : nargs = list_length(func->args);
585 273028 : if (nargs < 2 || nargs > 3)
586 250200 : return false;
587 :
588 22828 : second_arg = (Const *) lsecond(func->args);
589 22828 : if (!IsA(second_arg, Const) ||
590 22828 : second_arg->consttype != INT4OID ||
591 22828 : second_arg->constisnull)
592 0 : return false;
593 :
594 : /*
595 : * OK, it is indeed a length-coercion function.
596 : */
597 22828 : if (coercedTypmod != NULL)
598 22828 : *coercedTypmod = DatumGetInt32(second_arg->constvalue);
599 :
600 22828 : 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 279498 : 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 282418 : while (arg && IsA(arg, RelabelType))
645 2920 : arg = (Node *) ((RelabelType *) arg)->arg;
646 :
647 279498 : if (arg && IsA(arg, Const))
648 : {
649 : /* Modify the Const directly to preserve const-flatness. */
650 99208 : Const *con = (Const *) arg;
651 :
652 99208 : if (!overwrite_ok)
653 11958 : con = copyObject(con);
654 99208 : con->consttype = rtype;
655 99208 : con->consttypmod = rtypmod;
656 99208 : con->constcollid = rcollid;
657 : /* We keep the Const's original location. */
658 99208 : return (Node *) con;
659 : }
660 186368 : else if (exprType(arg) == rtype &&
661 12072 : exprTypmod(arg) == rtypmod &&
662 5994 : exprCollation(arg) == rcollid)
663 : {
664 : /* Sometimes we find a nest of relabels that net out to nothing. */
665 2756 : return arg;
666 : }
667 : else
668 : {
669 : /* Nope, gotta have a RelabelType. */
670 177534 : RelabelType *newrelabel = makeNode(RelabelType);
671 :
672 177534 : newrelabel->arg = (Expr *) arg;
673 177534 : newrelabel->resulttype = rtype;
674 177534 : newrelabel->resulttypmod = rtypmod;
675 177534 : newrelabel->resultcollid = rcollid;
676 177534 : newrelabel->relabelformat = rformat;
677 177534 : newrelabel->location = rlocation;
678 177534 : 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 691008 : strip_implicit_coercions(Node *node)
706 : {
707 691008 : if (node == NULL)
708 0 : return NULL;
709 691008 : if (IsA(node, FuncExpr))
710 : {
711 13158 : FuncExpr *f = (FuncExpr *) node;
712 :
713 13158 : if (f->funcformat == COERCE_IMPLICIT_CAST)
714 38 : return strip_implicit_coercions(linitial(f->args));
715 : }
716 677850 : else if (IsA(node, RelabelType))
717 : {
718 10918 : RelabelType *r = (RelabelType *) node;
719 :
720 10918 : if (r->relabelformat == COERCE_IMPLICIT_CAST)
721 14 : return strip_implicit_coercions((Node *) r->arg);
722 : }
723 666932 : else if (IsA(node, CoerceViaIO))
724 : {
725 592 : CoerceViaIO *c = (CoerceViaIO *) node;
726 :
727 592 : if (c->coerceformat == COERCE_IMPLICIT_CAST)
728 0 : return strip_implicit_coercions((Node *) c->arg);
729 : }
730 666340 : 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 666340 : 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 666340 : 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 690956 : 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 808034 : expression_returns_set(Node *clause)
764 : {
765 808034 : return expression_returns_set_walker(clause, NULL);
766 : }
767 :
768 : static bool
769 3411810 : expression_returns_set_walker(Node *node, void *context)
770 : {
771 3411810 : if (node == NULL)
772 32798 : return false;
773 3379012 : if (IsA(node, FuncExpr))
774 : {
775 89810 : FuncExpr *expr = (FuncExpr *) node;
776 :
777 89810 : if (expr->funcretset)
778 10214 : return true;
779 : /* else fall through to check args */
780 : }
781 3368798 : if (IsA(node, OpExpr))
782 : {
783 794200 : OpExpr *expr = (OpExpr *) node;
784 :
785 794200 : 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 3368792 : if (IsA(node, Aggref))
798 1126 : return false;
799 3367666 : if (IsA(node, GroupingFunc))
800 60 : return false;
801 3367606 : if (IsA(node, WindowFunc))
802 30 : return false;
803 :
804 3367576 : 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 12681108 : exprCollation(const Node *expr)
822 : {
823 : Oid coll;
824 :
825 12681108 : if (!expr)
826 0 : return InvalidOid;
827 :
828 12681108 : switch (nodeTag(expr))
829 : {
830 9351764 : case T_Var:
831 9351764 : coll = ((const Var *) expr)->varcollid;
832 9351764 : break;
833 1832036 : case T_Const:
834 1832036 : coll = ((const Const *) expr)->constcollid;
835 1832036 : break;
836 379228 : case T_Param:
837 379228 : coll = ((const Param *) expr)->paramcollid;
838 379228 : break;
839 77638 : case T_Aggref:
840 77638 : coll = ((const Aggref *) expr)->aggcollid;
841 77638 : break;
842 724 : case T_GroupingFunc:
843 724 : coll = InvalidOid;
844 724 : break;
845 4832 : case T_WindowFunc:
846 4832 : coll = ((const WindowFunc *) expr)->wincollid;
847 4832 : break;
848 300 : case T_MergeSupportFunc:
849 300 : coll = ((const MergeSupportFunc *) expr)->msfcollid;
850 300 : break;
851 7874 : case T_SubscriptingRef:
852 7874 : coll = ((const SubscriptingRef *) expr)->refcollid;
853 7874 : break;
854 405242 : case T_FuncExpr:
855 405242 : coll = ((const FuncExpr *) expr)->funccollid;
856 405242 : break;
857 0 : case T_NamedArgExpr:
858 0 : coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
859 0 : break;
860 101576 : case T_OpExpr:
861 101576 : coll = ((const OpExpr *) expr)->opcollid;
862 101576 : break;
863 130 : case T_DistinctExpr:
864 130 : coll = ((const DistinctExpr *) expr)->opcollid;
865 130 : break;
866 226 : case T_NullIfExpr:
867 226 : coll = ((const NullIfExpr *) expr)->opcollid;
868 226 : break;
869 17806 : case T_ScalarArrayOpExpr:
870 : /* ScalarArrayOpExpr's result is boolean ... */
871 17806 : coll = InvalidOid; /* ... so it has no collation */
872 17806 : break;
873 4536 : case T_BoolExpr:
874 : /* BoolExpr's result is boolean ... */
875 4536 : coll = InvalidOid; /* ... so it has no collation */
876 4536 : break;
877 3280 : case T_SubLink:
878 : {
879 3280 : const SubLink *sublink = (const SubLink *) expr;
880 :
881 3280 : if (sublink->subLinkType == EXPR_SUBLINK ||
882 298 : sublink->subLinkType == ARRAY_SUBLINK)
883 3208 : {
884 : /* get the collation of subselect's first target column */
885 3208 : Query *qtree = (Query *) sublink->subselect;
886 : TargetEntry *tent;
887 :
888 3208 : if (!qtree || !IsA(qtree, Query))
889 0 : elog(ERROR, "cannot get collation for untransformed sublink");
890 3208 : tent = linitial_node(TargetEntry, qtree->targetList);
891 : Assert(!tent->resjunk);
892 3208 : 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 72 : coll = InvalidOid; /* ... so it has no collation */
899 : }
900 : }
901 3280 : break;
902 20012 : case T_SubPlan:
903 : {
904 20012 : const SubPlan *subplan = (const SubPlan *) expr;
905 :
906 20012 : if (subplan->subLinkType == EXPR_SUBLINK ||
907 294 : subplan->subLinkType == ARRAY_SUBLINK)
908 : {
909 : /* get the collation of subselect's first target column */
910 19814 : 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 198 : coll = InvalidOid; /* ... so it has no collation */
917 : }
918 : }
919 20012 : 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 10100 : case T_FieldSelect:
929 10100 : coll = ((const FieldSelect *) expr)->resultcollid;
930 10100 : 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 102974 : case T_RelabelType:
936 102974 : coll = ((const RelabelType *) expr)->resultcollid;
937 102974 : break;
938 39718 : case T_CoerceViaIO:
939 39718 : coll = ((const CoerceViaIO *) expr)->resultcollid;
940 39718 : break;
941 1428 : case T_ArrayCoerceExpr:
942 1428 : coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
943 1428 : 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 176 : case T_CollateExpr:
949 176 : coll = ((const CollateExpr *) expr)->collOid;
950 176 : break;
951 144952 : case T_CaseExpr:
952 144952 : coll = ((const CaseExpr *) expr)->casecollid;
953 144952 : break;
954 31626 : case T_CaseTestExpr:
955 31626 : coll = ((const CaseTestExpr *) expr)->collation;
956 31626 : break;
957 25498 : case T_ArrayExpr:
958 25498 : coll = ((const ArrayExpr *) expr)->array_collid;
959 25498 : break;
960 4594 : case T_RowExpr:
961 : /* RowExpr's result is composite ... */
962 4594 : coll = InvalidOid; /* ... so it has no collation */
963 4594 : 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 3180 : case T_CoalesceExpr:
969 3180 : coll = ((const CoalesceExpr *) expr)->coalescecollid;
970 3180 : break;
971 2824 : case T_MinMaxExpr:
972 2824 : coll = ((const MinMaxExpr *) expr)->minmaxcollid;
973 2824 : break;
974 1458 : case T_SQLValueFunction:
975 : /* Returns either NAME or a non-collatable type */
976 1458 : if (((const SQLValueFunction *) expr)->type == NAMEOID)
977 1300 : coll = C_COLLATION_OID;
978 : else
979 158 : coll = InvalidOid;
980 1458 : break;
981 668 : 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 668 : if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
989 158 : coll = DEFAULT_COLLATION_OID;
990 : else
991 510 : coll = InvalidOid;
992 668 : break;
993 12 : case T_JsonValueExpr:
994 12 : coll = exprCollation((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
995 12 : break;
996 1290 : case T_JsonConstructorExpr:
997 : {
998 1290 : const JsonConstructorExpr *ctor = (const JsonConstructorExpr *) expr;
999 :
1000 1290 : if (ctor->coercion)
1001 236 : coll = exprCollation((Node *) ctor->coercion);
1002 : else
1003 1054 : coll = InvalidOid;
1004 : }
1005 1290 : 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 1826 : case T_NullTest:
1028 : /* NullTest's result is boolean ... */
1029 1826 : coll = InvalidOid; /* ... so it has no collation */
1030 1826 : break;
1031 422 : case T_BooleanTest:
1032 : /* BooleanTest's result is boolean ... */
1033 422 : coll = InvalidOid; /* ... so it has no collation */
1034 422 : break;
1035 64124 : case T_CoerceToDomain:
1036 64124 : coll = ((const CoerceToDomain *) expr)->resultcollid;
1037 64124 : break;
1038 748 : case T_CoerceToDomainValue:
1039 748 : coll = ((const CoerceToDomainValue *) expr)->collation;
1040 748 : break;
1041 27522 : case T_SetToDefault:
1042 27522 : coll = ((const SetToDefault *) expr)->collation;
1043 27522 : 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 382 : case T_NextValueExpr:
1049 : /* NextValueExpr's result is an integer type ... */
1050 382 : coll = InvalidOid; /* ... so it has no collation */
1051 382 : break;
1052 0 : case T_InferenceElem:
1053 0 : coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
1054 0 : break;
1055 408 : case T_ReturningExpr:
1056 408 : coll = exprCollation((Node *) ((const ReturningExpr *) expr)->retexpr);
1057 408 : break;
1058 4340 : case T_PlaceHolderVar:
1059 4340 : coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1060 4340 : 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 12681108 : 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 1298 : exprInputCollation(const Node *expr)
1077 : {
1078 : Oid coll;
1079 :
1080 1298 : if (!expr)
1081 0 : return InvalidOid;
1082 :
1083 1298 : 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 328 : case T_OpExpr:
1095 328 : coll = ((const OpExpr *) expr)->inputcollid;
1096 328 : 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 854 : default:
1110 854 : coll = InvalidOid;
1111 854 : break;
1112 : }
1113 1298 : 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 1833276 : exprSetCollation(Node *expr, Oid collation)
1125 : {
1126 1833276 : 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 44728 : case T_Aggref:
1138 44728 : ((Aggref *) expr)->aggcollid = collation;
1139 44728 : break;
1140 362 : case T_GroupingFunc:
1141 : Assert(!OidIsValid(collation));
1142 362 : break;
1143 3340 : case T_WindowFunc:
1144 3340 : ((WindowFunc *) expr)->wincollid = collation;
1145 3340 : break;
1146 192 : case T_MergeSupportFunc:
1147 192 : ((MergeSupportFunc *) expr)->msfcollid = collation;
1148 192 : break;
1149 12120 : case T_SubscriptingRef:
1150 12120 : ((SubscriptingRef *) expr)->refcollid = collation;
1151 12120 : break;
1152 449074 : case T_FuncExpr:
1153 449074 : ((FuncExpr *) expr)->funccollid = collation;
1154 449074 : break;
1155 48278 : case T_NamedArgExpr:
1156 : Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
1157 48278 : break;
1158 644968 : case T_OpExpr:
1159 644968 : ((OpExpr *) expr)->opcollid = collation;
1160 644968 : break;
1161 958 : case T_DistinctExpr:
1162 958 : ((DistinctExpr *) expr)->opcollid = collation;
1163 958 : break;
1164 258 : case T_NullIfExpr:
1165 258 : ((NullIfExpr *) expr)->opcollid = collation;
1166 258 : break;
1167 31974 : case T_ScalarArrayOpExpr:
1168 : /* ScalarArrayOpExpr's result is boolean ... */
1169 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1170 31974 : break;
1171 150410 : case T_BoolExpr:
1172 : /* BoolExpr's result is boolean ... */
1173 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1174 150410 : break;
1175 43608 : 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 43608 : 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 139770 : case T_RelabelType:
1209 139770 : ((RelabelType *) expr)->resultcollid = collation;
1210 139770 : break;
1211 24676 : case T_CoerceViaIO:
1212 24676 : ((CoerceViaIO *) expr)->resultcollid = collation;
1213 24676 : break;
1214 5192 : case T_ArrayCoerceExpr:
1215 5192 : ((ArrayCoerceExpr *) expr)->resultcollid = collation;
1216 5192 : 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 81662 : case T_CaseExpr:
1222 81662 : ((CaseExpr *) expr)->casecollid = collation;
1223 81662 : break;
1224 26484 : case T_ArrayExpr:
1225 26484 : ((ArrayExpr *) expr)->array_collid = collation;
1226 26484 : 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 6060 : case T_CoalesceExpr:
1236 6060 : ((CoalesceExpr *) expr)->coalescecollid = collation;
1237 6060 : break;
1238 318 : case T_MinMaxExpr:
1239 318 : ((MinMaxExpr *) expr)->minmaxcollid = collation;
1240 318 : break;
1241 2862 : case T_SQLValueFunction:
1242 : Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
1243 : (collation == C_COLLATION_OID) :
1244 : (collation == InvalidOid));
1245 2862 : break;
1246 768 : case T_XmlExpr:
1247 : Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
1248 : (collation == DEFAULT_COLLATION_OID) :
1249 : (collation == InvalidOid));
1250 768 : break;
1251 672 : case T_JsonValueExpr:
1252 672 : exprSetCollation((Node *) ((JsonValueExpr *) expr)->formatted_expr,
1253 : collation);
1254 672 : break;
1255 1372 : case T_JsonConstructorExpr:
1256 : {
1257 1372 : JsonConstructorExpr *ctor = (JsonConstructorExpr *) expr;
1258 :
1259 1372 : 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 1372 : 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 19092 : case T_NullTest:
1285 : /* NullTest's result is boolean ... */
1286 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1287 19092 : break;
1288 806 : case T_BooleanTest:
1289 : /* BooleanTest's result is boolean ... */
1290 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1291 806 : break;
1292 81106 : case T_CoerceToDomain:
1293 81106 : ((CoerceToDomain *) expr)->resultcollid = collation;
1294 81106 : 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 1833276 : }
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 1747046 : exprSetInputCollation(Node *expr, Oid inputcollation)
1325 : {
1326 1747046 : switch (nodeTag(expr))
1327 : {
1328 44728 : case T_Aggref:
1329 44728 : ((Aggref *) expr)->inputcollid = inputcollation;
1330 44728 : break;
1331 3340 : case T_WindowFunc:
1332 3340 : ((WindowFunc *) expr)->inputcollid = inputcollation;
1333 3340 : break;
1334 448864 : case T_FuncExpr:
1335 448864 : ((FuncExpr *) expr)->inputcollid = inputcollation;
1336 448864 : break;
1337 644956 : case T_OpExpr:
1338 644956 : ((OpExpr *) expr)->inputcollid = inputcollation;
1339 644956 : break;
1340 958 : case T_DistinctExpr:
1341 958 : ((DistinctExpr *) expr)->inputcollid = inputcollation;
1342 958 : break;
1343 258 : case T_NullIfExpr:
1344 258 : ((NullIfExpr *) expr)->inputcollid = inputcollation;
1345 258 : break;
1346 31974 : case T_ScalarArrayOpExpr:
1347 31974 : ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
1348 31974 : break;
1349 318 : case T_MinMaxExpr:
1350 318 : ((MinMaxExpr *) expr)->inputcollid = inputcollation;
1351 318 : break;
1352 571650 : default:
1353 571650 : break;
1354 : }
1355 1747046 : }
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 4238556 : exprLocation(const Node *expr)
1389 : {
1390 : int loc;
1391 :
1392 4238556 : if (expr == NULL)
1393 24700 : return -1;
1394 4213856 : 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 2026930 : case T_Var:
1403 2026930 : loc = ((const Var *) expr)->location;
1404 2026930 : break;
1405 1319246 : case T_Const:
1406 1319246 : loc = ((const Const *) expr)->location;
1407 1319246 : break;
1408 305894 : case T_Param:
1409 305894 : loc = ((const Param *) expr)->location;
1410 305894 : break;
1411 8678 : case T_Aggref:
1412 : /* function name should always be the first thing */
1413 8678 : loc = ((const Aggref *) expr)->location;
1414 8678 : break;
1415 70 : case T_GroupingFunc:
1416 70 : loc = ((const GroupingFunc *) expr)->location;
1417 70 : break;
1418 42 : case T_WindowFunc:
1419 : /* function name should always be the first thing */
1420 42 : loc = ((const WindowFunc *) expr)->location;
1421 42 : break;
1422 192 : case T_MergeSupportFunc:
1423 192 : loc = ((const MergeSupportFunc *) expr)->location;
1424 192 : 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 89120 : case T_FuncExpr:
1430 : {
1431 89120 : const FuncExpr *fexpr = (const FuncExpr *) expr;
1432 :
1433 : /* consider both function name and leftmost arg */
1434 89120 : loc = leftmostLoc(fexpr->location,
1435 89120 : exprLocation((Node *) fexpr->args));
1436 : }
1437 89120 : 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 12556 : case T_OpExpr:
1448 : case T_DistinctExpr: /* struct-equivalent to OpExpr */
1449 : case T_NullIfExpr: /* struct-equivalent to OpExpr */
1450 : {
1451 12556 : const OpExpr *opexpr = (const OpExpr *) expr;
1452 :
1453 : /* consider both operator name and leftmost arg */
1454 12556 : loc = leftmostLoc(opexpr->location,
1455 12556 : exprLocation((Node *) opexpr->args));
1456 : }
1457 12556 : 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 3894 : case T_FieldSelect:
1490 : /* just use argument's location */
1491 3894 : loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
1492 3894 : 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 13112 : case T_RelabelType:
1498 : {
1499 13112 : const RelabelType *rexpr = (const RelabelType *) expr;
1500 :
1501 : /* Much as above */
1502 13112 : loc = leftmostLoc(rexpr->location,
1503 13112 : exprLocation((Node *) rexpr->arg));
1504 : }
1505 13112 : break;
1506 23392 : case T_CoerceViaIO:
1507 : {
1508 23392 : const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
1509 :
1510 : /* Much as above */
1511 23392 : loc = leftmostLoc(cexpr->location,
1512 23392 : exprLocation((Node *) cexpr->arg));
1513 : }
1514 23392 : 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 10440 : case T_CaseExpr:
1538 : /* CASE keyword should always be the first thing */
1539 10440 : loc = ((const CaseExpr *) expr)->location;
1540 10440 : 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 282 : case T_RowExpr:
1550 : /* the location points at ROW or (, which must be leftmost */
1551 282 : loc = ((const RowExpr *) expr)->location;
1552 282 : 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 1896 : case T_SQLValueFunction:
1566 : /* function keyword should always be the first thing */
1567 1896 : loc = ((const SQLValueFunction *) expr)->location;
1568 1896 : break;
1569 202 : case T_XmlExpr:
1570 : {
1571 202 : const XmlExpr *xexpr = (const XmlExpr *) expr;
1572 :
1573 : /* consider both function name and leftmost arg */
1574 202 : loc = leftmostLoc(xexpr->location,
1575 202 : exprLocation((Node *) xexpr->args));
1576 : }
1577 202 : 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 966 : case T_CoerceToDomainValue:
1630 966 : loc = ((const CoerceToDomainValue *) expr)->location;
1631 966 : break;
1632 53494 : case T_SetToDefault:
1633 53494 : loc = ((const SetToDefault *) expr)->location;
1634 53494 : 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 84552 : case T_List:
1647 : {
1648 : /* report location of first list member that has a location */
1649 : ListCell *lc;
1650 :
1651 84552 : loc = -1; /* just to suppress compiler warning */
1652 85368 : foreach(lc, (const List *) expr)
1653 : {
1654 84946 : loc = exprLocation((Node *) lfirst(lc));
1655 84946 : if (loc >= 0)
1656 84130 : break;
1657 : }
1658 : }
1659 84552 : break;
1660 4992 : case T_A_Expr:
1661 : {
1662 4992 : 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 4992 : loc = leftmostLoc(aexpr->location,
1667 4992 : exprLocation(aexpr->lexpr));
1668 : }
1669 4992 : break;
1670 70288 : case T_ColumnRef:
1671 70288 : loc = ((const ColumnRef *) expr)->location;
1672 70288 : break;
1673 0 : case T_ParamRef:
1674 0 : loc = ((const ParamRef *) expr)->location;
1675 0 : break;
1676 57070 : case T_A_Const:
1677 57070 : loc = ((const A_Const *) expr)->location;
1678 57070 : break;
1679 3496 : case T_FuncCall:
1680 : {
1681 3496 : 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 3496 : loc = leftmostLoc(fc->location,
1686 3496 : exprLocation((Node *) fc->args));
1687 : }
1688 3496 : 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 7194 : case T_TypeCast:
1701 : {
1702 7194 : 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 7194 : loc = exprLocation(tc->arg);
1709 7194 : loc = leftmostLoc(loc, tc->typeName->location);
1710 7194 : loc = leftmostLoc(loc, tc->location);
1711 : }
1712 7194 : break;
1713 570 : case T_CollateClause:
1714 : /* just use argument's location */
1715 570 : loc = exprLocation(((const CollateClause *) expr)->arg);
1716 570 : 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 34172 : default:
1807 : /* for any other node type it's just unknown... */
1808 34172 : loc = -1;
1809 34172 : break;
1810 : }
1811 4213856 : 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 239046 : leftmostLoc(int loc1, int loc2)
1821 : {
1822 239046 : if (loc1 < 0)
1823 21454 : return loc2;
1824 217592 : else if (loc2 < 0)
1825 23716 : return loc1;
1826 : else
1827 193876 : 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 469016 : fix_opfuncids(Node *node)
1842 : {
1843 : /* This tree walk requires no special setup, so away we go... */
1844 469016 : fix_opfuncids_walker(node, NULL);
1845 469016 : }
1846 :
1847 : static bool
1848 1015454 : fix_opfuncids_walker(Node *node, void *context)
1849 : {
1850 1015454 : if (node == NULL)
1851 55928 : return false;
1852 959526 : if (IsA(node, OpExpr))
1853 56324 : set_opfuncid((OpExpr *) node);
1854 903202 : else if (IsA(node, DistinctExpr))
1855 6 : set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1856 903196 : else if (IsA(node, NullIfExpr))
1857 84 : set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1858 903112 : else if (IsA(node, ScalarArrayOpExpr))
1859 2172 : set_sa_opfuncid((ScalarArrayOpExpr *) node);
1860 959526 : 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 3634802 : set_opfuncid(OpExpr *opexpr)
1873 : {
1874 3634802 : if (opexpr->opfuncid == InvalidOid)
1875 194364 : opexpr->opfuncid = get_opcode(opexpr->opno);
1876 3634802 : }
1877 :
1878 : /*
1879 : * set_sa_opfuncid
1880 : * As above, for ScalarArrayOpExpr nodes.
1881 : */
1882 : void
1883 172624 : set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1884 : {
1885 172624 : if (opexpr->opfuncid == InvalidOid)
1886 492 : opexpr->opfuncid = get_opcode(opexpr->opno);
1887 172624 : }
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 19713352 : check_functions_in_node(Node *node, check_function_callback checker,
1911 : void *context)
1912 : {
1913 19713352 : switch (nodeTag(node))
1914 : {
1915 98062 : case T_Aggref:
1916 : {
1917 98062 : Aggref *expr = (Aggref *) node;
1918 :
1919 98062 : if (checker(expr->aggfnoid, context))
1920 1052 : return true;
1921 : }
1922 97010 : break;
1923 7112 : case T_WindowFunc:
1924 : {
1925 7112 : WindowFunc *expr = (WindowFunc *) node;
1926 :
1927 7112 : if (checker(expr->winfnoid, context))
1928 150 : return true;
1929 : }
1930 6962 : break;
1931 588832 : case T_FuncExpr:
1932 : {
1933 588832 : FuncExpr *expr = (FuncExpr *) node;
1934 :
1935 588832 : if (checker(expr->funcid, context))
1936 121498 : return true;
1937 : }
1938 467334 : break;
1939 1351428 : case T_OpExpr:
1940 : case T_DistinctExpr: /* struct-equivalent to OpExpr */
1941 : case T_NullIfExpr: /* struct-equivalent to OpExpr */
1942 : {
1943 1351428 : OpExpr *expr = (OpExpr *) node;
1944 :
1945 : /* Set opfuncid if it wasn't set already */
1946 1351428 : set_opfuncid(expr);
1947 1351428 : if (checker(expr->opfuncid, context))
1948 828 : return true;
1949 : }
1950 1350600 : break;
1951 56962 : case T_ScalarArrayOpExpr:
1952 : {
1953 56962 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1954 :
1955 56962 : set_sa_opfuncid(expr);
1956 56962 : if (checker(expr->opfuncid, context))
1957 90 : return true;
1958 : }
1959 56872 : break;
1960 37488 : case T_CoerceViaIO:
1961 : {
1962 37488 : CoerceViaIO *expr = (CoerceViaIO *) node;
1963 : Oid iofunc;
1964 : Oid typioparam;
1965 : bool typisvarlena;
1966 :
1967 : /* check the result type's input function */
1968 37488 : getTypeInputInfo(expr->resulttype,
1969 : &iofunc, &typioparam);
1970 37488 : if (checker(iofunc, context))
1971 672 : return true;
1972 : /* check the input type's output function */
1973 37440 : getTypeOutputInfo(exprType((Node *) expr->arg),
1974 : &iofunc, &typisvarlena);
1975 37440 : if (checker(iofunc, context))
1976 624 : return true;
1977 : }
1978 36816 : 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 17573174 : default:
1994 17573174 : break;
1995 : }
1996 19589062 : 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 93351786 : 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 93351786 : if (node == NULL)
2112 1969900 : return false;
2113 :
2114 : /* Guard against stack overflow due to overly complex expressions */
2115 91381886 : check_stack_depth();
2116 :
2117 91381886 : switch (nodeTag(node))
2118 : {
2119 36301720 : 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 36301720 : break;
2134 7446 : case T_WithCheckOption:
2135 7446 : return WALK(((WithCheckOption *) node)->qual);
2136 297972 : case T_Aggref:
2137 : {
2138 297972 : Aggref *expr = (Aggref *) node;
2139 :
2140 : /* recurse directly on Lists */
2141 297972 : if (LIST_WALK(expr->aggdirectargs))
2142 0 : return true;
2143 297972 : if (LIST_WALK(expr->args))
2144 20808 : return true;
2145 277164 : if (LIST_WALK(expr->aggorder))
2146 0 : return true;
2147 277164 : if (LIST_WALK(expr->aggdistinct))
2148 0 : return true;
2149 277164 : if (WALK(expr->aggfilter))
2150 70 : return true;
2151 : }
2152 277094 : break;
2153 3738 : case T_GroupingFunc:
2154 : {
2155 3738 : GroupingFunc *grouping = (GroupingFunc *) node;
2156 :
2157 3738 : if (LIST_WALK(grouping->args))
2158 246 : return true;
2159 : }
2160 3492 : break;
2161 17242 : case T_WindowFunc:
2162 : {
2163 17242 : WindowFunc *expr = (WindowFunc *) node;
2164 :
2165 : /* recurse directly on List */
2166 17242 : if (LIST_WALK(expr->args))
2167 630 : return true;
2168 16612 : if (WALK(expr->aggfilter))
2169 12 : return true;
2170 16600 : if (WALK(expr->runCondition))
2171 0 : return true;
2172 : }
2173 16600 : 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 201956 : case T_SubscriptingRef:
2183 : {
2184 201956 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
2185 :
2186 : /* recurse directly for upper/lower container index lists */
2187 201956 : if (LIST_WALK(sbsref->refupperindexpr))
2188 10822 : return true;
2189 191134 : if (LIST_WALK(sbsref->reflowerindexpr))
2190 0 : return true;
2191 : /* walker must see the refexpr and refassgnexpr, however */
2192 191134 : if (WALK(sbsref->refexpr))
2193 9952 : return true;
2194 :
2195 181182 : if (WALK(sbsref->refassgnexpr))
2196 154 : return true;
2197 : }
2198 181028 : break;
2199 3443852 : case T_FuncExpr:
2200 : {
2201 3443852 : FuncExpr *expr = (FuncExpr *) node;
2202 :
2203 3443852 : if (LIST_WALK(expr->args))
2204 71924 : return true;
2205 : }
2206 3371922 : break;
2207 100712 : case T_NamedArgExpr:
2208 100712 : return WALK(((NamedArgExpr *) node)->arg);
2209 7759588 : case T_OpExpr:
2210 : case T_DistinctExpr: /* struct-equivalent to OpExpr */
2211 : case T_NullIfExpr: /* struct-equivalent to OpExpr */
2212 : {
2213 7759588 : OpExpr *expr = (OpExpr *) node;
2214 :
2215 7759588 : if (LIST_WALK(expr->args))
2216 67884 : return true;
2217 : }
2218 7691602 : break;
2219 405490 : case T_ScalarArrayOpExpr:
2220 : {
2221 405490 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2222 :
2223 405490 : if (LIST_WALK(expr->args))
2224 37840 : return true;
2225 : }
2226 367650 : break;
2227 998036 : case T_BoolExpr:
2228 : {
2229 998036 : BoolExpr *expr = (BoolExpr *) node;
2230 :
2231 998036 : if (LIST_WALK(expr->args))
2232 16950 : return true;
2233 : }
2234 981080 : break;
2235 219774 : case T_SubLink:
2236 : {
2237 219774 : SubLink *sublink = (SubLink *) node;
2238 :
2239 219774 : 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 219720 : return WALK(sublink->subselect);
2247 : }
2248 : break;
2249 99984 : case T_SubPlan:
2250 : {
2251 99984 : SubPlan *subplan = (SubPlan *) node;
2252 :
2253 : /* recurse into the testexpr, but not into the Plan */
2254 99984 : if (WALK(subplan->testexpr))
2255 72 : return true;
2256 : /* also examine args list */
2257 99912 : if (LIST_WALK(subplan->args))
2258 402 : return true;
2259 : }
2260 99510 : break;
2261 7006 : case T_AlternativeSubPlan:
2262 7006 : return LIST_WALK(((AlternativeSubPlan *) node)->subplans);
2263 88758 : case T_FieldSelect:
2264 88758 : return WALK(((FieldSelect *) node)->arg);
2265 3086 : case T_FieldStore:
2266 : {
2267 3086 : FieldStore *fstore = (FieldStore *) node;
2268 :
2269 3086 : if (WALK(fstore->arg))
2270 0 : return true;
2271 3086 : if (WALK(fstore->newvals))
2272 12 : return true;
2273 : }
2274 3074 : break;
2275 1093788 : case T_RelabelType:
2276 1093788 : return WALK(((RelabelType *) node)->arg);
2277 224454 : case T_CoerceViaIO:
2278 224454 : return WALK(((CoerceViaIO *) node)->arg);
2279 48974 : case T_ArrayCoerceExpr:
2280 : {
2281 48974 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2282 :
2283 48974 : if (WALK(acoerce->arg))
2284 3840 : return true;
2285 45134 : if (WALK(acoerce->elemexpr))
2286 24 : return true;
2287 : }
2288 45110 : break;
2289 3118 : case T_ConvertRowtypeExpr:
2290 3118 : return WALK(((ConvertRowtypeExpr *) node)->arg);
2291 30862 : case T_CollateExpr:
2292 30862 : return WALK(((CollateExpr *) node)->arg);
2293 480574 : case T_CaseExpr:
2294 : {
2295 480574 : CaseExpr *caseexpr = (CaseExpr *) node;
2296 :
2297 480574 : if (WALK(caseexpr->arg))
2298 76 : return true;
2299 : /* we assume walker doesn't care about CaseWhens, either */
2300 1301416 : foreach(temp, caseexpr->args)
2301 : {
2302 829526 : CaseWhen *when = lfirst_node(CaseWhen, temp);
2303 :
2304 829526 : if (WALK(when->expr))
2305 8608 : return true;
2306 826902 : if (WALK(when->result))
2307 5984 : return true;
2308 : }
2309 471890 : if (WALK(caseexpr->defresult))
2310 9420 : return true;
2311 : }
2312 462470 : break;
2313 205342 : case T_ArrayExpr:
2314 205342 : return WALK(((ArrayExpr *) node)->elements);
2315 32898 : case T_RowExpr:
2316 : /* Assume colnames isn't interesting */
2317 32898 : 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 49858 : case T_CoalesceExpr:
2329 49858 : return WALK(((CoalesceExpr *) node)->args);
2330 5482 : case T_MinMaxExpr:
2331 5482 : return WALK(((MinMaxExpr *) node)->args);
2332 5058 : case T_XmlExpr:
2333 : {
2334 5058 : XmlExpr *xexpr = (XmlExpr *) node;
2335 :
2336 5058 : if (WALK(xexpr->named_args))
2337 12 : return true;
2338 : /* we assume walker doesn't care about arg_names */
2339 5046 : if (WALK(xexpr->args))
2340 24 : return true;
2341 : }
2342 5022 : 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 9510 : case T_JsonConstructorExpr:
2354 : {
2355 9510 : JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
2356 :
2357 9510 : if (WALK(ctor->args))
2358 48 : return true;
2359 9462 : if (WALK(ctor->func))
2360 96 : return true;
2361 9366 : if (WALK(ctor->coercion))
2362 12 : return true;
2363 : }
2364 9354 : 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 229946 : case T_NullTest:
2393 229946 : return WALK(((NullTest *) node)->arg);
2394 12308 : case T_BooleanTest:
2395 12308 : return WALK(((BooleanTest *) node)->arg);
2396 346958 : case T_CoerceToDomain:
2397 346958 : return WALK(((CoerceToDomain *) node)->arg);
2398 15282256 : case T_TargetEntry:
2399 15282256 : return WALK(((TargetEntry *) node)->expr);
2400 106372 : case T_Query:
2401 : /* Do nothing with a sub-Query, per discussion above */
2402 106372 : 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 7684 : case T_CommonTableExpr:
2428 : {
2429 7684 : 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 7684 : if (WALK(cte->ctequery))
2436 92 : return true;
2437 :
2438 7592 : if (WALK(cte->search_clause))
2439 0 : return true;
2440 7592 : if (WALK(cte->cycle_clause))
2441 0 : return true;
2442 : }
2443 7592 : 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 3754 : case T_PartitionBoundSpec:
2512 : {
2513 3754 : PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
2514 :
2515 3754 : if (WALK(pbs->listdatums))
2516 0 : return true;
2517 3754 : if (WALK(pbs->lowerdatums))
2518 0 : return true;
2519 3754 : if (WALK(pbs->upperdatums))
2520 0 : return true;
2521 : }
2522 3754 : break;
2523 5052 : case T_PartitionRangeDatum:
2524 : {
2525 5052 : PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
2526 :
2527 5052 : if (WALK(prd->value))
2528 0 : return true;
2529 : }
2530 5052 : break;
2531 21343624 : case T_List:
2532 71903190 : foreach(temp, (List *) node)
2533 : {
2534 51336054 : if (WALK(lfirst(temp)))
2535 776284 : return true;
2536 : }
2537 20567136 : break;
2538 1322666 : case T_FromExpr:
2539 : {
2540 1322666 : FromExpr *from = (FromExpr *) node;
2541 :
2542 1322666 : if (LIST_WALK(from->fromlist))
2543 63662 : return true;
2544 1259004 : if (WALK(from->quals))
2545 3222 : return true;
2546 : }
2547 1255770 : 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 6354 : case T_MergeAction:
2565 : {
2566 6354 : MergeAction *action = (MergeAction *) node;
2567 :
2568 6354 : if (WALK(action->qual))
2569 134 : return true;
2570 6220 : if (WALK(action->targetList))
2571 350 : return true;
2572 : }
2573 5870 : break;
2574 722 : case T_PartitionPruneStepOp:
2575 : {
2576 722 : PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
2577 :
2578 722 : if (WALK(opstep->exprs))
2579 0 : return true;
2580 : }
2581 722 : break;
2582 144 : case T_PartitionPruneStepCombine:
2583 : /* no expression subnodes */
2584 144 : break;
2585 275092 : case T_JoinExpr:
2586 : {
2587 275092 : JoinExpr *join = (JoinExpr *) node;
2588 :
2589 275092 : if (WALK(join->larg))
2590 17050 : return true;
2591 258042 : if (WALK(join->rarg))
2592 18588 : return true;
2593 239454 : if (WALK(join->quals))
2594 48 : return true;
2595 :
2596 : /*
2597 : * alias clause, using list are deemed uninteresting.
2598 : */
2599 : }
2600 239406 : break;
2601 22686 : case T_SetOperationStmt:
2602 : {
2603 22686 : SetOperationStmt *setop = (SetOperationStmt *) node;
2604 :
2605 22686 : if (WALK(setop->larg))
2606 0 : return true;
2607 22686 : if (WALK(setop->rarg))
2608 0 : return true;
2609 :
2610 : /* groupClauses are deemed uninteresting */
2611 : }
2612 22686 : 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 20502 : case T_PlaceHolderVar:
2624 20502 : return WALK(((PlaceHolderVar *) node)->phexpr);
2625 2862 : case T_InferenceElem:
2626 2862 : return WALK(((InferenceElem *) node)->expr);
2627 3252 : case T_ReturningExpr:
2628 3252 : return WALK(((ReturningExpr *) node)->retexpr);
2629 1192 : case T_AppendRelInfo:
2630 : {
2631 1192 : AppendRelInfo *appinfo = (AppendRelInfo *) node;
2632 :
2633 1192 : if (LIST_WALK(appinfo->translated_vars))
2634 0 : return true;
2635 : }
2636 1192 : break;
2637 0 : case T_PlaceHolderInfo:
2638 0 : return WALK(((PlaceHolderInfo *) node)->ph_var);
2639 176606 : case T_RangeTblFunction:
2640 176606 : return WALK(((RangeTblFunction *) node)->funcexpr);
2641 746 : case T_TableSampleClause:
2642 : {
2643 746 : TableSampleClause *tsc = (TableSampleClause *) node;
2644 :
2645 746 : if (LIST_WALK(tsc->args))
2646 0 : return true;
2647 746 : if (WALK(tsc->repeatable))
2648 0 : return true;
2649 : }
2650 746 : 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 72095174 : 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 1633320 : 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 1633320 : if (WALK(query->targetList))
2713 312038 : return true;
2714 1321252 : if (WALK(query->withCheckOptions))
2715 0 : return true;
2716 1321252 : if (WALK(query->onConflict))
2717 0 : return true;
2718 1321252 : if (WALK(query->mergeActionList))
2719 484 : return true;
2720 1320768 : if (WALK(query->mergeJoinCondition))
2721 260 : return true;
2722 1320508 : if (WALK(query->returningList))
2723 90 : return true;
2724 1320418 : if (WALK(query->jointree))
2725 66440 : return true;
2726 1253966 : if (WALK(query->setOperations))
2727 0 : return true;
2728 1253966 : if (WALK(query->havingQual))
2729 0 : return true;
2730 1253966 : if (WALK(query->limitOffset))
2731 6 : return true;
2732 1253960 : 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 1253960 : if ((flags & QTW_EXAMINE_SORTGROUP))
2741 : {
2742 33796 : if (WALK(query->groupClause))
2743 0 : return true;
2744 33796 : if (WALK(query->windowClause))
2745 0 : return true;
2746 33796 : if (WALK(query->sortClause))
2747 0 : return true;
2748 33796 : 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 1225758 : foreach(lc, query->windowClause)
2760 : {
2761 5600 : WindowClause *wc = lfirst_node(WindowClause, lc);
2762 :
2763 5600 : if (WALK(wc->startOffset))
2764 6 : return true;
2765 5594 : 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 1253954 : if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2783 : {
2784 648582 : if (WALK(query->cteList))
2785 86 : return true;
2786 : }
2787 1253868 : if (!(flags & QTW_IGNORE_RANGE_TABLE))
2788 : {
2789 688974 : if (range_table_walker(query->rtable, walker, context, flags))
2790 17554 : return true;
2791 : }
2792 1236314 : 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 690504 : range_table_walker_impl(List *rtable,
2802 : tree_walker_callback walker,
2803 : void *context,
2804 : int flags)
2805 : {
2806 : ListCell *rt;
2807 :
2808 1674204 : foreach(rt, rtable)
2809 : {
2810 1001254 : RangeTblEntry *rte = lfirst_node(RangeTblEntry, rt);
2811 :
2812 1001254 : if (range_table_entry_walker(rte, walker, context, flags))
2813 17554 : return true;
2814 : }
2815 672950 : return false;
2816 : }
2817 :
2818 : /*
2819 : * Some callers even want to scan the expressions in individual RTEs.
2820 : */
2821 : bool
2822 1001278 : 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 1001278 : if (flags & QTW_EXAMINE_RTES_BEFORE)
2833 80498 : if (WALK(rte))
2834 12 : return true;
2835 :
2836 1001266 : switch (rte->rtekind)
2837 : {
2838 631222 : case RTE_RELATION:
2839 631222 : if (WALK(rte->tablesample))
2840 0 : return true;
2841 631222 : break;
2842 85098 : case RTE_SUBQUERY:
2843 85098 : if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2844 83710 : if (WALK(rte->subquery))
2845 832 : return true;
2846 84266 : break;
2847 155962 : case RTE_JOIN:
2848 155962 : if (!(flags & QTW_IGNORE_JOINALIASES))
2849 129884 : if (WALK(rte->joinaliasvars))
2850 0 : return true;
2851 155962 : break;
2852 75310 : case RTE_FUNCTION:
2853 75310 : if (WALK(rte->functions))
2854 16670 : return true;
2855 58640 : break;
2856 988 : case RTE_TABLEFUNC:
2857 988 : if (WALK(rte->tablefunc))
2858 0 : return true;
2859 988 : break;
2860 17498 : case RTE_VALUES:
2861 17498 : if (WALK(rte->values_lists))
2862 64 : return true;
2863 17434 : break;
2864 28988 : case RTE_CTE:
2865 : case RTE_NAMEDTUPLESTORE:
2866 : case RTE_RESULT:
2867 : /* nothing to do */
2868 28988 : break;
2869 6200 : case RTE_GROUP:
2870 6200 : if (!(flags & QTW_IGNORE_GROUPEXPRS))
2871 6200 : if (WALK(rte->groupexprs))
2872 0 : return true;
2873 6200 : break;
2874 : }
2875 :
2876 983700 : if (WALK(rte->securityQuals))
2877 0 : return true;
2878 :
2879 983700 : if (flags & QTW_EXAMINE_RTES_AFTER)
2880 17582 : if (WALK(rte))
2881 0 : return true;
2882 :
2883 983700 : 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 16500344 : 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 16500344 : if (node == NULL)
2967 111332 : return NULL;
2968 :
2969 : /* Guard against stack overflow due to overly complex expressions */
2970 16389012 : check_stack_depth();
2971 :
2972 16389012 : 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 3031466 : case T_Var:
2980 : {
2981 3031466 : Var *var = (Var *) node;
2982 : Var *newnode;
2983 :
2984 3031466 : FLATCOPY(newnode, var, Var);
2985 : /* Assume we need not copy the varnullingrels bitmapset */
2986 3031466 : return (Node *) newnode;
2987 : }
2988 : break;
2989 2717722 : case T_Const:
2990 : {
2991 2717722 : Const *oldnode = (Const *) node;
2992 : Const *newnode;
2993 :
2994 2717722 : FLATCOPY(newnode, oldnode, Const);
2995 : /* XXX we don't bother with datumCopy; should we? */
2996 2717722 : return (Node *) newnode;
2997 : }
2998 : break;
2999 115806 : 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 115806 : return copyObject(node);
3012 1126 : case T_WithCheckOption:
3013 : {
3014 1126 : WithCheckOption *wco = (WithCheckOption *) node;
3015 : WithCheckOption *newnode;
3016 :
3017 1126 : FLATCOPY(newnode, wco, WithCheckOption);
3018 1126 : MUTATE(newnode->qual, wco->qual, Node *);
3019 1126 : return (Node *) newnode;
3020 : }
3021 116358 : case T_Aggref:
3022 : {
3023 116358 : Aggref *aggref = (Aggref *) node;
3024 : Aggref *newnode;
3025 :
3026 116358 : FLATCOPY(newnode, aggref, Aggref);
3027 : /* assume mutation doesn't change types of arguments */
3028 116358 : newnode->aggargtypes = list_copy(aggref->aggargtypes);
3029 116358 : MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
3030 116358 : MUTATE(newnode->args, aggref->args, List *);
3031 116358 : MUTATE(newnode->aggorder, aggref->aggorder, List *);
3032 116358 : MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
3033 116358 : MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
3034 116358 : 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 4738 : case T_WindowFunc:
3061 : {
3062 4738 : WindowFunc *wfunc = (WindowFunc *) node;
3063 : WindowFunc *newnode;
3064 :
3065 4738 : FLATCOPY(newnode, wfunc, WindowFunc);
3066 4738 : MUTATE(newnode->args, wfunc->args, List *);
3067 4738 : MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
3068 4738 : 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 46996 : case T_SubscriptingRef:
3082 : {
3083 46996 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
3084 : SubscriptingRef *newnode;
3085 :
3086 46996 : FLATCOPY(newnode, sbsref, SubscriptingRef);
3087 46996 : MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
3088 : List *);
3089 46996 : MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
3090 : List *);
3091 46996 : MUTATE(newnode->refexpr, sbsref->refexpr,
3092 : Expr *);
3093 46996 : MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
3094 : Expr *);
3095 :
3096 46996 : return (Node *) newnode;
3097 : }
3098 : break;
3099 289464 : case T_FuncExpr:
3100 : {
3101 289464 : FuncExpr *expr = (FuncExpr *) node;
3102 : FuncExpr *newnode;
3103 :
3104 289464 : FLATCOPY(newnode, expr, FuncExpr);
3105 289464 : MUTATE(newnode->args, expr->args, List *);
3106 289464 : 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 1044552 : case T_OpExpr:
3120 : {
3121 1044552 : OpExpr *expr = (OpExpr *) node;
3122 : OpExpr *newnode;
3123 :
3124 1044552 : FLATCOPY(newnode, expr, OpExpr);
3125 1044552 : MUTATE(newnode->args, expr->args, List *);
3126 1044546 : return (Node *) newnode;
3127 : }
3128 : break;
3129 2170 : case T_DistinctExpr:
3130 : {
3131 2170 : DistinctExpr *expr = (DistinctExpr *) node;
3132 : DistinctExpr *newnode;
3133 :
3134 2170 : FLATCOPY(newnode, expr, DistinctExpr);
3135 2170 : MUTATE(newnode->args, expr->args, List *);
3136 2170 : return (Node *) newnode;
3137 : }
3138 : break;
3139 266 : case T_NullIfExpr:
3140 : {
3141 266 : NullIfExpr *expr = (NullIfExpr *) node;
3142 : NullIfExpr *newnode;
3143 :
3144 266 : FLATCOPY(newnode, expr, NullIfExpr);
3145 266 : MUTATE(newnode->args, expr->args, List *);
3146 266 : return (Node *) newnode;
3147 : }
3148 : break;
3149 77024 : case T_ScalarArrayOpExpr:
3150 : {
3151 77024 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3152 : ScalarArrayOpExpr *newnode;
3153 :
3154 77024 : FLATCOPY(newnode, expr, ScalarArrayOpExpr);
3155 77024 : MUTATE(newnode->args, expr->args, List *);
3156 77024 : return (Node *) newnode;
3157 : }
3158 : break;
3159 125976 : case T_BoolExpr:
3160 : {
3161 125976 : BoolExpr *expr = (BoolExpr *) node;
3162 : BoolExpr *newnode;
3163 :
3164 125976 : FLATCOPY(newnode, expr, BoolExpr);
3165 125976 : MUTATE(newnode->args, expr->args, List *);
3166 125970 : return (Node *) newnode;
3167 : }
3168 : break;
3169 49346 : case T_SubLink:
3170 : {
3171 49346 : SubLink *sublink = (SubLink *) node;
3172 : SubLink *newnode;
3173 :
3174 49346 : FLATCOPY(newnode, sublink, SubLink);
3175 49346 : 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 49346 : MUTATE(newnode->subselect, sublink->subselect, Node *);
3182 49346 : return (Node *) newnode;
3183 : }
3184 : break;
3185 16890 : case T_SubPlan:
3186 : {
3187 16890 : SubPlan *subplan = (SubPlan *) node;
3188 : SubPlan *newnode;
3189 :
3190 16890 : FLATCOPY(newnode, subplan, SubPlan);
3191 : /* transform testexpr */
3192 16890 : MUTATE(newnode->testexpr, subplan->testexpr, Node *);
3193 : /* transform args list (params to be passed to subplan) */
3194 16890 : MUTATE(newnode->args, subplan->args, List *);
3195 : /* but not the sub-Plan itself, which is referenced as-is */
3196 16890 : 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 7842 : case T_FieldSelect:
3210 : {
3211 7842 : FieldSelect *fselect = (FieldSelect *) node;
3212 : FieldSelect *newnode;
3213 :
3214 7842 : FLATCOPY(newnode, fselect, FieldSelect);
3215 7842 : MUTATE(newnode->arg, fselect->arg, Expr *);
3216 7842 : 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 128994 : case T_RelabelType:
3232 : {
3233 128994 : RelabelType *relabel = (RelabelType *) node;
3234 : RelabelType *newnode;
3235 :
3236 128994 : FLATCOPY(newnode, relabel, RelabelType);
3237 128994 : MUTATE(newnode->arg, relabel->arg, Expr *);
3238 128994 : return (Node *) newnode;
3239 : }
3240 : break;
3241 27696 : case T_CoerceViaIO:
3242 : {
3243 27696 : CoerceViaIO *iocoerce = (CoerceViaIO *) node;
3244 : CoerceViaIO *newnode;
3245 :
3246 27696 : FLATCOPY(newnode, iocoerce, CoerceViaIO);
3247 27696 : MUTATE(newnode->arg, iocoerce->arg, Expr *);
3248 27696 : return (Node *) newnode;
3249 : }
3250 : break;
3251 12422 : case T_ArrayCoerceExpr:
3252 : {
3253 12422 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
3254 : ArrayCoerceExpr *newnode;
3255 :
3256 12422 : FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
3257 12422 : MUTATE(newnode->arg, acoerce->arg, Expr *);
3258 12422 : MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
3259 12422 : 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 6550 : case T_CollateExpr:
3273 : {
3274 6550 : CollateExpr *collate = (CollateExpr *) node;
3275 : CollateExpr *newnode;
3276 :
3277 6550 : FLATCOPY(newnode, collate, CollateExpr);
3278 6550 : MUTATE(newnode->arg, collate->arg, Expr *);
3279 6550 : return (Node *) newnode;
3280 : }
3281 : break;
3282 69272 : case T_CaseExpr:
3283 : {
3284 69272 : CaseExpr *caseexpr = (CaseExpr *) node;
3285 : CaseExpr *newnode;
3286 :
3287 69272 : FLATCOPY(newnode, caseexpr, CaseExpr);
3288 69272 : MUTATE(newnode->arg, caseexpr->arg, Expr *);
3289 69272 : MUTATE(newnode->args, caseexpr->args, List *);
3290 69272 : MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
3291 69272 : return (Node *) newnode;
3292 : }
3293 : break;
3294 122050 : case T_CaseWhen:
3295 : {
3296 122050 : CaseWhen *casewhen = (CaseWhen *) node;
3297 : CaseWhen *newnode;
3298 :
3299 122050 : FLATCOPY(newnode, casewhen, CaseWhen);
3300 122050 : MUTATE(newnode->expr, casewhen->expr, Expr *);
3301 122050 : MUTATE(newnode->result, casewhen->result, Expr *);
3302 122050 : return (Node *) newnode;
3303 : }
3304 : break;
3305 40576 : case T_ArrayExpr:
3306 : {
3307 40576 : ArrayExpr *arrayexpr = (ArrayExpr *) node;
3308 : ArrayExpr *newnode;
3309 :
3310 40576 : FLATCOPY(newnode, arrayexpr, ArrayExpr);
3311 40576 : MUTATE(newnode->elements, arrayexpr->elements, List *);
3312 40576 : return (Node *) newnode;
3313 : }
3314 : break;
3315 8946 : case T_RowExpr:
3316 : {
3317 8946 : RowExpr *rowexpr = (RowExpr *) node;
3318 : RowExpr *newnode;
3319 :
3320 8946 : FLATCOPY(newnode, rowexpr, RowExpr);
3321 8946 : MUTATE(newnode->args, rowexpr->args, List *);
3322 : /* Assume colnames needn't be duplicated */
3323 8946 : 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 10186 : case T_CoalesceExpr:
3338 : {
3339 10186 : CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3340 : CoalesceExpr *newnode;
3341 :
3342 10186 : FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
3343 10186 : MUTATE(newnode->args, coalesceexpr->args, List *);
3344 10186 : return (Node *) newnode;
3345 : }
3346 : break;
3347 1186 : case T_MinMaxExpr:
3348 : {
3349 1186 : MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3350 : MinMaxExpr *newnode;
3351 :
3352 1186 : FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
3353 1186 : MUTATE(newnode->args, minmaxexpr->args, List *);
3354 1186 : 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 2446 : case T_JsonReturning:
3370 : {
3371 2446 : JsonReturning *jr = (JsonReturning *) node;
3372 : JsonReturning *newnode;
3373 :
3374 2446 : FLATCOPY(newnode, jr, JsonReturning);
3375 2446 : MUTATE(newnode->format, jr->format, JsonFormat *);
3376 :
3377 2446 : 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 2446 : case T_JsonConstructorExpr:
3392 : {
3393 2446 : JsonConstructorExpr *jce = (JsonConstructorExpr *) node;
3394 : JsonConstructorExpr *newnode;
3395 :
3396 2446 : FLATCOPY(newnode, jce, JsonConstructorExpr);
3397 2446 : MUTATE(newnode->args, jce->args, List *);
3398 2446 : MUTATE(newnode->func, jce->func, Expr *);
3399 2446 : MUTATE(newnode->coercion, jce->coercion, Expr *);
3400 2446 : MUTATE(newnode->returning, jce->returning, JsonReturning *);
3401 :
3402 2446 : 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 39420 : case T_NullTest:
3441 : {
3442 39420 : NullTest *ntest = (NullTest *) node;
3443 : NullTest *newnode;
3444 :
3445 39420 : FLATCOPY(newnode, ntest, NullTest);
3446 39420 : MUTATE(newnode->arg, ntest->arg, Expr *);
3447 39420 : return (Node *) newnode;
3448 : }
3449 : break;
3450 1922 : case T_BooleanTest:
3451 : {
3452 1922 : BooleanTest *btest = (BooleanTest *) node;
3453 : BooleanTest *newnode;
3454 :
3455 1922 : FLATCOPY(newnode, btest, BooleanTest);
3456 1922 : MUTATE(newnode->arg, btest->arg, Expr *);
3457 1922 : return (Node *) newnode;
3458 : }
3459 : break;
3460 16122 : case T_CoerceToDomain:
3461 : {
3462 16122 : CoerceToDomain *ctest = (CoerceToDomain *) node;
3463 : CoerceToDomain *newnode;
3464 :
3465 16122 : FLATCOPY(newnode, ctest, CoerceToDomain);
3466 16122 : MUTATE(newnode->arg, ctest->arg, Expr *);
3467 16122 : return (Node *) newnode;
3468 : }
3469 : break;
3470 1758 : case T_ReturningExpr:
3471 : {
3472 1758 : ReturningExpr *rexpr = (ReturningExpr *) node;
3473 : ReturningExpr *newnode;
3474 :
3475 1758 : FLATCOPY(newnode, rexpr, ReturningExpr);
3476 1758 : MUTATE(newnode->retexpr, rexpr->retexpr, Expr *);
3477 1758 : return (Node *) newnode;
3478 : }
3479 : break;
3480 3526900 : case T_TargetEntry:
3481 : {
3482 3526900 : TargetEntry *targetentry = (TargetEntry *) node;
3483 : TargetEntry *newnode;
3484 :
3485 3526900 : FLATCOPY(newnode, targetentry, TargetEntry);
3486 3526900 : MUTATE(newnode->expr, targetentry->expr, Expr *);
3487 3523136 : return (Node *) newnode;
3488 : }
3489 : break;
3490 35250 : case T_Query:
3491 : /* Do nothing with a sub-Query, per discussion above */
3492 35250 : 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 112 : case T_CommonTableExpr:
3518 : {
3519 112 : CommonTableExpr *cte = (CommonTableExpr *) node;
3520 : CommonTableExpr *newnode;
3521 :
3522 112 : 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 112 : MUTATE(newnode->ctequery, cte->ctequery, Node *);
3529 :
3530 112 : MUTATE(newnode->search_clause, cte->search_clause, CTESearchClause *);
3531 112 : MUTATE(newnode->cycle_clause, cte->cycle_clause, CTECycleClause *);
3532 :
3533 112 : 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 4533940 : 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 4533940 : resultlist = NIL;
3569 14433526 : foreach(temp, (List *) node)
3570 : {
3571 9899586 : resultlist = lappend(resultlist,
3572 9903464 : mutator((Node *) lfirst(temp),
3573 : context));
3574 : }
3575 4530062 : return (Node *) resultlist;
3576 : }
3577 : break;
3578 23940 : case T_FromExpr:
3579 : {
3580 23940 : FromExpr *from = (FromExpr *) node;
3581 : FromExpr *newnode;
3582 :
3583 23940 : FLATCOPY(newnode, from, FromExpr);
3584 23940 : MUTATE(newnode->fromlist, from->fromlist, List *);
3585 23940 : MUTATE(newnode->quals, from->quals, Node *);
3586 23940 : 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 990 : case T_MergeAction:
3605 : {
3606 990 : MergeAction *action = (MergeAction *) node;
3607 : MergeAction *newnode;
3608 :
3609 990 : FLATCOPY(newnode, action, MergeAction);
3610 990 : MUTATE(newnode->qual, action->qual, Node *);
3611 990 : MUTATE(newnode->targetList, action->targetList, List *);
3612 :
3613 990 : return (Node *) newnode;
3614 : }
3615 : break;
3616 120 : case T_PartitionPruneStepOp:
3617 : {
3618 120 : PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
3619 : PartitionPruneStepOp *newnode;
3620 :
3621 120 : FLATCOPY(newnode, opstep, PartitionPruneStepOp);
3622 120 : MUTATE(newnode->exprs, opstep->exprs, List *);
3623 :
3624 120 : return (Node *) newnode;
3625 : }
3626 : break;
3627 12 : case T_PartitionPruneStepCombine:
3628 : /* no expression sub-nodes */
3629 12 : return copyObject(node);
3630 3946 : case T_JoinExpr:
3631 : {
3632 3946 : JoinExpr *join = (JoinExpr *) node;
3633 : JoinExpr *newnode;
3634 :
3635 3946 : FLATCOPY(newnode, join, JoinExpr);
3636 3946 : MUTATE(newnode->larg, join->larg, Node *);
3637 3946 : MUTATE(newnode->rarg, join->rarg, Node *);
3638 3946 : MUTATE(newnode->quals, join->quals, Node *);
3639 : /* We do not mutate alias or using by default */
3640 3946 : return (Node *) newnode;
3641 : }
3642 : break;
3643 168 : case T_SetOperationStmt:
3644 : {
3645 168 : SetOperationStmt *setop = (SetOperationStmt *) node;
3646 : SetOperationStmt *newnode;
3647 :
3648 168 : FLATCOPY(newnode, setop, SetOperationStmt);
3649 168 : MUTATE(newnode->larg, setop->larg, Node *);
3650 168 : MUTATE(newnode->rarg, setop->rarg, Node *);
3651 : /* We do not mutate groupClauses by default */
3652 168 : return (Node *) newnode;
3653 : }
3654 : break;
3655 444 : case T_IndexClause:
3656 : {
3657 444 : IndexClause *iclause = (IndexClause *) node;
3658 : IndexClause *newnode;
3659 :
3660 444 : FLATCOPY(newnode, iclause, IndexClause);
3661 444 : MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
3662 444 : MUTATE(newnode->indexquals, iclause->indexquals, List *);
3663 444 : return (Node *) newnode;
3664 : }
3665 : break;
3666 9550 : case T_PlaceHolderVar:
3667 : {
3668 9550 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
3669 : PlaceHolderVar *newnode;
3670 :
3671 9550 : FLATCOPY(newnode, phv, PlaceHolderVar);
3672 9550 : MUTATE(newnode->phexpr, phv->phexpr, Expr *);
3673 : /* Assume we need not copy the relids bitmapsets */
3674 9550 : 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 7090 : case T_AppendRelInfo:
3688 : {
3689 7090 : AppendRelInfo *appinfo = (AppendRelInfo *) node;
3690 : AppendRelInfo *newnode;
3691 :
3692 7090 : FLATCOPY(newnode, appinfo, AppendRelInfo);
3693 7090 : MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
3694 : /* Assume nothing need be done with parent_colnos[] */
3695 7090 : 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 87434 : case T_RangeTblFunction:
3710 : {
3711 87434 : RangeTblFunction *rtfunc = (RangeTblFunction *) node;
3712 : RangeTblFunction *newnode;
3713 :
3714 87434 : FLATCOPY(newnode, rtfunc, RangeTblFunction);
3715 87434 : MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
3716 : /* Assume we need not copy the coldef info lists */
3717 87434 : return (Node *) newnode;
3718 : }
3719 : break;
3720 466 : case T_TableSampleClause:
3721 : {
3722 466 : TableSampleClause *tsc = (TableSampleClause *) node;
3723 : TableSampleClause *newnode;
3724 :
3725 466 : FLATCOPY(newnode, tsc, TableSampleClause);
3726 466 : MUTATE(newnode->args, tsc->args, List *);
3727 466 : MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
3728 466 : 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 23736 : 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 23736 : if (!(flags & QTW_DONT_COPY_QUERY))
3785 : {
3786 : Query *newquery;
3787 :
3788 23736 : FLATCOPY(newquery, query, Query);
3789 23736 : query = newquery;
3790 : }
3791 :
3792 23736 : MUTATE(query->targetList, query->targetList, List *);
3793 23736 : MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
3794 23736 : MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
3795 23736 : MUTATE(query->mergeActionList, query->mergeActionList, List *);
3796 23736 : MUTATE(query->mergeJoinCondition, query->mergeJoinCondition, Node *);
3797 23736 : MUTATE(query->returningList, query->returningList, List *);
3798 23736 : MUTATE(query->jointree, query->jointree, FromExpr *);
3799 23736 : MUTATE(query->setOperations, query->setOperations, Node *);
3800 23736 : MUTATE(query->havingQual, query->havingQual, Node *);
3801 23736 : MUTATE(query->limitOffset, query->limitOffset, Node *);
3802 23736 : 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 23736 : 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 23736 : resultlist = NIL;
3827 23760 : foreach(temp, query->windowClause)
3828 : {
3829 24 : WindowClause *wc = lfirst_node(WindowClause, temp);
3830 : WindowClause *newnode;
3831 :
3832 24 : FLATCOPY(newnode, wc, WindowClause);
3833 24 : MUTATE(newnode->startOffset, wc->startOffset, Node *);
3834 24 : MUTATE(newnode->endOffset, wc->endOffset, Node *);
3835 :
3836 24 : resultlist = lappend(resultlist, (Node *) newnode);
3837 : }
3838 23736 : 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 23736 : if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
3854 23736 : MUTATE(query->cteList, query->cteList, List *);
3855 : else /* else copy CTE list as-is */
3856 0 : query->cteList = copyObject(query->cteList);
3857 23736 : query->rtable = range_table_mutator(query->rtable,
3858 : mutator, context, flags);
3859 23736 : 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 23736 : range_table_mutator_impl(List *rtable,
3869 : tree_mutator_callback mutator,
3870 : void *context,
3871 : int flags)
3872 : {
3873 23736 : List *newrt = NIL;
3874 : ListCell *rt;
3875 :
3876 67802 : foreach(rt, rtable)
3877 : {
3878 44066 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3879 : RangeTblEntry *newrte;
3880 :
3881 44066 : FLATCOPY(newrte, rte, RangeTblEntry);
3882 44066 : switch (rte->rtekind)
3883 : {
3884 29038 : case RTE_RELATION:
3885 29038 : MUTATE(newrte->tablesample, rte->tablesample,
3886 : TableSampleClause *);
3887 : /* we don't bother to copy eref, aliases, etc; OK? */
3888 29038 : break;
3889 3348 : case RTE_SUBQUERY:
3890 3348 : if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3891 3348 : 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 3348 : break;
3898 3956 : case RTE_JOIN:
3899 3956 : if (!(flags & QTW_IGNORE_JOINALIASES))
3900 3566 : MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
3901 : else
3902 : {
3903 : /* else, copy join aliases as-is */
3904 390 : newrte->joinaliasvars = copyObject(rte->joinaliasvars);
3905 : }
3906 3956 : break;
3907 5772 : case RTE_FUNCTION:
3908 5772 : MUTATE(newrte->functions, rte->functions, List *);
3909 5772 : break;
3910 0 : case RTE_TABLEFUNC:
3911 0 : MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
3912 0 : break;
3913 1314 : case RTE_VALUES:
3914 1314 : MUTATE(newrte->values_lists, rte->values_lists, List *);
3915 1314 : break;
3916 396 : case RTE_CTE:
3917 : case RTE_NAMEDTUPLESTORE:
3918 : case RTE_RESULT:
3919 : /* nothing to do */
3920 396 : break;
3921 242 : case RTE_GROUP:
3922 242 : if (!(flags & QTW_IGNORE_GROUPEXPRS))
3923 242 : 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 242 : break;
3930 : }
3931 44066 : MUTATE(newrte->securityQuals, rte->securityQuals, List *);
3932 44066 : newrt = lappend(newrt, newrte);
3933 : }
3934 23736 : 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 3538366 : query_or_expression_tree_walker_impl(Node *node,
3947 : tree_walker_callback walker,
3948 : void *context,
3949 : int flags)
3950 : {
3951 3538366 : if (node && IsA(node, Query))
3952 390818 : return query_tree_walker((Query *) node,
3953 : walker,
3954 : context,
3955 : flags);
3956 : else
3957 3147548 : 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 512030 : query_or_expression_tree_mutator_impl(Node *node,
3970 : tree_mutator_callback mutator,
3971 : void *context,
3972 : int flags)
3973 : {
3974 512030 : if (node && IsA(node, Query))
3975 6532 : return (Node *) query_tree_mutator((Query *) node,
3976 : mutator,
3977 : context,
3978 : flags);
3979 : else
3980 505498 : 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 11029096 : 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 11029096 : if (node == NULL)
4011 0 : return false;
4012 :
4013 : /* Guard against stack overflow due to overly complex expressions */
4014 11029096 : check_stack_depth();
4015 :
4016 11029096 : switch (nodeTag(node))
4017 : {
4018 1466982 : 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 1466982 : break;
4034 343660 : case T_Alias:
4035 : /* we assume the colnames list isn't interesting */
4036 343660 : break;
4037 564108 : case T_RangeVar:
4038 564108 : return WALK(((RangeVar *) node)->alias);
4039 472 : case T_GroupingFunc:
4040 472 : return WALK(((GroupingFunc *) node)->args);
4041 45210 : case T_SubLink:
4042 : {
4043 45210 : SubLink *sublink = (SubLink *) node;
4044 :
4045 45210 : if (WALK(sublink->testexpr))
4046 0 : return true;
4047 : /* we assume the operName is not interesting */
4048 45210 : if (WALK(sublink->subselect))
4049 0 : return true;
4050 : }
4051 45210 : break;
4052 58960 : case T_CaseExpr:
4053 : {
4054 58960 : CaseExpr *caseexpr = (CaseExpr *) node;
4055 :
4056 58960 : if (WALK(caseexpr->arg))
4057 0 : return true;
4058 : /* we assume walker doesn't care about CaseWhens, either */
4059 161144 : foreach(temp, caseexpr->args)
4060 : {
4061 102184 : CaseWhen *when = lfirst_node(CaseWhen, temp);
4062 :
4063 102184 : if (WALK(when->expr))
4064 0 : return true;
4065 102184 : if (WALK(when->result))
4066 0 : return true;
4067 : }
4068 58960 : if (WALK(caseexpr->defresult))
4069 0 : return true;
4070 : }
4071 58960 : break;
4072 7440 : case T_RowExpr:
4073 : /* Assume colnames isn't interesting */
4074 7440 : return WALK(((RowExpr *) node)->args);
4075 5484 : case T_CoalesceExpr:
4076 5484 : 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 3866 : case T_JsonValueExpr:
4093 : {
4094 3866 : JsonValueExpr *jve = (JsonValueExpr *) node;
4095 :
4096 3866 : if (WALK(jve->raw_expr))
4097 0 : return true;
4098 3866 : if (WALK(jve->formatted_expr))
4099 0 : return true;
4100 3866 : if (WALK(jve->format))
4101 0 : return true;
4102 : }
4103 3866 : 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 21194 : case T_NullTest:
4211 21194 : return WALK(((NullTest *) node)->arg);
4212 784 : case T_BooleanTest:
4213 784 : return WALK(((BooleanTest *) node)->arg);
4214 99234 : case T_JoinExpr:
4215 : {
4216 99234 : JoinExpr *join = (JoinExpr *) node;
4217 :
4218 99234 : if (WALK(join->larg))
4219 0 : return true;
4220 99234 : if (WALK(join->rarg))
4221 0 : return true;
4222 99234 : if (WALK(join->quals))
4223 0 : return true;
4224 99234 : if (WALK(join->alias))
4225 0 : return true;
4226 : /* using list is deemed uninteresting */
4227 : }
4228 99234 : 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 2014200 : case T_List:
4242 5666832 : foreach(temp, (List *) node)
4243 : {
4244 3652734 : if (WALK((Node *) lfirst(temp)))
4245 0 : return true;
4246 : }
4247 2014098 : break;
4248 73204 : case T_InsertStmt:
4249 : {
4250 73204 : InsertStmt *stmt = (InsertStmt *) node;
4251 :
4252 73204 : if (WALK(stmt->relation))
4253 0 : return true;
4254 73204 : if (WALK(stmt->cols))
4255 0 : return true;
4256 73204 : if (WALK(stmt->selectStmt))
4257 0 : return true;
4258 73204 : if (WALK(stmt->onConflictClause))
4259 0 : return true;
4260 73204 : if (WALK(stmt->returningClause))
4261 0 : return true;
4262 73204 : if (WALK(stmt->withClause))
4263 0 : return true;
4264 : }
4265 73204 : break;
4266 4570 : case T_DeleteStmt:
4267 : {
4268 4570 : DeleteStmt *stmt = (DeleteStmt *) node;
4269 :
4270 4570 : if (WALK(stmt->relation))
4271 0 : return true;
4272 4570 : if (WALK(stmt->usingClause))
4273 0 : return true;
4274 4570 : if (WALK(stmt->whereClause))
4275 0 : return true;
4276 4570 : if (WALK(stmt->returningClause))
4277 0 : return true;
4278 4570 : if (WALK(stmt->withClause))
4279 0 : return true;
4280 : }
4281 4570 : break;
4282 13394 : case T_UpdateStmt:
4283 : {
4284 13394 : UpdateStmt *stmt = (UpdateStmt *) node;
4285 :
4286 13394 : if (WALK(stmt->relation))
4287 0 : return true;
4288 13394 : if (WALK(stmt->targetList))
4289 0 : return true;
4290 13394 : if (WALK(stmt->whereClause))
4291 0 : return true;
4292 13394 : if (WALK(stmt->fromClause))
4293 0 : return true;
4294 13394 : if (WALK(stmt->returningClause))
4295 0 : return true;
4296 13394 : if (WALK(stmt->withClause))
4297 0 : return true;
4298 : }
4299 13394 : break;
4300 1950 : case T_MergeStmt:
4301 : {
4302 1950 : MergeStmt *stmt = (MergeStmt *) node;
4303 :
4304 1950 : if (WALK(stmt->relation))
4305 0 : return true;
4306 1950 : if (WALK(stmt->sourceRelation))
4307 0 : return true;
4308 1950 : if (WALK(stmt->joinCondition))
4309 0 : return true;
4310 1950 : if (WALK(stmt->mergeWhenClauses))
4311 0 : return true;
4312 1950 : if (WALK(stmt->returningClause))
4313 0 : return true;
4314 1950 : if (WALK(stmt->withClause))
4315 0 : return true;
4316 : }
4317 1950 : break;
4318 3036 : case T_MergeWhenClause:
4319 : {
4320 3036 : MergeWhenClause *mergeWhenClause = (MergeWhenClause *) node;
4321 :
4322 3036 : if (WALK(mergeWhenClause->condition))
4323 0 : return true;
4324 3036 : if (WALK(mergeWhenClause->targetList))
4325 0 : return true;
4326 3036 : if (WALK(mergeWhenClause->values))
4327 0 : return true;
4328 : }
4329 3036 : break;
4330 3330 : case T_ReturningClause:
4331 : {
4332 3330 : ReturningClause *returning = (ReturningClause *) node;
4333 :
4334 3330 : if (WALK(returning->options))
4335 0 : return true;
4336 3330 : if (WALK(returning->exprs))
4337 0 : return true;
4338 : }
4339 3330 : break;
4340 651822 : case T_SelectStmt:
4341 : {
4342 651822 : SelectStmt *stmt = (SelectStmt *) node;
4343 :
4344 651822 : if (WALK(stmt->distinctClause))
4345 0 : return true;
4346 651822 : if (WALK(stmt->intoClause))
4347 0 : return true;
4348 651822 : if (WALK(stmt->targetList))
4349 0 : return true;
4350 651816 : if (WALK(stmt->fromClause))
4351 0 : return true;
4352 651732 : if (WALK(stmt->whereClause))
4353 0 : return true;
4354 651726 : if (WALK(stmt->groupClause))
4355 0 : return true;
4356 651726 : if (WALK(stmt->havingClause))
4357 0 : return true;
4358 651726 : if (WALK(stmt->windowClause))
4359 0 : return true;
4360 651726 : if (WALK(stmt->valuesLists))
4361 0 : return true;
4362 651726 : if (WALK(stmt->sortClause))
4363 0 : return true;
4364 651726 : if (WALK(stmt->limitOffset))
4365 0 : return true;
4366 651726 : if (WALK(stmt->limitCount))
4367 0 : return true;
4368 651726 : if (WALK(stmt->lockingClause))
4369 0 : return true;
4370 651726 : if (WALK(stmt->withClause))
4371 0 : return true;
4372 651726 : if (WALK(stmt->larg))
4373 0 : return true;
4374 651726 : if (WALK(stmt->rarg))
4375 0 : return true;
4376 : }
4377 651714 : 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 724926 : case T_A_Expr:
4389 : {
4390 724926 : A_Expr *expr = (A_Expr *) node;
4391 :
4392 724926 : if (WALK(expr->lexpr))
4393 0 : return true;
4394 724926 : if (WALK(expr->rexpr))
4395 0 : return true;
4396 : /* operator name is deemed uninteresting */
4397 : }
4398 724926 : break;
4399 185288 : case T_BoolExpr:
4400 : {
4401 185288 : BoolExpr *expr = (BoolExpr *) node;
4402 :
4403 185288 : if (WALK(expr->args))
4404 0 : return true;
4405 : }
4406 185288 : break;
4407 2060266 : case T_ColumnRef:
4408 : /* we assume the fields contain nothing interesting */
4409 2060266 : break;
4410 419428 : case T_FuncCall:
4411 : {
4412 419428 : FuncCall *fcall = (FuncCall *) node;
4413 :
4414 419428 : if (WALK(fcall->args))
4415 0 : return true;
4416 419428 : if (WALK(fcall->agg_order))
4417 0 : return true;
4418 419428 : if (WALK(fcall->agg_filter))
4419 0 : return true;
4420 419428 : if (WALK(fcall->over))
4421 0 : return true;
4422 : /* function name is deemed uninteresting */
4423 : }
4424 419428 : break;
4425 48166 : case T_NamedArgExpr:
4426 48166 : return WALK(((NamedArgExpr *) node)->arg);
4427 18538 : case T_A_Indices:
4428 : {
4429 18538 : A_Indices *indices = (A_Indices *) node;
4430 :
4431 18538 : if (WALK(indices->lidx))
4432 0 : return true;
4433 18538 : if (WALK(indices->uidx))
4434 0 : return true;
4435 : }
4436 18538 : break;
4437 29304 : case T_A_Indirection:
4438 : {
4439 29304 : A_Indirection *indir = (A_Indirection *) node;
4440 :
4441 29304 : if (WALK(indir->arg))
4442 0 : return true;
4443 29304 : if (WALK(indir->indirection))
4444 0 : return true;
4445 : }
4446 29304 : break;
4447 9006 : case T_A_ArrayExpr:
4448 9006 : return WALK(((A_ArrayExpr *) node)->elements);
4449 1300818 : case T_ResTarget:
4450 : {
4451 1300818 : ResTarget *rt = (ResTarget *) node;
4452 :
4453 1300818 : if (WALK(rt->indirection))
4454 0 : return true;
4455 1300818 : if (WALK(rt->val))
4456 0 : return true;
4457 : }
4458 1300812 : break;
4459 396 : case T_MultiAssignRef:
4460 396 : return WALK(((MultiAssignRef *) node)->source);
4461 307776 : case T_TypeCast:
4462 : {
4463 307776 : TypeCast *tc = (TypeCast *) node;
4464 :
4465 307776 : if (WALK(tc->arg))
4466 0 : return true;
4467 307776 : if (WALK(tc->typeName))
4468 0 : return true;
4469 : }
4470 307776 : break;
4471 9060 : case T_CollateClause:
4472 9060 : return WALK(((CollateClause *) node)->arg);
4473 101646 : case T_SortBy:
4474 101646 : return WALK(((SortBy *) node)->node);
4475 4450 : case T_WindowDef:
4476 : {
4477 4450 : WindowDef *wd = (WindowDef *) node;
4478 :
4479 4450 : if (WALK(wd->partitionClause))
4480 0 : return true;
4481 4450 : if (WALK(wd->orderClause))
4482 0 : return true;
4483 4450 : if (WALK(wd->startOffset))
4484 0 : return true;
4485 4450 : if (WALK(wd->endOffset))
4486 0 : return true;
4487 : }
4488 4450 : break;
4489 18304 : case T_RangeSubselect:
4490 : {
4491 18304 : RangeSubselect *rs = (RangeSubselect *) node;
4492 :
4493 18304 : if (WALK(rs->subquery))
4494 0 : return true;
4495 18298 : if (WALK(rs->alias))
4496 0 : return true;
4497 : }
4498 18298 : break;
4499 59496 : case T_RangeFunction:
4500 : {
4501 59496 : RangeFunction *rf = (RangeFunction *) node;
4502 :
4503 59496 : if (WALK(rf->functions))
4504 0 : return true;
4505 59496 : if (WALK(rf->alias))
4506 0 : return true;
4507 59496 : if (WALK(rf->coldeflist))
4508 0 : return true;
4509 : }
4510 59496 : break;
4511 308 : case T_RangeTableSample:
4512 : {
4513 308 : RangeTableSample *rts = (RangeTableSample *) node;
4514 :
4515 308 : if (WALK(rts->relation))
4516 0 : return true;
4517 : /* method name is deemed uninteresting */
4518 308 : if (WALK(rts->args))
4519 0 : return true;
4520 308 : if (WALK(rts->repeatable))
4521 0 : return true;
4522 : }
4523 308 : 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 312350 : case T_TypeName:
4551 : {
4552 312350 : TypeName *tn = (TypeName *) node;
4553 :
4554 312350 : if (WALK(tn->typmods))
4555 0 : return true;
4556 312350 : if (WALK(tn->arrayBounds))
4557 0 : return true;
4558 : /* type name itself is deemed uninteresting */
4559 : }
4560 312350 : break;
4561 1954 : case T_ColumnDef:
4562 : {
4563 1954 : ColumnDef *coldef = (ColumnDef *) node;
4564 :
4565 1954 : if (WALK(coldef->typeName))
4566 0 : return true;
4567 1954 : if (WALK(coldef->raw_default))
4568 0 : return true;
4569 1954 : if (WALK(coldef->collClause))
4570 0 : return true;
4571 : /* for now, constraints are ignored */
4572 : }
4573 1954 : 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 1368 : case T_GroupingSet:
4584 1368 : return WALK(((GroupingSet *) node)->content);
4585 5684 : case T_LockingClause:
4586 5684 : return WALK(((LockingClause *) node)->lockedRels);
4587 202 : case T_XmlSerialize:
4588 : {
4589 202 : XmlSerialize *xs = (XmlSerialize *) node;
4590 :
4591 202 : if (WALK(xs->expr))
4592 0 : return true;
4593 202 : if (WALK(xs->typeName))
4594 0 : return true;
4595 : }
4596 202 : break;
4597 3838 : case T_WithClause:
4598 3838 : 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 4916 : case T_CommonTableExpr:
4622 : /* search_clause and cycle_clause are not interesting here */
4623 4916 : 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 802 : case T_JsonKeyValue:
4635 : {
4636 802 : JsonKeyValue *jkv = (JsonKeyValue *) node;
4637 :
4638 802 : if (WALK(jkv->key))
4639 0 : return true;
4640 802 : if (WALK(jkv->value))
4641 0 : return true;
4642 : }
4643 802 : break;
4644 434 : case T_JsonObjectConstructor:
4645 : {
4646 434 : JsonObjectConstructor *joc = (JsonObjectConstructor *) node;
4647 :
4648 434 : if (WALK(joc->output))
4649 0 : return true;
4650 434 : if (WALK(joc->exprs))
4651 0 : return true;
4652 : }
4653 434 : 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 10242226 : 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 1306772 : planstate_tree_walker_impl(PlanState *planstate,
4724 : planstate_tree_walker_callback walker,
4725 : void *context)
4726 : {
4727 1306772 : 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 1306772 : check_stack_depth();
4735 :
4736 : /* initPlan-s */
4737 1306772 : if (planstate_walk_subplans(planstate->initPlan, walker, context))
4738 0 : return true;
4739 :
4740 : /* lefttree */
4741 1306772 : if (outerPlanState(planstate))
4742 : {
4743 463476 : if (PSWALK(outerPlanState(planstate)))
4744 0 : return true;
4745 : }
4746 :
4747 : /* righttree */
4748 1306772 : if (innerPlanState(planstate))
4749 : {
4750 128860 : if (PSWALK(innerPlanState(planstate)))
4751 0 : return true;
4752 : }
4753 :
4754 : /* special child plans */
4755 1306772 : switch (nodeTag(plan))
4756 : {
4757 14886 : case T_Append:
4758 14886 : if (planstate_walk_members(((AppendState *) planstate)->appendplans,
4759 : ((AppendState *) planstate)->as_nplans,
4760 : walker, context))
4761 0 : return true;
4762 14886 : break;
4763 574 : case T_MergeAppend:
4764 574 : if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
4765 : ((MergeAppendState *) planstate)->ms_nplans,
4766 : walker, context))
4767 0 : return true;
4768 574 : break;
4769 152 : case T_BitmapAnd:
4770 152 : if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
4771 : ((BitmapAndState *) planstate)->nplans,
4772 : walker, context))
4773 0 : return true;
4774 152 : break;
4775 306 : case T_BitmapOr:
4776 306 : if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
4777 : ((BitmapOrState *) planstate)->nplans,
4778 : walker, context))
4779 0 : return true;
4780 306 : break;
4781 10124 : case T_SubqueryScan:
4782 10124 : if (PSWALK(((SubqueryScanState *) planstate)->subplan))
4783 0 : return true;
4784 10124 : 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 1280730 : default:
4793 1280730 : break;
4794 : }
4795 :
4796 : /* subPlan-s */
4797 1306772 : if (planstate_walk_subplans(planstate->subPlan, walker, context))
4798 0 : return true;
4799 :
4800 1306772 : return false;
4801 : }
4802 :
4803 : /*
4804 : * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
4805 : */
4806 : static bool
4807 2613544 : planstate_walk_subplans(List *plans,
4808 : planstate_tree_walker_callback walker,
4809 : void *context)
4810 : {
4811 : ListCell *lc;
4812 :
4813 2652550 : foreach(lc, plans)
4814 : {
4815 39006 : SubPlanState *sps = lfirst_node(SubPlanState, lc);
4816 :
4817 39006 : if (PSWALK(sps->planstate))
4818 0 : return true;
4819 : }
4820 :
4821 2613544 : 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 15918 : planstate_walk_members(PlanState **planstates, int nplans,
4830 : planstate_tree_walker_callback walker,
4831 : void *context)
4832 : {
4833 : int j;
4834 :
4835 63596 : for (j = 0; j < nplans; j++)
4836 : {
4837 47678 : if (PSWALK(planstates[j]))
4838 0 : return true;
4839 : }
4840 :
4841 15918 : return false;
4842 : }
|