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/execnodes.h"
21 #include "nodes/makefuncs.h"
22 #include "nodes/nodeFuncs.h"
23 #include "nodes/pathnodes.h"
24 #include "utils/builtins.h"
25 #include "utils/lsyscache.h"
26 
27 static bool expression_returns_set_walker(Node *node, void *context);
28 static int leftmostLoc(int loc1, int loc2);
29 static bool fix_opfuncids_walker(Node *node, void *context);
30 static bool planstate_walk_subplans(List *plans, bool (*walker) (),
31  void *context);
32 static bool planstate_walk_members(PlanState **planstates, int nplans,
33  bool (*walker) (), void *context);
34 
35 
36 /*
37  * exprType -
38  * returns the Oid of the type of the expression's result.
39  */
40 Oid
41 exprType(const Node *expr)
42 {
43  Oid type;
44 
45  if (!expr)
46  return InvalidOid;
47 
48  switch (nodeTag(expr))
49  {
50  case T_Var:
51  type = ((const Var *) expr)->vartype;
52  break;
53  case T_Const:
54  type = ((const Const *) expr)->consttype;
55  break;
56  case T_Param:
57  type = ((const Param *) expr)->paramtype;
58  break;
59  case T_Aggref:
60  type = ((const Aggref *) expr)->aggtype;
61  break;
62  case T_GroupingFunc:
63  type = INT4OID;
64  break;
65  case T_WindowFunc:
66  type = ((const WindowFunc *) expr)->wintype;
67  break;
68  case T_SubscriptingRef:
69  {
70  const SubscriptingRef *sbsref = (const SubscriptingRef *) expr;
71 
72  /* slice and/or store operations yield the container type */
73  if (sbsref->reflowerindexpr || sbsref->refassgnexpr)
74  type = sbsref->refcontainertype;
75  else
76  type = sbsref->refelemtype;
77  }
78  break;
79  case T_FuncExpr:
80  type = ((const FuncExpr *) expr)->funcresulttype;
81  break;
82  case T_NamedArgExpr:
83  type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
84  break;
85  case T_OpExpr:
86  type = ((const OpExpr *) expr)->opresulttype;
87  break;
88  case T_DistinctExpr:
89  type = ((const DistinctExpr *) expr)->opresulttype;
90  break;
91  case T_NullIfExpr:
92  type = ((const NullIfExpr *) expr)->opresulttype;
93  break;
95  type = BOOLOID;
96  break;
97  case T_BoolExpr:
98  type = BOOLOID;
99  break;
100  case T_SubLink:
101  {
102  const SubLink *sublink = (const SubLink *) expr;
103 
104  if (sublink->subLinkType == EXPR_SUBLINK ||
105  sublink->subLinkType == ARRAY_SUBLINK)
106  {
107  /* get the type of the subselect's first target column */
108  Query *qtree = (Query *) sublink->subselect;
109  TargetEntry *tent;
110 
111  if (!qtree || !IsA(qtree, Query))
112  elog(ERROR, "cannot get type for untransformed sublink");
113  tent = linitial_node(TargetEntry, qtree->targetList);
114  Assert(!tent->resjunk);
115  type = exprType((Node *) tent->expr);
116  if (sublink->subLinkType == ARRAY_SUBLINK)
117  {
118  type = get_promoted_array_type(type);
119  if (!OidIsValid(type))
120  ereport(ERROR,
121  (errcode(ERRCODE_UNDEFINED_OBJECT),
122  errmsg("could not find array type for data type %s",
123  format_type_be(exprType((Node *) tent->expr)))));
124  }
125  }
126  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
127  {
128  /* MULTIEXPR is always considered to return RECORD */
129  type = RECORDOID;
130  }
131  else
132  {
133  /* for all other sublink types, result is boolean */
134  type = BOOLOID;
135  }
136  }
137  break;
138  case T_SubPlan:
139  {
140  const SubPlan *subplan = (const SubPlan *) expr;
141 
142  if (subplan->subLinkType == EXPR_SUBLINK ||
143  subplan->subLinkType == ARRAY_SUBLINK)
144  {
145  /* get the type of the subselect's first target column */
146  type = subplan->firstColType;
147  if (subplan->subLinkType == ARRAY_SUBLINK)
148  {
149  type = get_promoted_array_type(type);
150  if (!OidIsValid(type))
151  ereport(ERROR,
152  (errcode(ERRCODE_UNDEFINED_OBJECT),
153  errmsg("could not find array type for data type %s",
154  format_type_be(subplan->firstColType))));
155  }
156  }
157  else if (subplan->subLinkType == MULTIEXPR_SUBLINK)
158  {
159  /* MULTIEXPR is always considered to return RECORD */
160  type = RECORDOID;
161  }
162  else
163  {
164  /* for all other subplan types, result is boolean */
165  type = BOOLOID;
166  }
167  }
168  break;
170  {
171  const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
172 
173  /* subplans should all return the same thing */
174  type = exprType((Node *) linitial(asplan->subplans));
175  }
176  break;
177  case T_FieldSelect:
178  type = ((const FieldSelect *) expr)->resulttype;
179  break;
180  case T_FieldStore:
181  type = ((const FieldStore *) expr)->resulttype;
182  break;
183  case T_RelabelType:
184  type = ((const RelabelType *) expr)->resulttype;
185  break;
186  case T_CoerceViaIO:
187  type = ((const CoerceViaIO *) expr)->resulttype;
188  break;
189  case T_ArrayCoerceExpr:
190  type = ((const ArrayCoerceExpr *) expr)->resulttype;
191  break;
193  type = ((const ConvertRowtypeExpr *) expr)->resulttype;
194  break;
195  case T_CollateExpr:
196  type = exprType((Node *) ((const CollateExpr *) expr)->arg);
197  break;
198  case T_CaseExpr:
199  type = ((const CaseExpr *) expr)->casetype;
200  break;
201  case T_CaseTestExpr:
202  type = ((const CaseTestExpr *) expr)->typeId;
203  break;
204  case T_ArrayExpr:
205  type = ((const ArrayExpr *) expr)->array_typeid;
206  break;
207  case T_RowExpr:
208  type = ((const RowExpr *) expr)->row_typeid;
209  break;
210  case T_RowCompareExpr:
211  type = BOOLOID;
212  break;
213  case T_CoalesceExpr:
214  type = ((const CoalesceExpr *) expr)->coalescetype;
215  break;
216  case T_MinMaxExpr:
217  type = ((const MinMaxExpr *) expr)->minmaxtype;
218  break;
219  case T_SQLValueFunction:
220  type = ((const SQLValueFunction *) expr)->type;
221  break;
222  case T_XmlExpr:
223  if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
224  type = BOOLOID;
225  else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
226  type = TEXTOID;
227  else
228  type = XMLOID;
229  break;
230  case T_NullTest:
231  type = BOOLOID;
232  break;
233  case T_BooleanTest:
234  type = BOOLOID;
235  break;
236  case T_CoerceToDomain:
237  type = ((const CoerceToDomain *) expr)->resulttype;
238  break;
240  type = ((const CoerceToDomainValue *) expr)->typeId;
241  break;
242  case T_SetToDefault:
243  type = ((const SetToDefault *) expr)->typeId;
244  break;
245  case T_CurrentOfExpr:
246  type = BOOLOID;
247  break;
248  case T_NextValueExpr:
249  type = ((const NextValueExpr *) expr)->typeId;
250  break;
251  case T_InferenceElem:
252  {
253  const InferenceElem *n = (const InferenceElem *) expr;
254 
255  type = exprType((Node *) n->expr);
256  }
257  break;
258  case T_PlaceHolderVar:
259  type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
260  break;
261  default:
262  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
263  type = InvalidOid; /* keep compiler quiet */
264  break;
265  }
266  return type;
267 }
268 
269 /*
270  * exprTypmod -
271  * returns the type-specific modifier of the expression's result type,
272  * if it can be determined. In many cases, it can't and we return -1.
273  */
274 int32
275 exprTypmod(const Node *expr)
276 {
277  if (!expr)
278  return -1;
279 
280  switch (nodeTag(expr))
281  {
282  case T_Var:
283  return ((const Var *) expr)->vartypmod;
284  case T_Const:
285  return ((const Const *) expr)->consttypmod;
286  case T_Param:
287  return ((const Param *) expr)->paramtypmod;
288  case T_SubscriptingRef:
289  /* typmod is the same for container or element */
290  return ((const SubscriptingRef *) expr)->reftypmod;
291  case T_FuncExpr:
292  {
293  int32 coercedTypmod;
294 
295  /* Be smart about length-coercion functions... */
296  if (exprIsLengthCoercion(expr, &coercedTypmod))
297  return coercedTypmod;
298  }
299  break;
300  case T_NamedArgExpr:
301  return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
302  case T_NullIfExpr:
303  {
304  /*
305  * Result is either first argument or NULL, so we can report
306  * first argument's typmod if known.
307  */
308  const NullIfExpr *nexpr = (const NullIfExpr *) expr;
309 
310  return exprTypmod((Node *) linitial(nexpr->args));
311  }
312  break;
313  case T_SubLink:
314  {
315  const SubLink *sublink = (const SubLink *) expr;
316 
317  if (sublink->subLinkType == EXPR_SUBLINK ||
318  sublink->subLinkType == ARRAY_SUBLINK)
319  {
320  /* get the typmod of the subselect's first target column */
321  Query *qtree = (Query *) sublink->subselect;
322  TargetEntry *tent;
323 
324  if (!qtree || !IsA(qtree, Query))
325  elog(ERROR, "cannot get type for untransformed sublink");
326  tent = linitial_node(TargetEntry, qtree->targetList);
327  Assert(!tent->resjunk);
328  return exprTypmod((Node *) tent->expr);
329  /* note we don't need to care if it's an array */
330  }
331  /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
332  }
333  break;
334  case T_SubPlan:
335  {
336  const SubPlan *subplan = (const SubPlan *) expr;
337 
338  if (subplan->subLinkType == EXPR_SUBLINK ||
339  subplan->subLinkType == ARRAY_SUBLINK)
340  {
341  /* get the typmod of the subselect's first target column */
342  /* note we don't need to care if it's an array */
343  return subplan->firstColTypmod;
344  }
345  /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
346  }
347  break;
349  {
350  const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
351 
352  /* subplans should all return the same thing */
353  return exprTypmod((Node *) linitial(asplan->subplans));
354  }
355  break;
356  case T_FieldSelect:
357  return ((const FieldSelect *) expr)->resulttypmod;
358  case T_RelabelType:
359  return ((const RelabelType *) expr)->resulttypmod;
360  case T_ArrayCoerceExpr:
361  return ((const ArrayCoerceExpr *) expr)->resulttypmod;
362  case T_CollateExpr:
363  return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
364  case T_CaseExpr:
365  {
366  /*
367  * If all the alternatives agree on type/typmod, return that
368  * typmod, else use -1
369  */
370  const CaseExpr *cexpr = (const CaseExpr *) expr;
371  Oid casetype = cexpr->casetype;
372  int32 typmod;
373  ListCell *arg;
374 
375  if (!cexpr->defresult)
376  return -1;
377  if (exprType((Node *) cexpr->defresult) != casetype)
378  return -1;
379  typmod = exprTypmod((Node *) cexpr->defresult);
380  if (typmod < 0)
381  return -1; /* no point in trying harder */
382  foreach(arg, cexpr->args)
383  {
384  CaseWhen *w = lfirst_node(CaseWhen, arg);
385 
386  if (exprType((Node *) w->result) != casetype)
387  return -1;
388  if (exprTypmod((Node *) w->result) != typmod)
389  return -1;
390  }
391  return typmod;
392  }
393  break;
394  case T_CaseTestExpr:
395  return ((const CaseTestExpr *) expr)->typeMod;
396  case T_ArrayExpr:
397  {
398  /*
399  * If all the elements agree on type/typmod, return that
400  * typmod, else use -1
401  */
402  const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
403  Oid commontype;
404  int32 typmod;
405  ListCell *elem;
406 
407  if (arrayexpr->elements == NIL)
408  return -1;
409  typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
410  if (typmod < 0)
411  return -1; /* no point in trying harder */
412  if (arrayexpr->multidims)
413  commontype = arrayexpr->array_typeid;
414  else
415  commontype = arrayexpr->element_typeid;
416  foreach(elem, arrayexpr->elements)
417  {
418  Node *e = (Node *) lfirst(elem);
419 
420  if (exprType(e) != commontype)
421  return -1;
422  if (exprTypmod(e) != typmod)
423  return -1;
424  }
425  return typmod;
426  }
427  break;
428  case T_CoalesceExpr:
429  {
430  /*
431  * If all the alternatives agree on type/typmod, return that
432  * typmod, else use -1
433  */
434  const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
435  Oid coalescetype = cexpr->coalescetype;
436  int32 typmod;
437  ListCell *arg;
438 
439  if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
440  return -1;
441  typmod = exprTypmod((Node *) linitial(cexpr->args));
442  if (typmod < 0)
443  return -1; /* no point in trying harder */
444  for_each_cell(arg, cexpr->args, list_second_cell(cexpr->args))
445  {
446  Node *e = (Node *) lfirst(arg);
447 
448  if (exprType(e) != coalescetype)
449  return -1;
450  if (exprTypmod(e) != typmod)
451  return -1;
452  }
453  return typmod;
454  }
455  break;
456  case T_MinMaxExpr:
457  {
458  /*
459  * If all the alternatives agree on type/typmod, return that
460  * typmod, else use -1
461  */
462  const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
463  Oid minmaxtype = mexpr->minmaxtype;
464  int32 typmod;
465  ListCell *arg;
466 
467  if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
468  return -1;
469  typmod = exprTypmod((Node *) linitial(mexpr->args));
470  if (typmod < 0)
471  return -1; /* no point in trying harder */
472  for_each_cell(arg, mexpr->args, list_second_cell(mexpr->args))
473  {
474  Node *e = (Node *) lfirst(arg);
475 
476  if (exprType(e) != minmaxtype)
477  return -1;
478  if (exprTypmod(e) != typmod)
479  return -1;
480  }
481  return typmod;
482  }
483  break;
484  case T_SQLValueFunction:
485  return ((const SQLValueFunction *) expr)->typmod;
486  case T_CoerceToDomain:
487  return ((const CoerceToDomain *) expr)->resulttypmod;
489  return ((const CoerceToDomainValue *) expr)->typeMod;
490  case T_SetToDefault:
491  return ((const SetToDefault *) expr)->typeMod;
492  case T_PlaceHolderVar:
493  return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
494  default:
495  break;
496  }
497  return -1;
498 }
499 
500 /*
501  * exprIsLengthCoercion
502  * Detect whether an expression tree is an application of a datatype's
503  * typmod-coercion function. Optionally extract the result's typmod.
504  *
505  * If coercedTypmod is not NULL, the typmod is stored there if the expression
506  * is a length-coercion function, else -1 is stored there.
507  *
508  * Note that a combined type-and-length coercion will be treated as a
509  * length coercion by this routine.
510  */
511 bool
512 exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
513 {
514  if (coercedTypmod != NULL)
515  *coercedTypmod = -1; /* default result on failure */
516 
517  /*
518  * Scalar-type length coercions are FuncExprs, array-type length coercions
519  * are ArrayCoerceExprs
520  */
521  if (expr && IsA(expr, FuncExpr))
522  {
523  const FuncExpr *func = (const FuncExpr *) expr;
524  int nargs;
525  Const *second_arg;
526 
527  /*
528  * If it didn't come from a coercion context, reject.
529  */
530  if (func->funcformat != COERCE_EXPLICIT_CAST &&
532  return false;
533 
534  /*
535  * If it's not a two-argument or three-argument function with the
536  * second argument being an int4 constant, it can't have been created
537  * from a length coercion (it must be a type coercion, instead).
538  */
539  nargs = list_length(func->args);
540  if (nargs < 2 || nargs > 3)
541  return false;
542 
543  second_arg = (Const *) lsecond(func->args);
544  if (!IsA(second_arg, Const) ||
545  second_arg->consttype != INT4OID ||
546  second_arg->constisnull)
547  return false;
548 
549  /*
550  * OK, it is indeed a length-coercion function.
551  */
552  if (coercedTypmod != NULL)
553  *coercedTypmod = DatumGetInt32(second_arg->constvalue);
554 
555  return true;
556  }
557 
558  if (expr && IsA(expr, ArrayCoerceExpr))
559  {
560  const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
561 
562  /* It's not a length coercion unless there's a nondefault typmod */
563  if (acoerce->resulttypmod < 0)
564  return false;
565 
566  /*
567  * OK, it is indeed a length-coercion expression.
568  */
569  if (coercedTypmod != NULL)
570  *coercedTypmod = acoerce->resulttypmod;
571 
572  return true;
573  }
574 
575  return false;
576 }
577 
578 /*
579  * relabel_to_typmod
580  * Add a RelabelType node that changes just the typmod of the expression.
581  *
582  * This is primarily intended to be used during planning. Therefore, it
583  * strips any existing RelabelType nodes to maintain the planner's invariant
584  * that there are not adjacent RelabelTypes.
585  */
586 Node *
588 {
589  Oid type = exprType(expr);
590  Oid coll = exprCollation(expr);
591 
592  /* Strip any existing RelabelType node(s) */
593  while (expr && IsA(expr, RelabelType))
594  expr = (Node *) ((RelabelType *) expr)->arg;
595 
596  /* Apply new typmod, preserving the previous exposed type and collation */
597  return (Node *) makeRelabelType((Expr *) expr, type, typmod, coll,
599 }
600 
601 /*
602  * strip_implicit_coercions: remove implicit coercions at top level of tree
603  *
604  * This doesn't modify or copy the input expression tree, just return a
605  * pointer to a suitable place within it.
606  *
607  * Note: there isn't any useful thing we can do with a RowExpr here, so
608  * just return it unchanged, even if it's marked as an implicit coercion.
609  */
610 Node *
612 {
613  if (node == NULL)
614  return NULL;
615  if (IsA(node, FuncExpr))
616  {
617  FuncExpr *f = (FuncExpr *) node;
618 
621  }
622  else if (IsA(node, RelabelType))
623  {
624  RelabelType *r = (RelabelType *) node;
625 
627  return strip_implicit_coercions((Node *) r->arg);
628  }
629  else if (IsA(node, CoerceViaIO))
630  {
631  CoerceViaIO *c = (CoerceViaIO *) node;
632 
634  return strip_implicit_coercions((Node *) c->arg);
635  }
636  else if (IsA(node, ArrayCoerceExpr))
637  {
638  ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
639 
641  return strip_implicit_coercions((Node *) c->arg);
642  }
643  else if (IsA(node, ConvertRowtypeExpr))
644  {
646 
648  return strip_implicit_coercions((Node *) c->arg);
649  }
650  else if (IsA(node, CoerceToDomain))
651  {
652  CoerceToDomain *c = (CoerceToDomain *) node;
653 
655  return strip_implicit_coercions((Node *) c->arg);
656  }
657  return node;
658 }
659 
660 /*
661  * expression_returns_set
662  * Test whether an expression returns a set result.
663  *
664  * Because we use expression_tree_walker(), this can also be applied to
665  * whole targetlists; it'll produce true if any one of the tlist items
666  * returns a set.
667  */
668 bool
670 {
671  return expression_returns_set_walker(clause, NULL);
672 }
673 
674 static bool
675 expression_returns_set_walker(Node *node, void *context)
676 {
677  if (node == NULL)
678  return false;
679  if (IsA(node, FuncExpr))
680  {
681  FuncExpr *expr = (FuncExpr *) node;
682 
683  if (expr->funcretset)
684  return true;
685  /* else fall through to check args */
686  }
687  if (IsA(node, OpExpr))
688  {
689  OpExpr *expr = (OpExpr *) node;
690 
691  if (expr->opretset)
692  return true;
693  /* else fall through to check args */
694  }
695 
696  /* Avoid recursion for some cases that parser checks not to return a set */
697  if (IsA(node, Aggref))
698  return false;
699  if (IsA(node, WindowFunc))
700  return false;
701 
703  context);
704 }
705 
706 
707 /*
708  * exprCollation -
709  * returns the Oid of the collation of the expression's result.
710  *
711  * Note: expression nodes that can invoke functions generally have an
712  * "inputcollid" field, which is what the function should use as collation.
713  * That is the resolved common collation of the node's inputs. It is often
714  * but not always the same as the result collation; in particular, if the
715  * function produces a non-collatable result type from collatable inputs
716  * or vice versa, the two are different.
717  */
718 Oid
719 exprCollation(const Node *expr)
720 {
721  Oid coll;
722 
723  if (!expr)
724  return InvalidOid;
725 
726  switch (nodeTag(expr))
727  {
728  case T_Var:
729  coll = ((const Var *) expr)->varcollid;
730  break;
731  case T_Const:
732  coll = ((const Const *) expr)->constcollid;
733  break;
734  case T_Param:
735  coll = ((const Param *) expr)->paramcollid;
736  break;
737  case T_Aggref:
738  coll = ((const Aggref *) expr)->aggcollid;
739  break;
740  case T_GroupingFunc:
741  coll = InvalidOid;
742  break;
743  case T_WindowFunc:
744  coll = ((const WindowFunc *) expr)->wincollid;
745  break;
746  case T_SubscriptingRef:
747  coll = ((const SubscriptingRef *) expr)->refcollid;
748  break;
749  case T_FuncExpr:
750  coll = ((const FuncExpr *) expr)->funccollid;
751  break;
752  case T_NamedArgExpr:
753  coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
754  break;
755  case T_OpExpr:
756  coll = ((const OpExpr *) expr)->opcollid;
757  break;
758  case T_DistinctExpr:
759  coll = ((const DistinctExpr *) expr)->opcollid;
760  break;
761  case T_NullIfExpr:
762  coll = ((const NullIfExpr *) expr)->opcollid;
763  break;
764  case T_ScalarArrayOpExpr:
765  coll = InvalidOid; /* result is always boolean */
766  break;
767  case T_BoolExpr:
768  coll = InvalidOid; /* result is always boolean */
769  break;
770  case T_SubLink:
771  {
772  const SubLink *sublink = (const SubLink *) expr;
773 
774  if (sublink->subLinkType == EXPR_SUBLINK ||
775  sublink->subLinkType == ARRAY_SUBLINK)
776  {
777  /* get the collation of subselect's first target column */
778  Query *qtree = (Query *) sublink->subselect;
779  TargetEntry *tent;
780 
781  if (!qtree || !IsA(qtree, Query))
782  elog(ERROR, "cannot get collation for untransformed sublink");
783  tent = linitial_node(TargetEntry, qtree->targetList);
784  Assert(!tent->resjunk);
785  coll = exprCollation((Node *) tent->expr);
786  /* collation doesn't change if it's converted to array */
787  }
788  else
789  {
790  /* otherwise, result is RECORD or BOOLEAN */
791  coll = InvalidOid;
792  }
793  }
794  break;
795  case T_SubPlan:
796  {
797  const SubPlan *subplan = (const SubPlan *) expr;
798 
799  if (subplan->subLinkType == EXPR_SUBLINK ||
800  subplan->subLinkType == ARRAY_SUBLINK)
801  {
802  /* get the collation of subselect's first target column */
803  coll = subplan->firstColCollation;
804  /* collation doesn't change if it's converted to array */
805  }
806  else
807  {
808  /* otherwise, result is RECORD or BOOLEAN */
809  coll = InvalidOid;
810  }
811  }
812  break;
814  {
815  const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
816 
817  /* subplans should all return the same thing */
818  coll = exprCollation((Node *) linitial(asplan->subplans));
819  }
820  break;
821  case T_FieldSelect:
822  coll = ((const FieldSelect *) expr)->resultcollid;
823  break;
824  case T_FieldStore:
825  coll = InvalidOid; /* result is always composite */
826  break;
827  case T_RelabelType:
828  coll = ((const RelabelType *) expr)->resultcollid;
829  break;
830  case T_CoerceViaIO:
831  coll = ((const CoerceViaIO *) expr)->resultcollid;
832  break;
833  case T_ArrayCoerceExpr:
834  coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
835  break;
837  coll = InvalidOid; /* result is always composite */
838  break;
839  case T_CollateExpr:
840  coll = ((const CollateExpr *) expr)->collOid;
841  break;
842  case T_CaseExpr:
843  coll = ((const CaseExpr *) expr)->casecollid;
844  break;
845  case T_CaseTestExpr:
846  coll = ((const CaseTestExpr *) expr)->collation;
847  break;
848  case T_ArrayExpr:
849  coll = ((const ArrayExpr *) expr)->array_collid;
850  break;
851  case T_RowExpr:
852  coll = InvalidOid; /* result is always composite */
853  break;
854  case T_RowCompareExpr:
855  coll = InvalidOid; /* result is always boolean */
856  break;
857  case T_CoalesceExpr:
858  coll = ((const CoalesceExpr *) expr)->coalescecollid;
859  break;
860  case T_MinMaxExpr:
861  coll = ((const MinMaxExpr *) expr)->minmaxcollid;
862  break;
863  case T_SQLValueFunction:
864  /* Returns either NAME or a non-collatable type */
865  if (((const SQLValueFunction *) expr)->type == NAMEOID)
866  coll = C_COLLATION_OID;
867  else
868  coll = InvalidOid;
869  break;
870  case T_XmlExpr:
871 
872  /*
873  * XMLSERIALIZE returns text from non-collatable inputs, so its
874  * collation is always default. The other cases return boolean or
875  * XML, which are non-collatable.
876  */
877  if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
878  coll = DEFAULT_COLLATION_OID;
879  else
880  coll = InvalidOid;
881  break;
882  case T_NullTest:
883  coll = InvalidOid; /* result is always boolean */
884  break;
885  case T_BooleanTest:
886  coll = InvalidOid; /* result is always boolean */
887  break;
888  case T_CoerceToDomain:
889  coll = ((const CoerceToDomain *) expr)->resultcollid;
890  break;
892  coll = ((const CoerceToDomainValue *) expr)->collation;
893  break;
894  case T_SetToDefault:
895  coll = ((const SetToDefault *) expr)->collation;
896  break;
897  case T_CurrentOfExpr:
898  coll = InvalidOid; /* result is always boolean */
899  break;
900  case T_NextValueExpr:
901  coll = InvalidOid; /* result is always an integer type */
902  break;
903  case T_InferenceElem:
904  coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
905  break;
906  case T_PlaceHolderVar:
907  coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
908  break;
909  default:
910  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
911  coll = InvalidOid; /* keep compiler quiet */
912  break;
913  }
914  return coll;
915 }
916 
917 /*
918  * exprInputCollation -
919  * returns the Oid of the collation a function should use, if available.
920  *
921  * Result is InvalidOid if the node type doesn't store this information.
922  */
923 Oid
925 {
926  Oid coll;
927 
928  if (!expr)
929  return InvalidOid;
930 
931  switch (nodeTag(expr))
932  {
933  case T_Aggref:
934  coll = ((const Aggref *) expr)->inputcollid;
935  break;
936  case T_WindowFunc:
937  coll = ((const WindowFunc *) expr)->inputcollid;
938  break;
939  case T_FuncExpr:
940  coll = ((const FuncExpr *) expr)->inputcollid;
941  break;
942  case T_OpExpr:
943  coll = ((const OpExpr *) expr)->inputcollid;
944  break;
945  case T_DistinctExpr:
946  coll = ((const DistinctExpr *) expr)->inputcollid;
947  break;
948  case T_NullIfExpr:
949  coll = ((const NullIfExpr *) expr)->inputcollid;
950  break;
951  case T_ScalarArrayOpExpr:
952  coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
953  break;
954  case T_MinMaxExpr:
955  coll = ((const MinMaxExpr *) expr)->inputcollid;
956  break;
957  default:
958  coll = InvalidOid;
959  break;
960  }
961  return coll;
962 }
963 
964 /*
965  * exprSetCollation -
966  * Assign collation information to an expression tree node.
967  *
968  * Note: since this is only used during parse analysis, we don't need to
969  * worry about subplans or PlaceHolderVars.
970  */
971 void
972 exprSetCollation(Node *expr, Oid collation)
973 {
974  switch (nodeTag(expr))
975  {
976  case T_Var:
977  ((Var *) expr)->varcollid = collation;
978  break;
979  case T_Const:
980  ((Const *) expr)->constcollid = collation;
981  break;
982  case T_Param:
983  ((Param *) expr)->paramcollid = collation;
984  break;
985  case T_Aggref:
986  ((Aggref *) expr)->aggcollid = collation;
987  break;
988  case T_GroupingFunc:
989  Assert(!OidIsValid(collation));
990  break;
991  case T_WindowFunc:
992  ((WindowFunc *) expr)->wincollid = collation;
993  break;
994  case T_SubscriptingRef:
995  ((SubscriptingRef *) expr)->refcollid = collation;
996  break;
997  case T_FuncExpr:
998  ((FuncExpr *) expr)->funccollid = collation;
999  break;
1000  case T_NamedArgExpr:
1001  Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
1002  break;
1003  case T_OpExpr:
1004  ((OpExpr *) expr)->opcollid = collation;
1005  break;
1006  case T_DistinctExpr:
1007  ((DistinctExpr *) expr)->opcollid = collation;
1008  break;
1009  case T_NullIfExpr:
1010  ((NullIfExpr *) expr)->opcollid = collation;
1011  break;
1012  case T_ScalarArrayOpExpr:
1013  Assert(!OidIsValid(collation)); /* result is always boolean */
1014  break;
1015  case T_BoolExpr:
1016  Assert(!OidIsValid(collation)); /* result is always boolean */
1017  break;
1018  case T_SubLink:
1019 #ifdef USE_ASSERT_CHECKING
1020  {
1021  SubLink *sublink = (SubLink *) expr;
1022 
1023  if (sublink->subLinkType == EXPR_SUBLINK ||
1024  sublink->subLinkType == ARRAY_SUBLINK)
1025  {
1026  /* get the collation of subselect's first target column */
1027  Query *qtree = (Query *) sublink->subselect;
1028  TargetEntry *tent;
1029 
1030  if (!qtree || !IsA(qtree, Query))
1031  elog(ERROR, "cannot set collation for untransformed sublink");
1032  tent = linitial_node(TargetEntry, qtree->targetList);
1033  Assert(!tent->resjunk);
1034  Assert(collation == exprCollation((Node *) tent->expr));
1035  }
1036  else
1037  {
1038  /* otherwise, result is RECORD or BOOLEAN */
1039  Assert(!OidIsValid(collation));
1040  }
1041  }
1042 #endif /* USE_ASSERT_CHECKING */
1043  break;
1044  case T_FieldSelect:
1045  ((FieldSelect *) expr)->resultcollid = collation;
1046  break;
1047  case T_FieldStore:
1048  Assert(!OidIsValid(collation)); /* result is always composite */
1049  break;
1050  case T_RelabelType:
1051  ((RelabelType *) expr)->resultcollid = collation;
1052  break;
1053  case T_CoerceViaIO:
1054  ((CoerceViaIO *) expr)->resultcollid = collation;
1055  break;
1056  case T_ArrayCoerceExpr:
1057  ((ArrayCoerceExpr *) expr)->resultcollid = collation;
1058  break;
1059  case T_ConvertRowtypeExpr:
1060  Assert(!OidIsValid(collation)); /* result is always composite */
1061  break;
1062  case T_CaseExpr:
1063  ((CaseExpr *) expr)->casecollid = collation;
1064  break;
1065  case T_ArrayExpr:
1066  ((ArrayExpr *) expr)->array_collid = collation;
1067  break;
1068  case T_RowExpr:
1069  Assert(!OidIsValid(collation)); /* result is always composite */
1070  break;
1071  case T_RowCompareExpr:
1072  Assert(!OidIsValid(collation)); /* result is always boolean */
1073  break;
1074  case T_CoalesceExpr:
1075  ((CoalesceExpr *) expr)->coalescecollid = collation;
1076  break;
1077  case T_MinMaxExpr:
1078  ((MinMaxExpr *) expr)->minmaxcollid = collation;
1079  break;
1080  case T_SQLValueFunction:
1081  Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
1082  (collation == C_COLLATION_OID) :
1083  (collation == InvalidOid));
1084  break;
1085  case T_XmlExpr:
1086  Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
1087  (collation == DEFAULT_COLLATION_OID) :
1088  (collation == InvalidOid));
1089  break;
1090  case T_NullTest:
1091  Assert(!OidIsValid(collation)); /* result is always boolean */
1092  break;
1093  case T_BooleanTest:
1094  Assert(!OidIsValid(collation)); /* result is always boolean */
1095  break;
1096  case T_CoerceToDomain:
1097  ((CoerceToDomain *) expr)->resultcollid = collation;
1098  break;
1099  case T_CoerceToDomainValue:
1100  ((CoerceToDomainValue *) expr)->collation = collation;
1101  break;
1102  case T_SetToDefault:
1103  ((SetToDefault *) expr)->collation = collation;
1104  break;
1105  case T_CurrentOfExpr:
1106  Assert(!OidIsValid(collation)); /* result is always boolean */
1107  break;
1108  case T_NextValueExpr:
1109  Assert(!OidIsValid(collation)); /* result is always an integer
1110  * type */
1111  break;
1112  default:
1113  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
1114  break;
1115  }
1116 }
1117 
1118 /*
1119  * exprSetInputCollation -
1120  * Assign input-collation information to an expression tree node.
1121  *
1122  * This is a no-op for node types that don't store their input collation.
1123  * Note we omit RowCompareExpr, which needs special treatment since it
1124  * contains multiple input collation OIDs.
1125  */
1126 void
1127 exprSetInputCollation(Node *expr, Oid inputcollation)
1128 {
1129  switch (nodeTag(expr))
1130  {
1131  case T_Aggref:
1132  ((Aggref *) expr)->inputcollid = inputcollation;
1133  break;
1134  case T_WindowFunc:
1135  ((WindowFunc *) expr)->inputcollid = inputcollation;
1136  break;
1137  case T_FuncExpr:
1138  ((FuncExpr *) expr)->inputcollid = inputcollation;
1139  break;
1140  case T_OpExpr:
1141  ((OpExpr *) expr)->inputcollid = inputcollation;
1142  break;
1143  case T_DistinctExpr:
1144  ((DistinctExpr *) expr)->inputcollid = inputcollation;
1145  break;
1146  case T_NullIfExpr:
1147  ((NullIfExpr *) expr)->inputcollid = inputcollation;
1148  break;
1149  case T_ScalarArrayOpExpr:
1150  ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
1151  break;
1152  case T_MinMaxExpr:
1153  ((MinMaxExpr *) expr)->inputcollid = inputcollation;
1154  break;
1155  default:
1156  break;
1157  }
1158 }
1159 
1160 
1161 /*
1162  * exprLocation -
1163  * returns the parse location of an expression tree, for error reports
1164  *
1165  * -1 is returned if the location can't be determined.
1166  *
1167  * For expressions larger than a single token, the intent here is to
1168  * return the location of the expression's leftmost token, not necessarily
1169  * the topmost Node's location field. For example, an OpExpr's location
1170  * field will point at the operator name, but if it is not a prefix operator
1171  * then we should return the location of the left-hand operand instead.
1172  * The reason is that we want to reference the entire expression not just
1173  * that operator, and pointing to its start seems to be the most natural way.
1174  *
1175  * The location is not perfect --- for example, since the grammar doesn't
1176  * explicitly represent parentheses in the parsetree, given something that
1177  * had been written "(a + b) * c" we are going to point at "a" not "(".
1178  * But it should be plenty good enough for error reporting purposes.
1179  *
1180  * You might think that this code is overly general, for instance why check
1181  * the operands of a FuncExpr node, when the function name can be expected
1182  * to be to the left of them? There are a couple of reasons. The grammar
1183  * sometimes builds expressions that aren't quite what the user wrote;
1184  * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
1185  * pointer is to the right of its leftmost argument. Also, nodes that were
1186  * inserted implicitly by parse analysis (such as FuncExprs for implicit
1187  * coercions) will have location -1, and so we can have odd combinations of
1188  * known and unknown locations in a tree.
1189  */
1190 int
1191 exprLocation(const Node *expr)
1192 {
1193  int loc;
1194 
1195  if (expr == NULL)
1196  return -1;
1197  switch (nodeTag(expr))
1198  {
1199  case T_RangeVar:
1200  loc = ((const RangeVar *) expr)->location;
1201  break;
1202  case T_TableFunc:
1203  loc = ((const TableFunc *) expr)->location;
1204  break;
1205  case T_Var:
1206  loc = ((const Var *) expr)->location;
1207  break;
1208  case T_Const:
1209  loc = ((const Const *) expr)->location;
1210  break;
1211  case T_Param:
1212  loc = ((const Param *) expr)->location;
1213  break;
1214  case T_Aggref:
1215  /* function name should always be the first thing */
1216  loc = ((const Aggref *) expr)->location;
1217  break;
1218  case T_GroupingFunc:
1219  loc = ((const GroupingFunc *) expr)->location;
1220  break;
1221  case T_WindowFunc:
1222  /* function name should always be the first thing */
1223  loc = ((const WindowFunc *) expr)->location;
1224  break;
1225  case T_SubscriptingRef:
1226  /* just use container argument's location */
1227  loc = exprLocation((Node *) ((const SubscriptingRef *) expr)->refexpr);
1228  break;
1229  case T_FuncExpr:
1230  {
1231  const FuncExpr *fexpr = (const FuncExpr *) expr;
1232 
1233  /* consider both function name and leftmost arg */
1234  loc = leftmostLoc(fexpr->location,
1235  exprLocation((Node *) fexpr->args));
1236  }
1237  break;
1238  case T_NamedArgExpr:
1239  {
1240  const NamedArgExpr *na = (const NamedArgExpr *) expr;
1241 
1242  /* consider both argument name and value */
1243  loc = leftmostLoc(na->location,
1244  exprLocation((Node *) na->arg));
1245  }
1246  break;
1247  case T_OpExpr:
1248  case T_DistinctExpr: /* struct-equivalent to OpExpr */
1249  case T_NullIfExpr: /* struct-equivalent to OpExpr */
1250  {
1251  const OpExpr *opexpr = (const OpExpr *) expr;
1252 
1253  /* consider both operator name and leftmost arg */
1254  loc = leftmostLoc(opexpr->location,
1255  exprLocation((Node *) opexpr->args));
1256  }
1257  break;
1258  case T_ScalarArrayOpExpr:
1259  {
1260  const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
1261 
1262  /* consider both operator name and leftmost arg */
1263  loc = leftmostLoc(saopexpr->location,
1264  exprLocation((Node *) saopexpr->args));
1265  }
1266  break;
1267  case T_BoolExpr:
1268  {
1269  const BoolExpr *bexpr = (const BoolExpr *) expr;
1270 
1271  /*
1272  * Same as above, to handle either NOT or AND/OR. We can't
1273  * special-case NOT because of the way that it's used for
1274  * things like IS NOT BETWEEN.
1275  */
1276  loc = leftmostLoc(bexpr->location,
1277  exprLocation((Node *) bexpr->args));
1278  }
1279  break;
1280  case T_SubLink:
1281  {
1282  const SubLink *sublink = (const SubLink *) expr;
1283 
1284  /* check the testexpr, if any, and the operator/keyword */
1285  loc = leftmostLoc(exprLocation(sublink->testexpr),
1286  sublink->location);
1287  }
1288  break;
1289  case T_FieldSelect:
1290  /* just use argument's location */
1291  loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
1292  break;
1293  case T_FieldStore:
1294  /* just use argument's location */
1295  loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
1296  break;
1297  case T_RelabelType:
1298  {
1299  const RelabelType *rexpr = (const RelabelType *) expr;
1300 
1301  /* Much as above */
1302  loc = leftmostLoc(rexpr->location,
1303  exprLocation((Node *) rexpr->arg));
1304  }
1305  break;
1306  case T_CoerceViaIO:
1307  {
1308  const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
1309 
1310  /* Much as above */
1311  loc = leftmostLoc(cexpr->location,
1312  exprLocation((Node *) cexpr->arg));
1313  }
1314  break;
1315  case T_ArrayCoerceExpr:
1316  {
1317  const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
1318 
1319  /* Much as above */
1320  loc = leftmostLoc(cexpr->location,
1321  exprLocation((Node *) cexpr->arg));
1322  }
1323  break;
1324  case T_ConvertRowtypeExpr:
1325  {
1326  const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
1327 
1328  /* Much as above */
1329  loc = leftmostLoc(cexpr->location,
1330  exprLocation((Node *) cexpr->arg));
1331  }
1332  break;
1333  case T_CollateExpr:
1334  /* just use argument's location */
1335  loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
1336  break;
1337  case T_CaseExpr:
1338  /* CASE keyword should always be the first thing */
1339  loc = ((const CaseExpr *) expr)->location;
1340  break;
1341  case T_CaseWhen:
1342  /* WHEN keyword should always be the first thing */
1343  loc = ((const CaseWhen *) expr)->location;
1344  break;
1345  case T_ArrayExpr:
1346  /* the location points at ARRAY or [, which must be leftmost */
1347  loc = ((const ArrayExpr *) expr)->location;
1348  break;
1349  case T_RowExpr:
1350  /* the location points at ROW or (, which must be leftmost */
1351  loc = ((const RowExpr *) expr)->location;
1352  break;
1353  case T_RowCompareExpr:
1354  /* just use leftmost argument's location */
1355  loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
1356  break;
1357  case T_CoalesceExpr:
1358  /* COALESCE keyword should always be the first thing */
1359  loc = ((const CoalesceExpr *) expr)->location;
1360  break;
1361  case T_MinMaxExpr:
1362  /* GREATEST/LEAST keyword should always be the first thing */
1363  loc = ((const MinMaxExpr *) expr)->location;
1364  break;
1365  case T_SQLValueFunction:
1366  /* function keyword should always be the first thing */
1367  loc = ((const SQLValueFunction *) expr)->location;
1368  break;
1369  case T_XmlExpr:
1370  {
1371  const XmlExpr *xexpr = (const XmlExpr *) expr;
1372 
1373  /* consider both function name and leftmost arg */
1374  loc = leftmostLoc(xexpr->location,
1375  exprLocation((Node *) xexpr->args));
1376  }
1377  break;
1378  case T_NullTest:
1379  {
1380  const NullTest *nexpr = (const NullTest *) expr;
1381 
1382  /* Much as above */
1383  loc = leftmostLoc(nexpr->location,
1384  exprLocation((Node *) nexpr->arg));
1385  }
1386  break;
1387  case T_BooleanTest:
1388  {
1389  const BooleanTest *bexpr = (const BooleanTest *) expr;
1390 
1391  /* Much as above */
1392  loc = leftmostLoc(bexpr->location,
1393  exprLocation((Node *) bexpr->arg));
1394  }
1395  break;
1396  case T_CoerceToDomain:
1397  {
1398  const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
1399 
1400  /* Much as above */
1401  loc = leftmostLoc(cexpr->location,
1402  exprLocation((Node *) cexpr->arg));
1403  }
1404  break;
1405  case T_CoerceToDomainValue:
1406  loc = ((const CoerceToDomainValue *) expr)->location;
1407  break;
1408  case T_SetToDefault:
1409  loc = ((const SetToDefault *) expr)->location;
1410  break;
1411  case T_TargetEntry:
1412  /* just use argument's location */
1413  loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
1414  break;
1415  case T_IntoClause:
1416  /* use the contained RangeVar's location --- close enough */
1417  loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
1418  break;
1419  case T_List:
1420  {
1421  /* report location of first list member that has a location */
1422  ListCell *lc;
1423 
1424  loc = -1; /* just to suppress compiler warning */
1425  foreach(lc, (const List *) expr)
1426  {
1427  loc = exprLocation((Node *) lfirst(lc));
1428  if (loc >= 0)
1429  break;
1430  }
1431  }
1432  break;
1433  case T_A_Expr:
1434  {
1435  const A_Expr *aexpr = (const A_Expr *) expr;
1436 
1437  /* use leftmost of operator or left operand (if any) */
1438  /* we assume right operand can't be to left of operator */
1439  loc = leftmostLoc(aexpr->location,
1440  exprLocation(aexpr->lexpr));
1441  }
1442  break;
1443  case T_ColumnRef:
1444  loc = ((const ColumnRef *) expr)->location;
1445  break;
1446  case T_ParamRef:
1447  loc = ((const ParamRef *) expr)->location;
1448  break;
1449  case T_A_Const:
1450  loc = ((const A_Const *) expr)->location;
1451  break;
1452  case T_FuncCall:
1453  {
1454  const FuncCall *fc = (const FuncCall *) expr;
1455 
1456  /* consider both function name and leftmost arg */
1457  /* (we assume any ORDER BY nodes must be to right of name) */
1458  loc = leftmostLoc(fc->location,
1459  exprLocation((Node *) fc->args));
1460  }
1461  break;
1462  case T_A_ArrayExpr:
1463  /* the location points at ARRAY or [, which must be leftmost */
1464  loc = ((const A_ArrayExpr *) expr)->location;
1465  break;
1466  case T_ResTarget:
1467  /* we need not examine the contained expression (if any) */
1468  loc = ((const ResTarget *) expr)->location;
1469  break;
1470  case T_MultiAssignRef:
1471  loc = exprLocation(((const MultiAssignRef *) expr)->source);
1472  break;
1473  case T_TypeCast:
1474  {
1475  const TypeCast *tc = (const TypeCast *) expr;
1476 
1477  /*
1478  * This could represent CAST(), ::, or TypeName 'literal', so
1479  * any of the components might be leftmost.
1480  */
1481  loc = exprLocation(tc->arg);
1482  loc = leftmostLoc(loc, tc->typeName->location);
1483  loc = leftmostLoc(loc, tc->location);
1484  }
1485  break;
1486  case T_CollateClause:
1487  /* just use argument's location */
1488  loc = exprLocation(((const CollateClause *) expr)->arg);
1489  break;
1490  case T_SortBy:
1491  /* just use argument's location (ignore operator, if any) */
1492  loc = exprLocation(((const SortBy *) expr)->node);
1493  break;
1494  case T_WindowDef:
1495  loc = ((const WindowDef *) expr)->location;
1496  break;
1497  case T_RangeTableSample:
1498  loc = ((const RangeTableSample *) expr)->location;
1499  break;
1500  case T_TypeName:
1501  loc = ((const TypeName *) expr)->location;
1502  break;
1503  case T_ColumnDef:
1504  loc = ((const ColumnDef *) expr)->location;
1505  break;
1506  case T_Constraint:
1507  loc = ((const Constraint *) expr)->location;
1508  break;
1509  case T_FunctionParameter:
1510  /* just use typename's location */
1511  loc = exprLocation((Node *) ((const FunctionParameter *) expr)->argType);
1512  break;
1513  case T_XmlSerialize:
1514  /* XMLSERIALIZE keyword should always be the first thing */
1515  loc = ((const XmlSerialize *) expr)->location;
1516  break;
1517  case T_GroupingSet:
1518  loc = ((const GroupingSet *) expr)->location;
1519  break;
1520  case T_WithClause:
1521  loc = ((const WithClause *) expr)->location;
1522  break;
1523  case T_InferClause:
1524  loc = ((const InferClause *) expr)->location;
1525  break;
1526  case T_OnConflictClause:
1527  loc = ((const OnConflictClause *) expr)->location;
1528  break;
1529  case T_CommonTableExpr:
1530  loc = ((const CommonTableExpr *) expr)->location;
1531  break;
1532  case T_PlaceHolderVar:
1533  /* just use argument's location */
1534  loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1535  break;
1536  case T_InferenceElem:
1537  /* just use nested expr's location */
1538  loc = exprLocation((Node *) ((const InferenceElem *) expr)->expr);
1539  break;
1540  case T_PartitionElem:
1541  loc = ((const PartitionElem *) expr)->location;
1542  break;
1543  case T_PartitionSpec:
1544  loc = ((const PartitionSpec *) expr)->location;
1545  break;
1546  case T_PartitionBoundSpec:
1547  loc = ((const PartitionBoundSpec *) expr)->location;
1548  break;
1549  case T_PartitionRangeDatum:
1550  loc = ((const PartitionRangeDatum *) expr)->location;
1551  break;
1552  default:
1553  /* for any other node type it's just unknown... */
1554  loc = -1;
1555  break;
1556  }
1557  return loc;
1558 }
1559 
1560 /*
1561  * leftmostLoc - support for exprLocation
1562  *
1563  * Take the minimum of two parse location values, but ignore unknowns
1564  */
1565 static int
1566 leftmostLoc(int loc1, int loc2)
1567 {
1568  if (loc1 < 0)
1569  return loc2;
1570  else if (loc2 < 0)
1571  return loc1;
1572  else
1573  return Min(loc1, loc2);
1574 }
1575 
1576 
1577 /*
1578  * fix_opfuncids
1579  * Calculate opfuncid field from opno for each OpExpr node in given tree.
1580  * The given tree can be anything expression_tree_walker handles.
1581  *
1582  * The argument is modified in-place. (This is OK since we'd want the
1583  * same change for any node, even if it gets visited more than once due to
1584  * shared structure.)
1585  */
1586 void
1588 {
1589  /* This tree walk requires no special setup, so away we go... */
1590  fix_opfuncids_walker(node, NULL);
1591 }
1592 
1593 static bool
1594 fix_opfuncids_walker(Node *node, void *context)
1595 {
1596  if (node == NULL)
1597  return false;
1598  if (IsA(node, OpExpr))
1599  set_opfuncid((OpExpr *) node);
1600  else if (IsA(node, DistinctExpr))
1601  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1602  else if (IsA(node, NullIfExpr))
1603  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1604  else if (IsA(node, ScalarArrayOpExpr))
1606  return expression_tree_walker(node, fix_opfuncids_walker, context);
1607 }
1608 
1609 /*
1610  * set_opfuncid
1611  * Set the opfuncid (procedure OID) in an OpExpr node,
1612  * if it hasn't been set already.
1613  *
1614  * Because of struct equivalence, this can also be used for
1615  * DistinctExpr and NullIfExpr nodes.
1616  */
1617 void
1619 {
1620  if (opexpr->opfuncid == InvalidOid)
1621  opexpr->opfuncid = get_opcode(opexpr->opno);
1622 }
1623 
1624 /*
1625  * set_sa_opfuncid
1626  * As above, for ScalarArrayOpExpr nodes.
1627  */
1628 void
1630 {
1631  if (opexpr->opfuncid == InvalidOid)
1632  opexpr->opfuncid = get_opcode(opexpr->opno);
1633 }
1634 
1635 
1636 /*
1637  * check_functions_in_node -
1638  * apply checker() to each function OID contained in given expression node
1639  *
1640  * Returns true if the checker() function does; for nodes representing more
1641  * than one function call, returns true if the checker() function does so
1642  * for any of those functions. Returns false if node does not invoke any
1643  * SQL-visible function. Caller must not pass node == NULL.
1644  *
1645  * This function examines only the given node; it does not recurse into any
1646  * sub-expressions. Callers typically prefer to keep control of the recursion
1647  * for themselves, in case additional checks should be made, or because they
1648  * have special rules about which parts of the tree need to be visited.
1649  *
1650  * Note: we ignore MinMaxExpr, SQLValueFunction, XmlExpr, CoerceToDomain,
1651  * and NextValueExpr nodes, because they do not contain SQL function OIDs.
1652  * However, they can invoke SQL-visible functions, so callers should take
1653  * thought about how to treat them.
1654  */
1655 bool
1657  void *context)
1658 {
1659  switch (nodeTag(node))
1660  {
1661  case T_Aggref:
1662  {
1663  Aggref *expr = (Aggref *) node;
1664 
1665  if (checker(expr->aggfnoid, context))
1666  return true;
1667  }
1668  break;
1669  case T_WindowFunc:
1670  {
1671  WindowFunc *expr = (WindowFunc *) node;
1672 
1673  if (checker(expr->winfnoid, context))
1674  return true;
1675  }
1676  break;
1677  case T_FuncExpr:
1678  {
1679  FuncExpr *expr = (FuncExpr *) node;
1680 
1681  if (checker(expr->funcid, context))
1682  return true;
1683  }
1684  break;
1685  case T_OpExpr:
1686  case T_DistinctExpr: /* struct-equivalent to OpExpr */
1687  case T_NullIfExpr: /* struct-equivalent to OpExpr */
1688  {
1689  OpExpr *expr = (OpExpr *) node;
1690 
1691  /* Set opfuncid if it wasn't set already */
1692  set_opfuncid(expr);
1693  if (checker(expr->opfuncid, context))
1694  return true;
1695  }
1696  break;
1697  case T_ScalarArrayOpExpr:
1698  {
1699  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1700 
1701  set_sa_opfuncid(expr);
1702  if (checker(expr->opfuncid, context))
1703  return true;
1704  }
1705  break;
1706  case T_CoerceViaIO:
1707  {
1708  CoerceViaIO *expr = (CoerceViaIO *) node;
1709  Oid iofunc;
1710  Oid typioparam;
1711  bool typisvarlena;
1712 
1713  /* check the result type's input function */
1715  &iofunc, &typioparam);
1716  if (checker(iofunc, context))
1717  return true;
1718  /* check the input type's output function */
1719  getTypeOutputInfo(exprType((Node *) expr->arg),
1720  &iofunc, &typisvarlena);
1721  if (checker(iofunc, context))
1722  return true;
1723  }
1724  break;
1725  case T_RowCompareExpr:
1726  {
1727  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1728  ListCell *opid;
1729 
1730  foreach(opid, rcexpr->opnos)
1731  {
1732  Oid opfuncid = get_opcode(lfirst_oid(opid));
1733 
1734  if (checker(opfuncid, context))
1735  return true;
1736  }
1737  }
1738  break;
1739  default:
1740  break;
1741  }
1742  return false;
1743 }
1744 
1745 
1746 /*
1747  * Standard expression-tree walking support
1748  *
1749  * We used to have near-duplicate code in many different routines that
1750  * understood how to recurse through an expression node tree. That was
1751  * a pain to maintain, and we frequently had bugs due to some particular
1752  * routine neglecting to support a particular node type. In most cases,
1753  * these routines only actually care about certain node types, and don't
1754  * care about other types except insofar as they have to recurse through
1755  * non-primitive node types. Therefore, we now provide generic tree-walking
1756  * logic to consolidate the redundant "boilerplate" code. There are
1757  * two versions: expression_tree_walker() and expression_tree_mutator().
1758  */
1759 
1760 /*
1761  * expression_tree_walker() is designed to support routines that traverse
1762  * a tree in a read-only fashion (although it will also work for routines
1763  * that modify nodes in-place but never add/delete/replace nodes).
1764  * A walker routine should look like this:
1765  *
1766  * bool my_walker (Node *node, my_struct *context)
1767  * {
1768  * if (node == NULL)
1769  * return false;
1770  * // check for nodes that special work is required for, eg:
1771  * if (IsA(node, Var))
1772  * {
1773  * ... do special actions for Var nodes
1774  * }
1775  * else if (IsA(node, ...))
1776  * {
1777  * ... do special actions for other node types
1778  * }
1779  * // for any node type not specially processed, do:
1780  * return expression_tree_walker(node, my_walker, (void *) context);
1781  * }
1782  *
1783  * The "context" argument points to a struct that holds whatever context
1784  * information the walker routine needs --- it can be used to return data
1785  * gathered by the walker, too. This argument is not touched by
1786  * expression_tree_walker, but it is passed down to recursive sub-invocations
1787  * of my_walker. The tree walk is started from a setup routine that
1788  * fills in the appropriate context struct, calls my_walker with the top-level
1789  * node of the tree, and then examines the results.
1790  *
1791  * The walker routine should return "false" to continue the tree walk, or
1792  * "true" to abort the walk and immediately return "true" to the top-level
1793  * caller. This can be used to short-circuit the traversal if the walker
1794  * has found what it came for. "false" is returned to the top-level caller
1795  * iff no invocation of the walker returned "true".
1796  *
1797  * The node types handled by expression_tree_walker include all those
1798  * normally found in target lists and qualifier clauses during the planning
1799  * stage. In particular, it handles List nodes since a cnf-ified qual clause
1800  * will have List structure at the top level, and it handles TargetEntry nodes
1801  * so that a scan of a target list can be handled without additional code.
1802  * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
1803  * handled, so that query jointrees and setOperation trees can be processed
1804  * without additional code.
1805  *
1806  * expression_tree_walker will handle SubLink nodes by recursing normally
1807  * into the "testexpr" subtree (which is an expression belonging to the outer
1808  * plan). It will also call the walker on the sub-Query node; however, when
1809  * expression_tree_walker itself is called on a Query node, it does nothing
1810  * and returns "false". The net effect is that unless the walker does
1811  * something special at a Query node, sub-selects will not be visited during
1812  * an expression tree walk. This is exactly the behavior wanted in many cases
1813  * --- and for those walkers that do want to recurse into sub-selects, special
1814  * behavior is typically needed anyway at the entry to a sub-select (such as
1815  * incrementing a depth counter). A walker that wants to examine sub-selects
1816  * should include code along the lines of:
1817  *
1818  * if (IsA(node, Query))
1819  * {
1820  * adjust context for subquery;
1821  * result = query_tree_walker((Query *) node, my_walker, context,
1822  * 0); // adjust flags as needed
1823  * restore context if needed;
1824  * return result;
1825  * }
1826  *
1827  * query_tree_walker is a convenience routine (see below) that calls the
1828  * walker on all the expression subtrees of the given Query node.
1829  *
1830  * expression_tree_walker will handle SubPlan nodes by recursing normally
1831  * into the "testexpr" and the "args" list (which are expressions belonging to
1832  * the outer plan). It will not touch the completed subplan, however. Since
1833  * there is no link to the original Query, it is not possible to recurse into
1834  * subselects of an already-planned expression tree. This is OK for current
1835  * uses, but may need to be revisited in future.
1836  */
1837 
1838 bool
1840  bool (*walker) (),
1841  void *context)
1842 {
1843  ListCell *temp;
1844 
1845  /*
1846  * The walker has already visited the current node, and so we need only
1847  * recurse into any sub-nodes it has.
1848  *
1849  * We assume that the walker is not interested in List nodes per se, so
1850  * when we expect a List we just recurse directly to self without
1851  * bothering to call the walker.
1852  */
1853  if (node == NULL)
1854  return false;
1855 
1856  /* Guard against stack overflow due to overly complex expressions */
1858 
1859  switch (nodeTag(node))
1860  {
1861  case T_Var:
1862  case T_Const:
1863  case T_Param:
1864  case T_CaseTestExpr:
1865  case T_SQLValueFunction:
1866  case T_CoerceToDomainValue:
1867  case T_SetToDefault:
1868  case T_CurrentOfExpr:
1869  case T_NextValueExpr:
1870  case T_RangeTblRef:
1871  case T_SortGroupClause:
1872  /* primitive node types with no expression subnodes */
1873  break;
1874  case T_WithCheckOption:
1875  return walker(((WithCheckOption *) node)->qual, context);
1876  case T_Aggref:
1877  {
1878  Aggref *expr = (Aggref *) node;
1879 
1880  /* recurse directly on List */
1882  walker, context))
1883  return true;
1884  if (expression_tree_walker((Node *) expr->args,
1885  walker, context))
1886  return true;
1887  if (expression_tree_walker((Node *) expr->aggorder,
1888  walker, context))
1889  return true;
1890  if (expression_tree_walker((Node *) expr->aggdistinct,
1891  walker, context))
1892  return true;
1893  if (walker((Node *) expr->aggfilter, context))
1894  return true;
1895  }
1896  break;
1897  case T_GroupingFunc:
1898  {
1899  GroupingFunc *grouping = (GroupingFunc *) node;
1900 
1901  if (expression_tree_walker((Node *) grouping->args,
1902  walker, context))
1903  return true;
1904  }
1905  break;
1906  case T_WindowFunc:
1907  {
1908  WindowFunc *expr = (WindowFunc *) node;
1909 
1910  /* recurse directly on List */
1911  if (expression_tree_walker((Node *) expr->args,
1912  walker, context))
1913  return true;
1914  if (walker((Node *) expr->aggfilter, context))
1915  return true;
1916  }
1917  break;
1918  case T_SubscriptingRef:
1919  {
1920  SubscriptingRef *sbsref = (SubscriptingRef *) node;
1921 
1922  /* recurse directly for upper/lower container index lists */
1924  walker, context))
1925  return true;
1927  walker, context))
1928  return true;
1929  /* walker must see the refexpr and refassgnexpr, however */
1930  if (walker(sbsref->refexpr, context))
1931  return true;
1932 
1933  if (walker(sbsref->refassgnexpr, context))
1934  return true;
1935  }
1936  break;
1937  case T_FuncExpr:
1938  {
1939  FuncExpr *expr = (FuncExpr *) node;
1940 
1941  if (expression_tree_walker((Node *) expr->args,
1942  walker, context))
1943  return true;
1944  }
1945  break;
1946  case T_NamedArgExpr:
1947  return walker(((NamedArgExpr *) node)->arg, context);
1948  case T_OpExpr:
1949  case T_DistinctExpr: /* struct-equivalent to OpExpr */
1950  case T_NullIfExpr: /* struct-equivalent to OpExpr */
1951  {
1952  OpExpr *expr = (OpExpr *) node;
1953 
1954  if (expression_tree_walker((Node *) expr->args,
1955  walker, context))
1956  return true;
1957  }
1958  break;
1959  case T_ScalarArrayOpExpr:
1960  {
1961  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1962 
1963  if (expression_tree_walker((Node *) expr->args,
1964  walker, context))
1965  return true;
1966  }
1967  break;
1968  case T_BoolExpr:
1969  {
1970  BoolExpr *expr = (BoolExpr *) node;
1971 
1972  if (expression_tree_walker((Node *) expr->args,
1973  walker, context))
1974  return true;
1975  }
1976  break;
1977  case T_SubLink:
1978  {
1979  SubLink *sublink = (SubLink *) node;
1980 
1981  if (walker(sublink->testexpr, context))
1982  return true;
1983 
1984  /*
1985  * Also invoke the walker on the sublink's Query node, so it
1986  * can recurse into the sub-query if it wants to.
1987  */
1988  return walker(sublink->subselect, context);
1989  }
1990  break;
1991  case T_SubPlan:
1992  {
1993  SubPlan *subplan = (SubPlan *) node;
1994 
1995  /* recurse into the testexpr, but not into the Plan */
1996  if (walker(subplan->testexpr, context))
1997  return true;
1998  /* also examine args list */
1999  if (expression_tree_walker((Node *) subplan->args,
2000  walker, context))
2001  return true;
2002  }
2003  break;
2004  case T_AlternativeSubPlan:
2005  return walker(((AlternativeSubPlan *) node)->subplans, context);
2006  case T_FieldSelect:
2007  return walker(((FieldSelect *) node)->arg, context);
2008  case T_FieldStore:
2009  {
2010  FieldStore *fstore = (FieldStore *) node;
2011 
2012  if (walker(fstore->arg, context))
2013  return true;
2014  if (walker(fstore->newvals, context))
2015  return true;
2016  }
2017  break;
2018  case T_RelabelType:
2019  return walker(((RelabelType *) node)->arg, context);
2020  case T_CoerceViaIO:
2021  return walker(((CoerceViaIO *) node)->arg, context);
2022  case T_ArrayCoerceExpr:
2023  {
2024  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2025 
2026  if (walker(acoerce->arg, context))
2027  return true;
2028  if (walker(acoerce->elemexpr, context))
2029  return true;
2030  }
2031  break;
2032  case T_ConvertRowtypeExpr:
2033  return walker(((ConvertRowtypeExpr *) node)->arg, context);
2034  case T_CollateExpr:
2035  return walker(((CollateExpr *) node)->arg, context);
2036  case T_CaseExpr:
2037  {
2038  CaseExpr *caseexpr = (CaseExpr *) node;
2039 
2040  if (walker(caseexpr->arg, context))
2041  return true;
2042  /* we assume walker doesn't care about CaseWhens, either */
2043  foreach(temp, caseexpr->args)
2044  {
2045  CaseWhen *when = lfirst_node(CaseWhen, temp);
2046 
2047  if (walker(when->expr, context))
2048  return true;
2049  if (walker(when->result, context))
2050  return true;
2051  }
2052  if (walker(caseexpr->defresult, context))
2053  return true;
2054  }
2055  break;
2056  case T_ArrayExpr:
2057  return walker(((ArrayExpr *) node)->elements, context);
2058  case T_RowExpr:
2059  /* Assume colnames isn't interesting */
2060  return walker(((RowExpr *) node)->args, context);
2061  case T_RowCompareExpr:
2062  {
2063  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2064 
2065  if (walker(rcexpr->largs, context))
2066  return true;
2067  if (walker(rcexpr->rargs, context))
2068  return true;
2069  }
2070  break;
2071  case T_CoalesceExpr:
2072  return walker(((CoalesceExpr *) node)->args, context);
2073  case T_MinMaxExpr:
2074  return walker(((MinMaxExpr *) node)->args, context);
2075  case T_XmlExpr:
2076  {
2077  XmlExpr *xexpr = (XmlExpr *) node;
2078 
2079  if (walker(xexpr->named_args, context))
2080  return true;
2081  /* we assume walker doesn't care about arg_names */
2082  if (walker(xexpr->args, context))
2083  return true;
2084  }
2085  break;
2086  case T_NullTest:
2087  return walker(((NullTest *) node)->arg, context);
2088  case T_BooleanTest:
2089  return walker(((BooleanTest *) node)->arg, context);
2090  case T_CoerceToDomain:
2091  return walker(((CoerceToDomain *) node)->arg, context);
2092  case T_TargetEntry:
2093  return walker(((TargetEntry *) node)->expr, context);
2094  case T_Query:
2095  /* Do nothing with a sub-Query, per discussion above */
2096  break;
2097  case T_WindowClause:
2098  {
2099  WindowClause *wc = (WindowClause *) node;
2100 
2101  if (walker(wc->partitionClause, context))
2102  return true;
2103  if (walker(wc->orderClause, context))
2104  return true;
2105  if (walker(wc->startOffset, context))
2106  return true;
2107  if (walker(wc->endOffset, context))
2108  return true;
2109  }
2110  break;
2111  case T_CommonTableExpr:
2112  {
2113  CommonTableExpr *cte = (CommonTableExpr *) node;
2114 
2115  /*
2116  * Invoke the walker on the CTE's Query node, so it can
2117  * recurse into the sub-query if it wants to.
2118  */
2119  return walker(cte->ctequery, context);
2120  }
2121  break;
2122  case T_List:
2123  foreach(temp, (List *) node)
2124  {
2125  if (walker((Node *) lfirst(temp), context))
2126  return true;
2127  }
2128  break;
2129  case T_FromExpr:
2130  {
2131  FromExpr *from = (FromExpr *) node;
2132 
2133  if (walker(from->fromlist, context))
2134  return true;
2135  if (walker(from->quals, context))
2136  return true;
2137  }
2138  break;
2139  case T_OnConflictExpr:
2140  {
2141  OnConflictExpr *onconflict = (OnConflictExpr *) node;
2142 
2143  if (walker((Node *) onconflict->arbiterElems, context))
2144  return true;
2145  if (walker(onconflict->arbiterWhere, context))
2146  return true;
2147  if (walker(onconflict->onConflictSet, context))
2148  return true;
2149  if (walker(onconflict->onConflictWhere, context))
2150  return true;
2151  if (walker(onconflict->exclRelTlist, context))
2152  return true;
2153  }
2154  break;
2156  {
2157  PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
2158 
2159  if (walker((Node *) opstep->exprs, context))
2160  return true;
2161  }
2162  break;
2164  /* no expression subnodes */
2165  break;
2166  case T_JoinExpr:
2167  {
2168  JoinExpr *join = (JoinExpr *) node;
2169 
2170  if (walker(join->larg, context))
2171  return true;
2172  if (walker(join->rarg, context))
2173  return true;
2174  if (walker(join->quals, context))
2175  return true;
2176 
2177  /*
2178  * alias clause, using list are deemed uninteresting.
2179  */
2180  }
2181  break;
2182  case T_SetOperationStmt:
2183  {
2184  SetOperationStmt *setop = (SetOperationStmt *) node;
2185 
2186  if (walker(setop->larg, context))
2187  return true;
2188  if (walker(setop->rarg, context))
2189  return true;
2190 
2191  /* groupClauses are deemed uninteresting */
2192  }
2193  break;
2194  case T_IndexClause:
2195  {
2196  IndexClause *iclause = (IndexClause *) node;
2197 
2198  if (walker(iclause->rinfo, context))
2199  return true;
2200  if (expression_tree_walker((Node *) iclause->indexquals,
2201  walker, context))
2202  return true;
2203  }
2204  break;
2205  case T_PlaceHolderVar:
2206  return walker(((PlaceHolderVar *) node)->phexpr, context);
2207  case T_InferenceElem:
2208  return walker(((InferenceElem *) node)->expr, context);
2209  case T_AppendRelInfo:
2210  {
2211  AppendRelInfo *appinfo = (AppendRelInfo *) node;
2212 
2213  if (expression_tree_walker((Node *) appinfo->translated_vars,
2214  walker, context))
2215  return true;
2216  }
2217  break;
2218  case T_PlaceHolderInfo:
2219  return walker(((PlaceHolderInfo *) node)->ph_var, context);
2220  case T_RangeTblFunction:
2221  return walker(((RangeTblFunction *) node)->funcexpr, context);
2222  case T_TableSampleClause:
2223  {
2224  TableSampleClause *tsc = (TableSampleClause *) node;
2225 
2226  if (expression_tree_walker((Node *) tsc->args,
2227  walker, context))
2228  return true;
2229  if (walker((Node *) tsc->repeatable, context))
2230  return true;
2231  }
2232  break;
2233  case T_TableFunc:
2234  {
2235  TableFunc *tf = (TableFunc *) node;
2236 
2237  if (walker(tf->ns_uris, context))
2238  return true;
2239  if (walker(tf->docexpr, context))
2240  return true;
2241  if (walker(tf->rowexpr, context))
2242  return true;
2243  if (walker(tf->colexprs, context))
2244  return true;
2245  if (walker(tf->coldefexprs, context))
2246  return true;
2247  }
2248  break;
2249  default:
2250  elog(ERROR, "unrecognized node type: %d",
2251  (int) nodeTag(node));
2252  break;
2253  }
2254  return false;
2255 }
2256 
2257 /*
2258  * query_tree_walker --- initiate a walk of a Query's expressions
2259  *
2260  * This routine exists just to reduce the number of places that need to know
2261  * where all the expression subtrees of a Query are. Note it can be used
2262  * for starting a walk at top level of a Query regardless of whether the
2263  * walker intends to descend into subqueries. It is also useful for
2264  * descending into subqueries within a walker.
2265  *
2266  * Some callers want to suppress visitation of certain items in the sub-Query,
2267  * typically because they need to process them specially, or don't actually
2268  * want to recurse into subqueries. This is supported by the flags argument,
2269  * which is the bitwise OR of flag values to add or suppress visitation of
2270  * indicated items. (More flag bits may be added as needed.)
2271  */
2272 bool
2274  bool (*walker) (),
2275  void *context,
2276  int flags)
2277 {
2278  Assert(query != NULL && IsA(query, Query));
2279 
2280  /*
2281  * We don't walk any utilityStmt here. However, we can't easily assert
2282  * that it is absent, since there are at least two code paths by which
2283  * action statements from CREATE RULE end up here, and NOTIFY is allowed
2284  * in a rule action.
2285  */
2286 
2287  if (walker((Node *) query->targetList, context))
2288  return true;
2289  if (walker((Node *) query->withCheckOptions, context))
2290  return true;
2291  if (walker((Node *) query->onConflict, context))
2292  return true;
2293  if (walker((Node *) query->returningList, context))
2294  return true;
2295  if (walker((Node *) query->jointree, context))
2296  return true;
2297  if (walker(query->setOperations, context))
2298  return true;
2299  if (walker(query->havingQual, context))
2300  return true;
2301  if (walker(query->limitOffset, context))
2302  return true;
2303  if (walker(query->limitCount, context))
2304  return true;
2305 
2306  /*
2307  * Most callers aren't interested in SortGroupClause nodes since those
2308  * don't contain actual expressions. However they do contain OIDs which
2309  * may be needed by dependency walkers etc.
2310  */
2311  if ((flags & QTW_EXAMINE_SORTGROUP))
2312  {
2313  if (walker((Node *) query->groupClause, context))
2314  return true;
2315  if (walker((Node *) query->windowClause, context))
2316  return true;
2317  if (walker((Node *) query->sortClause, context))
2318  return true;
2319  if (walker((Node *) query->distinctClause, context))
2320  return true;
2321  }
2322  else
2323  {
2324  /*
2325  * But we need to walk the expressions under WindowClause nodes even
2326  * if we're not interested in SortGroupClause nodes.
2327  */
2328  ListCell *lc;
2329 
2330  foreach(lc, query->windowClause)
2331  {
2333 
2334  if (walker(wc->startOffset, context))
2335  return true;
2336  if (walker(wc->endOffset, context))
2337  return true;
2338  }
2339  }
2340 
2341  /*
2342  * groupingSets and rowMarks are not walked:
2343  *
2344  * groupingSets contain only ressortgrouprefs (integers) which are
2345  * meaningless without the corresponding groupClause or tlist.
2346  * Accordingly, any walker that needs to care about them needs to handle
2347  * them itself in its Query processing.
2348  *
2349  * rowMarks is not walked because it contains only rangetable indexes (and
2350  * flags etc.) and therefore should be handled at Query level similarly.
2351  */
2352 
2353  if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2354  {
2355  if (walker((Node *) query->cteList, context))
2356  return true;
2357  }
2358  if (!(flags & QTW_IGNORE_RANGE_TABLE))
2359  {
2360  if (range_table_walker(query->rtable, walker, context, flags))
2361  return true;
2362  }
2363  return false;
2364 }
2365 
2366 /*
2367  * range_table_walker is just the part of query_tree_walker that scans
2368  * a query's rangetable. This is split out since it can be useful on
2369  * its own.
2370  */
2371 bool
2373  bool (*walker) (),
2374  void *context,
2375  int flags)
2376 {
2377  ListCell *rt;
2378 
2379  foreach(rt, rtable)
2380  {
2381  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2382 
2383  /*
2384  * Walkers might need to examine the RTE node itself either before or
2385  * after visiting its contents (or, conceivably, both). Note that if
2386  * you specify neither flag, the walker won't visit the RTE at all.
2387  */
2388  if (flags & QTW_EXAMINE_RTES_BEFORE)
2389  if (walker(rte, context))
2390  return true;
2391 
2392  switch (rte->rtekind)
2393  {
2394  case RTE_RELATION:
2395  if (walker(rte->tablesample, context))
2396  return true;
2397  break;
2398  case RTE_SUBQUERY:
2399  if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2400  if (walker(rte->subquery, context))
2401  return true;
2402  break;
2403  case RTE_JOIN:
2404  if (!(flags & QTW_IGNORE_JOINALIASES))
2405  if (walker(rte->joinaliasvars, context))
2406  return true;
2407  break;
2408  case RTE_FUNCTION:
2409  if (walker(rte->functions, context))
2410  return true;
2411  break;
2412  case RTE_TABLEFUNC:
2413  if (walker(rte->tablefunc, context))
2414  return true;
2415  break;
2416  case RTE_VALUES:
2417  if (walker(rte->values_lists, context))
2418  return true;
2419  break;
2420  case RTE_CTE:
2421  case RTE_NAMEDTUPLESTORE:
2422  case RTE_RESULT:
2423  /* nothing to do */
2424  break;
2425  }
2426 
2427  if (walker(rte->securityQuals, context))
2428  return true;
2429 
2430  if (flags & QTW_EXAMINE_RTES_AFTER)
2431  if (walker(rte, context))
2432  return true;
2433  }
2434  return false;
2435 }
2436 
2437 
2438 /*
2439  * expression_tree_mutator() is designed to support routines that make a
2440  * modified copy of an expression tree, with some nodes being added,
2441  * removed, or replaced by new subtrees. The original tree is (normally)
2442  * not changed. Each recursion level is responsible for returning a copy of
2443  * (or appropriately modified substitute for) the subtree it is handed.
2444  * A mutator routine should look like this:
2445  *
2446  * Node * my_mutator (Node *node, my_struct *context)
2447  * {
2448  * if (node == NULL)
2449  * return NULL;
2450  * // check for nodes that special work is required for, eg:
2451  * if (IsA(node, Var))
2452  * {
2453  * ... create and return modified copy of Var node
2454  * }
2455  * else if (IsA(node, ...))
2456  * {
2457  * ... do special transformations of other node types
2458  * }
2459  * // for any node type not specially processed, do:
2460  * return expression_tree_mutator(node, my_mutator, (void *) context);
2461  * }
2462  *
2463  * The "context" argument points to a struct that holds whatever context
2464  * information the mutator routine needs --- it can be used to return extra
2465  * data gathered by the mutator, too. This argument is not touched by
2466  * expression_tree_mutator, but it is passed down to recursive sub-invocations
2467  * of my_mutator. The tree walk is started from a setup routine that
2468  * fills in the appropriate context struct, calls my_mutator with the
2469  * top-level node of the tree, and does any required post-processing.
2470  *
2471  * Each level of recursion must return an appropriately modified Node.
2472  * If expression_tree_mutator() is called, it will make an exact copy
2473  * of the given Node, but invoke my_mutator() to copy the sub-node(s)
2474  * of that Node. In this way, my_mutator() has full control over the
2475  * copying process but need not directly deal with expression trees
2476  * that it has no interest in.
2477  *
2478  * Just as for expression_tree_walker, the node types handled by
2479  * expression_tree_mutator include all those normally found in target lists
2480  * and qualifier clauses during the planning stage.
2481  *
2482  * expression_tree_mutator will handle SubLink nodes by recursing normally
2483  * into the "testexpr" subtree (which is an expression belonging to the outer
2484  * plan). It will also call the mutator on the sub-Query node; however, when
2485  * expression_tree_mutator itself is called on a Query node, it does nothing
2486  * and returns the unmodified Query node. The net effect is that unless the
2487  * mutator does something special at a Query node, sub-selects will not be
2488  * visited or modified; the original sub-select will be linked to by the new
2489  * SubLink node. Mutators that want to descend into sub-selects will usually
2490  * do so by recognizing Query nodes and calling query_tree_mutator (below).
2491  *
2492  * expression_tree_mutator will handle a SubPlan node by recursing into the
2493  * "testexpr" and the "args" list (which belong to the outer plan), but it
2494  * will simply copy the link to the inner plan, since that's typically what
2495  * expression tree mutators want. A mutator that wants to modify the subplan
2496  * can force appropriate behavior by recognizing SubPlan expression nodes
2497  * and doing the right thing.
2498  */
2499 
2500 Node *
2502  Node *(*mutator) (),
2503  void *context)
2504 {
2505  /*
2506  * The mutator has already decided not to modify the current node, but we
2507  * must call the mutator for any sub-nodes.
2508  */
2509 
2510 #define FLATCOPY(newnode, node, nodetype) \
2511  ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2512  memcpy((newnode), (node), sizeof(nodetype)) )
2513 
2514 #define CHECKFLATCOPY(newnode, node, nodetype) \
2515  ( AssertMacro(IsA((node), nodetype)), \
2516  (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2517  memcpy((newnode), (node), sizeof(nodetype)) )
2518 
2519 #define MUTATE(newfield, oldfield, fieldtype) \
2520  ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
2521 
2522  if (node == NULL)
2523  return NULL;
2524 
2525  /* Guard against stack overflow due to overly complex expressions */
2527 
2528  switch (nodeTag(node))
2529  {
2530  /*
2531  * Primitive node types with no expression subnodes. Var and
2532  * Const are frequent enough to deserve special cases, the others
2533  * we just use copyObject for.
2534  */
2535  case T_Var:
2536  {
2537  Var *var = (Var *) node;
2538  Var *newnode;
2539 
2540  FLATCOPY(newnode, var, Var);
2541  return (Node *) newnode;
2542  }
2543  break;
2544  case T_Const:
2545  {
2546  Const *oldnode = (Const *) node;
2547  Const *newnode;
2548 
2549  FLATCOPY(newnode, oldnode, Const);
2550  /* XXX we don't bother with datumCopy; should we? */
2551  return (Node *) newnode;
2552  }
2553  break;
2554  case T_Param:
2555  case T_CaseTestExpr:
2556  case T_SQLValueFunction:
2557  case T_CoerceToDomainValue:
2558  case T_SetToDefault:
2559  case T_CurrentOfExpr:
2560  case T_NextValueExpr:
2561  case T_RangeTblRef:
2562  case T_SortGroupClause:
2563  return (Node *) copyObject(node);
2564  case T_WithCheckOption:
2565  {
2566  WithCheckOption *wco = (WithCheckOption *) node;
2567  WithCheckOption *newnode;
2568 
2569  FLATCOPY(newnode, wco, WithCheckOption);
2570  MUTATE(newnode->qual, wco->qual, Node *);
2571  return (Node *) newnode;
2572  }
2573  case T_Aggref:
2574  {
2575  Aggref *aggref = (Aggref *) node;
2576  Aggref *newnode;
2577 
2578  FLATCOPY(newnode, aggref, Aggref);
2579  /* assume mutation doesn't change types of arguments */
2580  newnode->aggargtypes = list_copy(aggref->aggargtypes);
2581  MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
2582  MUTATE(newnode->args, aggref->args, List *);
2583  MUTATE(newnode->aggorder, aggref->aggorder, List *);
2584  MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
2585  MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
2586  return (Node *) newnode;
2587  }
2588  break;
2589  case T_GroupingFunc:
2590  {
2591  GroupingFunc *grouping = (GroupingFunc *) node;
2592  GroupingFunc *newnode;
2593 
2594  FLATCOPY(newnode, grouping, GroupingFunc);
2595  MUTATE(newnode->args, grouping->args, List *);
2596 
2597  /*
2598  * We assume here that mutating the arguments does not change
2599  * the semantics, i.e. that the arguments are not mutated in a
2600  * way that makes them semantically different from their
2601  * previously matching expressions in the GROUP BY clause.
2602  *
2603  * If a mutator somehow wanted to do this, it would have to
2604  * handle the refs and cols lists itself as appropriate.
2605  */
2606  newnode->refs = list_copy(grouping->refs);
2607  newnode->cols = list_copy(grouping->cols);
2608 
2609  return (Node *) newnode;
2610  }
2611  break;
2612  case T_WindowFunc:
2613  {
2614  WindowFunc *wfunc = (WindowFunc *) node;
2615  WindowFunc *newnode;
2616 
2617  FLATCOPY(newnode, wfunc, WindowFunc);
2618  MUTATE(newnode->args, wfunc->args, List *);
2619  MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
2620  return (Node *) newnode;
2621  }
2622  break;
2623  case T_SubscriptingRef:
2624  {
2625  SubscriptingRef *sbsref = (SubscriptingRef *) node;
2626  SubscriptingRef *newnode;
2627 
2628  FLATCOPY(newnode, sbsref, SubscriptingRef);
2629  MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
2630  List *);
2631  MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
2632  List *);
2633  MUTATE(newnode->refexpr, sbsref->refexpr,
2634  Expr *);
2635  MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
2636  Expr *);
2637 
2638  return (Node *) newnode;
2639  }
2640  break;
2641  case T_FuncExpr:
2642  {
2643  FuncExpr *expr = (FuncExpr *) node;
2644  FuncExpr *newnode;
2645 
2646  FLATCOPY(newnode, expr, FuncExpr);
2647  MUTATE(newnode->args, expr->args, List *);
2648  return (Node *) newnode;
2649  }
2650  break;
2651  case T_NamedArgExpr:
2652  {
2653  NamedArgExpr *nexpr = (NamedArgExpr *) node;
2654  NamedArgExpr *newnode;
2655 
2656  FLATCOPY(newnode, nexpr, NamedArgExpr);
2657  MUTATE(newnode->arg, nexpr->arg, Expr *);
2658  return (Node *) newnode;
2659  }
2660  break;
2661  case T_OpExpr:
2662  {
2663  OpExpr *expr = (OpExpr *) node;
2664  OpExpr *newnode;
2665 
2666  FLATCOPY(newnode, expr, OpExpr);
2667  MUTATE(newnode->args, expr->args, List *);
2668  return (Node *) newnode;
2669  }
2670  break;
2671  case T_DistinctExpr:
2672  {
2673  DistinctExpr *expr = (DistinctExpr *) node;
2674  DistinctExpr *newnode;
2675 
2676  FLATCOPY(newnode, expr, DistinctExpr);
2677  MUTATE(newnode->args, expr->args, List *);
2678  return (Node *) newnode;
2679  }
2680  break;
2681  case T_NullIfExpr:
2682  {
2683  NullIfExpr *expr = (NullIfExpr *) node;
2684  NullIfExpr *newnode;
2685 
2686  FLATCOPY(newnode, expr, NullIfExpr);
2687  MUTATE(newnode->args, expr->args, List *);
2688  return (Node *) newnode;
2689  }
2690  break;
2691  case T_ScalarArrayOpExpr:
2692  {
2693  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2694  ScalarArrayOpExpr *newnode;
2695 
2696  FLATCOPY(newnode, expr, ScalarArrayOpExpr);
2697  MUTATE(newnode->args, expr->args, List *);
2698  return (Node *) newnode;
2699  }
2700  break;
2701  case T_BoolExpr:
2702  {
2703  BoolExpr *expr = (BoolExpr *) node;
2704  BoolExpr *newnode;
2705 
2706  FLATCOPY(newnode, expr, BoolExpr);
2707  MUTATE(newnode->args, expr->args, List *);
2708  return (Node *) newnode;
2709  }
2710  break;
2711  case T_SubLink:
2712  {
2713  SubLink *sublink = (SubLink *) node;
2714  SubLink *newnode;
2715 
2716  FLATCOPY(newnode, sublink, SubLink);
2717  MUTATE(newnode->testexpr, sublink->testexpr, Node *);
2718 
2719  /*
2720  * Also invoke the mutator on the sublink's Query node, so it
2721  * can recurse into the sub-query if it wants to.
2722  */
2723  MUTATE(newnode->subselect, sublink->subselect, Node *);
2724  return (Node *) newnode;
2725  }
2726  break;
2727  case T_SubPlan:
2728  {
2729  SubPlan *subplan = (SubPlan *) node;
2730  SubPlan *newnode;
2731 
2732  FLATCOPY(newnode, subplan, SubPlan);
2733  /* transform testexpr */
2734  MUTATE(newnode->testexpr, subplan->testexpr, Node *);
2735  /* transform args list (params to be passed to subplan) */
2736  MUTATE(newnode->args, subplan->args, List *);
2737  /* but not the sub-Plan itself, which is referenced as-is */
2738  return (Node *) newnode;
2739  }
2740  break;
2741  case T_AlternativeSubPlan:
2742  {
2743  AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
2744  AlternativeSubPlan *newnode;
2745 
2746  FLATCOPY(newnode, asplan, AlternativeSubPlan);
2747  MUTATE(newnode->subplans, asplan->subplans, List *);
2748  return (Node *) newnode;
2749  }
2750  break;
2751  case T_FieldSelect:
2752  {
2753  FieldSelect *fselect = (FieldSelect *) node;
2754  FieldSelect *newnode;
2755 
2756  FLATCOPY(newnode, fselect, FieldSelect);
2757  MUTATE(newnode->arg, fselect->arg, Expr *);
2758  return (Node *) newnode;
2759  }
2760  break;
2761  case T_FieldStore:
2762  {
2763  FieldStore *fstore = (FieldStore *) node;
2764  FieldStore *newnode;
2765 
2766  FLATCOPY(newnode, fstore, FieldStore);
2767  MUTATE(newnode->arg, fstore->arg, Expr *);
2768  MUTATE(newnode->newvals, fstore->newvals, List *);
2769  newnode->fieldnums = list_copy(fstore->fieldnums);
2770  return (Node *) newnode;
2771  }
2772  break;
2773  case T_RelabelType:
2774  {
2775  RelabelType *relabel = (RelabelType *) node;
2776  RelabelType *newnode;
2777 
2778  FLATCOPY(newnode, relabel, RelabelType);
2779  MUTATE(newnode->arg, relabel->arg, Expr *);
2780  return (Node *) newnode;
2781  }
2782  break;
2783  case T_CoerceViaIO:
2784  {
2785  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
2786  CoerceViaIO *newnode;
2787 
2788  FLATCOPY(newnode, iocoerce, CoerceViaIO);
2789  MUTATE(newnode->arg, iocoerce->arg, Expr *);
2790  return (Node *) newnode;
2791  }
2792  break;
2793  case T_ArrayCoerceExpr:
2794  {
2795  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2796  ArrayCoerceExpr *newnode;
2797 
2798  FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
2799  MUTATE(newnode->arg, acoerce->arg, Expr *);
2800  MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
2801  return (Node *) newnode;
2802  }
2803  break;
2804  case T_ConvertRowtypeExpr:
2805  {
2806  ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
2807  ConvertRowtypeExpr *newnode;
2808 
2809  FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
2810  MUTATE(newnode->arg, convexpr->arg, Expr *);
2811  return (Node *) newnode;
2812  }
2813  break;
2814  case T_CollateExpr:
2815  {
2816  CollateExpr *collate = (CollateExpr *) node;
2817  CollateExpr *newnode;
2818 
2819  FLATCOPY(newnode, collate, CollateExpr);
2820  MUTATE(newnode->arg, collate->arg, Expr *);
2821  return (Node *) newnode;
2822  }
2823  break;
2824  case T_CaseExpr:
2825  {
2826  CaseExpr *caseexpr = (CaseExpr *) node;
2827  CaseExpr *newnode;
2828 
2829  FLATCOPY(newnode, caseexpr, CaseExpr);
2830  MUTATE(newnode->arg, caseexpr->arg, Expr *);
2831  MUTATE(newnode->args, caseexpr->args, List *);
2832  MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
2833  return (Node *) newnode;
2834  }
2835  break;
2836  case T_CaseWhen:
2837  {
2838  CaseWhen *casewhen = (CaseWhen *) node;
2839  CaseWhen *newnode;
2840 
2841  FLATCOPY(newnode, casewhen, CaseWhen);
2842  MUTATE(newnode->expr, casewhen->expr, Expr *);
2843  MUTATE(newnode->result, casewhen->result, Expr *);
2844  return (Node *) newnode;
2845  }
2846  break;
2847  case T_ArrayExpr:
2848  {
2849  ArrayExpr *arrayexpr = (ArrayExpr *) node;
2850  ArrayExpr *newnode;
2851 
2852  FLATCOPY(newnode, arrayexpr, ArrayExpr);
2853  MUTATE(newnode->elements, arrayexpr->elements, List *);
2854  return (Node *) newnode;
2855  }
2856  break;
2857  case T_RowExpr:
2858  {
2859  RowExpr *rowexpr = (RowExpr *) node;
2860  RowExpr *newnode;
2861 
2862  FLATCOPY(newnode, rowexpr, RowExpr);
2863  MUTATE(newnode->args, rowexpr->args, List *);
2864  /* Assume colnames needn't be duplicated */
2865  return (Node *) newnode;
2866  }
2867  break;
2868  case T_RowCompareExpr:
2869  {
2870  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2871  RowCompareExpr *newnode;
2872 
2873  FLATCOPY(newnode, rcexpr, RowCompareExpr);
2874  MUTATE(newnode->largs, rcexpr->largs, List *);
2875  MUTATE(newnode->rargs, rcexpr->rargs, List *);
2876  return (Node *) newnode;
2877  }
2878  break;
2879  case T_CoalesceExpr:
2880  {
2881  CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2882  CoalesceExpr *newnode;
2883 
2884  FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
2885  MUTATE(newnode->args, coalesceexpr->args, List *);
2886  return (Node *) newnode;
2887  }
2888  break;
2889  case T_MinMaxExpr:
2890  {
2891  MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2892  MinMaxExpr *newnode;
2893 
2894  FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
2895  MUTATE(newnode->args, minmaxexpr->args, List *);
2896  return (Node *) newnode;
2897  }
2898  break;
2899  case T_XmlExpr:
2900  {
2901  XmlExpr *xexpr = (XmlExpr *) node;
2902  XmlExpr *newnode;
2903 
2904  FLATCOPY(newnode, xexpr, XmlExpr);
2905  MUTATE(newnode->named_args, xexpr->named_args, List *);
2906  /* assume mutator does not care about arg_names */
2907  MUTATE(newnode->args, xexpr->args, List *);
2908  return (Node *) newnode;
2909  }
2910  break;
2911  case T_NullTest:
2912  {
2913  NullTest *ntest = (NullTest *) node;
2914  NullTest *newnode;
2915 
2916  FLATCOPY(newnode, ntest, NullTest);
2917  MUTATE(newnode->arg, ntest->arg, Expr *);
2918  return (Node *) newnode;
2919  }
2920  break;
2921  case T_BooleanTest:
2922  {
2923  BooleanTest *btest = (BooleanTest *) node;
2924  BooleanTest *newnode;
2925 
2926  FLATCOPY(newnode, btest, BooleanTest);
2927  MUTATE(newnode->arg, btest->arg, Expr *);
2928  return (Node *) newnode;
2929  }
2930  break;
2931  case T_CoerceToDomain:
2932  {
2933  CoerceToDomain *ctest = (CoerceToDomain *) node;
2934  CoerceToDomain *newnode;
2935 
2936  FLATCOPY(newnode, ctest, CoerceToDomain);
2937  MUTATE(newnode->arg, ctest->arg, Expr *);
2938  return (Node *) newnode;
2939  }
2940  break;
2941  case T_TargetEntry:
2942  {
2943  TargetEntry *targetentry = (TargetEntry *) node;
2944  TargetEntry *newnode;
2945 
2946  FLATCOPY(newnode, targetentry, TargetEntry);
2947  MUTATE(newnode->expr, targetentry->expr, Expr *);
2948  return (Node *) newnode;
2949  }
2950  break;
2951  case T_Query:
2952  /* Do nothing with a sub-Query, per discussion above */
2953  return node;
2954  case T_WindowClause:
2955  {
2956  WindowClause *wc = (WindowClause *) node;
2957  WindowClause *newnode;
2958 
2959  FLATCOPY(newnode, wc, WindowClause);
2960  MUTATE(newnode->partitionClause, wc->partitionClause, List *);
2961  MUTATE(newnode->orderClause, wc->orderClause, List *);
2962  MUTATE(newnode->startOffset, wc->startOffset, Node *);
2963  MUTATE(newnode->endOffset, wc->endOffset, Node *);
2964  return (Node *) newnode;
2965  }
2966  break;
2967  case T_CommonTableExpr:
2968  {
2969  CommonTableExpr *cte = (CommonTableExpr *) node;
2970  CommonTableExpr *newnode;
2971 
2972  FLATCOPY(newnode, cte, CommonTableExpr);
2973 
2974  /*
2975  * Also invoke the mutator on the CTE's Query node, so it can
2976  * recurse into the sub-query if it wants to.
2977  */
2978  MUTATE(newnode->ctequery, cte->ctequery, Node *);
2979  return (Node *) newnode;
2980  }
2981  break;
2982  case T_List:
2983  {
2984  /*
2985  * We assume the mutator isn't interested in the list nodes
2986  * per se, so just invoke it on each list element. NOTE: this
2987  * would fail badly on a list with integer elements!
2988  */
2989  List *resultlist;
2990  ListCell *temp;
2991 
2992  resultlist = NIL;
2993  foreach(temp, (List *) node)
2994  {
2995  resultlist = lappend(resultlist,
2996  mutator((Node *) lfirst(temp),
2997  context));
2998  }
2999  return (Node *) resultlist;
3000  }
3001  break;
3002  case T_FromExpr:
3003  {
3004  FromExpr *from = (FromExpr *) node;
3005  FromExpr *newnode;
3006 
3007  FLATCOPY(newnode, from, FromExpr);
3008  MUTATE(newnode->fromlist, from->fromlist, List *);
3009  MUTATE(newnode->quals, from->quals, Node *);
3010  return (Node *) newnode;
3011  }
3012  break;
3013  case T_OnConflictExpr:
3014  {
3015  OnConflictExpr *oc = (OnConflictExpr *) node;
3016  OnConflictExpr *newnode;
3017 
3018  FLATCOPY(newnode, oc, OnConflictExpr);
3019  MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
3020  MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
3021  MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
3022  MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
3023  MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
3024 
3025  return (Node *) newnode;
3026  }
3027  break;
3029  {
3030  PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
3031  PartitionPruneStepOp *newnode;
3032 
3033  FLATCOPY(newnode, opstep, PartitionPruneStepOp);
3034  MUTATE(newnode->exprs, opstep->exprs, List *);
3035 
3036  return (Node *) newnode;
3037  }
3038  break;
3040  /* no expression sub-nodes */
3041  return (Node *) copyObject(node);
3042  case T_JoinExpr:
3043  {
3044  JoinExpr *join = (JoinExpr *) node;
3045  JoinExpr *newnode;
3046 
3047  FLATCOPY(newnode, join, JoinExpr);
3048  MUTATE(newnode->larg, join->larg, Node *);
3049  MUTATE(newnode->rarg, join->rarg, Node *);
3050  MUTATE(newnode->quals, join->quals, Node *);
3051  /* We do not mutate alias or using by default */
3052  return (Node *) newnode;
3053  }
3054  break;
3055  case T_SetOperationStmt:
3056  {
3057  SetOperationStmt *setop = (SetOperationStmt *) node;
3058  SetOperationStmt *newnode;
3059 
3060  FLATCOPY(newnode, setop, SetOperationStmt);
3061  MUTATE(newnode->larg, setop->larg, Node *);
3062  MUTATE(newnode->rarg, setop->rarg, Node *);
3063  /* We do not mutate groupClauses by default */
3064  return (Node *) newnode;
3065  }
3066  break;
3067  case T_IndexClause:
3068  {
3069  IndexClause *iclause = (IndexClause *) node;
3070  IndexClause *newnode;
3071 
3072  FLATCOPY(newnode, iclause, IndexClause);
3073  MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
3074  MUTATE(newnode->indexquals, iclause->indexquals, List *);
3075  return (Node *) newnode;
3076  }
3077  break;
3078  case T_PlaceHolderVar:
3079  {
3080  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3081  PlaceHolderVar *newnode;
3082 
3083  FLATCOPY(newnode, phv, PlaceHolderVar);
3084  MUTATE(newnode->phexpr, phv->phexpr, Expr *);
3085  /* Assume we need not copy the relids bitmapset */
3086  return (Node *) newnode;
3087  }
3088  break;
3089  case T_InferenceElem:
3090  {
3091  InferenceElem *inferenceelemdexpr = (InferenceElem *) node;
3092  InferenceElem *newnode;
3093 
3094  FLATCOPY(newnode, inferenceelemdexpr, InferenceElem);
3095  MUTATE(newnode->expr, newnode->expr, Node *);
3096  return (Node *) newnode;
3097  }
3098  break;
3099  case T_AppendRelInfo:
3100  {
3101  AppendRelInfo *appinfo = (AppendRelInfo *) node;
3102  AppendRelInfo *newnode;
3103 
3104  FLATCOPY(newnode, appinfo, AppendRelInfo);
3105  MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
3106  /* Assume nothing need be done with parent_colnos[] */
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:2273
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:2501
Node * subquery
Definition: parsenodes.h:542
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
PlaceHolderVar * ph_var
Definition: pathnodes.h:2267
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:1191
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:275
static int leftmostLoc(int loc1, int loc2)
Definition: nodeFuncs.c:1566
List * initPlan
Definition: execnodes.h:964
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:675
List * securityQuals
Definition: parsenodes.h:1102
List * withCheckOptions
Definition: parsenodes.h:171
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1587
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:911
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:669
List * columns
Definition: parsenodes.h:582
List * subPlan
Definition: execnodes.h:966
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:608
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:512
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:645
WithClause * withClause
Definition: parsenodes.h:1510
List * translated_vars
Definition: pathnodes.h:2217
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:347
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:1656
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:847
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:1033
static bool fix_opfuncids_walker(Node *node, void *context)
Definition: nodeFuncs.c:1594
Node * rowexpr
Definition: primnodes.h:88
Node * limitCount
Definition: parsenodes.h:161
char * c
struct RestrictInfo * rinfo
Definition: pathnodes.h:1225
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:3302
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:1226
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:1127
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:939
#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:924
#define Assert(condition)
Definition: c.h:739
#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:2372
int location
Definition: primnodes.h:1208
Node * endOffset
Definition: parsenodes.h:1334
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
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:1839
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
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:972
Query * subquery
Definition: parsenodes.h:1009
List * groupClause
Definition: parsenodes.h:148
int errmsg(const char *fmt,...)
Definition: elog.c:822
bool(* check_function_callback)(Oid func_id, void *context)
Definition: nodeFuncs.h:33
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1618
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:228
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:1032
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:1629
int location
Definition: primnodes.h:805
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:587
#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:611
List * args
Definition: primnodes.h:716