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