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