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