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