1 /*-------------------------------------------------------------------------
4 * Various general-purpose manipulations of Node trees
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/nodes/nodeFuncs.c
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_collation.h"
18 #include "catalog/pg_type.h"
19 #include "miscadmin.h"
20 #include "nodes/nodeFuncs.h"
21 #include "nodes/relation.h"
22 #include "utils/builtins.h"
23 #include "utils/lsyscache.h"
26 static bool expression_returns_set_walker(Node *node, void *context);
27 static int leftmostLoc(int loc1, int loc2);
32 * returns the Oid of the type of the expression's result.
35 exprType(const Node *expr)
42 switch (nodeTag(expr))
45 type = ((const Var *) expr)->vartype;
48 type = ((const Const *) expr)->consttype;
51 type = ((const Param *) expr)->paramtype;
54 type = ((const Aggref *) expr)->aggtype;
57 type = ((const WindowFunc *) expr)->wintype;
61 const ArrayRef *arrayref = (const ArrayRef *) expr;
63 /* slice and/or store operations yield the array type */
64 if (arrayref->reflowerindexpr || arrayref->refassgnexpr)
65 type = arrayref->refarraytype;
67 type = arrayref->refelemtype;
71 type = ((const FuncExpr *) expr)->funcresulttype;
74 type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
77 type = ((const OpExpr *) expr)->opresulttype;
80 type = ((const DistinctExpr *) expr)->opresulttype;
83 type = ((const NullIfExpr *) expr)->opresulttype;
85 case T_ScalarArrayOpExpr:
93 const SubLink *sublink = (const SubLink *) expr;
95 if (sublink->subLinkType == EXPR_SUBLINK ||
96 sublink->subLinkType == ARRAY_SUBLINK)
98 /* get the type of the subselect's first target column */
99 Query *qtree = (Query *) sublink->subselect;
102 if (!qtree || !IsA(qtree, Query))
103 elog(ERROR, "cannot get type for untransformed sublink");
104 tent = (TargetEntry *) linitial(qtree->targetList);
105 Assert(IsA(tent, TargetEntry));
106 Assert(!tent->resjunk);
107 type = exprType((Node *) tent->expr);
108 if (sublink->subLinkType == ARRAY_SUBLINK)
110 type = get_array_type(type);
111 if (!OidIsValid(type))
113 (errcode(ERRCODE_UNDEFINED_OBJECT),
114 errmsg("could not find array type for data type %s",
115 format_type_be(exprType((Node *) tent->expr)))));
120 /* for all other sublink types, result is boolean */
127 const SubPlan *subplan = (const SubPlan *) expr;
129 if (subplan->subLinkType == EXPR_SUBLINK ||
130 subplan->subLinkType == ARRAY_SUBLINK)
132 /* get the type of the subselect's first target column */
133 type = subplan->firstColType;
134 if (subplan->subLinkType == ARRAY_SUBLINK)
136 type = get_array_type(type);
137 if (!OidIsValid(type))
139 (errcode(ERRCODE_UNDEFINED_OBJECT),
140 errmsg("could not find array type for data type %s",
141 format_type_be(subplan->firstColType))));
146 /* for all other subplan types, result is boolean */
151 case T_AlternativeSubPlan:
153 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
155 /* subplans should all return the same thing */
156 type = exprType((Node *) linitial(asplan->subplans));
160 type = ((const FieldSelect *) expr)->resulttype;
163 type = ((const FieldStore *) expr)->resulttype;
166 type = ((const RelabelType *) expr)->resulttype;
169 type = ((const CoerceViaIO *) expr)->resulttype;
171 case T_ArrayCoerceExpr:
172 type = ((const ArrayCoerceExpr *) expr)->resulttype;
174 case T_ConvertRowtypeExpr:
175 type = ((const ConvertRowtypeExpr *) expr)->resulttype;
178 type = exprType((Node *) ((const CollateExpr *) expr)->arg);
181 type = ((const CaseExpr *) expr)->casetype;
184 type = ((const CaseTestExpr *) expr)->typeId;
187 type = ((const ArrayExpr *) expr)->array_typeid;
190 type = ((const RowExpr *) expr)->row_typeid;
192 case T_RowCompareExpr:
196 type = ((const CoalesceExpr *) expr)->coalescetype;
199 type = ((const MinMaxExpr *) expr)->minmaxtype;
202 if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
204 else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
215 case T_CoerceToDomain:
216 type = ((const CoerceToDomain *) expr)->resulttype;
218 case T_CoerceToDomainValue:
219 type = ((const CoerceToDomainValue *) expr)->typeId;
222 type = ((const SetToDefault *) expr)->typeId;
224 case T_CurrentOfExpr:
227 case T_PlaceHolderVar:
228 type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
231 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
232 type = InvalidOid; /* keep compiler quiet */
240 * returns the type-specific modifier of the expression's result type,
241 * if it can be determined. In many cases, it can't and we return -1.
244 exprTypmod(const Node *expr)
249 switch (nodeTag(expr))
252 return ((const Var *) expr)->vartypmod;
254 return ((const Const *) expr)->consttypmod;
256 return ((const Param *) expr)->paramtypmod;
258 /* typmod is the same for array or element */
259 return ((const ArrayRef *) expr)->reftypmod;
264 /* Be smart about length-coercion functions... */
265 if (exprIsLengthCoercion(expr, &coercedTypmod))
266 return coercedTypmod;
270 return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
274 * Result is either first argument or NULL, so we can report
275 * first argument's typmod if known.
277 const NullIfExpr *nexpr = (const NullIfExpr *) expr;
279 return exprTypmod((Node *) linitial(nexpr->args));
284 const SubLink *sublink = (const SubLink *) expr;
286 if (sublink->subLinkType == EXPR_SUBLINK ||
287 sublink->subLinkType == ARRAY_SUBLINK)
289 /* get the typmod of the subselect's first target column */
290 Query *qtree = (Query *) sublink->subselect;
293 if (!qtree || !IsA(qtree, Query))
294 elog(ERROR, "cannot get type for untransformed sublink");
295 tent = (TargetEntry *) linitial(qtree->targetList);
296 Assert(IsA(tent, TargetEntry));
297 Assert(!tent->resjunk);
298 return exprTypmod((Node *) tent->expr);
299 /* note we don't need to care if it's an array */
305 const SubPlan *subplan = (const SubPlan *) expr;
307 if (subplan->subLinkType == EXPR_SUBLINK ||
308 subplan->subLinkType == ARRAY_SUBLINK)
310 /* get the typmod of the subselect's first target column */
311 /* note we don't need to care if it's an array */
312 return subplan->firstColTypmod;
316 /* for all other subplan types, result is boolean */
321 case T_AlternativeSubPlan:
323 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
325 /* subplans should all return the same thing */
326 return exprTypmod((Node *) linitial(asplan->subplans));
330 return ((const FieldSelect *) expr)->resulttypmod;
332 return ((const RelabelType *) expr)->resulttypmod;
333 case T_ArrayCoerceExpr:
334 return ((const ArrayCoerceExpr *) expr)->resulttypmod;
336 return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
340 * If all the alternatives agree on type/typmod, return that
341 * typmod, else use -1
343 const CaseExpr *cexpr = (const CaseExpr *) expr;
344 Oid casetype = cexpr->casetype;
348 if (!cexpr->defresult)
350 if (exprType((Node *) cexpr->defresult) != casetype)
352 typmod = exprTypmod((Node *) cexpr->defresult);
354 return -1; /* no point in trying harder */
355 foreach(arg, cexpr->args)
357 CaseWhen *w = (CaseWhen *) lfirst(arg);
359 Assert(IsA(w, CaseWhen));
360 if (exprType((Node *) w->result) != casetype)
362 if (exprTypmod((Node *) w->result) != typmod)
369 return ((const CaseTestExpr *) expr)->typeMod;
373 * If all the elements agree on type/typmod, return that
374 * typmod, else use -1
376 const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
381 if (arrayexpr->elements == NIL)
383 typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
385 return -1; /* no point in trying harder */
386 if (arrayexpr->multidims)
387 commontype = arrayexpr->array_typeid;
389 commontype = arrayexpr->element_typeid;
390 foreach(elem, arrayexpr->elements)
392 Node *e = (Node *) lfirst(elem);
394 if (exprType(e) != commontype)
396 if (exprTypmod(e) != typmod)
405 * If all the alternatives agree on type/typmod, return that
406 * typmod, else use -1
408 const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
409 Oid coalescetype = cexpr->coalescetype;
413 if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
415 typmod = exprTypmod((Node *) linitial(cexpr->args));
417 return -1; /* no point in trying harder */
418 for_each_cell(arg, lnext(list_head(cexpr->args)))
420 Node *e = (Node *) lfirst(arg);
422 if (exprType(e) != coalescetype)
424 if (exprTypmod(e) != typmod)
433 * If all the alternatives agree on type/typmod, return that
434 * typmod, else use -1
436 const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
437 Oid minmaxtype = mexpr->minmaxtype;
441 if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
443 typmod = exprTypmod((Node *) linitial(mexpr->args));
445 return -1; /* no point in trying harder */
446 for_each_cell(arg, lnext(list_head(mexpr->args)))
448 Node *e = (Node *) lfirst(arg);
450 if (exprType(e) != minmaxtype)
452 if (exprTypmod(e) != typmod)
458 case T_CoerceToDomain:
459 return ((const CoerceToDomain *) expr)->resulttypmod;
460 case T_CoerceToDomainValue:
461 return ((const CoerceToDomainValue *) expr)->typeMod;
463 return ((const SetToDefault *) expr)->typeMod;
464 case T_PlaceHolderVar:
465 return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
473 * exprIsLengthCoercion
474 * Detect whether an expression tree is an application of a datatype's
475 * typmod-coercion function. Optionally extract the result's typmod.
477 * If coercedTypmod is not NULL, the typmod is stored there if the expression
478 * is a length-coercion function, else -1 is stored there.
480 * Note that a combined type-and-length coercion will be treated as a
481 * length coercion by this routine.
484 exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
486 if (coercedTypmod != NULL)
487 *coercedTypmod = -1; /* default result on failure */
490 * Scalar-type length coercions are FuncExprs, array-type length coercions
491 * are ArrayCoerceExprs
493 if (expr && IsA(expr, FuncExpr))
495 const FuncExpr *func = (const FuncExpr *) expr;
500 * If it didn't come from a coercion context, reject.
502 if (func->funcformat != COERCE_EXPLICIT_CAST &&
503 func->funcformat != COERCE_IMPLICIT_CAST)
507 * If it's not a two-argument or three-argument function with the
508 * second argument being an int4 constant, it can't have been created
509 * from a length coercion (it must be a type coercion, instead).
511 nargs = list_length(func->args);
512 if (nargs < 2 || nargs > 3)
515 second_arg = (Const *) lsecond(func->args);
516 if (!IsA(second_arg, Const) ||
517 second_arg->consttype != INT4OID ||
518 second_arg->constisnull)
522 * OK, it is indeed a length-coercion function.
524 if (coercedTypmod != NULL)
525 *coercedTypmod = DatumGetInt32(second_arg->constvalue);
530 if (expr && IsA(expr, ArrayCoerceExpr))
532 const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
534 /* It's not a length coercion unless there's a nondefault typmod */
535 if (acoerce->resulttypmod < 0)
539 * OK, it is indeed a length-coercion expression.
541 if (coercedTypmod != NULL)
542 *coercedTypmod = acoerce->resulttypmod;
551 * expression_returns_set
552 * Test whether an expression returns a set result.
554 * Because we use expression_tree_walker(), this can also be applied to
555 * whole targetlists; it'll produce TRUE if any one of the tlist items
559 expression_returns_set(Node *clause)
561 return expression_returns_set_walker(clause, NULL);
565 expression_returns_set_walker(Node *node, void *context)
569 if (IsA(node, FuncExpr))
571 FuncExpr *expr = (FuncExpr *) node;
573 if (expr->funcretset)
575 /* else fall through to check args */
577 if (IsA(node, OpExpr))
579 OpExpr *expr = (OpExpr *) node;
583 /* else fall through to check args */
586 /* Avoid recursion for some cases that can't return a set */
587 if (IsA(node, Aggref))
589 if (IsA(node, WindowFunc))
591 if (IsA(node, DistinctExpr))
593 if (IsA(node, NullIfExpr))
595 if (IsA(node, ScalarArrayOpExpr))
597 if (IsA(node, BoolExpr))
599 if (IsA(node, SubLink))
601 if (IsA(node, SubPlan))
603 if (IsA(node, AlternativeSubPlan))
605 if (IsA(node, ArrayExpr))
607 if (IsA(node, RowExpr))
609 if (IsA(node, RowCompareExpr))
611 if (IsA(node, CoalesceExpr))
613 if (IsA(node, MinMaxExpr))
615 if (IsA(node, XmlExpr))
618 return expression_tree_walker(node, expression_returns_set_walker,
625 * returns the Oid of the collation of the expression's result.
627 * Note: expression nodes that can invoke functions generally have an
628 * "inputcollid" field, which is what the function should use as collation.
629 * That is the resolved common collation of the node's inputs. It is often
630 * but not always the same as the result collation; in particular, if the
631 * function produces a non-collatable result type from collatable inputs
632 * or vice versa, the two are different.
635 exprCollation(const Node *expr)
642 switch (nodeTag(expr))
645 coll = ((const Var *) expr)->varcollid;
648 coll = ((const Const *) expr)->constcollid;
651 coll = ((const Param *) expr)->paramcollid;
654 coll = ((const Aggref *) expr)->aggcollid;
657 coll = ((const WindowFunc *) expr)->wincollid;
660 coll = ((const ArrayRef *) expr)->refcollid;
663 coll = ((const FuncExpr *) expr)->funccollid;
666 coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
669 coll = ((const OpExpr *) expr)->opcollid;
672 coll = ((const DistinctExpr *) expr)->opcollid;
675 coll = ((const NullIfExpr *) expr)->opcollid;
677 case T_ScalarArrayOpExpr:
678 coll = InvalidOid; /* result is always boolean */
681 coll = InvalidOid; /* result is always boolean */
685 const SubLink *sublink = (const SubLink *) expr;
687 if (sublink->subLinkType == EXPR_SUBLINK ||
688 sublink->subLinkType == ARRAY_SUBLINK)
690 /* get the collation of subselect's first target column */
691 Query *qtree = (Query *) sublink->subselect;
694 if (!qtree || !IsA(qtree, Query))
695 elog(ERROR, "cannot get collation for untransformed sublink");
696 tent = (TargetEntry *) linitial(qtree->targetList);
697 Assert(IsA(tent, TargetEntry));
698 Assert(!tent->resjunk);
699 coll = exprCollation((Node *) tent->expr);
700 /* collation doesn't change if it's converted to array */
704 /* for all other sublink types, result is boolean */
711 const SubPlan *subplan = (const SubPlan *) expr;
713 if (subplan->subLinkType == EXPR_SUBLINK ||
714 subplan->subLinkType == ARRAY_SUBLINK)
716 /* get the collation of subselect's first target column */
717 coll = subplan->firstColCollation;
718 /* collation doesn't change if it's converted to array */
722 /* for all other subplan types, result is boolean */
727 case T_AlternativeSubPlan:
729 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
731 /* subplans should all return the same thing */
732 coll = exprCollation((Node *) linitial(asplan->subplans));
736 coll = ((const FieldSelect *) expr)->resultcollid;
739 coll = InvalidOid; /* result is always composite */
742 coll = ((const RelabelType *) expr)->resultcollid;
745 coll = ((const CoerceViaIO *) expr)->resultcollid;
747 case T_ArrayCoerceExpr:
748 coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
750 case T_ConvertRowtypeExpr:
751 coll = InvalidOid; /* result is always composite */
754 coll = ((const CollateExpr *) expr)->collOid;
757 coll = ((const CaseExpr *) expr)->casecollid;
760 coll = ((const CaseTestExpr *) expr)->collation;
763 coll = ((const ArrayExpr *) expr)->array_collid;
766 coll = InvalidOid; /* result is always composite */
768 case T_RowCompareExpr:
769 coll = InvalidOid; /* result is always boolean */
772 coll = ((const CoalesceExpr *) expr)->coalescecollid;
775 coll = ((const MinMaxExpr *) expr)->minmaxcollid;
780 * XMLSERIALIZE returns text from non-collatable inputs, so its
781 * collation is always default. The other cases return boolean or
782 * XML, which are non-collatable.
784 if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
785 coll = DEFAULT_COLLATION_OID;
790 coll = InvalidOid; /* result is always boolean */
793 coll = InvalidOid; /* result is always boolean */
795 case T_CoerceToDomain:
796 coll = ((const CoerceToDomain *) expr)->resultcollid;
798 case T_CoerceToDomainValue:
799 coll = ((const CoerceToDomainValue *) expr)->collation;
802 coll = ((const SetToDefault *) expr)->collation;
804 case T_CurrentOfExpr:
805 coll = InvalidOid; /* result is always boolean */
807 case T_PlaceHolderVar:
808 coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
811 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
812 coll = InvalidOid; /* keep compiler quiet */
819 * exprInputCollation -
820 * returns the Oid of the collation a function should use, if available.
822 * Result is InvalidOid if the node type doesn't store this information.
825 exprInputCollation(const Node *expr)
832 switch (nodeTag(expr))
835 coll = ((const Aggref *) expr)->inputcollid;
838 coll = ((const WindowFunc *) expr)->inputcollid;
841 coll = ((const FuncExpr *) expr)->inputcollid;
844 coll = ((const OpExpr *) expr)->inputcollid;
847 coll = ((const DistinctExpr *) expr)->inputcollid;
850 coll = ((const NullIfExpr *) expr)->inputcollid;
852 case T_ScalarArrayOpExpr:
853 coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
856 coll = ((const MinMaxExpr *) expr)->inputcollid;
867 * Assign collation information to an expression tree node.
869 * Note: since this is only used during parse analysis, we don't need to
870 * worry about subplans or PlaceHolderVars.
873 exprSetCollation(Node *expr, Oid collation)
875 switch (nodeTag(expr))
878 ((Var *) expr)->varcollid = collation;
881 ((Const *) expr)->constcollid = collation;
884 ((Param *) expr)->paramcollid = collation;
887 ((Aggref *) expr)->aggcollid = collation;
890 ((WindowFunc *) expr)->wincollid = collation;
893 ((ArrayRef *) expr)->refcollid = collation;
896 ((FuncExpr *) expr)->funccollid = collation;
899 Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
902 ((OpExpr *) expr)->opcollid = collation;
905 ((DistinctExpr *) expr)->opcollid = collation;
908 ((NullIfExpr *) expr)->opcollid = collation;
910 case T_ScalarArrayOpExpr:
911 Assert(!OidIsValid(collation)); /* result is always boolean */
914 Assert(!OidIsValid(collation)); /* result is always boolean */
917 #ifdef USE_ASSERT_CHECKING
919 SubLink *sublink = (SubLink *) expr;
921 if (sublink->subLinkType == EXPR_SUBLINK ||
922 sublink->subLinkType == ARRAY_SUBLINK)
924 /* get the collation of subselect's first target column */
925 Query *qtree = (Query *) sublink->subselect;
928 if (!qtree || !IsA(qtree, Query))
929 elog(ERROR, "cannot set collation for untransformed sublink");
930 tent = (TargetEntry *) linitial(qtree->targetList);
931 Assert(IsA(tent, TargetEntry));
932 Assert(!tent->resjunk);
933 Assert(collation == exprCollation((Node *) tent->expr));
937 /* for all other sublink types, result is boolean */
938 Assert(!OidIsValid(collation));
941 #endif /* USE_ASSERT_CHECKING */
944 ((FieldSelect *) expr)->resultcollid = collation;
947 Assert(!OidIsValid(collation)); /* result is always composite */
950 ((RelabelType *) expr)->resultcollid = collation;
953 ((CoerceViaIO *) expr)->resultcollid = collation;
955 case T_ArrayCoerceExpr:
956 ((ArrayCoerceExpr *) expr)->resultcollid = collation;
958 case T_ConvertRowtypeExpr:
959 Assert(!OidIsValid(collation)); /* result is always composite */
962 ((CaseExpr *) expr)->casecollid = collation;
965 ((ArrayExpr *) expr)->array_collid = collation;
968 Assert(!OidIsValid(collation)); /* result is always composite */
970 case T_RowCompareExpr:
971 Assert(!OidIsValid(collation)); /* result is always boolean */
974 ((CoalesceExpr *) expr)->coalescecollid = collation;
977 ((MinMaxExpr *) expr)->minmaxcollid = collation;
980 Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
981 (collation == DEFAULT_COLLATION_OID) :
982 (collation == InvalidOid));
985 Assert(!OidIsValid(collation)); /* result is always boolean */
988 Assert(!OidIsValid(collation)); /* result is always boolean */
990 case T_CoerceToDomain:
991 ((CoerceToDomain *) expr)->resultcollid = collation;
993 case T_CoerceToDomainValue:
994 ((CoerceToDomainValue *) expr)->collation = collation;
997 ((SetToDefault *) expr)->collation = collation;
999 case T_CurrentOfExpr:
1000 Assert(!OidIsValid(collation)); /* result is always boolean */
1003 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
1009 * exprSetInputCollation -
1010 * Assign input-collation information to an expression tree node.
1012 * This is a no-op for node types that don't store their input collation.
1013 * Note we omit RowCompareExpr, which needs special treatment since it
1014 * contains multiple input collation OIDs.
1017 exprSetInputCollation(Node *expr, Oid inputcollation)
1019 switch (nodeTag(expr))
1022 ((Aggref *) expr)->inputcollid = inputcollation;
1025 ((WindowFunc *) expr)->inputcollid = inputcollation;
1028 ((FuncExpr *) expr)->inputcollid = inputcollation;
1031 ((OpExpr *) expr)->inputcollid = inputcollation;
1033 case T_DistinctExpr:
1034 ((DistinctExpr *) expr)->inputcollid = inputcollation;
1037 ((NullIfExpr *) expr)->inputcollid = inputcollation;
1039 case T_ScalarArrayOpExpr:
1040 ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
1043 ((MinMaxExpr *) expr)->inputcollid = inputcollation;
1053 * returns the parse location of an expression tree, for error reports
1055 * -1 is returned if the location can't be determined.
1057 * For expressions larger than a single token, the intent here is to
1058 * return the location of the expression's leftmost token, not necessarily
1059 * the topmost Node's location field. For example, an OpExpr's location
1060 * field will point at the operator name, but if it is not a prefix operator
1061 * then we should return the location of the left-hand operand instead.
1062 * The reason is that we want to reference the entire expression not just
1063 * that operator, and pointing to its start seems to be the most natural way.
1065 * The location is not perfect --- for example, since the grammar doesn't
1066 * explicitly represent parentheses in the parsetree, given something that
1067 * had been written "(a + b) * c" we are going to point at "a" not "(".
1068 * But it should be plenty good enough for error reporting purposes.
1070 * You might think that this code is overly general, for instance why check
1071 * the operands of a FuncExpr node, when the function name can be expected
1072 * to be to the left of them? There are a couple of reasons. The grammar
1073 * sometimes builds expressions that aren't quite what the user wrote;
1074 * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
1075 * pointer is to the right of its leftmost argument. Also, nodes that were
1076 * inserted implicitly by parse analysis (such as FuncExprs for implicit
1077 * coercions) will have location -1, and so we can have odd combinations of
1078 * known and unknown locations in a tree.
1081 exprLocation(const Node *expr)
1087 switch (nodeTag(expr))
1090 loc = ((const RangeVar *) expr)->location;
1093 loc = ((const Var *) expr)->location;
1096 loc = ((const Const *) expr)->location;
1099 loc = ((const Param *) expr)->location;
1102 /* function name should always be the first thing */
1103 loc = ((const Aggref *) expr)->location;
1106 /* function name should always be the first thing */
1107 loc = ((const WindowFunc *) expr)->location;
1110 /* just use array argument's location */
1111 loc = exprLocation((Node *) ((const ArrayRef *) expr)->refexpr);
1115 const FuncExpr *fexpr = (const FuncExpr *) expr;
1117 /* consider both function name and leftmost arg */
1118 loc = leftmostLoc(fexpr->location,
1119 exprLocation((Node *) fexpr->args));
1122 case T_NamedArgExpr:
1124 const NamedArgExpr *na = (const NamedArgExpr *) expr;
1126 /* consider both argument name and value */
1127 loc = leftmostLoc(na->location,
1128 exprLocation((Node *) na->arg));
1132 case T_DistinctExpr: /* struct-equivalent to OpExpr */
1133 case T_NullIfExpr: /* struct-equivalent to OpExpr */
1135 const OpExpr *opexpr = (const OpExpr *) expr;
1137 /* consider both operator name and leftmost arg */
1138 loc = leftmostLoc(opexpr->location,
1139 exprLocation((Node *) opexpr->args));
1142 case T_ScalarArrayOpExpr:
1144 const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
1146 /* consider both operator name and leftmost arg */
1147 loc = leftmostLoc(saopexpr->location,
1148 exprLocation((Node *) saopexpr->args));
1153 const BoolExpr *bexpr = (const BoolExpr *) expr;
1156 * Same as above, to handle either NOT or AND/OR. We can't
1157 * special-case NOT because of the way that it's used for
1158 * things like IS NOT BETWEEN.
1160 loc = leftmostLoc(bexpr->location,
1161 exprLocation((Node *) bexpr->args));
1166 const SubLink *sublink = (const SubLink *) expr;
1168 /* check the testexpr, if any, and the operator/keyword */
1169 loc = leftmostLoc(exprLocation(sublink->testexpr),
1174 /* just use argument's location */
1175 loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
1178 /* just use argument's location */
1179 loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
1183 const RelabelType *rexpr = (const RelabelType *) expr;
1186 loc = leftmostLoc(rexpr->location,
1187 exprLocation((Node *) rexpr->arg));
1192 const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
1195 loc = leftmostLoc(cexpr->location,
1196 exprLocation((Node *) cexpr->arg));
1199 case T_ArrayCoerceExpr:
1201 const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
1204 loc = leftmostLoc(cexpr->location,
1205 exprLocation((Node *) cexpr->arg));
1208 case T_ConvertRowtypeExpr:
1210 const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
1213 loc = leftmostLoc(cexpr->location,
1214 exprLocation((Node *) cexpr->arg));
1218 /* just use argument's location */
1219 loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
1222 /* CASE keyword should always be the first thing */
1223 loc = ((const CaseExpr *) expr)->location;
1226 /* WHEN keyword should always be the first thing */
1227 loc = ((const CaseWhen *) expr)->location;
1230 /* the location points at ARRAY or [, which must be leftmost */
1231 loc = ((const ArrayExpr *) expr)->location;
1234 /* the location points at ROW or (, which must be leftmost */
1235 loc = ((const RowExpr *) expr)->location;
1237 case T_RowCompareExpr:
1238 /* just use leftmost argument's location */
1239 loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
1241 case T_CoalesceExpr:
1242 /* COALESCE keyword should always be the first thing */
1243 loc = ((const CoalesceExpr *) expr)->location;
1246 /* GREATEST/LEAST keyword should always be the first thing */
1247 loc = ((const MinMaxExpr *) expr)->location;
1251 const XmlExpr *xexpr = (const XmlExpr *) expr;
1253 /* consider both function name and leftmost arg */
1254 loc = leftmostLoc(xexpr->location,
1255 exprLocation((Node *) xexpr->args));
1259 /* just use argument's location */
1260 loc = exprLocation((Node *) ((const NullTest *) expr)->arg);
1263 /* just use argument's location */
1264 loc = exprLocation((Node *) ((const BooleanTest *) expr)->arg);
1266 case T_CoerceToDomain:
1268 const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
1271 loc = leftmostLoc(cexpr->location,
1272 exprLocation((Node *) cexpr->arg));
1275 case T_CoerceToDomainValue:
1276 loc = ((const CoerceToDomainValue *) expr)->location;
1278 case T_SetToDefault:
1279 loc = ((const SetToDefault *) expr)->location;
1282 /* just use argument's location */
1283 loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
1286 /* use the contained RangeVar's location --- close enough */
1287 loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
1291 /* report location of first list member that has a location */
1294 loc = -1; /* just to suppress compiler warning */
1295 foreach(lc, (const List *) expr)
1297 loc = exprLocation((Node *) lfirst(lc));
1305 const A_Expr *aexpr = (const A_Expr *) expr;
1307 /* use leftmost of operator or left operand (if any) */
1308 /* we assume right operand can't be to left of operator */
1309 loc = leftmostLoc(aexpr->location,
1310 exprLocation(aexpr->lexpr));
1314 loc = ((const ColumnRef *) expr)->location;
1317 loc = ((const ParamRef *) expr)->location;
1320 loc = ((const A_Const *) expr)->location;
1324 const FuncCall *fc = (const FuncCall *) expr;
1326 /* consider both function name and leftmost arg */
1327 /* (we assume any ORDER BY nodes must be to right of name) */
1328 loc = leftmostLoc(fc->location,
1329 exprLocation((Node *) fc->args));
1333 /* the location points at ARRAY or [, which must be leftmost */
1334 loc = ((const A_ArrayExpr *) expr)->location;
1337 /* we need not examine the contained expression (if any) */
1338 loc = ((const ResTarget *) expr)->location;
1342 const TypeCast *tc = (const TypeCast *) expr;
1345 * This could represent CAST(), ::, or TypeName 'literal', so
1346 * any of the components might be leftmost.
1348 loc = exprLocation(tc->arg);
1349 loc = leftmostLoc(loc, tc->typeName->location);
1350 loc = leftmostLoc(loc, tc->location);
1353 case T_CollateClause:
1354 /* just use argument's location */
1355 loc = exprLocation(((const CollateClause *) expr)->arg);
1358 /* just use argument's location (ignore operator, if any) */
1359 loc = exprLocation(((const SortBy *) expr)->node);
1362 loc = ((const WindowDef *) expr)->location;
1365 loc = ((const TypeName *) expr)->location;
1368 loc = ((const Constraint *) expr)->location;
1370 case T_XmlSerialize:
1371 /* XMLSERIALIZE keyword should always be the first thing */
1372 loc = ((const XmlSerialize *) expr)->location;
1375 loc = ((const WithClause *) expr)->location;
1377 case T_CommonTableExpr:
1378 loc = ((const CommonTableExpr *) expr)->location;
1380 case T_PlaceHolderVar:
1381 /* just use argument's location */
1382 loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1385 /* for any other node type it's just unknown... */
1393 * leftmostLoc - support for exprLocation
1395 * Take the minimum of two parse location values, but ignore unknowns
1398 leftmostLoc(int loc1, int loc2)
1405 return Min(loc1, loc2);
1410 * Standard expression-tree walking support
1412 * We used to have near-duplicate code in many different routines that
1413 * understood how to recurse through an expression node tree. That was
1414 * a pain to maintain, and we frequently had bugs due to some particular
1415 * routine neglecting to support a particular node type. In most cases,
1416 * these routines only actually care about certain node types, and don't
1417 * care about other types except insofar as they have to recurse through
1418 * non-primitive node types. Therefore, we now provide generic tree-walking
1419 * logic to consolidate the redundant "boilerplate" code. There are
1420 * two versions: expression_tree_walker() and expression_tree_mutator().
1424 * expression_tree_walker() is designed to support routines that traverse
1425 * a tree in a read-only fashion (although it will also work for routines
1426 * that modify nodes in-place but never add/delete/replace nodes).
1427 * A walker routine should look like this:
1429 * bool my_walker (Node *node, my_struct *context)
1433 * // check for nodes that special work is required for, eg:
1434 * if (IsA(node, Var))
1436 * ... do special actions for Var nodes
1438 * else if (IsA(node, ...))
1440 * ... do special actions for other node types
1442 * // for any node type not specially processed, do:
1443 * return expression_tree_walker(node, my_walker, (void *) context);
1446 * The "context" argument points to a struct that holds whatever context
1447 * information the walker routine needs --- it can be used to return data
1448 * gathered by the walker, too. This argument is not touched by
1449 * expression_tree_walker, but it is passed down to recursive sub-invocations
1450 * of my_walker. The tree walk is started from a setup routine that
1451 * fills in the appropriate context struct, calls my_walker with the top-level
1452 * node of the tree, and then examines the results.
1454 * The walker routine should return "false" to continue the tree walk, or
1455 * "true" to abort the walk and immediately return "true" to the top-level
1456 * caller. This can be used to short-circuit the traversal if the walker
1457 * has found what it came for. "false" is returned to the top-level caller
1458 * iff no invocation of the walker returned "true".
1460 * The node types handled by expression_tree_walker include all those
1461 * normally found in target lists and qualifier clauses during the planning
1462 * stage. In particular, it handles List nodes since a cnf-ified qual clause
1463 * will have List structure at the top level, and it handles TargetEntry nodes
1464 * so that a scan of a target list can be handled without additional code.
1465 * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
1466 * handled, so that query jointrees and setOperation trees can be processed
1467 * without additional code.
1469 * expression_tree_walker will handle SubLink nodes by recursing normally
1470 * into the "testexpr" subtree (which is an expression belonging to the outer
1471 * plan). It will also call the walker on the sub-Query node; however, when
1472 * expression_tree_walker itself is called on a Query node, it does nothing
1473 * and returns "false". The net effect is that unless the walker does
1474 * something special at a Query node, sub-selects will not be visited during
1475 * an expression tree walk. This is exactly the behavior wanted in many cases
1476 * --- and for those walkers that do want to recurse into sub-selects, special
1477 * behavior is typically needed anyway at the entry to a sub-select (such as
1478 * incrementing a depth counter). A walker that wants to examine sub-selects
1479 * should include code along the lines of:
1481 * if (IsA(node, Query))
1483 * adjust context for subquery;
1484 * result = query_tree_walker((Query *) node, my_walker, context,
1485 * 0); // adjust flags as needed
1486 * restore context if needed;
1490 * query_tree_walker is a convenience routine (see below) that calls the
1491 * walker on all the expression subtrees of the given Query node.
1493 * expression_tree_walker will handle SubPlan nodes by recursing normally
1494 * into the "testexpr" and the "args" list (which are expressions belonging to
1495 * the outer plan). It will not touch the completed subplan, however. Since
1496 * there is no link to the original Query, it is not possible to recurse into
1497 * subselects of an already-planned expression tree. This is OK for current
1498 * uses, but may need to be revisited in future.
1502 expression_tree_walker(Node *node,
1509 * The walker has already visited the current node, and so we need only
1510 * recurse into any sub-nodes it has.
1512 * We assume that the walker is not interested in List nodes per se, so
1513 * when we expect a List we just recurse directly to self without
1514 * bothering to call the walker.
1519 /* Guard against stack overflow due to overly complex expressions */
1520 check_stack_depth();
1522 switch (nodeTag(node))
1527 case T_CoerceToDomainValue:
1528 case T_CaseTestExpr:
1529 case T_SetToDefault:
1530 case T_CurrentOfExpr:
1532 case T_SortGroupClause:
1533 /* primitive node types with no expression subnodes */
1537 Aggref *expr = (Aggref *) node;
1539 /* recurse directly on List */
1540 if (expression_tree_walker((Node *) expr->args,
1543 if (expression_tree_walker((Node *) expr->aggorder,
1546 if (expression_tree_walker((Node *) expr->aggdistinct,
1553 WindowFunc *expr = (WindowFunc *) node;
1555 /* recurse directly on List */
1556 if (expression_tree_walker((Node *) expr->args,
1563 ArrayRef *aref = (ArrayRef *) node;
1565 /* recurse directly for upper/lower array index lists */
1566 if (expression_tree_walker((Node *) aref->refupperindexpr,
1569 if (expression_tree_walker((Node *) aref->reflowerindexpr,
1572 /* walker must see the refexpr and refassgnexpr, however */
1573 if (walker(aref->refexpr, context))
1575 if (walker(aref->refassgnexpr, context))
1581 FuncExpr *expr = (FuncExpr *) node;
1583 if (expression_tree_walker((Node *) expr->args,
1588 case T_NamedArgExpr:
1589 return walker(((NamedArgExpr *) node)->arg, context);
1591 case T_DistinctExpr: /* struct-equivalent to OpExpr */
1592 case T_NullIfExpr: /* struct-equivalent to OpExpr */
1594 OpExpr *expr = (OpExpr *) node;
1596 if (expression_tree_walker((Node *) expr->args,
1601 case T_ScalarArrayOpExpr:
1603 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1605 if (expression_tree_walker((Node *) expr->args,
1612 BoolExpr *expr = (BoolExpr *) node;
1614 if (expression_tree_walker((Node *) expr->args,
1621 SubLink *sublink = (SubLink *) node;
1623 if (walker(sublink->testexpr, context))
1627 * Also invoke the walker on the sublink's Query node, so it
1628 * can recurse into the sub-query if it wants to.
1630 return walker(sublink->subselect, context);
1635 SubPlan *subplan = (SubPlan *) node;
1637 /* recurse into the testexpr, but not into the Plan */
1638 if (walker(subplan->testexpr, context))
1640 /* also examine args list */
1641 if (expression_tree_walker((Node *) subplan->args,
1646 case T_AlternativeSubPlan:
1647 return walker(((AlternativeSubPlan *) node)->subplans, context);
1649 return walker(((FieldSelect *) node)->arg, context);
1652 FieldStore *fstore = (FieldStore *) node;
1654 if (walker(fstore->arg, context))
1656 if (walker(fstore->newvals, context))
1661 return walker(((RelabelType *) node)->arg, context);
1663 return walker(((CoerceViaIO *) node)->arg, context);
1664 case T_ArrayCoerceExpr:
1665 return walker(((ArrayCoerceExpr *) node)->arg, context);
1666 case T_ConvertRowtypeExpr:
1667 return walker(((ConvertRowtypeExpr *) node)->arg, context);
1669 return walker(((CollateExpr *) node)->arg, context);
1672 CaseExpr *caseexpr = (CaseExpr *) node;
1674 if (walker(caseexpr->arg, context))
1676 /* we assume walker doesn't care about CaseWhens, either */
1677 foreach(temp, caseexpr->args)
1679 CaseWhen *when = (CaseWhen *) lfirst(temp);
1681 Assert(IsA(when, CaseWhen));
1682 if (walker(when->expr, context))
1684 if (walker(when->result, context))
1687 if (walker(caseexpr->defresult, context))
1692 return walker(((ArrayExpr *) node)->elements, context);
1694 /* Assume colnames isn't interesting */
1695 return walker(((RowExpr *) node)->args, context);
1696 case T_RowCompareExpr:
1698 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1700 if (walker(rcexpr->largs, context))
1702 if (walker(rcexpr->rargs, context))
1706 case T_CoalesceExpr:
1707 return walker(((CoalesceExpr *) node)->args, context);
1709 return walker(((MinMaxExpr *) node)->args, context);
1712 XmlExpr *xexpr = (XmlExpr *) node;
1714 if (walker(xexpr->named_args, context))
1716 /* we assume walker doesn't care about arg_names */
1717 if (walker(xexpr->args, context))
1722 return walker(((NullTest *) node)->arg, context);
1724 return walker(((BooleanTest *) node)->arg, context);
1725 case T_CoerceToDomain:
1726 return walker(((CoerceToDomain *) node)->arg, context);
1728 return walker(((TargetEntry *) node)->expr, context);
1730 /* Do nothing with a sub-Query, per discussion above */
1732 case T_WindowClause:
1734 WindowClause *wc = (WindowClause *) node;
1736 if (walker(wc->partitionClause, context))
1738 if (walker(wc->orderClause, context))
1740 if (walker(wc->startOffset, context))
1742 if (walker(wc->endOffset, context))
1746 case T_CommonTableExpr:
1748 CommonTableExpr *cte = (CommonTableExpr *) node;
1751 * Invoke the walker on the CTE's Query node, so it can
1752 * recurse into the sub-query if it wants to.
1754 return walker(cte->ctequery, context);
1758 foreach(temp, (List *) node)
1760 if (walker((Node *) lfirst(temp), context))
1766 FromExpr *from = (FromExpr *) node;
1768 if (walker(from->fromlist, context))
1770 if (walker(from->quals, context))
1776 JoinExpr *join = (JoinExpr *) node;
1778 if (walker(join->larg, context))
1780 if (walker(join->rarg, context))
1782 if (walker(join->quals, context))
1786 * alias clause, using list are deemed uninteresting.
1790 case T_SetOperationStmt:
1792 SetOperationStmt *setop = (SetOperationStmt *) node;
1794 if (walker(setop->larg, context))
1796 if (walker(setop->rarg, context))
1799 /* groupClauses are deemed uninteresting */
1802 case T_PlaceHolderVar:
1803 return walker(((PlaceHolderVar *) node)->phexpr, context);
1804 case T_AppendRelInfo:
1806 AppendRelInfo *appinfo = (AppendRelInfo *) node;
1808 if (expression_tree_walker((Node *) appinfo->translated_vars,
1813 case T_PlaceHolderInfo:
1814 return walker(((PlaceHolderInfo *) node)->ph_var, context);
1816 elog(ERROR, "unrecognized node type: %d",
1817 (int) nodeTag(node));
1824 * query_tree_walker --- initiate a walk of a Query's expressions
1826 * This routine exists just to reduce the number of places that need to know
1827 * where all the expression subtrees of a Query are. Note it can be used
1828 * for starting a walk at top level of a Query regardless of whether the
1829 * walker intends to descend into subqueries. It is also useful for
1830 * descending into subqueries within a walker.
1832 * Some callers want to suppress visitation of certain items in the sub-Query,
1833 * typically because they need to process them specially, or don't actually
1834 * want to recurse into subqueries. This is supported by the flags argument,
1835 * which is the bitwise OR of flag values to suppress visitation of
1836 * indicated items. (More flag bits may be added as needed.)
1839 query_tree_walker(Query *query,
1844 Assert(query != NULL && IsA(query, Query));
1846 if (walker((Node *) query->targetList, context))
1848 if (walker((Node *) query->returningList, context))
1850 if (walker((Node *) query->jointree, context))
1852 if (walker(query->setOperations, context))
1854 if (walker(query->havingQual, context))
1856 if (walker(query->limitOffset, context))
1858 if (walker(query->limitCount, context))
1860 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
1862 if (walker((Node *) query->cteList, context))
1865 if (!(flags & QTW_IGNORE_RANGE_TABLE))
1867 if (range_table_walker(query->rtable, walker, context, flags))
1874 * range_table_walker is just the part of query_tree_walker that scans
1875 * a query's rangetable. This is split out since it can be useful on
1879 range_table_walker(List *rtable,
1888 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
1890 /* For historical reasons, visiting RTEs is not the default */
1891 if (flags & QTW_EXAMINE_RTES)
1892 if (walker(rte, context))
1895 switch (rte->rtekind)
1902 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
1903 if (walker(rte->subquery, context))
1907 if (!(flags & QTW_IGNORE_JOINALIASES))
1908 if (walker(rte->joinaliasvars, context))
1912 if (walker(rte->funcexpr, context))
1916 if (walker(rte->values_lists, context))
1926 * expression_tree_mutator() is designed to support routines that make a
1927 * modified copy of an expression tree, with some nodes being added,
1928 * removed, or replaced by new subtrees. The original tree is (normally)
1929 * not changed. Each recursion level is responsible for returning a copy of
1930 * (or appropriately modified substitute for) the subtree it is handed.
1931 * A mutator routine should look like this:
1933 * Node * my_mutator (Node *node, my_struct *context)
1937 * // check for nodes that special work is required for, eg:
1938 * if (IsA(node, Var))
1940 * ... create and return modified copy of Var node
1942 * else if (IsA(node, ...))
1944 * ... do special transformations of other node types
1946 * // for any node type not specially processed, do:
1947 * return expression_tree_mutator(node, my_mutator, (void *) context);
1950 * The "context" argument points to a struct that holds whatever context
1951 * information the mutator routine needs --- it can be used to return extra
1952 * data gathered by the mutator, too. This argument is not touched by
1953 * expression_tree_mutator, but it is passed down to recursive sub-invocations
1954 * of my_mutator. The tree walk is started from a setup routine that
1955 * fills in the appropriate context struct, calls my_mutator with the
1956 * top-level node of the tree, and does any required post-processing.
1958 * Each level of recursion must return an appropriately modified Node.
1959 * If expression_tree_mutator() is called, it will make an exact copy
1960 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
1961 * of that Node. In this way, my_mutator() has full control over the
1962 * copying process but need not directly deal with expression trees
1963 * that it has no interest in.
1965 * Just as for expression_tree_walker, the node types handled by
1966 * expression_tree_mutator include all those normally found in target lists
1967 * and qualifier clauses during the planning stage.
1969 * expression_tree_mutator will handle SubLink nodes by recursing normally
1970 * into the "testexpr" subtree (which is an expression belonging to the outer
1971 * plan). It will also call the mutator on the sub-Query node; however, when
1972 * expression_tree_mutator itself is called on a Query node, it does nothing
1973 * and returns the unmodified Query node. The net effect is that unless the
1974 * mutator does something special at a Query node, sub-selects will not be
1975 * visited or modified; the original sub-select will be linked to by the new
1976 * SubLink node. Mutators that want to descend into sub-selects will usually
1977 * do so by recognizing Query nodes and calling query_tree_mutator (below).
1979 * expression_tree_mutator will handle a SubPlan node by recursing into the
1980 * "testexpr" and the "args" list (which belong to the outer plan), but it
1981 * will simply copy the link to the inner plan, since that's typically what
1982 * expression tree mutators want. A mutator that wants to modify the subplan
1983 * can force appropriate behavior by recognizing SubPlan expression nodes
1984 * and doing the right thing.
1988 expression_tree_mutator(Node *node,
1989 Node *(*mutator) (),
1993 * The mutator has already decided not to modify the current node, but we
1994 * must call the mutator for any sub-nodes.
1997 #define FLATCOPY(newnode, node, nodetype) \
1998 ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
1999 memcpy((newnode), (node), sizeof(nodetype)) )
2001 #define CHECKFLATCOPY(newnode, node, nodetype) \
2002 ( AssertMacro(IsA((node), nodetype)), \
2003 (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2004 memcpy((newnode), (node), sizeof(nodetype)) )
2006 #define MUTATE(newfield, oldfield, fieldtype) \
2007 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
2012 /* Guard against stack overflow due to overly complex expressions */
2013 check_stack_depth();
2015 switch (nodeTag(node))
2018 * Primitive node types with no expression subnodes. Var and
2019 * Const are frequent enough to deserve special cases, the others
2020 * we just use copyObject for.
2024 Var *var = (Var *) node;
2027 FLATCOPY(newnode, var, Var);
2028 return (Node *) newnode;
2033 Const *oldnode = (Const *) node;
2036 FLATCOPY(newnode, oldnode, Const);
2037 /* XXX we don't bother with datumCopy; should we? */
2038 return (Node *) newnode;
2042 case T_CoerceToDomainValue:
2043 case T_CaseTestExpr:
2044 case T_SetToDefault:
2045 case T_CurrentOfExpr:
2047 case T_SortGroupClause:
2048 return (Node *) copyObject(node);
2051 Aggref *aggref = (Aggref *) node;
2054 FLATCOPY(newnode, aggref, Aggref);
2055 MUTATE(newnode->args, aggref->args, List *);
2056 MUTATE(newnode->aggorder, aggref->aggorder, List *);
2057 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
2058 return (Node *) newnode;
2063 WindowFunc *wfunc = (WindowFunc *) node;
2064 WindowFunc *newnode;
2066 FLATCOPY(newnode, wfunc, WindowFunc);
2067 MUTATE(newnode->args, wfunc->args, List *);
2068 return (Node *) newnode;
2073 ArrayRef *arrayref = (ArrayRef *) node;
2076 FLATCOPY(newnode, arrayref, ArrayRef);
2077 MUTATE(newnode->refupperindexpr, arrayref->refupperindexpr,
2079 MUTATE(newnode->reflowerindexpr, arrayref->reflowerindexpr,
2081 MUTATE(newnode->refexpr, arrayref->refexpr,
2083 MUTATE(newnode->refassgnexpr, arrayref->refassgnexpr,
2085 return (Node *) newnode;
2090 FuncExpr *expr = (FuncExpr *) node;
2093 FLATCOPY(newnode, expr, FuncExpr);
2094 MUTATE(newnode->args, expr->args, List *);
2095 return (Node *) newnode;
2098 case T_NamedArgExpr:
2100 NamedArgExpr *nexpr = (NamedArgExpr *) node;
2101 NamedArgExpr *newnode;
2103 FLATCOPY(newnode, nexpr, NamedArgExpr);
2104 MUTATE(newnode->arg, nexpr->arg, Expr *);
2105 return (Node *) newnode;
2110 OpExpr *expr = (OpExpr *) node;
2113 FLATCOPY(newnode, expr, OpExpr);
2114 MUTATE(newnode->args, expr->args, List *);
2115 return (Node *) newnode;
2118 case T_DistinctExpr:
2120 DistinctExpr *expr = (DistinctExpr *) node;
2121 DistinctExpr *newnode;
2123 FLATCOPY(newnode, expr, DistinctExpr);
2124 MUTATE(newnode->args, expr->args, List *);
2125 return (Node *) newnode;
2130 NullIfExpr *expr = (NullIfExpr *) node;
2131 NullIfExpr *newnode;
2133 FLATCOPY(newnode, expr, NullIfExpr);
2134 MUTATE(newnode->args, expr->args, List *);
2135 return (Node *) newnode;
2138 case T_ScalarArrayOpExpr:
2140 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2141 ScalarArrayOpExpr *newnode;
2143 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
2144 MUTATE(newnode->args, expr->args, List *);
2145 return (Node *) newnode;
2150 BoolExpr *expr = (BoolExpr *) node;
2153 FLATCOPY(newnode, expr, BoolExpr);
2154 MUTATE(newnode->args, expr->args, List *);
2155 return (Node *) newnode;
2160 SubLink *sublink = (SubLink *) node;
2163 FLATCOPY(newnode, sublink, SubLink);
2164 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
2167 * Also invoke the mutator on the sublink's Query node, so it
2168 * can recurse into the sub-query if it wants to.
2170 MUTATE(newnode->subselect, sublink->subselect, Node *);
2171 return (Node *) newnode;
2176 SubPlan *subplan = (SubPlan *) node;
2179 FLATCOPY(newnode, subplan, SubPlan);
2180 /* transform testexpr */
2181 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
2182 /* transform args list (params to be passed to subplan) */
2183 MUTATE(newnode->args, subplan->args, List *);
2184 /* but not the sub-Plan itself, which is referenced as-is */
2185 return (Node *) newnode;
2188 case T_AlternativeSubPlan:
2190 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
2191 AlternativeSubPlan *newnode;
2193 FLATCOPY(newnode, asplan, AlternativeSubPlan);
2194 MUTATE(newnode->subplans, asplan->subplans, List *);
2195 return (Node *) newnode;
2200 FieldSelect *fselect = (FieldSelect *) node;
2201 FieldSelect *newnode;
2203 FLATCOPY(newnode, fselect, FieldSelect);
2204 MUTATE(newnode->arg, fselect->arg, Expr *);
2205 return (Node *) newnode;
2210 FieldStore *fstore = (FieldStore *) node;
2211 FieldStore *newnode;
2213 FLATCOPY(newnode, fstore, FieldStore);
2214 MUTATE(newnode->arg, fstore->arg, Expr *);
2215 MUTATE(newnode->newvals, fstore->newvals, List *);
2216 newnode->fieldnums = list_copy(fstore->fieldnums);
2217 return (Node *) newnode;
2222 RelabelType *relabel = (RelabelType *) node;
2223 RelabelType *newnode;
2225 FLATCOPY(newnode, relabel, RelabelType);
2226 MUTATE(newnode->arg, relabel->arg, Expr *);
2227 return (Node *) newnode;
2232 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
2233 CoerceViaIO *newnode;
2235 FLATCOPY(newnode, iocoerce, CoerceViaIO);
2236 MUTATE(newnode->arg, iocoerce->arg, Expr *);
2237 return (Node *) newnode;
2240 case T_ArrayCoerceExpr:
2242 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2243 ArrayCoerceExpr *newnode;
2245 FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
2246 MUTATE(newnode->arg, acoerce->arg, Expr *);
2247 return (Node *) newnode;
2250 case T_ConvertRowtypeExpr:
2252 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
2253 ConvertRowtypeExpr *newnode;
2255 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
2256 MUTATE(newnode->arg, convexpr->arg, Expr *);
2257 return (Node *) newnode;
2262 CollateExpr *collate = (CollateExpr *) node;
2263 CollateExpr *newnode;
2265 FLATCOPY(newnode, collate, CollateExpr);
2266 MUTATE(newnode->arg, collate->arg, Expr *);
2267 return (Node *) newnode;
2272 CaseExpr *caseexpr = (CaseExpr *) node;
2275 FLATCOPY(newnode, caseexpr, CaseExpr);
2276 MUTATE(newnode->arg, caseexpr->arg, Expr *);
2277 MUTATE(newnode->args, caseexpr->args, List *);
2278 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
2279 return (Node *) newnode;
2284 CaseWhen *casewhen = (CaseWhen *) node;
2287 FLATCOPY(newnode, casewhen, CaseWhen);
2288 MUTATE(newnode->expr, casewhen->expr, Expr *);
2289 MUTATE(newnode->result, casewhen->result, Expr *);
2290 return (Node *) newnode;
2295 ArrayExpr *arrayexpr = (ArrayExpr *) node;
2298 FLATCOPY(newnode, arrayexpr, ArrayExpr);
2299 MUTATE(newnode->elements, arrayexpr->elements, List *);
2300 return (Node *) newnode;
2305 RowExpr *rowexpr = (RowExpr *) node;
2308 FLATCOPY(newnode, rowexpr, RowExpr);
2309 MUTATE(newnode->args, rowexpr->args, List *);
2310 /* Assume colnames needn't be duplicated */
2311 return (Node *) newnode;
2314 case T_RowCompareExpr:
2316 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2317 RowCompareExpr *newnode;
2319 FLATCOPY(newnode, rcexpr, RowCompareExpr);
2320 MUTATE(newnode->largs, rcexpr->largs, List *);
2321 MUTATE(newnode->rargs, rcexpr->rargs, List *);
2322 return (Node *) newnode;
2325 case T_CoalesceExpr:
2327 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2328 CoalesceExpr *newnode;
2330 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
2331 MUTATE(newnode->args, coalesceexpr->args, List *);
2332 return (Node *) newnode;
2337 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2338 MinMaxExpr *newnode;
2340 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
2341 MUTATE(newnode->args, minmaxexpr->args, List *);
2342 return (Node *) newnode;
2347 XmlExpr *xexpr = (XmlExpr *) node;
2350 FLATCOPY(newnode, xexpr, XmlExpr);
2351 MUTATE(newnode->named_args, xexpr->named_args, List *);
2352 /* assume mutator does not care about arg_names */
2353 MUTATE(newnode->args, xexpr->args, List *);
2354 return (Node *) newnode;
2359 NullTest *ntest = (NullTest *) node;
2362 FLATCOPY(newnode, ntest, NullTest);
2363 MUTATE(newnode->arg, ntest->arg, Expr *);
2364 return (Node *) newnode;
2369 BooleanTest *btest = (BooleanTest *) node;
2370 BooleanTest *newnode;
2372 FLATCOPY(newnode, btest, BooleanTest);
2373 MUTATE(newnode->arg, btest->arg, Expr *);
2374 return (Node *) newnode;
2377 case T_CoerceToDomain:
2379 CoerceToDomain *ctest = (CoerceToDomain *) node;
2380 CoerceToDomain *newnode;
2382 FLATCOPY(newnode, ctest, CoerceToDomain);
2383 MUTATE(newnode->arg, ctest->arg, Expr *);
2384 return (Node *) newnode;
2389 TargetEntry *targetentry = (TargetEntry *) node;
2390 TargetEntry *newnode;
2392 FLATCOPY(newnode, targetentry, TargetEntry);
2393 MUTATE(newnode->expr, targetentry->expr, Expr *);
2394 return (Node *) newnode;
2398 /* Do nothing with a sub-Query, per discussion above */
2400 case T_WindowClause:
2402 WindowClause *wc = (WindowClause *) node;
2403 WindowClause *newnode;
2405 FLATCOPY(newnode, wc, WindowClause);
2406 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
2407 MUTATE(newnode->orderClause, wc->orderClause, List *);
2408 MUTATE(newnode->startOffset, wc->startOffset, Node *);
2409 MUTATE(newnode->endOffset, wc->endOffset, Node *);
2410 return (Node *) newnode;
2413 case T_CommonTableExpr:
2415 CommonTableExpr *cte = (CommonTableExpr *) node;
2416 CommonTableExpr *newnode;
2418 FLATCOPY(newnode, cte, CommonTableExpr);
2421 * Also invoke the mutator on the CTE's Query node, so it can
2422 * recurse into the sub-query if it wants to.
2424 MUTATE(newnode->ctequery, cte->ctequery, Node *);
2425 return (Node *) newnode;
2431 * We assume the mutator isn't interested in the list nodes
2432 * per se, so just invoke it on each list element. NOTE: this
2433 * would fail badly on a list with integer elements!
2439 foreach(temp, (List *) node)
2441 resultlist = lappend(resultlist,
2442 mutator((Node *) lfirst(temp),
2445 return (Node *) resultlist;
2450 FromExpr *from = (FromExpr *) node;
2453 FLATCOPY(newnode, from, FromExpr);
2454 MUTATE(newnode->fromlist, from->fromlist, List *);
2455 MUTATE(newnode->quals, from->quals, Node *);
2456 return (Node *) newnode;
2461 JoinExpr *join = (JoinExpr *) node;
2464 FLATCOPY(newnode, join, JoinExpr);
2465 MUTATE(newnode->larg, join->larg, Node *);
2466 MUTATE(newnode->rarg, join->rarg, Node *);
2467 MUTATE(newnode->quals, join->quals, Node *);
2468 /* We do not mutate alias or using by default */
2469 return (Node *) newnode;
2472 case T_SetOperationStmt:
2474 SetOperationStmt *setop = (SetOperationStmt *) node;
2475 SetOperationStmt *newnode;
2477 FLATCOPY(newnode, setop, SetOperationStmt);
2478 MUTATE(newnode->larg, setop->larg, Node *);
2479 MUTATE(newnode->rarg, setop->rarg, Node *);
2480 /* We do not mutate groupClauses by default */
2481 return (Node *) newnode;
2484 case T_PlaceHolderVar:
2486 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2487 PlaceHolderVar *newnode;
2489 FLATCOPY(newnode, phv, PlaceHolderVar);
2490 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
2491 /* Assume we need not copy the relids bitmapset */
2492 return (Node *) newnode;
2495 case T_AppendRelInfo:
2497 AppendRelInfo *appinfo = (AppendRelInfo *) node;
2498 AppendRelInfo *newnode;
2500 FLATCOPY(newnode, appinfo, AppendRelInfo);
2501 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
2502 return (Node *) newnode;
2505 case T_PlaceHolderInfo:
2507 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
2508 PlaceHolderInfo *newnode;
2510 FLATCOPY(newnode, phinfo, PlaceHolderInfo);
2511 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
2512 /* Assume we need not copy the relids bitmapsets */
2513 return (Node *) newnode;
2517 elog(ERROR, "unrecognized node type: %d",
2518 (int) nodeTag(node));
2521 /* can't get here, but keep compiler happy */
2527 * query_tree_mutator --- initiate modification of a Query's expressions
2529 * This routine exists just to reduce the number of places that need to know
2530 * where all the expression subtrees of a Query are. Note it can be used
2531 * for starting a walk at top level of a Query regardless of whether the
2532 * mutator intends to descend into subqueries. It is also useful for
2533 * descending into subqueries within a mutator.
2535 * Some callers want to suppress mutating of certain items in the Query,
2536 * typically because they need to process them specially, or don't actually
2537 * want to recurse into subqueries. This is supported by the flags argument,
2538 * which is the bitwise OR of flag values to suppress mutating of
2539 * indicated items. (More flag bits may be added as needed.)
2541 * Normally the Query node itself is copied, but some callers want it to be
2542 * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags. All
2543 * modified substructure is safely copied in any case.
2546 query_tree_mutator(Query *query,
2547 Node *(*mutator) (),
2551 Assert(query != NULL && IsA(query, Query));
2553 if (!(flags & QTW_DONT_COPY_QUERY))
2557 FLATCOPY(newquery, query, Query);
2561 MUTATE(query->targetList, query->targetList, List *);
2562 MUTATE(query->returningList, query->returningList, List *);
2563 MUTATE(query->jointree, query->jointree, FromExpr *);
2564 MUTATE(query->setOperations, query->setOperations, Node *);
2565 MUTATE(query->havingQual, query->havingQual, Node *);
2566 MUTATE(query->limitOffset, query->limitOffset, Node *);
2567 MUTATE(query->limitCount, query->limitCount, Node *);
2568 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2569 MUTATE(query->cteList, query->cteList, List *);
2570 else /* else copy CTE list as-is */
2571 query->cteList = copyObject(query->cteList);
2572 query->rtable = range_table_mutator(query->rtable,
2573 mutator, context, flags);
2578 * range_table_mutator is just the part of query_tree_mutator that processes
2579 * a query's rangetable. This is split out since it can be useful on
2583 range_table_mutator(List *rtable,
2584 Node *(*mutator) (),
2593 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2594 RangeTblEntry *newrte;
2596 FLATCOPY(newrte, rte, RangeTblEntry);
2597 switch (rte->rtekind)
2601 /* we don't bother to copy eref, aliases, etc; OK? */
2604 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2606 CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
2607 MUTATE(newrte->subquery, newrte->subquery, Query *);
2611 /* else, copy RT subqueries as-is */
2612 newrte->subquery = copyObject(rte->subquery);
2616 if (!(flags & QTW_IGNORE_JOINALIASES))
2617 MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
2620 /* else, copy join aliases as-is */
2621 newrte->joinaliasvars = copyObject(rte->joinaliasvars);
2625 MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
2628 MUTATE(newrte->values_lists, rte->values_lists, List *);
2631 newrt = lappend(newrt, newrte);
2637 * query_or_expression_tree_walker --- hybrid form
2639 * This routine will invoke query_tree_walker if called on a Query node,
2640 * else will invoke the walker directly. This is a useful way of starting
2641 * the recursion when the walker's normal change of state is not appropriate
2642 * for the outermost Query node.
2645 query_or_expression_tree_walker(Node *node,
2650 if (node && IsA(node, Query))
2651 return query_tree_walker((Query *) node,
2656 return walker(node, context);
2660 * query_or_expression_tree_mutator --- hybrid form
2662 * This routine will invoke query_tree_mutator if called on a Query node,
2663 * else will invoke the mutator directly. This is a useful way of starting
2664 * the recursion when the mutator's normal change of state is not appropriate
2665 * for the outermost Query node.
2668 query_or_expression_tree_mutator(Node *node,
2669 Node *(*mutator) (),
2673 if (node && IsA(node, Query))
2674 return (Node *) query_tree_mutator((Query *) node,
2679 return mutator(node, context);
2684 * raw_expression_tree_walker --- walk raw parse trees
2686 * This has exactly the same API as expression_tree_walker, but instead of
2687 * walking post-analysis parse trees, it knows how to walk the node types
2688 * found in raw grammar output. (There is not currently any need for a
2689 * combined walker, so we keep them separate in the name of efficiency.)
2690 * Unlike expression_tree_walker, there is no special rule about query
2691 * boundaries: we descend to everything that's possibly interesting.
2693 * Currently, the node type coverage extends to SelectStmt and everything
2694 * that could appear under it, but not other statement types.
2697 raw_expression_tree_walker(Node *node, bool (*walker) (), void *context)
2702 * The walker has already visited the current node, and so we need only
2703 * recurse into any sub-nodes it has.
2708 /* Guard against stack overflow due to overly complex expressions */
2709 check_stack_depth();
2711 switch (nodeTag(node))
2713 case T_SetToDefault:
2714 case T_CurrentOfExpr:
2723 /* primitive node types with no subnodes */
2726 /* we assume the colnames list isn't interesting */
2729 return walker(((RangeVar *) node)->alias, context);
2732 SubLink *sublink = (SubLink *) node;
2734 if (walker(sublink->testexpr, context))
2736 /* we assume the operName is not interesting */
2737 if (walker(sublink->subselect, context))
2743 CaseExpr *caseexpr = (CaseExpr *) node;
2745 if (walker(caseexpr->arg, context))
2747 /* we assume walker doesn't care about CaseWhens, either */
2748 foreach(temp, caseexpr->args)
2750 CaseWhen *when = (CaseWhen *) lfirst(temp);
2752 Assert(IsA(when, CaseWhen));
2753 if (walker(when->expr, context))
2755 if (walker(when->result, context))
2758 if (walker(caseexpr->defresult, context))
2763 /* Assume colnames isn't interesting */
2764 return walker(((RowExpr *) node)->args, context);
2765 case T_CoalesceExpr:
2766 return walker(((CoalesceExpr *) node)->args, context);
2768 return walker(((MinMaxExpr *) node)->args, context);
2771 XmlExpr *xexpr = (XmlExpr *) node;
2773 if (walker(xexpr->named_args, context))
2775 /* we assume walker doesn't care about arg_names */
2776 if (walker(xexpr->args, context))
2781 return walker(((NullTest *) node)->arg, context);
2783 return walker(((BooleanTest *) node)->arg, context);
2786 JoinExpr *join = (JoinExpr *) node;
2788 if (walker(join->larg, context))
2790 if (walker(join->rarg, context))
2792 if (walker(join->quals, context))
2794 if (walker(join->alias, context))
2796 /* using list is deemed uninteresting */
2801 IntoClause *into = (IntoClause *) node;
2803 if (walker(into->rel, context))
2805 /* colNames, options are deemed uninteresting */
2809 foreach(temp, (List *) node)
2811 if (walker((Node *) lfirst(temp), context))
2817 InsertStmt *stmt = (InsertStmt *) node;
2819 if (walker(stmt->relation, context))
2821 if (walker(stmt->cols, context))
2823 if (walker(stmt->selectStmt, context))
2825 if (walker(stmt->returningList, context))
2827 if (walker(stmt->withClause, context))
2833 DeleteStmt *stmt = (DeleteStmt *) node;
2835 if (walker(stmt->relation, context))
2837 if (walker(stmt->usingClause, context))
2839 if (walker(stmt->whereClause, context))
2841 if (walker(stmt->returningList, context))
2843 if (walker(stmt->withClause, context))
2849 UpdateStmt *stmt = (UpdateStmt *) node;
2851 if (walker(stmt->relation, context))
2853 if (walker(stmt->targetList, context))
2855 if (walker(stmt->whereClause, context))
2857 if (walker(stmt->fromClause, context))
2859 if (walker(stmt->returningList, context))
2861 if (walker(stmt->withClause, context))
2867 SelectStmt *stmt = (SelectStmt *) node;
2869 if (walker(stmt->distinctClause, context))
2871 if (walker(stmt->intoClause, context))
2873 if (walker(stmt->targetList, context))
2875 if (walker(stmt->fromClause, context))
2877 if (walker(stmt->whereClause, context))
2879 if (walker(stmt->groupClause, context))
2881 if (walker(stmt->havingClause, context))
2883 if (walker(stmt->windowClause, context))
2885 if (walker(stmt->withClause, context))
2887 if (walker(stmt->valuesLists, context))
2889 if (walker(stmt->sortClause, context))
2891 if (walker(stmt->limitOffset, context))
2893 if (walker(stmt->limitCount, context))
2895 if (walker(stmt->lockingClause, context))
2897 if (walker(stmt->larg, context))
2899 if (walker(stmt->rarg, context))
2905 A_Expr *expr = (A_Expr *) node;
2907 if (walker(expr->lexpr, context))
2909 if (walker(expr->rexpr, context))
2911 /* operator name is deemed uninteresting */
2915 /* we assume the fields contain nothing interesting */
2919 FuncCall *fcall = (FuncCall *) node;
2921 if (walker(fcall->args, context))
2923 if (walker(fcall->agg_order, context))
2925 if (walker(fcall->over, context))
2927 /* function name is deemed uninteresting */
2930 case T_NamedArgExpr:
2931 return walker(((NamedArgExpr *) node)->arg, context);
2934 A_Indices *indices = (A_Indices *) node;
2936 if (walker(indices->lidx, context))
2938 if (walker(indices->uidx, context))
2942 case T_A_Indirection:
2944 A_Indirection *indir = (A_Indirection *) node;
2946 if (walker(indir->arg, context))
2948 if (walker(indir->indirection, context))
2953 return walker(((A_ArrayExpr *) node)->elements, context);
2956 ResTarget *rt = (ResTarget *) node;
2958 if (walker(rt->indirection, context))
2960 if (walker(rt->val, context))
2966 TypeCast *tc = (TypeCast *) node;
2968 if (walker(tc->arg, context))
2970 if (walker(tc->typeName, context))
2974 case T_CollateClause:
2975 return walker(((CollateClause *) node)->arg, context);
2977 return walker(((SortBy *) node)->node, context);
2980 WindowDef *wd = (WindowDef *) node;
2982 if (walker(wd->partitionClause, context))
2984 if (walker(wd->orderClause, context))
2986 if (walker(wd->startOffset, context))
2988 if (walker(wd->endOffset, context))
2992 case T_RangeSubselect:
2994 RangeSubselect *rs = (RangeSubselect *) node;
2996 if (walker(rs->subquery, context))
2998 if (walker(rs->alias, context))
3002 case T_RangeFunction:
3004 RangeFunction *rf = (RangeFunction *) node;
3006 if (walker(rf->funccallnode, context))
3008 if (walker(rf->alias, context))
3014 TypeName *tn = (TypeName *) node;
3016 if (walker(tn->typmods, context))
3018 if (walker(tn->arrayBounds, context))
3020 /* type name itself is deemed uninteresting */
3025 ColumnDef *coldef = (ColumnDef *) node;
3027 if (walker(coldef->typeName, context))
3029 if (walker(coldef->raw_default, context))
3031 if (walker(coldef->collClause, context))
3033 /* for now, constraints are ignored */
3036 case T_LockingClause:
3037 return walker(((LockingClause *) node)->lockedRels, context);
3038 case T_XmlSerialize:
3040 XmlSerialize *xs = (XmlSerialize *) node;
3042 if (walker(xs->expr, context))
3044 if (walker(xs->typeName, context))
3049 return walker(((WithClause *) node)->ctes, context);
3050 case T_CommonTableExpr:
3051 return walker(((CommonTableExpr *) node)->ctequery, context);
3053 elog(ERROR, "unrecognized node type: %d",
3054 (int) nodeTag(node));