1 /*-------------------------------------------------------------------------
4 * Various general-purpose manipulations of Node trees
6 * Portions Copyright (c) 1996-2012, 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/makefuncs.h"
21 #include "nodes/nodeFuncs.h"
22 #include "nodes/relation.h"
23 #include "utils/builtins.h"
24 #include "utils/lsyscache.h"
27 static bool expression_returns_set_walker(Node *node, void *context);
28 static int leftmostLoc(int loc1, int loc2);
33 * returns the Oid of the type of the expression's result.
36 exprType(const Node *expr)
43 switch (nodeTag(expr))
46 type = ((const Var *) expr)->vartype;
49 type = ((const Const *) expr)->consttype;
52 type = ((const Param *) expr)->paramtype;
55 type = ((const Aggref *) expr)->aggtype;
58 type = ((const WindowFunc *) expr)->wintype;
62 const ArrayRef *arrayref = (const ArrayRef *) expr;
64 /* slice and/or store operations yield the array type */
65 if (arrayref->reflowerindexpr || arrayref->refassgnexpr)
66 type = arrayref->refarraytype;
68 type = arrayref->refelemtype;
72 type = ((const FuncExpr *) expr)->funcresulttype;
75 type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
78 type = ((const OpExpr *) expr)->opresulttype;
81 type = ((const DistinctExpr *) expr)->opresulttype;
84 type = ((const NullIfExpr *) expr)->opresulttype;
86 case T_ScalarArrayOpExpr:
94 const SubLink *sublink = (const SubLink *) expr;
96 if (sublink->subLinkType == EXPR_SUBLINK ||
97 sublink->subLinkType == ARRAY_SUBLINK)
99 /* get the type of the subselect's first target column */
100 Query *qtree = (Query *) sublink->subselect;
103 if (!qtree || !IsA(qtree, Query))
104 elog(ERROR, "cannot get type for untransformed sublink");
105 tent = (TargetEntry *) linitial(qtree->targetList);
106 Assert(IsA(tent, TargetEntry));
107 Assert(!tent->resjunk);
108 type = exprType((Node *) tent->expr);
109 if (sublink->subLinkType == ARRAY_SUBLINK)
111 type = get_array_type(type);
112 if (!OidIsValid(type))
114 (errcode(ERRCODE_UNDEFINED_OBJECT),
115 errmsg("could not find array type for data type %s",
116 format_type_be(exprType((Node *) tent->expr)))));
121 /* for all other sublink types, result is boolean */
128 const SubPlan *subplan = (const SubPlan *) expr;
130 if (subplan->subLinkType == EXPR_SUBLINK ||
131 subplan->subLinkType == ARRAY_SUBLINK)
133 /* get the type of the subselect's first target column */
134 type = subplan->firstColType;
135 if (subplan->subLinkType == ARRAY_SUBLINK)
137 type = get_array_type(type);
138 if (!OidIsValid(type))
140 (errcode(ERRCODE_UNDEFINED_OBJECT),
141 errmsg("could not find array type for data type %s",
142 format_type_be(subplan->firstColType))));
147 /* for all other subplan types, result is boolean */
152 case T_AlternativeSubPlan:
154 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
156 /* subplans should all return the same thing */
157 type = exprType((Node *) linitial(asplan->subplans));
161 type = ((const FieldSelect *) expr)->resulttype;
164 type = ((const FieldStore *) expr)->resulttype;
167 type = ((const RelabelType *) expr)->resulttype;
170 type = ((const CoerceViaIO *) expr)->resulttype;
172 case T_ArrayCoerceExpr:
173 type = ((const ArrayCoerceExpr *) expr)->resulttype;
175 case T_ConvertRowtypeExpr:
176 type = ((const ConvertRowtypeExpr *) expr)->resulttype;
179 type = exprType((Node *) ((const CollateExpr *) expr)->arg);
182 type = ((const CaseExpr *) expr)->casetype;
185 type = ((const CaseTestExpr *) expr)->typeId;
188 type = ((const ArrayExpr *) expr)->array_typeid;
191 type = ((const RowExpr *) expr)->row_typeid;
193 case T_RowCompareExpr:
197 type = ((const CoalesceExpr *) expr)->coalescetype;
200 type = ((const MinMaxExpr *) expr)->minmaxtype;
203 if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
205 else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
216 case T_CoerceToDomain:
217 type = ((const CoerceToDomain *) expr)->resulttype;
219 case T_CoerceToDomainValue:
220 type = ((const CoerceToDomainValue *) expr)->typeId;
223 type = ((const SetToDefault *) expr)->typeId;
225 case T_CurrentOfExpr:
228 case T_PlaceHolderVar:
229 type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
232 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
233 type = InvalidOid; /* keep compiler quiet */
241 * returns the type-specific modifier of the expression's result type,
242 * if it can be determined. In many cases, it can't and we return -1.
245 exprTypmod(const Node *expr)
250 switch (nodeTag(expr))
253 return ((const Var *) expr)->vartypmod;
255 return ((const Const *) expr)->consttypmod;
257 return ((const Param *) expr)->paramtypmod;
259 /* typmod is the same for array or element */
260 return ((const ArrayRef *) expr)->reftypmod;
265 /* Be smart about length-coercion functions... */
266 if (exprIsLengthCoercion(expr, &coercedTypmod))
267 return coercedTypmod;
271 return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
275 * Result is either first argument or NULL, so we can report
276 * first argument's typmod if known.
278 const NullIfExpr *nexpr = (const NullIfExpr *) expr;
280 return exprTypmod((Node *) linitial(nexpr->args));
285 const SubLink *sublink = (const SubLink *) expr;
287 if (sublink->subLinkType == EXPR_SUBLINK ||
288 sublink->subLinkType == ARRAY_SUBLINK)
290 /* get the typmod of the subselect's first target column */
291 Query *qtree = (Query *) sublink->subselect;
294 if (!qtree || !IsA(qtree, Query))
295 elog(ERROR, "cannot get type for untransformed sublink");
296 tent = (TargetEntry *) linitial(qtree->targetList);
297 Assert(IsA(tent, TargetEntry));
298 Assert(!tent->resjunk);
299 return exprTypmod((Node *) tent->expr);
300 /* note we don't need to care if it's an array */
306 const SubPlan *subplan = (const SubPlan *) expr;
308 if (subplan->subLinkType == EXPR_SUBLINK ||
309 subplan->subLinkType == ARRAY_SUBLINK)
311 /* get the typmod of the subselect's first target column */
312 /* note we don't need to care if it's an array */
313 return subplan->firstColTypmod;
317 /* for all other subplan types, result is boolean */
322 case T_AlternativeSubPlan:
324 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
326 /* subplans should all return the same thing */
327 return exprTypmod((Node *) linitial(asplan->subplans));
331 return ((const FieldSelect *) expr)->resulttypmod;
333 return ((const RelabelType *) expr)->resulttypmod;
334 case T_ArrayCoerceExpr:
335 return ((const ArrayCoerceExpr *) expr)->resulttypmod;
337 return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
341 * If all the alternatives agree on type/typmod, return that
342 * typmod, else use -1
344 const CaseExpr *cexpr = (const CaseExpr *) expr;
345 Oid casetype = cexpr->casetype;
349 if (!cexpr->defresult)
351 if (exprType((Node *) cexpr->defresult) != casetype)
353 typmod = exprTypmod((Node *) cexpr->defresult);
355 return -1; /* no point in trying harder */
356 foreach(arg, cexpr->args)
358 CaseWhen *w = (CaseWhen *) lfirst(arg);
360 Assert(IsA(w, CaseWhen));
361 if (exprType((Node *) w->result) != casetype)
363 if (exprTypmod((Node *) w->result) != typmod)
370 return ((const CaseTestExpr *) expr)->typeMod;
374 * If all the elements agree on type/typmod, return that
375 * typmod, else use -1
377 const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
382 if (arrayexpr->elements == NIL)
384 typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
386 return -1; /* no point in trying harder */
387 if (arrayexpr->multidims)
388 commontype = arrayexpr->array_typeid;
390 commontype = arrayexpr->element_typeid;
391 foreach(elem, arrayexpr->elements)
393 Node *e = (Node *) lfirst(elem);
395 if (exprType(e) != commontype)
397 if (exprTypmod(e) != typmod)
406 * If all the alternatives agree on type/typmod, return that
407 * typmod, else use -1
409 const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
410 Oid coalescetype = cexpr->coalescetype;
414 if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
416 typmod = exprTypmod((Node *) linitial(cexpr->args));
418 return -1; /* no point in trying harder */
419 for_each_cell(arg, lnext(list_head(cexpr->args)))
421 Node *e = (Node *) lfirst(arg);
423 if (exprType(e) != coalescetype)
425 if (exprTypmod(e) != typmod)
434 * If all the alternatives agree on type/typmod, return that
435 * typmod, else use -1
437 const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
438 Oid minmaxtype = mexpr->minmaxtype;
442 if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
444 typmod = exprTypmod((Node *) linitial(mexpr->args));
446 return -1; /* no point in trying harder */
447 for_each_cell(arg, lnext(list_head(mexpr->args)))
449 Node *e = (Node *) lfirst(arg);
451 if (exprType(e) != minmaxtype)
453 if (exprTypmod(e) != typmod)
459 case T_CoerceToDomain:
460 return ((const CoerceToDomain *) expr)->resulttypmod;
461 case T_CoerceToDomainValue:
462 return ((const CoerceToDomainValue *) expr)->typeMod;
464 return ((const SetToDefault *) expr)->typeMod;
465 case T_PlaceHolderVar:
466 return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
474 * exprIsLengthCoercion
475 * Detect whether an expression tree is an application of a datatype's
476 * typmod-coercion function. Optionally extract the result's typmod.
478 * If coercedTypmod is not NULL, the typmod is stored there if the expression
479 * is a length-coercion function, else -1 is stored there.
481 * Note that a combined type-and-length coercion will be treated as a
482 * length coercion by this routine.
485 exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
487 if (coercedTypmod != NULL)
488 *coercedTypmod = -1; /* default result on failure */
491 * Scalar-type length coercions are FuncExprs, array-type length coercions
492 * are ArrayCoerceExprs
494 if (expr && IsA(expr, FuncExpr))
496 const FuncExpr *func = (const FuncExpr *) expr;
501 * If it didn't come from a coercion context, reject.
503 if (func->funcformat != COERCE_EXPLICIT_CAST &&
504 func->funcformat != COERCE_IMPLICIT_CAST)
508 * If it's not a two-argument or three-argument function with the
509 * second argument being an int4 constant, it can't have been created
510 * from a length coercion (it must be a type coercion, instead).
512 nargs = list_length(func->args);
513 if (nargs < 2 || nargs > 3)
516 second_arg = (Const *) lsecond(func->args);
517 if (!IsA(second_arg, Const) ||
518 second_arg->consttype != INT4OID ||
519 second_arg->constisnull)
523 * OK, it is indeed a length-coercion function.
525 if (coercedTypmod != NULL)
526 *coercedTypmod = DatumGetInt32(second_arg->constvalue);
531 if (expr && IsA(expr, ArrayCoerceExpr))
533 const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
535 /* It's not a length coercion unless there's a nondefault typmod */
536 if (acoerce->resulttypmod < 0)
540 * OK, it is indeed a length-coercion expression.
542 if (coercedTypmod != NULL)
543 *coercedTypmod = acoerce->resulttypmod;
553 * Add a RelabelType node that changes just the typmod of the expression.
555 * This is primarily intended to be used during planning. Therefore, it
556 * strips any existing RelabelType nodes to maintain the planner's invariant
557 * that there are not adjacent RelabelTypes, and it uses COERCE_DONTCARE
558 * which would typically be inappropriate earlier.
561 relabel_to_typmod(Node *expr, int32 typmod)
563 Oid type = exprType(expr);
564 Oid coll = exprCollation(expr);
566 /* Strip any existing RelabelType node(s) */
567 while (expr && IsA(expr, RelabelType))
568 expr = (Node *) ((RelabelType *) expr)->arg;
570 /* Apply new typmod, preserving the previous exposed type and collation */
571 return (Node *) makeRelabelType((Expr *) expr, type, typmod, coll,
576 * expression_returns_set
577 * Test whether an expression returns a set result.
579 * Because we use expression_tree_walker(), this can also be applied to
580 * whole targetlists; it'll produce TRUE if any one of the tlist items
584 expression_returns_set(Node *clause)
586 return expression_returns_set_walker(clause, NULL);
590 expression_returns_set_walker(Node *node, void *context)
594 if (IsA(node, FuncExpr))
596 FuncExpr *expr = (FuncExpr *) node;
598 if (expr->funcretset)
600 /* else fall through to check args */
602 if (IsA(node, OpExpr))
604 OpExpr *expr = (OpExpr *) node;
608 /* else fall through to check args */
611 /* Avoid recursion for some cases that can't return a set */
612 if (IsA(node, Aggref))
614 if (IsA(node, WindowFunc))
616 if (IsA(node, DistinctExpr))
618 if (IsA(node, NullIfExpr))
620 if (IsA(node, ScalarArrayOpExpr))
622 if (IsA(node, BoolExpr))
624 if (IsA(node, SubLink))
626 if (IsA(node, SubPlan))
628 if (IsA(node, AlternativeSubPlan))
630 if (IsA(node, ArrayExpr))
632 if (IsA(node, RowExpr))
634 if (IsA(node, RowCompareExpr))
636 if (IsA(node, CoalesceExpr))
638 if (IsA(node, MinMaxExpr))
640 if (IsA(node, XmlExpr))
643 return expression_tree_walker(node, expression_returns_set_walker,
650 * returns the Oid of the collation of the expression's result.
652 * Note: expression nodes that can invoke functions generally have an
653 * "inputcollid" field, which is what the function should use as collation.
654 * That is the resolved common collation of the node's inputs. It is often
655 * but not always the same as the result collation; in particular, if the
656 * function produces a non-collatable result type from collatable inputs
657 * or vice versa, the two are different.
660 exprCollation(const Node *expr)
667 switch (nodeTag(expr))
670 coll = ((const Var *) expr)->varcollid;
673 coll = ((const Const *) expr)->constcollid;
676 coll = ((const Param *) expr)->paramcollid;
679 coll = ((const Aggref *) expr)->aggcollid;
682 coll = ((const WindowFunc *) expr)->wincollid;
685 coll = ((const ArrayRef *) expr)->refcollid;
688 coll = ((const FuncExpr *) expr)->funccollid;
691 coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
694 coll = ((const OpExpr *) expr)->opcollid;
697 coll = ((const DistinctExpr *) expr)->opcollid;
700 coll = ((const NullIfExpr *) expr)->opcollid;
702 case T_ScalarArrayOpExpr:
703 coll = InvalidOid; /* result is always boolean */
706 coll = InvalidOid; /* result is always boolean */
710 const SubLink *sublink = (const SubLink *) expr;
712 if (sublink->subLinkType == EXPR_SUBLINK ||
713 sublink->subLinkType == ARRAY_SUBLINK)
715 /* get the collation of subselect's first target column */
716 Query *qtree = (Query *) sublink->subselect;
719 if (!qtree || !IsA(qtree, Query))
720 elog(ERROR, "cannot get collation for untransformed sublink");
721 tent = (TargetEntry *) linitial(qtree->targetList);
722 Assert(IsA(tent, TargetEntry));
723 Assert(!tent->resjunk);
724 coll = exprCollation((Node *) tent->expr);
725 /* collation doesn't change if it's converted to array */
729 /* for all other sublink types, result is boolean */
736 const SubPlan *subplan = (const SubPlan *) expr;
738 if (subplan->subLinkType == EXPR_SUBLINK ||
739 subplan->subLinkType == ARRAY_SUBLINK)
741 /* get the collation of subselect's first target column */
742 coll = subplan->firstColCollation;
743 /* collation doesn't change if it's converted to array */
747 /* for all other subplan types, result is boolean */
752 case T_AlternativeSubPlan:
754 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
756 /* subplans should all return the same thing */
757 coll = exprCollation((Node *) linitial(asplan->subplans));
761 coll = ((const FieldSelect *) expr)->resultcollid;
764 coll = InvalidOid; /* result is always composite */
767 coll = ((const RelabelType *) expr)->resultcollid;
770 coll = ((const CoerceViaIO *) expr)->resultcollid;
772 case T_ArrayCoerceExpr:
773 coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
775 case T_ConvertRowtypeExpr:
776 coll = InvalidOid; /* result is always composite */
779 coll = ((const CollateExpr *) expr)->collOid;
782 coll = ((const CaseExpr *) expr)->casecollid;
785 coll = ((const CaseTestExpr *) expr)->collation;
788 coll = ((const ArrayExpr *) expr)->array_collid;
791 coll = InvalidOid; /* result is always composite */
793 case T_RowCompareExpr:
794 coll = InvalidOid; /* result is always boolean */
797 coll = ((const CoalesceExpr *) expr)->coalescecollid;
800 coll = ((const MinMaxExpr *) expr)->minmaxcollid;
805 * XMLSERIALIZE returns text from non-collatable inputs, so its
806 * collation is always default. The other cases return boolean or
807 * XML, which are non-collatable.
809 if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
810 coll = DEFAULT_COLLATION_OID;
815 coll = InvalidOid; /* result is always boolean */
818 coll = InvalidOid; /* result is always boolean */
820 case T_CoerceToDomain:
821 coll = ((const CoerceToDomain *) expr)->resultcollid;
823 case T_CoerceToDomainValue:
824 coll = ((const CoerceToDomainValue *) expr)->collation;
827 coll = ((const SetToDefault *) expr)->collation;
829 case T_CurrentOfExpr:
830 coll = InvalidOid; /* result is always boolean */
832 case T_PlaceHolderVar:
833 coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
836 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
837 coll = InvalidOid; /* keep compiler quiet */
844 * exprInputCollation -
845 * returns the Oid of the collation a function should use, if available.
847 * Result is InvalidOid if the node type doesn't store this information.
850 exprInputCollation(const Node *expr)
857 switch (nodeTag(expr))
860 coll = ((const Aggref *) expr)->inputcollid;
863 coll = ((const WindowFunc *) expr)->inputcollid;
866 coll = ((const FuncExpr *) expr)->inputcollid;
869 coll = ((const OpExpr *) expr)->inputcollid;
872 coll = ((const DistinctExpr *) expr)->inputcollid;
875 coll = ((const NullIfExpr *) expr)->inputcollid;
877 case T_ScalarArrayOpExpr:
878 coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
881 coll = ((const MinMaxExpr *) expr)->inputcollid;
892 * Assign collation information to an expression tree node.
894 * Note: since this is only used during parse analysis, we don't need to
895 * worry about subplans or PlaceHolderVars.
898 exprSetCollation(Node *expr, Oid collation)
900 switch (nodeTag(expr))
903 ((Var *) expr)->varcollid = collation;
906 ((Const *) expr)->constcollid = collation;
909 ((Param *) expr)->paramcollid = collation;
912 ((Aggref *) expr)->aggcollid = collation;
915 ((WindowFunc *) expr)->wincollid = collation;
918 ((ArrayRef *) expr)->refcollid = collation;
921 ((FuncExpr *) expr)->funccollid = collation;
924 Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
927 ((OpExpr *) expr)->opcollid = collation;
930 ((DistinctExpr *) expr)->opcollid = collation;
933 ((NullIfExpr *) expr)->opcollid = collation;
935 case T_ScalarArrayOpExpr:
936 Assert(!OidIsValid(collation)); /* result is always boolean */
939 Assert(!OidIsValid(collation)); /* result is always boolean */
942 #ifdef USE_ASSERT_CHECKING
944 SubLink *sublink = (SubLink *) expr;
946 if (sublink->subLinkType == EXPR_SUBLINK ||
947 sublink->subLinkType == ARRAY_SUBLINK)
949 /* get the collation of subselect's first target column */
950 Query *qtree = (Query *) sublink->subselect;
953 if (!qtree || !IsA(qtree, Query))
954 elog(ERROR, "cannot set collation for untransformed sublink");
955 tent = (TargetEntry *) linitial(qtree->targetList);
956 Assert(IsA(tent, TargetEntry));
957 Assert(!tent->resjunk);
958 Assert(collation == exprCollation((Node *) tent->expr));
962 /* for all other sublink types, result is boolean */
963 Assert(!OidIsValid(collation));
966 #endif /* USE_ASSERT_CHECKING */
969 ((FieldSelect *) expr)->resultcollid = collation;
972 Assert(!OidIsValid(collation)); /* result is always composite */
975 ((RelabelType *) expr)->resultcollid = collation;
978 ((CoerceViaIO *) expr)->resultcollid = collation;
980 case T_ArrayCoerceExpr:
981 ((ArrayCoerceExpr *) expr)->resultcollid = collation;
983 case T_ConvertRowtypeExpr:
984 Assert(!OidIsValid(collation)); /* result is always composite */
987 ((CaseExpr *) expr)->casecollid = collation;
990 ((ArrayExpr *) expr)->array_collid = collation;
993 Assert(!OidIsValid(collation)); /* result is always composite */
995 case T_RowCompareExpr:
996 Assert(!OidIsValid(collation)); /* result is always boolean */
999 ((CoalesceExpr *) expr)->coalescecollid = collation;
1002 ((MinMaxExpr *) expr)->minmaxcollid = collation;
1005 Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
1006 (collation == DEFAULT_COLLATION_OID) :
1007 (collation == InvalidOid));
1010 Assert(!OidIsValid(collation)); /* result is always boolean */
1013 Assert(!OidIsValid(collation)); /* result is always boolean */
1015 case T_CoerceToDomain:
1016 ((CoerceToDomain *) expr)->resultcollid = collation;
1018 case T_CoerceToDomainValue:
1019 ((CoerceToDomainValue *) expr)->collation = collation;
1021 case T_SetToDefault:
1022 ((SetToDefault *) expr)->collation = collation;
1024 case T_CurrentOfExpr:
1025 Assert(!OidIsValid(collation)); /* result is always boolean */
1028 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
1034 * exprSetInputCollation -
1035 * Assign input-collation information to an expression tree node.
1037 * This is a no-op for node types that don't store their input collation.
1038 * Note we omit RowCompareExpr, which needs special treatment since it
1039 * contains multiple input collation OIDs.
1042 exprSetInputCollation(Node *expr, Oid inputcollation)
1044 switch (nodeTag(expr))
1047 ((Aggref *) expr)->inputcollid = inputcollation;
1050 ((WindowFunc *) expr)->inputcollid = inputcollation;
1053 ((FuncExpr *) expr)->inputcollid = inputcollation;
1056 ((OpExpr *) expr)->inputcollid = inputcollation;
1058 case T_DistinctExpr:
1059 ((DistinctExpr *) expr)->inputcollid = inputcollation;
1062 ((NullIfExpr *) expr)->inputcollid = inputcollation;
1064 case T_ScalarArrayOpExpr:
1065 ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
1068 ((MinMaxExpr *) expr)->inputcollid = inputcollation;
1078 * returns the parse location of an expression tree, for error reports
1080 * -1 is returned if the location can't be determined.
1082 * For expressions larger than a single token, the intent here is to
1083 * return the location of the expression's leftmost token, not necessarily
1084 * the topmost Node's location field. For example, an OpExpr's location
1085 * field will point at the operator name, but if it is not a prefix operator
1086 * then we should return the location of the left-hand operand instead.
1087 * The reason is that we want to reference the entire expression not just
1088 * that operator, and pointing to its start seems to be the most natural way.
1090 * The location is not perfect --- for example, since the grammar doesn't
1091 * explicitly represent parentheses in the parsetree, given something that
1092 * had been written "(a + b) * c" we are going to point at "a" not "(".
1093 * But it should be plenty good enough for error reporting purposes.
1095 * You might think that this code is overly general, for instance why check
1096 * the operands of a FuncExpr node, when the function name can be expected
1097 * to be to the left of them? There are a couple of reasons. The grammar
1098 * sometimes builds expressions that aren't quite what the user wrote;
1099 * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
1100 * pointer is to the right of its leftmost argument. Also, nodes that were
1101 * inserted implicitly by parse analysis (such as FuncExprs for implicit
1102 * coercions) will have location -1, and so we can have odd combinations of
1103 * known and unknown locations in a tree.
1106 exprLocation(const Node *expr)
1112 switch (nodeTag(expr))
1115 loc = ((const RangeVar *) expr)->location;
1118 loc = ((const Var *) expr)->location;
1121 loc = ((const Const *) expr)->location;
1124 loc = ((const Param *) expr)->location;
1127 /* function name should always be the first thing */
1128 loc = ((const Aggref *) expr)->location;
1131 /* function name should always be the first thing */
1132 loc = ((const WindowFunc *) expr)->location;
1135 /* just use array argument's location */
1136 loc = exprLocation((Node *) ((const ArrayRef *) expr)->refexpr);
1140 const FuncExpr *fexpr = (const FuncExpr *) expr;
1142 /* consider both function name and leftmost arg */
1143 loc = leftmostLoc(fexpr->location,
1144 exprLocation((Node *) fexpr->args));
1147 case T_NamedArgExpr:
1149 const NamedArgExpr *na = (const NamedArgExpr *) expr;
1151 /* consider both argument name and value */
1152 loc = leftmostLoc(na->location,
1153 exprLocation((Node *) na->arg));
1157 case T_DistinctExpr: /* struct-equivalent to OpExpr */
1158 case T_NullIfExpr: /* struct-equivalent to OpExpr */
1160 const OpExpr *opexpr = (const OpExpr *) expr;
1162 /* consider both operator name and leftmost arg */
1163 loc = leftmostLoc(opexpr->location,
1164 exprLocation((Node *) opexpr->args));
1167 case T_ScalarArrayOpExpr:
1169 const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
1171 /* consider both operator name and leftmost arg */
1172 loc = leftmostLoc(saopexpr->location,
1173 exprLocation((Node *) saopexpr->args));
1178 const BoolExpr *bexpr = (const BoolExpr *) expr;
1181 * Same as above, to handle either NOT or AND/OR. We can't
1182 * special-case NOT because of the way that it's used for
1183 * things like IS NOT BETWEEN.
1185 loc = leftmostLoc(bexpr->location,
1186 exprLocation((Node *) bexpr->args));
1191 const SubLink *sublink = (const SubLink *) expr;
1193 /* check the testexpr, if any, and the operator/keyword */
1194 loc = leftmostLoc(exprLocation(sublink->testexpr),
1199 /* just use argument's location */
1200 loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
1203 /* just use argument's location */
1204 loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
1208 const RelabelType *rexpr = (const RelabelType *) expr;
1211 loc = leftmostLoc(rexpr->location,
1212 exprLocation((Node *) rexpr->arg));
1217 const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
1220 loc = leftmostLoc(cexpr->location,
1221 exprLocation((Node *) cexpr->arg));
1224 case T_ArrayCoerceExpr:
1226 const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
1229 loc = leftmostLoc(cexpr->location,
1230 exprLocation((Node *) cexpr->arg));
1233 case T_ConvertRowtypeExpr:
1235 const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
1238 loc = leftmostLoc(cexpr->location,
1239 exprLocation((Node *) cexpr->arg));
1243 /* just use argument's location */
1244 loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
1247 /* CASE keyword should always be the first thing */
1248 loc = ((const CaseExpr *) expr)->location;
1251 /* WHEN keyword should always be the first thing */
1252 loc = ((const CaseWhen *) expr)->location;
1255 /* the location points at ARRAY or [, which must be leftmost */
1256 loc = ((const ArrayExpr *) expr)->location;
1259 /* the location points at ROW or (, which must be leftmost */
1260 loc = ((const RowExpr *) expr)->location;
1262 case T_RowCompareExpr:
1263 /* just use leftmost argument's location */
1264 loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
1266 case T_CoalesceExpr:
1267 /* COALESCE keyword should always be the first thing */
1268 loc = ((const CoalesceExpr *) expr)->location;
1271 /* GREATEST/LEAST keyword should always be the first thing */
1272 loc = ((const MinMaxExpr *) expr)->location;
1276 const XmlExpr *xexpr = (const XmlExpr *) expr;
1278 /* consider both function name and leftmost arg */
1279 loc = leftmostLoc(xexpr->location,
1280 exprLocation((Node *) xexpr->args));
1284 /* just use argument's location */
1285 loc = exprLocation((Node *) ((const NullTest *) expr)->arg);
1288 /* just use argument's location */
1289 loc = exprLocation((Node *) ((const BooleanTest *) expr)->arg);
1291 case T_CoerceToDomain:
1293 const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
1296 loc = leftmostLoc(cexpr->location,
1297 exprLocation((Node *) cexpr->arg));
1300 case T_CoerceToDomainValue:
1301 loc = ((const CoerceToDomainValue *) expr)->location;
1303 case T_SetToDefault:
1304 loc = ((const SetToDefault *) expr)->location;
1307 /* just use argument's location */
1308 loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
1311 /* use the contained RangeVar's location --- close enough */
1312 loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
1316 /* report location of first list member that has a location */
1319 loc = -1; /* just to suppress compiler warning */
1320 foreach(lc, (const List *) expr)
1322 loc = exprLocation((Node *) lfirst(lc));
1330 const A_Expr *aexpr = (const A_Expr *) expr;
1332 /* use leftmost of operator or left operand (if any) */
1333 /* we assume right operand can't be to left of operator */
1334 loc = leftmostLoc(aexpr->location,
1335 exprLocation(aexpr->lexpr));
1339 loc = ((const ColumnRef *) expr)->location;
1342 loc = ((const ParamRef *) expr)->location;
1345 loc = ((const A_Const *) expr)->location;
1349 const FuncCall *fc = (const FuncCall *) expr;
1351 /* consider both function name and leftmost arg */
1352 /* (we assume any ORDER BY nodes must be to right of name) */
1353 loc = leftmostLoc(fc->location,
1354 exprLocation((Node *) fc->args));
1358 /* the location points at ARRAY or [, which must be leftmost */
1359 loc = ((const A_ArrayExpr *) expr)->location;
1362 /* we need not examine the contained expression (if any) */
1363 loc = ((const ResTarget *) expr)->location;
1367 const TypeCast *tc = (const TypeCast *) expr;
1370 * This could represent CAST(), ::, or TypeName 'literal', so
1371 * any of the components might be leftmost.
1373 loc = exprLocation(tc->arg);
1374 loc = leftmostLoc(loc, tc->typeName->location);
1375 loc = leftmostLoc(loc, tc->location);
1378 case T_CollateClause:
1379 /* just use argument's location */
1380 loc = exprLocation(((const CollateClause *) expr)->arg);
1383 /* just use argument's location (ignore operator, if any) */
1384 loc = exprLocation(((const SortBy *) expr)->node);
1387 loc = ((const WindowDef *) expr)->location;
1390 loc = ((const TypeName *) expr)->location;
1393 loc = ((const Constraint *) expr)->location;
1395 case T_XmlSerialize:
1396 /* XMLSERIALIZE keyword should always be the first thing */
1397 loc = ((const XmlSerialize *) expr)->location;
1400 loc = ((const WithClause *) expr)->location;
1402 case T_CommonTableExpr:
1403 loc = ((const CommonTableExpr *) expr)->location;
1405 case T_PlaceHolderVar:
1406 /* just use argument's location */
1407 loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
1410 /* for any other node type it's just unknown... */
1418 * leftmostLoc - support for exprLocation
1420 * Take the minimum of two parse location values, but ignore unknowns
1423 leftmostLoc(int loc1, int loc2)
1430 return Min(loc1, loc2);
1435 * Standard expression-tree walking support
1437 * We used to have near-duplicate code in many different routines that
1438 * understood how to recurse through an expression node tree. That was
1439 * a pain to maintain, and we frequently had bugs due to some particular
1440 * routine neglecting to support a particular node type. In most cases,
1441 * these routines only actually care about certain node types, and don't
1442 * care about other types except insofar as they have to recurse through
1443 * non-primitive node types. Therefore, we now provide generic tree-walking
1444 * logic to consolidate the redundant "boilerplate" code. There are
1445 * two versions: expression_tree_walker() and expression_tree_mutator().
1449 * expression_tree_walker() is designed to support routines that traverse
1450 * a tree in a read-only fashion (although it will also work for routines
1451 * that modify nodes in-place but never add/delete/replace nodes).
1452 * A walker routine should look like this:
1454 * bool my_walker (Node *node, my_struct *context)
1458 * // check for nodes that special work is required for, eg:
1459 * if (IsA(node, Var))
1461 * ... do special actions for Var nodes
1463 * else if (IsA(node, ...))
1465 * ... do special actions for other node types
1467 * // for any node type not specially processed, do:
1468 * return expression_tree_walker(node, my_walker, (void *) context);
1471 * The "context" argument points to a struct that holds whatever context
1472 * information the walker routine needs --- it can be used to return data
1473 * gathered by the walker, too. This argument is not touched by
1474 * expression_tree_walker, but it is passed down to recursive sub-invocations
1475 * of my_walker. The tree walk is started from a setup routine that
1476 * fills in the appropriate context struct, calls my_walker with the top-level
1477 * node of the tree, and then examines the results.
1479 * The walker routine should return "false" to continue the tree walk, or
1480 * "true" to abort the walk and immediately return "true" to the top-level
1481 * caller. This can be used to short-circuit the traversal if the walker
1482 * has found what it came for. "false" is returned to the top-level caller
1483 * iff no invocation of the walker returned "true".
1485 * The node types handled by expression_tree_walker include all those
1486 * normally found in target lists and qualifier clauses during the planning
1487 * stage. In particular, it handles List nodes since a cnf-ified qual clause
1488 * will have List structure at the top level, and it handles TargetEntry nodes
1489 * so that a scan of a target list can be handled without additional code.
1490 * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
1491 * handled, so that query jointrees and setOperation trees can be processed
1492 * without additional code.
1494 * expression_tree_walker will handle SubLink nodes by recursing normally
1495 * into the "testexpr" subtree (which is an expression belonging to the outer
1496 * plan). It will also call the walker on the sub-Query node; however, when
1497 * expression_tree_walker itself is called on a Query node, it does nothing
1498 * and returns "false". The net effect is that unless the walker does
1499 * something special at a Query node, sub-selects will not be visited during
1500 * an expression tree walk. This is exactly the behavior wanted in many cases
1501 * --- and for those walkers that do want to recurse into sub-selects, special
1502 * behavior is typically needed anyway at the entry to a sub-select (such as
1503 * incrementing a depth counter). A walker that wants to examine sub-selects
1504 * should include code along the lines of:
1506 * if (IsA(node, Query))
1508 * adjust context for subquery;
1509 * result = query_tree_walker((Query *) node, my_walker, context,
1510 * 0); // adjust flags as needed
1511 * restore context if needed;
1515 * query_tree_walker is a convenience routine (see below) that calls the
1516 * walker on all the expression subtrees of the given Query node.
1518 * expression_tree_walker will handle SubPlan nodes by recursing normally
1519 * into the "testexpr" and the "args" list (which are expressions belonging to
1520 * the outer plan). It will not touch the completed subplan, however. Since
1521 * there is no link to the original Query, it is not possible to recurse into
1522 * subselects of an already-planned expression tree. This is OK for current
1523 * uses, but may need to be revisited in future.
1527 expression_tree_walker(Node *node,
1534 * The walker has already visited the current node, and so we need only
1535 * recurse into any sub-nodes it has.
1537 * We assume that the walker is not interested in List nodes per se, so
1538 * when we expect a List we just recurse directly to self without
1539 * bothering to call the walker.
1544 /* Guard against stack overflow due to overly complex expressions */
1545 check_stack_depth();
1547 switch (nodeTag(node))
1552 case T_CoerceToDomainValue:
1553 case T_CaseTestExpr:
1554 case T_SetToDefault:
1555 case T_CurrentOfExpr:
1557 case T_SortGroupClause:
1558 /* primitive node types with no expression subnodes */
1562 Aggref *expr = (Aggref *) node;
1564 /* recurse directly on List */
1565 if (expression_tree_walker((Node *) expr->args,
1568 if (expression_tree_walker((Node *) expr->aggorder,
1571 if (expression_tree_walker((Node *) expr->aggdistinct,
1578 WindowFunc *expr = (WindowFunc *) node;
1580 /* recurse directly on List */
1581 if (expression_tree_walker((Node *) expr->args,
1588 ArrayRef *aref = (ArrayRef *) node;
1590 /* recurse directly for upper/lower array index lists */
1591 if (expression_tree_walker((Node *) aref->refupperindexpr,
1594 if (expression_tree_walker((Node *) aref->reflowerindexpr,
1597 /* walker must see the refexpr and refassgnexpr, however */
1598 if (walker(aref->refexpr, context))
1600 if (walker(aref->refassgnexpr, context))
1606 FuncExpr *expr = (FuncExpr *) node;
1608 if (expression_tree_walker((Node *) expr->args,
1613 case T_NamedArgExpr:
1614 return walker(((NamedArgExpr *) node)->arg, context);
1616 case T_DistinctExpr: /* struct-equivalent to OpExpr */
1617 case T_NullIfExpr: /* struct-equivalent to OpExpr */
1619 OpExpr *expr = (OpExpr *) node;
1621 if (expression_tree_walker((Node *) expr->args,
1626 case T_ScalarArrayOpExpr:
1628 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1630 if (expression_tree_walker((Node *) expr->args,
1637 BoolExpr *expr = (BoolExpr *) node;
1639 if (expression_tree_walker((Node *) expr->args,
1646 SubLink *sublink = (SubLink *) node;
1648 if (walker(sublink->testexpr, context))
1652 * Also invoke the walker on the sublink's Query node, so it
1653 * can recurse into the sub-query if it wants to.
1655 return walker(sublink->subselect, context);
1660 SubPlan *subplan = (SubPlan *) node;
1662 /* recurse into the testexpr, but not into the Plan */
1663 if (walker(subplan->testexpr, context))
1665 /* also examine args list */
1666 if (expression_tree_walker((Node *) subplan->args,
1671 case T_AlternativeSubPlan:
1672 return walker(((AlternativeSubPlan *) node)->subplans, context);
1674 return walker(((FieldSelect *) node)->arg, context);
1677 FieldStore *fstore = (FieldStore *) node;
1679 if (walker(fstore->arg, context))
1681 if (walker(fstore->newvals, context))
1686 return walker(((RelabelType *) node)->arg, context);
1688 return walker(((CoerceViaIO *) node)->arg, context);
1689 case T_ArrayCoerceExpr:
1690 return walker(((ArrayCoerceExpr *) node)->arg, context);
1691 case T_ConvertRowtypeExpr:
1692 return walker(((ConvertRowtypeExpr *) node)->arg, context);
1694 return walker(((CollateExpr *) node)->arg, context);
1697 CaseExpr *caseexpr = (CaseExpr *) node;
1699 if (walker(caseexpr->arg, context))
1701 /* we assume walker doesn't care about CaseWhens, either */
1702 foreach(temp, caseexpr->args)
1704 CaseWhen *when = (CaseWhen *) lfirst(temp);
1706 Assert(IsA(when, CaseWhen));
1707 if (walker(when->expr, context))
1709 if (walker(when->result, context))
1712 if (walker(caseexpr->defresult, context))
1717 return walker(((ArrayExpr *) node)->elements, context);
1719 /* Assume colnames isn't interesting */
1720 return walker(((RowExpr *) node)->args, context);
1721 case T_RowCompareExpr:
1723 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1725 if (walker(rcexpr->largs, context))
1727 if (walker(rcexpr->rargs, context))
1731 case T_CoalesceExpr:
1732 return walker(((CoalesceExpr *) node)->args, context);
1734 return walker(((MinMaxExpr *) node)->args, context);
1737 XmlExpr *xexpr = (XmlExpr *) node;
1739 if (walker(xexpr->named_args, context))
1741 /* we assume walker doesn't care about arg_names */
1742 if (walker(xexpr->args, context))
1747 return walker(((NullTest *) node)->arg, context);
1749 return walker(((BooleanTest *) node)->arg, context);
1750 case T_CoerceToDomain:
1751 return walker(((CoerceToDomain *) node)->arg, context);
1753 return walker(((TargetEntry *) node)->expr, context);
1755 /* Do nothing with a sub-Query, per discussion above */
1757 case T_WindowClause:
1759 WindowClause *wc = (WindowClause *) node;
1761 if (walker(wc->partitionClause, context))
1763 if (walker(wc->orderClause, context))
1765 if (walker(wc->startOffset, context))
1767 if (walker(wc->endOffset, context))
1771 case T_CommonTableExpr:
1773 CommonTableExpr *cte = (CommonTableExpr *) node;
1776 * Invoke the walker on the CTE's Query node, so it can
1777 * recurse into the sub-query if it wants to.
1779 return walker(cte->ctequery, context);
1783 foreach(temp, (List *) node)
1785 if (walker((Node *) lfirst(temp), context))
1791 FromExpr *from = (FromExpr *) node;
1793 if (walker(from->fromlist, context))
1795 if (walker(from->quals, context))
1801 JoinExpr *join = (JoinExpr *) node;
1803 if (walker(join->larg, context))
1805 if (walker(join->rarg, context))
1807 if (walker(join->quals, context))
1811 * alias clause, using list are deemed uninteresting.
1815 case T_SetOperationStmt:
1817 SetOperationStmt *setop = (SetOperationStmt *) node;
1819 if (walker(setop->larg, context))
1821 if (walker(setop->rarg, context))
1824 /* groupClauses are deemed uninteresting */
1827 case T_PlaceHolderVar:
1828 return walker(((PlaceHolderVar *) node)->phexpr, context);
1829 case T_AppendRelInfo:
1831 AppendRelInfo *appinfo = (AppendRelInfo *) node;
1833 if (expression_tree_walker((Node *) appinfo->translated_vars,
1838 case T_PlaceHolderInfo:
1839 return walker(((PlaceHolderInfo *) node)->ph_var, context);
1841 elog(ERROR, "unrecognized node type: %d",
1842 (int) nodeTag(node));
1849 * query_tree_walker --- initiate a walk of a Query's expressions
1851 * This routine exists just to reduce the number of places that need to know
1852 * where all the expression subtrees of a Query are. Note it can be used
1853 * for starting a walk at top level of a Query regardless of whether the
1854 * walker intends to descend into subqueries. It is also useful for
1855 * descending into subqueries within a walker.
1857 * Some callers want to suppress visitation of certain items in the sub-Query,
1858 * typically because they need to process them specially, or don't actually
1859 * want to recurse into subqueries. This is supported by the flags argument,
1860 * which is the bitwise OR of flag values to suppress visitation of
1861 * indicated items. (More flag bits may be added as needed.)
1864 query_tree_walker(Query *query,
1869 Assert(query != NULL && IsA(query, Query));
1871 if (walker((Node *) query->targetList, context))
1873 if (walker((Node *) query->returningList, context))
1875 if (walker((Node *) query->jointree, context))
1877 if (walker(query->setOperations, context))
1879 if (walker(query->havingQual, context))
1881 if (walker(query->limitOffset, context))
1883 if (walker(query->limitCount, context))
1885 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
1887 if (walker((Node *) query->cteList, context))
1890 if (!(flags & QTW_IGNORE_RANGE_TABLE))
1892 if (range_table_walker(query->rtable, walker, context, flags))
1899 * range_table_walker is just the part of query_tree_walker that scans
1900 * a query's rangetable. This is split out since it can be useful on
1904 range_table_walker(List *rtable,
1913 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
1915 /* For historical reasons, visiting RTEs is not the default */
1916 if (flags & QTW_EXAMINE_RTES)
1917 if (walker(rte, context))
1920 switch (rte->rtekind)
1927 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
1928 if (walker(rte->subquery, context))
1932 if (!(flags & QTW_IGNORE_JOINALIASES))
1933 if (walker(rte->joinaliasvars, context))
1937 if (walker(rte->funcexpr, context))
1941 if (walker(rte->values_lists, context))
1951 * expression_tree_mutator() is designed to support routines that make a
1952 * modified copy of an expression tree, with some nodes being added,
1953 * removed, or replaced by new subtrees. The original tree is (normally)
1954 * not changed. Each recursion level is responsible for returning a copy of
1955 * (or appropriately modified substitute for) the subtree it is handed.
1956 * A mutator routine should look like this:
1958 * Node * my_mutator (Node *node, my_struct *context)
1962 * // check for nodes that special work is required for, eg:
1963 * if (IsA(node, Var))
1965 * ... create and return modified copy of Var node
1967 * else if (IsA(node, ...))
1969 * ... do special transformations of other node types
1971 * // for any node type not specially processed, do:
1972 * return expression_tree_mutator(node, my_mutator, (void *) context);
1975 * The "context" argument points to a struct that holds whatever context
1976 * information the mutator routine needs --- it can be used to return extra
1977 * data gathered by the mutator, too. This argument is not touched by
1978 * expression_tree_mutator, but it is passed down to recursive sub-invocations
1979 * of my_mutator. The tree walk is started from a setup routine that
1980 * fills in the appropriate context struct, calls my_mutator with the
1981 * top-level node of the tree, and does any required post-processing.
1983 * Each level of recursion must return an appropriately modified Node.
1984 * If expression_tree_mutator() is called, it will make an exact copy
1985 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
1986 * of that Node. In this way, my_mutator() has full control over the
1987 * copying process but need not directly deal with expression trees
1988 * that it has no interest in.
1990 * Just as for expression_tree_walker, the node types handled by
1991 * expression_tree_mutator include all those normally found in target lists
1992 * and qualifier clauses during the planning stage.
1994 * expression_tree_mutator will handle SubLink nodes by recursing normally
1995 * into the "testexpr" subtree (which is an expression belonging to the outer
1996 * plan). It will also call the mutator on the sub-Query node; however, when
1997 * expression_tree_mutator itself is called on a Query node, it does nothing
1998 * and returns the unmodified Query node. The net effect is that unless the
1999 * mutator does something special at a Query node, sub-selects will not be
2000 * visited or modified; the original sub-select will be linked to by the new
2001 * SubLink node. Mutators that want to descend into sub-selects will usually
2002 * do so by recognizing Query nodes and calling query_tree_mutator (below).
2004 * expression_tree_mutator will handle a SubPlan node by recursing into the
2005 * "testexpr" and the "args" list (which belong to the outer plan), but it
2006 * will simply copy the link to the inner plan, since that's typically what
2007 * expression tree mutators want. A mutator that wants to modify the subplan
2008 * can force appropriate behavior by recognizing SubPlan expression nodes
2009 * and doing the right thing.
2013 expression_tree_mutator(Node *node,
2014 Node *(*mutator) (),
2018 * The mutator has already decided not to modify the current node, but we
2019 * must call the mutator for any sub-nodes.
2022 #define FLATCOPY(newnode, node, nodetype) \
2023 ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2024 memcpy((newnode), (node), sizeof(nodetype)) )
2026 #define CHECKFLATCOPY(newnode, node, nodetype) \
2027 ( AssertMacro(IsA((node), nodetype)), \
2028 (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
2029 memcpy((newnode), (node), sizeof(nodetype)) )
2031 #define MUTATE(newfield, oldfield, fieldtype) \
2032 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
2037 /* Guard against stack overflow due to overly complex expressions */
2038 check_stack_depth();
2040 switch (nodeTag(node))
2043 * Primitive node types with no expression subnodes. Var and
2044 * Const are frequent enough to deserve special cases, the others
2045 * we just use copyObject for.
2049 Var *var = (Var *) node;
2052 FLATCOPY(newnode, var, Var);
2053 return (Node *) newnode;
2058 Const *oldnode = (Const *) node;
2061 FLATCOPY(newnode, oldnode, Const);
2062 /* XXX we don't bother with datumCopy; should we? */
2063 return (Node *) newnode;
2067 case T_CoerceToDomainValue:
2068 case T_CaseTestExpr:
2069 case T_SetToDefault:
2070 case T_CurrentOfExpr:
2072 case T_SortGroupClause:
2073 return (Node *) copyObject(node);
2076 Aggref *aggref = (Aggref *) node;
2079 FLATCOPY(newnode, aggref, Aggref);
2080 MUTATE(newnode->args, aggref->args, List *);
2081 MUTATE(newnode->aggorder, aggref->aggorder, List *);
2082 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
2083 return (Node *) newnode;
2088 WindowFunc *wfunc = (WindowFunc *) node;
2089 WindowFunc *newnode;
2091 FLATCOPY(newnode, wfunc, WindowFunc);
2092 MUTATE(newnode->args, wfunc->args, List *);
2093 return (Node *) newnode;
2098 ArrayRef *arrayref = (ArrayRef *) node;
2101 FLATCOPY(newnode, arrayref, ArrayRef);
2102 MUTATE(newnode->refupperindexpr, arrayref->refupperindexpr,
2104 MUTATE(newnode->reflowerindexpr, arrayref->reflowerindexpr,
2106 MUTATE(newnode->refexpr, arrayref->refexpr,
2108 MUTATE(newnode->refassgnexpr, arrayref->refassgnexpr,
2110 return (Node *) newnode;
2115 FuncExpr *expr = (FuncExpr *) node;
2118 FLATCOPY(newnode, expr, FuncExpr);
2119 MUTATE(newnode->args, expr->args, List *);
2120 return (Node *) newnode;
2123 case T_NamedArgExpr:
2125 NamedArgExpr *nexpr = (NamedArgExpr *) node;
2126 NamedArgExpr *newnode;
2128 FLATCOPY(newnode, nexpr, NamedArgExpr);
2129 MUTATE(newnode->arg, nexpr->arg, Expr *);
2130 return (Node *) newnode;
2135 OpExpr *expr = (OpExpr *) node;
2138 FLATCOPY(newnode, expr, OpExpr);
2139 MUTATE(newnode->args, expr->args, List *);
2140 return (Node *) newnode;
2143 case T_DistinctExpr:
2145 DistinctExpr *expr = (DistinctExpr *) node;
2146 DistinctExpr *newnode;
2148 FLATCOPY(newnode, expr, DistinctExpr);
2149 MUTATE(newnode->args, expr->args, List *);
2150 return (Node *) newnode;
2155 NullIfExpr *expr = (NullIfExpr *) node;
2156 NullIfExpr *newnode;
2158 FLATCOPY(newnode, expr, NullIfExpr);
2159 MUTATE(newnode->args, expr->args, List *);
2160 return (Node *) newnode;
2163 case T_ScalarArrayOpExpr:
2165 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
2166 ScalarArrayOpExpr *newnode;
2168 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
2169 MUTATE(newnode->args, expr->args, List *);
2170 return (Node *) newnode;
2175 BoolExpr *expr = (BoolExpr *) node;
2178 FLATCOPY(newnode, expr, BoolExpr);
2179 MUTATE(newnode->args, expr->args, List *);
2180 return (Node *) newnode;
2185 SubLink *sublink = (SubLink *) node;
2188 FLATCOPY(newnode, sublink, SubLink);
2189 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
2192 * Also invoke the mutator on the sublink's Query node, so it
2193 * can recurse into the sub-query if it wants to.
2195 MUTATE(newnode->subselect, sublink->subselect, Node *);
2196 return (Node *) newnode;
2201 SubPlan *subplan = (SubPlan *) node;
2204 FLATCOPY(newnode, subplan, SubPlan);
2205 /* transform testexpr */
2206 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
2207 /* transform args list (params to be passed to subplan) */
2208 MUTATE(newnode->args, subplan->args, List *);
2209 /* but not the sub-Plan itself, which is referenced as-is */
2210 return (Node *) newnode;
2213 case T_AlternativeSubPlan:
2215 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
2216 AlternativeSubPlan *newnode;
2218 FLATCOPY(newnode, asplan, AlternativeSubPlan);
2219 MUTATE(newnode->subplans, asplan->subplans, List *);
2220 return (Node *) newnode;
2225 FieldSelect *fselect = (FieldSelect *) node;
2226 FieldSelect *newnode;
2228 FLATCOPY(newnode, fselect, FieldSelect);
2229 MUTATE(newnode->arg, fselect->arg, Expr *);
2230 return (Node *) newnode;
2235 FieldStore *fstore = (FieldStore *) node;
2236 FieldStore *newnode;
2238 FLATCOPY(newnode, fstore, FieldStore);
2239 MUTATE(newnode->arg, fstore->arg, Expr *);
2240 MUTATE(newnode->newvals, fstore->newvals, List *);
2241 newnode->fieldnums = list_copy(fstore->fieldnums);
2242 return (Node *) newnode;
2247 RelabelType *relabel = (RelabelType *) node;
2248 RelabelType *newnode;
2250 FLATCOPY(newnode, relabel, RelabelType);
2251 MUTATE(newnode->arg, relabel->arg, Expr *);
2252 return (Node *) newnode;
2257 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
2258 CoerceViaIO *newnode;
2260 FLATCOPY(newnode, iocoerce, CoerceViaIO);
2261 MUTATE(newnode->arg, iocoerce->arg, Expr *);
2262 return (Node *) newnode;
2265 case T_ArrayCoerceExpr:
2267 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2268 ArrayCoerceExpr *newnode;
2270 FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
2271 MUTATE(newnode->arg, acoerce->arg, Expr *);
2272 return (Node *) newnode;
2275 case T_ConvertRowtypeExpr:
2277 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
2278 ConvertRowtypeExpr *newnode;
2280 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
2281 MUTATE(newnode->arg, convexpr->arg, Expr *);
2282 return (Node *) newnode;
2287 CollateExpr *collate = (CollateExpr *) node;
2288 CollateExpr *newnode;
2290 FLATCOPY(newnode, collate, CollateExpr);
2291 MUTATE(newnode->arg, collate->arg, Expr *);
2292 return (Node *) newnode;
2297 CaseExpr *caseexpr = (CaseExpr *) node;
2300 FLATCOPY(newnode, caseexpr, CaseExpr);
2301 MUTATE(newnode->arg, caseexpr->arg, Expr *);
2302 MUTATE(newnode->args, caseexpr->args, List *);
2303 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
2304 return (Node *) newnode;
2309 CaseWhen *casewhen = (CaseWhen *) node;
2312 FLATCOPY(newnode, casewhen, CaseWhen);
2313 MUTATE(newnode->expr, casewhen->expr, Expr *);
2314 MUTATE(newnode->result, casewhen->result, Expr *);
2315 return (Node *) newnode;
2320 ArrayExpr *arrayexpr = (ArrayExpr *) node;
2323 FLATCOPY(newnode, arrayexpr, ArrayExpr);
2324 MUTATE(newnode->elements, arrayexpr->elements, List *);
2325 return (Node *) newnode;
2330 RowExpr *rowexpr = (RowExpr *) node;
2333 FLATCOPY(newnode, rowexpr, RowExpr);
2334 MUTATE(newnode->args, rowexpr->args, List *);
2335 /* Assume colnames needn't be duplicated */
2336 return (Node *) newnode;
2339 case T_RowCompareExpr:
2341 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2342 RowCompareExpr *newnode;
2344 FLATCOPY(newnode, rcexpr, RowCompareExpr);
2345 MUTATE(newnode->largs, rcexpr->largs, List *);
2346 MUTATE(newnode->rargs, rcexpr->rargs, List *);
2347 return (Node *) newnode;
2350 case T_CoalesceExpr:
2352 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2353 CoalesceExpr *newnode;
2355 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
2356 MUTATE(newnode->args, coalesceexpr->args, List *);
2357 return (Node *) newnode;
2362 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2363 MinMaxExpr *newnode;
2365 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
2366 MUTATE(newnode->args, minmaxexpr->args, List *);
2367 return (Node *) newnode;
2372 XmlExpr *xexpr = (XmlExpr *) node;
2375 FLATCOPY(newnode, xexpr, XmlExpr);
2376 MUTATE(newnode->named_args, xexpr->named_args, List *);
2377 /* assume mutator does not care about arg_names */
2378 MUTATE(newnode->args, xexpr->args, List *);
2379 return (Node *) newnode;
2384 NullTest *ntest = (NullTest *) node;
2387 FLATCOPY(newnode, ntest, NullTest);
2388 MUTATE(newnode->arg, ntest->arg, Expr *);
2389 return (Node *) newnode;
2394 BooleanTest *btest = (BooleanTest *) node;
2395 BooleanTest *newnode;
2397 FLATCOPY(newnode, btest, BooleanTest);
2398 MUTATE(newnode->arg, btest->arg, Expr *);
2399 return (Node *) newnode;
2402 case T_CoerceToDomain:
2404 CoerceToDomain *ctest = (CoerceToDomain *) node;
2405 CoerceToDomain *newnode;
2407 FLATCOPY(newnode, ctest, CoerceToDomain);
2408 MUTATE(newnode->arg, ctest->arg, Expr *);
2409 return (Node *) newnode;
2414 TargetEntry *targetentry = (TargetEntry *) node;
2415 TargetEntry *newnode;
2417 FLATCOPY(newnode, targetentry, TargetEntry);
2418 MUTATE(newnode->expr, targetentry->expr, Expr *);
2419 return (Node *) newnode;
2423 /* Do nothing with a sub-Query, per discussion above */
2425 case T_WindowClause:
2427 WindowClause *wc = (WindowClause *) node;
2428 WindowClause *newnode;
2430 FLATCOPY(newnode, wc, WindowClause);
2431 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
2432 MUTATE(newnode->orderClause, wc->orderClause, List *);
2433 MUTATE(newnode->startOffset, wc->startOffset, Node *);
2434 MUTATE(newnode->endOffset, wc->endOffset, Node *);
2435 return (Node *) newnode;
2438 case T_CommonTableExpr:
2440 CommonTableExpr *cte = (CommonTableExpr *) node;
2441 CommonTableExpr *newnode;
2443 FLATCOPY(newnode, cte, CommonTableExpr);
2446 * Also invoke the mutator on the CTE's Query node, so it can
2447 * recurse into the sub-query if it wants to.
2449 MUTATE(newnode->ctequery, cte->ctequery, Node *);
2450 return (Node *) newnode;
2456 * We assume the mutator isn't interested in the list nodes
2457 * per se, so just invoke it on each list element. NOTE: this
2458 * would fail badly on a list with integer elements!
2464 foreach(temp, (List *) node)
2466 resultlist = lappend(resultlist,
2467 mutator((Node *) lfirst(temp),
2470 return (Node *) resultlist;
2475 FromExpr *from = (FromExpr *) node;
2478 FLATCOPY(newnode, from, FromExpr);
2479 MUTATE(newnode->fromlist, from->fromlist, List *);
2480 MUTATE(newnode->quals, from->quals, Node *);
2481 return (Node *) newnode;
2486 JoinExpr *join = (JoinExpr *) node;
2489 FLATCOPY(newnode, join, JoinExpr);
2490 MUTATE(newnode->larg, join->larg, Node *);
2491 MUTATE(newnode->rarg, join->rarg, Node *);
2492 MUTATE(newnode->quals, join->quals, Node *);
2493 /* We do not mutate alias or using by default */
2494 return (Node *) newnode;
2497 case T_SetOperationStmt:
2499 SetOperationStmt *setop = (SetOperationStmt *) node;
2500 SetOperationStmt *newnode;
2502 FLATCOPY(newnode, setop, SetOperationStmt);
2503 MUTATE(newnode->larg, setop->larg, Node *);
2504 MUTATE(newnode->rarg, setop->rarg, Node *);
2505 /* We do not mutate groupClauses by default */
2506 return (Node *) newnode;
2509 case T_PlaceHolderVar:
2511 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2512 PlaceHolderVar *newnode;
2514 FLATCOPY(newnode, phv, PlaceHolderVar);
2515 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
2516 /* Assume we need not copy the relids bitmapset */
2517 return (Node *) newnode;
2520 case T_AppendRelInfo:
2522 AppendRelInfo *appinfo = (AppendRelInfo *) node;
2523 AppendRelInfo *newnode;
2525 FLATCOPY(newnode, appinfo, AppendRelInfo);
2526 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
2527 return (Node *) newnode;
2530 case T_PlaceHolderInfo:
2532 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
2533 PlaceHolderInfo *newnode;
2535 FLATCOPY(newnode, phinfo, PlaceHolderInfo);
2536 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
2537 /* Assume we need not copy the relids bitmapsets */
2538 return (Node *) newnode;
2542 elog(ERROR, "unrecognized node type: %d",
2543 (int) nodeTag(node));
2546 /* can't get here, but keep compiler happy */
2552 * query_tree_mutator --- initiate modification of a Query's expressions
2554 * This routine exists just to reduce the number of places that need to know
2555 * where all the expression subtrees of a Query are. Note it can be used
2556 * for starting a walk at top level of a Query regardless of whether the
2557 * mutator intends to descend into subqueries. It is also useful for
2558 * descending into subqueries within a mutator.
2560 * Some callers want to suppress mutating of certain items in the Query,
2561 * typically because they need to process them specially, or don't actually
2562 * want to recurse into subqueries. This is supported by the flags argument,
2563 * which is the bitwise OR of flag values to suppress mutating of
2564 * indicated items. (More flag bits may be added as needed.)
2566 * Normally the Query node itself is copied, but some callers want it to be
2567 * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags. All
2568 * modified substructure is safely copied in any case.
2571 query_tree_mutator(Query *query,
2572 Node *(*mutator) (),
2576 Assert(query != NULL && IsA(query, Query));
2578 if (!(flags & QTW_DONT_COPY_QUERY))
2582 FLATCOPY(newquery, query, Query);
2586 MUTATE(query->targetList, query->targetList, List *);
2587 MUTATE(query->returningList, query->returningList, List *);
2588 MUTATE(query->jointree, query->jointree, FromExpr *);
2589 MUTATE(query->setOperations, query->setOperations, Node *);
2590 MUTATE(query->havingQual, query->havingQual, Node *);
2591 MUTATE(query->limitOffset, query->limitOffset, Node *);
2592 MUTATE(query->limitCount, query->limitCount, Node *);
2593 if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
2594 MUTATE(query->cteList, query->cteList, List *);
2595 else /* else copy CTE list as-is */
2596 query->cteList = copyObject(query->cteList);
2597 query->rtable = range_table_mutator(query->rtable,
2598 mutator, context, flags);
2603 * range_table_mutator is just the part of query_tree_mutator that processes
2604 * a query's rangetable. This is split out since it can be useful on
2608 range_table_mutator(List *rtable,
2609 Node *(*mutator) (),
2618 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2619 RangeTblEntry *newrte;
2621 FLATCOPY(newrte, rte, RangeTblEntry);
2622 switch (rte->rtekind)
2626 /* we don't bother to copy eref, aliases, etc; OK? */
2629 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
2631 CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
2632 MUTATE(newrte->subquery, newrte->subquery, Query *);
2636 /* else, copy RT subqueries as-is */
2637 newrte->subquery = copyObject(rte->subquery);
2641 if (!(flags & QTW_IGNORE_JOINALIASES))
2642 MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
2645 /* else, copy join aliases as-is */
2646 newrte->joinaliasvars = copyObject(rte->joinaliasvars);
2650 MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
2653 MUTATE(newrte->values_lists, rte->values_lists, List *);
2656 newrt = lappend(newrt, newrte);
2662 * query_or_expression_tree_walker --- hybrid form
2664 * This routine will invoke query_tree_walker if called on a Query node,
2665 * else will invoke the walker directly. This is a useful way of starting
2666 * the recursion when the walker's normal change of state is not appropriate
2667 * for the outermost Query node.
2670 query_or_expression_tree_walker(Node *node,
2675 if (node && IsA(node, Query))
2676 return query_tree_walker((Query *) node,
2681 return walker(node, context);
2685 * query_or_expression_tree_mutator --- hybrid form
2687 * This routine will invoke query_tree_mutator if called on a Query node,
2688 * else will invoke the mutator directly. This is a useful way of starting
2689 * the recursion when the mutator's normal change of state is not appropriate
2690 * for the outermost Query node.
2693 query_or_expression_tree_mutator(Node *node,
2694 Node *(*mutator) (),
2698 if (node && IsA(node, Query))
2699 return (Node *) query_tree_mutator((Query *) node,
2704 return mutator(node, context);
2709 * raw_expression_tree_walker --- walk raw parse trees
2711 * This has exactly the same API as expression_tree_walker, but instead of
2712 * walking post-analysis parse trees, it knows how to walk the node types
2713 * found in raw grammar output. (There is not currently any need for a
2714 * combined walker, so we keep them separate in the name of efficiency.)
2715 * Unlike expression_tree_walker, there is no special rule about query
2716 * boundaries: we descend to everything that's possibly interesting.
2718 * Currently, the node type coverage extends to SelectStmt and everything
2719 * that could appear under it, but not other statement types.
2722 raw_expression_tree_walker(Node *node,
2729 * The walker has already visited the current node, and so we need only
2730 * recurse into any sub-nodes it has.
2735 /* Guard against stack overflow due to overly complex expressions */
2736 check_stack_depth();
2738 switch (nodeTag(node))
2740 case T_SetToDefault:
2741 case T_CurrentOfExpr:
2750 /* primitive node types with no subnodes */
2753 /* we assume the colnames list isn't interesting */
2756 return walker(((RangeVar *) node)->alias, context);
2759 SubLink *sublink = (SubLink *) node;
2761 if (walker(sublink->testexpr, context))
2763 /* we assume the operName is not interesting */
2764 if (walker(sublink->subselect, context))
2770 CaseExpr *caseexpr = (CaseExpr *) node;
2772 if (walker(caseexpr->arg, context))
2774 /* we assume walker doesn't care about CaseWhens, either */
2775 foreach(temp, caseexpr->args)
2777 CaseWhen *when = (CaseWhen *) lfirst(temp);
2779 Assert(IsA(when, CaseWhen));
2780 if (walker(when->expr, context))
2782 if (walker(when->result, context))
2785 if (walker(caseexpr->defresult, context))
2790 /* Assume colnames isn't interesting */
2791 return walker(((RowExpr *) node)->args, context);
2792 case T_CoalesceExpr:
2793 return walker(((CoalesceExpr *) node)->args, context);
2795 return walker(((MinMaxExpr *) node)->args, context);
2798 XmlExpr *xexpr = (XmlExpr *) node;
2800 if (walker(xexpr->named_args, context))
2802 /* we assume walker doesn't care about arg_names */
2803 if (walker(xexpr->args, context))
2808 return walker(((NullTest *) node)->arg, context);
2810 return walker(((BooleanTest *) node)->arg, context);
2813 JoinExpr *join = (JoinExpr *) node;
2815 if (walker(join->larg, context))
2817 if (walker(join->rarg, context))
2819 if (walker(join->quals, context))
2821 if (walker(join->alias, context))
2823 /* using list is deemed uninteresting */
2828 IntoClause *into = (IntoClause *) node;
2830 if (walker(into->rel, context))
2832 /* colNames, options are deemed uninteresting */
2836 foreach(temp, (List *) node)
2838 if (walker((Node *) lfirst(temp), context))
2844 InsertStmt *stmt = (InsertStmt *) node;
2846 if (walker(stmt->relation, context))
2848 if (walker(stmt->cols, context))
2850 if (walker(stmt->selectStmt, context))
2852 if (walker(stmt->returningList, context))
2854 if (walker(stmt->withClause, context))
2860 DeleteStmt *stmt = (DeleteStmt *) node;
2862 if (walker(stmt->relation, context))
2864 if (walker(stmt->usingClause, context))
2866 if (walker(stmt->whereClause, context))
2868 if (walker(stmt->returningList, context))
2870 if (walker(stmt->withClause, context))
2876 UpdateStmt *stmt = (UpdateStmt *) node;
2878 if (walker(stmt->relation, context))
2880 if (walker(stmt->targetList, context))
2882 if (walker(stmt->whereClause, context))
2884 if (walker(stmt->fromClause, context))
2886 if (walker(stmt->returningList, context))
2888 if (walker(stmt->withClause, context))
2894 SelectStmt *stmt = (SelectStmt *) node;
2896 if (walker(stmt->distinctClause, context))
2898 if (walker(stmt->intoClause, context))
2900 if (walker(stmt->targetList, context))
2902 if (walker(stmt->fromClause, context))
2904 if (walker(stmt->whereClause, context))
2906 if (walker(stmt->groupClause, context))
2908 if (walker(stmt->havingClause, context))
2910 if (walker(stmt->windowClause, context))
2912 if (walker(stmt->withClause, context))
2914 if (walker(stmt->valuesLists, context))
2916 if (walker(stmt->sortClause, context))
2918 if (walker(stmt->limitOffset, context))
2920 if (walker(stmt->limitCount, context))
2922 if (walker(stmt->lockingClause, context))
2924 if (walker(stmt->larg, context))
2926 if (walker(stmt->rarg, context))
2932 A_Expr *expr = (A_Expr *) node;
2934 if (walker(expr->lexpr, context))
2936 if (walker(expr->rexpr, context))
2938 /* operator name is deemed uninteresting */
2942 /* we assume the fields contain nothing interesting */
2946 FuncCall *fcall = (FuncCall *) node;
2948 if (walker(fcall->args, context))
2950 if (walker(fcall->agg_order, context))
2952 if (walker(fcall->over, context))
2954 /* function name is deemed uninteresting */
2957 case T_NamedArgExpr:
2958 return walker(((NamedArgExpr *) node)->arg, context);
2961 A_Indices *indices = (A_Indices *) node;
2963 if (walker(indices->lidx, context))
2965 if (walker(indices->uidx, context))
2969 case T_A_Indirection:
2971 A_Indirection *indir = (A_Indirection *) node;
2973 if (walker(indir->arg, context))
2975 if (walker(indir->indirection, context))
2980 return walker(((A_ArrayExpr *) node)->elements, context);
2983 ResTarget *rt = (ResTarget *) node;
2985 if (walker(rt->indirection, context))
2987 if (walker(rt->val, context))
2993 TypeCast *tc = (TypeCast *) node;
2995 if (walker(tc->arg, context))
2997 if (walker(tc->typeName, context))
3001 case T_CollateClause:
3002 return walker(((CollateClause *) node)->arg, context);
3004 return walker(((SortBy *) node)->node, context);
3007 WindowDef *wd = (WindowDef *) node;
3009 if (walker(wd->partitionClause, context))
3011 if (walker(wd->orderClause, context))
3013 if (walker(wd->startOffset, context))
3015 if (walker(wd->endOffset, context))
3019 case T_RangeSubselect:
3021 RangeSubselect *rs = (RangeSubselect *) node;
3023 if (walker(rs->subquery, context))
3025 if (walker(rs->alias, context))
3029 case T_RangeFunction:
3031 RangeFunction *rf = (RangeFunction *) node;
3033 if (walker(rf->funccallnode, context))
3035 if (walker(rf->alias, context))
3041 TypeName *tn = (TypeName *) node;
3043 if (walker(tn->typmods, context))
3045 if (walker(tn->arrayBounds, context))
3047 /* type name itself is deemed uninteresting */
3052 ColumnDef *coldef = (ColumnDef *) node;
3054 if (walker(coldef->typeName, context))
3056 if (walker(coldef->raw_default, context))
3058 if (walker(coldef->collClause, context))
3060 /* for now, constraints are ignored */
3063 case T_LockingClause:
3064 return walker(((LockingClause *) node)->lockedRels, context);
3065 case T_XmlSerialize:
3067 XmlSerialize *xs = (XmlSerialize *) node;
3069 if (walker(xs->expr, context))
3071 if (walker(xs->typeName, context))
3076 return walker(((WithClause *) node)->ctes, context);
3077 case T_CommonTableExpr:
3078 return walker(((CommonTableExpr *) node)->ctequery, context);
3080 elog(ERROR, "unrecognized node type: %d",
3081 (int) nodeTag(node));