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