1 /*-------------------------------------------------------------------------
4 * Various general-purpose manipulations of Node trees
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_type.h"
18 #include "miscadmin.h"
19 #include "nodes/nodeFuncs.h"
20 #include "nodes/relation.h"
21 #include "utils/builtins.h"
22 #include "utils/lsyscache.h"
25 static bool expression_returns_set_walker(Node
*node
, void *context
);
26 static int leftmostLoc(int loc1
, int loc2
);
31 * returns the Oid of the type of the expression. (Used for typechecking.)
41 switch (nodeTag(expr
))
44 type
= ((Var
*) expr
)->vartype
;
47 type
= ((Const
*) expr
)->consttype
;
50 type
= ((Param
*) expr
)->paramtype
;
53 type
= ((Aggref
*) expr
)->aggtype
;
57 ArrayRef
*arrayref
= (ArrayRef
*) expr
;
59 /* slice and/or store operations yield the array type */
60 if (arrayref
->reflowerindexpr
|| arrayref
->refassgnexpr
)
61 type
= arrayref
->refarraytype
;
63 type
= arrayref
->refelemtype
;
67 type
= ((FuncExpr
*) expr
)->funcresulttype
;
70 type
= ((OpExpr
*) expr
)->opresulttype
;
73 type
= ((DistinctExpr
*) expr
)->opresulttype
;
75 case T_ScalarArrayOpExpr
:
83 SubLink
*sublink
= (SubLink
*) expr
;
85 if (sublink
->subLinkType
== EXPR_SUBLINK
||
86 sublink
->subLinkType
== ARRAY_SUBLINK
)
88 /* get the type of the subselect's first target column */
89 Query
*qtree
= (Query
*) sublink
->subselect
;
92 if (!qtree
|| !IsA(qtree
, Query
))
93 elog(ERROR
, "cannot get type for untransformed sublink");
94 tent
= (TargetEntry
*) linitial(qtree
->targetList
);
95 Assert(IsA(tent
, TargetEntry
));
96 Assert(!tent
->resjunk
);
97 type
= exprType((Node
*) tent
->expr
);
98 if (sublink
->subLinkType
== ARRAY_SUBLINK
)
100 type
= get_array_type(type
);
101 if (!OidIsValid(type
))
103 (errcode(ERRCODE_UNDEFINED_OBJECT
),
104 errmsg("could not find array type for data type %s",
105 format_type_be(exprType((Node
*) tent
->expr
)))));
110 /* for all other sublink types, result is boolean */
118 * Although the parser does not ever deal with already-planned
119 * expression trees, we support SubPlan nodes in this routine
120 * for the convenience of ruleutils.c.
122 SubPlan
*subplan
= (SubPlan
*) expr
;
124 if (subplan
->subLinkType
== EXPR_SUBLINK
||
125 subplan
->subLinkType
== ARRAY_SUBLINK
)
127 /* get the type of the subselect's first target column */
128 type
= subplan
->firstColType
;
129 if (subplan
->subLinkType
== ARRAY_SUBLINK
)
131 type
= get_array_type(type
);
132 if (!OidIsValid(type
))
134 (errcode(ERRCODE_UNDEFINED_OBJECT
),
135 errmsg("could not find array type for data type %s",
136 format_type_be(subplan
->firstColType
))));
141 /* for all other subplan types, result is boolean */
146 case T_AlternativeSubPlan
:
148 /* As above, supported for the convenience of ruleutils.c */
149 AlternativeSubPlan
*asplan
= (AlternativeSubPlan
*) expr
;
151 /* subplans should all return the same thing */
152 type
= exprType((Node
*) linitial(asplan
->subplans
));
156 type
= ((FieldSelect
*) expr
)->resulttype
;
159 type
= ((FieldStore
*) expr
)->resulttype
;
162 type
= ((RelabelType
*) expr
)->resulttype
;
165 type
= ((CoerceViaIO
*) expr
)->resulttype
;
167 case T_ArrayCoerceExpr
:
168 type
= ((ArrayCoerceExpr
*) expr
)->resulttype
;
170 case T_ConvertRowtypeExpr
:
171 type
= ((ConvertRowtypeExpr
*) expr
)->resulttype
;
174 type
= ((CaseExpr
*) expr
)->casetype
;
177 type
= ((CaseTestExpr
*) expr
)->typeId
;
180 type
= ((ArrayExpr
*) expr
)->array_typeid
;
183 type
= ((RowExpr
*) expr
)->row_typeid
;
185 case T_RowCompareExpr
:
189 type
= ((CoalesceExpr
*) expr
)->coalescetype
;
192 type
= ((MinMaxExpr
*) expr
)->minmaxtype
;
195 if (((XmlExpr
*) expr
)->op
== IS_DOCUMENT
)
197 else if (((XmlExpr
*) expr
)->op
== IS_XMLSERIALIZE
)
203 type
= exprType((Node
*) linitial(((NullIfExpr
*) expr
)->args
));
211 case T_CoerceToDomain
:
212 type
= ((CoerceToDomain
*) expr
)->resulttype
;
214 case T_CoerceToDomainValue
:
215 type
= ((CoerceToDomainValue
*) expr
)->typeId
;
218 type
= ((SetToDefault
*) expr
)->typeId
;
220 case T_CurrentOfExpr
:
223 case T_PlaceHolderVar
:
224 type
= exprType((Node
*) ((PlaceHolderVar
*) expr
)->phexpr
);
227 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(expr
));
228 type
= InvalidOid
; /* keep compiler quiet */
236 * returns the type-specific attrmod of the expression, if it can be
237 * determined. In most cases, it can't and we return -1.
240 exprTypmod(Node
*expr
)
245 switch (nodeTag(expr
))
248 return ((Var
*) expr
)->vartypmod
;
250 return ((Const
*) expr
)->consttypmod
;
252 return ((Param
*) expr
)->paramtypmod
;
254 /* typmod is the same for array or element */
255 return ((ArrayRef
*) expr
)->reftypmod
;
260 /* Be smart about length-coercion functions... */
261 if (exprIsLengthCoercion(expr
, &coercedTypmod
))
262 return coercedTypmod
;
267 SubLink
*sublink
= (SubLink
*) expr
;
269 if (sublink
->subLinkType
== EXPR_SUBLINK
||
270 sublink
->subLinkType
== ARRAY_SUBLINK
)
272 /* get the typmod of the subselect's first target column */
273 Query
*qtree
= (Query
*) sublink
->subselect
;
276 if (!qtree
|| !IsA(qtree
, Query
))
277 elog(ERROR
, "cannot get type for untransformed sublink");
278 tent
= (TargetEntry
*) linitial(qtree
->targetList
);
279 Assert(IsA(tent
, TargetEntry
));
280 Assert(!tent
->resjunk
);
281 return exprTypmod((Node
*) tent
->expr
);
282 /* note we don't need to care if it's an array */
287 return ((FieldSelect
*) expr
)->resulttypmod
;
289 return ((RelabelType
*) expr
)->resulttypmod
;
290 case T_ArrayCoerceExpr
:
291 return ((ArrayCoerceExpr
*) expr
)->resulttypmod
;
295 * If all the alternatives agree on type/typmod, return that
296 * typmod, else use -1
298 CaseExpr
*cexpr
= (CaseExpr
*) expr
;
299 Oid casetype
= cexpr
->casetype
;
303 if (!cexpr
->defresult
)
305 if (exprType((Node
*) cexpr
->defresult
) != casetype
)
307 typmod
= exprTypmod((Node
*) cexpr
->defresult
);
309 return -1; /* no point in trying harder */
310 foreach(arg
, cexpr
->args
)
312 CaseWhen
*w
= (CaseWhen
*) lfirst(arg
);
314 Assert(IsA(w
, CaseWhen
));
315 if (exprType((Node
*) w
->result
) != casetype
)
317 if (exprTypmod((Node
*) w
->result
) != typmod
)
324 return ((CaseTestExpr
*) expr
)->typeMod
;
328 * If all the elements agree on type/typmod, return that
329 * typmod, else use -1
331 ArrayExpr
*arrayexpr
= (ArrayExpr
*) expr
;
336 if (arrayexpr
->elements
== NIL
)
338 typmod
= exprTypmod((Node
*) linitial(arrayexpr
->elements
));
340 return -1; /* no point in trying harder */
341 if (arrayexpr
->multidims
)
342 commontype
= arrayexpr
->array_typeid
;
344 commontype
= arrayexpr
->element_typeid
;
345 foreach(elem
, arrayexpr
->elements
)
347 Node
*e
= (Node
*) lfirst(elem
);
349 if (exprType(e
) != commontype
)
351 if (exprTypmod(e
) != typmod
)
360 * If all the alternatives agree on type/typmod, return that
361 * typmod, else use -1
363 CoalesceExpr
*cexpr
= (CoalesceExpr
*) expr
;
364 Oid coalescetype
= cexpr
->coalescetype
;
368 if (exprType((Node
*) linitial(cexpr
->args
)) != coalescetype
)
370 typmod
= exprTypmod((Node
*) linitial(cexpr
->args
));
372 return -1; /* no point in trying harder */
373 for_each_cell(arg
, lnext(list_head(cexpr
->args
)))
375 Node
*e
= (Node
*) lfirst(arg
);
377 if (exprType(e
) != coalescetype
)
379 if (exprTypmod(e
) != typmod
)
388 * If all the alternatives agree on type/typmod, return that
389 * typmod, else use -1
391 MinMaxExpr
*mexpr
= (MinMaxExpr
*) expr
;
392 Oid minmaxtype
= mexpr
->minmaxtype
;
396 if (exprType((Node
*) linitial(mexpr
->args
)) != minmaxtype
)
398 typmod
= exprTypmod((Node
*) linitial(mexpr
->args
));
400 return -1; /* no point in trying harder */
401 for_each_cell(arg
, lnext(list_head(mexpr
->args
)))
403 Node
*e
= (Node
*) lfirst(arg
);
405 if (exprType(e
) != minmaxtype
)
407 if (exprTypmod(e
) != typmod
)
415 NullIfExpr
*nexpr
= (NullIfExpr
*) expr
;
417 return exprTypmod((Node
*) linitial(nexpr
->args
));
420 case T_CoerceToDomain
:
421 return ((CoerceToDomain
*) expr
)->resulttypmod
;
422 case T_CoerceToDomainValue
:
423 return ((CoerceToDomainValue
*) expr
)->typeMod
;
425 return ((SetToDefault
*) expr
)->typeMod
;
426 case T_PlaceHolderVar
:
427 return exprTypmod((Node
*) ((PlaceHolderVar
*) expr
)->phexpr
);
435 * exprIsLengthCoercion
436 * Detect whether an expression tree is an application of a datatype's
437 * typmod-coercion function. Optionally extract the result's typmod.
439 * If coercedTypmod is not NULL, the typmod is stored there if the expression
440 * is a length-coercion function, else -1 is stored there.
442 * Note that a combined type-and-length coercion will be treated as a
443 * length coercion by this routine.
446 exprIsLengthCoercion(Node
*expr
, int32
*coercedTypmod
)
448 if (coercedTypmod
!= NULL
)
449 *coercedTypmod
= -1; /* default result on failure */
452 * Scalar-type length coercions are FuncExprs, array-type length coercions
453 * are ArrayCoerceExprs
455 if (expr
&& IsA(expr
, FuncExpr
))
457 FuncExpr
*func
= (FuncExpr
*) expr
;
462 * If it didn't come from a coercion context, reject.
464 if (func
->funcformat
!= COERCE_EXPLICIT_CAST
&&
465 func
->funcformat
!= COERCE_IMPLICIT_CAST
)
469 * If it's not a two-argument or three-argument function with the
470 * second argument being an int4 constant, it can't have been created
471 * from a length coercion (it must be a type coercion, instead).
473 nargs
= list_length(func
->args
);
474 if (nargs
< 2 || nargs
> 3)
477 second_arg
= (Const
*) lsecond(func
->args
);
478 if (!IsA(second_arg
, Const
) ||
479 second_arg
->consttype
!= INT4OID
||
480 second_arg
->constisnull
)
484 * OK, it is indeed a length-coercion function.
486 if (coercedTypmod
!= NULL
)
487 *coercedTypmod
= DatumGetInt32(second_arg
->constvalue
);
492 if (expr
&& IsA(expr
, ArrayCoerceExpr
))
494 ArrayCoerceExpr
*acoerce
= (ArrayCoerceExpr
*) expr
;
496 /* It's not a length coercion unless there's a nondefault typmod */
497 if (acoerce
->resulttypmod
< 0)
501 * OK, it is indeed a length-coercion expression.
503 if (coercedTypmod
!= NULL
)
504 *coercedTypmod
= acoerce
->resulttypmod
;
513 * expression_returns_set
514 * Test whether an expression returns a set result.
516 * Because we use expression_tree_walker(), this can also be applied to
517 * whole targetlists; it'll produce TRUE if any one of the tlist items
521 expression_returns_set(Node
*clause
)
523 return expression_returns_set_walker(clause
, NULL
);
527 expression_returns_set_walker(Node
*node
, void *context
)
531 if (IsA(node
, FuncExpr
))
533 FuncExpr
*expr
= (FuncExpr
*) node
;
535 if (expr
->funcretset
)
537 /* else fall through to check args */
539 if (IsA(node
, OpExpr
))
541 OpExpr
*expr
= (OpExpr
*) node
;
545 /* else fall through to check args */
548 /* Avoid recursion for some cases that can't return a set */
549 if (IsA(node
, Aggref
))
551 if (IsA(node
, DistinctExpr
))
553 if (IsA(node
, ScalarArrayOpExpr
))
555 if (IsA(node
, BoolExpr
))
557 if (IsA(node
, SubLink
))
559 if (IsA(node
, SubPlan
))
561 if (IsA(node
, AlternativeSubPlan
))
563 if (IsA(node
, ArrayExpr
))
565 if (IsA(node
, RowExpr
))
567 if (IsA(node
, RowCompareExpr
))
569 if (IsA(node
, CoalesceExpr
))
571 if (IsA(node
, MinMaxExpr
))
573 if (IsA(node
, XmlExpr
))
575 if (IsA(node
, NullIfExpr
))
578 return expression_tree_walker(node
, expression_returns_set_walker
,
585 * returns the parse location of an expression tree, for error reports
587 * -1 is returned if the location can't be determined.
589 * For expressions larger than a single token, the intent here is to
590 * return the location of the expression's leftmost token, not necessarily
591 * the topmost Node's location field. For example, an OpExpr's location
592 * field will point at the operator name, but if it is not a prefix operator
593 * then we should return the location of the left-hand operand instead.
594 * The reason is that we want to reference the entire expression not just
595 * that operator, and pointing to its start seems to be the most natural way.
597 * The location is not perfect --- for example, since the grammar doesn't
598 * explicitly represent parentheses in the parsetree, given something that
599 * had been written "(a + b) * c" we are going to point at "a" not "(".
600 * But it should be plenty good enough for error reporting purposes.
602 * You might think that this code is overly general, for instance why check
603 * the operands of a FuncExpr node, when the function name can be expected
604 * to be to the left of them? There are a couple of reasons. The grammar
605 * sometimes builds expressions that aren't quite what the user wrote;
606 * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
607 * pointer is to the right of its leftmost argument. Also, nodes that were
608 * inserted implicitly by parse analysis (such as FuncExprs for implicit
609 * coercions) will have location -1, and so we can have odd combinations of
610 * known and unknown locations in a tree.
613 exprLocation(Node
*expr
)
619 switch (nodeTag(expr
))
622 loc
= ((RangeVar
*) expr
)->location
;
625 loc
= ((Var
*) expr
)->location
;
628 loc
= ((Const
*) expr
)->location
;
631 loc
= ((Param
*) expr
)->location
;
634 /* function name should always be the first thing */
635 loc
= ((Aggref
*) expr
)->location
;
638 /* just use array argument's location */
639 loc
= exprLocation((Node
*) ((ArrayRef
*) expr
)->refexpr
);
643 FuncExpr
*fexpr
= (FuncExpr
*) expr
;
645 /* consider both function name and leftmost arg */
646 loc
= leftmostLoc(fexpr
->location
,
647 exprLocation((Node
*) fexpr
->args
));
651 case T_DistinctExpr
: /* struct-equivalent to OpExpr */
652 case T_NullIfExpr
: /* struct-equivalent to OpExpr */
654 OpExpr
*opexpr
= (OpExpr
*) expr
;
656 /* consider both operator name and leftmost arg */
657 loc
= leftmostLoc(opexpr
->location
,
658 exprLocation((Node
*) opexpr
->args
));
661 case T_ScalarArrayOpExpr
:
663 ScalarArrayOpExpr
*saopexpr
= (ScalarArrayOpExpr
*) expr
;
665 /* consider both operator name and leftmost arg */
666 loc
= leftmostLoc(saopexpr
->location
,
667 exprLocation((Node
*) saopexpr
->args
));
672 BoolExpr
*bexpr
= (BoolExpr
*) expr
;
675 * Same as above, to handle either NOT or AND/OR. We can't
676 * special-case NOT because of the way that it's used for
677 * things like IS NOT BETWEEN.
679 loc
= leftmostLoc(bexpr
->location
,
680 exprLocation((Node
*) bexpr
->args
));
685 SubLink
*sublink
= (SubLink
*) expr
;
687 /* check the testexpr, if any, and the operator/keyword */
688 loc
= leftmostLoc(exprLocation(sublink
->testexpr
),
693 /* just use argument's location */
694 loc
= exprLocation((Node
*) ((FieldSelect
*) expr
)->arg
);
697 /* just use argument's location */
698 loc
= exprLocation((Node
*) ((FieldStore
*) expr
)->arg
);
702 RelabelType
*rexpr
= (RelabelType
*) expr
;
705 loc
= leftmostLoc(rexpr
->location
,
706 exprLocation((Node
*) rexpr
->arg
));
711 CoerceViaIO
*cexpr
= (CoerceViaIO
*) expr
;
714 loc
= leftmostLoc(cexpr
->location
,
715 exprLocation((Node
*) cexpr
->arg
));
718 case T_ArrayCoerceExpr
:
720 ArrayCoerceExpr
*cexpr
= (ArrayCoerceExpr
*) expr
;
723 loc
= leftmostLoc(cexpr
->location
,
724 exprLocation((Node
*) cexpr
->arg
));
727 case T_ConvertRowtypeExpr
:
729 ConvertRowtypeExpr
*cexpr
= (ConvertRowtypeExpr
*) expr
;
732 loc
= leftmostLoc(cexpr
->location
,
733 exprLocation((Node
*) cexpr
->arg
));
737 /* CASE keyword should always be the first thing */
738 loc
= ((CaseExpr
*) expr
)->location
;
741 /* WHEN keyword should always be the first thing */
742 loc
= ((CaseWhen
*) expr
)->location
;
745 /* the location points at ARRAY or [, which must be leftmost */
746 loc
= ((ArrayExpr
*) expr
)->location
;
749 /* the location points at ROW or (, which must be leftmost */
750 loc
= ((RowExpr
*) expr
)->location
;
752 case T_RowCompareExpr
:
753 /* just use leftmost argument's location */
754 loc
= exprLocation((Node
*) ((RowCompareExpr
*) expr
)->largs
);
757 /* COALESCE keyword should always be the first thing */
758 loc
= ((CoalesceExpr
*) expr
)->location
;
761 /* GREATEST/LEAST keyword should always be the first thing */
762 loc
= ((MinMaxExpr
*) expr
)->location
;
766 XmlExpr
*xexpr
= (XmlExpr
*) expr
;
768 /* consider both function name and leftmost arg */
769 loc
= leftmostLoc(xexpr
->location
,
770 exprLocation((Node
*) xexpr
->args
));
774 /* just use argument's location */
775 loc
= exprLocation((Node
*) ((NullTest
*) expr
)->arg
);
778 /* just use argument's location */
779 loc
= exprLocation((Node
*) ((BooleanTest
*) expr
)->arg
);
781 case T_CoerceToDomain
:
783 CoerceToDomain
*cexpr
= (CoerceToDomain
*) expr
;
786 loc
= leftmostLoc(cexpr
->location
,
787 exprLocation((Node
*) cexpr
->arg
));
790 case T_CoerceToDomainValue
:
791 loc
= ((CoerceToDomainValue
*) expr
)->location
;
794 loc
= ((SetToDefault
*) expr
)->location
;
797 /* just use argument's location */
798 loc
= exprLocation((Node
*) ((TargetEntry
*) expr
)->expr
);
801 /* use the contained RangeVar's location --- close enough */
802 loc
= exprLocation((Node
*) ((IntoClause
*) expr
)->rel
);
806 /* report location of first list member that has a location */
809 loc
= -1; /* just to suppress compiler warning */
810 foreach(lc
, (List
*) expr
)
812 loc
= exprLocation((Node
*) lfirst(lc
));
820 A_Expr
*aexpr
= (A_Expr
*) expr
;
822 /* use leftmost of operator or left operand (if any) */
823 /* we assume right operand can't be to left of operator */
824 loc
= leftmostLoc(aexpr
->location
,
825 exprLocation(aexpr
->lexpr
));
829 loc
= ((ColumnRef
*) expr
)->location
;
832 loc
= ((ParamRef
*) expr
)->location
;
835 loc
= ((A_Const
*) expr
)->location
;
839 FuncCall
*fc
= (FuncCall
*) expr
;
841 /* consider both function name and leftmost arg */
842 loc
= leftmostLoc(fc
->location
,
843 exprLocation((Node
*) fc
->args
));
847 /* the location points at ARRAY or [, which must be leftmost */
848 loc
= ((A_ArrayExpr
*) expr
)->location
;
851 /* we need not examine the contained expression (if any) */
852 loc
= ((ResTarget
*) expr
)->location
;
856 TypeCast
*tc
= (TypeCast
*) expr
;
859 * This could represent CAST(), ::, or TypeName 'literal',
860 * so any of the components might be leftmost.
862 loc
= exprLocation(tc
->arg
);
863 loc
= leftmostLoc(loc
, tc
->typename
->location
);
864 loc
= leftmostLoc(loc
, tc
->location
);
868 /* just use argument's location (ignore operator, if any) */
869 loc
= exprLocation(((SortBy
*) expr
)->node
);
872 loc
= ((TypeName
*) expr
)->location
;
875 /* XMLSERIALIZE keyword should always be the first thing */
876 loc
= ((XmlSerialize
*) expr
)->location
;
879 loc
= ((WithClause
*) expr
)->location
;
881 case T_CommonTableExpr
:
882 loc
= ((CommonTableExpr
*) expr
)->location
;
884 case T_PlaceHolderVar
:
885 /* just use argument's location */
886 loc
= exprLocation((Node
*) ((PlaceHolderVar
*) expr
)->phexpr
);
889 /* for any other node type it's just unknown... */
897 * leftmostLoc - support for exprLocation
899 * Take the minimum of two parse location values, but ignore unknowns
902 leftmostLoc(int loc1
, int loc2
)
909 return Min(loc1
, loc2
);
914 * Standard expression-tree walking support
916 * We used to have near-duplicate code in many different routines that
917 * understood how to recurse through an expression node tree. That was
918 * a pain to maintain, and we frequently had bugs due to some particular
919 * routine neglecting to support a particular node type. In most cases,
920 * these routines only actually care about certain node types, and don't
921 * care about other types except insofar as they have to recurse through
922 * non-primitive node types. Therefore, we now provide generic tree-walking
923 * logic to consolidate the redundant "boilerplate" code. There are
924 * two versions: expression_tree_walker() and expression_tree_mutator().
928 * expression_tree_walker() is designed to support routines that traverse
929 * a tree in a read-only fashion (although it will also work for routines
930 * that modify nodes in-place but never add/delete/replace nodes).
931 * A walker routine should look like this:
933 * bool my_walker (Node *node, my_struct *context)
937 * // check for nodes that special work is required for, eg:
938 * if (IsA(node, Var))
940 * ... do special actions for Var nodes
942 * else if (IsA(node, ...))
944 * ... do special actions for other node types
946 * // for any node type not specially processed, do:
947 * return expression_tree_walker(node, my_walker, (void *) context);
950 * The "context" argument points to a struct that holds whatever context
951 * information the walker routine needs --- it can be used to return data
952 * gathered by the walker, too. This argument is not touched by
953 * expression_tree_walker, but it is passed down to recursive sub-invocations
954 * of my_walker. The tree walk is started from a setup routine that
955 * fills in the appropriate context struct, calls my_walker with the top-level
956 * node of the tree, and then examines the results.
958 * The walker routine should return "false" to continue the tree walk, or
959 * "true" to abort the walk and immediately return "true" to the top-level
960 * caller. This can be used to short-circuit the traversal if the walker
961 * has found what it came for. "false" is returned to the top-level caller
962 * iff no invocation of the walker returned "true".
964 * The node types handled by expression_tree_walker include all those
965 * normally found in target lists and qualifier clauses during the planning
966 * stage. In particular, it handles List nodes since a cnf-ified qual clause
967 * will have List structure at the top level, and it handles TargetEntry nodes
968 * so that a scan of a target list can be handled without additional code.
969 * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
970 * handled, so that query jointrees and setOperation trees can be processed
971 * without additional code.
973 * expression_tree_walker will handle SubLink nodes by recursing normally
974 * into the "testexpr" subtree (which is an expression belonging to the outer
975 * plan). It will also call the walker on the sub-Query node; however, when
976 * expression_tree_walker itself is called on a Query node, it does nothing
977 * and returns "false". The net effect is that unless the walker does
978 * something special at a Query node, sub-selects will not be visited during
979 * an expression tree walk. This is exactly the behavior wanted in many cases
980 * --- and for those walkers that do want to recurse into sub-selects, special
981 * behavior is typically needed anyway at the entry to a sub-select (such as
982 * incrementing a depth counter). A walker that wants to examine sub-selects
983 * should include code along the lines of:
985 * if (IsA(node, Query))
987 * adjust context for subquery;
988 * result = query_tree_walker((Query *) node, my_walker, context,
989 * 0); // adjust flags as needed
990 * restore context if needed;
994 * query_tree_walker is a convenience routine (see below) that calls the
995 * walker on all the expression subtrees of the given Query node.
997 * expression_tree_walker will handle SubPlan nodes by recursing normally
998 * into the "testexpr" and the "args" list (which are expressions belonging to
999 * the outer plan). It will not touch the completed subplan, however. Since
1000 * there is no link to the original Query, it is not possible to recurse into
1001 * subselects of an already-planned expression tree. This is OK for current
1002 * uses, but may need to be revisited in future.
1006 expression_tree_walker(Node
*node
,
1013 * The walker has already visited the current node, and so we need only
1014 * recurse into any sub-nodes it has.
1016 * We assume that the walker is not interested in List nodes per se, so
1017 * when we expect a List we just recurse directly to self without
1018 * bothering to call the walker.
1023 /* Guard against stack overflow due to overly complex expressions */
1024 check_stack_depth();
1026 switch (nodeTag(node
))
1031 case T_CoerceToDomainValue
:
1032 case T_CaseTestExpr
:
1033 case T_SetToDefault
:
1034 case T_CurrentOfExpr
:
1036 /* primitive node types with no expression subnodes */
1040 Aggref
*expr
= (Aggref
*) node
;
1042 /* recurse directly on List */
1043 if (expression_tree_walker((Node
*) expr
->args
,
1050 ArrayRef
*aref
= (ArrayRef
*) node
;
1052 /* recurse directly for upper/lower array index lists */
1053 if (expression_tree_walker((Node
*) aref
->refupperindexpr
,
1056 if (expression_tree_walker((Node
*) aref
->reflowerindexpr
,
1059 /* walker must see the refexpr and refassgnexpr, however */
1060 if (walker(aref
->refexpr
, context
))
1062 if (walker(aref
->refassgnexpr
, context
))
1068 FuncExpr
*expr
= (FuncExpr
*) node
;
1070 if (expression_tree_walker((Node
*) expr
->args
,
1077 OpExpr
*expr
= (OpExpr
*) node
;
1079 if (expression_tree_walker((Node
*) expr
->args
,
1084 case T_DistinctExpr
:
1086 DistinctExpr
*expr
= (DistinctExpr
*) node
;
1088 if (expression_tree_walker((Node
*) expr
->args
,
1093 case T_ScalarArrayOpExpr
:
1095 ScalarArrayOpExpr
*expr
= (ScalarArrayOpExpr
*) node
;
1097 if (expression_tree_walker((Node
*) expr
->args
,
1104 BoolExpr
*expr
= (BoolExpr
*) node
;
1106 if (expression_tree_walker((Node
*) expr
->args
,
1113 SubLink
*sublink
= (SubLink
*) node
;
1115 if (walker(sublink
->testexpr
, context
))
1119 * Also invoke the walker on the sublink's Query node, so it
1120 * can recurse into the sub-query if it wants to.
1122 return walker(sublink
->subselect
, context
);
1127 SubPlan
*subplan
= (SubPlan
*) node
;
1129 /* recurse into the testexpr, but not into the Plan */
1130 if (walker(subplan
->testexpr
, context
))
1132 /* also examine args list */
1133 if (expression_tree_walker((Node
*) subplan
->args
,
1138 case T_AlternativeSubPlan
:
1139 return walker(((AlternativeSubPlan
*) node
)->subplans
, context
);
1141 return walker(((FieldSelect
*) node
)->arg
, context
);
1144 FieldStore
*fstore
= (FieldStore
*) node
;
1146 if (walker(fstore
->arg
, context
))
1148 if (walker(fstore
->newvals
, context
))
1153 return walker(((RelabelType
*) node
)->arg
, context
);
1155 return walker(((CoerceViaIO
*) node
)->arg
, context
);
1156 case T_ArrayCoerceExpr
:
1157 return walker(((ArrayCoerceExpr
*) node
)->arg
, context
);
1158 case T_ConvertRowtypeExpr
:
1159 return walker(((ConvertRowtypeExpr
*) node
)->arg
, context
);
1162 CaseExpr
*caseexpr
= (CaseExpr
*) node
;
1164 if (walker(caseexpr
->arg
, context
))
1166 /* we assume walker doesn't care about CaseWhens, either */
1167 foreach(temp
, caseexpr
->args
)
1169 CaseWhen
*when
= (CaseWhen
*) lfirst(temp
);
1171 Assert(IsA(when
, CaseWhen
));
1172 if (walker(when
->expr
, context
))
1174 if (walker(when
->result
, context
))
1177 if (walker(caseexpr
->defresult
, context
))
1182 return walker(((ArrayExpr
*) node
)->elements
, context
);
1184 /* Assume colnames isn't interesting */
1185 return walker(((RowExpr
*) node
)->args
, context
);
1186 case T_RowCompareExpr
:
1188 RowCompareExpr
*rcexpr
= (RowCompareExpr
*) node
;
1190 if (walker(rcexpr
->largs
, context
))
1192 if (walker(rcexpr
->rargs
, context
))
1196 case T_CoalesceExpr
:
1197 return walker(((CoalesceExpr
*) node
)->args
, context
);
1199 return walker(((MinMaxExpr
*) node
)->args
, context
);
1202 XmlExpr
*xexpr
= (XmlExpr
*) node
;
1204 if (walker(xexpr
->named_args
, context
))
1206 /* we assume walker doesn't care about arg_names */
1207 if (walker(xexpr
->args
, context
))
1212 return walker(((NullIfExpr
*) node
)->args
, context
);
1214 return walker(((NullTest
*) node
)->arg
, context
);
1216 return walker(((BooleanTest
*) node
)->arg
, context
);
1217 case T_CoerceToDomain
:
1218 return walker(((CoerceToDomain
*) node
)->arg
, context
);
1220 return walker(((TargetEntry
*) node
)->expr
, context
);
1222 /* Do nothing with a sub-Query, per discussion above */
1224 case T_CommonTableExpr
:
1226 CommonTableExpr
*cte
= (CommonTableExpr
*) node
;
1229 * Invoke the walker on the CTE's Query node, so it
1230 * can recurse into the sub-query if it wants to.
1232 return walker(cte
->ctequery
, context
);
1236 foreach(temp
, (List
*) node
)
1238 if (walker((Node
*) lfirst(temp
), context
))
1244 FromExpr
*from
= (FromExpr
*) node
;
1246 if (walker(from
->fromlist
, context
))
1248 if (walker(from
->quals
, context
))
1254 JoinExpr
*join
= (JoinExpr
*) node
;
1256 if (walker(join
->larg
, context
))
1258 if (walker(join
->rarg
, context
))
1260 if (walker(join
->quals
, context
))
1264 * alias clause, using list are deemed uninteresting.
1268 case T_SetOperationStmt
:
1270 SetOperationStmt
*setop
= (SetOperationStmt
*) node
;
1272 if (walker(setop
->larg
, context
))
1274 if (walker(setop
->rarg
, context
))
1277 /* groupClauses are deemed uninteresting */
1280 case T_FlattenedSubLink
:
1282 FlattenedSubLink
*fslink
= (FlattenedSubLink
*) node
;
1284 if (walker(fslink
->quals
, context
))
1288 case T_PlaceHolderVar
:
1289 return walker(((PlaceHolderVar
*) node
)->phexpr
, context
);
1290 case T_AppendRelInfo
:
1292 AppendRelInfo
*appinfo
= (AppendRelInfo
*) node
;
1294 if (expression_tree_walker((Node
*) appinfo
->translated_vars
,
1299 case T_PlaceHolderInfo
:
1300 return walker(((PlaceHolderInfo
*) node
)->ph_var
, context
);
1302 elog(ERROR
, "unrecognized node type: %d",
1303 (int) nodeTag(node
));
1310 * query_tree_walker --- initiate a walk of a Query's expressions
1312 * This routine exists just to reduce the number of places that need to know
1313 * where all the expression subtrees of a Query are. Note it can be used
1314 * for starting a walk at top level of a Query regardless of whether the
1315 * walker intends to descend into subqueries. It is also useful for
1316 * descending into subqueries within a walker.
1318 * Some callers want to suppress visitation of certain items in the sub-Query,
1319 * typically because they need to process them specially, or don't actually
1320 * want to recurse into subqueries. This is supported by the flags argument,
1321 * which is the bitwise OR of flag values to suppress visitation of
1322 * indicated items. (More flag bits may be added as needed.)
1325 query_tree_walker(Query
*query
,
1330 Assert(query
!= NULL
&& IsA(query
, Query
));
1332 if (walker((Node
*) query
->targetList
, context
))
1334 if (walker((Node
*) query
->returningList
, context
))
1336 if (walker((Node
*) query
->jointree
, context
))
1338 if (walker(query
->setOperations
, context
))
1340 if (walker(query
->havingQual
, context
))
1342 if (walker(query
->limitOffset
, context
))
1344 if (walker(query
->limitCount
, context
))
1346 if (!(flags
& QTW_IGNORE_CTE_SUBQUERIES
))
1348 if (walker((Node
*) query
->cteList
, context
))
1351 if (range_table_walker(query
->rtable
, walker
, context
, flags
))
1357 * range_table_walker is just the part of query_tree_walker that scans
1358 * a query's rangetable. This is split out since it can be useful on
1362 range_table_walker(List
*rtable
,
1371 RangeTblEntry
*rte
= (RangeTblEntry
*) lfirst(rt
);
1373 /* For historical reasons, visiting RTEs is not the default */
1374 if (flags
& QTW_EXAMINE_RTES
)
1375 if (walker(rte
, context
))
1378 switch (rte
->rtekind
)
1386 if (!(flags
& QTW_IGNORE_RT_SUBQUERIES
))
1387 if (walker(rte
->subquery
, context
))
1391 if (!(flags
& QTW_IGNORE_JOINALIASES
))
1392 if (walker(rte
->joinaliasvars
, context
))
1396 if (walker(rte
->funcexpr
, context
))
1400 if (walker(rte
->values_lists
, context
))
1410 * expression_tree_mutator() is designed to support routines that make a
1411 * modified copy of an expression tree, with some nodes being added,
1412 * removed, or replaced by new subtrees. The original tree is (normally)
1413 * not changed. Each recursion level is responsible for returning a copy of
1414 * (or appropriately modified substitute for) the subtree it is handed.
1415 * A mutator routine should look like this:
1417 * Node * my_mutator (Node *node, my_struct *context)
1421 * // check for nodes that special work is required for, eg:
1422 * if (IsA(node, Var))
1424 * ... create and return modified copy of Var node
1426 * else if (IsA(node, ...))
1428 * ... do special transformations of other node types
1430 * // for any node type not specially processed, do:
1431 * return expression_tree_mutator(node, my_mutator, (void *) context);
1434 * The "context" argument points to a struct that holds whatever context
1435 * information the mutator routine needs --- it can be used to return extra
1436 * data gathered by the mutator, too. This argument is not touched by
1437 * expression_tree_mutator, but it is passed down to recursive sub-invocations
1438 * of my_mutator. The tree walk is started from a setup routine that
1439 * fills in the appropriate context struct, calls my_mutator with the
1440 * top-level node of the tree, and does any required post-processing.
1442 * Each level of recursion must return an appropriately modified Node.
1443 * If expression_tree_mutator() is called, it will make an exact copy
1444 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
1445 * of that Node. In this way, my_mutator() has full control over the
1446 * copying process but need not directly deal with expression trees
1447 * that it has no interest in.
1449 * Just as for expression_tree_walker, the node types handled by
1450 * expression_tree_mutator include all those normally found in target lists
1451 * and qualifier clauses during the planning stage.
1453 * expression_tree_mutator will handle SubLink nodes by recursing normally
1454 * into the "testexpr" subtree (which is an expression belonging to the outer
1455 * plan). It will also call the mutator on the sub-Query node; however, when
1456 * expression_tree_mutator itself is called on a Query node, it does nothing
1457 * and returns the unmodified Query node. The net effect is that unless the
1458 * mutator does something special at a Query node, sub-selects will not be
1459 * visited or modified; the original sub-select will be linked to by the new
1460 * SubLink node. Mutators that want to descend into sub-selects will usually
1461 * do so by recognizing Query nodes and calling query_tree_mutator (below).
1463 * expression_tree_mutator will handle a SubPlan node by recursing into the
1464 * "testexpr" and the "args" list (which belong to the outer plan), but it
1465 * will simply copy the link to the inner plan, since that's typically what
1466 * expression tree mutators want. A mutator that wants to modify the subplan
1467 * can force appropriate behavior by recognizing SubPlan expression nodes
1468 * and doing the right thing.
1472 expression_tree_mutator(Node
*node
,
1473 Node
*(*mutator
) (),
1477 * The mutator has already decided not to modify the current node, but we
1478 * must call the mutator for any sub-nodes.
1481 #define FLATCOPY(newnode, node, nodetype) \
1482 ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
1483 memcpy((newnode), (node), sizeof(nodetype)) )
1485 #define CHECKFLATCOPY(newnode, node, nodetype) \
1486 ( AssertMacro(IsA((node), nodetype)), \
1487 (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
1488 memcpy((newnode), (node), sizeof(nodetype)) )
1490 #define MUTATE(newfield, oldfield, fieldtype) \
1491 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
1496 /* Guard against stack overflow due to overly complex expressions */
1497 check_stack_depth();
1499 switch (nodeTag(node
))
1502 * Primitive node types with no expression subnodes. Var and
1503 * Const are frequent enough to deserve special cases, the others
1504 * we just use copyObject for.
1508 Var
*var
= (Var
*) node
;
1511 FLATCOPY(newnode
, var
, Var
);
1512 return (Node
*) newnode
;
1517 Const
*oldnode
= (Const
*) node
;
1520 FLATCOPY(newnode
, oldnode
, Const
);
1521 /* XXX we don't bother with datumCopy; should we? */
1522 return (Node
*) newnode
;
1526 case T_CoerceToDomainValue
:
1527 case T_CaseTestExpr
:
1528 case T_SetToDefault
:
1529 case T_CurrentOfExpr
:
1531 return (Node
*) copyObject(node
);
1534 Aggref
*aggref
= (Aggref
*) node
;
1537 FLATCOPY(newnode
, aggref
, Aggref
);
1538 MUTATE(newnode
->args
, aggref
->args
, List
*);
1539 return (Node
*) newnode
;
1544 ArrayRef
*arrayref
= (ArrayRef
*) node
;
1547 FLATCOPY(newnode
, arrayref
, ArrayRef
);
1548 MUTATE(newnode
->refupperindexpr
, arrayref
->refupperindexpr
,
1550 MUTATE(newnode
->reflowerindexpr
, arrayref
->reflowerindexpr
,
1552 MUTATE(newnode
->refexpr
, arrayref
->refexpr
,
1554 MUTATE(newnode
->refassgnexpr
, arrayref
->refassgnexpr
,
1556 return (Node
*) newnode
;
1561 FuncExpr
*expr
= (FuncExpr
*) node
;
1564 FLATCOPY(newnode
, expr
, FuncExpr
);
1565 MUTATE(newnode
->args
, expr
->args
, List
*);
1566 return (Node
*) newnode
;
1571 OpExpr
*expr
= (OpExpr
*) node
;
1574 FLATCOPY(newnode
, expr
, OpExpr
);
1575 MUTATE(newnode
->args
, expr
->args
, List
*);
1576 return (Node
*) newnode
;
1579 case T_DistinctExpr
:
1581 DistinctExpr
*expr
= (DistinctExpr
*) node
;
1582 DistinctExpr
*newnode
;
1584 FLATCOPY(newnode
, expr
, DistinctExpr
);
1585 MUTATE(newnode
->args
, expr
->args
, List
*);
1586 return (Node
*) newnode
;
1589 case T_ScalarArrayOpExpr
:
1591 ScalarArrayOpExpr
*expr
= (ScalarArrayOpExpr
*) node
;
1592 ScalarArrayOpExpr
*newnode
;
1594 FLATCOPY(newnode
, expr
, ScalarArrayOpExpr
);
1595 MUTATE(newnode
->args
, expr
->args
, List
*);
1596 return (Node
*) newnode
;
1601 BoolExpr
*expr
= (BoolExpr
*) node
;
1604 FLATCOPY(newnode
, expr
, BoolExpr
);
1605 MUTATE(newnode
->args
, expr
->args
, List
*);
1606 return (Node
*) newnode
;
1611 SubLink
*sublink
= (SubLink
*) node
;
1614 FLATCOPY(newnode
, sublink
, SubLink
);
1615 MUTATE(newnode
->testexpr
, sublink
->testexpr
, Node
*);
1618 * Also invoke the mutator on the sublink's Query node, so it
1619 * can recurse into the sub-query if it wants to.
1621 MUTATE(newnode
->subselect
, sublink
->subselect
, Node
*);
1622 return (Node
*) newnode
;
1627 SubPlan
*subplan
= (SubPlan
*) node
;
1630 FLATCOPY(newnode
, subplan
, SubPlan
);
1631 /* transform testexpr */
1632 MUTATE(newnode
->testexpr
, subplan
->testexpr
, Node
*);
1633 /* transform args list (params to be passed to subplan) */
1634 MUTATE(newnode
->args
, subplan
->args
, List
*);
1635 /* but not the sub-Plan itself, which is referenced as-is */
1636 return (Node
*) newnode
;
1639 case T_AlternativeSubPlan
:
1641 AlternativeSubPlan
*asplan
= (AlternativeSubPlan
*) node
;
1642 AlternativeSubPlan
*newnode
;
1644 FLATCOPY(newnode
, asplan
, AlternativeSubPlan
);
1645 MUTATE(newnode
->subplans
, asplan
->subplans
, List
*);
1646 return (Node
*) newnode
;
1651 FieldSelect
*fselect
= (FieldSelect
*) node
;
1652 FieldSelect
*newnode
;
1654 FLATCOPY(newnode
, fselect
, FieldSelect
);
1655 MUTATE(newnode
->arg
, fselect
->arg
, Expr
*);
1656 return (Node
*) newnode
;
1661 FieldStore
*fstore
= (FieldStore
*) node
;
1662 FieldStore
*newnode
;
1664 FLATCOPY(newnode
, fstore
, FieldStore
);
1665 MUTATE(newnode
->arg
, fstore
->arg
, Expr
*);
1666 MUTATE(newnode
->newvals
, fstore
->newvals
, List
*);
1667 newnode
->fieldnums
= list_copy(fstore
->fieldnums
);
1668 return (Node
*) newnode
;
1673 RelabelType
*relabel
= (RelabelType
*) node
;
1674 RelabelType
*newnode
;
1676 FLATCOPY(newnode
, relabel
, RelabelType
);
1677 MUTATE(newnode
->arg
, relabel
->arg
, Expr
*);
1678 return (Node
*) newnode
;
1683 CoerceViaIO
*iocoerce
= (CoerceViaIO
*) node
;
1684 CoerceViaIO
*newnode
;
1686 FLATCOPY(newnode
, iocoerce
, CoerceViaIO
);
1687 MUTATE(newnode
->arg
, iocoerce
->arg
, Expr
*);
1688 return (Node
*) newnode
;
1691 case T_ArrayCoerceExpr
:
1693 ArrayCoerceExpr
*acoerce
= (ArrayCoerceExpr
*) node
;
1694 ArrayCoerceExpr
*newnode
;
1696 FLATCOPY(newnode
, acoerce
, ArrayCoerceExpr
);
1697 MUTATE(newnode
->arg
, acoerce
->arg
, Expr
*);
1698 return (Node
*) newnode
;
1701 case T_ConvertRowtypeExpr
:
1703 ConvertRowtypeExpr
*convexpr
= (ConvertRowtypeExpr
*) node
;
1704 ConvertRowtypeExpr
*newnode
;
1706 FLATCOPY(newnode
, convexpr
, ConvertRowtypeExpr
);
1707 MUTATE(newnode
->arg
, convexpr
->arg
, Expr
*);
1708 return (Node
*) newnode
;
1713 CaseExpr
*caseexpr
= (CaseExpr
*) node
;
1716 FLATCOPY(newnode
, caseexpr
, CaseExpr
);
1717 MUTATE(newnode
->arg
, caseexpr
->arg
, Expr
*);
1718 MUTATE(newnode
->args
, caseexpr
->args
, List
*);
1719 MUTATE(newnode
->defresult
, caseexpr
->defresult
, Expr
*);
1720 return (Node
*) newnode
;
1725 CaseWhen
*casewhen
= (CaseWhen
*) node
;
1728 FLATCOPY(newnode
, casewhen
, CaseWhen
);
1729 MUTATE(newnode
->expr
, casewhen
->expr
, Expr
*);
1730 MUTATE(newnode
->result
, casewhen
->result
, Expr
*);
1731 return (Node
*) newnode
;
1736 ArrayExpr
*arrayexpr
= (ArrayExpr
*) node
;
1739 FLATCOPY(newnode
, arrayexpr
, ArrayExpr
);
1740 MUTATE(newnode
->elements
, arrayexpr
->elements
, List
*);
1741 return (Node
*) newnode
;
1746 RowExpr
*rowexpr
= (RowExpr
*) node
;
1749 FLATCOPY(newnode
, rowexpr
, RowExpr
);
1750 MUTATE(newnode
->args
, rowexpr
->args
, List
*);
1751 /* Assume colnames needn't be duplicated */
1752 return (Node
*) newnode
;
1755 case T_RowCompareExpr
:
1757 RowCompareExpr
*rcexpr
= (RowCompareExpr
*) node
;
1758 RowCompareExpr
*newnode
;
1760 FLATCOPY(newnode
, rcexpr
, RowCompareExpr
);
1761 MUTATE(newnode
->largs
, rcexpr
->largs
, List
*);
1762 MUTATE(newnode
->rargs
, rcexpr
->rargs
, List
*);
1763 return (Node
*) newnode
;
1766 case T_CoalesceExpr
:
1768 CoalesceExpr
*coalesceexpr
= (CoalesceExpr
*) node
;
1769 CoalesceExpr
*newnode
;
1771 FLATCOPY(newnode
, coalesceexpr
, CoalesceExpr
);
1772 MUTATE(newnode
->args
, coalesceexpr
->args
, List
*);
1773 return (Node
*) newnode
;
1778 MinMaxExpr
*minmaxexpr
= (MinMaxExpr
*) node
;
1779 MinMaxExpr
*newnode
;
1781 FLATCOPY(newnode
, minmaxexpr
, MinMaxExpr
);
1782 MUTATE(newnode
->args
, minmaxexpr
->args
, List
*);
1783 return (Node
*) newnode
;
1788 XmlExpr
*xexpr
= (XmlExpr
*) node
;
1791 FLATCOPY(newnode
, xexpr
, XmlExpr
);
1792 MUTATE(newnode
->named_args
, xexpr
->named_args
, List
*);
1793 /* assume mutator does not care about arg_names */
1794 MUTATE(newnode
->args
, xexpr
->args
, List
*);
1795 return (Node
*) newnode
;
1800 NullIfExpr
*expr
= (NullIfExpr
*) node
;
1801 NullIfExpr
*newnode
;
1803 FLATCOPY(newnode
, expr
, NullIfExpr
);
1804 MUTATE(newnode
->args
, expr
->args
, List
*);
1805 return (Node
*) newnode
;
1810 NullTest
*ntest
= (NullTest
*) node
;
1813 FLATCOPY(newnode
, ntest
, NullTest
);
1814 MUTATE(newnode
->arg
, ntest
->arg
, Expr
*);
1815 return (Node
*) newnode
;
1820 BooleanTest
*btest
= (BooleanTest
*) node
;
1821 BooleanTest
*newnode
;
1823 FLATCOPY(newnode
, btest
, BooleanTest
);
1824 MUTATE(newnode
->arg
, btest
->arg
, Expr
*);
1825 return (Node
*) newnode
;
1828 case T_CoerceToDomain
:
1830 CoerceToDomain
*ctest
= (CoerceToDomain
*) node
;
1831 CoerceToDomain
*newnode
;
1833 FLATCOPY(newnode
, ctest
, CoerceToDomain
);
1834 MUTATE(newnode
->arg
, ctest
->arg
, Expr
*);
1835 return (Node
*) newnode
;
1840 TargetEntry
*targetentry
= (TargetEntry
*) node
;
1841 TargetEntry
*newnode
;
1843 FLATCOPY(newnode
, targetentry
, TargetEntry
);
1844 MUTATE(newnode
->expr
, targetentry
->expr
, Expr
*);
1845 return (Node
*) newnode
;
1849 /* Do nothing with a sub-Query, per discussion above */
1851 case T_CommonTableExpr
:
1853 CommonTableExpr
*cte
= (CommonTableExpr
*) node
;
1854 CommonTableExpr
*newnode
;
1856 FLATCOPY(newnode
, cte
, CommonTableExpr
);
1859 * Also invoke the mutator on the CTE's Query node, so it
1860 * can recurse into the sub-query if it wants to.
1862 MUTATE(newnode
->ctequery
, cte
->ctequery
, Node
*);
1863 return (Node
*) newnode
;
1869 * We assume the mutator isn't interested in the list nodes
1870 * per se, so just invoke it on each list element. NOTE: this
1871 * would fail badly on a list with integer elements!
1877 foreach(temp
, (List
*) node
)
1879 resultlist
= lappend(resultlist
,
1880 mutator((Node
*) lfirst(temp
),
1883 return (Node
*) resultlist
;
1888 FromExpr
*from
= (FromExpr
*) node
;
1891 FLATCOPY(newnode
, from
, FromExpr
);
1892 MUTATE(newnode
->fromlist
, from
->fromlist
, List
*);
1893 MUTATE(newnode
->quals
, from
->quals
, Node
*);
1894 return (Node
*) newnode
;
1899 JoinExpr
*join
= (JoinExpr
*) node
;
1902 FLATCOPY(newnode
, join
, JoinExpr
);
1903 MUTATE(newnode
->larg
, join
->larg
, Node
*);
1904 MUTATE(newnode
->rarg
, join
->rarg
, Node
*);
1905 MUTATE(newnode
->quals
, join
->quals
, Node
*);
1906 /* We do not mutate alias or using by default */
1907 return (Node
*) newnode
;
1910 case T_SetOperationStmt
:
1912 SetOperationStmt
*setop
= (SetOperationStmt
*) node
;
1913 SetOperationStmt
*newnode
;
1915 FLATCOPY(newnode
, setop
, SetOperationStmt
);
1916 MUTATE(newnode
->larg
, setop
->larg
, Node
*);
1917 MUTATE(newnode
->rarg
, setop
->rarg
, Node
*);
1918 /* We do not mutate groupClauses by default */
1919 return (Node
*) newnode
;
1922 case T_FlattenedSubLink
:
1924 FlattenedSubLink
*fslink
= (FlattenedSubLink
*) node
;
1925 FlattenedSubLink
*newnode
;
1927 FLATCOPY(newnode
, fslink
, FlattenedSubLink
);
1928 /* Assume we need not copy the relids bitmapsets */
1929 MUTATE(newnode
->quals
, fslink
->quals
, Expr
*);
1930 return (Node
*) newnode
;
1933 case T_PlaceHolderVar
:
1935 PlaceHolderVar
*phv
= (PlaceHolderVar
*) node
;
1936 PlaceHolderVar
*newnode
;
1938 FLATCOPY(newnode
, phv
, PlaceHolderVar
);
1939 MUTATE(newnode
->phexpr
, phv
->phexpr
, Expr
*);
1940 /* Assume we need not copy the relids bitmapset */
1941 return (Node
*) newnode
;
1944 case T_AppendRelInfo
:
1946 AppendRelInfo
*appinfo
= (AppendRelInfo
*) node
;
1947 AppendRelInfo
*newnode
;
1949 FLATCOPY(newnode
, appinfo
, AppendRelInfo
);
1950 MUTATE(newnode
->translated_vars
, appinfo
->translated_vars
, List
*);
1951 return (Node
*) newnode
;
1954 case T_PlaceHolderInfo
:
1956 PlaceHolderInfo
*phinfo
= (PlaceHolderInfo
*) node
;
1957 PlaceHolderInfo
*newnode
;
1959 FLATCOPY(newnode
, phinfo
, PlaceHolderInfo
);
1960 MUTATE(newnode
->ph_var
, phinfo
->ph_var
, PlaceHolderVar
*);
1961 /* Assume we need not copy the relids bitmapsets */
1962 return (Node
*) newnode
;
1966 elog(ERROR
, "unrecognized node type: %d",
1967 (int) nodeTag(node
));
1970 /* can't get here, but keep compiler happy */
1976 * query_tree_mutator --- initiate modification of a Query's expressions
1978 * This routine exists just to reduce the number of places that need to know
1979 * where all the expression subtrees of a Query are. Note it can be used
1980 * for starting a walk at top level of a Query regardless of whether the
1981 * mutator intends to descend into subqueries. It is also useful for
1982 * descending into subqueries within a mutator.
1984 * Some callers want to suppress mutating of certain items in the Query,
1985 * typically because they need to process them specially, or don't actually
1986 * want to recurse into subqueries. This is supported by the flags argument,
1987 * which is the bitwise OR of flag values to suppress mutating of
1988 * indicated items. (More flag bits may be added as needed.)
1990 * Normally the Query node itself is copied, but some callers want it to be
1991 * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags. All
1992 * modified substructure is safely copied in any case.
1995 query_tree_mutator(Query
*query
,
1996 Node
*(*mutator
) (),
2000 Assert(query
!= NULL
&& IsA(query
, Query
));
2002 if (!(flags
& QTW_DONT_COPY_QUERY
))
2006 FLATCOPY(newquery
, query
, Query
);
2010 MUTATE(query
->targetList
, query
->targetList
, List
*);
2011 MUTATE(query
->returningList
, query
->returningList
, List
*);
2012 MUTATE(query
->jointree
, query
->jointree
, FromExpr
*);
2013 MUTATE(query
->setOperations
, query
->setOperations
, Node
*);
2014 MUTATE(query
->havingQual
, query
->havingQual
, Node
*);
2015 MUTATE(query
->limitOffset
, query
->limitOffset
, Node
*);
2016 MUTATE(query
->limitCount
, query
->limitCount
, Node
*);
2017 if (!(flags
& QTW_IGNORE_CTE_SUBQUERIES
))
2018 MUTATE(query
->cteList
, query
->cteList
, List
*);
2019 else /* else copy CTE list as-is */
2020 query
->cteList
= copyObject(query
->cteList
);
2021 query
->rtable
= range_table_mutator(query
->rtable
,
2022 mutator
, context
, flags
);
2027 * range_table_mutator is just the part of query_tree_mutator that processes
2028 * a query's rangetable. This is split out since it can be useful on
2032 range_table_mutator(List
*rtable
,
2033 Node
*(*mutator
) (),
2042 RangeTblEntry
*rte
= (RangeTblEntry
*) lfirst(rt
);
2043 RangeTblEntry
*newrte
;
2045 FLATCOPY(newrte
, rte
, RangeTblEntry
);
2046 switch (rte
->rtekind
)
2051 /* we don't bother to copy eref, aliases, etc; OK? */
2054 if (!(flags
& QTW_IGNORE_RT_SUBQUERIES
))
2056 CHECKFLATCOPY(newrte
->subquery
, rte
->subquery
, Query
);
2057 MUTATE(newrte
->subquery
, newrte
->subquery
, Query
*);
2061 /* else, copy RT subqueries as-is */
2062 newrte
->subquery
= copyObject(rte
->subquery
);
2066 if (!(flags
& QTW_IGNORE_JOINALIASES
))
2067 MUTATE(newrte
->joinaliasvars
, rte
->joinaliasvars
, List
*);
2070 /* else, copy join aliases as-is */
2071 newrte
->joinaliasvars
= copyObject(rte
->joinaliasvars
);
2075 MUTATE(newrte
->funcexpr
, rte
->funcexpr
, Node
*);
2078 MUTATE(newrte
->values_lists
, rte
->values_lists
, List
*);
2081 newrt
= lappend(newrt
, newrte
);
2087 * query_or_expression_tree_walker --- hybrid form
2089 * This routine will invoke query_tree_walker if called on a Query node,
2090 * else will invoke the walker directly. This is a useful way of starting
2091 * the recursion when the walker's normal change of state is not appropriate
2092 * for the outermost Query node.
2095 query_or_expression_tree_walker(Node
*node
,
2100 if (node
&& IsA(node
, Query
))
2101 return query_tree_walker((Query
*) node
,
2106 return walker(node
, context
);
2110 * query_or_expression_tree_mutator --- hybrid form
2112 * This routine will invoke query_tree_mutator if called on a Query node,
2113 * else will invoke the mutator directly. This is a useful way of starting
2114 * the recursion when the mutator's normal change of state is not appropriate
2115 * for the outermost Query node.
2118 query_or_expression_tree_mutator(Node
*node
,
2119 Node
*(*mutator
) (),
2123 if (node
&& IsA(node
, Query
))
2124 return (Node
*) query_tree_mutator((Query
*) node
,
2129 return mutator(node
, context
);
2134 * raw_expression_tree_walker --- walk raw parse trees
2136 * This has exactly the same API as expression_tree_walker, but instead of
2137 * walking post-analysis parse trees, it knows how to walk the node types
2138 * found in raw grammar output. (There is not currently any need for a
2139 * combined walker, so we keep them separate in the name of efficiency.)
2140 * Unlike expression_tree_walker, there is no special rule about query
2141 * boundaries: we descend to everything that's possibly interesting.
2143 * Currently, the node type coverage extends to SelectStmt and everything
2144 * that could appear under it, but not other statement types.
2147 raw_expression_tree_walker(Node
*node
, bool (*walker
) (), void *context
)
2152 * The walker has already visited the current node, and so we need only
2153 * recurse into any sub-nodes it has.
2158 /* Guard against stack overflow due to overly complex expressions */
2159 check_stack_depth();
2161 switch (nodeTag(node
))
2163 case T_SetToDefault
:
2164 case T_CurrentOfExpr
:
2173 /* primitive node types with no subnodes */
2176 /* we assume the colnames list isn't interesting */
2179 return walker(((RangeVar
*) node
)->alias
, context
);
2182 SubLink
*sublink
= (SubLink
*) node
;
2184 if (walker(sublink
->testexpr
, context
))
2186 /* we assume the operName is not interesting */
2187 if (walker(sublink
->subselect
, context
))
2193 CaseExpr
*caseexpr
= (CaseExpr
*) node
;
2195 if (walker(caseexpr
->arg
, context
))
2197 /* we assume walker doesn't care about CaseWhens, either */
2198 foreach(temp
, caseexpr
->args
)
2200 CaseWhen
*when
= (CaseWhen
*) lfirst(temp
);
2202 Assert(IsA(when
, CaseWhen
));
2203 if (walker(when
->expr
, context
))
2205 if (walker(when
->result
, context
))
2208 if (walker(caseexpr
->defresult
, context
))
2213 /* Assume colnames isn't interesting */
2214 return walker(((RowExpr
*) node
)->args
, context
);
2215 case T_CoalesceExpr
:
2216 return walker(((CoalesceExpr
*) node
)->args
, context
);
2218 return walker(((MinMaxExpr
*) node
)->args
, context
);
2221 XmlExpr
*xexpr
= (XmlExpr
*) node
;
2223 if (walker(xexpr
->named_args
, context
))
2225 /* we assume walker doesn't care about arg_names */
2226 if (walker(xexpr
->args
, context
))
2231 return walker(((NullTest
*) node
)->arg
, context
);
2233 return walker(((BooleanTest
*) node
)->arg
, context
);
2236 JoinExpr
*join
= (JoinExpr
*) node
;
2238 if (walker(join
->larg
, context
))
2240 if (walker(join
->rarg
, context
))
2242 if (walker(join
->quals
, context
))
2244 if (walker(join
->alias
, context
))
2246 /* using list is deemed uninteresting */
2251 IntoClause
*into
= (IntoClause
*) node
;
2253 if (walker(into
->rel
, context
))
2255 /* colNames, options are deemed uninteresting */
2259 foreach(temp
, (List
*) node
)
2261 if (walker((Node
*) lfirst(temp
), context
))
2267 SelectStmt
*stmt
= (SelectStmt
*) node
;
2269 if (walker(stmt
->distinctClause
, context
))
2271 if (walker(stmt
->intoClause
, context
))
2273 if (walker(stmt
->targetList
, context
))
2275 if (walker(stmt
->fromClause
, context
))
2277 if (walker(stmt
->whereClause
, context
))
2279 if (walker(stmt
->groupClause
, context
))
2281 if (walker(stmt
->havingClause
, context
))
2283 if (walker(stmt
->withClause
, context
))
2285 if (walker(stmt
->valuesLists
, context
))
2287 if (walker(stmt
->sortClause
, context
))
2289 if (walker(stmt
->limitOffset
, context
))
2291 if (walker(stmt
->limitCount
, context
))
2293 if (walker(stmt
->lockingClause
, context
))
2295 if (walker(stmt
->larg
, context
))
2297 if (walker(stmt
->rarg
, context
))
2303 A_Expr
*expr
= (A_Expr
*) node
;
2305 if (walker(expr
->lexpr
, context
))
2307 if (walker(expr
->rexpr
, context
))
2309 /* operator name is deemed uninteresting */
2313 /* we assume the fields contain nothing interesting */
2317 FuncCall
*fcall
= (FuncCall
*) node
;
2319 if (walker(fcall
->args
, context
))
2321 /* function name is deemed uninteresting */
2326 A_Indices
*indices
= (A_Indices
*) node
;
2328 if (walker(indices
->lidx
, context
))
2330 if (walker(indices
->uidx
, context
))
2334 case T_A_Indirection
:
2336 A_Indirection
*indir
= (A_Indirection
*) node
;
2338 if (walker(indir
->arg
, context
))
2340 if (walker(indir
->indirection
, context
))
2345 return walker(((A_ArrayExpr
*) node
)->elements
, context
);
2348 ResTarget
*rt
= (ResTarget
*) node
;
2350 if (walker(rt
->indirection
, context
))
2352 if (walker(rt
->val
, context
))
2358 TypeCast
*tc
= (TypeCast
*) node
;
2360 if (walker(tc
->arg
, context
))
2362 if (walker(tc
->typename
, context
))
2367 return walker(((SortBy
*) node
)->node
, context
);
2368 case T_RangeSubselect
:
2370 RangeSubselect
*rs
= (RangeSubselect
*) node
;
2372 if (walker(rs
->subquery
, context
))
2374 if (walker(rs
->alias
, context
))
2378 case T_RangeFunction
:
2380 RangeFunction
*rf
= (RangeFunction
*) node
;
2382 if (walker(rf
->funccallnode
, context
))
2384 if (walker(rf
->alias
, context
))
2390 TypeName
*tn
= (TypeName
*) node
;
2392 if (walker(tn
->typmods
, context
))
2394 if (walker(tn
->arrayBounds
, context
))
2396 /* type name itself is deemed uninteresting */
2401 ColumnDef
*coldef
= (ColumnDef
*) node
;
2403 if (walker(coldef
->typename
, context
))
2405 if (walker(coldef
->raw_default
, context
))
2407 /* for now, constraints are ignored */
2410 case T_LockingClause
:
2411 return walker(((LockingClause
*) node
)->lockedRels
, context
);
2412 case T_XmlSerialize
:
2414 XmlSerialize
*xs
= (XmlSerialize
*) node
;
2416 if (walker(xs
->expr
, context
))
2418 if (walker(xs
->typename
, context
))
2423 return walker(((WithClause
*) node
)->ctes
, context
);
2424 case T_CommonTableExpr
:
2425 return walker(((CommonTableExpr
*) node
)->ctequery
, context
);
2427 elog(ERROR
, "unrecognized node type: %d",
2428 (int) nodeTag(node
));