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