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