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