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