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