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