PostgreSQL Source Code  git master
nodeFuncs.c
Go to the documentation of this file.
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,
32  void *context);
33 static bool planstate_walk_members(PlanState **planstates, int nplans,
35  void *context);
36 
37 
38 /*
39  * exprType -
40  * returns the Oid of the type of the expression's result.
41  */
42 Oid
43 exprType(const Node *expr)
44 {
45  Oid type;
46 
47  if (!expr)
48  return InvalidOid;
49 
50  switch (nodeTag(expr))
51  {
52  case T_Var:
53  type = ((const Var *) expr)->vartype;
54  break;
55  case T_Const:
56  type = ((const Const *) expr)->consttype;
57  break;
58  case T_Param:
59  type = ((const Param *) expr)->paramtype;
60  break;
61  case T_Aggref:
62  type = ((const Aggref *) expr)->aggtype;
63  break;
64  case T_GroupingFunc:
65  type = INT4OID;
66  break;
67  case T_WindowFunc:
68  type = ((const WindowFunc *) expr)->wintype;
69  break;
70  case T_SubscriptingRef:
71  type = ((const SubscriptingRef *) expr)->refrestype;
72  break;
73  case T_FuncExpr:
74  type = ((const FuncExpr *) expr)->funcresulttype;
75  break;
76  case T_NamedArgExpr:
77  type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
78  break;
79  case T_OpExpr:
80  type = ((const OpExpr *) expr)->opresulttype;
81  break;
82  case T_DistinctExpr:
83  type = ((const DistinctExpr *) expr)->opresulttype;
84  break;
85  case T_NullIfExpr:
86  type = ((const NullIfExpr *) expr)->opresulttype;
87  break;
88  case T_ScalarArrayOpExpr:
89  type = BOOLOID;
90  break;
91  case T_BoolExpr:
92  type = BOOLOID;
93  break;
94  case T_SubLink:
95  {
96  const SubLink *sublink = (const SubLink *) expr;
97 
98  if (sublink->subLinkType == EXPR_SUBLINK ||
99  sublink->subLinkType == ARRAY_SUBLINK)
100  {
101  /* get the type of the subselect's first target column */
102  Query *qtree = (Query *) sublink->subselect;
103  TargetEntry *tent;
104 
105  if (!qtree || !IsA(qtree, Query))
106  elog(ERROR, "cannot get type for untransformed sublink");
107  tent = linitial_node(TargetEntry, qtree->targetList);
108  Assert(!tent->resjunk);
109  type = exprType((Node *) tent->expr);
110  if (sublink->subLinkType == ARRAY_SUBLINK)
111  {
113  if (!OidIsValid(type))
114  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  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
121  {
122  /* MULTIEXPR is always considered to return RECORD */
123  type = RECORDOID;
124  }
125  else
126  {
127  /* for all other sublink types, result is boolean */
128  type = BOOLOID;
129  }
130  }
131  break;
132  case T_SubPlan:
133  {
134  const SubPlan *subplan = (const SubPlan *) expr;
135 
136  if (subplan->subLinkType == EXPR_SUBLINK ||
137  subplan->subLinkType == ARRAY_SUBLINK)
138  {
139  /* get the type of the subselect's first target column */
140  type = subplan->firstColType;
141  if (subplan->subLinkType == ARRAY_SUBLINK)
142  {
144  if (!OidIsValid(type))
145  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  else if (subplan->subLinkType == MULTIEXPR_SUBLINK)
152  {
153  /* MULTIEXPR is always considered to return RECORD */
154  type = RECORDOID;
155  }
156  else
157  {
158  /* for all other subplan types, result is boolean */
159  type = BOOLOID;
160  }
161  }
162  break;
163  case T_AlternativeSubPlan:
164  {
165  const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
166 
167  /* subplans should all return the same thing */
168  type = exprType((Node *) linitial(asplan->subplans));
169  }
170  break;
171  case T_FieldSelect:
172  type = ((const FieldSelect *) expr)->resulttype;
173  break;
174  case T_FieldStore:
175  type = ((const FieldStore *) expr)->resulttype;
176  break;
177  case T_RelabelType:
178  type = ((const RelabelType *) expr)->resulttype;
179  break;
180  case T_CoerceViaIO:
181  type = ((const CoerceViaIO *) expr)->resulttype;
182  break;
183  case T_ArrayCoerceExpr:
184  type = ((const ArrayCoerceExpr *) expr)->resulttype;
185  break;
186  case T_ConvertRowtypeExpr:
187  type = ((const ConvertRowtypeExpr *) expr)->resulttype;
188  break;
189  case T_CollateExpr:
190  type = exprType((Node *) ((const CollateExpr *) expr)->arg);
191  break;
192  case T_CaseExpr:
193  type = ((const CaseExpr *) expr)->casetype;
194  break;
195  case T_CaseTestExpr:
196  type = ((const CaseTestExpr *) expr)->typeId;
197  break;
198  case T_ArrayExpr:
199  type = ((const ArrayExpr *) expr)->array_typeid;
200  break;
201  case T_RowExpr:
202  type = ((const RowExpr *) expr)->row_typeid;
203  break;
204  case T_RowCompareExpr:
205  type = BOOLOID;
206  break;
207  case T_CoalesceExpr:
208  type = ((const CoalesceExpr *) expr)->coalescetype;
209  break;
210  case T_MinMaxExpr:
211  type = ((const MinMaxExpr *) expr)->minmaxtype;
212  break;
213  case T_SQLValueFunction:
214  type = ((const SQLValueFunction *) expr)->type;
215  break;
216  case T_XmlExpr:
217  if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
218  type = BOOLOID;
219  else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
220  type = TEXTOID;
221  else
222  type = XMLOID;
223  break;
224  case T_JsonValueExpr:
225  {
226  const JsonValueExpr *jve = (const JsonValueExpr *) expr;
227 
228  type = exprType((Node *) jve->formatted_expr);
229  }
230  break;
231  case T_JsonConstructorExpr:
232  type = ((const JsonConstructorExpr *) expr)->returning->typid;
233  break;
234  case T_JsonIsPredicate:
235  type = BOOLOID;
236  break;
237  case T_NullTest:
238  type = BOOLOID;
239  break;
240  case T_BooleanTest:
241  type = BOOLOID;
242  break;
243  case T_CoerceToDomain:
244  type = ((const CoerceToDomain *) expr)->resulttype;
245  break;
246  case T_CoerceToDomainValue:
247  type = ((const CoerceToDomainValue *) expr)->typeId;
248  break;
249  case T_SetToDefault:
250  type = ((const SetToDefault *) expr)->typeId;
251  break;
252  case T_CurrentOfExpr:
253  type = BOOLOID;
254  break;
255  case T_NextValueExpr:
256  type = ((const NextValueExpr *) expr)->typeId;
257  break;
258  case T_InferenceElem:
259  {
260  const InferenceElem *n = (const InferenceElem *) expr;
261 
262  type = exprType((Node *) n->expr);
263  }
264  break;
265  case T_PlaceHolderVar:
266  type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
267  break;
268  default:
269  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
270  type = InvalidOid; /* keep compiler quiet */
271  break;
272  }
273  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 exprTypmod(const Node *expr)
283 {
284  if (!expr)
285  return -1;
286 
287  switch (nodeTag(expr))
288  {
289  case T_Var:
290  return ((const Var *) expr)->vartypmod;
291  case T_Const:
292  return ((const Const *) expr)->consttypmod;
293  case T_Param:
294  return ((const Param *) expr)->paramtypmod;
295  case T_SubscriptingRef:
296  return ((const SubscriptingRef *) expr)->reftypmod;
297  case T_FuncExpr:
298  {
299  int32 coercedTypmod;
300 
301  /* Be smart about length-coercion functions... */
302  if (exprIsLengthCoercion(expr, &coercedTypmod))
303  return coercedTypmod;
304  }
305  break;
306  case T_NamedArgExpr:
307  return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
308  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  const NullIfExpr *nexpr = (const NullIfExpr *) expr;
315 
316  return exprTypmod((Node *) linitial(nexpr->args));
317  }
318  break;
319  case T_SubLink:
320  {
321  const SubLink *sublink = (const SubLink *) expr;
322 
323  if (sublink->subLinkType == EXPR_SUBLINK ||
324  sublink->subLinkType == ARRAY_SUBLINK)
325  {
326  /* get the typmod of the subselect's first target column */
327  Query *qtree = (Query *) sublink->subselect;
328  TargetEntry *tent;
329 
330  if (!qtree || !IsA(qtree, Query))
331  elog(ERROR, "cannot get type for untransformed sublink");
332  tent = linitial_node(TargetEntry, qtree->targetList);
333  Assert(!tent->resjunk);
334  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  break;
340  case T_SubPlan:
341  {
342  const SubPlan *subplan = (const SubPlan *) expr;
343 
344  if (subplan->subLinkType == EXPR_SUBLINK ||
345  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  return subplan->firstColTypmod;
350  }
351  /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
352  }
353  break;
354  case T_AlternativeSubPlan:
355  {
356  const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
357 
358  /* subplans should all return the same thing */
359  return exprTypmod((Node *) linitial(asplan->subplans));
360  }
361  break;
362  case T_FieldSelect:
363  return ((const FieldSelect *) expr)->resulttypmod;
364  case T_RelabelType:
365  return ((const RelabelType *) expr)->resulttypmod;
366  case T_ArrayCoerceExpr:
367  return ((const ArrayCoerceExpr *) expr)->resulttypmod;
368  case T_CollateExpr:
369  return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
370  case T_CaseExpr:
371  {
372  /*
373  * If all the alternatives agree on type/typmod, return that
374  * typmod, else use -1
375  */
376  const CaseExpr *cexpr = (const CaseExpr *) expr;
377  Oid casetype = cexpr->casetype;
378  int32 typmod;
379  ListCell *arg;
380 
381  if (!cexpr->defresult)
382  return -1;
383  if (exprType((Node *) cexpr->defresult) != casetype)
384  return -1;
385  typmod = exprTypmod((Node *) cexpr->defresult);
386  if (typmod < 0)
387  return -1; /* no point in trying harder */
388  foreach(arg, cexpr->args)
389  {
391 
392  if (exprType((Node *) w->result) != casetype)
393  return -1;
394  if (exprTypmod((Node *) w->result) != typmod)
395  return -1;
396  }
397  return typmod;
398  }
399  break;
400  case T_CaseTestExpr:
401  return ((const CaseTestExpr *) expr)->typeMod;
402  case T_ArrayExpr:
403  {
404  /*
405  * If all the elements agree on type/typmod, return that
406  * typmod, else use -1
407  */
408  const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
409  Oid commontype;
410  int32 typmod;
411  ListCell *elem;
412 
413  if (arrayexpr->elements == NIL)
414  return -1;
415  typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
416  if (typmod < 0)
417  return -1; /* no point in trying harder */
418  if (arrayexpr->multidims)
419  commontype = arrayexpr->array_typeid;
420  else
421  commontype = arrayexpr->element_typeid;
422  foreach(elem, arrayexpr->elements)
423  {
424  Node *e = (Node *) lfirst(elem);
425 
426  if (exprType(e) != commontype)
427  return -1;
428  if (exprTypmod(e) != typmod)
429  return -1;
430  }
431  return typmod;
432  }
433  break;
434  case T_CoalesceExpr:
435  {
436  /*
437  * If all the alternatives agree on type/typmod, return that
438  * typmod, else use -1
439  */
440  const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
441  Oid coalescetype = cexpr->coalescetype;
442  int32 typmod;
443  ListCell *arg;
444 
445  if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
446  return -1;
447  typmod = exprTypmod((Node *) linitial(cexpr->args));
448  if (typmod < 0)
449  return -1; /* no point in trying harder */
450  for_each_from(arg, cexpr->args, 1)
451  {
452  Node *e = (Node *) lfirst(arg);
453 
454  if (exprType(e) != coalescetype)
455  return -1;
456  if (exprTypmod(e) != typmod)
457  return -1;
458  }
459  return typmod;
460  }
461  break;
462  case T_MinMaxExpr:
463  {
464  /*
465  * If all the alternatives agree on type/typmod, return that
466  * typmod, else use -1
467  */
468  const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
469  Oid minmaxtype = mexpr->minmaxtype;
470  int32 typmod;
471  ListCell *arg;
472 
473  if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
474  return -1;
475  typmod = exprTypmod((Node *) linitial(mexpr->args));
476  if (typmod < 0)
477  return -1; /* no point in trying harder */
478  for_each_from(arg, mexpr->args, 1)
479  {
480  Node *e = (Node *) lfirst(arg);
481 
482  if (exprType(e) != minmaxtype)
483  return -1;
484  if (exprTypmod(e) != typmod)
485  return -1;
486  }
487  return typmod;
488  }
489  break;
490  case T_SQLValueFunction:
491  return ((const SQLValueFunction *) expr)->typmod;
492  case T_JsonValueExpr:
493  return exprTypmod((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
494  case T_JsonConstructorExpr:
495  return ((const JsonConstructorExpr *) expr)->returning->typmod;
496  case T_CoerceToDomain:
497  return ((const CoerceToDomain *) expr)->resulttypmod;
498  case T_CoerceToDomainValue:
499  return ((const CoerceToDomainValue *) expr)->typeMod;
500  case T_SetToDefault:
501  return ((const SetToDefault *) expr)->typeMod;
502  case T_PlaceHolderVar:
503  return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
504  default:
505  break;
506  }
507  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 exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
523 {
524  if (coercedTypmod != NULL)
525  *coercedTypmod = -1; /* default result on failure */
526 
527  /*
528  * Scalar-type length coercions are FuncExprs, array-type length coercions
529  * are ArrayCoerceExprs
530  */
531  if (expr && IsA(expr, FuncExpr))
532  {
533  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  if (func->funcformat != COERCE_EXPLICIT_CAST &&
541  func->funcformat != COERCE_IMPLICIT_CAST)
542  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  nargs = list_length(func->args);
550  if (nargs < 2 || nargs > 3)
551  return false;
552 
553  second_arg = (Const *) lsecond(func->args);
554  if (!IsA(second_arg, Const) ||
555  second_arg->consttype != INT4OID ||
556  second_arg->constisnull)
557  return false;
558 
559  /*
560  * OK, it is indeed a length-coercion function.
561  */
562  if (coercedTypmod != NULL)
563  *coercedTypmod = DatumGetInt32(second_arg->constvalue);
564 
565  return true;
566  }
567 
568  if (expr && IsA(expr, ArrayCoerceExpr))
569  {
570  const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
571 
572  /* It's not a length coercion unless there's a nondefault typmod */
573  if (acoerce->resulttypmod < 0)
574  return false;
575 
576  /*
577  * OK, it is indeed a length-coercion expression.
578  */
579  if (coercedTypmod != NULL)
580  *coercedTypmod = acoerce->resulttypmod;
581 
582  return true;
583  }
584 
585  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 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  while (arg && IsA(arg, RelabelType))
610  arg = (Node *) ((RelabelType *) arg)->arg;
611 
612  if (arg && IsA(arg, Const))
613  {
614  /* Modify the Const directly to preserve const-flatness. */
615  Const *con = (Const *) arg;
616 
617  if (!overwrite_ok)
618  con = copyObject(con);
619  con->consttype = rtype;
620  con->consttypmod = rtypmod;
621  con->constcollid = rcollid;
622  /* We keep the Const's original location. */
623  return (Node *) con;
624  }
625  else if (exprType(arg) == rtype &&
626  exprTypmod(arg) == rtypmod &&
627  exprCollation(arg) == rcollid)
628  {
629  /* Sometimes we find a nest of relabels that net out to nothing. */
630  return arg;
631  }
632  else
633  {
634  /* Nope, gotta have a RelabelType. */
635  RelabelType *newrelabel = makeNode(RelabelType);
636 
637  newrelabel->arg = (Expr *) arg;
638  newrelabel->resulttype = rtype;
639  newrelabel->resulttypmod = rtypmod;
640  newrelabel->resultcollid = rcollid;
641  newrelabel->relabelformat = rformat;
642  newrelabel->location = rlocation;
643  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 *
655 {
656  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 *
671 {
672  if (node == NULL)
673  return NULL;
674  if (IsA(node, FuncExpr))
675  {
676  FuncExpr *f = (FuncExpr *) node;
677 
678  if (f->funcformat == COERCE_IMPLICIT_CAST)
680  }
681  else if (IsA(node, RelabelType))
682  {
683  RelabelType *r = (RelabelType *) node;
684 
685  if (r->relabelformat == COERCE_IMPLICIT_CAST)
686  return strip_implicit_coercions((Node *) r->arg);
687  }
688  else if (IsA(node, CoerceViaIO))
689  {
690  CoerceViaIO *c = (CoerceViaIO *) node;
691 
692  if (c->coerceformat == COERCE_IMPLICIT_CAST)
693  return strip_implicit_coercions((Node *) c->arg);
694  }
695  else if (IsA(node, ArrayCoerceExpr))
696  {
697  ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
698 
699  if (c->coerceformat == COERCE_IMPLICIT_CAST)
700  return strip_implicit_coercions((Node *) c->arg);
701  }
702  else if (IsA(node, ConvertRowtypeExpr))
703  {
705 
706  if (c->convertformat == COERCE_IMPLICIT_CAST)
707  return strip_implicit_coercions((Node *) c->arg);
708  }
709  else if (IsA(node, CoerceToDomain))
710  {
711  CoerceToDomain *c = (CoerceToDomain *) node;
712 
713  if (c->coercionformat == COERCE_IMPLICIT_CAST)
714  return strip_implicit_coercions((Node *) c->arg);
715  }
716  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
729 {
730  return expression_returns_set_walker(clause, NULL);
731 }
732 
733 static bool
734 expression_returns_set_walker(Node *node, void *context)
735 {
736  if (node == NULL)
737  return false;
738  if (IsA(node, FuncExpr))
739  {
740  FuncExpr *expr = (FuncExpr *) node;
741 
742  if (expr->funcretset)
743  return true;
744  /* else fall through to check args */
745  }
746  if (IsA(node, OpExpr))
747  {
748  OpExpr *expr = (OpExpr *) node;
749 
750  if (expr->opretset)
751  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  if (IsA(node, Aggref))
763  return false;
764  if (IsA(node, GroupingFunc))
765  return false;
766  if (IsA(node, WindowFunc))
767  return false;
768 
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 exprCollation(const Node *expr)
787 {
788  Oid coll;
789 
790  if (!expr)
791  return InvalidOid;
792 
793  switch (nodeTag(expr))
794  {
795  case T_Var:
796  coll = ((const Var *) expr)->varcollid;
797  break;
798  case T_Const:
799  coll = ((const Const *) expr)->constcollid;
800  break;
801  case T_Param:
802  coll = ((const Param *) expr)->paramcollid;
803  break;
804  case T_Aggref:
805  coll = ((const Aggref *) expr)->aggcollid;
806  break;
807  case T_GroupingFunc:
808  coll = InvalidOid;
809  break;
810  case T_WindowFunc:
811  coll = ((const WindowFunc *) expr)->wincollid;
812  break;
813  case T_SubscriptingRef:
814  coll = ((const SubscriptingRef *) expr)->refcollid;
815  break;
816  case T_FuncExpr:
817  coll = ((const FuncExpr *) expr)->funccollid;
818  break;
819  case T_NamedArgExpr:
820  coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
821  break;
822  case T_OpExpr:
823  coll = ((const OpExpr *) expr)->opcollid;
824  break;
825  case T_DistinctExpr:
826  coll = ((const DistinctExpr *) expr)->opcollid;
827  break;
828  case T_NullIfExpr:
829  coll = ((const NullIfExpr *) expr)->opcollid;
830  break;
831  case T_ScalarArrayOpExpr:
832  /* ScalarArrayOpExpr's result is boolean ... */
833  coll = InvalidOid; /* ... so it has no collation */
834  break;
835  case T_BoolExpr:
836  /* BoolExpr's result is boolean ... */
837  coll = InvalidOid; /* ... so it has no collation */
838  break;
839  case T_SubLink:
840  {
841  const SubLink *sublink = (const SubLink *) expr;
842 
843  if (sublink->subLinkType == EXPR_SUBLINK ||
844  sublink->subLinkType == ARRAY_SUBLINK)
845  {
846  /* get the collation of subselect's first target column */
847  Query *qtree = (Query *) sublink->subselect;
848  TargetEntry *tent;
849 
850  if (!qtree || !IsA(qtree, Query))
851  elog(ERROR, "cannot get collation for untransformed sublink");
852  tent = linitial_node(TargetEntry, qtree->targetList);
853  Assert(!tent->resjunk);
854  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  coll = InvalidOid; /* ... so it has no collation */
861  }
862  }
863  break;
864  case T_SubPlan:
865  {
866  const SubPlan *subplan = (const SubPlan *) expr;
867 
868  if (subplan->subLinkType == EXPR_SUBLINK ||
869  subplan->subLinkType == ARRAY_SUBLINK)
870  {
871  /* get the collation of subselect's first target column */
872  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  coll = InvalidOid; /* ... so it has no collation */
879  }
880  }
881  break;
882  case T_AlternativeSubPlan:
883  {
884  const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
885 
886  /* subplans should all return the same thing */
887  coll = exprCollation((Node *) linitial(asplan->subplans));
888  }
889  break;
890  case T_FieldSelect:
891  coll = ((const FieldSelect *) expr)->resultcollid;
892  break;
893  case T_FieldStore:
894  /* FieldStore's result is composite ... */
895  coll = InvalidOid; /* ... so it has no collation */
896  break;
897  case T_RelabelType:
898  coll = ((const RelabelType *) expr)->resultcollid;
899  break;
900  case T_CoerceViaIO:
901  coll = ((const CoerceViaIO *) expr)->resultcollid;
902  break;
903  case T_ArrayCoerceExpr:
904  coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
905  break;
906  case T_ConvertRowtypeExpr:
907  /* ConvertRowtypeExpr's result is composite ... */
908  coll = InvalidOid; /* ... so it has no collation */
909  break;
910  case T_CollateExpr:
911  coll = ((const CollateExpr *) expr)->collOid;
912  break;
913  case T_CaseExpr:
914  coll = ((const CaseExpr *) expr)->casecollid;
915  break;
916  case T_CaseTestExpr:
917  coll = ((const CaseTestExpr *) expr)->collation;
918  break;
919  case T_ArrayExpr:
920  coll = ((const ArrayExpr *) expr)->array_collid;
921  break;
922  case T_RowExpr:
923  /* RowExpr's result is composite ... */
924  coll = InvalidOid; /* ... so it has no collation */
925  break;
926  case T_RowCompareExpr:
927  /* RowCompareExpr's result is boolean ... */
928  coll = InvalidOid; /* ... so it has no collation */
929  break;
930  case T_CoalesceExpr:
931  coll = ((const CoalesceExpr *) expr)->coalescecollid;
932  break;
933  case T_MinMaxExpr:
934  coll = ((const MinMaxExpr *) expr)->minmaxcollid;
935  break;
936  case T_SQLValueFunction:
937  /* Returns either NAME or a non-collatable type */
938  if (((const SQLValueFunction *) expr)->type == NAMEOID)
939  coll = C_COLLATION_OID;
940  else
941  coll = InvalidOid;
942  break;
943  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  if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
951  coll = DEFAULT_COLLATION_OID;
952  else
953  coll = InvalidOid;
954  break;
955  case T_JsonValueExpr:
956  coll = exprCollation((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
957  break;
958  case T_JsonConstructorExpr:
959  {
960  const JsonConstructorExpr *ctor = (const JsonConstructorExpr *) expr;
961 
962  if (ctor->coercion)
963  coll = exprCollation((Node *) ctor->coercion);
964  else
965  coll = InvalidOid;
966  }
967  break;
968  case T_JsonIsPredicate:
969  /* IS JSON's result is boolean ... */
970  coll = InvalidOid; /* ... so it has no collation */
971  break;
972  case T_NullTest:
973  /* NullTest's result is boolean ... */
974  coll = InvalidOid; /* ... so it has no collation */
975  break;
976  case T_BooleanTest:
977  /* BooleanTest's result is boolean ... */
978  coll = InvalidOid; /* ... so it has no collation */
979  break;
980  case T_CoerceToDomain:
981  coll = ((const CoerceToDomain *) expr)->resultcollid;
982  break;
983  case T_CoerceToDomainValue:
984  coll = ((const CoerceToDomainValue *) expr)->collation;
985  break;
986  case T_SetToDefault:
987  coll = ((const SetToDefault *) expr)->collation;
988  break;
989  case T_CurrentOfExpr:
990  /* CurrentOfExpr's result is boolean ... */
991  coll = InvalidOid; /* ... so it has no collation */
992  break;
993  case T_NextValueExpr:
994  /* NextValueExpr's result is an integer type ... */
995  coll = InvalidOid; /* ... so it has no collation */
996  break;
997  case T_InferenceElem:
998  coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
999  break;
1000  case T_PlaceHolderVar:
1001  coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1002  break;
1003  default:
1004  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
1005  coll = InvalidOid; /* keep compiler quiet */
1006  break;
1007  }
1008  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
1019 {
1020  Oid coll;
1021 
1022  if (!expr)
1023  return InvalidOid;
1024 
1025  switch (nodeTag(expr))
1026  {
1027  case T_Aggref:
1028  coll = ((const Aggref *) expr)->inputcollid;
1029  break;
1030  case T_WindowFunc:
1031  coll = ((const WindowFunc *) expr)->inputcollid;
1032  break;
1033  case T_FuncExpr:
1034  coll = ((const FuncExpr *) expr)->inputcollid;
1035  break;
1036  case T_OpExpr:
1037  coll = ((const OpExpr *) expr)->inputcollid;
1038  break;
1039  case T_DistinctExpr:
1040  coll = ((const DistinctExpr *) expr)->inputcollid;
1041  break;
1042  case T_NullIfExpr:
1043  coll = ((const NullIfExpr *) expr)->inputcollid;
1044  break;
1045  case T_ScalarArrayOpExpr:
1046  coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
1047  break;
1048  case T_MinMaxExpr:
1049  coll = ((const MinMaxExpr *) expr)->inputcollid;
1050  break;
1051  default:
1052  coll = InvalidOid;
1053  break;
1054  }
1055  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 exprSetCollation(Node *expr, Oid collation)
1067 {
1068  switch (nodeTag(expr))
1069  {
1070  case T_Var:
1071  ((Var *) expr)->varcollid = collation;
1072  break;
1073  case T_Const:
1074  ((Const *) expr)->constcollid = collation;
1075  break;
1076  case T_Param:
1077  ((Param *) expr)->paramcollid = collation;
1078  break;
1079  case T_Aggref:
1080  ((Aggref *) expr)->aggcollid = collation;
1081  break;
1082  case T_GroupingFunc:
1083  Assert(!OidIsValid(collation));
1084  break;
1085  case T_WindowFunc:
1086  ((WindowFunc *) expr)->wincollid = collation;
1087  break;
1088  case T_SubscriptingRef:
1089  ((SubscriptingRef *) expr)->refcollid = collation;
1090  break;
1091  case T_FuncExpr:
1092  ((FuncExpr *) expr)->funccollid = collation;
1093  break;
1094  case T_NamedArgExpr:
1095  Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
1096  break;
1097  case T_OpExpr:
1098  ((OpExpr *) expr)->opcollid = collation;
1099  break;
1100  case T_DistinctExpr:
1101  ((DistinctExpr *) expr)->opcollid = collation;
1102  break;
1103  case T_NullIfExpr:
1104  ((NullIfExpr *) expr)->opcollid = collation;
1105  break;
1106  case T_ScalarArrayOpExpr:
1107  /* ScalarArrayOpExpr's result is boolean ... */
1108  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1109  break;
1110  case T_BoolExpr:
1111  /* BoolExpr's result is boolean ... */
1112  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1113  break;
1114  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  break;
1140  case T_FieldSelect:
1141  ((FieldSelect *) expr)->resultcollid = collation;
1142  break;
1143  case T_FieldStore:
1144  /* FieldStore's result is composite ... */
1145  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1146  break;
1147  case T_RelabelType:
1148  ((RelabelType *) expr)->resultcollid = collation;
1149  break;
1150  case T_CoerceViaIO:
1151  ((CoerceViaIO *) expr)->resultcollid = collation;
1152  break;
1153  case T_ArrayCoerceExpr:
1154  ((ArrayCoerceExpr *) expr)->resultcollid = collation;
1155  break;
1156  case T_ConvertRowtypeExpr:
1157  /* ConvertRowtypeExpr's result is composite ... */
1158  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1159  break;
1160  case T_CaseExpr:
1161  ((CaseExpr *) expr)->casecollid = collation;
1162  break;
1163  case T_ArrayExpr:
1164  ((ArrayExpr *) expr)->array_collid = collation;
1165  break;
1166  case T_RowExpr:
1167  /* RowExpr's result is composite ... */
1168  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1169  break;
1170  case T_RowCompareExpr:
1171  /* RowCompareExpr's result is boolean ... */
1172  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1173  break;
1174  case T_CoalesceExpr:
1175  ((CoalesceExpr *) expr)->coalescecollid = collation;
1176  break;
1177  case T_MinMaxExpr:
1178  ((MinMaxExpr *) expr)->minmaxcollid = collation;
1179  break;
1180  case T_SQLValueFunction:
1181  Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
1182  (collation == C_COLLATION_OID) :
1183  (collation == InvalidOid));
1184  break;
1185  case T_XmlExpr:
1186  Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
1187  (collation == DEFAULT_COLLATION_OID) :
1188  (collation == InvalidOid));
1189  break;
1190  case T_JsonValueExpr:
1191  exprSetCollation((Node *) ((JsonValueExpr *) expr)->formatted_expr,
1192  collation);
1193  break;
1194  case T_JsonConstructorExpr:
1195  {
1196  JsonConstructorExpr *ctor = (JsonConstructorExpr *) expr;
1197 
1198  if (ctor->coercion)
1199  exprSetCollation((Node *) ctor->coercion, collation);
1200  else
1201  Assert(!OidIsValid(collation)); /* result is always a
1202  * json[b] type */
1203  }
1204  break;
1205  case T_JsonIsPredicate:
1206  Assert(!OidIsValid(collation)); /* result is always boolean */
1207  break;
1208  case T_NullTest:
1209  /* NullTest's result is boolean ... */
1210  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1211  break;
1212  case T_BooleanTest:
1213  /* BooleanTest's result is boolean ... */
1214  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1215  break;
1216  case T_CoerceToDomain:
1217  ((CoerceToDomain *) expr)->resultcollid = collation;
1218  break;
1219  case T_CoerceToDomainValue:
1220  ((CoerceToDomainValue *) expr)->collation = collation;
1221  break;
1222  case T_SetToDefault:
1223  ((SetToDefault *) expr)->collation = collation;
1224  break;
1225  case T_CurrentOfExpr:
1226  /* CurrentOfExpr's result is boolean ... */
1227  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1228  break;
1229  case T_NextValueExpr:
1230  /* NextValueExpr's result is an integer type ... */
1231  Assert(!OidIsValid(collation)); /* ... so never set a collation */
1232  break;
1233  default:
1234  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
1235  break;
1236  }
1237 }
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 exprSetInputCollation(Node *expr, Oid inputcollation)
1249 {
1250  switch (nodeTag(expr))
1251  {
1252  case T_Aggref:
1253  ((Aggref *) expr)->inputcollid = inputcollation;
1254  break;
1255  case T_WindowFunc:
1256  ((WindowFunc *) expr)->inputcollid = inputcollation;
1257  break;
1258  case T_FuncExpr:
1259  ((FuncExpr *) expr)->inputcollid = inputcollation;
1260  break;
1261  case T_OpExpr:
1262  ((OpExpr *) expr)->inputcollid = inputcollation;
1263  break;
1264  case T_DistinctExpr:
1265  ((DistinctExpr *) expr)->inputcollid = inputcollation;
1266  break;
1267  case T_NullIfExpr:
1268  ((NullIfExpr *) expr)->inputcollid = inputcollation;
1269  break;
1270  case T_ScalarArrayOpExpr:
1271  ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
1272  break;
1273  case T_MinMaxExpr:
1274  ((MinMaxExpr *) expr)->inputcollid = inputcollation;
1275  break;
1276  default:
1277  break;
1278  }
1279 }
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 exprLocation(const Node *expr)
1313 {
1314  int loc;
1315 
1316  if (expr == NULL)
1317  return -1;
1318  switch (nodeTag(expr))
1319  {
1320  case T_RangeVar:
1321  loc = ((const RangeVar *) expr)->location;
1322  break;
1323  case T_TableFunc:
1324  loc = ((const TableFunc *) expr)->location;
1325  break;
1326  case T_Var:
1327  loc = ((const Var *) expr)->location;
1328  break;
1329  case T_Const:
1330  loc = ((const Const *) expr)->location;
1331  break;
1332  case T_Param:
1333  loc = ((const Param *) expr)->location;
1334  break;
1335  case T_Aggref:
1336  /* function name should always be the first thing */
1337  loc = ((const Aggref *) expr)->location;
1338  break;
1339  case T_GroupingFunc:
1340  loc = ((const GroupingFunc *) expr)->location;
1341  break;
1342  case T_WindowFunc:
1343  /* function name should always be the first thing */
1344  loc = ((const WindowFunc *) expr)->location;
1345  break;
1346  case T_SubscriptingRef:
1347  /* just use container argument's location */
1348  loc = exprLocation((Node *) ((const SubscriptingRef *) expr)->refexpr);
1349  break;
1350  case T_FuncExpr:
1351  {
1352  const FuncExpr *fexpr = (const FuncExpr *) expr;
1353 
1354  /* consider both function name and leftmost arg */
1355  loc = leftmostLoc(fexpr->location,
1356  exprLocation((Node *) fexpr->args));
1357  }
1358  break;
1359  case T_NamedArgExpr:
1360  {
1361  const NamedArgExpr *na = (const NamedArgExpr *) expr;
1362 
1363  /* consider both argument name and value */
1364  loc = leftmostLoc(na->location,
1365  exprLocation((Node *) na->arg));
1366  }
1367  break;
1368  case T_OpExpr:
1369  case T_DistinctExpr: /* struct-equivalent to OpExpr */
1370  case T_NullIfExpr: /* struct-equivalent to OpExpr */
1371  {
1372  const OpExpr *opexpr = (const OpExpr *) expr;
1373 
1374  /* consider both operator name and leftmost arg */
1375  loc = leftmostLoc(opexpr->location,
1376  exprLocation((Node *) opexpr->args));
1377  }
1378  break;
1379  case T_ScalarArrayOpExpr:
1380  {
1381  const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
1382 
1383  /* consider both operator name and leftmost arg */
1384  loc = leftmostLoc(saopexpr->location,
1385  exprLocation((Node *) saopexpr->args));
1386  }
1387  break;
1388  case T_BoolExpr:
1389  {
1390  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  loc = leftmostLoc(bexpr->location,
1398  exprLocation((Node *) bexpr->args));
1399  }
1400  break;
1401  case T_SubLink:
1402  {
1403  const SubLink *sublink = (const SubLink *) expr;
1404 
1405  /* check the testexpr, if any, and the operator/keyword */
1406  loc = leftmostLoc(exprLocation(sublink->testexpr),
1407  sublink->location);
1408  }
1409  break;
1410  case T_FieldSelect:
1411  /* just use argument's location */
1412  loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
1413  break;
1414  case T_FieldStore:
1415  /* just use argument's location */
1416  loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
1417  break;
1418  case T_RelabelType:
1419  {
1420  const RelabelType *rexpr = (const RelabelType *) expr;
1421 
1422  /* Much as above */
1423  loc = leftmostLoc(rexpr->location,
1424  exprLocation((Node *) rexpr->arg));
1425  }
1426  break;
1427  case T_CoerceViaIO:
1428  {
1429  const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
1430 
1431  /* Much as above */
1432  loc = leftmostLoc(cexpr->location,
1433  exprLocation((Node *) cexpr->arg));
1434  }
1435  break;
1436  case T_ArrayCoerceExpr:
1437  {
1438  const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
1439 
1440  /* Much as above */
1441  loc = leftmostLoc(cexpr->location,
1442  exprLocation((Node *) cexpr->arg));
1443  }
1444  break;
1445  case T_ConvertRowtypeExpr:
1446  {
1447  const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
1448 
1449  /* Much as above */
1450  loc = leftmostLoc(cexpr->location,
1451  exprLocation((Node *) cexpr->arg));
1452  }
1453  break;
1454  case T_CollateExpr:
1455  /* just use argument's location */
1456  loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
1457  break;
1458  case T_CaseExpr:
1459  /* CASE keyword should always be the first thing */
1460  loc = ((const CaseExpr *) expr)->location;
1461  break;
1462  case T_CaseWhen:
1463  /* WHEN keyword should always be the first thing */
1464  loc = ((const CaseWhen *) expr)->location;
1465  break;
1466  case T_ArrayExpr:
1467  /* the location points at ARRAY or [, which must be leftmost */
1468  loc = ((const ArrayExpr *) expr)->location;
1469  break;
1470  case T_RowExpr:
1471  /* the location points at ROW or (, which must be leftmost */
1472  loc = ((const RowExpr *) expr)->location;
1473  break;
1474  case T_RowCompareExpr:
1475  /* just use leftmost argument's location */
1476  loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
1477  break;
1478  case T_CoalesceExpr:
1479  /* COALESCE keyword should always be the first thing */
1480  loc = ((const CoalesceExpr *) expr)->location;
1481  break;
1482  case T_MinMaxExpr:
1483  /* GREATEST/LEAST keyword should always be the first thing */
1484  loc = ((const MinMaxExpr *) expr)->location;
1485  break;
1486  case T_SQLValueFunction:
1487  /* function keyword should always be the first thing */
1488  loc = ((const SQLValueFunction *) expr)->location;
1489  break;
1490  case T_XmlExpr:
1491  {
1492  const XmlExpr *xexpr = (const XmlExpr *) expr;
1493 
1494  /* consider both function name and leftmost arg */
1495  loc = leftmostLoc(xexpr->location,
1496  exprLocation((Node *) xexpr->args));
1497  }
1498  break;
1499  case T_JsonFormat:
1500  loc = ((const JsonFormat *) expr)->location;
1501  break;
1502  case T_JsonValueExpr:
1503  loc = exprLocation((Node *) ((const JsonValueExpr *) expr)->raw_expr);
1504  break;
1505  case T_JsonConstructorExpr:
1506  loc = ((const JsonConstructorExpr *) expr)->location;
1507  break;
1508  case T_JsonIsPredicate:
1509  loc = ((const JsonIsPredicate *) expr)->location;
1510  break;
1511  case T_NullTest:
1512  {
1513  const NullTest *nexpr = (const NullTest *) expr;
1514 
1515  /* Much as above */
1516  loc = leftmostLoc(nexpr->location,
1517  exprLocation((Node *) nexpr->arg));
1518  }
1519  break;
1520  case T_BooleanTest:
1521  {
1522  const BooleanTest *bexpr = (const BooleanTest *) expr;
1523 
1524  /* Much as above */
1525  loc = leftmostLoc(bexpr->location,
1526  exprLocation((Node *) bexpr->arg));
1527  }
1528  break;
1529  case T_CoerceToDomain:
1530  {
1531  const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
1532 
1533  /* Much as above */
1534  loc = leftmostLoc(cexpr->location,
1535  exprLocation((Node *) cexpr->arg));
1536  }
1537  break;
1538  case T_CoerceToDomainValue:
1539  loc = ((const CoerceToDomainValue *) expr)->location;
1540  break;
1541  case T_SetToDefault:
1542  loc = ((const SetToDefault *) expr)->location;
1543  break;
1544  case T_TargetEntry:
1545  /* just use argument's location */
1546  loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
1547  break;
1548  case T_IntoClause:
1549  /* use the contained RangeVar's location --- close enough */
1550  loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
1551  break;
1552  case T_List:
1553  {
1554  /* report location of first list member that has a location */
1555  ListCell *lc;
1556 
1557  loc = -1; /* just to suppress compiler warning */
1558  foreach(lc, (const List *) expr)
1559  {
1560  loc = exprLocation((Node *) lfirst(lc));
1561  if (loc >= 0)
1562  break;
1563  }
1564  }
1565  break;
1566  case T_A_Expr:
1567  {
1568  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  loc = leftmostLoc(aexpr->location,
1573  exprLocation(aexpr->lexpr));
1574  }
1575  break;
1576  case T_ColumnRef:
1577  loc = ((const ColumnRef *) expr)->location;
1578  break;
1579  case T_ParamRef:
1580  loc = ((const ParamRef *) expr)->location;
1581  break;
1582  case T_A_Const:
1583  loc = ((const A_Const *) expr)->location;
1584  break;
1585  case T_FuncCall:
1586  {
1587  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  loc = leftmostLoc(fc->location,
1592  exprLocation((Node *) fc->args));
1593  }
1594  break;
1595  case T_A_ArrayExpr:
1596  /* the location points at ARRAY or [, which must be leftmost */
1597  loc = ((const A_ArrayExpr *) expr)->location;
1598  break;
1599  case T_ResTarget:
1600  /* we need not examine the contained expression (if any) */
1601  loc = ((const ResTarget *) expr)->location;
1602  break;
1603  case T_MultiAssignRef:
1604  loc = exprLocation(((const MultiAssignRef *) expr)->source);
1605  break;
1606  case T_TypeCast:
1607  {
1608  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  loc = exprLocation(tc->arg);
1615  loc = leftmostLoc(loc, tc->typeName->location);
1616  loc = leftmostLoc(loc, tc->location);
1617  }
1618  break;
1619  case T_CollateClause:
1620  /* just use argument's location */
1621  loc = exprLocation(((const CollateClause *) expr)->arg);
1622  break;
1623  case T_SortBy:
1624  /* just use argument's location (ignore operator, if any) */
1625  loc = exprLocation(((const SortBy *) expr)->node);
1626  break;
1627  case T_WindowDef:
1628  loc = ((const WindowDef *) expr)->location;
1629  break;
1630  case T_RangeTableSample:
1631  loc = ((const RangeTableSample *) expr)->location;
1632  break;
1633  case T_TypeName:
1634  loc = ((const TypeName *) expr)->location;
1635  break;
1636  case T_ColumnDef:
1637  loc = ((const ColumnDef *) expr)->location;
1638  break;
1639  case T_Constraint:
1640  loc = ((const Constraint *) expr)->location;
1641  break;
1642  case T_FunctionParameter:
1643  /* just use typename's location */
1644  loc = exprLocation((Node *) ((const FunctionParameter *) expr)->argType);
1645  break;
1646  case T_XmlSerialize:
1647  /* XMLSERIALIZE keyword should always be the first thing */
1648  loc = ((const XmlSerialize *) expr)->location;
1649  break;
1650  case T_GroupingSet:
1651  loc = ((const GroupingSet *) expr)->location;
1652  break;
1653  case T_WithClause:
1654  loc = ((const WithClause *) expr)->location;
1655  break;
1656  case T_InferClause:
1657  loc = ((const InferClause *) expr)->location;
1658  break;
1659  case T_OnConflictClause:
1660  loc = ((const OnConflictClause *) expr)->location;
1661  break;
1662  case T_CTESearchClause:
1663  loc = ((const CTESearchClause *) expr)->location;
1664  break;
1665  case T_CTECycleClause:
1666  loc = ((const CTECycleClause *) expr)->location;
1667  break;
1668  case T_CommonTableExpr:
1669  loc = ((const CommonTableExpr *) expr)->location;
1670  break;
1671  case T_JsonKeyValue:
1672  /* just use the key's location */
1673  loc = exprLocation((Node *) ((const JsonKeyValue *) expr)->key);
1674  break;
1675  case T_JsonObjectConstructor:
1676  loc = ((const JsonObjectConstructor *) expr)->location;
1677  break;
1678  case T_JsonArrayConstructor:
1679  loc = ((const JsonArrayConstructor *) expr)->location;
1680  break;
1681  case T_JsonArrayQueryConstructor:
1682  loc = ((const JsonArrayQueryConstructor *) expr)->location;
1683  break;
1684  case T_JsonAggConstructor:
1685  loc = ((const JsonAggConstructor *) expr)->location;
1686  break;
1687  case T_JsonObjectAgg:
1688  loc = exprLocation((Node *) ((const JsonObjectAgg *) expr)->constructor);
1689  break;
1690  case T_JsonArrayAgg:
1691  loc = exprLocation((Node *) ((const JsonArrayAgg *) expr)->constructor);
1692  break;
1693  case T_PlaceHolderVar:
1694  /* just use argument's location */
1695  loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1696  break;
1697  case T_InferenceElem:
1698  /* just use nested expr's location */
1699  loc = exprLocation((Node *) ((const InferenceElem *) expr)->expr);
1700  break;
1701  case T_PartitionElem:
1702  loc = ((const PartitionElem *) expr)->location;
1703  break;
1704  case T_PartitionSpec:
1705  loc = ((const PartitionSpec *) expr)->location;
1706  break;
1707  case T_PartitionBoundSpec:
1708  loc = ((const PartitionBoundSpec *) expr)->location;
1709  break;
1710  case T_PartitionRangeDatum:
1711  loc = ((const PartitionRangeDatum *) expr)->location;
1712  break;
1713  default:
1714  /* for any other node type it's just unknown... */
1715  loc = -1;
1716  break;
1717  }
1718  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 leftmostLoc(int loc1, int loc2)
1728 {
1729  if (loc1 < 0)
1730  return loc2;
1731  else if (loc2 < 0)
1732  return loc1;
1733  else
1734  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
1749 {
1750  /* This tree walk requires no special setup, so away we go... */
1751  fix_opfuncids_walker(node, NULL);
1752 }
1753 
1754 static bool
1755 fix_opfuncids_walker(Node *node, void *context)
1756 {
1757  if (node == NULL)
1758  return false;
1759  if (IsA(node, OpExpr))
1760  set_opfuncid((OpExpr *) node);
1761  else if (IsA(node, DistinctExpr))
1762  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1763  else if (IsA(node, NullIfExpr))
1764  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1765  else if (IsA(node, ScalarArrayOpExpr))
1767  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
1780 {
1781  if (opexpr->opfuncid == InvalidOid)
1782  opexpr->opfuncid = get_opcode(opexpr->opno);
1783 }
1784 
1785 /*
1786  * set_sa_opfuncid
1787  * As above, for ScalarArrayOpExpr nodes.
1788  */
1789 void
1791 {
1792  if (opexpr->opfuncid == InvalidOid)
1793  opexpr->opfuncid = get_opcode(opexpr->opno);
1794 }
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
1818  void *context)
1819 {
1820  switch (nodeTag(node))
1821  {
1822  case T_Aggref:
1823  {
1824  Aggref *expr = (Aggref *) node;
1825 
1826  if (checker(expr->aggfnoid, context))
1827  return true;
1828  }
1829  break;
1830  case T_WindowFunc:
1831  {
1832  WindowFunc *expr = (WindowFunc *) node;
1833 
1834  if (checker(expr->winfnoid, context))
1835  return true;
1836  }
1837  break;
1838  case T_FuncExpr:
1839  {
1840  FuncExpr *expr = (FuncExpr *) node;
1841 
1842  if (checker(expr->funcid, context))
1843  return true;
1844  }
1845  break;
1846  case T_OpExpr:
1847  case T_DistinctExpr: /* struct-equivalent to OpExpr */
1848  case T_NullIfExpr: /* struct-equivalent to OpExpr */
1849  {
1850  OpExpr *expr = (OpExpr *) node;
1851 
1852  /* Set opfuncid if it wasn't set already */
1853  set_opfuncid(expr);
1854  if (checker(expr->opfuncid, context))
1855  return true;
1856  }
1857  break;
1858  case T_ScalarArrayOpExpr:
1859  {
1860  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1861 
1862  set_sa_opfuncid(expr);
1863  if (checker(expr->opfuncid, context))
1864  return true;
1865  }
1866  break;
1867  case T_CoerceViaIO:
1868  {
1869  CoerceViaIO *expr = (CoerceViaIO *) node;
1870  Oid iofunc;
1871  Oid typioparam;
1872  bool typisvarlena;
1873 
1874  /* check the result type's input function */
1876  &iofunc, &typioparam);
1877  if (checker(iofunc, context))
1878  return true;
1879  /* check the input type's output function */
1880  getTypeOutputInfo(exprType((Node *) expr->arg),
1881  &iofunc, &typisvarlena);
1882  if (checker(iofunc, context))
1883  return true;
1884  }
1885  break;
1886  case T_RowCompareExpr:
1887  {
1888  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1889  ListCell *opid;
1890 
1891  foreach(opid, rcexpr->opnos)
1892  {
1893  Oid opfuncid = get_opcode(lfirst_oid(opid));
1894 
1895  if (checker(opfuncid, context))
1896  return true;
1897  }
1898  }
1899  break;
1900  default:
1901  break;
1902  }
1903  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
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  if (node == NULL)
2019  return false;
2020 
2021  /* Guard against stack overflow due to overly complex expressions */
2023 
2024  switch (nodeTag(node))
2025  {
2026  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  break;
2040  case T_WithCheckOption:
2041  return WALK(((WithCheckOption *) node)->qual);
2042  case T_Aggref:
2043  {
2044  Aggref *expr = (Aggref *) node;
2045 
2046  /* recurse directly on Lists */
2047  if (LIST_WALK(expr->aggdirectargs))
2048  return true;
2049  if (LIST_WALK(expr->args))
2050  return true;
2051  if (LIST_WALK(expr->aggorder))
2052  return true;
2053  if (LIST_WALK(expr->aggdistinct))
2054  return true;
2055  if (WALK(expr->aggfilter))
2056  return true;
2057  }
2058  break;
2059  case T_GroupingFunc:
2060  {
2061  GroupingFunc *grouping = (GroupingFunc *) node;
2062 
2063  if (LIST_WALK(grouping->args))
2064  return true;
2065  }
2066  break;
2067  case T_WindowFunc:
2068  {
2069  WindowFunc *expr = (WindowFunc *) node;
2070 
2071  /* recurse directly on List */
2072  if (LIST_WALK(expr->args))
2073  return true;
2074  if (WALK(expr->aggfilter))
2075  return true;
2076  }
2077  break;
2078  case T_SubscriptingRef:
2079  {
2080  SubscriptingRef *sbsref = (SubscriptingRef *) node;
2081 
2082  /* recurse directly for upper/lower container index lists */
2083  if (LIST_WALK(sbsref->refupperindexpr))
2084  return true;
2085  if (LIST_WALK(sbsref->reflowerindexpr))
2086  return true;
2087  /* walker must see the refexpr and refassgnexpr, however */
2088  if (WALK(sbsref->refexpr))
2089  return true;
2090 
2091  if (WALK(sbsref->refassgnexpr))
2092  return true;
2093  }
2094  break;
2095  case T_FuncExpr:
2096  {
2097  FuncExpr *expr = (FuncExpr *) node;
2098 
2099  if (LIST_WALK(expr->args))
2100  return true;
2101  }
2102  break;
2103  case T_NamedArgExpr:
2104  return WALK(((NamedArgExpr *) node)->arg);
2105  case T_OpExpr:
2106  case T_DistinctExpr: /* struct-equivalent to OpExpr */
2107  case T_NullIfExpr: /* struct-equivalent to OpExpr */
2108  {
2109  OpExpr *expr = (OpExpr *) node;
2110 
2111  if (LIST_WALK(expr->args))
2112  return true;
2113  }
2114  break;
2115  case T_ScalarArrayOpExpr:
2116  {
2117  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2118 
2119  if (LIST_WALK(expr->args))
2120  return true;
2121  }
2122  break;
2123  case T_BoolExpr:
2124  {
2125  BoolExpr *expr = (BoolExpr *) node;
2126 
2127  if (LIST_WALK(expr->args))
2128  return true;
2129  }
2130  break;
2131  case T_SubLink:
2132  {
2133  SubLink *sublink = (SubLink *) node;
2134 
2135  if (WALK(sublink->testexpr))
2136  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  return WALK(sublink->subselect);
2143  }
2144  break;
2145  case T_SubPlan:
2146  {
2147  SubPlan *subplan = (SubPlan *) node;
2148 
2149  /* recurse into the testexpr, but not into the Plan */
2150  if (WALK(subplan->testexpr))
2151  return true;
2152  /* also examine args list */
2153  if (LIST_WALK(subplan->args))
2154  return true;
2155  }
2156  break;
2157  case T_AlternativeSubPlan:
2158  return LIST_WALK(((AlternativeSubPlan *) node)->subplans);
2159  case T_FieldSelect:
2160  return WALK(((FieldSelect *) node)->arg);
2161  case T_FieldStore:
2162  {
2163  FieldStore *fstore = (FieldStore *) node;
2164 
2165  if (WALK(fstore->arg))
2166  return true;
2167  if (WALK(fstore->newvals))
2168  return true;
2169  }
2170  break;
2171  case T_RelabelType:
2172  return WALK(((RelabelType *) node)->arg);
2173  case T_CoerceViaIO:
2174  return WALK(((CoerceViaIO *) node)->arg);
2175  case T_ArrayCoerceExpr:
2176  {
2177  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2178 
2179  if (WALK(acoerce->arg))
2180  return true;
2181  if (WALK(acoerce->elemexpr))
2182  return true;
2183  }
2184  break;
2185  case T_ConvertRowtypeExpr:
2186  return WALK(((ConvertRowtypeExpr *) node)->arg);
2187  case T_CollateExpr:
2188  return WALK(((CollateExpr *) node)->arg);
2189  case T_CaseExpr:
2190  {
2191  CaseExpr *caseexpr = (CaseExpr *) node;
2192 
2193  if (WALK(caseexpr->arg))
2194  return true;
2195  /* we assume walker doesn't care about CaseWhens, either */
2196  foreach(temp, caseexpr->args)
2197  {
2198  CaseWhen *when = lfirst_node(CaseWhen, temp);
2199 
2200  if (WALK(when->expr))
2201  return true;
2202  if (WALK(when->result))
2203  return true;
2204  }
2205  if (WALK(caseexpr->defresult))
2206  return true;
2207  }
2208  break;
2209  case T_ArrayExpr:
2210  return WALK(((ArrayExpr *) node)->elements);
2211  case T_RowExpr:
2212  /* Assume colnames isn't interesting */
2213  return WALK(((RowExpr *) node)->args);
2214  case T_RowCompareExpr:
2215  {
2216  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2217 
2218  if (WALK(rcexpr->largs))
2219  return true;
2220  if (WALK(rcexpr->rargs))
2221  return true;
2222  }
2223  break;
2224  case T_CoalesceExpr:
2225  return WALK(((CoalesceExpr *) node)->args);
2226  case T_MinMaxExpr:
2227  return WALK(((MinMaxExpr *) node)->args);
2228  case T_XmlExpr:
2229  {
2230  XmlExpr *xexpr = (XmlExpr *) node;
2231 
2232  if (WALK(xexpr->named_args))
2233  return true;
2234  /* we assume walker doesn't care about arg_names */
2235  if (WALK(xexpr->args))
2236  return true;
2237  }
2238  break;
2239  case T_JsonValueExpr:
2240  {
2241  JsonValueExpr *jve = (JsonValueExpr *) node;
2242 
2243  if (WALK(jve->raw_expr))
2244  return true;
2245  if (WALK(jve->formatted_expr))
2246  return true;
2247  }
2248  break;
2249  case T_JsonConstructorExpr:
2250  {
2251  JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
2252 
2253  if (WALK(ctor->args))
2254  return true;
2255  if (WALK(ctor->func))
2256  return true;
2257  if (WALK(ctor->coercion))
2258  return true;
2259  }
2260  break;
2261  case T_JsonIsPredicate:
2262  return WALK(((JsonIsPredicate *) node)->expr);
2263  case T_NullTest:
2264  return WALK(((NullTest *) node)->arg);
2265  case T_BooleanTest:
2266  return WALK(((BooleanTest *) node)->arg);
2267  case T_CoerceToDomain:
2268  return WALK(((CoerceToDomain *) node)->arg);
2269  case T_TargetEntry:
2270  return WALK(((TargetEntry *) node)->expr);
2271  case T_Query:
2272  /* Do nothing with a sub-Query, per discussion above */
2273  break;
2274  case T_WindowClause:
2275  {
2276  WindowClause *wc = (WindowClause *) node;
2277 
2278  if (WALK(wc->partitionClause))
2279  return true;
2280  if (WALK(wc->orderClause))
2281  return true;
2282  if (WALK(wc->startOffset))
2283  return true;
2284  if (WALK(wc->endOffset))
2285  return true;
2286  }
2287  break;
2288  case T_CTECycleClause:
2289  {
2290  CTECycleClause *cc = (CTECycleClause *) node;
2291 
2292  if (WALK(cc->cycle_mark_value))
2293  return true;
2294  if (WALK(cc->cycle_mark_default))
2295  return true;
2296  }
2297  break;
2298  case T_CommonTableExpr:
2299  {
2300  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  if (WALK(cte->ctequery))
2307  return true;
2308 
2309  if (WALK(cte->search_clause))
2310  return true;
2311  if (WALK(cte->cycle_clause))
2312  return true;
2313  }
2314  break;
2315  case T_JsonKeyValue:
2316  {
2317  JsonKeyValue *kv = (JsonKeyValue *) node;
2318 
2319  if (WALK(kv->key))
2320  return true;
2321  if (WALK(kv->value))
2322  return true;
2323  }
2324  break;
2325  case T_JsonObjectConstructor:
2326  {
2328 
2329  if (LIST_WALK(ctor->exprs))
2330  return true;
2331  }
2332  break;
2333  case T_JsonArrayConstructor:
2334  {
2335  JsonArrayConstructor *ctor = (JsonArrayConstructor *) node;
2336 
2337  if (LIST_WALK(ctor->exprs))
2338  return true;
2339  }
2340  break;
2341  case T_JsonArrayQueryConstructor:
2342  {
2344 
2345  if (WALK(ctor->query))
2346  return true;
2347  }
2348  break;
2349  case T_JsonAggConstructor:
2350  {
2351  JsonAggConstructor *ctor = (JsonAggConstructor *) node;
2352 
2353  if (WALK(ctor->agg_filter))
2354  return true;
2355  if (WALK(ctor->agg_order))
2356  return true;
2357  if (WALK(ctor->over))
2358  return true;
2359  }
2360  break;
2361  case T_JsonObjectAgg:
2362  {
2363  JsonObjectAgg *ctor = (JsonObjectAgg *) node;
2364 
2365  if (WALK(ctor->constructor))
2366  return true;
2367  if (WALK(ctor->arg))
2368  return true;
2369  }
2370  break;
2371  case T_JsonArrayAgg:
2372  {
2373  JsonArrayAgg *ctor = (JsonArrayAgg *) node;
2374 
2375  if (WALK(ctor->constructor))
2376  return true;
2377  if (WALK(ctor->arg))
2378  return true;
2379  }
2380  break;
2381 
2382  case T_PartitionBoundSpec:
2383  {
2384  PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
2385 
2386  if (WALK(pbs->listdatums))
2387  return true;
2388  if (WALK(pbs->lowerdatums))
2389  return true;
2390  if (WALK(pbs->upperdatums))
2391  return true;
2392  }
2393  break;
2394  case T_PartitionRangeDatum:
2395  {
2396  PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
2397 
2398  if (WALK(prd->value))
2399  return true;
2400  }
2401  break;
2402  case T_List:
2403  foreach(temp, (List *) node)
2404  {
2405  if (WALK(lfirst(temp)))
2406  return true;
2407  }
2408  break;
2409  case T_FromExpr:
2410  {
2411  FromExpr *from = (FromExpr *) node;
2412 
2413  if (LIST_WALK(from->fromlist))
2414  return true;
2415  if (WALK(from->quals))
2416  return true;
2417  }
2418  break;
2419  case T_OnConflictExpr:
2420  {
2421  OnConflictExpr *onconflict = (OnConflictExpr *) node;
2422 
2423  if (WALK(onconflict->arbiterElems))
2424  return true;
2425  if (WALK(onconflict->arbiterWhere))
2426  return true;
2427  if (WALK(onconflict->onConflictSet))
2428  return true;
2429  if (WALK(onconflict->onConflictWhere))
2430  return true;
2431  if (WALK(onconflict->exclRelTlist))
2432  return true;
2433  }
2434  break;
2435  case T_MergeAction:
2436  {
2437  MergeAction *action = (MergeAction *) node;
2438 
2439  if (WALK(action->qual))
2440  return true;
2441  if (WALK(action->targetList))
2442  return true;
2443  }
2444  break;
2445  case T_PartitionPruneStepOp:
2446  {
2447  PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
2448 
2449  if (WALK(opstep->exprs))
2450  return true;
2451  }
2452  break;
2453  case T_PartitionPruneStepCombine:
2454  /* no expression subnodes */
2455  break;
2456  case T_JoinExpr:
2457  {
2458  JoinExpr *join = (JoinExpr *) node;
2459 
2460  if (WALK(join->larg))
2461  return true;
2462  if (WALK(join->rarg))
2463  return true;
2464  if (WALK(join->quals))
2465  return true;
2466 
2467  /*
2468  * alias clause, using list are deemed uninteresting.
2469  */
2470  }
2471  break;
2472  case T_SetOperationStmt:
2473  {
2474  SetOperationStmt *setop = (SetOperationStmt *) node;
2475 
2476  if (WALK(setop->larg))
2477  return true;
2478  if (WALK(setop->rarg))
2479  return true;
2480 
2481  /* groupClauses are deemed uninteresting */
2482  }
2483  break;
2484  case T_IndexClause:
2485  {
2486  IndexClause *iclause = (IndexClause *) node;
2487 
2488  if (WALK(iclause->rinfo))
2489  return true;
2490  if (LIST_WALK(iclause->indexquals))
2491  return true;
2492  }
2493  break;
2494  case T_PlaceHolderVar:
2495  return WALK(((PlaceHolderVar *) node)->phexpr);
2496  case T_InferenceElem:
2497  return WALK(((InferenceElem *) node)->expr);
2498  case T_AppendRelInfo:
2499  {
2500  AppendRelInfo *appinfo = (AppendRelInfo *) node;
2501 
2502  if (LIST_WALK(appinfo->translated_vars))
2503  return true;
2504  }
2505  break;
2506  case T_PlaceHolderInfo:
2507  return WALK(((PlaceHolderInfo *) node)->ph_var);
2508  case T_RangeTblFunction:
2509  return WALK(((RangeTblFunction *) node)->funcexpr);
2510  case T_TableSampleClause:
2511  {
2512  TableSampleClause *tsc = (TableSampleClause *) node;
2513 
2514  if (LIST_WALK(tsc->args))
2515  return true;
2516  if (WALK(tsc->repeatable))
2517  return true;
2518  }
2519  break;
2520  case T_TableFunc:
2521  {
2522  TableFunc *tf = (TableFunc *) node;
2523 
2524  if (WALK(tf->ns_uris))
2525  return true;
2526  if (WALK(tf->docexpr))
2527  return true;
2528  if (WALK(tf->rowexpr))
2529  return true;
2530  if (WALK(tf->colexprs))
2531  return true;
2532  if (WALK(tf->coldefexprs))
2533  return true;
2534  }
2535  break;
2536  default:
2537  elog(ERROR, "unrecognized node type: %d",
2538  (int) nodeTag(node));
2539  break;
2540  }
2541  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
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  if (WALK(query->targetList))
2578  return true;
2579  if (WALK(query->withCheckOptions))
2580  return true;
2581  if (WALK(query->onConflict))
2582  return true;
2583  if (WALK(query->mergeActionList))
2584  return true;
2585  if (WALK(query->returningList))
2586  return true;
2587  if (WALK(query->jointree))
2588  return true;
2589  if (WALK(query->setOperations))
2590  return true;
2591  if (WALK(query->havingQual))
2592  return true;
2593  if (WALK(query->limitOffset))
2594  return true;
2595  if (WALK(query->limitCount))
2596  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  if ((flags & QTW_EXAMINE_SORTGROUP))
2604  {
2605  if (WALK(query->groupClause))
2606  return true;
2607  if (WALK(query->windowClause))
2608  return true;
2609  if (WALK(query->sortClause))
2610  return true;
2611  if (WALK(query->distinctClause))
2612  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  foreach(lc, query->windowClause)
2623  {
2625 
2626  if (WALK(wc->startOffset))
2627  return true;
2628  if (WALK(wc->endOffset))
2629  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  if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2646  {
2647  if (WALK(query->cteList))
2648  return true;
2649  }
2650  if (!(flags & QTW_IGNORE_RANGE_TABLE))
2651  {
2652  if (range_table_walker(query->rtable, walker, context, flags))
2653  return true;
2654  }
2655  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
2665  tree_walker_callback walker,
2666  void *context,
2667  int flags)
2668 {
2669  ListCell *rt;
2670 
2671  foreach(rt, rtable)
2672  {
2674 
2675  if (range_table_entry_walker(rte, walker, context, flags))
2676  return true;
2677  }
2678  return false;
2679 }
2680 
2681 /*
2682  * Some callers even want to scan the expressions in individual RTEs.
2683  */
2684 bool
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  if (flags & QTW_EXAMINE_RTES_BEFORE)
2696  if (WALK(rte))
2697  return true;
2698 
2699  switch (rte->rtekind)
2700  {
2701  case RTE_RELATION:
2702  if (WALK(rte->tablesample))
2703  return true;
2704  break;
2705  case RTE_SUBQUERY:
2706  if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2707  if (WALK(rte->subquery))
2708  return true;
2709  break;
2710  case RTE_JOIN:
2711  if (!(flags & QTW_IGNORE_JOINALIASES))
2712  if (WALK(rte->joinaliasvars))
2713  return true;
2714  break;
2715  case RTE_FUNCTION:
2716  if (WALK(rte->functions))
2717  return true;
2718  break;
2719  case RTE_TABLEFUNC:
2720  if (WALK(rte->tablefunc))
2721  return true;
2722  break;
2723  case RTE_VALUES:
2724  if (WALK(rte->values_lists))
2725  return true;
2726  break;
2727  case RTE_CTE:
2728  case RTE_NAMEDTUPLESTORE:
2729  case RTE_RESULT:
2730  /* nothing to do */
2731  break;
2732  }
2733 
2734  if (WALK(rte->securityQuals))
2735  return true;
2736 
2737  if (flags & QTW_EXAMINE_RTES_AFTER)
2738  if (WALK(rte))
2739  return true;
2740 
2741  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 *
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  if (node == NULL)
2825  return NULL;
2826 
2827  /* Guard against stack overflow due to overly complex expressions */
2829 
2830  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  case T_Var:
2838  {
2839  Var *var = (Var *) node;
2840  Var *newnode;
2841 
2842  FLATCOPY(newnode, var, Var);
2843  /* Assume we need not copy the varnullingrels bitmapset */
2844  return (Node *) newnode;
2845  }
2846  break;
2847  case T_Const:
2848  {
2849  Const *oldnode = (Const *) node;
2850  Const *newnode;
2851 
2852  FLATCOPY(newnode, oldnode, Const);
2853  /* XXX we don't bother with datumCopy; should we? */
2854  return (Node *) newnode;
2855  }
2856  break;
2857  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  return (Node *) copyObject(node);
2869  case T_WithCheckOption:
2870  {
2871  WithCheckOption *wco = (WithCheckOption *) node;
2872  WithCheckOption *newnode;
2873 
2874  FLATCOPY(newnode, wco, WithCheckOption);
2875  MUTATE(newnode->qual, wco->qual, Node *);
2876  return (Node *) newnode;
2877  }
2878  case T_Aggref:
2879  {
2880  Aggref *aggref = (Aggref *) node;
2881  Aggref *newnode;
2882 
2883  FLATCOPY(newnode, aggref, Aggref);
2884  /* assume mutation doesn't change types of arguments */
2885  newnode->aggargtypes = list_copy(aggref->aggargtypes);
2886  MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
2887  MUTATE(newnode->args, aggref->args, List *);
2888  MUTATE(newnode->aggorder, aggref->aggorder, List *);
2889  MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
2890  MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
2891  return (Node *) newnode;
2892  }
2893  break;
2894  case T_GroupingFunc:
2895  {
2896  GroupingFunc *grouping = (GroupingFunc *) node;
2897  GroupingFunc *newnode;
2898 
2899  FLATCOPY(newnode, grouping, GroupingFunc);
2900  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  newnode->refs = list_copy(grouping->refs);
2912  newnode->cols = list_copy(grouping->cols);
2913 
2914  return (Node *) newnode;
2915  }
2916  break;
2917  case T_WindowFunc:
2918  {
2919  WindowFunc *wfunc = (WindowFunc *) node;
2920  WindowFunc *newnode;
2921 
2922  FLATCOPY(newnode, wfunc, WindowFunc);
2923  MUTATE(newnode->args, wfunc->args, List *);
2924  MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
2925  return (Node *) newnode;
2926  }
2927  break;
2928  case T_SubscriptingRef:
2929  {
2930  SubscriptingRef *sbsref = (SubscriptingRef *) node;
2931  SubscriptingRef *newnode;
2932 
2933  FLATCOPY(newnode, sbsref, SubscriptingRef);
2934  MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
2935  List *);
2936  MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
2937  List *);
2938  MUTATE(newnode->refexpr, sbsref->refexpr,
2939  Expr *);
2940  MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
2941  Expr *);
2942 
2943  return (Node *) newnode;
2944  }
2945  break;
2946  case T_FuncExpr:
2947  {
2948  FuncExpr *expr = (FuncExpr *) node;
2949  FuncExpr *newnode;
2950 
2951  FLATCOPY(newnode, expr, FuncExpr);
2952  MUTATE(newnode->args, expr->args, List *);
2953  return (Node *) newnode;
2954  }
2955  break;
2956  case T_NamedArgExpr:
2957  {
2958  NamedArgExpr *nexpr = (NamedArgExpr *) node;
2959  NamedArgExpr *newnode;
2960 
2961  FLATCOPY(newnode, nexpr, NamedArgExpr);
2962  MUTATE(newnode->arg, nexpr->arg, Expr *);
2963  return (Node *) newnode;
2964  }
2965  break;
2966  case T_OpExpr:
2967  {
2968  OpExpr *expr = (OpExpr *) node;
2969  OpExpr *newnode;
2970 
2971  FLATCOPY(newnode, expr, OpExpr);
2972  MUTATE(newnode->args, expr->args, List *);
2973  return (Node *) newnode;
2974  }
2975  break;
2976  case T_DistinctExpr:
2977  {
2978  DistinctExpr *expr = (DistinctExpr *) node;
2979  DistinctExpr *newnode;
2980 
2981  FLATCOPY(newnode, expr, DistinctExpr);
2982  MUTATE(newnode->args, expr->args, List *);
2983  return (Node *) newnode;
2984  }
2985  break;
2986  case T_NullIfExpr:
2987  {
2988  NullIfExpr *expr = (NullIfExpr *) node;
2989  NullIfExpr *newnode;
2990 
2991  FLATCOPY(newnode, expr, NullIfExpr);
2992  MUTATE(newnode->args, expr->args, List *);
2993  return (Node *) newnode;
2994  }
2995  break;
2996  case T_ScalarArrayOpExpr:
2997  {
2998  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2999  ScalarArrayOpExpr *newnode;
3000 
3001  FLATCOPY(newnode, expr, ScalarArrayOpExpr);
3002  MUTATE(newnode->args, expr->args, List *);
3003  return (Node *) newnode;
3004  }
3005  break;
3006  case T_BoolExpr:
3007  {
3008  BoolExpr *expr = (BoolExpr *) node;
3009  BoolExpr *newnode;
3010 
3011  FLATCOPY(newnode, expr, BoolExpr);
3012  MUTATE(newnode->args, expr->args, List *);
3013  return (Node *) newnode;
3014  }
3015  break;
3016  case T_SubLink:
3017  {
3018  SubLink *sublink = (SubLink *) node;
3019  SubLink *newnode;
3020 
3021  FLATCOPY(newnode, sublink, SubLink);
3022  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  MUTATE(newnode->subselect, sublink->subselect, Node *);
3029  return (Node *) newnode;
3030  }
3031  break;
3032  case T_SubPlan:
3033  {
3034  SubPlan *subplan = (SubPlan *) node;
3035  SubPlan *newnode;
3036 
3037  FLATCOPY(newnode, subplan, SubPlan);
3038  /* transform testexpr */
3039  MUTATE(newnode->testexpr, subplan->testexpr, Node *);
3040  /* transform args list (params to be passed to subplan) */
3041  MUTATE(newnode->args, subplan->args, List *);
3042  /* but not the sub-Plan itself, which is referenced as-is */
3043  return (Node *) newnode;
3044  }
3045  break;
3046  case T_AlternativeSubPlan:
3047  {
3048  AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
3049  AlternativeSubPlan *newnode;
3050 
3051  FLATCOPY(newnode, asplan, AlternativeSubPlan);
3052  MUTATE(newnode->subplans, asplan->subplans, List *);
3053  return (Node *) newnode;
3054  }
3055  break;
3056  case T_FieldSelect:
3057  {
3058  FieldSelect *fselect = (FieldSelect *) node;
3059  FieldSelect *newnode;
3060 
3061  FLATCOPY(newnode, fselect, FieldSelect);
3062  MUTATE(newnode->arg, fselect->arg, Expr *);
3063  return (Node *) newnode;
3064  }
3065  break;
3066  case T_FieldStore:
3067  {
3068  FieldStore *fstore = (FieldStore *) node;
3069  FieldStore *newnode;
3070 
3071  FLATCOPY(newnode, fstore, FieldStore);
3072  MUTATE(newnode->arg, fstore->arg, Expr *);
3073  MUTATE(newnode->newvals, fstore->newvals, List *);
3074  newnode->fieldnums = list_copy(fstore->fieldnums);
3075  return (Node *) newnode;
3076  }
3077  break;
3078  case T_RelabelType:
3079  {
3080  RelabelType *relabel = (RelabelType *) node;
3081  RelabelType *newnode;
3082 
3083  FLATCOPY(newnode, relabel, RelabelType);
3084  MUTATE(newnode->arg, relabel->arg, Expr *);
3085  return (Node *) newnode;
3086  }
3087  break;
3088  case T_CoerceViaIO:
3089  {
3090  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
3091  CoerceViaIO *newnode;
3092 
3093  FLATCOPY(newnode, iocoerce, CoerceViaIO);
3094  MUTATE(newnode->arg, iocoerce->arg, Expr *);
3095  return (Node *) newnode;
3096  }
3097  break;
3098  case T_ArrayCoerceExpr:
3099  {
3100  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
3101  ArrayCoerceExpr *newnode;
3102 
3103  FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
3104  MUTATE(newnode->arg, acoerce->arg, Expr *);
3105  MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
3106  return (Node *) newnode;
3107  }
3108  break;
3109  case T_ConvertRowtypeExpr:
3110  {
3111  ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
3112  ConvertRowtypeExpr *newnode;
3113 
3114  FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
3115  MUTATE(newnode->arg, convexpr->arg, Expr *);
3116  return (Node *) newnode;
3117  }
3118  break;
3119  case T_CollateExpr:
3120  {
3121  CollateExpr *collate = (CollateExpr *) node;
3122  CollateExpr *newnode;
3123 
3124  FLATCOPY(newnode, collate, CollateExpr);
3125  MUTATE(newnode->arg, collate->arg, Expr *);
3126  return (Node *) newnode;
3127  }
3128  break;
3129  case T_CaseExpr:
3130  {
3131  CaseExpr *caseexpr = (CaseExpr *) node;
3132  CaseExpr *newnode;
3133 
3134  FLATCOPY(newnode, caseexpr, CaseExpr);
3135  MUTATE(newnode->arg, caseexpr->arg, Expr *);
3136  MUTATE(newnode->args, caseexpr->args, List *);
3137  MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
3138  return (Node *) newnode;
3139  }
3140  break;
3141  case T_CaseWhen:
3142  {
3143  CaseWhen *casewhen = (CaseWhen *) node;
3144  CaseWhen *newnode;
3145 
3146  FLATCOPY(newnode, casewhen, CaseWhen);
3147  MUTATE(newnode->expr, casewhen->expr, Expr *);
3148  MUTATE(newnode->result, casewhen->result, Expr *);
3149  return (Node *) newnode;
3150  }
3151  break;
3152  case T_ArrayExpr:
3153  {
3154  ArrayExpr *arrayexpr = (ArrayExpr *) node;
3155  ArrayExpr *newnode;
3156 
3157  FLATCOPY(newnode, arrayexpr, ArrayExpr);
3158  MUTATE(newnode->elements, arrayexpr->elements, List *);
3159  return (Node *) newnode;
3160  }
3161  break;
3162  case T_RowExpr:
3163  {
3164  RowExpr *rowexpr = (RowExpr *) node;
3165  RowExpr *newnode;
3166 
3167  FLATCOPY(newnode, rowexpr, RowExpr);
3168  MUTATE(newnode->args, rowexpr->args, List *);
3169  /* Assume colnames needn't be duplicated */
3170  return (Node *) newnode;
3171  }
3172  break;
3173  case T_RowCompareExpr:
3174  {
3175  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3176  RowCompareExpr *newnode;
3177 
3178  FLATCOPY(newnode, rcexpr, RowCompareExpr);
3179  MUTATE(newnode->largs, rcexpr->largs, List *);
3180  MUTATE(newnode->rargs, rcexpr->rargs, List *);
3181  return (Node *) newnode;
3182  }
3183  break;
3184  case T_CoalesceExpr:
3185  {
3186  CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3187  CoalesceExpr *newnode;
3188 
3189  FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
3190  MUTATE(newnode->args, coalesceexpr->args, List *);
3191  return (Node *) newnode;
3192  }
3193  break;
3194  case T_MinMaxExpr:
3195  {
3196  MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3197  MinMaxExpr *newnode;
3198 
3199  FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
3200  MUTATE(newnode->args, minmaxexpr->args, List *);
3201  return (Node *) newnode;
3202  }
3203  break;
3204  case T_XmlExpr:
3205  {
3206  XmlExpr *xexpr = (XmlExpr *) node;
3207  XmlExpr *newnode;
3208 
3209  FLATCOPY(newnode, xexpr, XmlExpr);
3210  MUTATE(newnode->named_args, xexpr->named_args, List *);
3211  /* assume mutator does not care about arg_names */
3212  MUTATE(newnode->args, xexpr->args, List *);
3213  return (Node *) newnode;
3214  }
3215  break;
3216  case T_JsonReturning:
3217  {
3218  JsonReturning *jr = (JsonReturning *) node;
3219  JsonReturning *newnode;
3220 
3221  FLATCOPY(newnode, jr, JsonReturning);
3222  MUTATE(newnode->format, jr->format, JsonFormat *);
3223 
3224  return (Node *) newnode;
3225  }
3226  case T_JsonValueExpr:
3227  {
3228  JsonValueExpr *jve = (JsonValueExpr *) node;
3229  JsonValueExpr *newnode;
3230 
3231  FLATCOPY(newnode, jve, JsonValueExpr);
3232  MUTATE(newnode->raw_expr, jve->raw_expr, Expr *);
3233  MUTATE(newnode->formatted_expr, jve->formatted_expr, Expr *);
3234  MUTATE(newnode->format, jve->format, JsonFormat *);
3235 
3236  return (Node *) newnode;
3237  }
3238  case T_JsonConstructorExpr:
3239  {
3240  JsonConstructorExpr *jce = (JsonConstructorExpr *) node;
3241  JsonConstructorExpr *newnode;
3242 
3243  FLATCOPY(newnode, jce, JsonConstructorExpr);
3244  MUTATE(newnode->args, jce->args, List *);
3245  MUTATE(newnode->func, jce->func, Expr *);
3246  MUTATE(newnode->coercion, jce->coercion, Expr *);
3247  MUTATE(newnode->returning, jce->returning, JsonReturning *);
3248 
3249  return (Node *) newnode;
3250  }
3251  case T_JsonIsPredicate:
3252  {
3253  JsonIsPredicate *pred = (JsonIsPredicate *) node;
3254  JsonIsPredicate *newnode;
3255 
3256  FLATCOPY(newnode, pred, JsonIsPredicate);
3257  MUTATE(newnode->expr, pred->expr, Node *);
3258  MUTATE(newnode->format, pred->format, JsonFormat *);
3259 
3260  return (Node *) newnode;
3261  }
3262  case T_NullTest:
3263  {
3264  NullTest *ntest = (NullTest *) node;
3265  NullTest *newnode;
3266 
3267  FLATCOPY(newnode, ntest, NullTest);
3268  MUTATE(newnode->arg, ntest->arg, Expr *);
3269  return (Node *) newnode;
3270  }
3271  break;
3272  case T_BooleanTest:
3273  {
3274  BooleanTest *btest = (BooleanTest *) node;
3275  BooleanTest *newnode;
3276 
3277  FLATCOPY(newnode, btest, BooleanTest);
3278  MUTATE(newnode->arg, btest->arg, Expr *);
3279  return (Node *) newnode;
3280  }
3281  break;
3282  case T_CoerceToDomain:
3283  {
3284  CoerceToDomain *ctest = (CoerceToDomain *) node;
3285  CoerceToDomain *newnode;
3286 
3287  FLATCOPY(newnode, ctest, CoerceToDomain);
3288  MUTATE(newnode->arg, ctest->arg, Expr *);
3289  return (Node *) newnode;
3290  }
3291  break;
3292  case T_TargetEntry:
3293  {
3294  TargetEntry *targetentry = (TargetEntry *) node;
3295  TargetEntry *newnode;
3296 
3297  FLATCOPY(newnode, targetentry, TargetEntry);
3298  MUTATE(newnode->expr, targetentry->expr, Expr *);
3299  return (Node *) newnode;
3300  }
3301  break;
3302  case T_Query:
3303  /* Do nothing with a sub-Query, per discussion above */
3304  return node;
3305  case T_WindowClause:
3306  {
3307  WindowClause *wc = (WindowClause *) node;
3308  WindowClause *newnode;
3309 
3310  FLATCOPY(newnode, wc, WindowClause);
3311  MUTATE(newnode->partitionClause, wc->partitionClause, List *);
3312  MUTATE(newnode->orderClause, wc->orderClause, List *);
3313  MUTATE(newnode->startOffset, wc->startOffset, Node *);
3314  MUTATE(newnode->endOffset, wc->endOffset, Node *);
3315  return (Node *) newnode;
3316  }
3317  break;
3318  case T_CTECycleClause:
3319  {
3320  CTECycleClause *cc = (CTECycleClause *) node;
3321  CTECycleClause *newnode;
3322 
3323  FLATCOPY(newnode, cc, CTECycleClause);
3324  MUTATE(newnode->cycle_mark_value, cc->cycle_mark_value, Node *);
3326  return (Node *) newnode;
3327  }
3328  break;
3329  case T_CommonTableExpr:
3330  {
3331  CommonTableExpr *cte = (CommonTableExpr *) node;
3332  CommonTableExpr *newnode;
3333 
3334  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  MUTATE(newnode->ctequery, cte->ctequery, Node *);
3341 
3342  MUTATE(newnode->search_clause, cte->search_clause, CTESearchClause *);
3343  MUTATE(newnode->cycle_clause, cte->cycle_clause, CTECycleClause *);
3344 
3345  return (Node *) newnode;
3346  }
3347  break;
3348  case T_PartitionBoundSpec:
3349  {
3350  PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
3351  PartitionBoundSpec *newnode;
3352 
3353  FLATCOPY(newnode, pbs, PartitionBoundSpec);
3354  MUTATE(newnode->listdatums, pbs->listdatums, List *);
3355  MUTATE(newnode->lowerdatums, pbs->lowerdatums, List *);
3356  MUTATE(newnode->upperdatums, pbs->upperdatums, List *);
3357  return (Node *) newnode;
3358  }
3359  break;
3360  case T_PartitionRangeDatum:
3361  {
3362  PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
3363  PartitionRangeDatum *newnode;
3364 
3365  FLATCOPY(newnode, prd, PartitionRangeDatum);
3366  MUTATE(newnode->value, prd->value, Node *);
3367  return (Node *) newnode;
3368  }
3369  break;
3370  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  resultlist = NIL;
3381  foreach(temp, (List *) node)
3382  {
3383  resultlist = lappend(resultlist,
3384  mutator((Node *) lfirst(temp),
3385  context));
3386  }
3387  return (Node *) resultlist;
3388  }
3389  break;
3390  case T_FromExpr:
3391  {
3392  FromExpr *from = (FromExpr *) node;
3393  FromExpr *newnode;
3394 
3395  FLATCOPY(newnode, from, FromExpr);
3396  MUTATE(newnode->fromlist, from->fromlist, List *);
3397  MUTATE(newnode->quals, from->quals, Node *);
3398  return (Node *) newnode;
3399  }
3400  break;
3401  case T_OnConflictExpr:
3402  {
3403  OnConflictExpr *oc = (OnConflictExpr *) node;
3404  OnConflictExpr *newnode;
3405 
3406  FLATCOPY(newnode, oc, OnConflictExpr);
3407  MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
3408  MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
3409  MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
3410  MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
3411  MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
3412 
3413  return (Node *) newnode;
3414  }
3415  break;
3416  case T_MergeAction:
3417  {
3418  MergeAction *action = (MergeAction *) node;
3419  MergeAction *newnode;
3420 
3421  FLATCOPY(newnode, action, MergeAction);
3422  MUTATE(newnode->qual, action->qual, Node *);
3423  MUTATE(newnode->targetList, action->targetList, List *);
3424 
3425  return (Node *) newnode;
3426  }
3427  break;
3428  case T_PartitionPruneStepOp:
3429  {
3430  PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
3431  PartitionPruneStepOp *newnode;
3432 
3433  FLATCOPY(newnode, opstep, PartitionPruneStepOp);
3434  MUTATE(newnode->exprs, opstep->exprs, List *);
3435 
3436  return (Node *) newnode;
3437  }
3438  break;
3439  case T_PartitionPruneStepCombine:
3440  /* no expression sub-nodes */
3441  return (Node *) copyObject(node);
3442  case T_JoinExpr:
3443  {
3444  JoinExpr *join = (JoinExpr *) node;
3445  JoinExpr *newnode;
3446 
3447  FLATCOPY(newnode, join, JoinExpr);
3448  MUTATE(newnode->larg, join->larg, Node *);
3449  MUTATE(newnode->rarg, join->rarg, Node *);
3450  MUTATE(newnode->quals, join->quals, Node *);
3451  /* We do not mutate alias or using by default */
3452  return (Node *) newnode;
3453  }
3454  break;
3455  case T_SetOperationStmt:
3456  {
3457  SetOperationStmt *setop = (SetOperationStmt *) node;
3458  SetOperationStmt *newnode;
3459 
3460  FLATCOPY(newnode, setop, SetOperationStmt);
3461  MUTATE(newnode->larg, setop->larg, Node *);
3462  MUTATE(newnode->rarg, setop->rarg, Node *);
3463  /* We do not mutate groupClauses by default */
3464  return (Node *) newnode;
3465  }
3466  break;
3467  case T_IndexClause:
3468  {
3469  IndexClause *iclause = (IndexClause *) node;
3470  IndexClause *newnode;
3471 
3472  FLATCOPY(newnode, iclause, IndexClause);
3473  MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
3474  MUTATE(newnode->indexquals, iclause->indexquals, List *);
3475  return (Node *) newnode;
3476  }
3477  break;
3478  case T_PlaceHolderVar:
3479  {
3480  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3481  PlaceHolderVar *newnode;
3482 
3483  FLATCOPY(newnode, phv, PlaceHolderVar);
3484  MUTATE(newnode->phexpr, phv->phexpr, Expr *);
3485  /* Assume we need not copy the relids bitmapsets */
3486  return (Node *) newnode;
3487  }
3488  break;
3489  case T_InferenceElem:
3490  {
3491  InferenceElem *inferenceelemdexpr = (InferenceElem *) node;
3492  InferenceElem *newnode;
3493 
3494  FLATCOPY(newnode, inferenceelemdexpr, InferenceElem);
3495  MUTATE(newnode->expr, newnode->expr, Node *);
3496  return (Node *) newnode;
3497  }
3498  break;
3499  case T_AppendRelInfo:
3500  {
3501  AppendRelInfo *appinfo = (AppendRelInfo *) node;
3502  AppendRelInfo *newnode;
3503 
3504  FLATCOPY(newnode, appinfo, AppendRelInfo);
3505  MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
3506  /* Assume nothing need be done with parent_colnos[] */
3507  return (Node *) newnode;
3508  }
3509  break;
3510  case T_PlaceHolderInfo:
3511  {
3512  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
3513  PlaceHolderInfo *newnode;
3514 
3515  FLATCOPY(newnode, phinfo, PlaceHolderInfo);
3516  MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
3517  /* Assume we need not copy the relids bitmapsets */
3518  return (Node *) newnode;
3519  }
3520  break;
3521  case T_RangeTblFunction:
3522  {
3523  RangeTblFunction *rtfunc = (RangeTblFunction *) node;
3524  RangeTblFunction *newnode;
3525 
3526  FLATCOPY(newnode, rtfunc, RangeTblFunction);
3527  MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
3528  /* Assume we need not copy the coldef info lists */
3529  return (Node *) newnode;
3530  }
3531  break;
3532  case T_TableSampleClause:
3533  {
3534  TableSampleClause *tsc = (TableSampleClause *) node;
3535  TableSampleClause *newnode;
3536 
3537  FLATCOPY(newnode, tsc, TableSampleClause);
3538  MUTATE(newnode->args, tsc->args, List *);
3539  MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
3540  return (Node *) newnode;
3541  }
3542  break;
3543  case T_TableFunc:
3544  {
3545  TableFunc *tf = (TableFunc *) node;
3546  TableFunc *newnode;
3547 
3548  FLATCOPY(newnode, tf, TableFunc);
3549  MUTATE(newnode->ns_uris, tf->ns_uris, List *);
3550  MUTATE(newnode->docexpr, tf->docexpr, Node *);
3551  MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
3552  MUTATE(newnode->colexprs, tf->colexprs, List *);
3553  MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
3554  return (Node *) newnode;
3555  }
3556  break;
3557  default:
3558  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 *
3588  tree_mutator_callback mutator,
3589  void *context,
3590  int flags)
3591 {
3592  Assert(query != NULL && IsA(query, Query));
3593 
3594  if (!(flags & QTW_DONT_COPY_QUERY))
3595  {
3596  Query *newquery;
3597 
3598  FLATCOPY(newquery, query, Query);
3599  query = newquery;
3600  }
3601 
3602  MUTATE(query->targetList, query->targetList, List *);
3603  MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
3604  MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
3605  MUTATE(query->mergeActionList, query->mergeActionList, List *);
3606  MUTATE(query->returningList, query->returningList, List *);
3607  MUTATE(query->jointree, query->jointree, FromExpr *);
3608  MUTATE(query->setOperations, query->setOperations, Node *);
3609  MUTATE(query->havingQual, query->havingQual, Node *);
3610  MUTATE(query->limitOffset, query->limitOffset, Node *);
3611  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  if ((flags & QTW_EXAMINE_SORTGROUP))
3620  {
3621  MUTATE(query->groupClause, query->groupClause, List *);
3622  MUTATE(query->windowClause, query->windowClause, List *);
3623  MUTATE(query->sortClause, query->sortClause, List *);
3624  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  resultlist = NIL;
3636  foreach(temp, query->windowClause)
3637  {
3638  WindowClause *wc = lfirst_node(WindowClause, temp);
3639  WindowClause *newnode;
3640 
3641  FLATCOPY(newnode, wc, WindowClause);
3642  MUTATE(newnode->startOffset, wc->startOffset, Node *);
3643  MUTATE(newnode->endOffset, wc->endOffset, Node *);
3644 
3645  resultlist = lappend(resultlist, (Node *) newnode);
3646  }
3647  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  if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
3663  MUTATE(query->cteList, query->cteList, List *);
3664  else /* else copy CTE list as-is */
3665  query->cteList = copyObject(query->cteList);
3666  query->rtable = range_table_mutator(query->rtable,
3667  mutator, context, flags);
3668  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 *
3678  tree_mutator_callback mutator,
3679  void *context,
3680  int flags)
3681 {
3682  List *newrt = NIL;
3683  ListCell *rt;
3684 
3685  foreach(rt, rtable)
3686  {
3687  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3688  RangeTblEntry *newrte;
3689 
3690  FLATCOPY(newrte, rte, RangeTblEntry);
3691  switch (rte->rtekind)
3692  {
3693  case RTE_RELATION:
3694  MUTATE(newrte->tablesample, rte->tablesample,
3695  TableSampleClause *);
3696  /* we don't bother to copy eref, aliases, etc; OK? */
3697  break;
3698  case RTE_SUBQUERY:
3699  if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3700  MUTATE(newrte->subquery, rte->subquery, Query *);
3701  else
3702  {
3703  /* else, copy RT subqueries as-is */
3704  newrte->subquery = copyObject(rte->subquery);
3705  }
3706  break;
3707  case RTE_JOIN:
3708  if (!(flags & QTW_IGNORE_JOINALIASES))
3709  MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
3710  else
3711  {
3712  /* else, copy join aliases as-is */
3713  newrte->joinaliasvars = copyObject(rte->joinaliasvars);
3714  }
3715  break;
3716  case RTE_FUNCTION:
3717  MUTATE(newrte->functions, rte->functions, List *);
3718  break;
3719  case RTE_TABLEFUNC:
3720  MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
3721  break;
3722  case RTE_VALUES:
3723  MUTATE(newrte->values_lists, rte->values_lists, List *);
3724  break;
3725  case RTE_CTE:
3726  case RTE_NAMEDTUPLESTORE:
3727  case RTE_RESULT:
3728  /* nothing to do */
3729  break;
3730  }
3731  MUTATE(newrte->securityQuals, rte->securityQuals, List *);
3732  newrt = lappend(newrt, newrte);
3733  }
3734  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
3747  tree_walker_callback walker,
3748  void *context,
3749  int flags)
3750 {
3751  if (node && IsA(node, Query))
3752  return query_tree_walker((Query *) node,
3753  walker,
3754  context,
3755  flags);
3756  else
3757  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 *
3770  tree_mutator_callback mutator,
3771  void *context,
3772  int flags)
3773 {
3774  if (node && IsA(node, Query))
3775  return (Node *) query_tree_mutator((Query *) node,
3776  mutator,
3777  context,
3778  flags);
3779  else
3780  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
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  if (node == NULL)
3811  return false;
3812 
3813  /* Guard against stack overflow due to overly complex expressions */
3815 
3816  switch (nodeTag(node))
3817  {
3818  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  break;
3832  case T_Alias:
3833  /* we assume the colnames list isn't interesting */
3834  break;
3835  case T_RangeVar:
3836  return WALK(((RangeVar *) node)->alias);
3837  case T_GroupingFunc:
3838  return WALK(((GroupingFunc *) node)->args);
3839  case T_SubLink:
3840  {
3841  SubLink *sublink = (SubLink *) node;
3842 
3843  if (WALK(sublink->testexpr))
3844  return true;
3845  /* we assume the operName is not interesting */
3846  if (WALK(sublink->subselect))
3847  return true;
3848  }
3849  break;
3850  case T_CaseExpr:
3851  {
3852  CaseExpr *caseexpr = (CaseExpr *) node;
3853 
3854  if (WALK(caseexpr->arg))
3855  return true;
3856  /* we assume walker doesn't care about CaseWhens, either */
3857  foreach(temp, caseexpr->args)
3858  {
3859  CaseWhen *when = lfirst_node(CaseWhen, temp);
3860 
3861  if (WALK(when->expr))
3862  return true;
3863  if (WALK(when->result))
3864  return true;
3865  }
3866  if (WALK(caseexpr->defresult))
3867  return true;
3868  }
3869  break;
3870  case T_RowExpr:
3871  /* Assume colnames isn't interesting */
3872  return WALK(((RowExpr *) node)->args);
3873  case T_CoalesceExpr:
3874  return WALK(((CoalesceExpr *) node)->args);
3875  case T_MinMaxExpr:
3876  return WALK(((MinMaxExpr *) node)->args);
3877  case T_XmlExpr:
3878  {
3879  XmlExpr *xexpr = (XmlExpr *) node;
3880 
3881  if (WALK(xexpr->named_args))
3882  return true;
3883  /* we assume walker doesn't care about arg_names */
3884  if (WALK(xexpr->args))
3885  return true;
3886  }
3887  break;
3888  case T_JsonReturning:
3889  return WALK(((JsonReturning *) node)->format);
3890  case T_JsonValueExpr:
3891  {
3892  JsonValueExpr *jve = (JsonValueExpr *) node;
3893 
3894  if (WALK(jve->raw_expr))
3895  return true;
3896  if (WALK(jve->formatted_expr))
3897  return true;
3898  if (WALK(jve->format))
3899  return true;
3900  }
3901  break;
3902  case T_JsonParseExpr:
3903  {
3904  JsonParseExpr *jpe = (JsonParseExpr *) node;
3905 
3906  if (WALK(jpe->expr))
3907  return true;
3908  if (WALK(jpe->output))
3909  return true;
3910  }
3911  break;
3912  case T_JsonScalarExpr:
3913  {
3914  JsonScalarExpr *jse = (JsonScalarExpr *) node;
3915 
3916  if (WALK(jse->expr))
3917  return true;
3918  if (WALK(jse->output))
3919  return true;
3920  }
3921  break;
3922  case T_JsonSerializeExpr:
3923  {
3924  JsonSerializeExpr *jse = (JsonSerializeExpr *) node;
3925 
3926  if (WALK(jse->expr))
3927  return true;
3928  if (WALK(jse->output))
3929  return true;
3930  }
3931  break;
3932  case T_JsonConstructorExpr:
3933  {
3934  JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
3935 
3936  if (WALK(ctor->args))
3937  return true;
3938  if (WALK(ctor->func))
3939  return true;
3940  if (WALK(ctor->coercion))
3941  return true;
3942  if (WALK(ctor->returning))
3943  return true;
3944  }
3945  break;
3946  case T_JsonIsPredicate:
3947  return WALK(((JsonIsPredicate *) node)->expr);
3948  case T_NullTest:
3949  return WALK(((NullTest *) node)->arg);
3950  case T_BooleanTest:
3951  return WALK(((BooleanTest *) node)->arg);
3952  case T_JoinExpr:
3953  {
3954  JoinExpr *join = (JoinExpr *) node;
3955 
3956  if (WALK(join->larg))
3957  return true;
3958  if (WALK(join->rarg))
3959  return true;
3960  if (WALK(join->quals))
3961  return true;
3962  if (WALK(join->alias))
3963  return true;
3964  /* using list is deemed uninteresting */
3965  }
3966  break;
3967  case T_IntoClause:
3968  {
3969  IntoClause *into = (IntoClause *) node;
3970 
3971  if (WALK(into->rel))
3972  return true;
3973  /* colNames, options are deemed uninteresting */
3974  /* viewQuery should be null in raw parsetree, but check it */
3975  if (WALK(into->viewQuery))
3976  return true;
3977  }
3978  break;
3979  case T_List:
3980  foreach(temp, (List *) node)
3981  {
3982  if (WALK((Node *) lfirst(temp)))
3983  return true;
3984  }
3985  break;
3986  case T_InsertStmt:
3987  {
3988  InsertStmt *stmt = (InsertStmt *) node;
3989 
3990  if (WALK(stmt->relation))
3991  return true;
3992  if (WALK(stmt->cols))
3993  return true;
3994  if (WALK(stmt->selectStmt))
3995  return true;
3996  if (WALK(stmt->onConflictClause))
3997  return true;
3998  if (WALK(stmt->returningList))
3999  return true;
4000  if (WALK(stmt->withClause))
4001  return true;
4002  }
4003  break;
4004  case T_DeleteStmt:
4005  {
4006  DeleteStmt *stmt = (DeleteStmt *) node;
4007 
4008  if (WALK(stmt->relation))
4009  return true;
4010  if (WALK(stmt->usingClause))
4011  return true;
4012  if (WALK(stmt->whereClause))
4013  return true;
4014  if (WALK(stmt->returningList))
4015  return true;
4016  if (WALK(stmt->withClause))
4017  return true;
4018  }
4019  break;
4020  case T_UpdateStmt:
4021  {
4022  UpdateStmt *stmt = (UpdateStmt *) node;
4023 
4024  if (WALK(stmt->relation))
4025  return true;
4026  if (WALK(stmt->targetList))
4027  return true;
4028  if (WALK(stmt->whereClause))
4029  return true;
4030  if (WALK(stmt->fromClause))
4031  return true;
4032  if (WALK(stmt->returningList))
4033  return true;
4034  if (WALK(stmt->withClause))
4035  return true;
4036  }
4037  break;
4038  case T_MergeStmt:
4039  {
4040  MergeStmt *stmt = (MergeStmt *) node;
4041 
4042  if (WALK(stmt->relation))
4043  return true;
4044  if (WALK(stmt->sourceRelation))
4045  return true;
4046  if (WALK(stmt->joinCondition))
4047  return true;
4048  if (WALK(stmt->mergeWhenClauses))
4049  return true;
4050  if (WALK(stmt->withClause))
4051  return true;
4052  }
4053  break;
4054  case T_MergeWhenClause:
4055  {
4056  MergeWhenClause *mergeWhenClause = (MergeWhenClause *) node;
4057 
4058  if (WALK(mergeWhenClause->condition))
4059  return true;
4060  if (WALK(mergeWhenClause->targetList))
4061  return true;
4062  if (WALK(mergeWhenClause->values))
4063  return true;
4064  }
4065  break;
4066  case T_SelectStmt:
4067  {
4068  SelectStmt *stmt = (SelectStmt *) node;
4069 
4070  if (WALK(stmt->distinctClause))
4071  return true;
4072  if (WALK(stmt->intoClause))
4073  return true;
4074  if (WALK(stmt->targetList))
4075  return true;
4076  if (WALK(stmt->fromClause))
4077  return true;
4078  if (WALK(stmt->whereClause))
4079  return true;
4080  if (WALK(stmt->groupClause))
4081  return true;
4082  if (WALK(stmt->havingClause))
4083  return true;
4084  if (WALK(stmt->windowClause))
4085  return true;
4086  if (WALK(stmt->valuesLists))
4087  return true;
4088  if (WALK(stmt->sortClause))
4089  return true;
4090  if (WALK(stmt->limitOffset))
4091  return true;
4092  if (WALK(stmt->limitCount))
4093  return true;
4094  if (WALK(stmt->lockingClause))
4095  return true;
4096  if (WALK(stmt->withClause))
4097  return true;
4098  if (WALK(stmt->larg))
4099  return true;
4100  if (WALK(stmt->rarg))
4101  return true;
4102  }
4103  break;
4104  case T_PLAssignStmt:
4105  {
4106  PLAssignStmt *stmt = (PLAssignStmt *) node;
4107 
4108  if (WALK(stmt->indirection))
4109  return true;
4110  if (WALK(stmt->val))
4111  return true;
4112  }
4113  break;
4114  case T_A_Expr:
4115  {
4116  A_Expr *expr = (A_Expr *) node;
4117 
4118  if (WALK(expr->lexpr))
4119  return true;
4120  if (WALK(expr->rexpr))
4121  return true;
4122  /* operator name is deemed uninteresting */
4123  }
4124  break;
4125  case T_BoolExpr:
4126  {
4127  BoolExpr *expr = (BoolExpr *) node;
4128 
4129  if (WALK(expr->args))
4130  return true;
4131  }
4132  break;
4133  case T_ColumnRef:
4134  /* we assume the fields contain nothing interesting */
4135  break;
4136  case T_FuncCall:
4137  {
4138  FuncCall *fcall = (FuncCall *) node;
4139 
4140  if (WALK(fcall->args))
4141  return true;
4142  if (WALK(fcall->agg_order))
4143  return true;
4144  if (WALK(fcall->agg_filter))
4145  return true;
4146  if (WALK(fcall->over))
4147  return true;
4148  /* function name is deemed uninteresting */
4149  }
4150  break;
4151  case T_NamedArgExpr:
4152  return WALK(((NamedArgExpr *) node)->arg);
4153  case T_A_Indices:
4154  {
4155  A_Indices *indices = (A_Indices *) node;
4156 
4157  if (WALK(indices->lidx))
4158  return true;
4159  if (WALK(indices->uidx))
4160  return true;
4161  }
4162  break;
4163  case T_A_Indirection:
4164  {
4165  A_Indirection *indir = (A_Indirection *) node;
4166 
4167  if (WALK(indir->arg))
4168  return true;
4169  if (WALK(indir->indirection))
4170  return true;
4171  }
4172  break;
4173  case T_A_ArrayExpr:
4174  return WALK(((A_ArrayExpr *) node)->elements);
4175  case T_ResTarget:
4176  {
4177  ResTarget *rt = (ResTarget *) node;
4178 
4179  if (WALK(rt->indirection))
4180  return true;
4181  if (WALK(rt->val))
4182  return true;
4183  }
4184  break;
4185  case T_MultiAssignRef:
4186  return WALK(((MultiAssignRef *) node)->source);
4187  case T_TypeCast:
4188  {
4189  TypeCast *tc = (TypeCast *) node;
4190 
4191  if (WALK(tc->arg))
4192  return true;
4193  if (WALK(tc->typeName))
4194  return true;
4195  }
4196  break;
4197  case T_CollateClause:
4198  return WALK(((CollateClause *) node)->arg);
4199  case T_SortBy:
4200  return WALK(((SortBy *) node)->node);
4201  case T_WindowDef:
4202  {
4203  WindowDef *wd = (WindowDef *) node;
4204 
4205  if (WALK(wd->partitionClause))
4206  return true;
4207  if (WALK(wd->orderClause))
4208  return true;
4209  if (WALK(wd->startOffset))
4210  return true;
4211  if (WALK(wd->endOffset))
4212  return true;
4213  }
4214  break;
4215  case T_RangeSubselect:
4216  {
4217  RangeSubselect *rs = (RangeSubselect *) node;
4218 
4219  if (WALK(rs->subquery))
4220  return true;
4221  if (WALK(rs->alias))
4222  return true;
4223  }
4224  break;
4225  case T_RangeFunction:
4226  {
4227  RangeFunction *rf = (RangeFunction *) node;
4228 
4229  if (WALK(rf->functions))
4230  return true;
4231  if (WALK(rf->alias))
4232  return true;
4233  if (WALK(rf->coldeflist))
4234  return true;
4235  }
4236  break;
4237  case T_RangeTableSample:
4238  {
4239  RangeTableSample *rts = (RangeTableSample *) node;
4240 
4241  if (WALK(rts->relation))
4242  return true;
4243  /* method name is deemed uninteresting */
4244  if (WALK(rts->args))
4245  return true;
4246  if (WALK(rts->repeatable))
4247  return true;
4248  }
4249  break;
4250  case T_RangeTableFunc:
4251  {
4252  RangeTableFunc *rtf = (RangeTableFunc *) node;
4253 
4254  if (WALK(rtf->docexpr))
4255  return true;
4256  if (WALK(rtf->rowexpr))
4257  return true;
4258  if (WALK(rtf->namespaces))
4259  return true;
4260  if (WALK(rtf->columns))
4261  return true;
4262  if (WALK(rtf->alias))
4263  return true;
4264  }
4265  break;
4266  case T_RangeTableFuncCol:
4267  {
4268  RangeTableFuncCol *rtfc = (RangeTableFuncCol *) node;
4269 
4270  if (WALK(rtfc->colexpr))
4271  return true;
4272  if (WALK(rtfc->coldefexpr))
4273  return true;
4274  }
4275  break;
4276  case T_TypeName:
4277  {
4278  TypeName *tn = (TypeName *) node;
4279 
4280  if (WALK(tn->typmods))
4281  return true;
4282  if (WALK(tn->arrayBounds))
4283  return true;
4284  /* type name itself is deemed uninteresting */
4285  }
4286  break;
4287  case T_ColumnDef:
4288  {
4289  ColumnDef *coldef = (ColumnDef *) node;
4290 
4291  if (WALK(coldef->typeName))
4292  return true;
4293  if (WALK(coldef->raw_default))
4294  return true;
4295  if (WALK(coldef->collClause))
4296  return true;
4297  /* for now, constraints are ignored */
4298  }
4299  break;
4300  case T_IndexElem:
4301  {
4302  IndexElem *indelem = (IndexElem *) node;
4303 
4304  if (WALK(indelem->expr))
4305  return true;
4306  /* collation and opclass names are deemed uninteresting */
4307  }
4308  break;
4309  case T_GroupingSet:
4310  return WALK(((GroupingSet *) node)->content);
4311  case T_LockingClause:
4312  return WALK(((LockingClause *) node)->lockedRels);
4313  case T_XmlSerialize:
4314  {
4315  XmlSerialize *xs = (XmlSerialize *) node;
4316 
4317  if (WALK(xs->expr))
4318  return true;
4319  if (WALK(xs->typeName))
4320  return true;
4321  }
4322  break;
4323  case T_WithClause:
4324  return WALK(((WithClause *) node)->ctes);
4325  case T_InferClause:
4326  {
4327  InferClause *stmt = (InferClause *) node;
4328 
4329  if (WALK(stmt->indexElems))
4330  return true;
4331  if (WALK(stmt->whereClause))
4332  return true;
4333  }
4334  break;
4335  case T_OnConflictClause:
4336  {
4338 
4339  if (WALK(stmt->infer))
4340  return true;
4341  if (WALK(stmt->targetList))
4342  return true;
4343  if (WALK(stmt->whereClause))
4344  return true;
4345  }
4346  break;
4347  case T_CommonTableExpr:
4348  /* search_clause and cycle_clause are not interesting here */
4349  return WALK(((CommonTableExpr *) node)->ctequery);
4350  case T_JsonOutput:
4351  {
4352  JsonOutput *out = (JsonOutput *) node;
4353 
4354  if (WALK(out->typeName))
4355  return true;
4356  if (WALK(out->returning))
4357  return true;
4358  }
4359  break;
4360  case T_JsonKeyValue:
4361  {
4362  JsonKeyValue *jkv = (JsonKeyValue *) node;
4363 
4364  if (WALK(jkv->key))
4365  return true;
4366  if (WALK(jkv->value))
4367  return true;
4368  }
4369  break;
4370  case T_JsonObjectConstructor:
4371  {
4373 
4374  if (WALK(joc->output))
4375  return true;
4376  if (WALK(joc->exprs))
4377  return true;
4378  }
4379  break;
4380  case T_JsonArrayConstructor:
4381  {
4383 
4384  if (WALK(jac->output))
4385  return true;
4386  if (WALK(jac->exprs))
4387  return true;
4388  }
4389  break;
4390  case T_JsonAggConstructor:
4391  {
4392  JsonAggConstructor *ctor = (JsonAggConstructor *) node;
4393 
4394  if (WALK(ctor->output))
4395  return true;
4396  if (WALK(ctor->agg_order))
4397  return true;
4398  if (WALK(ctor->agg_filter))
4399  return true;
4400  if (WALK(ctor->over))
4401  return true;
4402  }
4403  break;
4404  case T_JsonObjectAgg:
4405  {
4406  JsonObjectAgg *joa = (JsonObjectAgg *) node;
4407 
4408  if (WALK(joa->constructor))
4409  return true;
4410  if (WALK(joa->arg))
4411  return true;
4412  }
4413  break;
4414  case T_JsonArrayAgg:
4415  {
4416  JsonArrayAgg *jaa = (JsonArrayAgg *) node;
4417 
4418  if (WALK(jaa->constructor))
4419  return true;
4420  if (WALK(jaa->arg))
4421  return true;
4422  }
4423  break;
4424  case T_JsonArrayQueryConstructor:
4425  {
4427 
4428  if (WALK(jaqc->output))
4429  return true;
4430  if (WALK(jaqc->query))
4431  return true;
4432  }
4433  break;
4434  default:
4435  elog(ERROR, "unrecognized node type: %d",
4436  (int) nodeTag(node));
4437  break;
4438  }
4439  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
4451  void *context)
4452 {
4453  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 */
4461 
4462  /* initPlan-s */
4463  if (planstate_walk_subplans(planstate->initPlan, walker, context))
4464  return true;
4465 
4466  /* lefttree */
4467  if (outerPlanState(planstate))
4468  {
4469  if (PSWALK(outerPlanState(planstate)))
4470  return true;
4471  }
4472 
4473  /* righttree */
4474  if (innerPlanState(planstate))
4475  {
4476  if (PSWALK(innerPlanState(planstate)))
4477  return true;
4478  }
4479 
4480  /* special child plans */
4481  switch (nodeTag(plan))
4482  {
4483  case T_Append:
4484  if (planstate_walk_members(((AppendState *) planstate)->appendplans,
4485  ((AppendState *) planstate)->as_nplans,
4486  walker, context))
4487  return true;
4488  break;
4489  case T_MergeAppend:
4490  if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
4491  ((MergeAppendState *) planstate)->ms_nplans,
4492  walker, context))
4493  return true;
4494  break;
4495  case T_BitmapAnd:
4496  if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
4497  ((BitmapAndState *) planstate)->nplans,
4498  walker, context))
4499  return true;
4500  break;
4501  case T_BitmapOr:
4502  if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
4503  ((BitmapOrState *) planstate)->nplans,
4504  walker, context))
4505  return true;
4506  break;
4507  case T_SubqueryScan:
4508  if (PSWALK(((SubqueryScanState *) planstate)->subplan))
4509  return true;
4510  break;
4511  case T_CustomScan:
4512  foreach(lc, ((CustomScanState *) planstate)->custom_ps)
4513  {
4514  if (PSWALK(lfirst(lc)))
4515  return true;
4516  }
4517  break;
4518  default:
4519  break;
4520  }
4521 
4522  /* subPlan-s */
4523  if (planstate_walk_subplans(planstate->subPlan, walker, context))
4524  return true;
4525 
4526  return false;
4527 }
4528 
4529 /*
4530  * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
4531  */
4532 static bool
4535  void *context)
4536 {
4537  ListCell *lc;
4538 
4539  foreach(lc, plans)
4540  {
4542 
4543  if (PSWALK(sps->planstate))
4544  return true;
4545  }
4546 
4547  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 planstate_walk_members(PlanState **planstates, int nplans,
4557  void *context)
4558 {
4559  int j;
4560 
4561  for (j = 0; j < nplans; j++)
4562  {
4563  if (PSWALK(planstates[j]))
4564  return true;
4565  }
4566 
4567  return false;
4568 }
#define Min(x, y)
Definition: c.h:993
signed int int32
Definition: c.h:483
#define OidIsValid(objectId)
Definition: c.h:764
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define outerPlanState(node)
Definition: execnodes.h:1133
#define innerPlanState(node)
Definition: execnodes.h:1132
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define stmt
Definition: indent_codes.h:59
int j
Definition: isn.c:74
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
List * list_copy(const List *oldlist)
Definition: list.c:1572
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2889
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1289
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2856
Oid get_promoted_array_type(Oid typid)
Definition: lsyscache.c:2793
bool query_tree_walker_impl(Query *query, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:2563
#define FLATCOPY(newnode, node, nodetype)
#define LIST_WALK(l)
bool raw_expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:3800
Node * query_or_expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *context, int flags)
Definition: nodeFuncs.c:3769
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
bool range_table_entry_walker_impl(RangeTblEntry *rte, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:2685
Query * query_tree_mutator_impl(Query *query, tree_mutator_callback mutator, void *context, int flags)
Definition: nodeFuncs.c:3587
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:522
void exprSetCollation(Node *expr, Oid collation)
Definition: nodeFuncs.c:1066
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:1018
#define WALK(n)
Node * expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *context)
Definition: nodeFuncs.c:2808
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:282
static bool planstate_walk_subplans(List *plans, planstate_tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:4533
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1817
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:786
static bool fix_opfuncids_walker(Node *node, void *context)
Definition: nodeFuncs.c:1755
void exprSetInputCollation(Node *expr, Oid inputcollation)
Definition: nodeFuncs.c:1248
bool query_or_expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:3746
List * range_table_mutator_impl(List *rtable, tree_mutator_callback mutator, void *context, int flags)
Definition: nodeFuncs.c:3677
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
Definition: nodeFuncs.c:601
bool expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:2000
bool range_table_walker_impl(List *rtable, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:2664
#define MUTATE(newfield, oldfield, fieldtype)
#define PSWALK(n)
static int leftmostLoc(int loc1, int loc2)
Definition: nodeFuncs.c:1727
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:654
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1312
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:670
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:728
bool planstate_tree_walker_impl(PlanState *planstate, planstate_tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:4449
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1748
static bool expression_returns_set_walker(Node *node, void *context)
Definition: nodeFuncs.c:734
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1790
static bool planstate_walk_members(PlanState **planstates, int nplans, planstate_tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:4555
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1779
#define QTW_DONT_COPY_QUERY
Definition: nodeFuncs.h:29
#define QTW_IGNORE_CTE_SUBQUERIES
Definition: nodeFuncs.h:23
#define QTW_IGNORE_RT_SUBQUERIES
Definition: nodeFuncs.h:22
#define range_table_walker(rt, w, c, f)
Definition: nodeFuncs.h:161
bool(* planstate_tree_walker_callback)(struct PlanState *planstate, void *context)
Definition: nodeFuncs.h:37
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156
#define range_table_entry_walker(r, w, c, f)
Definition: nodeFuncs.h:166
#define QTW_EXAMINE_RTES_AFTER
Definition: nodeFuncs.h:28
#define QTW_EXAMINE_SORTGROUP
Definition: nodeFuncs.h:30
bool(* tree_walker_callback)(Node *node, void *context)
Definition: nodeFuncs.h:36
Node *(* tree_mutator_callback)(Node *node, void *context)
Definition: nodeFuncs.h:41
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define query_tree_mutator(q, m, c, f)
Definition: nodeFuncs.h:158
bool(* check_function_callback)(Oid func_id, void *context)
Definition: nodeFuncs.h:33
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:27
#define range_table_mutator(rt, m, c, f)
Definition: nodeFuncs.h:163
#define QTW_IGNORE_RANGE_TABLE
Definition: nodeFuncs.h:26
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:25
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define copyObject(obj)
Definition: nodes.h:244
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define makeNode(_type_)
Definition: nodes.h:176
@ RTE_JOIN
Definition: parsenodes.h:1015
@ RTE_CTE
Definition: parsenodes.h:1019
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1020
@ RTE_VALUES
Definition: parsenodes.h:1018
@ RTE_SUBQUERY
Definition: parsenodes.h:1014
@ RTE_RESULT
Definition: parsenodes.h:1021
@ RTE_FUNCTION
Definition: parsenodes.h:1016
@ RTE_TABLEFUNC
Definition: parsenodes.h:1017
@ RTE_RELATION
Definition: parsenodes.h:1013
void * arg
static char format
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
#define NIL
Definition: pg_list.h:68
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define plan(x)
Definition: pg_regress.c:154
static rewind_source * source
Definition: pg_rewind.c:89
void check_stack_depth(void)
Definition: postgres.c:3523
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * c
e
Definition: preproc-init.c:82
static int fc(const char *x)
Definition: preproc-init.c:99
@ ARRAY_SUBLINK
Definition: primnodes.h:930
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:929
@ EXPR_SUBLINK
Definition: primnodes.h:928
@ IS_DOCUMENT
Definition: primnodes.h:1514
@ IS_XMLSERIALIZE
Definition: primnodes.h:1513
CoercionForm
Definition: primnodes.h:660
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:663
@ COERCE_EXPLICIT_CAST
Definition: primnodes.h:662
int location
Definition: parsenodes.h:335
Node * lexpr
Definition: parsenodes.h:333
Node * rexpr
Definition: parsenodes.h:334
Node * uidx
Definition: parsenodes.h:458
Node * lidx
Definition: parsenodes.h:457
List * indirection
Definition: parsenodes.h:480
Oid aggfnoid
Definition: primnodes.h:422
List * aggdistinct
Definition: primnodes.h:452
List * aggdirectargs
Definition: primnodes.h:443
List * args
Definition: primnodes.h:446
Expr * aggfilter
Definition: primnodes.h:455
List * aggorder
Definition: primnodes.h:449
List * translated_vars
Definition: pathnodes.h:2956
List * elements
Definition: primnodes.h:1307
int location
Definition: primnodes.h:868
List * args
Definition: primnodes.h:867
Expr * arg
Definition: primnodes.h:1716
Node * cycle_mark_default
Definition: parsenodes.h:1613
Node * cycle_mark_value
Definition: parsenodes.h:1612
Expr * arg
Definition: primnodes.h:1240
Expr * defresult
Definition: primnodes.h:1242
List * args
Definition: primnodes.h:1241
Expr * result
Definition: primnodes.h:1253
Expr * expr
Definition: primnodes.h:1252
List * args
Definition: primnodes.h:1419
Expr * arg
Definition: primnodes.h:1134
Oid resulttype
Definition: primnodes.h:1135
Expr * arg
Definition: primnodes.h:1206
CollateClause * collClause
Definition: parsenodes.h:735
TypeName * typeName
Definition: parsenodes.h:721
Node * raw_default
Definition: parsenodes.h:729
Oid consttype
Definition: primnodes.h:290
Expr * arg
Definition: primnodes.h:1055
List * newvals
Definition: primnodes.h:1087
Expr * arg
Definition: primnodes.h:1086
Node * quals
Definition: primnodes.h:2014
List * fromlist
Definition: primnodes.h:2013
Node * agg_filter
Definition: parsenodes.h:426
List * agg_order
Definition: parsenodes.h:425
List * args
Definition: parsenodes.h:424
struct WindowDef * over
Definition: parsenodes.h:427
Oid funcid
Definition: primnodes.h:677
List * args
Definition: primnodes.h:695
int location
Definition: primnodes.h:697
List * indexquals
Definition: pathnodes.h:1727
struct RestrictInfo * rinfo
Definition: pathnodes.h:1726
Node * expr
Definition: parsenodes.h:778
RangeVar * rel
Definition: primnodes.h:140
Node * quals
Definition: primnodes.h:1994
Node * larg
Definition: primnodes.h:1987
Node * rarg
Definition: primnodes.h:1988
struct WindowDef * over
Definition: parsenodes.h:1831
JsonOutput * output
Definition: parsenodes.h:1828
JsonValueExpr * arg
Definition: parsenodes.h:1856
JsonAggConstructor * constructor
Definition: parsenodes.h:1855
JsonOutput * output
Definition: parsenodes.h:1801
JsonReturning * returning
Definition: primnodes.h:1633
JsonFormat * format
Definition: primnodes.h:1659
JsonValueExpr * value
Definition: parsenodes.h:1739
JsonAggConstructor * constructor
Definition: parsenodes.h:1842
JsonKeyValue * arg
Definition: parsenodes.h:1843
JsonOutput * output
Definition: parsenodes.h:1787
JsonReturning * returning
Definition: parsenodes.h:1727
TypeName * typeName
Definition: parsenodes.h:1726
JsonValueExpr * expr
Definition: parsenodes.h:1749
JsonOutput * output
Definition: parsenodes.h:1750
JsonFormat * format
Definition: primnodes.h:1590
JsonOutput * output
Definition: parsenodes.h:1763
JsonOutput * output
Definition: parsenodes.h:1775
JsonValueExpr * expr
Definition: parsenodes.h:1774
Expr * formatted_expr
Definition: primnodes.h:1607
JsonFormat * format
Definition: primnodes.h:1608
Expr * raw_expr
Definition: primnodes.h:1606
Definition: pg_list.h:54
Node * qual
Definition: parsenodes.h:1695
List * targetList
Definition: parsenodes.h:1696
List * args
Definition: primnodes.h:1445
Expr * arg
Definition: primnodes.h:718
Definition: nodes.h:129
int location
Definition: primnodes.h:1696
Expr * arg
Definition: primnodes.h:1692
List * arbiterElems
Definition: primnodes.h:2032
List * onConflictSet
Definition: primnodes.h:2038
List * exclRelTlist
Definition: primnodes.h:2041
Node * onConflictWhere
Definition: primnodes.h:2039
Node * arbiterWhere
Definition: primnodes.h:2034
int location
Definition: primnodes.h:766
Oid opno
Definition: primnodes.h:745
List * args
Definition: primnodes.h:763
PlaceHolderVar * ph_var
Definition: pathnodes.h:3044
Plan * plan
Definition: execnodes.h:1037
List * subPlan
Definition: execnodes.h:1064
List * initPlan
Definition: execnodes.h:1062
Node * limitCount
Definition: parsenodes.h:211
FromExpr * jointree
Definition: parsenodes.h:181
List * returningList
Definition: parsenodes.h:195
Node * setOperations
Definition: parsenodes.h:216
List * cteList
Definition: parsenodes.h:172
OnConflictExpr * onConflict
Definition: parsenodes.h:193
List * groupClause
Definition: parsenodes.h:197
Node * havingQual
Definition: parsenodes.h:202
List * rtable
Definition: parsenodes.h:174
Node * limitOffset
Definition: parsenodes.h:210
List * mergeActionList
Definition: parsenodes.h:184
List * windowClause
Definition: parsenodes.h:204
List * targetList
Definition: parsenodes.h:188
List * distinctClause
Definition: parsenodes.h:206
List * sortClause
Definition: parsenodes.h:208
Alias * alias
Definition: parsenodes.h:641
List * coldeflist
Definition: parsenodes.h:642
List * functions
Definition: parsenodes.h:640
Node * subquery
Definition: parsenodes.h:616
Alias * alias
Definition: parsenodes.h:617
List * namespaces
Definition: parsenodes.h:655
Node * docexpr
Definition: parsenodes.h:653
Node * rowexpr
Definition: parsenodes.h:654
List * columns
Definition: parsenodes.h:656
Alias * alias
Definition: parsenodes.h:657
TableFunc * tablefunc
Definition: parsenodes.h:1153
struct TableSampleClause * tablesample
Definition: parsenodes.h:1074
List * securityQuals
Definition: parsenodes.h:1203
Query * subquery
Definition: parsenodes.h:1080
List * values_lists
Definition: parsenodes.h:1158
List * joinaliasvars
Definition: parsenodes.h:1128
List * functions
Definition: parsenodes.h:1147
RTEKind rtekind
Definition: parsenodes.h:1032
Oid resulttype
Definition: primnodes.h:1112
Expr * arg
Definition: primnodes.h:1111
Node * val
Definition: parsenodes.h:516
List * indirection
Definition: parsenodes.h:515
List * args
Definition: primnodes.h:1338
struct PlanState * planstate
Definition: execnodes.h:953
List * args
Definition: primnodes.h:1018
Node * testexpr
Definition: primnodes.h:994
int32 firstColTypmod
Definition: primnodes.h:1002
Oid firstColCollation
Definition: primnodes.h:1003
SubLinkType subLinkType
Definition: primnodes.h:992
Oid firstColType
Definition: primnodes.h:1001
Expr * refassgnexpr
Definition: primnodes.h:630
List * refupperindexpr
Definition: primnodes.h:620
Expr * refexpr
Definition: primnodes.h:628
List * reflowerindexpr
Definition: primnodes.h:626
Node * docexpr
Definition: primnodes.h:103
Node * rowexpr
Definition: primnodes.h:105
List * colexprs
Definition: primnodes.h:115
Expr * expr
Definition: primnodes.h:1895
TypeName * typeName
Definition: parsenodes.h:371
int location
Definition: parsenodes.h:372
Node * arg
Definition: parsenodes.h:370
List * arrayBounds
Definition: parsenodes.h:271
int location
Definition: parsenodes.h:272
List * typmods
Definition: parsenodes.h:269
Definition: primnodes.h:226
Node * startOffset
Definition: parsenodes.h:1500
List * partitionClause
Definition: parsenodes.h:1496
Node * endOffset
Definition: parsenodes.h:1501
List * orderClause
Definition: parsenodes.h:1498
List * orderClause
Definition: parsenodes.h:564
List * partitionClause
Definition: parsenodes.h:563
Node * startOffset
Definition: parsenodes.h:566
Node * endOffset
Definition: parsenodes.h:567
List * args
Definition: primnodes.h:553
Expr * aggfilter
Definition: primnodes.h:555
Oid winfnoid
Definition: primnodes.h:545
List * args
Definition: primnodes.h:1535
int location
Definition: primnodes.h:1544
List * named_args
Definition: primnodes.h:1531
TypeName * typeName
Definition: parsenodes.h:841
Node * expr
Definition: parsenodes.h:840
Definition: type.h:88
const char * type