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