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