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 25829790 : exprType(const Node *expr)
43 : {
44 : Oid type;
45 :
46 25829790 : if (!expr)
47 360 : return InvalidOid;
48 :
49 25829430 : switch (nodeTag(expr))
50 : {
51 13496226 : case T_Var:
52 13496226 : type = ((const Var *) expr)->vartype;
53 13496226 : break;
54 4004350 : case T_Const:
55 4004350 : type = ((const Const *) expr)->consttype;
56 4004350 : break;
57 2163704 : case T_Param:
58 2163704 : type = ((const Param *) expr)->paramtype;
59 2163704 : break;
60 248946 : case T_Aggref:
61 248946 : type = ((const Aggref *) expr)->aggtype;
62 248946 : break;
63 2056 : case T_GroupingFunc:
64 2056 : type = INT4OID;
65 2056 : break;
66 18120 : case T_WindowFunc:
67 18120 : type = ((const WindowFunc *) expr)->wintype;
68 18120 : break;
69 798 : case T_MergeSupportFunc:
70 798 : type = ((const MergeSupportFunc *) expr)->msftype;
71 798 : break;
72 120600 : case T_SubscriptingRef:
73 120600 : type = ((const SubscriptingRef *) expr)->refrestype;
74 120600 : break;
75 2013078 : case T_FuncExpr:
76 2013078 : type = ((const FuncExpr *) expr)->funcresulttype;
77 2013078 : break;
78 97984 : case T_NamedArgExpr:
79 97984 : type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
80 97984 : break;
81 1420388 : case T_OpExpr:
82 1420388 : type = ((const OpExpr *) expr)->opresulttype;
83 1420388 : break;
84 2076 : case T_DistinctExpr:
85 2076 : type = ((const DistinctExpr *) expr)->opresulttype;
86 2076 : break;
87 912 : case T_NullIfExpr:
88 912 : type = ((const NullIfExpr *) expr)->opresulttype;
89 912 : break;
90 94468 : case T_ScalarArrayOpExpr:
91 94468 : type = BOOLOID;
92 94468 : break;
93 292290 : case T_BoolExpr:
94 292290 : type = BOOLOID;
95 292290 : break;
96 86386 : case T_SubLink:
97 : {
98 86386 : const SubLink *sublink = (const SubLink *) expr;
99 :
100 86386 : if (sublink->subLinkType == EXPR_SUBLINK ||
101 31184 : sublink->subLinkType == ARRAY_SUBLINK)
102 70826 : {
103 : /* get the type of the subselect's first target column */
104 70826 : Query *qtree = (Query *) sublink->subselect;
105 : TargetEntry *tent;
106 :
107 70826 : if (!qtree || !IsA(qtree, Query))
108 0 : elog(ERROR, "cannot get type for untransformed sublink");
109 70826 : tent = linitial_node(TargetEntry, qtree->targetList);
110 : Assert(!tent->resjunk);
111 70826 : type = exprType((Node *) tent->expr);
112 70826 : if (sublink->subLinkType == ARRAY_SUBLINK)
113 : {
114 15624 : type = get_promoted_array_type(type);
115 15624 : 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 15560 : 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 15422 : type = BOOLOID;
131 : }
132 : }
133 86386 : break;
134 42226 : case T_SubPlan:
135 : {
136 42226 : const SubPlan *subplan = (const SubPlan *) expr;
137 :
138 42226 : if (subplan->subLinkType == EXPR_SUBLINK ||
139 3422 : subplan->subLinkType == ARRAY_SUBLINK)
140 : {
141 : /* get the type of the subselect's first target column */
142 39140 : type = subplan->firstColType;
143 39140 : 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 3086 : 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 2906 : type = BOOLOID;
162 : }
163 : }
164 42226 : break;
165 1182 : case T_AlternativeSubPlan:
166 : {
167 1182 : const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
168 :
169 : /* subplans should all return the same thing */
170 1182 : type = exprType((Node *) linitial(asplan->subplans));
171 : }
172 1182 : break;
173 35464 : case T_FieldSelect:
174 35464 : type = ((const FieldSelect *) expr)->resulttype;
175 35464 : break;
176 1126 : case T_FieldStore:
177 1126 : type = ((const FieldStore *) expr)->resulttype;
178 1126 : break;
179 441766 : case T_RelabelType:
180 441766 : type = ((const RelabelType *) expr)->resulttype;
181 441766 : break;
182 127690 : case T_CoerceViaIO:
183 127690 : type = ((const CoerceViaIO *) expr)->resulttype;
184 127690 : break;
185 15468 : case T_ArrayCoerceExpr:
186 15468 : type = ((const ArrayCoerceExpr *) expr)->resulttype;
187 15468 : break;
188 2208 : case T_ConvertRowtypeExpr:
189 2208 : type = ((const ConvertRowtypeExpr *) expr)->resulttype;
190 2208 : break;
191 8570 : case T_CollateExpr:
192 8570 : type = exprType((Node *) ((const CollateExpr *) expr)->arg);
193 8570 : break;
194 466976 : case T_CaseExpr:
195 466976 : type = ((const CaseExpr *) expr)->casetype;
196 466976 : break;
197 35832 : case T_CaseTestExpr:
198 35832 : type = ((const CaseTestExpr *) expr)->typeId;
199 35832 : break;
200 97802 : case T_ArrayExpr:
201 97802 : type = ((const ArrayExpr *) expr)->array_typeid;
202 97802 : break;
203 14388 : case T_RowExpr:
204 14388 : type = ((const RowExpr *) expr)->row_typeid;
205 14388 : break;
206 348 : case T_RowCompareExpr:
207 348 : type = BOOLOID;
208 348 : break;
209 21276 : case T_CoalesceExpr:
210 21276 : type = ((const CoalesceExpr *) expr)->coalescetype;
211 21276 : break;
212 6054 : case T_MinMaxExpr:
213 6054 : type = ((const MinMaxExpr *) expr)->minmaxtype;
214 6054 : break;
215 9808 : case T_SQLValueFunction:
216 9808 : type = ((const SQLValueFunction *) expr)->type;
217 9808 : break;
218 25082 : case T_XmlExpr:
219 25082 : if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
220 102 : type = BOOLOID;
221 24980 : else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
222 810 : type = TEXTOID;
223 : else
224 24170 : type = XMLOID;
225 25082 : 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 42380 : case T_NullTest:
254 42380 : type = BOOLOID;
255 42380 : break;
256 2764 : case T_BooleanTest:
257 2764 : type = BOOLOID;
258 2764 : break;
259 234838 : case T_CoerceToDomain:
260 234838 : type = ((const CoerceToDomain *) expr)->resulttype;
261 234838 : break;
262 1898 : case T_CoerceToDomainValue:
263 1898 : type = ((const CoerceToDomainValue *) expr)->typeId;
264 1898 : break;
265 99466 : case T_SetToDefault:
266 99466 : type = ((const SetToDefault *) expr)->typeId;
267 99466 : break;
268 254 : case T_CurrentOfExpr:
269 254 : type = BOOLOID;
270 254 : break;
271 1922 : case T_NextValueExpr:
272 1922 : type = ((const NextValueExpr *) expr)->typeId;
273 1922 : 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 10146 : case T_PlaceHolderVar:
282 10146 : type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
283 10146 : 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 25829430 : 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 9572940 : exprTypmod(const Node *expr)
299 : {
300 9572940 : if (!expr)
301 0 : return -1;
302 :
303 9572940 : switch (nodeTag(expr))
304 : {
305 5587624 : case T_Var:
306 5587624 : return ((const Var *) expr)->vartypmod;
307 1496856 : case T_Const:
308 1496856 : return ((const Const *) expr)->consttypmod;
309 137150 : case T_Param:
310 137150 : return ((const Param *) expr)->paramtypmod;
311 35938 : case T_SubscriptingRef:
312 35938 : return ((const SubscriptingRef *) expr)->reftypmod;
313 1239072 : case T_FuncExpr:
314 : {
315 : int32 coercedTypmod;
316 :
317 : /* Be smart about length-coercion functions... */
318 1239072 : if (exprIsLengthCoercion(expr, &coercedTypmod))
319 25588 : return coercedTypmod;
320 : }
321 1213484 : 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 6216 : case T_SubLink:
336 : {
337 6216 : const SubLink *sublink = (const SubLink *) expr;
338 :
339 6216 : if (sublink->subLinkType == EXPR_SUBLINK ||
340 638 : sublink->subLinkType == ARRAY_SUBLINK)
341 : {
342 : /* get the typmod of the subselect's first target column */
343 6146 : Query *qtree = (Query *) sublink->subselect;
344 : TargetEntry *tent;
345 :
346 6146 : if (!qtree || !IsA(qtree, Query))
347 0 : elog(ERROR, "cannot get type for untransformed sublink");
348 6146 : tent = linitial_node(TargetEntry, qtree->targetList);
349 : Assert(!tent->resjunk);
350 6146 : 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 29688 : case T_SubPlan:
357 : {
358 29688 : const SubPlan *subplan = (const SubPlan *) expr;
359 :
360 29688 : if (subplan->subLinkType == EXPR_SUBLINK ||
361 1858 : 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 28064 : return subplan->firstColTypmod;
366 : }
367 : /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
368 : }
369 1624 : break;
370 594 : case T_AlternativeSubPlan:
371 : {
372 594 : const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
373 :
374 : /* subplans should all return the same thing */
375 594 : return exprTypmod((Node *) linitial(asplan->subplans));
376 : }
377 : break;
378 15052 : case T_FieldSelect:
379 15052 : return ((const FieldSelect *) expr)->resulttypmod;
380 142798 : case T_RelabelType:
381 142798 : return ((const RelabelType *) expr)->resulttypmod;
382 7266 : case T_ArrayCoerceExpr:
383 7266 : return ((const ArrayCoerceExpr *) expr)->resulttypmod;
384 118 : case T_CollateExpr:
385 118 : return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
386 216050 : case T_CaseExpr:
387 : {
388 : /*
389 : * If all the alternatives agree on type/typmod, return that
390 : * typmod, else use -1
391 : */
392 216050 : const CaseExpr *cexpr = (const CaseExpr *) expr;
393 216050 : Oid casetype = cexpr->casetype;
394 : int32 typmod;
395 : ListCell *arg;
396 :
397 216050 : if (!cexpr->defresult)
398 0 : return -1;
399 216050 : if (exprType((Node *) cexpr->defresult) != casetype)
400 0 : return -1;
401 216050 : typmod = exprTypmod((Node *) cexpr->defresult);
402 216050 : if (typmod < 0)
403 216050 : 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 12328 : case T_CaseTestExpr:
417 12328 : return ((const CaseTestExpr *) expr)->typeMod;
418 37640 : case T_ArrayExpr:
419 : {
420 : /*
421 : * If all the elements agree on type/typmod, return that
422 : * typmod, else use -1
423 : */
424 37640 : const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
425 : Oid commontype;
426 : int32 typmod;
427 : ListCell *elem;
428 :
429 37640 : if (arrayexpr->elements == NIL)
430 182 : return -1;
431 37458 : typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
432 37458 : if (typmod < 0)
433 37422 : 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 5706 : case T_CoalesceExpr:
451 : {
452 : /*
453 : * If all the alternatives agree on type/typmod, return that
454 : * typmod, else use -1
455 : */
456 5706 : const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
457 5706 : Oid coalescetype = cexpr->coalescetype;
458 : int32 typmod;
459 : ListCell *arg;
460 :
461 5706 : if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
462 0 : return -1;
463 5706 : typmod = exprTypmod((Node *) linitial(cexpr->args));
464 5706 : if (typmod < 0)
465 5706 : 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 26122 : case T_SetToDefault:
531 26122 : return ((const SetToDefault *) expr)->typeMod;
532 6462 : case T_PlaceHolderVar:
533 6462 : return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
534 379772 : default:
535 379772 : break;
536 : }
537 1594950 : 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 1240468 : exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
553 : {
554 1240468 : if (coercedTypmod != NULL)
555 1240468 : *coercedTypmod = -1; /* default result on failure */
556 :
557 : /*
558 : * Scalar-type length coercions are FuncExprs, array-type length coercions
559 : * are ArrayCoerceExprs
560 : */
561 1240468 : if (expr && IsA(expr, FuncExpr))
562 : {
563 1240468 : 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 1240468 : if (func->funcformat != COERCE_EXPLICIT_CAST &&
571 1209030 : func->funcformat != COERCE_IMPLICIT_CAST)
572 969128 : 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 271340 : nargs = list_length(func->args);
580 271340 : if (nargs < 2 || nargs > 3)
581 245664 : return false;
582 :
583 25676 : second_arg = (Const *) lsecond(func->args);
584 25676 : if (!IsA(second_arg, Const) ||
585 25676 : second_arg->consttype != INT4OID ||
586 25676 : second_arg->constisnull)
587 0 : return false;
588 :
589 : /*
590 : * OK, it is indeed a length-coercion function.
591 : */
592 25676 : if (coercedTypmod != NULL)
593 25676 : *coercedTypmod = DatumGetInt32(second_arg->constvalue);
594 :
595 25676 : 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 206842 : 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 209446 : while (arg && IsA(arg, RelabelType))
640 2604 : arg = (Node *) ((RelabelType *) arg)->arg;
641 :
642 206842 : if (arg && IsA(arg, Const))
643 : {
644 : /* Modify the Const directly to preserve const-flatness. */
645 92850 : Const *con = (Const *) arg;
646 :
647 92850 : if (!overwrite_ok)
648 11594 : con = copyObject(con);
649 92850 : con->consttype = rtype;
650 92850 : con->consttypmod = rtypmod;
651 92850 : con->constcollid = rcollid;
652 : /* We keep the Const's original location. */
653 92850 : return (Node *) con;
654 : }
655 118464 : else if (exprType(arg) == rtype &&
656 8860 : exprTypmod(arg) == rtypmod &&
657 4388 : 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 111566 : RelabelType *newrelabel = makeNode(RelabelType);
666 :
667 111566 : newrelabel->arg = (Expr *) arg;
668 111566 : newrelabel->resulttype = rtype;
669 111566 : newrelabel->resulttypmod = rtypmod;
670 111566 : newrelabel->resultcollid = rcollid;
671 111566 : newrelabel->relabelformat = rformat;
672 111566 : newrelabel->location = rlocation;
673 111566 : 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 613770 : strip_implicit_coercions(Node *node)
701 : {
702 613770 : if (node == NULL)
703 0 : return NULL;
704 613770 : if (IsA(node, FuncExpr))
705 : {
706 12774 : FuncExpr *f = (FuncExpr *) node;
707 :
708 12774 : if (f->funcformat == COERCE_IMPLICIT_CAST)
709 44 : return strip_implicit_coercions(linitial(f->args));
710 : }
711 600996 : else if (IsA(node, RelabelType))
712 : {
713 10336 : RelabelType *r = (RelabelType *) node;
714 :
715 10336 : if (r->relabelformat == COERCE_IMPLICIT_CAST)
716 14 : return strip_implicit_coercions((Node *) r->arg);
717 : }
718 590660 : else if (IsA(node, CoerceViaIO))
719 : {
720 576 : CoerceViaIO *c = (CoerceViaIO *) node;
721 :
722 576 : if (c->coerceformat == COERCE_IMPLICIT_CAST)
723 0 : return strip_implicit_coercions((Node *) c->arg);
724 : }
725 590084 : 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 590084 : 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 590084 : 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 613712 : 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 772390 : expression_returns_set(Node *clause)
759 : {
760 772390 : return expression_returns_set_walker(clause, NULL);
761 : }
762 :
763 : static bool
764 3226964 : expression_returns_set_walker(Node *node, void *context)
765 : {
766 3226964 : if (node == NULL)
767 30098 : return false;
768 3196866 : if (IsA(node, FuncExpr))
769 : {
770 85690 : FuncExpr *expr = (FuncExpr *) node;
771 :
772 85690 : if (expr->funcretset)
773 9816 : return true;
774 : /* else fall through to check args */
775 : }
776 3187050 : if (IsA(node, OpExpr))
777 : {
778 749496 : OpExpr *expr = (OpExpr *) node;
779 :
780 749496 : 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 3187044 : if (IsA(node, Aggref))
793 1222 : return false;
794 3185822 : if (IsA(node, GroupingFunc))
795 36 : return false;
796 3185786 : if (IsA(node, WindowFunc))
797 30 : return false;
798 :
799 3185756 : 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 12107302 : exprCollation(const Node *expr)
817 : {
818 : Oid coll;
819 :
820 12107302 : if (!expr)
821 0 : return InvalidOid;
822 :
823 12107302 : switch (nodeTag(expr))
824 : {
825 8951178 : case T_Var:
826 8951178 : coll = ((const Var *) expr)->varcollid;
827 8951178 : break;
828 1759076 : case T_Const:
829 1759076 : coll = ((const Const *) expr)->constcollid;
830 1759076 : break;
831 370874 : case T_Param:
832 370874 : coll = ((const Param *) expr)->paramcollid;
833 370874 : break;
834 71506 : case T_Aggref:
835 71506 : coll = ((const Aggref *) expr)->aggcollid;
836 71506 : break;
837 658 : case T_GroupingFunc:
838 658 : coll = InvalidOid;
839 658 : break;
840 4772 : case T_WindowFunc:
841 4772 : coll = ((const WindowFunc *) expr)->wincollid;
842 4772 : break;
843 264 : case T_MergeSupportFunc:
844 264 : coll = ((const MergeSupportFunc *) expr)->msfcollid;
845 264 : break;
846 7652 : case T_SubscriptingRef:
847 7652 : coll = ((const SubscriptingRef *) expr)->refcollid;
848 7652 : break;
849 383596 : case T_FuncExpr:
850 383596 : coll = ((const FuncExpr *) expr)->funccollid;
851 383596 : break;
852 0 : case T_NamedArgExpr:
853 0 : coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
854 0 : break;
855 98222 : case T_OpExpr:
856 98222 : coll = ((const OpExpr *) expr)->opcollid;
857 98222 : 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 17202 : case T_ScalarArrayOpExpr:
865 : /* ScalarArrayOpExpr's result is boolean ... */
866 17202 : coll = InvalidOid; /* ... so it has no collation */
867 17202 : break;
868 4212 : case T_BoolExpr:
869 : /* BoolExpr's result is boolean ... */
870 4212 : coll = InvalidOid; /* ... so it has no collation */
871 4212 : break;
872 2892 : case T_SubLink:
873 : {
874 2892 : const SubLink *sublink = (const SubLink *) expr;
875 :
876 2892 : if (sublink->subLinkType == EXPR_SUBLINK ||
877 264 : sublink->subLinkType == ARRAY_SUBLINK)
878 2834 : {
879 : /* get the collation of subselect's first target column */
880 2834 : Query *qtree = (Query *) sublink->subselect;
881 : TargetEntry *tent;
882 :
883 2834 : if (!qtree || !IsA(qtree, Query))
884 0 : elog(ERROR, "cannot get collation for untransformed sublink");
885 2834 : tent = linitial_node(TargetEntry, qtree->targetList);
886 : Assert(!tent->resjunk);
887 2834 : 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 2892 : break;
897 17526 : case T_SubPlan:
898 : {
899 17526 : const SubPlan *subplan = (const SubPlan *) expr;
900 :
901 17526 : if (subplan->subLinkType == EXPR_SUBLINK ||
902 288 : subplan->subLinkType == ARRAY_SUBLINK)
903 : {
904 : /* get the collation of subselect's first target column */
905 17334 : 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 17526 : 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 9404 : case T_FieldSelect:
924 9404 : coll = ((const FieldSelect *) expr)->resultcollid;
925 9404 : 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 70574 : case T_RelabelType:
931 70574 : coll = ((const RelabelType *) expr)->resultcollid;
932 70574 : break;
933 35828 : case T_CoerceViaIO:
934 35828 : coll = ((const CoerceViaIO *) expr)->resultcollid;
935 35828 : break;
936 1422 : case T_ArrayCoerceExpr:
937 1422 : coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
938 1422 : 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 94 : case T_CollateExpr:
944 94 : coll = ((const CollateExpr *) expr)->collOid;
945 94 : break;
946 139078 : case T_CaseExpr:
947 139078 : coll = ((const CaseExpr *) expr)->casecollid;
948 139078 : break;
949 29408 : case T_CaseTestExpr:
950 29408 : coll = ((const CaseTestExpr *) expr)->collation;
951 29408 : break;
952 24856 : case T_ArrayExpr:
953 24856 : coll = ((const ArrayExpr *) expr)->array_collid;
954 24856 : break;
955 4294 : case T_RowExpr:
956 : /* RowExpr's result is composite ... */
957 4294 : coll = InvalidOid; /* ... so it has no collation */
958 4294 : 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 2880 : case T_CoalesceExpr:
964 2880 : coll = ((const CoalesceExpr *) expr)->coalescecollid;
965 2880 : 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 2284 : case T_NullTest:
1026 : /* NullTest's result is boolean ... */
1027 2284 : coll = InvalidOid; /* ... so it has no collation */
1028 2284 : break;
1029 420 : case T_BooleanTest:
1030 : /* BooleanTest's result is boolean ... */
1031 420 : coll = InvalidOid; /* ... so it has no collation */
1032 420 : 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 25800 : case T_SetToDefault:
1040 25800 : coll = ((const SetToDefault *) expr)->collation;
1041 25800 : break;
1042 254 : case T_CurrentOfExpr:
1043 : /* CurrentOfExpr's result is boolean ... */
1044 254 : coll = InvalidOid; /* ... so it has no collation */
1045 254 : break;
1046 488 : case T_NextValueExpr:
1047 : /* NextValueExpr's result is an integer type ... */
1048 488 : coll = InvalidOid; /* ... so it has no collation */
1049 488 : break;
1050 0 : case T_InferenceElem:
1051 0 : coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
1052 0 : break;
1053 3550 : case T_PlaceHolderVar:
1054 3550 : coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1055 3550 : 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 12107302 : 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 1747516 : exprSetCollation(Node *expr, Oid collation)
1120 : {
1121 1747516 : 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 39734 : case T_Aggref:
1133 39734 : ((Aggref *) expr)->aggcollid = collation;
1134 39734 : break;
1135 314 : case T_GroupingFunc:
1136 : Assert(!OidIsValid(collation));
1137 314 : break;
1138 3288 : case T_WindowFunc:
1139 3288 : ((WindowFunc *) expr)->wincollid = collation;
1140 3288 : break;
1141 174 : case T_MergeSupportFunc:
1142 174 : ((MergeSupportFunc *) expr)->msfcollid = collation;
1143 174 : break;
1144 11646 : case T_SubscriptingRef:
1145 11646 : ((SubscriptingRef *) expr)->refcollid = collation;
1146 11646 : break;
1147 439942 : case T_FuncExpr:
1148 439942 : ((FuncExpr *) expr)->funccollid = collation;
1149 439942 : break;
1150 48974 : case T_NamedArgExpr:
1151 : Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
1152 48974 : break;
1153 615956 : case T_OpExpr:
1154 615956 : ((OpExpr *) expr)->opcollid = collation;
1155 615956 : break;
1156 950 : case T_DistinctExpr:
1157 950 : ((DistinctExpr *) expr)->opcollid = collation;
1158 950 : break;
1159 242 : case T_NullIfExpr:
1160 242 : ((NullIfExpr *) expr)->opcollid = collation;
1161 242 : break;
1162 30052 : case T_ScalarArrayOpExpr:
1163 : /* ScalarArrayOpExpr's result is boolean ... */
1164 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1165 30052 : break;
1166 143462 : case T_BoolExpr:
1167 : /* BoolExpr's result is boolean ... */
1168 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1169 143462 : break;
1170 40222 : 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 40222 : 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 127734 : case T_RelabelType:
1204 127734 : ((RelabelType *) expr)->resultcollid = collation;
1205 127734 : break;
1206 23076 : case T_CoerceViaIO:
1207 23076 : ((CoerceViaIO *) expr)->resultcollid = collation;
1208 23076 : break;
1209 4976 : case T_ArrayCoerceExpr:
1210 4976 : ((ArrayCoerceExpr *) expr)->resultcollid = collation;
1211 4976 : 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 79924 : case T_CaseExpr:
1217 79924 : ((CaseExpr *) expr)->casecollid = collation;
1218 79924 : break;
1219 25238 : case T_ArrayExpr:
1220 25238 : ((ArrayExpr *) expr)->array_collid = collation;
1221 25238 : 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 5710 : case T_CoalesceExpr:
1231 5710 : ((CoalesceExpr *) expr)->coalescecollid = collation;
1232 5710 : break;
1233 318 : case T_MinMaxExpr:
1234 318 : ((MinMaxExpr *) expr)->minmaxcollid = collation;
1235 318 : break;
1236 2560 : case T_SQLValueFunction:
1237 : Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
1238 : (collation == C_COLLATION_OID) :
1239 : (collation == InvalidOid));
1240 2560 : 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 19716 : case T_NullTest:
1283 : /* NullTest's result is boolean ... */
1284 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1285 19716 : break;
1286 812 : case T_BooleanTest:
1287 : /* BooleanTest's result is boolean ... */
1288 : Assert(!OidIsValid(collation)); /* ... so never set a collation */
1289 812 : 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 1747516 : }
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 1671866 : exprSetInputCollation(Node *expr, Oid inputcollation)
1323 : {
1324 1671866 : switch (nodeTag(expr))
1325 : {
1326 39734 : case T_Aggref:
1327 39734 : ((Aggref *) expr)->inputcollid = inputcollation;
1328 39734 : break;
1329 3288 : case T_WindowFunc:
1330 3288 : ((WindowFunc *) expr)->inputcollid = inputcollation;
1331 3288 : break;
1332 439594 : case T_FuncExpr:
1333 439594 : ((FuncExpr *) expr)->inputcollid = inputcollation;
1334 439594 : break;
1335 615944 : case T_OpExpr:
1336 615944 : ((OpExpr *) expr)->inputcollid = inputcollation;
1337 615944 : break;
1338 950 : case T_DistinctExpr:
1339 950 : ((DistinctExpr *) expr)->inputcollid = inputcollation;
1340 950 : break;
1341 242 : case T_NullIfExpr:
1342 242 : ((NullIfExpr *) expr)->inputcollid = inputcollation;
1343 242 : break;
1344 30052 : case T_ScalarArrayOpExpr:
1345 30052 : ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
1346 30052 : break;
1347 318 : case T_MinMaxExpr:
1348 318 : ((MinMaxExpr *) expr)->inputcollid = inputcollation;
1349 318 : break;
1350 541744 : default:
1351 541744 : break;
1352 : }
1353 1671866 : }
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 3998552 : exprLocation(const Node *expr)
1387 : {
1388 : int loc;
1389 :
1390 3998552 : if (expr == NULL)
1391 22232 : return -1;
1392 3976320 : 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 1888806 : case T_Var:
1401 1888806 : loc = ((const Var *) expr)->location;
1402 1888806 : break;
1403 1259366 : case T_Const:
1404 1259366 : loc = ((const Const *) expr)->location;
1405 1259366 : break;
1406 301180 : case T_Param:
1407 301180 : loc = ((const Param *) expr)->location;
1408 301180 : break;
1409 8118 : case T_Aggref:
1410 : /* function name should always be the first thing */
1411 8118 : loc = ((const Aggref *) expr)->location;
1412 8118 : 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 82238 : case T_FuncExpr:
1428 : {
1429 82238 : const FuncExpr *fexpr = (const FuncExpr *) expr;
1430 :
1431 : /* consider both function name and leftmost arg */
1432 82238 : loc = leftmostLoc(fexpr->location,
1433 82238 : exprLocation((Node *) fexpr->args));
1434 : }
1435 82238 : 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 12184 : case T_OpExpr:
1446 : case T_DistinctExpr: /* struct-equivalent to OpExpr */
1447 : case T_NullIfExpr: /* struct-equivalent to OpExpr */
1448 : {
1449 12184 : const OpExpr *opexpr = (const OpExpr *) expr;
1450 :
1451 : /* consider both operator name and leftmost arg */
1452 12184 : loc = leftmostLoc(opexpr->location,
1453 12184 : exprLocation((Node *) opexpr->args));
1454 : }
1455 12184 : 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 12690 : case T_RelabelType:
1496 : {
1497 12690 : const RelabelType *rexpr = (const RelabelType *) expr;
1498 :
1499 : /* Much as above */
1500 12690 : loc = leftmostLoc(rexpr->location,
1501 12690 : exprLocation((Node *) rexpr->arg));
1502 : }
1503 12690 : break;
1504 21014 : case T_CoerceViaIO:
1505 : {
1506 21014 : const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
1507 :
1508 : /* Much as above */
1509 21014 : loc = leftmostLoc(cexpr->location,
1510 21014 : exprLocation((Node *) cexpr->arg));
1511 : }
1512 21014 : 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 9390 : case T_CaseExpr:
1536 : /* CASE keyword should always be the first thing */
1537 9390 : loc = ((const CaseExpr *) expr)->location;
1538 9390 : 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 252 : case T_RowExpr:
1548 : /* the location points at ROW or (, which must be leftmost */
1549 252 : loc = ((const RowExpr *) expr)->location;
1550 252 : 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 1644 : case T_SQLValueFunction:
1564 : /* function keyword should always be the first thing */
1565 1644 : loc = ((const SQLValueFunction *) expr)->location;
1566 1644 : 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 50060 : case T_SetToDefault:
1631 50060 : loc = ((const SetToDefault *) expr)->location;
1632 50060 : 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 79582 : case T_List:
1642 : {
1643 : /* report location of first list member that has a location */
1644 : ListCell *lc;
1645 :
1646 79582 : loc = -1; /* just to suppress compiler warning */
1647 80366 : foreach(lc, (const List *) expr)
1648 : {
1649 79952 : loc = exprLocation((Node *) lfirst(lc));
1650 79952 : if (loc >= 0)
1651 79168 : break;
1652 : }
1653 : }
1654 79582 : break;
1655 4914 : case T_A_Expr:
1656 : {
1657 4914 : 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 4914 : loc = leftmostLoc(aexpr->location,
1662 4914 : exprLocation(aexpr->lexpr));
1663 : }
1664 4914 : break;
1665 63962 : case T_ColumnRef:
1666 63962 : loc = ((const ColumnRef *) expr)->location;
1667 63962 : break;
1668 0 : case T_ParamRef:
1669 0 : loc = ((const ParamRef *) expr)->location;
1670 0 : break;
1671 60804 : case T_A_Const:
1672 60804 : loc = ((const A_Const *) expr)->location;
1673 60804 : break;
1674 3456 : case T_FuncCall:
1675 : {
1676 3456 : 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 3456 : loc = leftmostLoc(fc->location,
1681 3456 : exprLocation((Node *) fc->args));
1682 : }
1683 3456 : 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 7090 : case T_TypeCast:
1696 : {
1697 7090 : 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 7090 : loc = exprLocation(tc->arg);
1704 7090 : loc = leftmostLoc(loc, tc->typeName->location);
1705 7090 : loc = leftmostLoc(loc, tc->location);
1706 : }
1707 7090 : 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 32450 : default:
1803 : /* for any other node type it's just unknown... */
1804 32450 : loc = -1;
1805 32450 : break;
1806 : }
1807 3976320 : 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 219404 : leftmostLoc(int loc1, int loc2)
1817 : {
1818 219404 : if (loc1 < 0)
1819 19436 : return loc2;
1820 199968 : else if (loc2 < 0)
1821 21184 : return loc1;
1822 : else
1823 178784 : 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 460734 : fix_opfuncids(Node *node)
1838 : {
1839 : /* This tree walk requires no special setup, so away we go... */
1840 460734 : fix_opfuncids_walker(node, NULL);
1841 460734 : }
1842 :
1843 : static bool
1844 1011326 : fix_opfuncids_walker(Node *node, void *context)
1845 : {
1846 1011326 : if (node == NULL)
1847 53930 : return false;
1848 957396 : if (IsA(node, OpExpr))
1849 57824 : set_opfuncid((OpExpr *) node);
1850 899572 : else if (IsA(node, DistinctExpr))
1851 6 : set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1852 899566 : else if (IsA(node, NullIfExpr))
1853 84 : set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1854 899482 : else if (IsA(node, ScalarArrayOpExpr))
1855 2124 : set_sa_opfuncid((ScalarArrayOpExpr *) node);
1856 957396 : 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 3459872 : set_opfuncid(OpExpr *opexpr)
1869 : {
1870 3459872 : if (opexpr->opfuncid == InvalidOid)
1871 207576 : opexpr->opfuncid = get_opcode(opexpr->opno);
1872 3459872 : }
1873 :
1874 : /*
1875 : * set_sa_opfuncid
1876 : * As above, for ScalarArrayOpExpr nodes.
1877 : */
1878 : void
1879 166330 : set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1880 : {
1881 166330 : if (opexpr->opfuncid == InvalidOid)
1882 492 : opexpr->opfuncid = get_opcode(opexpr->opno);
1883 166330 : }
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 19183388 : check_functions_in_node(Node *node, check_function_callback checker,
1907 : void *context)
1908 : {
1909 19183388 : switch (nodeTag(node))
1910 : {
1911 90870 : case T_Aggref:
1912 : {
1913 90870 : Aggref *expr = (Aggref *) node;
1914 :
1915 90870 : if (checker(expr->aggfnoid, context))
1916 1052 : return true;
1917 : }
1918 89818 : break;
1919 7022 : case T_WindowFunc:
1920 : {
1921 7022 : WindowFunc *expr = (WindowFunc *) node;
1922 :
1923 7022 : if (checker(expr->winfnoid, context))
1924 150 : return true;
1925 : }
1926 6872 : break;
1927 566508 : case T_FuncExpr:
1928 : {
1929 566508 : FuncExpr *expr = (FuncExpr *) node;
1930 :
1931 566508 : if (checker(expr->funcid, context))
1932 120688 : return true;
1933 : }
1934 445820 : break;
1935 1270464 : case T_OpExpr:
1936 : case T_DistinctExpr: /* struct-equivalent to OpExpr */
1937 : case T_NullIfExpr: /* struct-equivalent to OpExpr */
1938 : {
1939 1270464 : OpExpr *expr = (OpExpr *) node;
1940 :
1941 : /* Set opfuncid if it wasn't set already */
1942 1270464 : set_opfuncid(expr);
1943 1270464 : if (checker(expr->opfuncid, context))
1944 738 : return true;
1945 : }
1946 1269726 : break;
1947 55610 : case T_ScalarArrayOpExpr:
1948 : {
1949 55610 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1950 :
1951 55610 : set_sa_opfuncid(expr);
1952 55610 : if (checker(expr->opfuncid, context))
1953 90 : return true;
1954 : }
1955 55520 : break;
1956 35732 : case T_CoerceViaIO:
1957 : {
1958 35732 : CoerceViaIO *expr = (CoerceViaIO *) node;
1959 : Oid iofunc;
1960 : Oid typioparam;
1961 : bool typisvarlena;
1962 :
1963 : /* check the result type's input function */
1964 35732 : getTypeInputInfo(expr->resulttype,
1965 : &iofunc, &typioparam);
1966 35732 : if (checker(iofunc, context))
1967 658 : return true;
1968 : /* check the input type's output function */
1969 35684 : getTypeOutputInfo(exprType((Node *) expr->arg),
1970 : &iofunc, &typisvarlena);
1971 35684 : if (checker(iofunc, context))
1972 610 : return true;
1973 : }
1974 35074 : 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 17156948 : default:
1990 17156948 : break;
1991 : }
1992 19060012 : 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 90007314 : 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 90007314 : if (node == NULL)
2108 1867880 : return false;
2109 :
2110 : /* Guard against stack overflow due to overly complex expressions */
2111 88139434 : check_stack_depth();
2112 :
2113 88139434 : switch (nodeTag(node))
2114 : {
2115 35462088 : 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 35462088 : break;
2130 7446 : case T_WithCheckOption:
2131 7446 : return WALK(((WithCheckOption *) node)->qual);
2132 276656 : case T_Aggref:
2133 : {
2134 276656 : Aggref *expr = (Aggref *) node;
2135 :
2136 : /* recurse directly on Lists */
2137 276656 : if (LIST_WALK(expr->aggdirectargs))
2138 0 : return true;
2139 276656 : if (LIST_WALK(expr->args))
2140 19090 : return true;
2141 257566 : if (LIST_WALK(expr->aggorder))
2142 0 : return true;
2143 257566 : if (LIST_WALK(expr->aggdistinct))
2144 0 : return true;
2145 257566 : if (WALK(expr->aggfilter))
2146 70 : return true;
2147 : }
2148 257496 : 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 17088 : case T_WindowFunc:
2158 : {
2159 17088 : WindowFunc *expr = (WindowFunc *) node;
2160 :
2161 : /* recurse directly on List */
2162 17088 : if (LIST_WALK(expr->args))
2163 618 : return true;
2164 16470 : if (WALK(expr->aggfilter))
2165 12 : return true;
2166 16458 : if (WALK(expr->runCondition))
2167 0 : return true;
2168 : }
2169 16458 : break;
2170 534 : case T_WindowFuncRunCondition:
2171 : {
2172 534 : WindowFuncRunCondition *expr = (WindowFuncRunCondition *) node;
2173 :
2174 534 : if (WALK(expr->arg))
2175 0 : return true;
2176 : }
2177 534 : break;
2178 180668 : case T_SubscriptingRef:
2179 : {
2180 180668 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
2181 :
2182 : /* recurse directly for upper/lower container index lists */
2183 180668 : if (LIST_WALK(sbsref->refupperindexpr))
2184 10390 : return true;
2185 170278 : if (LIST_WALK(sbsref->reflowerindexpr))
2186 0 : return true;
2187 : /* walker must see the refexpr and refassgnexpr, however */
2188 170278 : if (WALK(sbsref->refexpr))
2189 8170 : return true;
2190 :
2191 162108 : if (WALK(sbsref->refassgnexpr))
2192 154 : return true;
2193 : }
2194 161954 : break;
2195 3310486 : case T_FuncExpr:
2196 : {
2197 3310486 : FuncExpr *expr = (FuncExpr *) node;
2198 :
2199 3310486 : if (LIST_WALK(expr->args))
2200 70400 : return true;
2201 : }
2202 3240080 : break;
2203 103868 : case T_NamedArgExpr:
2204 103868 : return WALK(((NamedArgExpr *) node)->arg);
2205 7352058 : case T_OpExpr:
2206 : case T_DistinctExpr: /* struct-equivalent to OpExpr */
2207 : case T_NullIfExpr: /* struct-equivalent to OpExpr */
2208 : {
2209 7352058 : OpExpr *expr = (OpExpr *) node;
2210 :
2211 7352058 : if (LIST_WALK(expr->args))
2212 63596 : return true;
2213 : }
2214 7288354 : break;
2215 386468 : case T_ScalarArrayOpExpr:
2216 : {
2217 386468 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2218 :
2219 386468 : if (LIST_WALK(expr->args))
2220 36388 : return true;
2221 : }
2222 350080 : break;
2223 948928 : case T_BoolExpr:
2224 : {
2225 948928 : BoolExpr *expr = (BoolExpr *) node;
2226 :
2227 948928 : if (LIST_WALK(expr->args))
2228 15880 : return true;
2229 : }
2230 933042 : break;
2231 203170 : case T_SubLink:
2232 : {
2233 203170 : SubLink *sublink = (SubLink *) node;
2234 :
2235 203170 : if (WALK(sublink->testexpr))
2236 54 : return true;
2237 :
2238 : /*
2239 : * Also invoke the walker on the sublink's Query node, so it
2240 : * can recurse into the sub-query if it wants to.
2241 : */
2242 203116 : return WALK(sublink->subselect);
2243 : }
2244 : break;
2245 92382 : case T_SubPlan:
2246 : {
2247 92382 : SubPlan *subplan = (SubPlan *) node;
2248 :
2249 : /* recurse into the testexpr, but not into the Plan */
2250 92382 : if (WALK(subplan->testexpr))
2251 72 : return true;
2252 : /* also examine args list */
2253 92310 : if (LIST_WALK(subplan->args))
2254 396 : return true;
2255 : }
2256 91914 : break;
2257 6804 : case T_AlternativeSubPlan:
2258 6804 : return LIST_WALK(((AlternativeSubPlan *) node)->subplans);
2259 83866 : case T_FieldSelect:
2260 83866 : return WALK(((FieldSelect *) node)->arg);
2261 3086 : case T_FieldStore:
2262 : {
2263 3086 : FieldStore *fstore = (FieldStore *) node;
2264 :
2265 3086 : if (WALK(fstore->arg))
2266 0 : return true;
2267 3086 : if (WALK(fstore->newvals))
2268 12 : return true;
2269 : }
2270 3074 : break;
2271 963770 : case T_RelabelType:
2272 963770 : return WALK(((RelabelType *) node)->arg);
2273 216826 : case T_CoerceViaIO:
2274 216826 : return WALK(((CoerceViaIO *) node)->arg);
2275 47168 : case T_ArrayCoerceExpr:
2276 : {
2277 47168 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2278 :
2279 47168 : if (WALK(acoerce->arg))
2280 3676 : return true;
2281 43492 : if (WALK(acoerce->elemexpr))
2282 24 : return true;
2283 : }
2284 43468 : break;
2285 2818 : case T_ConvertRowtypeExpr:
2286 2818 : return WALK(((ConvertRowtypeExpr *) node)->arg);
2287 29030 : case T_CollateExpr:
2288 29030 : return WALK(((CollateExpr *) node)->arg);
2289 462016 : case T_CaseExpr:
2290 : {
2291 462016 : CaseExpr *caseexpr = (CaseExpr *) node;
2292 :
2293 462016 : if (WALK(caseexpr->arg))
2294 64 : return true;
2295 : /* we assume walker doesn't care about CaseWhens, either */
2296 1211876 : foreach(temp, caseexpr->args)
2297 : {
2298 758984 : CaseWhen *when = lfirst_node(CaseWhen, temp);
2299 :
2300 758984 : if (WALK(when->expr))
2301 9060 : return true;
2302 755736 : if (WALK(when->result))
2303 5812 : return true;
2304 : }
2305 452892 : if (WALK(caseexpr->defresult))
2306 9234 : return true;
2307 : }
2308 443658 : break;
2309 206056 : case T_ArrayExpr:
2310 206056 : return WALK(((ArrayExpr *) node)->elements);
2311 30648 : case T_RowExpr:
2312 : /* Assume colnames isn't interesting */
2313 30648 : return WALK(((RowExpr *) node)->args);
2314 1578 : case T_RowCompareExpr:
2315 : {
2316 1578 : RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2317 :
2318 1578 : if (WALK(rcexpr->largs))
2319 0 : return true;
2320 1578 : if (WALK(rcexpr->rargs))
2321 0 : return true;
2322 : }
2323 1578 : break;
2324 45302 : case T_CoalesceExpr:
2325 45302 : return WALK(((CoalesceExpr *) node)->args);
2326 5460 : case T_MinMaxExpr:
2327 5460 : return WALK(((MinMaxExpr *) node)->args);
2328 4998 : case T_XmlExpr:
2329 : {
2330 4998 : XmlExpr *xexpr = (XmlExpr *) node;
2331 :
2332 4998 : if (WALK(xexpr->named_args))
2333 12 : return true;
2334 : /* we assume walker doesn't care about arg_names */
2335 4986 : if (WALK(xexpr->args))
2336 24 : return true;
2337 : }
2338 4962 : break;
2339 3300 : case T_JsonValueExpr:
2340 : {
2341 3300 : JsonValueExpr *jve = (JsonValueExpr *) node;
2342 :
2343 3300 : if (WALK(jve->raw_expr))
2344 0 : return true;
2345 3300 : if (WALK(jve->formatted_expr))
2346 0 : return true;
2347 : }
2348 3300 : break;
2349 8454 : case T_JsonConstructorExpr:
2350 : {
2351 8454 : JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
2352 :
2353 8454 : if (WALK(ctor->args))
2354 0 : return true;
2355 8454 : if (WALK(ctor->func))
2356 48 : return true;
2357 8406 : if (WALK(ctor->coercion))
2358 0 : return true;
2359 : }
2360 8406 : break;
2361 2472 : case T_JsonIsPredicate:
2362 2472 : return WALK(((JsonIsPredicate *) node)->expr);
2363 16084 : case T_JsonExpr:
2364 : {
2365 16084 : JsonExpr *jexpr = (JsonExpr *) node;
2366 :
2367 16084 : if (WALK(jexpr->formatted_expr))
2368 84 : return true;
2369 16000 : if (WALK(jexpr->path_spec))
2370 6 : return true;
2371 15994 : if (WALK(jexpr->coercion_expr))
2372 84 : return true;
2373 15910 : if (WALK(jexpr->passing_values))
2374 6 : return true;
2375 : /* we assume walker doesn't care about passing_names */
2376 15904 : if (WALK(jexpr->on_empty))
2377 0 : return true;
2378 15904 : if (WALK(jexpr->on_error))
2379 6 : return true;
2380 : }
2381 15898 : break;
2382 17248 : case T_JsonBehavior:
2383 : {
2384 17248 : JsonBehavior *behavior = (JsonBehavior *) node;
2385 :
2386 17248 : if (WALK(behavior->expr))
2387 6 : return true;
2388 : }
2389 17242 : break;
2390 239696 : case T_NullTest:
2391 239696 : return WALK(((NullTest *) node)->arg);
2392 12384 : case T_BooleanTest:
2393 12384 : return WALK(((BooleanTest *) node)->arg);
2394 320578 : case T_CoerceToDomain:
2395 320578 : return WALK(((CoerceToDomain *) node)->arg);
2396 14888138 : case T_TargetEntry:
2397 14888138 : return WALK(((TargetEntry *) node)->expr);
2398 98488 : case T_Query:
2399 : /* Do nothing with a sub-Query, per discussion above */
2400 98488 : break;
2401 136 : case T_WindowClause:
2402 : {
2403 136 : WindowClause *wc = (WindowClause *) node;
2404 :
2405 136 : if (WALK(wc->partitionClause))
2406 0 : return true;
2407 136 : if (WALK(wc->orderClause))
2408 0 : return true;
2409 136 : if (WALK(wc->startOffset))
2410 0 : return true;
2411 136 : if (WALK(wc->endOffset))
2412 0 : return true;
2413 : }
2414 136 : break;
2415 84 : case T_CTECycleClause:
2416 : {
2417 84 : CTECycleClause *cc = (CTECycleClause *) node;
2418 :
2419 84 : if (WALK(cc->cycle_mark_value))
2420 0 : return true;
2421 84 : if (WALK(cc->cycle_mark_default))
2422 0 : return true;
2423 : }
2424 84 : break;
2425 7846 : case T_CommonTableExpr:
2426 : {
2427 7846 : CommonTableExpr *cte = (CommonTableExpr *) node;
2428 :
2429 : /*
2430 : * Invoke the walker on the CTE's Query node, so it can
2431 : * recurse into the sub-query if it wants to.
2432 : */
2433 7846 : if (WALK(cte->ctequery))
2434 92 : return true;
2435 :
2436 7754 : if (WALK(cte->search_clause))
2437 0 : return true;
2438 7754 : if (WALK(cte->cycle_clause))
2439 0 : return true;
2440 : }
2441 7754 : break;
2442 0 : case T_JsonKeyValue:
2443 : {
2444 0 : JsonKeyValue *kv = (JsonKeyValue *) node;
2445 :
2446 0 : if (WALK(kv->key))
2447 0 : return true;
2448 0 : if (WALK(kv->value))
2449 0 : return true;
2450 : }
2451 0 : break;
2452 0 : case T_JsonObjectConstructor:
2453 : {
2454 0 : JsonObjectConstructor *ctor = (JsonObjectConstructor *) node;
2455 :
2456 0 : if (LIST_WALK(ctor->exprs))
2457 0 : return true;
2458 : }
2459 0 : break;
2460 0 : case T_JsonArrayConstructor:
2461 : {
2462 0 : JsonArrayConstructor *ctor = (JsonArrayConstructor *) node;
2463 :
2464 0 : if (LIST_WALK(ctor->exprs))
2465 0 : return true;
2466 : }
2467 0 : break;
2468 0 : case T_JsonArrayQueryConstructor:
2469 : {
2470 0 : JsonArrayQueryConstructor *ctor = (JsonArrayQueryConstructor *) node;
2471 :
2472 0 : if (WALK(ctor->query))
2473 0 : return true;
2474 : }
2475 0 : break;
2476 0 : case T_JsonAggConstructor:
2477 : {
2478 0 : JsonAggConstructor *ctor = (JsonAggConstructor *) node;
2479 :
2480 0 : if (WALK(ctor->agg_filter))
2481 0 : return true;
2482 0 : if (WALK(ctor->agg_order))
2483 0 : return true;
2484 0 : if (WALK(ctor->over))
2485 0 : return true;
2486 : }
2487 0 : break;
2488 0 : case T_JsonObjectAgg:
2489 : {
2490 0 : JsonObjectAgg *ctor = (JsonObjectAgg *) node;
2491 :
2492 0 : if (WALK(ctor->constructor))
2493 0 : return true;
2494 0 : if (WALK(ctor->arg))
2495 0 : return true;
2496 : }
2497 0 : break;
2498 0 : case T_JsonArrayAgg:
2499 : {
2500 0 : JsonArrayAgg *ctor = (JsonArrayAgg *) node;
2501 :
2502 0 : if (WALK(ctor->constructor))
2503 0 : return true;
2504 0 : if (WALK(ctor->arg))
2505 0 : return true;
2506 : }
2507 0 : break;
2508 :
2509 4186 : case T_PartitionBoundSpec:
2510 : {
2511 4186 : PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
2512 :
2513 4186 : if (WALK(pbs->listdatums))
2514 0 : return true;
2515 4186 : if (WALK(pbs->lowerdatums))
2516 0 : return true;
2517 4186 : if (WALK(pbs->upperdatums))
2518 0 : return true;
2519 : }
2520 4186 : break;
2521 5760 : case T_PartitionRangeDatum:
2522 : {
2523 5760 : PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
2524 :
2525 5760 : if (WALK(prd->value))
2526 0 : return true;
2527 : }
2528 5760 : break;
2529 20317188 : case T_List:
2530 69291170 : foreach(temp, (List *) node)
2531 : {
2532 49717712 : if (WALK(lfirst(temp)))
2533 743436 : return true;
2534 : }
2535 19573458 : break;
2536 1264228 : case T_FromExpr:
2537 : {
2538 1264228 : FromExpr *from = (FromExpr *) node;
2539 :
2540 1264228 : if (LIST_WALK(from->fromlist))
2541 58656 : return true;
2542 1205572 : if (WALK(from->quals))
2543 2480 : return true;
2544 : }
2545 1203080 : break;
2546 2414 : case T_OnConflictExpr:
2547 : {
2548 2414 : OnConflictExpr *onconflict = (OnConflictExpr *) node;
2549 :
2550 2414 : if (WALK(onconflict->arbiterElems))
2551 0 : return true;
2552 2414 : if (WALK(onconflict->arbiterWhere))
2553 0 : return true;
2554 2414 : if (WALK(onconflict->onConflictSet))
2555 0 : return true;
2556 2414 : if (WALK(onconflict->onConflictWhere))
2557 0 : return true;
2558 2414 : if (WALK(onconflict->exclRelTlist))
2559 0 : return true;
2560 : }
2561 2414 : break;
2562 6032 : case T_MergeAction:
2563 : {
2564 6032 : MergeAction *action = (MergeAction *) node;
2565 :
2566 6032 : if (WALK(action->qual))
2567 134 : return true;
2568 5898 : if (WALK(action->targetList))
2569 338 : return true;
2570 : }
2571 5560 : break;
2572 0 : case T_PartitionPruneStepOp:
2573 : {
2574 0 : PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
2575 :
2576 0 : if (WALK(opstep->exprs))
2577 0 : return true;
2578 : }
2579 0 : break;
2580 0 : case T_PartitionPruneStepCombine:
2581 : /* no expression subnodes */
2582 0 : break;
2583 260856 : case T_JoinExpr:
2584 : {
2585 260856 : JoinExpr *join = (JoinExpr *) node;
2586 :
2587 260856 : if (WALK(join->larg))
2588 13802 : return true;
2589 247054 : if (WALK(join->rarg))
2590 16472 : return true;
2591 230582 : if (WALK(join->quals))
2592 36 : return true;
2593 :
2594 : /*
2595 : * alias clause, using list are deemed uninteresting.
2596 : */
2597 : }
2598 230546 : break;
2599 21518 : case T_SetOperationStmt:
2600 : {
2601 21518 : SetOperationStmt *setop = (SetOperationStmt *) node;
2602 :
2603 21518 : if (WALK(setop->larg))
2604 0 : return true;
2605 21518 : if (WALK(setop->rarg))
2606 0 : return true;
2607 :
2608 : /* groupClauses are deemed uninteresting */
2609 : }
2610 21518 : break;
2611 0 : case T_IndexClause:
2612 : {
2613 0 : IndexClause *iclause = (IndexClause *) node;
2614 :
2615 0 : if (WALK(iclause->rinfo))
2616 0 : return true;
2617 0 : if (LIST_WALK(iclause->indexquals))
2618 0 : return true;
2619 : }
2620 0 : break;
2621 18532 : case T_PlaceHolderVar:
2622 18532 : return WALK(((PlaceHolderVar *) node)->phexpr);
2623 2472 : case T_InferenceElem:
2624 2472 : return WALK(((InferenceElem *) node)->expr);
2625 1288 : case T_AppendRelInfo:
2626 : {
2627 1288 : AppendRelInfo *appinfo = (AppendRelInfo *) node;
2628 :
2629 1288 : if (LIST_WALK(appinfo->translated_vars))
2630 0 : return true;
2631 : }
2632 1288 : break;
2633 0 : case T_PlaceHolderInfo:
2634 0 : return WALK(((PlaceHolderInfo *) node)->ph_var);
2635 162314 : case T_RangeTblFunction:
2636 162314 : return WALK(((RangeTblFunction *) node)->funcexpr);
2637 746 : case T_TableSampleClause:
2638 : {
2639 746 : TableSampleClause *tsc = (TableSampleClause *) node;
2640 :
2641 746 : if (LIST_WALK(tsc->args))
2642 0 : return true;
2643 746 : if (WALK(tsc->repeatable))
2644 0 : return true;
2645 : }
2646 746 : break;
2647 2838 : case T_TableFunc:
2648 : {
2649 2838 : TableFunc *tf = (TableFunc *) node;
2650 :
2651 2838 : if (WALK(tf->ns_uris))
2652 0 : return true;
2653 2838 : if (WALK(tf->docexpr))
2654 90 : return true;
2655 2748 : if (WALK(tf->rowexpr))
2656 0 : return true;
2657 2748 : if (WALK(tf->colexprs))
2658 0 : return true;
2659 2748 : if (WALK(tf->coldefexprs))
2660 0 : return true;
2661 2748 : if (WALK(tf->colvalexprs))
2662 0 : return true;
2663 2748 : if (WALK(tf->passingvalexprs))
2664 0 : return true;
2665 : }
2666 2748 : break;
2667 0 : default:
2668 0 : elog(ERROR, "unrecognized node type: %d",
2669 : (int) nodeTag(node));
2670 : break;
2671 : }
2672 69504228 : return false;
2673 :
2674 : /* The WALK() macro can be re-used below, but LIST_WALK() not so much */
2675 : #undef LIST_WALK
2676 : }
2677 :
2678 : /*
2679 : * query_tree_walker --- initiate a walk of a Query's expressions
2680 : *
2681 : * This routine exists just to reduce the number of places that need to know
2682 : * where all the expression subtrees of a Query are. Note it can be used
2683 : * for starting a walk at top level of a Query regardless of whether the
2684 : * walker intends to descend into subqueries. It is also useful for
2685 : * descending into subqueries within a walker.
2686 : *
2687 : * Some callers want to suppress visitation of certain items in the sub-Query,
2688 : * typically because they need to process them specially, or don't actually
2689 : * want to recurse into subqueries. This is supported by the flags argument,
2690 : * which is the bitwise OR of flag values to add or suppress visitation of
2691 : * indicated items. (More flag bits may be added as needed.)
2692 : */
2693 : bool
2694 1566696 : query_tree_walker_impl(Query *query,
2695 : tree_walker_callback walker,
2696 : void *context,
2697 : int flags)
2698 : {
2699 : Assert(query != NULL && IsA(query, Query));
2700 :
2701 : /*
2702 : * We don't walk any utilityStmt here. However, we can't easily assert
2703 : * that it is absent, since there are at least two code paths by which
2704 : * action statements from CREATE RULE end up here, and NOTIFY is allowed
2705 : * in a rule action.
2706 : */
2707 :
2708 1566696 : if (WALK(query->targetList))
2709 303902 : return true;
2710 1262764 : if (WALK(query->withCheckOptions))
2711 0 : return true;
2712 1262764 : if (WALK(query->onConflict))
2713 0 : return true;
2714 1262764 : if (WALK(query->mergeActionList))
2715 472 : return true;
2716 1262292 : if (WALK(query->mergeJoinCondition))
2717 248 : return true;
2718 1262044 : if (WALK(query->returningList))
2719 74 : return true;
2720 1261970 : if (WALK(query->jointree))
2721 60722 : return true;
2722 1201236 : if (WALK(query->setOperations))
2723 0 : return true;
2724 1201236 : if (WALK(query->havingQual))
2725 0 : return true;
2726 1201236 : if (WALK(query->limitOffset))
2727 6 : return true;
2728 1201230 : if (WALK(query->limitCount))
2729 0 : return true;
2730 :
2731 : /*
2732 : * Most callers aren't interested in SortGroupClause nodes since those
2733 : * don't contain actual expressions. However they do contain OIDs which
2734 : * may be needed by dependency walkers etc.
2735 : */
2736 1201230 : if ((flags & QTW_EXAMINE_SORTGROUP))
2737 : {
2738 30000 : if (WALK(query->groupClause))
2739 0 : return true;
2740 30000 : if (WALK(query->windowClause))
2741 0 : return true;
2742 30000 : if (WALK(query->sortClause))
2743 0 : return true;
2744 30000 : if (WALK(query->distinctClause))
2745 0 : return true;
2746 : }
2747 : else
2748 : {
2749 : /*
2750 : * But we need to walk the expressions under WindowClause nodes even
2751 : * if we're not interested in SortGroupClause nodes.
2752 : */
2753 : ListCell *lc;
2754 :
2755 1176714 : foreach(lc, query->windowClause)
2756 : {
2757 5490 : WindowClause *wc = lfirst_node(WindowClause, lc);
2758 :
2759 5490 : if (WALK(wc->startOffset))
2760 6 : return true;
2761 5484 : if (WALK(wc->endOffset))
2762 0 : return true;
2763 : }
2764 : }
2765 :
2766 : /*
2767 : * groupingSets and rowMarks are not walked:
2768 : *
2769 : * groupingSets contain only ressortgrouprefs (integers) which are
2770 : * meaningless without the corresponding groupClause or tlist.
2771 : * Accordingly, any walker that needs to care about them needs to handle
2772 : * them itself in its Query processing.
2773 : *
2774 : * rowMarks is not walked because it contains only rangetable indexes (and
2775 : * flags etc.) and therefore should be handled at Query level similarly.
2776 : */
2777 :
2778 1201224 : if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2779 : {
2780 618860 : if (WALK(query->cteList))
2781 86 : return true;
2782 : }
2783 1201138 : if (!(flags & QTW_IGNORE_RANGE_TABLE))
2784 : {
2785 655646 : if (range_table_walker(query->rtable, walker, context, flags))
2786 17316 : return true;
2787 : }
2788 1183822 : return false;
2789 : }
2790 :
2791 : /*
2792 : * range_table_walker is just the part of query_tree_walker that scans
2793 : * a query's rangetable. This is split out since it can be useful on
2794 : * its own.
2795 : */
2796 : bool
2797 657192 : range_table_walker_impl(List *rtable,
2798 : tree_walker_callback walker,
2799 : void *context,
2800 : int flags)
2801 : {
2802 : ListCell *rt;
2803 :
2804 1589416 : foreach(rt, rtable)
2805 : {
2806 949540 : RangeTblEntry *rte = lfirst_node(RangeTblEntry, rt);
2807 :
2808 949540 : if (range_table_entry_walker(rte, walker, context, flags))
2809 17316 : return true;
2810 : }
2811 639876 : return false;
2812 : }
2813 :
2814 : /*
2815 : * Some callers even want to scan the expressions in individual RTEs.
2816 : */
2817 : bool
2818 949564 : range_table_entry_walker_impl(RangeTblEntry *rte,
2819 : tree_walker_callback walker,
2820 : void *context,
2821 : int flags)
2822 : {
2823 : /*
2824 : * Walkers might need to examine the RTE node itself either before or
2825 : * after visiting its contents (or, conceivably, both). Note that if you
2826 : * specify neither flag, the walker won't be called on the RTE at all.
2827 : */
2828 949564 : if (flags & QTW_EXAMINE_RTES_BEFORE)
2829 77830 : if (WALK(rte))
2830 12 : return true;
2831 :
2832 949552 : switch (rte->rtekind)
2833 : {
2834 602464 : case RTE_RELATION:
2835 602464 : if (WALK(rte->tablesample))
2836 0 : return true;
2837 602464 : break;
2838 81672 : case RTE_SUBQUERY:
2839 81672 : if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2840 80324 : if (WALK(rte->subquery))
2841 832 : return true;
2842 80840 : break;
2843 149198 : case RTE_JOIN:
2844 149198 : if (!(flags & QTW_IGNORE_JOINALIASES))
2845 126080 : if (WALK(rte->joinaliasvars))
2846 0 : return true;
2847 149198 : break;
2848 68816 : case RTE_FUNCTION:
2849 68816 : if (WALK(rte->functions))
2850 16432 : return true;
2851 52384 : break;
2852 862 : case RTE_TABLEFUNC:
2853 862 : if (WALK(rte->tablefunc))
2854 0 : return true;
2855 862 : break;
2856 17226 : case RTE_VALUES:
2857 17226 : if (WALK(rte->values_lists))
2858 64 : return true;
2859 17162 : break;
2860 29314 : case RTE_CTE:
2861 : case RTE_NAMEDTUPLESTORE:
2862 : case RTE_RESULT:
2863 : /* nothing to do */
2864 29314 : break;
2865 : }
2866 :
2867 932224 : if (WALK(rte->securityQuals))
2868 0 : return true;
2869 :
2870 932224 : if (flags & QTW_EXAMINE_RTES_AFTER)
2871 18650 : if (WALK(rte))
2872 0 : return true;
2873 :
2874 932224 : return false;
2875 : }
2876 :
2877 :
2878 : /*
2879 : * expression_tree_mutator() is designed to support routines that make a
2880 : * modified copy of an expression tree, with some nodes being added,
2881 : * removed, or replaced by new subtrees. The original tree is (normally)
2882 : * not changed. Each recursion level is responsible for returning a copy of
2883 : * (or appropriately modified substitute for) the subtree it is handed.
2884 : * A mutator routine should look like this:
2885 : *
2886 : * Node * my_mutator (Node *node, my_struct *context)
2887 : * {
2888 : * if (node == NULL)
2889 : * return NULL;
2890 : * // check for nodes that special work is required for, eg:
2891 : * if (IsA(node, Var))
2892 : * {
2893 : * ... create and return modified copy of Var node
2894 : * }
2895 : * else if (IsA(node, ...))
2896 : * {
2897 : * ... do special transformations of other node types
2898 : * }
2899 : * // for any node type not specially processed, do:
2900 : * return expression_tree_mutator(node, my_mutator, (void *) context);
2901 : * }
2902 : *
2903 : * The "context" argument points to a struct that holds whatever context
2904 : * information the mutator routine needs --- it can be used to return extra
2905 : * data gathered by the mutator, too. This argument is not touched by
2906 : * expression_tree_mutator, but it is passed down to recursive sub-invocations
2907 : * of my_mutator. The tree walk is started from a setup routine that
2908 : * fills in the appropriate context struct, calls my_mutator with the
2909 : * top-level node of the tree, and does any required post-processing.
2910 : *
2911 : * Each level of recursion must return an appropriately modified Node.
2912 : * If expression_tree_mutator() is called, it will make an exact copy
2913 : * of the given Node, but invoke my_mutator() to copy the sub-node(s)
2914 : * of that Node. In this way, my_mutator() has full control over the
2915 : * copying process but need not directly deal with expression trees
2916 : * that it has no interest in.
2917 : *
2918 : * Just as for expression_tree_walker, the node types handled by
2919 : * expression_tree_mutator include all those normally found in target lists
2920 : * and qualifier clauses during the planning stage.
2921 : *
2922 : * expression_tree_mutator will handle SubLink nodes by recursing normally
2923 : * into the "testexpr" subtree (which is an expression belonging to the outer
2924 : * plan). It will also call the mutator on the sub-Query node; however, when
2925 : * expression_tree_mutator itself is called on a Query node, it does nothing
2926 : * and returns the unmodified Query node. The net effect is that unless the
2927 : * mutator does something special at a Query node, sub-selects will not be
2928 : * visited or modified; the original sub-select will be linked to by the new
2929 : * SubLink node. Mutators that want to descend into sub-selects will usually
2930 : * do so by recognizing Query nodes and calling query_tree_mutator (below).
2931 : *
2932 : * expression_tree_mutator will handle a SubPlan node by recursing into the
2933 : * "testexpr" and the "args" list (which belong to the outer plan), but it
2934 : * will simply copy the link to the inner plan, since that's typically what
2935 : * expression tree mutators want. A mutator that wants to modify the subplan
2936 : * can force appropriate behavior by recognizing SubPlan expression nodes
2937 : * and doing the right thing.
2938 : */
2939 :
2940 : Node *
2941 15036600 : expression_tree_mutator_impl(Node *node,
2942 : tree_mutator_callback mutator,
2943 : void *context)
2944 : {
2945 : /*
2946 : * The mutator has already decided not to modify the current node, but we
2947 : * must call the mutator for any sub-nodes.
2948 : */
2949 :
2950 : #define FLATCOPY(newnode, node, nodetype) \
2951 : ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2952 : memcpy((newnode), (node), sizeof(nodetype)) )
2953 :
2954 : #define MUTATE(newfield, oldfield, fieldtype) \
2955 : ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
2956 :
2957 15036600 : if (node == NULL)
2958 110984 : return NULL;
2959 :
2960 : /* Guard against stack overflow due to overly complex expressions */
2961 14925616 : check_stack_depth();
2962 :
2963 14925616 : switch (nodeTag(node))
2964 : {
2965 : /*
2966 : * Primitive node types with no expression subnodes. Var and
2967 : * Const are frequent enough to deserve special cases, the others
2968 : * we just use copyObject for.
2969 : */
2970 2527254 : case T_Var:
2971 : {
2972 2527254 : Var *var = (Var *) node;
2973 : Var *newnode;
2974 :
2975 2527254 : FLATCOPY(newnode, var, Var);
2976 : /* Assume we need not copy the varnullingrels bitmapset */
2977 2527254 : return (Node *) newnode;
2978 : }
2979 : break;
2980 2561672 : case T_Const:
2981 : {
2982 2561672 : Const *oldnode = (Const *) node;
2983 : Const *newnode;
2984 :
2985 2561672 : FLATCOPY(newnode, oldnode, Const);
2986 : /* XXX we don't bother with datumCopy; should we? */
2987 2561672 : return (Node *) newnode;
2988 : }
2989 : break;
2990 107832 : case T_Param:
2991 : case T_CaseTestExpr:
2992 : case T_SQLValueFunction:
2993 : case T_JsonFormat:
2994 : case T_CoerceToDomainValue:
2995 : case T_SetToDefault:
2996 : case T_CurrentOfExpr:
2997 : case T_NextValueExpr:
2998 : case T_RangeTblRef:
2999 : case T_SortGroupClause:
3000 : case T_CTESearchClause:
3001 : case T_MergeSupportFunc:
3002 107832 : return (Node *) copyObject(node);
3003 1126 : case T_WithCheckOption:
3004 : {
3005 1126 : WithCheckOption *wco = (WithCheckOption *) node;
3006 : WithCheckOption *newnode;
3007 :
3008 1126 : FLATCOPY(newnode, wco, WithCheckOption);
3009 1126 : MUTATE(newnode->qual, wco->qual, Node *);
3010 1126 : return (Node *) newnode;
3011 : }
3012 107474 : case T_Aggref:
3013 : {
3014 107474 : Aggref *aggref = (Aggref *) node;
3015 : Aggref *newnode;
3016 :
3017 107474 : FLATCOPY(newnode, aggref, Aggref);
3018 : /* assume mutation doesn't change types of arguments */
3019 107474 : newnode->aggargtypes = list_copy(aggref->aggargtypes);
3020 107474 : MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
3021 107474 : MUTATE(newnode->args, aggref->args, List *);
3022 107474 : MUTATE(newnode->aggorder, aggref->aggorder, List *);
3023 107474 : MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
3024 107474 : MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
3025 107474 : return (Node *) newnode;
3026 : }
3027 : break;
3028 1030 : case T_GroupingFunc:
3029 : {
3030 1030 : GroupingFunc *grouping = (GroupingFunc *) node;
3031 : GroupingFunc *newnode;
3032 :
3033 1030 : FLATCOPY(newnode, grouping, GroupingFunc);
3034 1030 : MUTATE(newnode->args, grouping->args, List *);
3035 :
3036 : /*
3037 : * We assume here that mutating the arguments does not change
3038 : * the semantics, i.e. that the arguments are not mutated in a
3039 : * way that makes them semantically different from their
3040 : * previously matching expressions in the GROUP BY clause.
3041 : *
3042 : * If a mutator somehow wanted to do this, it would have to
3043 : * handle the refs and cols lists itself as appropriate.
3044 : */
3045 1030 : newnode->refs = list_copy(grouping->refs);
3046 1030 : newnode->cols = list_copy(grouping->cols);
3047 :
3048 1030 : return (Node *) newnode;
3049 : }
3050 : break;
3051 4402 : case T_WindowFunc:
3052 : {
3053 4402 : WindowFunc *wfunc = (WindowFunc *) node;
3054 : WindowFunc *newnode;
3055 :
3056 4402 : FLATCOPY(newnode, wfunc, WindowFunc);
3057 4402 : MUTATE(newnode->args, wfunc->args, List *);
3058 4402 : MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
3059 4402 : return (Node *) newnode;
3060 : }
3061 : break;
3062 0 : case T_WindowFuncRunCondition:
3063 : {
3064 0 : WindowFuncRunCondition *wfuncrc = (WindowFuncRunCondition *) node;
3065 : WindowFuncRunCondition *newnode;
3066 :
3067 0 : FLATCOPY(newnode, wfuncrc, WindowFuncRunCondition);
3068 0 : MUTATE(newnode->arg, wfuncrc->arg, Expr *);
3069 0 : return (Node *) newnode;
3070 : }
3071 : break;
3072 37450 : case T_SubscriptingRef:
3073 : {
3074 37450 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
3075 : SubscriptingRef *newnode;
3076 :
3077 37450 : FLATCOPY(newnode, sbsref, SubscriptingRef);
3078 37450 : MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
3079 : List *);
3080 37450 : MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
3081 : List *);
3082 37450 : MUTATE(newnode->refexpr, sbsref->refexpr,
3083 : Expr *);
3084 37450 : MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
3085 : Expr *);
3086 :
3087 37450 : return (Node *) newnode;
3088 : }
3089 : break;
3090 271356 : case T_FuncExpr:
3091 : {
3092 271356 : FuncExpr *expr = (FuncExpr *) node;
3093 : FuncExpr *newnode;
3094 :
3095 271356 : FLATCOPY(newnode, expr, FuncExpr);
3096 271356 : MUTATE(newnode->args, expr->args, List *);
3097 271356 : return (Node *) newnode;
3098 : }
3099 : break;
3100 0 : case T_NamedArgExpr:
3101 : {
3102 0 : NamedArgExpr *nexpr = (NamedArgExpr *) node;
3103 : NamedArgExpr *newnode;
3104 :
3105 0 : FLATCOPY(newnode, nexpr, NamedArgExpr);
3106 0 : MUTATE(newnode->arg, nexpr->arg, Expr *);
3107 0 : return (Node *) newnode;
3108 : }
3109 : break;
3110 969162 : case T_OpExpr:
3111 : {
3112 969162 : OpExpr *expr = (OpExpr *) node;
3113 : OpExpr *newnode;
3114 :
3115 969162 : FLATCOPY(newnode, expr, OpExpr);
3116 969162 : MUTATE(newnode->args, expr->args, List *);
3117 969162 : return (Node *) newnode;
3118 : }
3119 : break;
3120 2092 : case T_DistinctExpr:
3121 : {
3122 2092 : DistinctExpr *expr = (DistinctExpr *) node;
3123 : DistinctExpr *newnode;
3124 :
3125 2092 : FLATCOPY(newnode, expr, DistinctExpr);
3126 2092 : MUTATE(newnode->args, expr->args, List *);
3127 2092 : return (Node *) newnode;
3128 : }
3129 : break;
3130 260 : case T_NullIfExpr:
3131 : {
3132 260 : NullIfExpr *expr = (NullIfExpr *) node;
3133 : NullIfExpr *newnode;
3134 :
3135 260 : FLATCOPY(newnode, expr, NullIfExpr);
3136 260 : MUTATE(newnode->args, expr->args, List *);
3137 260 : return (Node *) newnode;
3138 : }
3139 : break;
3140 73602 : case T_ScalarArrayOpExpr:
3141 : {
3142 73602 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3143 : ScalarArrayOpExpr *newnode;
3144 :
3145 73602 : FLATCOPY(newnode, expr, ScalarArrayOpExpr);
3146 73602 : MUTATE(newnode->args, expr->args, List *);
3147 73602 : return (Node *) newnode;
3148 : }
3149 : break;
3150 117696 : case T_BoolExpr:
3151 : {
3152 117696 : BoolExpr *expr = (BoolExpr *) node;
3153 : BoolExpr *newnode;
3154 :
3155 117696 : FLATCOPY(newnode, expr, BoolExpr);
3156 117696 : MUTATE(newnode->args, expr->args, List *);
3157 117690 : return (Node *) newnode;
3158 : }
3159 : break;
3160 44788 : case T_SubLink:
3161 : {
3162 44788 : SubLink *sublink = (SubLink *) node;
3163 : SubLink *newnode;
3164 :
3165 44788 : FLATCOPY(newnode, sublink, SubLink);
3166 44788 : MUTATE(newnode->testexpr, sublink->testexpr, Node *);
3167 :
3168 : /*
3169 : * Also invoke the mutator on the sublink's Query node, so it
3170 : * can recurse into the sub-query if it wants to.
3171 : */
3172 44788 : MUTATE(newnode->subselect, sublink->subselect, Node *);
3173 44788 : return (Node *) newnode;
3174 : }
3175 : break;
3176 15010 : case T_SubPlan:
3177 : {
3178 15010 : SubPlan *subplan = (SubPlan *) node;
3179 : SubPlan *newnode;
3180 :
3181 15010 : FLATCOPY(newnode, subplan, SubPlan);
3182 : /* transform testexpr */
3183 15010 : MUTATE(newnode->testexpr, subplan->testexpr, Node *);
3184 : /* transform args list (params to be passed to subplan) */
3185 15010 : MUTATE(newnode->args, subplan->args, List *);
3186 : /* but not the sub-Plan itself, which is referenced as-is */
3187 15010 : return (Node *) newnode;
3188 : }
3189 : break;
3190 144 : case T_AlternativeSubPlan:
3191 : {
3192 144 : AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
3193 : AlternativeSubPlan *newnode;
3194 :
3195 144 : FLATCOPY(newnode, asplan, AlternativeSubPlan);
3196 144 : MUTATE(newnode->subplans, asplan->subplans, List *);
3197 144 : return (Node *) newnode;
3198 : }
3199 : break;
3200 6590 : case T_FieldSelect:
3201 : {
3202 6590 : FieldSelect *fselect = (FieldSelect *) node;
3203 : FieldSelect *newnode;
3204 :
3205 6590 : FLATCOPY(newnode, fselect, FieldSelect);
3206 6590 : MUTATE(newnode->arg, fselect->arg, Expr *);
3207 6590 : return (Node *) newnode;
3208 : }
3209 : break;
3210 436 : case T_FieldStore:
3211 : {
3212 436 : FieldStore *fstore = (FieldStore *) node;
3213 : FieldStore *newnode;
3214 :
3215 436 : FLATCOPY(newnode, fstore, FieldStore);
3216 436 : MUTATE(newnode->arg, fstore->arg, Expr *);
3217 436 : MUTATE(newnode->newvals, fstore->newvals, List *);
3218 436 : newnode->fieldnums = list_copy(fstore->fieldnums);
3219 436 : return (Node *) newnode;
3220 : }
3221 : break;
3222 111992 : case T_RelabelType:
3223 : {
3224 111992 : RelabelType *relabel = (RelabelType *) node;
3225 : RelabelType *newnode;
3226 :
3227 111992 : FLATCOPY(newnode, relabel, RelabelType);
3228 111992 : MUTATE(newnode->arg, relabel->arg, Expr *);
3229 111992 : return (Node *) newnode;
3230 : }
3231 : break;
3232 25170 : case T_CoerceViaIO:
3233 : {
3234 25170 : CoerceViaIO *iocoerce = (CoerceViaIO *) node;
3235 : CoerceViaIO *newnode;
3236 :
3237 25170 : FLATCOPY(newnode, iocoerce, CoerceViaIO);
3238 25170 : MUTATE(newnode->arg, iocoerce->arg, Expr *);
3239 25170 : return (Node *) newnode;
3240 : }
3241 : break;
3242 10478 : case T_ArrayCoerceExpr:
3243 : {
3244 10478 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
3245 : ArrayCoerceExpr *newnode;
3246 :
3247 10478 : FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
3248 10478 : MUTATE(newnode->arg, acoerce->arg, Expr *);
3249 10478 : MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
3250 10478 : return (Node *) newnode;
3251 : }
3252 : break;
3253 154 : case T_ConvertRowtypeExpr:
3254 : {
3255 154 : ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
3256 : ConvertRowtypeExpr *newnode;
3257 :
3258 154 : FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
3259 154 : MUTATE(newnode->arg, convexpr->arg, Expr *);
3260 154 : return (Node *) newnode;
3261 : }
3262 : break;
3263 6064 : case T_CollateExpr:
3264 : {
3265 6064 : CollateExpr *collate = (CollateExpr *) node;
3266 : CollateExpr *newnode;
3267 :
3268 6064 : FLATCOPY(newnode, collate, CollateExpr);
3269 6064 : MUTATE(newnode->arg, collate->arg, Expr *);
3270 6064 : return (Node *) newnode;
3271 : }
3272 : break;
3273 63820 : case T_CaseExpr:
3274 : {
3275 63820 : CaseExpr *caseexpr = (CaseExpr *) node;
3276 : CaseExpr *newnode;
3277 :
3278 63820 : FLATCOPY(newnode, caseexpr, CaseExpr);
3279 63820 : MUTATE(newnode->arg, caseexpr->arg, Expr *);
3280 63820 : MUTATE(newnode->args, caseexpr->args, List *);
3281 63820 : MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
3282 63820 : return (Node *) newnode;
3283 : }
3284 : break;
3285 92802 : case T_CaseWhen:
3286 : {
3287 92802 : CaseWhen *casewhen = (CaseWhen *) node;
3288 : CaseWhen *newnode;
3289 :
3290 92802 : FLATCOPY(newnode, casewhen, CaseWhen);
3291 92802 : MUTATE(newnode->expr, casewhen->expr, Expr *);
3292 92802 : MUTATE(newnode->result, casewhen->result, Expr *);
3293 92802 : return (Node *) newnode;
3294 : }
3295 : break;
3296 40528 : case T_ArrayExpr:
3297 : {
3298 40528 : ArrayExpr *arrayexpr = (ArrayExpr *) node;
3299 : ArrayExpr *newnode;
3300 :
3301 40528 : FLATCOPY(newnode, arrayexpr, ArrayExpr);
3302 40528 : MUTATE(newnode->elements, arrayexpr->elements, List *);
3303 40528 : return (Node *) newnode;
3304 : }
3305 : break;
3306 8058 : case T_RowExpr:
3307 : {
3308 8058 : RowExpr *rowexpr = (RowExpr *) node;
3309 : RowExpr *newnode;
3310 :
3311 8058 : FLATCOPY(newnode, rowexpr, RowExpr);
3312 8058 : MUTATE(newnode->args, rowexpr->args, List *);
3313 : /* Assume colnames needn't be duplicated */
3314 8058 : return (Node *) newnode;
3315 : }
3316 : break;
3317 312 : case T_RowCompareExpr:
3318 : {
3319 312 : RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3320 : RowCompareExpr *newnode;
3321 :
3322 312 : FLATCOPY(newnode, rcexpr, RowCompareExpr);
3323 312 : MUTATE(newnode->largs, rcexpr->largs, List *);
3324 312 : MUTATE(newnode->rargs, rcexpr->rargs, List *);
3325 312 : return (Node *) newnode;
3326 : }
3327 : break;
3328 9776 : case T_CoalesceExpr:
3329 : {
3330 9776 : CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3331 : CoalesceExpr *newnode;
3332 :
3333 9776 : FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
3334 9776 : MUTATE(newnode->args, coalesceexpr->args, List *);
3335 9776 : return (Node *) newnode;
3336 : }
3337 : break;
3338 1148 : case T_MinMaxExpr:
3339 : {
3340 1148 : MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3341 : MinMaxExpr *newnode;
3342 :
3343 1148 : FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
3344 1148 : MUTATE(newnode->args, minmaxexpr->args, List *);
3345 1148 : return (Node *) newnode;
3346 : }
3347 : break;
3348 798 : case T_XmlExpr:
3349 : {
3350 798 : XmlExpr *xexpr = (XmlExpr *) node;
3351 : XmlExpr *newnode;
3352 :
3353 798 : FLATCOPY(newnode, xexpr, XmlExpr);
3354 798 : MUTATE(newnode->named_args, xexpr->named_args, List *);
3355 : /* assume mutator does not care about arg_names */
3356 798 : MUTATE(newnode->args, xexpr->args, List *);
3357 798 : return (Node *) newnode;
3358 : }
3359 : break;
3360 1750 : case T_JsonReturning:
3361 : {
3362 1750 : JsonReturning *jr = (JsonReturning *) node;
3363 : JsonReturning *newnode;
3364 :
3365 1750 : FLATCOPY(newnode, jr, JsonReturning);
3366 1750 : MUTATE(newnode->format, jr->format, JsonFormat *);
3367 :
3368 1750 : return (Node *) newnode;
3369 : }
3370 258 : case T_JsonValueExpr:
3371 : {
3372 258 : JsonValueExpr *jve = (JsonValueExpr *) node;
3373 : JsonValueExpr *newnode;
3374 :
3375 258 : FLATCOPY(newnode, jve, JsonValueExpr);
3376 258 : MUTATE(newnode->raw_expr, jve->raw_expr, Expr *);
3377 258 : MUTATE(newnode->formatted_expr, jve->formatted_expr, Expr *);
3378 258 : MUTATE(newnode->format, jve->format, JsonFormat *);
3379 :
3380 258 : return (Node *) newnode;
3381 : }
3382 1750 : case T_JsonConstructorExpr:
3383 : {
3384 1750 : JsonConstructorExpr *jce = (JsonConstructorExpr *) node;
3385 : JsonConstructorExpr *newnode;
3386 :
3387 1750 : FLATCOPY(newnode, jce, JsonConstructorExpr);
3388 1750 : MUTATE(newnode->args, jce->args, List *);
3389 1750 : MUTATE(newnode->func, jce->func, Expr *);
3390 1750 : MUTATE(newnode->coercion, jce->coercion, Expr *);
3391 1750 : MUTATE(newnode->returning, jce->returning, JsonReturning *);
3392 :
3393 1750 : return (Node *) newnode;
3394 : }
3395 532 : case T_JsonIsPredicate:
3396 : {
3397 532 : JsonIsPredicate *pred = (JsonIsPredicate *) node;
3398 : JsonIsPredicate *newnode;
3399 :
3400 532 : FLATCOPY(newnode, pred, JsonIsPredicate);
3401 532 : MUTATE(newnode->expr, pred->expr, Node *);
3402 532 : MUTATE(newnode->format, pred->format, JsonFormat *);
3403 :
3404 532 : return (Node *) newnode;
3405 : }
3406 3568 : case T_JsonExpr:
3407 : {
3408 3568 : JsonExpr *jexpr = (JsonExpr *) node;
3409 : JsonExpr *newnode;
3410 :
3411 3568 : FLATCOPY(newnode, jexpr, JsonExpr);
3412 3568 : MUTATE(newnode->formatted_expr, jexpr->formatted_expr, Node *);
3413 3568 : MUTATE(newnode->path_spec, jexpr->path_spec, Node *);
3414 3562 : MUTATE(newnode->coercion_expr, jexpr->coercion_expr, Node *);
3415 3562 : MUTATE(newnode->passing_values, jexpr->passing_values, List *);
3416 : /* assume mutator does not care about passing_names */
3417 3562 : MUTATE(newnode->on_empty, jexpr->on_empty, JsonBehavior *);
3418 3562 : MUTATE(newnode->on_error, jexpr->on_error, JsonBehavior *);
3419 3562 : return (Node *) newnode;
3420 : }
3421 : break;
3422 3850 : case T_JsonBehavior:
3423 : {
3424 3850 : JsonBehavior *behavior = (JsonBehavior *) node;
3425 : JsonBehavior *newnode;
3426 :
3427 3850 : FLATCOPY(newnode, behavior, JsonBehavior);
3428 3850 : MUTATE(newnode->expr, behavior->expr, Node *);
3429 3850 : return (Node *) newnode;
3430 : }
3431 : break;
3432 42448 : case T_NullTest:
3433 : {
3434 42448 : NullTest *ntest = (NullTest *) node;
3435 : NullTest *newnode;
3436 :
3437 42448 : FLATCOPY(newnode, ntest, NullTest);
3438 42448 : MUTATE(newnode->arg, ntest->arg, Expr *);
3439 42448 : return (Node *) newnode;
3440 : }
3441 : break;
3442 1964 : case T_BooleanTest:
3443 : {
3444 1964 : BooleanTest *btest = (BooleanTest *) node;
3445 : BooleanTest *newnode;
3446 :
3447 1964 : FLATCOPY(newnode, btest, BooleanTest);
3448 1964 : MUTATE(newnode->arg, btest->arg, Expr *);
3449 1964 : return (Node *) newnode;
3450 : }
3451 : break;
3452 15968 : case T_CoerceToDomain:
3453 : {
3454 15968 : CoerceToDomain *ctest = (CoerceToDomain *) node;
3455 : CoerceToDomain *newnode;
3456 :
3457 15968 : FLATCOPY(newnode, ctest, CoerceToDomain);
3458 15968 : MUTATE(newnode->arg, ctest->arg, Expr *);
3459 15968 : return (Node *) newnode;
3460 : }
3461 : break;
3462 3234046 : case T_TargetEntry:
3463 : {
3464 3234046 : TargetEntry *targetentry = (TargetEntry *) node;
3465 : TargetEntry *newnode;
3466 :
3467 3234046 : FLATCOPY(newnode, targetentry, TargetEntry);
3468 3234046 : MUTATE(newnode->expr, targetentry->expr, Expr *);
3469 3230498 : return (Node *) newnode;
3470 : }
3471 : break;
3472 32304 : case T_Query:
3473 : /* Do nothing with a sub-Query, per discussion above */
3474 32304 : return node;
3475 0 : case T_WindowClause:
3476 : {
3477 0 : WindowClause *wc = (WindowClause *) node;
3478 : WindowClause *newnode;
3479 :
3480 0 : FLATCOPY(newnode, wc, WindowClause);
3481 0 : MUTATE(newnode->partitionClause, wc->partitionClause, List *);
3482 0 : MUTATE(newnode->orderClause, wc->orderClause, List *);
3483 0 : MUTATE(newnode->startOffset, wc->startOffset, Node *);
3484 0 : MUTATE(newnode->endOffset, wc->endOffset, Node *);
3485 0 : return (Node *) newnode;
3486 : }
3487 : break;
3488 0 : case T_CTECycleClause:
3489 : {
3490 0 : CTECycleClause *cc = (CTECycleClause *) node;
3491 : CTECycleClause *newnode;
3492 :
3493 0 : FLATCOPY(newnode, cc, CTECycleClause);
3494 0 : MUTATE(newnode->cycle_mark_value, cc->cycle_mark_value, Node *);
3495 0 : MUTATE(newnode->cycle_mark_default, cc->cycle_mark_default, Node *);
3496 0 : return (Node *) newnode;
3497 : }
3498 : break;
3499 100 : case T_CommonTableExpr:
3500 : {
3501 100 : CommonTableExpr *cte = (CommonTableExpr *) node;
3502 : CommonTableExpr *newnode;
3503 :
3504 100 : FLATCOPY(newnode, cte, CommonTableExpr);
3505 :
3506 : /*
3507 : * Also invoke the mutator on the CTE's Query node, so it can
3508 : * recurse into the sub-query if it wants to.
3509 : */
3510 100 : MUTATE(newnode->ctequery, cte->ctequery, Node *);
3511 :
3512 100 : MUTATE(newnode->search_clause, cte->search_clause, CTESearchClause *);
3513 100 : MUTATE(newnode->cycle_clause, cte->cycle_clause, CTECycleClause *);
3514 :
3515 100 : return (Node *) newnode;
3516 : }
3517 : break;
3518 0 : case T_PartitionBoundSpec:
3519 : {
3520 0 : PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
3521 : PartitionBoundSpec *newnode;
3522 :
3523 0 : FLATCOPY(newnode, pbs, PartitionBoundSpec);
3524 0 : MUTATE(newnode->listdatums, pbs->listdatums, List *);
3525 0 : MUTATE(newnode->lowerdatums, pbs->lowerdatums, List *);
3526 0 : MUTATE(newnode->upperdatums, pbs->upperdatums, List *);
3527 0 : return (Node *) newnode;
3528 : }
3529 : break;
3530 0 : case T_PartitionRangeDatum:
3531 : {
3532 0 : PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
3533 : PartitionRangeDatum *newnode;
3534 :
3535 0 : FLATCOPY(newnode, prd, PartitionRangeDatum);
3536 0 : MUTATE(newnode->value, prd->value, Node *);
3537 0 : return (Node *) newnode;
3538 : }
3539 : break;
3540 4239390 : case T_List:
3541 : {
3542 : /*
3543 : * We assume the mutator isn't interested in the list nodes
3544 : * per se, so just invoke it on each list element. NOTE: this
3545 : * would fail badly on a list with integer elements!
3546 : */
3547 : List *resultlist;
3548 : ListCell *temp;
3549 :
3550 4239390 : resultlist = NIL;
3551 13466450 : foreach(temp, (List *) node)
3552 : {
3553 9227060 : resultlist = lappend(resultlist,
3554 9230710 : mutator((Node *) lfirst(temp),
3555 : context));
3556 : }
3557 4235740 : return (Node *) resultlist;
3558 : }
3559 : break;
3560 21738 : case T_FromExpr:
3561 : {
3562 21738 : FromExpr *from = (FromExpr *) node;
3563 : FromExpr *newnode;
3564 :
3565 21738 : FLATCOPY(newnode, from, FromExpr);
3566 21738 : MUTATE(newnode->fromlist, from->fromlist, List *);
3567 21738 : MUTATE(newnode->quals, from->quals, Node *);
3568 21738 : return (Node *) newnode;
3569 : }
3570 : break;
3571 348 : case T_OnConflictExpr:
3572 : {
3573 348 : OnConflictExpr *oc = (OnConflictExpr *) node;
3574 : OnConflictExpr *newnode;
3575 :
3576 348 : FLATCOPY(newnode, oc, OnConflictExpr);
3577 348 : MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
3578 348 : MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
3579 348 : MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
3580 348 : MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
3581 348 : MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
3582 :
3583 348 : return (Node *) newnode;
3584 : }
3585 : break;
3586 972 : case T_MergeAction:
3587 : {
3588 972 : MergeAction *action = (MergeAction *) node;
3589 : MergeAction *newnode;
3590 :
3591 972 : FLATCOPY(newnode, action, MergeAction);
3592 972 : MUTATE(newnode->qual, action->qual, Node *);
3593 972 : MUTATE(newnode->targetList, action->targetList, List *);
3594 :
3595 972 : return (Node *) newnode;
3596 : }
3597 : break;
3598 0 : case T_PartitionPruneStepOp:
3599 : {
3600 0 : PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
3601 : PartitionPruneStepOp *newnode;
3602 :
3603 0 : FLATCOPY(newnode, opstep, PartitionPruneStepOp);
3604 0 : MUTATE(newnode->exprs, opstep->exprs, List *);
3605 :
3606 0 : return (Node *) newnode;
3607 : }
3608 : break;
3609 0 : case T_PartitionPruneStepCombine:
3610 : /* no expression sub-nodes */
3611 0 : return (Node *) copyObject(node);
3612 3398 : case T_JoinExpr:
3613 : {
3614 3398 : JoinExpr *join = (JoinExpr *) node;
3615 : JoinExpr *newnode;
3616 :
3617 3398 : FLATCOPY(newnode, join, JoinExpr);
3618 3398 : MUTATE(newnode->larg, join->larg, Node *);
3619 3398 : MUTATE(newnode->rarg, join->rarg, Node *);
3620 3398 : MUTATE(newnode->quals, join->quals, Node *);
3621 : /* We do not mutate alias or using by default */
3622 3398 : return (Node *) newnode;
3623 : }
3624 : break;
3625 156 : case T_SetOperationStmt:
3626 : {
3627 156 : SetOperationStmt *setop = (SetOperationStmt *) node;
3628 : SetOperationStmt *newnode;
3629 :
3630 156 : FLATCOPY(newnode, setop, SetOperationStmt);
3631 156 : MUTATE(newnode->larg, setop->larg, Node *);
3632 156 : MUTATE(newnode->rarg, setop->rarg, Node *);
3633 : /* We do not mutate groupClauses by default */
3634 156 : return (Node *) newnode;
3635 : }
3636 : break;
3637 444 : case T_IndexClause:
3638 : {
3639 444 : IndexClause *iclause = (IndexClause *) node;
3640 : IndexClause *newnode;
3641 :
3642 444 : FLATCOPY(newnode, iclause, IndexClause);
3643 444 : MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
3644 444 : MUTATE(newnode->indexquals, iclause->indexquals, List *);
3645 444 : return (Node *) newnode;
3646 : }
3647 : break;
3648 7568 : case T_PlaceHolderVar:
3649 : {
3650 7568 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
3651 : PlaceHolderVar *newnode;
3652 :
3653 7568 : FLATCOPY(newnode, phv, PlaceHolderVar);
3654 7568 : MUTATE(newnode->phexpr, phv->phexpr, Expr *);
3655 : /* Assume we need not copy the relids bitmapsets */
3656 7568 : return (Node *) newnode;
3657 : }
3658 : break;
3659 2084 : case T_InferenceElem:
3660 : {
3661 2084 : InferenceElem *inferenceelemdexpr = (InferenceElem *) node;
3662 : InferenceElem *newnode;
3663 :
3664 2084 : FLATCOPY(newnode, inferenceelemdexpr, InferenceElem);
3665 2084 : MUTATE(newnode->expr, newnode->expr, Node *);
3666 2084 : return (Node *) newnode;
3667 : }
3668 : break;
3669 7082 : case T_AppendRelInfo:
3670 : {
3671 7082 : AppendRelInfo *appinfo = (AppendRelInfo *) node;
3672 : AppendRelInfo *newnode;
3673 :
3674 7082 : FLATCOPY(newnode, appinfo, AppendRelInfo);
3675 7082 : MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
3676 : /* Assume nothing need be done with parent_colnos[] */
3677 7082 : return (Node *) newnode;
3678 : }
3679 : break;
3680 0 : case T_PlaceHolderInfo:
3681 : {
3682 0 : PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
3683 : PlaceHolderInfo *newnode;
3684 :
3685 0 : FLATCOPY(newnode, phinfo, PlaceHolderInfo);
3686 0 : MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
3687 : /* Assume we need not copy the relids bitmapsets */
3688 0 : return (Node *) newnode;
3689 : }
3690 : break;
3691 82006 : case T_RangeTblFunction:
3692 : {
3693 82006 : RangeTblFunction *rtfunc = (RangeTblFunction *) node;
3694 : RangeTblFunction *newnode;
3695 :
3696 82006 : FLATCOPY(newnode, rtfunc, RangeTblFunction);
3697 82006 : MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
3698 : /* Assume we need not copy the coldef info lists */
3699 82006 : return (Node *) newnode;
3700 : }
3701 : break;
3702 466 : case T_TableSampleClause:
3703 : {
3704 466 : TableSampleClause *tsc = (TableSampleClause *) node;
3705 : TableSampleClause *newnode;
3706 :
3707 466 : FLATCOPY(newnode, tsc, TableSampleClause);
3708 466 : MUTATE(newnode->args, tsc->args, List *);
3709 466 : MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
3710 466 : return (Node *) newnode;
3711 : }
3712 : break;
3713 950 : case T_TableFunc:
3714 : {
3715 950 : TableFunc *tf = (TableFunc *) node;
3716 : TableFunc *newnode;
3717 :
3718 950 : FLATCOPY(newnode, tf, TableFunc);
3719 950 : MUTATE(newnode->ns_uris, tf->ns_uris, List *);
3720 950 : MUTATE(newnode->docexpr, tf->docexpr, Node *);
3721 950 : MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
3722 950 : MUTATE(newnode->colexprs, tf->colexprs, List *);
3723 950 : MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
3724 950 : MUTATE(newnode->colvalexprs, tf->colvalexprs, List *);
3725 950 : MUTATE(newnode->passingvalexprs, tf->passingvalexprs, List *);
3726 950 : return (Node *) newnode;
3727 : }
3728 : break;
3729 0 : default:
3730 0 : elog(ERROR, "unrecognized node type: %d",
3731 : (int) nodeTag(node));
3732 : break;
3733 : }
3734 : /* can't get here, but keep compiler happy */
3735 : return NULL;
3736 : }
3737 :
3738 :
3739 : /*
3740 : * query_tree_mutator --- initiate modification of a Query's expressions
3741 : *
3742 : * This routine exists just to reduce the number of places that need to know
3743 : * where all the expression subtrees of a Query are. Note it can be used
3744 : * for starting a walk at top level of a Query regardless of whether the
3745 : * mutator intends to descend into subqueries. It is also useful for
3746 : * descending into subqueries within a mutator.
3747 : *
3748 : * Some callers want to suppress mutating of certain items in the Query,
3749 : * typically because they need to process them specially, or don't actually
3750 : * want to recurse into subqueries. This is supported by the flags argument,
3751 : * which is the bitwise OR of flag values to suppress mutating of
3752 : * indicated items. (More flag bits may be added as needed.)
3753 : *
3754 : * Normally the top-level Query node itself is copied, but some callers want
3755 : * it to be modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.
3756 : * All modified substructure is safely copied in any case.
3757 : */
3758 : Query *
3759 21498 : query_tree_mutator_impl(Query *query,
3760 : tree_mutator_callback mutator,
3761 : void *context,
3762 : int flags)
3763 : {
3764 : Assert(query != NULL && IsA(query, Query));
3765 :
3766 21498 : if (!(flags & QTW_DONT_COPY_QUERY))
3767 : {
3768 : Query *newquery;
3769 :
3770 21498 : FLATCOPY(newquery, query, Query);
3771 21498 : query = newquery;
3772 : }
3773 :
3774 21498 : MUTATE(query->targetList, query->targetList, List *);
3775 21498 : MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
3776 21498 : MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
3777 21498 : MUTATE(query->mergeActionList, query->mergeActionList, List *);
3778 21498 : MUTATE(query->mergeJoinCondition, query->mergeJoinCondition, Node *);
3779 21498 : MUTATE(query->returningList, query->returningList, List *);
3780 21498 : MUTATE(query->jointree, query->jointree, FromExpr *);
3781 21498 : MUTATE(query->setOperations, query->setOperations, Node *);
3782 21498 : MUTATE(query->havingQual, query->havingQual, Node *);
3783 21498 : MUTATE(query->limitOffset, query->limitOffset, Node *);
3784 21498 : MUTATE(query->limitCount, query->limitCount, Node *);
3785 :
3786 : /*
3787 : * Most callers aren't interested in SortGroupClause nodes since those
3788 : * don't contain actual expressions. However they do contain OIDs, which
3789 : * may be of interest to some mutators.
3790 : */
3791 :
3792 21498 : if ((flags & QTW_EXAMINE_SORTGROUP))
3793 : {
3794 0 : MUTATE(query->groupClause, query->groupClause, List *);
3795 0 : MUTATE(query->windowClause, query->windowClause, List *);
3796 0 : MUTATE(query->sortClause, query->sortClause, List *);
3797 0 : MUTATE(query->distinctClause, query->distinctClause, List *);
3798 : }
3799 : else
3800 : {
3801 : /*
3802 : * But we need to mutate the expressions under WindowClause nodes even
3803 : * if we're not interested in SortGroupClause nodes.
3804 : */
3805 : List *resultlist;
3806 : ListCell *temp;
3807 :
3808 21498 : resultlist = NIL;
3809 21522 : foreach(temp, query->windowClause)
3810 : {
3811 24 : WindowClause *wc = lfirst_node(WindowClause, temp);
3812 : WindowClause *newnode;
3813 :
3814 24 : FLATCOPY(newnode, wc, WindowClause);
3815 24 : MUTATE(newnode->startOffset, wc->startOffset, Node *);
3816 24 : MUTATE(newnode->endOffset, wc->endOffset, Node *);
3817 :
3818 24 : resultlist = lappend(resultlist, (Node *) newnode);
3819 : }
3820 21498 : query->windowClause = resultlist;
3821 : }
3822 :
3823 : /*
3824 : * groupingSets and rowMarks are not mutated:
3825 : *
3826 : * groupingSets contain only ressortgroup refs (integers) which are
3827 : * meaningless without the groupClause or tlist. Accordingly, any mutator
3828 : * that needs to care about them needs to handle them itself in its Query
3829 : * processing.
3830 : *
3831 : * rowMarks contains only rangetable indexes (and flags etc.) and
3832 : * therefore should be handled at Query level similarly.
3833 : */
3834 :
3835 21498 : if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
3836 21498 : MUTATE(query->cteList, query->cteList, List *);
3837 : else /* else copy CTE list as-is */
3838 0 : query->cteList = copyObject(query->cteList);
3839 21498 : query->rtable = range_table_mutator(query->rtable,
3840 : mutator, context, flags);
3841 21498 : return query;
3842 : }
3843 :
3844 : /*
3845 : * range_table_mutator is just the part of query_tree_mutator that processes
3846 : * a query's rangetable. This is split out since it can be useful on
3847 : * its own.
3848 : */
3849 : List *
3850 21498 : range_table_mutator_impl(List *rtable,
3851 : tree_mutator_callback mutator,
3852 : void *context,
3853 : int flags)
3854 : {
3855 21498 : List *newrt = NIL;
3856 : ListCell *rt;
3857 :
3858 62044 : foreach(rt, rtable)
3859 : {
3860 40546 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3861 : RangeTblEntry *newrte;
3862 :
3863 40546 : FLATCOPY(newrte, rte, RangeTblEntry);
3864 40546 : switch (rte->rtekind)
3865 : {
3866 26916 : case RTE_RELATION:
3867 26916 : MUTATE(newrte->tablesample, rte->tablesample,
3868 : TableSampleClause *);
3869 : /* we don't bother to copy eref, aliases, etc; OK? */
3870 26916 : break;
3871 3100 : case RTE_SUBQUERY:
3872 3100 : if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3873 3100 : MUTATE(newrte->subquery, rte->subquery, Query *);
3874 : else
3875 : {
3876 : /* else, copy RT subqueries as-is */
3877 0 : newrte->subquery = copyObject(rte->subquery);
3878 : }
3879 3100 : break;
3880 3408 : case RTE_JOIN:
3881 3408 : if (!(flags & QTW_IGNORE_JOINALIASES))
3882 3026 : MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
3883 : else
3884 : {
3885 : /* else, copy join aliases as-is */
3886 382 : newrte->joinaliasvars = copyObject(rte->joinaliasvars);
3887 : }
3888 3408 : break;
3889 5570 : case RTE_FUNCTION:
3890 5570 : MUTATE(newrte->functions, rte->functions, List *);
3891 5570 : break;
3892 0 : case RTE_TABLEFUNC:
3893 0 : MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
3894 0 : break;
3895 1176 : case RTE_VALUES:
3896 1176 : MUTATE(newrte->values_lists, rte->values_lists, List *);
3897 1176 : break;
3898 376 : case RTE_CTE:
3899 : case RTE_NAMEDTUPLESTORE:
3900 : case RTE_RESULT:
3901 : /* nothing to do */
3902 376 : break;
3903 : }
3904 40546 : MUTATE(newrte->securityQuals, rte->securityQuals, List *);
3905 40546 : newrt = lappend(newrt, newrte);
3906 : }
3907 21498 : return newrt;
3908 : }
3909 :
3910 : /*
3911 : * query_or_expression_tree_walker --- hybrid form
3912 : *
3913 : * This routine will invoke query_tree_walker if called on a Query node,
3914 : * else will invoke the walker directly. This is a useful way of starting
3915 : * the recursion when the walker's normal change of state is not appropriate
3916 : * for the outermost Query node.
3917 : */
3918 : bool
3919 3382600 : query_or_expression_tree_walker_impl(Node *node,
3920 : tree_walker_callback walker,
3921 : void *context,
3922 : int flags)
3923 : {
3924 3382600 : if (node && IsA(node, Query))
3925 376814 : return query_tree_walker((Query *) node,
3926 : walker,
3927 : context,
3928 : flags);
3929 : else
3930 3005786 : return WALK(node);
3931 : }
3932 :
3933 : /*
3934 : * query_or_expression_tree_mutator --- hybrid form
3935 : *
3936 : * This routine will invoke query_tree_mutator if called on a Query node,
3937 : * else will invoke the mutator directly. This is a useful way of starting
3938 : * the recursion when the mutator's normal change of state is not appropriate
3939 : * for the outermost Query node.
3940 : */
3941 : Node *
3942 190540 : query_or_expression_tree_mutator_impl(Node *node,
3943 : tree_mutator_callback mutator,
3944 : void *context,
3945 : int flags)
3946 : {
3947 190540 : if (node && IsA(node, Query))
3948 6232 : return (Node *) query_tree_mutator((Query *) node,
3949 : mutator,
3950 : context,
3951 : flags);
3952 : else
3953 184308 : return mutator(node, context);
3954 : }
3955 :
3956 :
3957 : /*
3958 : * raw_expression_tree_walker --- walk raw parse trees
3959 : *
3960 : * This has exactly the same API as expression_tree_walker, but instead of
3961 : * walking post-analysis parse trees, it knows how to walk the node types
3962 : * found in raw grammar output. (There is not currently any need for a
3963 : * combined walker, so we keep them separate in the name of efficiency.)
3964 : * Unlike expression_tree_walker, there is no special rule about query
3965 : * boundaries: we descend to everything that's possibly interesting.
3966 : *
3967 : * Currently, the node type coverage here extends only to DML statements
3968 : * (SELECT/INSERT/UPDATE/DELETE/MERGE) and nodes that can appear in them,
3969 : * because this is used mainly during analysis of CTEs, and only DML
3970 : * statements can appear in CTEs.
3971 : */
3972 : bool
3973 10443262 : raw_expression_tree_walker_impl(Node *node,
3974 : tree_walker_callback walker,
3975 : void *context)
3976 : {
3977 : ListCell *temp;
3978 :
3979 : /*
3980 : * The walker has already visited the current node, and so we need only
3981 : * recurse into any sub-nodes it has.
3982 : */
3983 10443262 : if (node == NULL)
3984 0 : return false;
3985 :
3986 : /* Guard against stack overflow due to overly complex expressions */
3987 10443262 : check_stack_depth();
3988 :
3989 10443256 : switch (nodeTag(node))
3990 : {
3991 1407566 : case T_JsonFormat:
3992 : case T_SetToDefault:
3993 : case T_CurrentOfExpr:
3994 : case T_SQLValueFunction:
3995 : case T_Integer:
3996 : case T_Float:
3997 : case T_Boolean:
3998 : case T_String:
3999 : case T_BitString:
4000 : case T_ParamRef:
4001 : case T_A_Const:
4002 : case T_A_Star:
4003 : case T_MergeSupportFunc:
4004 : /* primitive node types with no subnodes */
4005 1407566 : break;
4006 325424 : case T_Alias:
4007 : /* we assume the colnames list isn't interesting */
4008 325424 : break;
4009 541688 : case T_RangeVar:
4010 541688 : return WALK(((RangeVar *) node)->alias);
4011 424 : case T_GroupingFunc:
4012 424 : return WALK(((GroupingFunc *) node)->args);
4013 41724 : case T_SubLink:
4014 : {
4015 41724 : SubLink *sublink = (SubLink *) node;
4016 :
4017 41724 : if (WALK(sublink->testexpr))
4018 0 : return true;
4019 : /* we assume the operName is not interesting */
4020 41724 : if (WALK(sublink->subselect))
4021 0 : return true;
4022 : }
4023 41724 : break;
4024 57452 : case T_CaseExpr:
4025 : {
4026 57452 : CaseExpr *caseexpr = (CaseExpr *) node;
4027 :
4028 57452 : if (WALK(caseexpr->arg))
4029 0 : return true;
4030 : /* we assume walker doesn't care about CaseWhens, either */
4031 156558 : foreach(temp, caseexpr->args)
4032 : {
4033 99106 : CaseWhen *when = lfirst_node(CaseWhen, temp);
4034 :
4035 99106 : if (WALK(when->expr))
4036 0 : return true;
4037 99106 : if (WALK(when->result))
4038 0 : return true;
4039 : }
4040 57452 : if (WALK(caseexpr->defresult))
4041 0 : return true;
4042 : }
4043 57452 : break;
4044 6998 : case T_RowExpr:
4045 : /* Assume colnames isn't interesting */
4046 6998 : return WALK(((RowExpr *) node)->args);
4047 5144 : case T_CoalesceExpr:
4048 5144 : return WALK(((CoalesceExpr *) node)->args);
4049 372 : case T_MinMaxExpr:
4050 372 : return WALK(((MinMaxExpr *) node)->args);
4051 620 : case T_XmlExpr:
4052 : {
4053 620 : XmlExpr *xexpr = (XmlExpr *) node;
4054 :
4055 620 : if (WALK(xexpr->named_args))
4056 0 : return true;
4057 : /* we assume walker doesn't care about arg_names */
4058 620 : if (WALK(xexpr->args))
4059 0 : return true;
4060 : }
4061 620 : break;
4062 1258 : case T_JsonReturning:
4063 1258 : return WALK(((JsonReturning *) node)->format);
4064 3404 : case T_JsonValueExpr:
4065 : {
4066 3404 : JsonValueExpr *jve = (JsonValueExpr *) node;
4067 :
4068 3404 : if (WALK(jve->raw_expr))
4069 0 : return true;
4070 3404 : if (WALK(jve->formatted_expr))
4071 0 : return true;
4072 3404 : if (WALK(jve->format))
4073 0 : return true;
4074 : }
4075 3404 : break;
4076 164 : case T_JsonParseExpr:
4077 : {
4078 164 : JsonParseExpr *jpe = (JsonParseExpr *) node;
4079 :
4080 164 : if (WALK(jpe->expr))
4081 0 : return true;
4082 164 : if (WALK(jpe->output))
4083 0 : return true;
4084 : }
4085 164 : break;
4086 112 : case T_JsonScalarExpr:
4087 : {
4088 112 : JsonScalarExpr *jse = (JsonScalarExpr *) node;
4089 :
4090 112 : if (WALK(jse->expr))
4091 0 : return true;
4092 112 : if (WALK(jse->output))
4093 0 : return true;
4094 : }
4095 112 : break;
4096 90 : case T_JsonSerializeExpr:
4097 : {
4098 90 : JsonSerializeExpr *jse = (JsonSerializeExpr *) node;
4099 :
4100 90 : if (WALK(jse->expr))
4101 0 : return true;
4102 90 : if (WALK(jse->output))
4103 0 : return true;
4104 : }
4105 90 : break;
4106 0 : case T_JsonConstructorExpr:
4107 : {
4108 0 : JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
4109 :
4110 0 : if (WALK(ctor->args))
4111 0 : return true;
4112 0 : if (WALK(ctor->func))
4113 0 : return true;
4114 0 : if (WALK(ctor->coercion))
4115 0 : return true;
4116 0 : if (WALK(ctor->returning))
4117 0 : return true;
4118 : }
4119 0 : break;
4120 362 : case T_JsonIsPredicate:
4121 362 : return WALK(((JsonIsPredicate *) node)->expr);
4122 342 : case T_JsonArgument:
4123 342 : return WALK(((JsonArgument *) node)->val);
4124 1296 : case T_JsonFuncExpr:
4125 : {
4126 1296 : JsonFuncExpr *jfe = (JsonFuncExpr *) node;
4127 :
4128 1296 : if (WALK(jfe->context_item))
4129 0 : return true;
4130 1296 : if (WALK(jfe->pathspec))
4131 0 : return true;
4132 1296 : if (WALK(jfe->passing))
4133 0 : return true;
4134 1296 : if (WALK(jfe->output))
4135 0 : return true;
4136 1296 : if (WALK(jfe->on_empty))
4137 0 : return true;
4138 1296 : if (WALK(jfe->on_error))
4139 0 : return true;
4140 : }
4141 1296 : break;
4142 816 : case T_JsonBehavior:
4143 : {
4144 816 : JsonBehavior *jb = (JsonBehavior *) node;
4145 :
4146 816 : if (WALK(jb->expr))
4147 0 : return true;
4148 : }
4149 816 : break;
4150 428 : case T_JsonTable:
4151 : {
4152 428 : JsonTable *jt = (JsonTable *) node;
4153 :
4154 428 : if (WALK(jt->context_item))
4155 0 : return true;
4156 428 : if (WALK(jt->pathspec))
4157 0 : return true;
4158 428 : if (WALK(jt->passing))
4159 0 : return true;
4160 428 : if (WALK(jt->columns))
4161 0 : return true;
4162 428 : if (WALK(jt->on_error))
4163 0 : return true;
4164 : }
4165 428 : break;
4166 1246 : case T_JsonTableColumn:
4167 : {
4168 1246 : JsonTableColumn *jtc = (JsonTableColumn *) node;
4169 :
4170 1246 : if (WALK(jtc->typeName))
4171 0 : return true;
4172 1246 : if (WALK(jtc->on_empty))
4173 0 : return true;
4174 1246 : if (WALK(jtc->on_error))
4175 0 : return true;
4176 1246 : if (WALK(jtc->columns))
4177 0 : return true;
4178 : }
4179 1246 : break;
4180 428 : case T_JsonTablePathSpec:
4181 428 : return WALK(((JsonTablePathSpec *) node)->string);
4182 21702 : case T_NullTest:
4183 21702 : return WALK(((NullTest *) node)->arg);
4184 790 : case T_BooleanTest:
4185 790 : return WALK(((BooleanTest *) node)->arg);
4186 96546 : case T_JoinExpr:
4187 : {
4188 96546 : JoinExpr *join = (JoinExpr *) node;
4189 :
4190 96546 : if (WALK(join->larg))
4191 0 : return true;
4192 96546 : if (WALK(join->rarg))
4193 0 : return true;
4194 96546 : if (WALK(join->quals))
4195 0 : return true;
4196 96546 : if (WALK(join->alias))
4197 0 : return true;
4198 : /* using list is deemed uninteresting */
4199 : }
4200 96546 : break;
4201 30 : case T_IntoClause:
4202 : {
4203 30 : IntoClause *into = (IntoClause *) node;
4204 :
4205 30 : if (WALK(into->rel))
4206 0 : return true;
4207 : /* colNames, options are deemed uninteresting */
4208 : /* viewQuery should be null in raw parsetree, but check it */
4209 30 : if (WALK(into->viewQuery))
4210 0 : return true;
4211 : }
4212 30 : break;
4213 1918790 : case T_List:
4214 5373214 : foreach(temp, (List *) node)
4215 : {
4216 3454514 : if (WALK((Node *) lfirst(temp)))
4217 0 : return true;
4218 : }
4219 1918700 : break;
4220 72444 : case T_InsertStmt:
4221 : {
4222 72444 : InsertStmt *stmt = (InsertStmt *) node;
4223 :
4224 72444 : if (WALK(stmt->relation))
4225 0 : return true;
4226 72444 : if (WALK(stmt->cols))
4227 0 : return true;
4228 72444 : if (WALK(stmt->selectStmt))
4229 0 : return true;
4230 72444 : if (WALK(stmt->onConflictClause))
4231 0 : return true;
4232 72444 : if (WALK(stmt->returningList))
4233 0 : return true;
4234 72444 : if (WALK(stmt->withClause))
4235 0 : return true;
4236 : }
4237 72444 : break;
4238 4416 : case T_DeleteStmt:
4239 : {
4240 4416 : DeleteStmt *stmt = (DeleteStmt *) node;
4241 :
4242 4416 : if (WALK(stmt->relation))
4243 0 : return true;
4244 4416 : if (WALK(stmt->usingClause))
4245 0 : return true;
4246 4416 : if (WALK(stmt->whereClause))
4247 0 : return true;
4248 4416 : if (WALK(stmt->returningList))
4249 0 : return true;
4250 4416 : if (WALK(stmt->withClause))
4251 0 : return true;
4252 : }
4253 4416 : break;
4254 13330 : case T_UpdateStmt:
4255 : {
4256 13330 : UpdateStmt *stmt = (UpdateStmt *) node;
4257 :
4258 13330 : if (WALK(stmt->relation))
4259 0 : return true;
4260 13330 : if (WALK(stmt->targetList))
4261 0 : return true;
4262 13330 : if (WALK(stmt->whereClause))
4263 0 : return true;
4264 13330 : if (WALK(stmt->fromClause))
4265 0 : return true;
4266 13330 : if (WALK(stmt->returningList))
4267 0 : return true;
4268 13330 : if (WALK(stmt->withClause))
4269 0 : return true;
4270 : }
4271 13330 : break;
4272 1890 : case T_MergeStmt:
4273 : {
4274 1890 : MergeStmt *stmt = (MergeStmt *) node;
4275 :
4276 1890 : if (WALK(stmt->relation))
4277 0 : return true;
4278 1890 : if (WALK(stmt->sourceRelation))
4279 0 : return true;
4280 1890 : if (WALK(stmt->joinCondition))
4281 0 : return true;
4282 1890 : if (WALK(stmt->mergeWhenClauses))
4283 0 : return true;
4284 1890 : if (WALK(stmt->returningList))
4285 0 : return true;
4286 1890 : if (WALK(stmt->withClause))
4287 0 : return true;
4288 : }
4289 1890 : break;
4290 2942 : case T_MergeWhenClause:
4291 : {
4292 2942 : MergeWhenClause *mergeWhenClause = (MergeWhenClause *) node;
4293 :
4294 2942 : if (WALK(mergeWhenClause->condition))
4295 0 : return true;
4296 2942 : if (WALK(mergeWhenClause->targetList))
4297 0 : return true;
4298 2942 : if (WALK(mergeWhenClause->values))
4299 0 : return true;
4300 : }
4301 2942 : break;
4302 626274 : case T_SelectStmt:
4303 : {
4304 626274 : SelectStmt *stmt = (SelectStmt *) node;
4305 :
4306 626274 : if (WALK(stmt->distinctClause))
4307 0 : return true;
4308 626274 : if (WALK(stmt->intoClause))
4309 0 : return true;
4310 626274 : if (WALK(stmt->targetList))
4311 0 : return true;
4312 626262 : if (WALK(stmt->fromClause))
4313 0 : return true;
4314 626190 : if (WALK(stmt->whereClause))
4315 0 : return true;
4316 626184 : if (WALK(stmt->groupClause))
4317 0 : return true;
4318 626184 : if (WALK(stmt->havingClause))
4319 0 : return true;
4320 626184 : if (WALK(stmt->windowClause))
4321 0 : return true;
4322 626184 : if (WALK(stmt->valuesLists))
4323 0 : return true;
4324 626184 : if (WALK(stmt->sortClause))
4325 0 : return true;
4326 626184 : if (WALK(stmt->limitOffset))
4327 0 : return true;
4328 626184 : if (WALK(stmt->limitCount))
4329 0 : return true;
4330 626184 : if (WALK(stmt->lockingClause))
4331 0 : return true;
4332 626184 : if (WALK(stmt->withClause))
4333 0 : return true;
4334 626184 : if (WALK(stmt->larg))
4335 0 : return true;
4336 626184 : if (WALK(stmt->rarg))
4337 0 : return true;
4338 : }
4339 626172 : break;
4340 0 : case T_PLAssignStmt:
4341 : {
4342 0 : PLAssignStmt *stmt = (PLAssignStmt *) node;
4343 :
4344 0 : if (WALK(stmt->indirection))
4345 0 : return true;
4346 0 : if (WALK(stmt->val))
4347 0 : return true;
4348 : }
4349 0 : break;
4350 690682 : case T_A_Expr:
4351 : {
4352 690682 : A_Expr *expr = (A_Expr *) node;
4353 :
4354 690682 : if (WALK(expr->lexpr))
4355 0 : return true;
4356 690682 : if (WALK(expr->rexpr))
4357 0 : return true;
4358 : /* operator name is deemed uninteresting */
4359 : }
4360 690682 : break;
4361 176552 : case T_BoolExpr:
4362 : {
4363 176552 : BoolExpr *expr = (BoolExpr *) node;
4364 :
4365 176552 : if (WALK(expr->args))
4366 0 : return true;
4367 : }
4368 176552 : break;
4369 1932730 : case T_ColumnRef:
4370 : /* we assume the fields contain nothing interesting */
4371 1932730 : break;
4372 403286 : case T_FuncCall:
4373 : {
4374 403286 : FuncCall *fcall = (FuncCall *) node;
4375 :
4376 403286 : if (WALK(fcall->args))
4377 0 : return true;
4378 403286 : if (WALK(fcall->agg_order))
4379 0 : return true;
4380 403286 : if (WALK(fcall->agg_filter))
4381 0 : return true;
4382 403286 : if (WALK(fcall->over))
4383 0 : return true;
4384 : /* function name is deemed uninteresting */
4385 : }
4386 403286 : break;
4387 48862 : case T_NamedArgExpr:
4388 48862 : return WALK(((NamedArgExpr *) node)->arg);
4389 17830 : case T_A_Indices:
4390 : {
4391 17830 : A_Indices *indices = (A_Indices *) node;
4392 :
4393 17830 : if (WALK(indices->lidx))
4394 0 : return true;
4395 17830 : if (WALK(indices->uidx))
4396 0 : return true;
4397 : }
4398 17830 : break;
4399 27266 : case T_A_Indirection:
4400 : {
4401 27266 : A_Indirection *indir = (A_Indirection *) node;
4402 :
4403 27266 : if (WALK(indir->arg))
4404 0 : return true;
4405 27266 : if (WALK(indir->indirection))
4406 0 : return true;
4407 : }
4408 27266 : break;
4409 9206 : case T_A_ArrayExpr:
4410 9206 : return WALK(((A_ArrayExpr *) node)->elements);
4411 1215952 : case T_ResTarget:
4412 : {
4413 1215952 : ResTarget *rt = (ResTarget *) node;
4414 :
4415 1215952 : if (WALK(rt->indirection))
4416 0 : return true;
4417 1215952 : if (WALK(rt->val))
4418 0 : return true;
4419 : }
4420 1215944 : break;
4421 396 : case T_MultiAssignRef:
4422 396 : return WALK(((MultiAssignRef *) node)->source);
4423 277152 : case T_TypeCast:
4424 : {
4425 277152 : TypeCast *tc = (TypeCast *) node;
4426 :
4427 277152 : if (WALK(tc->arg))
4428 0 : return true;
4429 277152 : if (WALK(tc->typeName))
4430 0 : return true;
4431 : }
4432 277152 : break;
4433 8270 : case T_CollateClause:
4434 8270 : return WALK(((CollateClause *) node)->arg);
4435 94458 : case T_SortBy:
4436 94458 : return WALK(((SortBy *) node)->node);
4437 4386 : case T_WindowDef:
4438 : {
4439 4386 : WindowDef *wd = (WindowDef *) node;
4440 :
4441 4386 : if (WALK(wd->partitionClause))
4442 0 : return true;
4443 4386 : if (WALK(wd->orderClause))
4444 0 : return true;
4445 4386 : if (WALK(wd->startOffset))
4446 0 : return true;
4447 4386 : if (WALK(wd->endOffset))
4448 0 : return true;
4449 : }
4450 4386 : break;
4451 17128 : case T_RangeSubselect:
4452 : {
4453 17128 : RangeSubselect *rs = (RangeSubselect *) node;
4454 :
4455 17128 : if (WALK(rs->subquery))
4456 0 : return true;
4457 17122 : if (WALK(rs->alias))
4458 0 : return true;
4459 : }
4460 17122 : break;
4461 54846 : case T_RangeFunction:
4462 : {
4463 54846 : RangeFunction *rf = (RangeFunction *) node;
4464 :
4465 54846 : if (WALK(rf->functions))
4466 0 : return true;
4467 54846 : if (WALK(rf->alias))
4468 0 : return true;
4469 54846 : if (WALK(rf->coldeflist))
4470 0 : return true;
4471 : }
4472 54846 : break;
4473 308 : case T_RangeTableSample:
4474 : {
4475 308 : RangeTableSample *rts = (RangeTableSample *) node;
4476 :
4477 308 : if (WALK(rts->relation))
4478 0 : return true;
4479 : /* method name is deemed uninteresting */
4480 308 : if (WALK(rts->args))
4481 0 : return true;
4482 308 : if (WALK(rts->repeatable))
4483 0 : return true;
4484 : }
4485 308 : break;
4486 232 : case T_RangeTableFunc:
4487 : {
4488 232 : RangeTableFunc *rtf = (RangeTableFunc *) node;
4489 :
4490 232 : if (WALK(rtf->docexpr))
4491 0 : return true;
4492 232 : if (WALK(rtf->rowexpr))
4493 0 : return true;
4494 232 : if (WALK(rtf->namespaces))
4495 0 : return true;
4496 232 : if (WALK(rtf->columns))
4497 0 : return true;
4498 232 : if (WALK(rtf->alias))
4499 0 : return true;
4500 : }
4501 232 : break;
4502 822 : case T_RangeTableFuncCol:
4503 : {
4504 822 : RangeTableFuncCol *rtfc = (RangeTableFuncCol *) node;
4505 :
4506 822 : if (WALK(rtfc->colexpr))
4507 0 : return true;
4508 822 : if (WALK(rtfc->coldefexpr))
4509 0 : return true;
4510 : }
4511 822 : break;
4512 281422 : case T_TypeName:
4513 : {
4514 281422 : TypeName *tn = (TypeName *) node;
4515 :
4516 281422 : if (WALK(tn->typmods))
4517 0 : return true;
4518 281422 : if (WALK(tn->arrayBounds))
4519 0 : return true;
4520 : /* type name itself is deemed uninteresting */
4521 : }
4522 281422 : break;
4523 1950 : case T_ColumnDef:
4524 : {
4525 1950 : ColumnDef *coldef = (ColumnDef *) node;
4526 :
4527 1950 : if (WALK(coldef->typeName))
4528 0 : return true;
4529 1950 : if (WALK(coldef->raw_default))
4530 0 : return true;
4531 1950 : if (WALK(coldef->collClause))
4532 0 : return true;
4533 : /* for now, constraints are ignored */
4534 : }
4535 1950 : break;
4536 1580 : case T_IndexElem:
4537 : {
4538 1580 : IndexElem *indelem = (IndexElem *) node;
4539 :
4540 1580 : if (WALK(indelem->expr))
4541 0 : return true;
4542 : /* collation and opclass names are deemed uninteresting */
4543 : }
4544 1580 : break;
4545 1248 : case T_GroupingSet:
4546 1248 : return WALK(((GroupingSet *) node)->content);
4547 5400 : case T_LockingClause:
4548 5400 : return WALK(((LockingClause *) node)->lockedRels);
4549 190 : case T_XmlSerialize:
4550 : {
4551 190 : XmlSerialize *xs = (XmlSerialize *) node;
4552 :
4553 190 : if (WALK(xs->expr))
4554 0 : return true;
4555 190 : if (WALK(xs->typeName))
4556 0 : return true;
4557 : }
4558 190 : break;
4559 3712 : case T_WithClause:
4560 3712 : return WALK(((WithClause *) node)->ctes);
4561 1330 : case T_InferClause:
4562 : {
4563 1330 : InferClause *stmt = (InferClause *) node;
4564 :
4565 1330 : if (WALK(stmt->indexElems))
4566 0 : return true;
4567 1330 : if (WALK(stmt->whereClause))
4568 0 : return true;
4569 : }
4570 1330 : break;
4571 1492 : case T_OnConflictClause:
4572 : {
4573 1492 : OnConflictClause *stmt = (OnConflictClause *) node;
4574 :
4575 1492 : if (WALK(stmt->infer))
4576 0 : return true;
4577 1492 : if (WALK(stmt->targetList))
4578 0 : return true;
4579 1492 : if (WALK(stmt->whereClause))
4580 0 : return true;
4581 : }
4582 1492 : break;
4583 4822 : case T_CommonTableExpr:
4584 : /* search_clause and cycle_clause are not interesting here */
4585 4822 : return WALK(((CommonTableExpr *) node)->ctequery);
4586 1258 : case T_JsonOutput:
4587 : {
4588 1258 : JsonOutput *out = (JsonOutput *) node;
4589 :
4590 1258 : if (WALK(out->typeName))
4591 0 : return true;
4592 1258 : if (WALK(out->returning))
4593 0 : return true;
4594 : }
4595 1258 : break;
4596 676 : case T_JsonKeyValue:
4597 : {
4598 676 : JsonKeyValue *jkv = (JsonKeyValue *) node;
4599 :
4600 676 : if (WALK(jkv->key))
4601 0 : return true;
4602 676 : if (WALK(jkv->value))
4603 0 : return true;
4604 : }
4605 676 : break;
4606 380 : case T_JsonObjectConstructor:
4607 : {
4608 380 : JsonObjectConstructor *joc = (JsonObjectConstructor *) node;
4609 :
4610 380 : if (WALK(joc->output))
4611 0 : return true;
4612 380 : if (WALK(joc->exprs))
4613 0 : return true;
4614 : }
4615 380 : break;
4616 182 : case T_JsonArrayConstructor:
4617 : {
4618 182 : JsonArrayConstructor *jac = (JsonArrayConstructor *) node;
4619 :
4620 182 : if (WALK(jac->output))
4621 0 : return true;
4622 182 : if (WALK(jac->exprs))
4623 0 : return true;
4624 : }
4625 182 : break;
4626 342 : case T_JsonAggConstructor:
4627 : {
4628 342 : JsonAggConstructor *ctor = (JsonAggConstructor *) node;
4629 :
4630 342 : if (WALK(ctor->output))
4631 0 : return true;
4632 342 : if (WALK(ctor->agg_order))
4633 0 : return true;
4634 342 : if (WALK(ctor->agg_filter))
4635 0 : return true;
4636 342 : if (WALK(ctor->over))
4637 0 : return true;
4638 : }
4639 342 : break;
4640 156 : case T_JsonObjectAgg:
4641 : {
4642 156 : JsonObjectAgg *joa = (JsonObjectAgg *) node;
4643 :
4644 156 : if (WALK(joa->constructor))
4645 0 : return true;
4646 156 : if (WALK(joa->arg))
4647 0 : return true;
4648 : }
4649 156 : break;
4650 186 : case T_JsonArrayAgg:
4651 : {
4652 186 : JsonArrayAgg *jaa = (JsonArrayAgg *) node;
4653 :
4654 186 : if (WALK(jaa->constructor))
4655 0 : return true;
4656 186 : if (WALK(jaa->arg))
4657 0 : return true;
4658 : }
4659 186 : break;
4660 54 : case T_JsonArrayQueryConstructor:
4661 : {
4662 54 : JsonArrayQueryConstructor *jaqc = (JsonArrayQueryConstructor *) node;
4663 :
4664 54 : if (WALK(jaqc->output))
4665 0 : return true;
4666 54 : if (WALK(jaqc->query))
4667 0 : return true;
4668 : }
4669 54 : break;
4670 0 : default:
4671 0 : elog(ERROR, "unrecognized node type: %d",
4672 : (int) nodeTag(node));
4673 : break;
4674 : }
4675 9687168 : return false;
4676 : }
4677 :
4678 : /*
4679 : * planstate_tree_walker --- walk plan state trees
4680 : *
4681 : * The walker has already visited the current node, and so we need only
4682 : * recurse into any sub-nodes it has.
4683 : */
4684 : bool
4685 1260902 : planstate_tree_walker_impl(PlanState *planstate,
4686 : planstate_tree_walker_callback walker,
4687 : void *context)
4688 : {
4689 1260902 : Plan *plan = planstate->plan;
4690 : ListCell *lc;
4691 :
4692 : /* We don't need implicit coercions to Node here */
4693 : #define PSWALK(n) walker(n, context)
4694 :
4695 : /* Guard against stack overflow due to overly complex plan trees */
4696 1260902 : check_stack_depth();
4697 :
4698 : /* initPlan-s */
4699 1260902 : if (planstate_walk_subplans(planstate->initPlan, walker, context))
4700 0 : return true;
4701 :
4702 : /* lefttree */
4703 1260902 : if (outerPlanState(planstate))
4704 : {
4705 449004 : if (PSWALK(outerPlanState(planstate)))
4706 0 : return true;
4707 : }
4708 :
4709 : /* righttree */
4710 1260902 : if (innerPlanState(planstate))
4711 : {
4712 125234 : if (PSWALK(innerPlanState(planstate)))
4713 0 : return true;
4714 : }
4715 :
4716 : /* special child plans */
4717 1260902 : switch (nodeTag(plan))
4718 : {
4719 15098 : case T_Append:
4720 15098 : if (planstate_walk_members(((AppendState *) planstate)->appendplans,
4721 : ((AppendState *) planstate)->as_nplans,
4722 : walker, context))
4723 0 : return true;
4724 15098 : break;
4725 532 : case T_MergeAppend:
4726 532 : if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
4727 : ((MergeAppendState *) planstate)->ms_nplans,
4728 : walker, context))
4729 0 : return true;
4730 532 : break;
4731 104 : case T_BitmapAnd:
4732 104 : if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
4733 : ((BitmapAndState *) planstate)->nplans,
4734 : walker, context))
4735 0 : return true;
4736 104 : break;
4737 330 : case T_BitmapOr:
4738 330 : if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
4739 : ((BitmapOrState *) planstate)->nplans,
4740 : walker, context))
4741 0 : return true;
4742 330 : break;
4743 10664 : case T_SubqueryScan:
4744 10664 : if (PSWALK(((SubqueryScanState *) planstate)->subplan))
4745 0 : return true;
4746 10664 : break;
4747 0 : case T_CustomScan:
4748 0 : foreach(lc, ((CustomScanState *) planstate)->custom_ps)
4749 : {
4750 0 : if (PSWALK(lfirst(lc)))
4751 0 : return true;
4752 : }
4753 0 : break;
4754 1234174 : default:
4755 1234174 : break;
4756 : }
4757 :
4758 : /* subPlan-s */
4759 1260902 : if (planstate_walk_subplans(planstate->subPlan, walker, context))
4760 0 : return true;
4761 :
4762 1260902 : return false;
4763 : }
4764 :
4765 : /*
4766 : * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
4767 : */
4768 : static bool
4769 2521804 : planstate_walk_subplans(List *plans,
4770 : planstate_tree_walker_callback walker,
4771 : void *context)
4772 : {
4773 : ListCell *lc;
4774 :
4775 2557778 : foreach(lc, plans)
4776 : {
4777 35974 : SubPlanState *sps = lfirst_node(SubPlanState, lc);
4778 :
4779 35974 : if (PSWALK(sps->planstate))
4780 0 : return true;
4781 : }
4782 :
4783 2521804 : return false;
4784 : }
4785 :
4786 : /*
4787 : * Walk the constituent plans of a ModifyTable, Append, MergeAppend,
4788 : * BitmapAnd, or BitmapOr node.
4789 : */
4790 : static bool
4791 16064 : planstate_walk_members(PlanState **planstates, int nplans,
4792 : planstate_tree_walker_callback walker,
4793 : void *context)
4794 : {
4795 : int j;
4796 :
4797 64274 : for (j = 0; j < nplans; j++)
4798 : {
4799 48210 : if (PSWALK(planstates[j]))
4800 0 : return true;
4801 : }
4802 :
4803 16064 : return false;
4804 : }
|