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-2020, 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  {
2382 
2383  if (range_table_entry_walker(rte, walker, context, flags))
2384  return true;
2385  }
2386  return false;
2387 }
2388 
2389 /*
2390  * Some callers even want to scan the expressions in individual RTEs.
2391  */
2392 bool
2394  bool (*walker) (),
2395  void *context,
2396  int flags)
2397 {
2398  /*
2399  * Walkers might need to examine the RTE node itself either before or
2400  * after visiting its contents (or, conceivably, both). Note that if you
2401  * specify neither flag, the walker won't be called on the RTE at all.
2402  */
2403  if (flags & QTW_EXAMINE_RTES_BEFORE)
2404  if (walker(rte, context))
2405  return true;
2406 
2407  switch (rte->rtekind)
2408  {
2409  case RTE_RELATION:
2410  if (walker(rte->tablesample, context))
2411  return true;
2412  break;
2413  case RTE_SUBQUERY:
2414  if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2415  if (walker(rte->subquery, context))
2416  return true;
2417  break;
2418  case RTE_JOIN:
2419  if (!(flags & QTW_IGNORE_JOINALIASES))
2420  if (walker(rte->joinaliasvars, context))
2421  return true;
2422  break;
2423  case RTE_FUNCTION:
2424  if (walker(rte->functions, context))
2425  return true;
2426  break;
2427  case RTE_TABLEFUNC:
2428  if (walker(rte->tablefunc, context))
2429  return true;
2430  break;
2431  case RTE_VALUES:
2432  if (walker(rte->values_lists, context))
2433  return true;
2434  break;
2435  case RTE_CTE:
2436  case RTE_NAMEDTUPLESTORE:
2437  case RTE_RESULT:
2438  /* nothing to do */
2439  break;
2440  }
2441 
2442  if (walker(rte->securityQuals, context))
2443  return true;
2444 
2445  if (flags & QTW_EXAMINE_RTES_AFTER)
2446  if (walker(rte, context))
2447  return true;
2448 
2449  return false;
2450 }
2451 
2452 
2453 /*
2454  * expression_tree_mutator() is designed to support routines that make a
2455  * modified copy of an expression tree, with some nodes being added,
2456  * removed, or replaced by new subtrees. The original tree is (normally)
2457  * not changed. Each recursion level is responsible for returning a copy of
2458  * (or appropriately modified substitute for) the subtree it is handed.
2459  * A mutator routine should look like this:
2460  *
2461  * Node * my_mutator (Node *node, my_struct *context)
2462  * {
2463  * if (node == NULL)
2464  * return NULL;
2465  * // check for nodes that special work is required for, eg:
2466  * if (IsA(node, Var))
2467  * {
2468  * ... create and return modified copy of Var node
2469  * }
2470  * else if (IsA(node, ...))
2471  * {
2472  * ... do special transformations of other node types
2473  * }
2474  * // for any node type not specially processed, do:
2475  * return expression_tree_mutator(node, my_mutator, (void *) context);
2476  * }
2477  *
2478  * The "context" argument points to a struct that holds whatever context
2479  * information the mutator routine needs --- it can be used to return extra
2480  * data gathered by the mutator, too. This argument is not touched by
2481  * expression_tree_mutator, but it is passed down to recursive sub-invocations
2482  * of my_mutator. The tree walk is started from a setup routine that
2483  * fills in the appropriate context struct, calls my_mutator with the
2484  * top-level node of the tree, and does any required post-processing.
2485  *
2486  * Each level of recursion must return an appropriately modified Node.
2487  * If expression_tree_mutator() is called, it will make an exact copy
2488  * of the given Node, but invoke my_mutator() to copy the sub-node(s)
2489  * of that Node. In this way, my_mutator() has full control over the
2490  * copying process but need not directly deal with expression trees
2491  * that it has no interest in.
2492  *
2493  * Just as for expression_tree_walker, the node types handled by
2494  * expression_tree_mutator include all those normally found in target lists
2495  * and qualifier clauses during the planning stage.
2496  *
2497  * expression_tree_mutator will handle SubLink nodes by recursing normally
2498  * into the "testexpr" subtree (which is an expression belonging to the outer
2499  * plan). It will also call the mutator on the sub-Query node; however, when
2500  * expression_tree_mutator itself is called on a Query node, it does nothing
2501  * and returns the unmodified Query node. The net effect is that unless the
2502  * mutator does something special at a Query node, sub-selects will not be
2503  * visited or modified; the original sub-select will be linked to by the new
2504  * SubLink node. Mutators that want to descend into sub-selects will usually
2505  * do so by recognizing Query nodes and calling query_tree_mutator (below).
2506  *
2507  * expression_tree_mutator will handle a SubPlan node by recursing into the
2508  * "testexpr" and the "args" list (which belong to the outer plan), but it
2509  * will simply copy the link to the inner plan, since that's typically what
2510  * expression tree mutators want. A mutator that wants to modify the subplan
2511  * can force appropriate behavior by recognizing SubPlan expression nodes
2512  * and doing the right thing.
2513  */
2514 
2515 Node *
2517  Node *(*mutator) (),
2518  void *context)
2519 {
2520  /*
2521  * The mutator has already decided not to modify the current node, but we
2522  * must call the mutator for any sub-nodes.
2523  */
2524 
2525 #define FLATCOPY(newnode, node, nodetype) \
2526  ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2527  memcpy((newnode), (node), sizeof(nodetype)) )
2528 
2529 #define CHECKFLATCOPY(newnode, node, nodetype) \
2530  ( AssertMacro(IsA((node), nodetype)), \
2531  (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2532  memcpy((newnode), (node), sizeof(nodetype)) )
2533 
2534 #define MUTATE(newfield, oldfield, fieldtype) \
2535  ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
2536 
2537  if (node == NULL)
2538  return NULL;
2539 
2540  /* Guard against stack overflow due to overly complex expressions */
2542 
2543  switch (nodeTag(node))
2544  {
2545  /*
2546  * Primitive node types with no expression subnodes. Var and
2547  * Const are frequent enough to deserve special cases, the others
2548  * we just use copyObject for.
2549  */
2550  case T_Var:
2551  {
2552  Var *var = (Var *) node;
2553  Var *newnode;
2554 
2555  FLATCOPY(newnode, var, Var);
2556  return (Node *) newnode;
2557  }
2558  break;
2559  case T_Const:
2560  {
2561  Const *oldnode = (Const *) node;
2562  Const *newnode;
2563 
2564  FLATCOPY(newnode, oldnode, Const);
2565  /* XXX we don't bother with datumCopy; should we? */
2566  return (Node *) newnode;
2567  }
2568  break;
2569  case T_Param:
2570  case T_CaseTestExpr:
2571  case T_SQLValueFunction:
2572  case T_CoerceToDomainValue:
2573  case T_SetToDefault:
2574  case T_CurrentOfExpr:
2575  case T_NextValueExpr:
2576  case T_RangeTblRef:
2577  case T_SortGroupClause:
2578  return (Node *) copyObject(node);
2579  case T_WithCheckOption:
2580  {
2581  WithCheckOption *wco = (WithCheckOption *) node;
2582  WithCheckOption *newnode;
2583 
2584  FLATCOPY(newnode, wco, WithCheckOption);
2585  MUTATE(newnode->qual, wco->qual, Node *);
2586  return (Node *) newnode;
2587  }
2588  case T_Aggref:
2589  {
2590  Aggref *aggref = (Aggref *) node;
2591  Aggref *newnode;
2592 
2593  FLATCOPY(newnode, aggref, Aggref);
2594  /* assume mutation doesn't change types of arguments */
2595  newnode->aggargtypes = list_copy(aggref->aggargtypes);
2596  MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
2597  MUTATE(newnode->args, aggref->args, List *);
2598  MUTATE(newnode->aggorder, aggref->aggorder, List *);
2599  MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
2600  MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
2601  return (Node *) newnode;
2602  }
2603  break;
2604  case T_GroupingFunc:
2605  {
2606  GroupingFunc *grouping = (GroupingFunc *) node;
2607  GroupingFunc *newnode;
2608 
2609  FLATCOPY(newnode, grouping, GroupingFunc);
2610  MUTATE(newnode->args, grouping->args, List *);
2611 
2612  /*
2613  * We assume here that mutating the arguments does not change
2614  * the semantics, i.e. that the arguments are not mutated in a
2615  * way that makes them semantically different from their
2616  * previously matching expressions in the GROUP BY clause.
2617  *
2618  * If a mutator somehow wanted to do this, it would have to
2619  * handle the refs and cols lists itself as appropriate.
2620  */
2621  newnode->refs = list_copy(grouping->refs);
2622  newnode->cols = list_copy(grouping->cols);
2623 
2624  return (Node *) newnode;
2625  }
2626  break;
2627  case T_WindowFunc:
2628  {
2629  WindowFunc *wfunc = (WindowFunc *) node;
2630  WindowFunc *newnode;
2631 
2632  FLATCOPY(newnode, wfunc, WindowFunc);
2633  MUTATE(newnode->args, wfunc->args, List *);
2634  MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
2635  return (Node *) newnode;
2636  }
2637  break;
2638  case T_SubscriptingRef:
2639  {
2640  SubscriptingRef *sbsref = (SubscriptingRef *) node;
2641  SubscriptingRef *newnode;
2642 
2643  FLATCOPY(newnode, sbsref, SubscriptingRef);
2644  MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
2645  List *);
2646  MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
2647  List *);
2648  MUTATE(newnode->refexpr, sbsref->refexpr,
2649  Expr *);
2650  MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
2651  Expr *);
2652 
2653  return (Node *) newnode;
2654  }
2655  break;
2656  case T_FuncExpr:
2657  {
2658  FuncExpr *expr = (FuncExpr *) node;
2659  FuncExpr *newnode;
2660 
2661  FLATCOPY(newnode, expr, FuncExpr);
2662  MUTATE(newnode->args, expr->args, List *);
2663  return (Node *) newnode;
2664  }
2665  break;
2666  case T_NamedArgExpr:
2667  {
2668  NamedArgExpr *nexpr = (NamedArgExpr *) node;
2669  NamedArgExpr *newnode;
2670 
2671  FLATCOPY(newnode, nexpr, NamedArgExpr);
2672  MUTATE(newnode->arg, nexpr->arg, Expr *);
2673  return (Node *) newnode;
2674  }
2675  break;
2676  case T_OpExpr:
2677  {
2678  OpExpr *expr = (OpExpr *) node;
2679  OpExpr *newnode;
2680 
2681  FLATCOPY(newnode, expr, OpExpr);
2682  MUTATE(newnode->args, expr->args, List *);
2683  return (Node *) newnode;
2684  }
2685  break;
2686  case T_DistinctExpr:
2687  {
2688  DistinctExpr *expr = (DistinctExpr *) node;
2689  DistinctExpr *newnode;
2690 
2691  FLATCOPY(newnode, expr, DistinctExpr);
2692  MUTATE(newnode->args, expr->args, List *);
2693  return (Node *) newnode;
2694  }
2695  break;
2696  case T_NullIfExpr:
2697  {
2698  NullIfExpr *expr = (NullIfExpr *) node;
2699  NullIfExpr *newnode;
2700 
2701  FLATCOPY(newnode, expr, NullIfExpr);
2702  MUTATE(newnode->args, expr->args, List *);
2703  return (Node *) newnode;
2704  }
2705  break;
2706  case T_ScalarArrayOpExpr:
2707  {
2708  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2709  ScalarArrayOpExpr *newnode;
2710 
2711  FLATCOPY(newnode, expr, ScalarArrayOpExpr);
2712  MUTATE(newnode->args, expr->args, List *);
2713  return (Node *) newnode;
2714  }
2715  break;
2716  case T_BoolExpr:
2717  {
2718  BoolExpr *expr = (BoolExpr *) node;
2719  BoolExpr *newnode;
2720 
2721  FLATCOPY(newnode, expr, BoolExpr);
2722  MUTATE(newnode->args, expr->args, List *);
2723  return (Node *) newnode;
2724  }
2725  break;
2726  case T_SubLink:
2727  {
2728  SubLink *sublink = (SubLink *) node;
2729  SubLink *newnode;
2730 
2731  FLATCOPY(newnode, sublink, SubLink);
2732  MUTATE(newnode->testexpr, sublink->testexpr, Node *);
2733 
2734  /*
2735  * Also invoke the mutator on the sublink's Query node, so it
2736  * can recurse into the sub-query if it wants to.
2737  */
2738  MUTATE(newnode->subselect, sublink->subselect, Node *);
2739  return (Node *) newnode;
2740  }
2741  break;
2742  case T_SubPlan:
2743  {
2744  SubPlan *subplan = (SubPlan *) node;
2745  SubPlan *newnode;
2746 
2747  FLATCOPY(newnode, subplan, SubPlan);
2748  /* transform testexpr */
2749  MUTATE(newnode->testexpr, subplan->testexpr, Node *);
2750  /* transform args list (params to be passed to subplan) */
2751  MUTATE(newnode->args, subplan->args, List *);
2752  /* but not the sub-Plan itself, which is referenced as-is */
2753  return (Node *) newnode;
2754  }
2755  break;
2756  case T_AlternativeSubPlan:
2757  {
2758  AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
2759  AlternativeSubPlan *newnode;
2760 
2761  FLATCOPY(newnode, asplan, AlternativeSubPlan);
2762  MUTATE(newnode->subplans, asplan->subplans, List *);
2763  return (Node *) newnode;
2764  }
2765  break;
2766  case T_FieldSelect:
2767  {
2768  FieldSelect *fselect = (FieldSelect *) node;
2769  FieldSelect *newnode;
2770 
2771  FLATCOPY(newnode, fselect, FieldSelect);
2772  MUTATE(newnode->arg, fselect->arg, Expr *);
2773  return (Node *) newnode;
2774  }
2775  break;
2776  case T_FieldStore:
2777  {
2778  FieldStore *fstore = (FieldStore *) node;
2779  FieldStore *newnode;
2780 
2781  FLATCOPY(newnode, fstore, FieldStore);
2782  MUTATE(newnode->arg, fstore->arg, Expr *);
2783  MUTATE(newnode->newvals, fstore->newvals, List *);
2784  newnode->fieldnums = list_copy(fstore->fieldnums);
2785  return (Node *) newnode;
2786  }
2787  break;
2788  case T_RelabelType:
2789  {
2790  RelabelType *relabel = (RelabelType *) node;
2791  RelabelType *newnode;
2792 
2793  FLATCOPY(newnode, relabel, RelabelType);
2794  MUTATE(newnode->arg, relabel->arg, Expr *);
2795  return (Node *) newnode;
2796  }
2797  break;
2798  case T_CoerceViaIO:
2799  {
2800  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
2801  CoerceViaIO *newnode;
2802 
2803  FLATCOPY(newnode, iocoerce, CoerceViaIO);
2804  MUTATE(newnode->arg, iocoerce->arg, Expr *);
2805  return (Node *) newnode;
2806  }
2807  break;
2808  case T_ArrayCoerceExpr:
2809  {
2810  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2811  ArrayCoerceExpr *newnode;
2812 
2813  FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
2814  MUTATE(newnode->arg, acoerce->arg, Expr *);
2815  MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
2816  return (Node *) newnode;
2817  }
2818  break;
2819  case T_ConvertRowtypeExpr:
2820  {
2821  ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
2822  ConvertRowtypeExpr *newnode;
2823 
2824  FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
2825  MUTATE(newnode->arg, convexpr->arg, Expr *);
2826  return (Node *) newnode;
2827  }
2828  break;
2829  case T_CollateExpr:
2830  {
2831  CollateExpr *collate = (CollateExpr *) node;
2832  CollateExpr *newnode;
2833 
2834  FLATCOPY(newnode, collate, CollateExpr);
2835  MUTATE(newnode->arg, collate->arg, Expr *);
2836  return (Node *) newnode;
2837  }
2838  break;
2839  case T_CaseExpr:
2840  {
2841  CaseExpr *caseexpr = (CaseExpr *) node;
2842  CaseExpr *newnode;
2843 
2844  FLATCOPY(newnode, caseexpr, CaseExpr);
2845  MUTATE(newnode->arg, caseexpr->arg, Expr *);
2846  MUTATE(newnode->args, caseexpr->args, List *);
2847  MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
2848  return (Node *) newnode;
2849  }
2850  break;
2851  case T_CaseWhen:
2852  {
2853  CaseWhen *casewhen = (CaseWhen *) node;
2854  CaseWhen *newnode;
2855 
2856  FLATCOPY(newnode, casewhen, CaseWhen);
2857  MUTATE(newnode->expr, casewhen->expr, Expr *);
2858  MUTATE(newnode->result, casewhen->result, Expr *);
2859  return (Node *) newnode;
2860  }
2861  break;
2862  case T_ArrayExpr:
2863  {
2864  ArrayExpr *arrayexpr = (ArrayExpr *) node;
2865  ArrayExpr *newnode;
2866 
2867  FLATCOPY(newnode, arrayexpr, ArrayExpr);
2868  MUTATE(newnode->elements, arrayexpr->elements, List *);
2869  return (Node *) newnode;
2870  }
2871  break;
2872  case T_RowExpr:
2873  {
2874  RowExpr *rowexpr = (RowExpr *) node;
2875  RowExpr *newnode;
2876 
2877  FLATCOPY(newnode, rowexpr, RowExpr);
2878  MUTATE(newnode->args, rowexpr->args, List *);
2879  /* Assume colnames needn't be duplicated */
2880  return (Node *) newnode;
2881  }
2882  break;
2883  case T_RowCompareExpr:
2884  {
2885  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2886  RowCompareExpr *newnode;
2887 
2888  FLATCOPY(newnode, rcexpr, RowCompareExpr);
2889  MUTATE(newnode->largs, rcexpr->largs, List *);
2890  MUTATE(newnode->rargs, rcexpr->rargs, List *);
2891  return (Node *) newnode;
2892  }
2893  break;
2894  case T_CoalesceExpr:
2895  {
2896  CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2897  CoalesceExpr *newnode;
2898 
2899  FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
2900  MUTATE(newnode->args, coalesceexpr->args, List *);
2901  return (Node *) newnode;
2902  }
2903  break;
2904  case T_MinMaxExpr:
2905  {
2906  MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2907  MinMaxExpr *newnode;
2908 
2909  FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
2910  MUTATE(newnode->args, minmaxexpr->args, List *);
2911  return (Node *) newnode;
2912  }
2913  break;
2914  case T_XmlExpr:
2915  {
2916  XmlExpr *xexpr = (XmlExpr *) node;
2917  XmlExpr *newnode;
2918 
2919  FLATCOPY(newnode, xexpr, XmlExpr);
2920  MUTATE(newnode->named_args, xexpr->named_args, List *);
2921  /* assume mutator does not care about arg_names */
2922  MUTATE(newnode->args, xexpr->args, List *);
2923  return (Node *) newnode;
2924  }
2925  break;
2926  case T_NullTest:
2927  {
2928  NullTest *ntest = (NullTest *) node;
2929  NullTest *newnode;
2930 
2931  FLATCOPY(newnode, ntest, NullTest);
2932  MUTATE(newnode->arg, ntest->arg, Expr *);
2933  return (Node *) newnode;
2934  }
2935  break;
2936  case T_BooleanTest:
2937  {
2938  BooleanTest *btest = (BooleanTest *) node;
2939  BooleanTest *newnode;
2940 
2941  FLATCOPY(newnode, btest, BooleanTest);
2942  MUTATE(newnode->arg, btest->arg, Expr *);
2943  return (Node *) newnode;
2944  }
2945  break;
2946  case T_CoerceToDomain:
2947  {
2948  CoerceToDomain *ctest = (CoerceToDomain *) node;
2949  CoerceToDomain *newnode;
2950 
2951  FLATCOPY(newnode, ctest, CoerceToDomain);
2952  MUTATE(newnode->arg, ctest->arg, Expr *);
2953  return (Node *) newnode;
2954  }
2955  break;
2956  case T_TargetEntry:
2957  {
2958  TargetEntry *targetentry = (TargetEntry *) node;
2959  TargetEntry *newnode;
2960 
2961  FLATCOPY(newnode, targetentry, TargetEntry);
2962  MUTATE(newnode->expr, targetentry->expr, Expr *);
2963  return (Node *) newnode;
2964  }
2965  break;
2966  case T_Query:
2967  /* Do nothing with a sub-Query, per discussion above */
2968  return node;
2969  case T_WindowClause:
2970  {
2971  WindowClause *wc = (WindowClause *) node;
2972  WindowClause *newnode;
2973 
2974  FLATCOPY(newnode, wc, WindowClause);
2975  MUTATE(newnode->partitionClause, wc->partitionClause, List *);
2976  MUTATE(newnode->orderClause, wc->orderClause, List *);
2977  MUTATE(newnode->startOffset, wc->startOffset, Node *);
2978  MUTATE(newnode->endOffset, wc->endOffset, Node *);
2979  return (Node *) newnode;
2980  }
2981  break;
2982  case T_CommonTableExpr:
2983  {
2984  CommonTableExpr *cte = (CommonTableExpr *) node;
2985  CommonTableExpr *newnode;
2986 
2987  FLATCOPY(newnode, cte, CommonTableExpr);
2988 
2989  /*
2990  * Also invoke the mutator on the CTE's Query node, so it can
2991  * recurse into the sub-query if it wants to.
2992  */
2993  MUTATE(newnode->ctequery, cte->ctequery, Node *);
2994  return (Node *) newnode;
2995  }
2996  break;
2997  case T_List:
2998  {
2999  /*
3000  * We assume the mutator isn't interested in the list nodes
3001  * per se, so just invoke it on each list element. NOTE: this
3002  * would fail badly on a list with integer elements!
3003  */
3004  List *resultlist;
3005  ListCell *temp;
3006 
3007  resultlist = NIL;
3008  foreach(temp, (List *) node)
3009  {
3010  resultlist = lappend(resultlist,
3011  mutator((Node *) lfirst(temp),
3012  context));
3013  }
3014  return (Node *) resultlist;
3015  }
3016  break;
3017  case T_FromExpr:
3018  {
3019  FromExpr *from = (FromExpr *) node;
3020  FromExpr *newnode;
3021 
3022  FLATCOPY(newnode, from, FromExpr);
3023  MUTATE(newnode->fromlist, from->fromlist, List *);
3024  MUTATE(newnode->quals, from->quals, Node *);
3025  return (Node *) newnode;
3026  }
3027  break;
3028  case T_OnConflictExpr:
3029  {
3030  OnConflictExpr *oc = (OnConflictExpr *) node;
3031  OnConflictExpr *newnode;
3032 
3033  FLATCOPY(newnode, oc, OnConflictExpr);
3034  MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
3035  MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
3036  MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
3037  MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
3038  MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
3039 
3040  return (Node *) newnode;
3041  }
3042  break;
3044  {
3045  PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
3046  PartitionPruneStepOp *newnode;
3047 
3048  FLATCOPY(newnode, opstep, PartitionPruneStepOp);
3049  MUTATE(newnode->exprs, opstep->exprs, List *);
3050 
3051  return (Node *) newnode;
3052  }
3053  break;
3055  /* no expression sub-nodes */
3056  return (Node *) copyObject(node);
3057  case T_JoinExpr:
3058  {
3059  JoinExpr *join = (JoinExpr *) node;
3060  JoinExpr *newnode;
3061 
3062  FLATCOPY(newnode, join, JoinExpr);
3063  MUTATE(newnode->larg, join->larg, Node *);
3064  MUTATE(newnode->rarg, join->rarg, Node *);
3065  MUTATE(newnode->quals, join->quals, Node *);
3066  /* We do not mutate alias or using by default */
3067  return (Node *) newnode;
3068  }
3069  break;
3070  case T_SetOperationStmt:
3071  {
3072  SetOperationStmt *setop = (SetOperationStmt *) node;
3073  SetOperationStmt *newnode;
3074 
3075  FLATCOPY(newnode, setop, SetOperationStmt);
3076  MUTATE(newnode->larg, setop->larg, Node *);
3077  MUTATE(newnode->rarg, setop->rarg, Node *);
3078  /* We do not mutate groupClauses by default */
3079  return (Node *) newnode;
3080  }
3081  break;
3082  case T_IndexClause:
3083  {
3084  IndexClause *iclause = (IndexClause *) node;
3085  IndexClause *newnode;
3086 
3087  FLATCOPY(newnode, iclause, IndexClause);
3088  MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
3089  MUTATE(newnode->indexquals, iclause->indexquals, List *);
3090  return (Node *) newnode;
3091  }
3092  break;
3093  case T_PlaceHolderVar:
3094  {
3095  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3096  PlaceHolderVar *newnode;
3097 
3098  FLATCOPY(newnode, phv, PlaceHolderVar);
3099  MUTATE(newnode->phexpr, phv->phexpr, Expr *);
3100  /* Assume we need not copy the relids bitmapset */
3101  return (Node *) newnode;
3102  }
3103  break;
3104  case T_InferenceElem:
3105  {
3106  InferenceElem *inferenceelemdexpr = (InferenceElem *) node;
3107  InferenceElem *newnode;
3108 
3109  FLATCOPY(newnode, inferenceelemdexpr, InferenceElem);
3110  MUTATE(newnode->expr, newnode->expr, Node *);
3111  return (Node *) newnode;
3112  }
3113  break;
3114  case T_AppendRelInfo:
3115  {
3116  AppendRelInfo *appinfo = (AppendRelInfo *) node;
3117  AppendRelInfo *newnode;
3118 
3119  FLATCOPY(newnode, appinfo, AppendRelInfo);
3120  MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
3121  /* Assume nothing need be done with parent_colnos[] */
3122  return (Node *) newnode;
3123  }
3124  break;
3125  case T_PlaceHolderInfo:
3126  {
3127  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
3128  PlaceHolderInfo *newnode;
3129 
3130  FLATCOPY(newnode, phinfo, PlaceHolderInfo);
3131  MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
3132  /* Assume we need not copy the relids bitmapsets */
3133  return (Node *) newnode;
3134  }
3135  break;
3136  case T_RangeTblFunction:
3137  {
3138  RangeTblFunction *rtfunc = (RangeTblFunction *) node;
3139  RangeTblFunction *newnode;
3140 
3141  FLATCOPY(newnode, rtfunc, RangeTblFunction);
3142  MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
3143  /* Assume we need not copy the coldef info lists */
3144  return (Node *) newnode;
3145  }
3146  break;
3147  case T_TableSampleClause:
3148  {
3149  TableSampleClause *tsc = (TableSampleClause *) node;
3150  TableSampleClause *newnode;
3151 
3152  FLATCOPY(newnode, tsc, TableSampleClause);
3153  MUTATE(newnode->args, tsc->args, List *);
3154  MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
3155  return (Node *) newnode;
3156  }
3157  break;
3158  case T_TableFunc:
3159  {
3160  TableFunc *tf = (TableFunc *) node;
3161  TableFunc *newnode;
3162 
3163  FLATCOPY(newnode, tf, TableFunc);
3164  MUTATE(newnode->ns_uris, tf->ns_uris, List *);
3165  MUTATE(newnode->docexpr, tf->docexpr, Node *);
3166  MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
3167  MUTATE(newnode->colexprs, tf->colexprs, List *);
3168  MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
3169  return (Node *) newnode;
3170  }
3171  break;
3172  default:
3173  elog(ERROR, "unrecognized node type: %d",
3174  (int) nodeTag(node));
3175  break;
3176  }
3177  /* can't get here, but keep compiler happy */
3178  return NULL;
3179 }
3180 
3181 
3182 /*
3183  * query_tree_mutator --- initiate modification of a Query's expressions
3184  *
3185  * This routine exists just to reduce the number of places that need to know
3186  * where all the expression subtrees of a Query are. Note it can be used
3187  * for starting a walk at top level of a Query regardless of whether the
3188  * mutator intends to descend into subqueries. It is also useful for
3189  * descending into subqueries within a mutator.
3190  *
3191  * Some callers want to suppress mutating of certain items in the Query,
3192  * typically because they need to process them specially, or don't actually
3193  * want to recurse into subqueries. This is supported by the flags argument,
3194  * which is the bitwise OR of flag values to suppress mutating of
3195  * indicated items. (More flag bits may be added as needed.)
3196  *
3197  * Normally the Query node itself is copied, but some callers want it to be
3198  * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags. All
3199  * modified substructure is safely copied in any case.
3200  */
3201 Query *
3203  Node *(*mutator) (),
3204  void *context,
3205  int flags)
3206 {
3207  Assert(query != NULL && IsA(query, Query));
3208 
3209  if (!(flags & QTW_DONT_COPY_QUERY))
3210  {
3211  Query *newquery;
3212 
3213  FLATCOPY(newquery, query, Query);
3214  query = newquery;
3215  }
3216 
3217  MUTATE(query->targetList, query->targetList, List *);
3218  MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
3219  MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
3220  MUTATE(query->returningList, query->returningList, List *);
3221  MUTATE(query->jointree, query->jointree, FromExpr *);
3222  MUTATE(query->setOperations, query->setOperations, Node *);
3223  MUTATE(query->havingQual, query->havingQual, Node *);
3224  MUTATE(query->limitOffset, query->limitOffset, Node *);
3225  MUTATE(query->limitCount, query->limitCount, Node *);
3226 
3227  /*
3228  * Most callers aren't interested in SortGroupClause nodes since those
3229  * don't contain actual expressions. However they do contain OIDs, which
3230  * may be of interest to some mutators.
3231  */
3232 
3233  if ((flags & QTW_EXAMINE_SORTGROUP))
3234  {
3235  MUTATE(query->groupClause, query->groupClause, List *);
3236  MUTATE(query->windowClause, query->windowClause, List *);
3237  MUTATE(query->sortClause, query->sortClause, List *);
3238  MUTATE(query->distinctClause, query->distinctClause, List *);
3239  }
3240  else
3241  {
3242  /*
3243  * But we need to mutate the expressions under WindowClause nodes even
3244  * if we're not interested in SortGroupClause nodes.
3245  */
3246  List *resultlist;
3247  ListCell *temp;
3248 
3249  resultlist = NIL;
3250  foreach(temp, query->windowClause)
3251  {
3252  WindowClause *wc = lfirst_node(WindowClause, temp);
3253  WindowClause *newnode;
3254 
3255  FLATCOPY(newnode, wc, WindowClause);
3256  MUTATE(newnode->startOffset, wc->startOffset, Node *);
3257  MUTATE(newnode->endOffset, wc->endOffset, Node *);
3258 
3259  resultlist = lappend(resultlist, (Node *) newnode);
3260  }
3261  query->windowClause = resultlist;
3262  }
3263 
3264  /*
3265  * groupingSets and rowMarks are not mutated:
3266  *
3267  * groupingSets contain only ressortgroup refs (integers) which are
3268  * meaningless without the groupClause or tlist. Accordingly, any mutator
3269  * that needs to care about them needs to handle them itself in its Query
3270  * processing.
3271  *
3272  * rowMarks contains only rangetable indexes (and flags etc.) and
3273  * therefore should be handled at Query level similarly.
3274  */
3275 
3276  if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
3277  MUTATE(query->cteList, query->cteList, List *);
3278  else /* else copy CTE list as-is */
3279  query->cteList = copyObject(query->cteList);
3280  query->rtable = range_table_mutator(query->rtable,
3281  mutator, context, flags);
3282  return query;
3283 }
3284 
3285 /*
3286  * range_table_mutator is just the part of query_tree_mutator that processes
3287  * a query's rangetable. This is split out since it can be useful on
3288  * its own.
3289  */
3290 List *
3292  Node *(*mutator) (),
3293  void *context,
3294  int flags)
3295 {
3296  List *newrt = NIL;
3297  ListCell *rt;
3298 
3299  foreach(rt, rtable)
3300  {
3301  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3302  RangeTblEntry *newrte;
3303 
3304  FLATCOPY(newrte, rte, RangeTblEntry);
3305  switch (rte->rtekind)
3306  {
3307  case RTE_RELATION:
3308  MUTATE(newrte->tablesample, rte->tablesample,
3309  TableSampleClause *);
3310  /* we don't bother to copy eref, aliases, etc; OK? */
3311  break;
3312  case RTE_SUBQUERY:
3313  if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3314  {
3315  CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
3316  MUTATE(newrte->subquery, newrte->subquery, Query *);
3317  }
3318  else
3319  {
3320  /* else, copy RT subqueries as-is */
3321  newrte->subquery = copyObject(rte->subquery);
3322  }
3323  break;
3324  case RTE_JOIN:
3325  if (!(flags & QTW_IGNORE_JOINALIASES))
3326  MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
3327  else
3328  {
3329  /* else, copy join aliases as-is */
3330  newrte->joinaliasvars = copyObject(rte->joinaliasvars);
3331  }
3332  break;
3333  case RTE_FUNCTION:
3334  MUTATE(newrte->functions, rte->functions, List *);
3335  break;
3336  case RTE_TABLEFUNC:
3337  MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
3338  break;
3339  case RTE_VALUES:
3340  MUTATE(newrte->values_lists, rte->values_lists, List *);
3341  break;
3342  case RTE_CTE:
3343  case RTE_NAMEDTUPLESTORE:
3344  case RTE_RESULT:
3345  /* nothing to do */
3346  break;
3347  }
3348  MUTATE(newrte->securityQuals, rte->securityQuals, List *);
3349  newrt = lappend(newrt, newrte);
3350  }
3351  return newrt;
3352 }
3353 
3354 /*
3355  * query_or_expression_tree_walker --- hybrid form
3356  *
3357  * This routine will invoke query_tree_walker if called on a Query node,
3358  * else will invoke the walker directly. This is a useful way of starting
3359  * the recursion when the walker's normal change of state is not appropriate
3360  * for the outermost Query node.
3361  */
3362 bool
3364  bool (*walker) (),
3365  void *context,
3366  int flags)
3367 {
3368  if (node && IsA(node, Query))
3369  return query_tree_walker((Query *) node,
3370  walker,
3371  context,
3372  flags);
3373  else
3374  return walker(node, context);
3375 }
3376 
3377 /*
3378  * query_or_expression_tree_mutator --- hybrid form
3379  *
3380  * This routine will invoke query_tree_mutator if called on a Query node,
3381  * else will invoke the mutator directly. This is a useful way of starting
3382  * the recursion when the mutator's normal change of state is not appropriate
3383  * for the outermost Query node.
3384  */
3385 Node *
3387  Node *(*mutator) (),
3388  void *context,
3389  int flags)
3390 {
3391  if (node && IsA(node, Query))
3392  return (Node *) query_tree_mutator((Query *) node,
3393  mutator,
3394  context,
3395  flags);
3396  else
3397  return mutator(node, context);
3398 }
3399 
3400 
3401 /*
3402  * raw_expression_tree_walker --- walk raw parse trees
3403  *
3404  * This has exactly the same API as expression_tree_walker, but instead of
3405  * walking post-analysis parse trees, it knows how to walk the node types
3406  * found in raw grammar output. (There is not currently any need for a
3407  * combined walker, so we keep them separate in the name of efficiency.)
3408  * Unlike expression_tree_walker, there is no special rule about query
3409  * boundaries: we descend to everything that's possibly interesting.
3410  *
3411  * Currently, the node type coverage here extends only to DML statements
3412  * (SELECT/INSERT/UPDATE/DELETE) and nodes that can appear in them, because
3413  * this is used mainly during analysis of CTEs, and only DML statements can
3414  * appear in CTEs.
3415  */
3416 bool
3418  bool (*walker) (),
3419  void *context)
3420 {
3421  ListCell *temp;
3422 
3423  /*
3424  * The walker has already visited the current node, and so we need only
3425  * recurse into any sub-nodes it has.
3426  */
3427  if (node == NULL)
3428  return false;
3429 
3430  /* Guard against stack overflow due to overly complex expressions */
3432 
3433  switch (nodeTag(node))
3434  {
3435  case T_SetToDefault:
3436  case T_CurrentOfExpr:
3437  case T_SQLValueFunction:
3438  case T_Integer:
3439  case T_Float:
3440  case T_String:
3441  case T_BitString:
3442  case T_Null:
3443  case T_ParamRef:
3444  case T_A_Const:
3445  case T_A_Star:
3446  /* primitive node types with no subnodes */
3447  break;
3448  case T_Alias:
3449  /* we assume the colnames list isn't interesting */
3450  break;
3451  case T_RangeVar:
3452  return walker(((RangeVar *) node)->alias, context);
3453  case T_GroupingFunc:
3454  return walker(((GroupingFunc *) node)->args, context);
3455  case T_SubLink:
3456  {
3457  SubLink *sublink = (SubLink *) node;
3458 
3459  if (walker(sublink->testexpr, context))
3460  return true;
3461  /* we assume the operName is not interesting */
3462  if (walker(sublink->subselect, context))
3463  return true;
3464  }
3465  break;
3466  case T_CaseExpr:
3467  {
3468  CaseExpr *caseexpr = (CaseExpr *) node;
3469 
3470  if (walker(caseexpr->arg, context))
3471  return true;
3472  /* we assume walker doesn't care about CaseWhens, either */
3473  foreach(temp, caseexpr->args)
3474  {
3475  CaseWhen *when = lfirst_node(CaseWhen, temp);
3476 
3477  if (walker(when->expr, context))
3478  return true;
3479  if (walker(when->result, context))
3480  return true;
3481  }
3482  if (walker(caseexpr->defresult, context))
3483  return true;
3484  }
3485  break;
3486  case T_RowExpr:
3487  /* Assume colnames isn't interesting */
3488  return walker(((RowExpr *) node)->args, context);
3489  case T_CoalesceExpr:
3490  return walker(((CoalesceExpr *) node)->args, context);
3491  case T_MinMaxExpr:
3492  return walker(((MinMaxExpr *) node)->args, context);
3493  case T_XmlExpr:
3494  {
3495  XmlExpr *xexpr = (XmlExpr *) node;
3496 
3497  if (walker(xexpr->named_args, context))
3498  return true;
3499  /* we assume walker doesn't care about arg_names */
3500  if (walker(xexpr->args, context))
3501  return true;
3502  }
3503  break;
3504  case T_NullTest:
3505  return walker(((NullTest *) node)->arg, context);
3506  case T_BooleanTest:
3507  return walker(((BooleanTest *) node)->arg, context);
3508  case T_JoinExpr:
3509  {
3510  JoinExpr *join = (JoinExpr *) node;
3511 
3512  if (walker(join->larg, context))
3513  return true;
3514  if (walker(join->rarg, context))
3515  return true;
3516  if (walker(join->quals, context))
3517  return true;
3518  if (walker(join->alias, context))
3519  return true;
3520  /* using list is deemed uninteresting */
3521  }
3522  break;
3523  case T_IntoClause:
3524  {
3525  IntoClause *into = (IntoClause *) node;
3526 
3527  if (walker(into->rel, context))
3528  return true;
3529  /* colNames, options are deemed uninteresting */
3530  /* viewQuery should be null in raw parsetree, but check it */
3531  if (walker(into->viewQuery, context))
3532  return true;
3533  }
3534  break;
3535  case T_List:
3536  foreach(temp, (List *) node)
3537  {
3538  if (walker((Node *) lfirst(temp), context))
3539  return true;
3540  }
3541  break;
3542  case T_InsertStmt:
3543  {
3544  InsertStmt *stmt = (InsertStmt *) node;
3545 
3546  if (walker(stmt->relation, context))
3547  return true;
3548  if (walker(stmt->cols, context))
3549  return true;
3550  if (walker(stmt->selectStmt, context))
3551  return true;
3552  if (walker(stmt->onConflictClause, context))
3553  return true;
3554  if (walker(stmt->returningList, context))
3555  return true;
3556  if (walker(stmt->withClause, context))
3557  return true;
3558  }
3559  break;
3560  case T_DeleteStmt:
3561  {
3562  DeleteStmt *stmt = (DeleteStmt *) node;
3563 
3564  if (walker(stmt->relation, context))
3565  return true;
3566  if (walker(stmt->usingClause, context))
3567  return true;
3568  if (walker(stmt->whereClause, context))
3569  return true;
3570  if (walker(stmt->returningList, context))
3571  return true;
3572  if (walker(stmt->withClause, context))
3573  return true;
3574  }
3575  break;
3576  case T_UpdateStmt:
3577  {
3578  UpdateStmt *stmt = (UpdateStmt *) node;
3579 
3580  if (walker(stmt->relation, context))
3581  return true;
3582  if (walker(stmt->targetList, context))
3583  return true;
3584  if (walker(stmt->whereClause, context))
3585  return true;
3586  if (walker(stmt->fromClause, context))
3587  return true;
3588  if (walker(stmt->returningList, context))
3589  return true;
3590  if (walker(stmt->withClause, context))
3591  return true;
3592  }
3593  break;
3594  case T_SelectStmt:
3595  {
3596  SelectStmt *stmt = (SelectStmt *) node;
3597 
3598  if (walker(stmt->distinctClause, context))
3599  return true;
3600  if (walker(stmt->intoClause, context))
3601  return true;
3602  if (walker(stmt->targetList, context))
3603  return true;
3604  if (walker(stmt->fromClause, context))
3605  return true;
3606  if (walker(stmt->whereClause, context))
3607  return true;
3608  if (walker(stmt->groupClause, context))
3609  return true;
3610  if (walker(stmt->havingClause, context))
3611  return true;
3612  if (walker(stmt->windowClause, context))
3613  return true;
3614  if (walker(stmt->valuesLists, context))
3615  return true;
3616  if (walker(stmt->sortClause, context))
3617  return true;
3618  if (walker(stmt->limitOffset, context))
3619  return true;
3620  if (walker(stmt->limitCount, context))
3621  return true;
3622  if (walker(stmt->lockingClause, context))
3623  return true;
3624  if (walker(stmt->withClause, context))
3625  return true;
3626  if (walker(stmt->larg, context))
3627  return true;
3628  if (walker(stmt->rarg, context))
3629  return true;
3630  }
3631  break;
3632  case T_A_Expr:
3633  {
3634  A_Expr *expr = (A_Expr *) node;
3635 
3636  if (walker(expr->lexpr, context))
3637  return true;
3638  if (walker(expr->rexpr, context))
3639  return true;
3640  /* operator name is deemed uninteresting */
3641  }
3642  break;
3643  case T_BoolExpr:
3644  {
3645  BoolExpr *expr = (BoolExpr *) node;
3646 
3647  if (walker(expr->args, context))
3648  return true;
3649  }
3650  break;
3651  case T_ColumnRef:
3652  /* we assume the fields contain nothing interesting */
3653  break;
3654  case T_FuncCall:
3655  {
3656  FuncCall *fcall = (FuncCall *) node;
3657 
3658  if (walker(fcall->args, context))
3659  return true;
3660  if (walker(fcall->agg_order, context))
3661  return true;
3662  if (walker(fcall->agg_filter, context))
3663  return true;
3664  if (walker(fcall->over, context))
3665  return true;
3666  /* function name is deemed uninteresting */
3667  }
3668  break;
3669  case T_NamedArgExpr:
3670  return walker(((NamedArgExpr *) node)->arg, context);
3671  case T_A_Indices:
3672  {
3673  A_Indices *indices = (A_Indices *) node;
3674 
3675  if (walker(indices->lidx, context))
3676  return true;
3677  if (walker(indices->uidx, context))
3678  return true;
3679  }
3680  break;
3681  case T_A_Indirection:
3682  {
3683  A_Indirection *indir = (A_Indirection *) node;
3684 
3685  if (walker(indir->arg, context))
3686  return true;
3687  if (walker(indir->indirection, context))
3688  return true;
3689  }
3690  break;
3691  case T_A_ArrayExpr:
3692  return walker(((A_ArrayExpr *) node)->elements, context);
3693  case T_ResTarget:
3694  {
3695  ResTarget *rt = (ResTarget *) node;
3696 
3697  if (walker(rt->indirection, context))
3698  return true;
3699  if (walker(rt->val, context))
3700  return true;
3701  }
3702  break;
3703  case T_MultiAssignRef:
3704  return walker(((MultiAssignRef *) node)->source, context);
3705  case T_TypeCast:
3706  {
3707  TypeCast *tc = (TypeCast *) node;
3708 
3709  if (walker(tc->arg, context))
3710  return true;
3711  if (walker(tc->typeName, context))
3712  return true;
3713  }
3714  break;
3715  case T_CollateClause:
3716  return walker(((CollateClause *) node)->arg, context);
3717  case T_SortBy:
3718  return walker(((SortBy *) node)->node, context);
3719  case T_WindowDef:
3720  {
3721  WindowDef *wd = (WindowDef *) node;
3722 
3723  if (walker(wd->partitionClause, context))
3724  return true;
3725  if (walker(wd->orderClause, context))
3726  return true;
3727  if (walker(wd->startOffset, context))
3728  return true;
3729  if (walker(wd->endOffset, context))
3730  return true;
3731  }
3732  break;
3733  case T_RangeSubselect:
3734  {
3735  RangeSubselect *rs = (RangeSubselect *) node;
3736 
3737  if (walker(rs->subquery, context))
3738  return true;
3739  if (walker(rs->alias, context))
3740  return true;
3741  }
3742  break;
3743  case T_RangeFunction:
3744  {
3745  RangeFunction *rf = (RangeFunction *) node;
3746 
3747  if (walker(rf->functions, context))
3748  return true;
3749  if (walker(rf->alias, context))
3750  return true;
3751  if (walker(rf->coldeflist, context))
3752  return true;
3753  }
3754  break;
3755  case T_RangeTableSample:
3756  {
3757  RangeTableSample *rts = (RangeTableSample *) node;
3758 
3759  if (walker(rts->relation, context))
3760  return true;
3761  /* method name is deemed uninteresting */
3762  if (walker(rts->args, context))
3763  return true;
3764  if (walker(rts->repeatable, context))
3765  return true;
3766  }
3767  break;
3768  case T_RangeTableFunc:
3769  {
3770  RangeTableFunc *rtf = (RangeTableFunc *) node;
3771 
3772  if (walker(rtf->docexpr, context))
3773  return true;
3774  if (walker(rtf->rowexpr, context))
3775  return true;
3776  if (walker(rtf->namespaces, context))
3777  return true;
3778  if (walker(rtf->columns, context))
3779  return true;
3780  if (walker(rtf->alias, context))
3781  return true;
3782  }
3783  break;
3784  case T_RangeTableFuncCol:
3785  {
3786  RangeTableFuncCol *rtfc = (RangeTableFuncCol *) node;
3787 
3788  if (walker(rtfc->colexpr, context))
3789  return true;
3790  if (walker(rtfc->coldefexpr, context))
3791  return true;
3792  }
3793  break;
3794  case T_TypeName:
3795  {
3796  TypeName *tn = (TypeName *) node;
3797 
3798  if (walker(tn->typmods, context))
3799  return true;
3800  if (walker(tn->arrayBounds, context))
3801  return true;
3802  /* type name itself is deemed uninteresting */
3803  }
3804  break;
3805  case T_ColumnDef:
3806  {
3807  ColumnDef *coldef = (ColumnDef *) node;
3808 
3809  if (walker(coldef->typeName, context))
3810  return true;
3811  if (walker(coldef->raw_default, context))
3812  return true;
3813  if (walker(coldef->collClause, context))
3814  return true;
3815  /* for now, constraints are ignored */
3816  }
3817  break;
3818  case T_IndexElem:
3819  {
3820  IndexElem *indelem = (IndexElem *) node;
3821 
3822  if (walker(indelem->expr, context))
3823  return true;
3824  /* collation and opclass names are deemed uninteresting */
3825  }
3826  break;
3827  case T_GroupingSet:
3828  return walker(((GroupingSet *) node)->content, context);
3829  case T_LockingClause:
3830  return walker(((LockingClause *) node)->lockedRels, context);
3831  case T_XmlSerialize:
3832  {
3833  XmlSerialize *xs = (XmlSerialize *) node;
3834 
3835  if (walker(xs->expr, context))
3836  return true;
3837  if (walker(xs->typeName, context))
3838  return true;
3839  }
3840  break;
3841  case T_WithClause:
3842  return walker(((WithClause *) node)->ctes, context);
3843  case T_InferClause:
3844  {
3845  InferClause *stmt = (InferClause *) node;
3846 
3847  if (walker(stmt->indexElems, context))
3848  return true;
3849  if (walker(stmt->whereClause, context))
3850  return true;
3851  }
3852  break;
3853  case T_OnConflictClause:
3854  {
3855  OnConflictClause *stmt = (OnConflictClause *) node;
3856 
3857  if (walker(stmt->infer, context))
3858  return true;
3859  if (walker(stmt->targetList, context))
3860  return true;
3861  if (walker(stmt->whereClause, context))
3862  return true;
3863  }
3864  break;
3865  case T_CommonTableExpr:
3866  return walker(((CommonTableExpr *) node)->ctequery, context);
3867  default:
3868  elog(ERROR, "unrecognized node type: %d",
3869  (int) nodeTag(node));
3870  break;
3871  }
3872  return false;
3873 }
3874 
3875 /*
3876  * planstate_tree_walker --- walk plan state trees
3877  *
3878  * The walker has already visited the current node, and so we need only
3879  * recurse into any sub-nodes it has.
3880  */
3881 bool
3883  bool (*walker) (),
3884  void *context)
3885 {
3886  Plan *plan = planstate->plan;
3887  ListCell *lc;
3888 
3889  /* Guard against stack overflow due to overly complex plan trees */
3891 
3892  /* initPlan-s */
3893  if (planstate_walk_subplans(planstate->initPlan, walker, context))
3894  return true;
3895 
3896  /* lefttree */
3897  if (outerPlanState(planstate))
3898  {
3899  if (walker(outerPlanState(planstate), context))
3900  return true;
3901  }
3902 
3903  /* righttree */
3904  if (innerPlanState(planstate))
3905  {
3906  if (walker(innerPlanState(planstate), context))
3907  return true;
3908  }
3909 
3910  /* special child plans */
3911  switch (nodeTag(plan))
3912  {
3913  case T_ModifyTable:
3914  if (planstate_walk_members(((ModifyTableState *) planstate)->mt_plans,
3915  ((ModifyTableState *) planstate)->mt_nplans,
3916  walker, context))
3917  return true;
3918  break;
3919  case T_Append:
3920  if (planstate_walk_members(((AppendState *) planstate)->appendplans,
3921  ((AppendState *) planstate)->as_nplans,
3922  walker, context))
3923  return true;
3924  break;
3925  case T_MergeAppend:
3926  if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
3927  ((MergeAppendState *) planstate)->ms_nplans,
3928  walker, context))
3929  return true;
3930  break;
3931  case T_BitmapAnd:
3932  if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
3933  ((BitmapAndState *) planstate)->nplans,
3934  walker, context))
3935  return true;
3936  break;
3937  case T_BitmapOr:
3938  if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
3939  ((BitmapOrState *) planstate)->nplans,
3940  walker, context))
3941  return true;
3942  break;
3943  case T_SubqueryScan:
3944  if (walker(((SubqueryScanState *) planstate)->subplan, context))
3945  return true;
3946  break;
3947  case T_CustomScan:
3948  foreach(lc, ((CustomScanState *) planstate)->custom_ps)
3949  {
3950  if (walker((PlanState *) lfirst(lc), context))
3951  return true;
3952  }
3953  break;
3954  default:
3955  break;
3956  }
3957 
3958  /* subPlan-s */
3959  if (planstate_walk_subplans(planstate->subPlan, walker, context))
3960  return true;
3961 
3962  return false;
3963 }
3964 
3965 /*
3966  * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
3967  */
3968 static bool
3970  bool (*walker) (),
3971  void *context)
3972 {
3973  ListCell *lc;
3974 
3975  foreach(lc, plans)
3976  {
3978 
3979  if (walker(sps->planstate, context))
3980  return true;
3981  }
3982 
3983  return false;
3984 }
3985 
3986 /*
3987  * Walk the constituent plans of a ModifyTable, Append, MergeAppend,
3988  * BitmapAnd, or BitmapOr node.
3989  */
3990 static bool
3991 planstate_walk_members(PlanState **planstates, int nplans,
3992  bool (*walker) (), void *context)
3993 {
3994  int j;
3995 
3996  for (j = 0; j < nplans; j++)
3997  {
3998  if (walker(planstates[j], context))
3999  return true;
4000  }
4001 
4002  return false;
4003 }
Datum constvalue
Definition: primnodes.h:214
List * aggdistinct
Definition: primnodes.h:321
List * indirection
Definition: parsenodes.h:441
Node * limitOffset
Definition: parsenodes.h:160
List * partitionClause
Definition: parsenodes.h:489
Oid minmaxtype
Definition: primnodes.h:1102
bool multidims
Definition: primnodes.h:992
#define NIL
Definition: pg_list.h:65
struct SelectStmt * larg
Definition: parsenodes.h:1628
Oid firstColType
Definition: primnodes.h:713
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2273
List * fromClause
Definition: parsenodes.h:1560
List * args
Definition: primnodes.h:1106
List * args
Definition: primnodes.h:1022
Alias * alias
Definition: parsenodes.h:567
#define IsA(nodeptr, _type_)
Definition: nodes.h:577
Node * docexpr
Definition: primnodes.h:87
Expr * arg
Definition: primnodes.h:791
List * joinaliasvars
Definition: parsenodes.h:1050
Node * val
Definition: parsenodes.h:442
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2516
Node * subquery
Definition: parsenodes.h:542
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2735
PlaceHolderVar * ph_var
Definition: pathnodes.h:2287
List * sortClause
Definition: parsenodes.h:158
#define CHECKFLATCOPY(newnode, node, nodetype)
List * args
Definition: primnodes.h:359
bool range_table_entry_walker(RangeTblEntry *rte, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2393
IntoClause * intoClause
Definition: parsenodes.h:1595
List * refs
Definition: primnodes.h:361
#define QTW_EXAMINE_RTES_AFTER
Definition: nodeFuncs.h:27
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1191
List * fromClause
Definition: parsenodes.h:1597
List * args
Definition: primnodes.h:377
List * args
Definition: primnodes.h:477
#define DatumGetInt32(X)
Definition: postgres.h:472
RangeVar * relation
Definition: parsenodes.h:1543
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:970
Node * limitOffset
Definition: parsenodes.h:1618
#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:1124
List * withCheckOptions
Definition: parsenodes.h:171
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1587
int location
Definition: primnodes.h:1189
Node * agg_filter
Definition: parsenodes.h:353
Oid resulttype
Definition: primnodes.h:835
#define QTW_EXAMINE_SORTGROUP
Definition: nodeFuncs.h:30
#define Min(x, y)
Definition: c.h:920
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:669
List * columns
Definition: parsenodes.h:582
List * subPlan
Definition: execnodes.h:972
List * indexElems
Definition: parsenodes.h:1411
SubLinkType subLinkType
Definition: primnodes.h:704
Expr * arg
Definition: primnodes.h:814
static bool planstate_walk_members(PlanState **planstates, int nplans, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3991
List * list_copy(const List *oldlist)
Definition: list.c:1404
Definition: nodes.h:526
#define QTW_DONT_COPY_QUERY
Definition: nodeFuncs.h:29
CoercionForm coercionformat
Definition: primnodes.h:1263
Definition: nodes.h:49
int errcode(int sqlerrcode)
Definition: elog.c:610
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:512
List * args
Definition: primnodes.h:319
Oid array_typeid
Definition: primnodes.h:988
char * format_type_be(Oid type_oid)
Definition: format_type.c:327
Expr * arg
Definition: primnodes.h:762
bool funcretset
Definition: primnodes.h:471
static int fc(const char *x)
Definition: preproc-init.c:99
List * targetList
Definition: parsenodes.h:1558
List * fromlist
Definition: primnodes.h:1510
TypeName * typeName
Definition: parsenodes.h:762
int location
Definition: parsenodes.h:359
Oid casetype
Definition: primnodes.h:930
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
#define linitial_node(type, l)
Definition: pg_list.h:198
List * refupperindexpr
Definition: primnodes.h:422
#define OidIsValid(objectId)
Definition: c.h:644
WithClause * withClause
Definition: parsenodes.h:1532
List * translated_vars
Definition: pathnodes.h:2237
List * agg_order
Definition: parsenodes.h:352
List * values_lists
Definition: parsenodes.h:1073
Node * quals
Definition: primnodes.h:1511
#define lsecond(l)
Definition: pg_list.h:200
int location
Definition: primnodes.h:584
Node * whereClause
Definition: parsenodes.h:1559
signed int int32
Definition: c.h:355
OnConflictClause * onConflictClause
Definition: parsenodes.h:1530
List * windowClause
Definition: parsenodes.h:154
List * targetList
Definition: parsenodes.h:140
int location
Definition: parsenodes.h:300
List * arbiterElems
Definition: primnodes.h:1529
Node * larg
Definition: primnodes.h:1490
#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:1547
Oid consttype
Definition: primnodes.h:210
List * aggargtypes
Definition: primnodes.h:317
Node * expr
Definition: parsenodes.h:700
CoercionForm funcformat
Definition: primnodes.h:474
List * distinctClause
Definition: parsenodes.h:1593
List * returningList
Definition: parsenodes.h:1546
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:469
#define ERROR
Definition: elog.h:43
Node * viewQuery
Definition: primnodes.h:118
struct PlanState * planstate
Definition: execnodes.h:853
struct WindowDef * over
Definition: parsenodes.h:358
bool raw_expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3417
TableFunc * tablefunc
Definition: parsenodes.h:1068
List * partitionClause
Definition: parsenodes.h:1352
List * coldeflist
Definition: parsenodes.h:568
Node * selectStmt
Definition: parsenodes.h:1529
List * args
Definition: primnodes.h:1086
Expr * arg
Definition: primnodes.h:1219
Node * endOffset
Definition: parsenodes.h:493
List * cols
Definition: parsenodes.h:1528
List * coldefexprs
Definition: primnodes.h:94
#define lfirst_node(type, lc)
Definition: pg_list.h:193
#define outerPlanState(node)
Definition: execnodes.h:1039
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:1243
List * sortClause
Definition: parsenodes.h:1617
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:1538
int location
Definition: primnodes.h:523
void check_stack_depth(void)
Definition: postgres.c:3312
List * targetList
Definition: parsenodes.h:1596
static ListCell * list_second_cell(const List *l)
Definition: pg_list.h:139
Node * rexpr
Definition: parsenodes.h:278
List * aggorder
Definition: primnodes.h:320
List * namespaces
Definition: parsenodes.h:581
Expr * arg
Definition: primnodes.h:1242
List * functions
Definition: parsenodes.h:566
int location
Definition: parsenodes.h:279
List * aggdirectargs
Definition: primnodes.h:318
Oid winfnoid
Definition: primnodes.h:373
Expr * arg
Definition: primnodes.h:834
List * elements
Definition: primnodes.h:991
List * valuesLists
Definition: parsenodes.h:1611
Expr * elemexpr
Definition: primnodes.h:859
Definition: type.h:82
List * returningList
Definition: parsenodes.h:146
Node * rowexpr
Definition: parsenodes.h:580
List * returningList
Definition: parsenodes.h:1531
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2702
Node * lexpr
Definition: parsenodes.h:277
List * lockingClause
Definition: parsenodes.h:1620
static bool planstate_walk_subplans(List *plans, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3969
List * indexquals
Definition: pathnodes.h:1244
Definition: nodes.h:297
List * newvals
Definition: primnodes.h:792
List * cols
Definition: primnodes.h:362
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:3363
Definition: nodes.h:151
Oid get_promoted_array_type(Oid typid)
Definition: lsyscache.c:2639
Node * startOffset
Definition: parsenodes.h:1355
List * args
Definition: primnodes.h:933
Definition: nodes.h:310
CoercionForm coerceformat
Definition: primnodes.h:863
void exprSetInputCollation(Node *expr, Oid inputcollation)
Definition: nodeFuncs.c:1127
List * orderClause
Definition: parsenodes.h:490
Node * quals
Definition: primnodes.h:1493
CoercionForm convertformat
Definition: primnodes.h:886
RangeVar * relation
Definition: parsenodes.h:1527
#define FLATCOPY(newnode, node, nodetype)
Oid opfuncid
Definition: primnodes.h:517
List * typmods
Definition: parsenodes.h:213
TypeName * typeName
Definition: parsenodes.h:299
Node * testexpr
Definition: primnodes.h:706
InferClause * infer
Definition: parsenodes.h:1427
Node * whereClause
Definition: parsenodes.h:1545
List * usingClause
Definition: parsenodes.h:1544
List * windowClause
Definition: parsenodes.h:1601
Alias * alias
Definition: parsenodes.h:583
Plan * plan
Definition: execnodes.h:945
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1153
Oid aggfnoid
Definition: primnodes.h:312
List * colexprs
Definition: primnodes.h:93
List * named_args
Definition: primnodes.h:1183
int32 firstColTypmod
Definition: primnodes.h:714
#define ereport(elevel,...)
Definition: elog.h:144
List * args
Definition: primnodes.h:1185
int location
Definition: primnodes.h:839
Node * rarg
Definition: primnodes.h:1491
Alias * alias
Definition: primnodes.h:1494
Expr * arg
Definition: primnodes.h:498
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:924
#define Assert(condition)
Definition: c.h:738
#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:3386
List * functions
Definition: parsenodes.h:1062
List * indirection
Definition: parsenodes.h:406
List * ns_uris
Definition: primnodes.h:85
Expr * aggfilter
Definition: primnodes.h:378
Expr * expr
Definition: primnodes.h:1407
Node * docexpr
Definition: parsenodes.h:579
Alias * alias
Definition: parsenodes.h:543
RangeVar * relation
Definition: parsenodes.h:1557
struct SelectStmt * rarg
Definition: parsenodes.h:1629
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:1222
Node * endOffset
Definition: parsenodes.h:1356
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
List * args
Definition: parsenodes.h:351
List * returningList
Definition: parsenodes.h:1561
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:900
Expr * aggfilter
Definition: primnodes.h:322
TypeName * typeName
Definition: parsenodes.h:647
Node * whereClause
Definition: parsenodes.h:1412
CollateClause * collClause
Definition: parsenodes.h:659
List * args
Definition: primnodes.h:583
#define nodeTag(nodeptr)
Definition: nodes.h:531
List * groupClause
Definition: parsenodes.h:1599
Oid element_typeid
Definition: primnodes.h:990
CoercionForm coerceformat
Definition: primnodes.h:838
RTEKind rtekind
Definition: parsenodes.h:975
Definition: nodes.h:292
Node * arbiterWhere
Definition: primnodes.h:1531
List * orderClause
Definition: parsenodes.h:1353
#define QTW_IGNORE_RT_SUBQUERIES
Definition: nodeFuncs.h:20
Expr * refassgnexpr
Definition: primnodes.h:430
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:1010
List * groupClause
Definition: parsenodes.h:148
int errmsg(const char *fmt,...)
Definition: elog.c:824
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:1600
#define QTW_IGNORE_RANGE_TABLE
Definition: nodeFuncs.h:24
List * fieldnums
Definition: primnodes.h:793
List * reflowerindexpr
Definition: primnodes.h:424
#define elog(elevel,...)
Definition: elog.h:214
Oid coalescetype
Definition: primnodes.h:1084
List * onConflictSet
Definition: primnodes.h:1535
Node * expr
Definition: parsenodes.h:761
void * arg
Node * uidx
Definition: parsenodes.h:384
Oid refcontainertype
Definition: primnodes.h:418
Oid firstColCollation
Definition: primnodes.h:715
Expr * refexpr
Definition: primnodes.h:427
Expr * arg
Definition: primnodes.h:932
int location
Definition: primnodes.h:478
Oid opno
Definition: primnodes.h:516
WithClause * withClause
Definition: parsenodes.h:1621
int32 resulttypmod
Definition: primnodes.h:861
Expr * result
Definition: primnodes.h:945
List * range_table_mutator(List *rtable, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3291
#define copyObject(obj)
Definition: nodes.h:642
List * args
Definition: primnodes.h:522
#define innerPlanState(node)
Definition: execnodes.h:1038
Node * havingQual
Definition: parsenodes.h:152
CoercionForm relabelformat
Definition: primnodes.h:818
Expr * defresult
Definition: primnodes.h:934
Expr * expr
Definition: primnodes.h:944
Node * onConflictWhere
Definition: primnodes.h:1536
Definition: pg_list.h:50
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3882
struct TableSampleClause * tablesample
Definition: parsenodes.h:1005
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1629
int location
Definition: primnodes.h:819
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:587
#define MUTATE(newfield, oldfield, fieldtype)
bool constisnull
Definition: primnodes.h:215
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3202
WithClause * withClause
Definition: parsenodes.h:1562
Node * limitCount
Definition: parsenodes.h:1619
Node * whereClause
Definition: parsenodes.h:1598
#define lfirst_oid(lc)
Definition: pg_list.h:192
bool opretset
Definition: primnodes.h:519
#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:730