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