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