1 /*-------------------------------------------------------------------------
4 * routines to manipulate qualification clauses
6 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.235 2007/02/19 07:03:30 tgl Exp $
14 * AUTHOR DATE MAJOR EVENT
15 * Andrew Yu Nov 3, 1994 clause.c and clauses.c combined
17 *-------------------------------------------------------------------------
22 #include "catalog/pg_aggregate.h"
23 #include "catalog/pg_language.h"
24 #include "catalog/pg_operator.h"
25 #include "catalog/pg_proc.h"
26 #include "catalog/pg_type.h"
27 #include "executor/executor.h"
28 #include "executor/functions.h"
29 #include "miscadmin.h"
30 #include "nodes/makefuncs.h"
31 #include "optimizer/clauses.h"
32 #include "optimizer/cost.h"
33 #include "optimizer/planmain.h"
34 #include "optimizer/planner.h"
35 #include "optimizer/var.h"
36 #include "parser/analyze.h"
37 #include "parser/parse_clause.h"
38 #include "parser/parse_coerce.h"
39 #include "parser/parse_expr.h"
40 #include "tcop/tcopprot.h"
41 #include "utils/acl.h"
42 #include "utils/builtins.h"
43 #include "utils/datum.h"
44 #include "utils/lsyscache.h"
45 #include "utils/memutils.h"
46 #include "utils/syscache.h"
47 #include "utils/typcache.h"
52 ParamListInfo boundParams;
56 } eval_const_expressions_context;
63 } substitute_actual_parameters_context;
65 static bool contain_agg_clause_walker(Node *node, void *context);
66 static bool count_agg_clauses_walker(Node *node, AggClauseCounts *counts);
67 static bool expression_returns_set_walker(Node *node, void *context);
68 static bool expression_returns_set_rows_walker(Node *node, double *count);
69 static bool contain_subplans_walker(Node *node, void *context);
70 static bool contain_mutable_functions_walker(Node *node, void *context);
71 static bool contain_volatile_functions_walker(Node *node, void *context);
72 static bool contain_nonstrict_functions_walker(Node *node, void *context);
73 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
74 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
75 static bool set_coercionform_dontcare_walker(Node *node, void *context);
76 static Node *eval_const_expressions_mutator(Node *node,
77 eval_const_expressions_context *context);
78 static List *simplify_or_arguments(List *args,
79 eval_const_expressions_context *context,
80 bool *haveNull, bool *forceTrue);
81 static List *simplify_and_arguments(List *args,
82 eval_const_expressions_context *context,
83 bool *haveNull, bool *forceFalse);
84 static Expr *simplify_boolean_equality(List *args);
85 static Expr *simplify_function(Oid funcid, Oid result_type, List *args,
87 eval_const_expressions_context *context);
88 static Expr *evaluate_function(Oid funcid, Oid result_type, List *args,
90 eval_const_expressions_context *context);
91 static Expr *inline_function(Oid funcid, Oid result_type, List *args,
93 eval_const_expressions_context *context);
94 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
96 static Node *substitute_actual_parameters_mutator(Node *node,
97 substitute_actual_parameters_context *context);
98 static void sql_inline_error_callback(void *arg);
99 static Expr *evaluate_expr(Expr *expr, Oid result_type);
102 /*****************************************************************************
103 * OPERATOR clause functions
104 *****************************************************************************/
108 * Creates an operator clause given its operator info, left operand,
109 * and right operand (pass NULL to create single-operand clause).
112 make_opclause(Oid opno, Oid opresulttype, bool opretset,
113 Expr *leftop, Expr *rightop)
115 OpExpr *expr = makeNode(OpExpr);
118 expr->opfuncid = InvalidOid;
119 expr->opresulttype = opresulttype;
120 expr->opretset = opretset;
122 expr->args = list_make2(leftop, rightop);
124 expr->args = list_make1(leftop);
125 return (Expr *) expr;
131 * Returns the left operand of a clause of the form (op expr expr)
135 get_leftop(Expr *clause)
137 OpExpr *expr = (OpExpr *) clause;
139 if (expr->args != NIL)
140 return linitial(expr->args);
148 * Returns the right operand in a clause of the form (op expr expr).
149 * NB: result will be NULL if applied to a unary op clause.
152 get_rightop(Expr *clause)
154 OpExpr *expr = (OpExpr *) clause;
156 if (list_length(expr->args) >= 2)
157 return lsecond(expr->args);
162 /*****************************************************************************
163 * NOT clause functions
164 *****************************************************************************/
169 * Returns t iff this is a 'not' clause: (NOT expr).
172 not_clause(Node *clause)
174 return (clause != NULL &&
175 IsA(clause, BoolExpr) &&
176 ((BoolExpr *) clause)->boolop == NOT_EXPR);
182 * Create a 'not' clause given the expression to be negated.
185 make_notclause(Expr *notclause)
187 BoolExpr *expr = makeNode(BoolExpr);
189 expr->boolop = NOT_EXPR;
190 expr->args = list_make1(notclause);
191 return (Expr *) expr;
197 * Retrieve the clause within a 'not' clause
200 get_notclausearg(Expr *notclause)
202 return linitial(((BoolExpr *) notclause)->args);
205 /*****************************************************************************
206 * OR clause functions
207 *****************************************************************************/
212 * Returns t iff the clause is an 'or' clause: (OR { expr }).
215 or_clause(Node *clause)
217 return (clause != NULL &&
218 IsA(clause, BoolExpr) &&
219 ((BoolExpr *) clause)->boolop == OR_EXPR);
225 * Creates an 'or' clause given a list of its subclauses.
228 make_orclause(List *orclauses)
230 BoolExpr *expr = makeNode(BoolExpr);
232 expr->boolop = OR_EXPR;
233 expr->args = orclauses;
234 return (Expr *) expr;
237 /*****************************************************************************
238 * AND clause functions
239 *****************************************************************************/
245 * Returns t iff its argument is an 'and' clause: (AND { expr }).
248 and_clause(Node *clause)
250 return (clause != NULL &&
251 IsA(clause, BoolExpr) &&
252 ((BoolExpr *) clause)->boolop == AND_EXPR);
258 * Creates an 'and' clause given a list of its subclauses.
261 make_andclause(List *andclauses)
263 BoolExpr *expr = makeNode(BoolExpr);
265 expr->boolop = AND_EXPR;
266 expr->args = andclauses;
267 return (Expr *) expr;
273 * Variant of make_andclause for ANDing two qual conditions together.
274 * Qual conditions have the property that a NULL nodetree is interpreted
277 * NB: this makes no attempt to preserve AND/OR flatness; so it should not
278 * be used on a qual that has already been run through prepqual.c.
281 make_and_qual(Node *qual1, Node *qual2)
287 return (Node *) make_andclause(list_make2(qual1, qual2));
291 * Sometimes (such as in the input of ExecQual), we use lists of expression
292 * nodes with implicit AND semantics.
294 * These functions convert between an AND-semantics expression list and the
295 * ordinary representation of a boolean expression.
297 * Note that an empty list is considered equivalent to TRUE.
300 make_ands_explicit(List *andclauses)
302 if (andclauses == NIL)
303 return (Expr *) makeBoolConst(true, false);
304 else if (list_length(andclauses) == 1)
305 return (Expr *) linitial(andclauses);
307 return make_andclause(andclauses);
311 make_ands_implicit(Expr *clause)
314 * NB: because the parser sets the qual field to NULL in a query that has
315 * no WHERE clause, we must consider a NULL input clause as TRUE, even
316 * though one might more reasonably think it FALSE. Grumble. If this
317 * causes trouble, consider changing the parser's behavior.
320 return NIL; /* NULL -> NIL list == TRUE */
321 else if (and_clause((Node *) clause))
322 return ((BoolExpr *) clause)->args;
323 else if (IsA(clause, Const) &&
324 !((Const *) clause)->constisnull &&
325 DatumGetBool(((Const *) clause)->constvalue))
326 return NIL; /* constant TRUE input -> NIL list */
328 return list_make1(clause);
332 /*****************************************************************************
333 * Aggregate-function clause manipulation
334 *****************************************************************************/
338 * Recursively search for Aggref nodes within a clause.
340 * Returns true if any aggregate found.
342 * This does not descend into subqueries, and so should be used only after
343 * reduction of sublinks to subplans, or in contexts where it's known there
344 * are no subqueries. There mustn't be outer-aggregate references either.
346 * (If you want something like this but able to deal with subqueries,
347 * see rewriteManip.c's checkExprHasAggs().)
350 contain_agg_clause(Node *clause)
352 return contain_agg_clause_walker(clause, NULL);
356 contain_agg_clause_walker(Node *node, void *context)
360 if (IsA(node, Aggref))
362 Assert(((Aggref *) node)->agglevelsup == 0);
363 return true; /* abort the tree traversal and return true */
365 Assert(!IsA(node, SubLink));
366 return expression_tree_walker(node, contain_agg_clause_walker, context);
371 * Recursively count the Aggref nodes in an expression tree.
373 * Note: this also checks for nested aggregates, which are an error.
375 * We not only count the nodes, but attempt to estimate the total space
376 * needed for their transition state values if all are evaluated in parallel
377 * (as would be done in a HashAgg plan). See AggClauseCounts for the exact
378 * set of statistics returned.
380 * NOTE that the counts are ADDED to those already in *counts ... so the
381 * caller is responsible for zeroing the struct initially.
383 * This does not descend into subqueries, and so should be used only after
384 * reduction of sublinks to subplans, or in contexts where it's known there
385 * are no subqueries. There mustn't be outer-aggregate references either.
388 count_agg_clauses(Node *clause, AggClauseCounts *counts)
390 /* no setup needed */
391 count_agg_clauses_walker(clause, counts);
395 count_agg_clauses_walker(Node *node, AggClauseCounts *counts)
399 if (IsA(node, Aggref))
401 Aggref *aggref = (Aggref *) node;
405 Form_pg_aggregate aggform;
410 Assert(aggref->agglevelsup == 0);
412 if (aggref->aggdistinct)
413 counts->numDistinctAggs++;
415 /* extract argument types */
416 numArguments = list_length(aggref->args);
417 inputTypes = (Oid *) palloc(sizeof(Oid) * numArguments);
419 foreach(l, aggref->args)
421 inputTypes[i++] = exprType((Node *) lfirst(l));
424 /* fetch aggregate transition datatype from pg_aggregate */
425 aggTuple = SearchSysCache(AGGFNOID,
426 ObjectIdGetDatum(aggref->aggfnoid),
428 if (!HeapTupleIsValid(aggTuple))
429 elog(ERROR, "cache lookup failed for aggregate %u",
431 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
432 aggtranstype = aggform->aggtranstype;
433 ReleaseSysCache(aggTuple);
435 /* resolve actual type of transition state, if polymorphic */
436 if (aggtranstype == ANYARRAYOID || aggtranstype == ANYELEMENTOID)
438 /* have to fetch the agg's declared input types... */
439 Oid *declaredArgTypes;
442 (void) get_func_signature(aggref->aggfnoid,
443 &declaredArgTypes, &agg_nargs);
444 Assert(agg_nargs == numArguments);
445 aggtranstype = enforce_generic_type_consistency(inputTypes,
449 pfree(declaredArgTypes);
453 * If the transition type is pass-by-value then it doesn't add
454 * anything to the required size of the hashtable. If it is
455 * pass-by-reference then we have to add the estimated size of the
456 * value itself, plus palloc overhead.
458 if (!get_typbyval(aggtranstype))
460 int32 aggtranstypmod;
464 * If transition state is of same type as first input, assume it's
465 * the same typmod (same width) as well. This works for cases
466 * like MAX/MIN and is probably somewhat reasonable otherwise.
468 if (numArguments > 0 && aggtranstype == inputTypes[0])
469 aggtranstypmod = exprTypmod((Node *) linitial(aggref->args));
473 avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
474 avgwidth = MAXALIGN(avgwidth);
476 counts->transitionSpace += avgwidth + 2 * sizeof(void *);
480 * Complain if the aggregate's arguments contain any aggregates;
481 * nested agg functions are semantically nonsensical.
483 if (contain_agg_clause((Node *) aggref->args))
485 (errcode(ERRCODE_GROUPING_ERROR),
486 errmsg("aggregate function calls cannot be nested")));
489 * Having checked that, we need not recurse into the argument.
493 Assert(!IsA(node, SubLink));
494 return expression_tree_walker(node, count_agg_clauses_walker,
499 /*****************************************************************************
500 * Support for expressions returning sets
501 *****************************************************************************/
504 * expression_returns_set
505 * Test whether an expression returns a set result.
507 * Because we use expression_tree_walker(), this can also be applied to
508 * whole targetlists; it'll produce TRUE if any one of the tlist items
512 expression_returns_set(Node *clause)
514 return expression_returns_set_walker(clause, NULL);
518 expression_returns_set_walker(Node *node, void *context)
522 if (IsA(node, FuncExpr))
524 FuncExpr *expr = (FuncExpr *) node;
526 if (expr->funcretset)
528 /* else fall through to check args */
530 if (IsA(node, OpExpr))
532 OpExpr *expr = (OpExpr *) node;
536 /* else fall through to check args */
539 /* Avoid recursion for some cases that can't return a set */
540 if (IsA(node, Aggref))
542 if (IsA(node, DistinctExpr))
544 if (IsA(node, ScalarArrayOpExpr))
546 if (IsA(node, BoolExpr))
548 if (IsA(node, SubLink))
550 if (IsA(node, SubPlan))
552 if (IsA(node, ArrayExpr))
554 if (IsA(node, RowExpr))
556 if (IsA(node, RowCompareExpr))
558 if (IsA(node, CoalesceExpr))
560 if (IsA(node, MinMaxExpr))
562 if (IsA(node, XmlExpr))
564 if (IsA(node, NullIfExpr))
567 return expression_tree_walker(node, expression_returns_set_walker,
572 * expression_returns_set_rows
573 * Estimate the number of rows in a set result.
575 * We use the product of the rowcount estimates of all the functions in
576 * the given tree. The result is 1 if there are no set-returning functions.
579 expression_returns_set_rows(Node *clause)
583 (void) expression_returns_set_rows_walker(clause, &result);
588 expression_returns_set_rows_walker(Node *node, double *count)
592 if (IsA(node, FuncExpr))
594 FuncExpr *expr = (FuncExpr *) node;
596 if (expr->funcretset)
597 *count *= get_func_rows(expr->funcid);
599 if (IsA(node, OpExpr))
601 OpExpr *expr = (OpExpr *) node;
606 *count *= get_func_rows(expr->opfuncid);
610 /* Avoid recursion for some cases that can't return a set */
611 if (IsA(node, Aggref))
613 if (IsA(node, DistinctExpr))
615 if (IsA(node, ScalarArrayOpExpr))
617 if (IsA(node, BoolExpr))
619 if (IsA(node, SubLink))
621 if (IsA(node, SubPlan))
623 if (IsA(node, ArrayExpr))
625 if (IsA(node, RowExpr))
627 if (IsA(node, RowCompareExpr))
629 if (IsA(node, CoalesceExpr))
631 if (IsA(node, MinMaxExpr))
633 if (IsA(node, XmlExpr))
635 if (IsA(node, NullIfExpr))
638 return expression_tree_walker(node, expression_returns_set_rows_walker,
643 /*****************************************************************************
644 * Subplan clause manipulation
645 *****************************************************************************/
649 * Recursively search for subplan nodes within a clause.
651 * If we see a SubLink node, we will return TRUE. This is only possible if
652 * the expression tree hasn't yet been transformed by subselect.c. We do not
653 * know whether the node will produce a true subplan or just an initplan,
654 * but we make the conservative assumption that it will be a subplan.
656 * Returns true if any subplan found.
659 contain_subplans(Node *clause)
661 return contain_subplans_walker(clause, NULL);
665 contain_subplans_walker(Node *node, void *context)
669 if (IsA(node, SubPlan) ||
671 return true; /* abort the tree traversal and return true */
672 return expression_tree_walker(node, contain_subplans_walker, context);
676 /*****************************************************************************
677 * Check clauses for mutable functions
678 *****************************************************************************/
681 * contain_mutable_functions
682 * Recursively search for mutable functions within a clause.
684 * Returns true if any mutable function (or operator implemented by a
685 * mutable function) is found. This test is needed so that we don't
686 * mistakenly think that something like "WHERE random() < 0.5" can be treated
687 * as a constant qualification.
689 * XXX we do not examine sub-selects to see if they contain uses of
690 * mutable functions. It's not real clear if that is correct or not...
693 contain_mutable_functions(Node *clause)
695 return contain_mutable_functions_walker(clause, NULL);
699 contain_mutable_functions_walker(Node *node, void *context)
703 if (IsA(node, FuncExpr))
705 FuncExpr *expr = (FuncExpr *) node;
707 if (func_volatile(expr->funcid) != PROVOLATILE_IMMUTABLE)
709 /* else fall through to check args */
711 if (IsA(node, OpExpr))
713 OpExpr *expr = (OpExpr *) node;
715 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
717 /* else fall through to check args */
719 if (IsA(node, DistinctExpr))
721 DistinctExpr *expr = (DistinctExpr *) node;
723 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
725 /* else fall through to check args */
727 if (IsA(node, ScalarArrayOpExpr))
729 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
731 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
733 /* else fall through to check args */
735 if (IsA(node, NullIfExpr))
737 NullIfExpr *expr = (NullIfExpr *) node;
739 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
741 /* else fall through to check args */
743 if (IsA(node, RowCompareExpr))
745 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
748 foreach(opid, rcexpr->opnos)
750 if (op_volatile(lfirst_oid(opid)) != PROVOLATILE_IMMUTABLE)
753 /* else fall through to check args */
755 return expression_tree_walker(node, contain_mutable_functions_walker,
760 /*****************************************************************************
761 * Check clauses for volatile functions
762 *****************************************************************************/
765 * contain_volatile_functions
766 * Recursively search for volatile functions within a clause.
768 * Returns true if any volatile function (or operator implemented by a
769 * volatile function) is found. This test prevents invalid conversions
770 * of volatile expressions into indexscan quals.
772 * XXX we do not examine sub-selects to see if they contain uses of
773 * volatile functions. It's not real clear if that is correct or not...
776 contain_volatile_functions(Node *clause)
778 return contain_volatile_functions_walker(clause, NULL);
782 contain_volatile_functions_walker(Node *node, void *context)
786 if (IsA(node, FuncExpr))
788 FuncExpr *expr = (FuncExpr *) node;
790 if (func_volatile(expr->funcid) == PROVOLATILE_VOLATILE)
792 /* else fall through to check args */
794 if (IsA(node, OpExpr))
796 OpExpr *expr = (OpExpr *) node;
798 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
800 /* else fall through to check args */
802 if (IsA(node, DistinctExpr))
804 DistinctExpr *expr = (DistinctExpr *) node;
806 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
808 /* else fall through to check args */
810 if (IsA(node, ScalarArrayOpExpr))
812 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
814 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
816 /* else fall through to check args */
818 if (IsA(node, NullIfExpr))
820 NullIfExpr *expr = (NullIfExpr *) node;
822 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
824 /* else fall through to check args */
826 if (IsA(node, RowCompareExpr))
828 /* RowCompare probably can't have volatile ops, but check anyway */
829 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
832 foreach(opid, rcexpr->opnos)
834 if (op_volatile(lfirst_oid(opid)) == PROVOLATILE_VOLATILE)
837 /* else fall through to check args */
839 return expression_tree_walker(node, contain_volatile_functions_walker,
844 /*****************************************************************************
845 * Check clauses for nonstrict functions
846 *****************************************************************************/
849 * contain_nonstrict_functions
850 * Recursively search for nonstrict functions within a clause.
852 * Returns true if any nonstrict construct is found --- ie, anything that
853 * could produce non-NULL output with a NULL input.
855 * The idea here is that the caller has verified that the expression contains
856 * one or more Var or Param nodes (as appropriate for the caller's need), and
857 * now wishes to prove that the expression result will be NULL if any of these
858 * inputs is NULL. If we return false, then the proof succeeded.
861 contain_nonstrict_functions(Node *clause)
863 return contain_nonstrict_functions_walker(clause, NULL);
867 contain_nonstrict_functions_walker(Node *node, void *context)
871 if (IsA(node, Aggref))
873 /* an aggregate could return non-null with null input */
876 if (IsA(node, ArrayRef))
878 /* array assignment is nonstrict, but subscripting is strict */
879 if (((ArrayRef *) node)->refassgnexpr != NULL)
881 /* else fall through to check args */
883 if (IsA(node, FuncExpr))
885 FuncExpr *expr = (FuncExpr *) node;
887 if (!func_strict(expr->funcid))
889 /* else fall through to check args */
891 if (IsA(node, OpExpr))
893 OpExpr *expr = (OpExpr *) node;
895 if (!op_strict(expr->opno))
897 /* else fall through to check args */
899 if (IsA(node, DistinctExpr))
901 /* IS DISTINCT FROM is inherently non-strict */
904 if (IsA(node, ScalarArrayOpExpr))
906 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
908 if (!is_strict_saop(expr, false))
910 /* else fall through to check args */
912 if (IsA(node, BoolExpr))
914 BoolExpr *expr = (BoolExpr *) node;
916 switch (expr->boolop)
920 /* AND, OR are inherently non-strict */
926 if (IsA(node, SubLink))
928 /* In some cases a sublink might be strict, but in general not */
931 if (IsA(node, SubPlan))
933 if (IsA(node, FieldStore))
935 if (IsA(node, CaseExpr))
937 if (IsA(node, CaseWhen))
939 if (IsA(node, ArrayExpr))
941 if (IsA(node, RowExpr))
943 if (IsA(node, RowCompareExpr))
945 if (IsA(node, CoalesceExpr))
947 if (IsA(node, MinMaxExpr))
949 if (IsA(node, XmlExpr))
951 if (IsA(node, NullIfExpr))
953 if (IsA(node, NullTest))
955 if (IsA(node, BooleanTest))
957 return expression_tree_walker(node, contain_nonstrict_functions_walker,
963 * find_nonnullable_rels
964 * Determine which base rels are forced nonnullable by given clause.
966 * Returns the set of all Relids that are referenced in the clause in such
967 * a way that the clause cannot possibly return TRUE if any of these Relids
968 * is an all-NULL row. (It is OK to err on the side of conservatism; hence
969 * the analysis here is simplistic.)
971 * The semantics here are subtly different from contain_nonstrict_functions:
972 * that function is concerned with NULL results from arbitrary expressions,
973 * but here we assume that the input is a Boolean expression, and wish to
974 * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
975 * the expression to have been AND/OR flattened and converted to implicit-AND
978 * top_level is TRUE while scanning top-level AND/OR structure; here, showing
979 * the result is either FALSE or NULL is good enough. top_level is FALSE when
980 * we have descended below a NOT or a strict function: now we must be able to
981 * prove that the subexpression goes to NULL.
983 * We don't use expression_tree_walker here because we don't want to descend
984 * through very many kinds of nodes; only the ones we can be sure are strict.
987 find_nonnullable_rels(Node *clause)
989 return find_nonnullable_rels_walker(clause, true);
993 find_nonnullable_rels_walker(Node *node, bool top_level)
995 Relids result = NULL;
1002 Var *var = (Var *) node;
1004 if (var->varlevelsup == 0)
1005 result = bms_make_singleton(var->varno);
1007 else if (IsA(node, List))
1010 * At top level, we are examining an implicit-AND list: if any of
1011 * the arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1012 * If not at top level, we are examining the arguments of a strict
1013 * function: if any of them produce NULL then the result of the
1014 * function must be NULL. So in both cases, the set of nonnullable
1015 * rels is the union of those found in the arms, and we pass down
1016 * the top_level flag unmodified.
1018 foreach(l, (List *) node)
1020 result = bms_join(result,
1021 find_nonnullable_rels_walker(lfirst(l),
1025 else if (IsA(node, FuncExpr))
1027 FuncExpr *expr = (FuncExpr *) node;
1029 if (func_strict(expr->funcid))
1030 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1032 else if (IsA(node, OpExpr))
1034 OpExpr *expr = (OpExpr *) node;
1036 if (op_strict(expr->opno))
1037 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1039 else if (IsA(node, ScalarArrayOpExpr))
1041 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1043 if (is_strict_saop(expr, true))
1044 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1046 else if (IsA(node, BoolExpr))
1048 BoolExpr *expr = (BoolExpr *) node;
1050 switch (expr->boolop)
1053 /* At top level we can just recurse (to the List case) */
1056 result = find_nonnullable_rels_walker((Node *) expr->args,
1061 * Below top level, even if one arm produces NULL, the result
1062 * could be FALSE (hence not NULL). However, if *all* the
1063 * arms produce NULL then the result is NULL, so we can
1064 * take the intersection of the sets of nonnullable rels,
1065 * just as for OR. Fall through to share code.
1070 * OR is strict if all of its arms are, so we can take the
1071 * intersection of the sets of nonnullable rels for each arm.
1072 * This works for both values of top_level.
1074 foreach(l, expr->args)
1078 subresult = find_nonnullable_rels_walker(lfirst(l),
1080 if (result == NULL) /* first subresult? */
1083 result = bms_int_members(result, subresult);
1085 * If the intersection is empty, we can stop looking.
1086 * This also justifies the test for first-subresult above.
1088 if (bms_is_empty(result))
1093 /* NOT will return null if its arg is null */
1094 result = find_nonnullable_rels_walker((Node *) expr->args,
1098 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1102 else if (IsA(node, RelabelType))
1104 RelabelType *expr = (RelabelType *) node;
1106 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1108 else if (IsA(node, ConvertRowtypeExpr))
1110 /* not clear this is useful, but it can't hurt */
1111 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1113 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1115 else if (IsA(node, NullTest))
1117 /* IS NOT NULL can be considered strict, but only at top level */
1118 NullTest *expr = (NullTest *) node;
1120 if (top_level && expr->nulltesttype == IS_NOT_NULL)
1121 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1123 else if (IsA(node, BooleanTest))
1125 /* Boolean tests that reject NULL are strict at top level */
1126 BooleanTest *expr = (BooleanTest *) node;
1129 (expr->booltesttype == IS_TRUE ||
1130 expr->booltesttype == IS_FALSE ||
1131 expr->booltesttype == IS_NOT_UNKNOWN))
1132 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1138 * Can we treat a ScalarArrayOpExpr as strict?
1140 * If "falseOK" is true, then a "false" result can be considered strict,
1141 * else we need to guarantee an actual NULL result for NULL input.
1143 * "foo op ALL array" is strict if the op is strict *and* we can prove
1144 * that the array input isn't an empty array. We can check that
1145 * for the cases of an array constant and an ARRAY[] construct.
1147 * "foo op ANY array" is strict in the falseOK sense if the op is strict.
1148 * If not falseOK, the test is the same as for "foo op ALL array".
1151 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
1155 /* The contained operator must be strict. */
1156 if (!op_strict(expr->opno))
1158 /* If ANY and falseOK, that's all we need to check. */
1159 if (expr->useOr && falseOK)
1161 /* Else, we have to see if the array is provably non-empty. */
1162 Assert(list_length(expr->args) == 2);
1163 rightop = (Node *) lsecond(expr->args);
1164 if (rightop && IsA(rightop, Const))
1166 Datum arraydatum = ((Const *) rightop)->constvalue;
1167 bool arrayisnull = ((Const *) rightop)->constisnull;
1168 ArrayType *arrayval;
1173 arrayval = DatumGetArrayTypeP(arraydatum);
1174 nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1178 else if (rightop && IsA(rightop, ArrayExpr))
1180 ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1182 if (arrayexpr->elements != NIL && !arrayexpr->multidims)
1189 /*****************************************************************************
1190 * Check for "pseudo-constant" clauses
1191 *****************************************************************************/
1194 * is_pseudo_constant_clause
1195 * Detect whether an expression is "pseudo constant", ie, it contains no
1196 * variables of the current query level and no uses of volatile functions.
1197 * Such an expr is not necessarily a true constant: it can still contain
1198 * Params and outer-level Vars, not to mention functions whose results
1199 * may vary from one statement to the next. However, the expr's value
1200 * will be constant over any one scan of the current query, so it can be
1201 * used as, eg, an indexscan key.
1203 * CAUTION: this function omits to test for one very important class of
1204 * not-constant expressions, namely aggregates (Aggrefs). In current usage
1205 * this is only applied to WHERE clauses and so a check for Aggrefs would be
1206 * a waste of cycles; but be sure to also check contain_agg_clause() if you
1207 * want to know about pseudo-constness in other contexts.
1210 is_pseudo_constant_clause(Node *clause)
1213 * We could implement this check in one recursive scan. But since the
1214 * check for volatile functions is both moderately expensive and unlikely
1215 * to fail, it seems better to look for Vars first and only check for
1216 * volatile functions if we find no Vars.
1218 if (!contain_var_clause(clause) &&
1219 !contain_volatile_functions(clause))
1225 * is_pseudo_constant_clause_relids
1226 * Same as above, except caller already has available the var membership
1227 * of the expression; this lets us avoid the contain_var_clause() scan.
1230 is_pseudo_constant_clause_relids(Node *clause, Relids relids)
1232 if (bms_is_empty(relids) &&
1233 !contain_volatile_functions(clause))
1239 /*****************************************************************************
1240 * Tests on clauses of queries
1242 * Possibly this code should go someplace else, since this isn't quite the
1243 * same meaning of "clause" as is used elsewhere in this module. But I can't
1244 * think of a better place for it...
1245 *****************************************************************************/
1248 * Test whether a query uses DISTINCT ON, ie, has a distinct-list that is
1249 * not the same as the set of output columns.
1252 has_distinct_on_clause(Query *query)
1256 /* Is there a DISTINCT clause at all? */
1257 if (query->distinctClause == NIL)
1261 * If the DISTINCT list contains all the nonjunk targetlist items, and
1262 * nothing else (ie, no junk tlist items), then it's a simple DISTINCT,
1263 * else it's DISTINCT ON. We do not require the lists to be in the same
1264 * order (since the parser may have adjusted the DISTINCT clause ordering
1265 * to agree with ORDER BY). Furthermore, a non-DISTINCT junk tlist item
1266 * that is in the sortClause is also evidence of DISTINCT ON, since we
1267 * don't allow ORDER BY on junk tlist items when plain DISTINCT is used.
1269 * This code assumes that the DISTINCT list is valid, ie, all its entries
1270 * match some entry of the tlist.
1272 foreach(l, query->targetList)
1274 TargetEntry *tle = (TargetEntry *) lfirst(l);
1276 if (tle->ressortgroupref == 0)
1279 continue; /* we can ignore unsorted junk cols */
1280 return true; /* definitely not in DISTINCT list */
1282 if (targetIsInSortList(tle, InvalidOid, query->distinctClause))
1285 return true; /* junk TLE in DISTINCT means DISTINCT ON */
1286 /* else this TLE is okay, keep looking */
1290 /* This TLE is not in DISTINCT list */
1292 return true; /* non-junk, non-DISTINCT, so DISTINCT ON */
1293 if (targetIsInSortList(tle, InvalidOid, query->sortClause))
1294 return true; /* sorted, non-distinct junk */
1295 /* unsorted junk is okay, keep looking */
1298 /* It's a simple DISTINCT */
1303 * Test whether a query uses simple DISTINCT, ie, has a distinct-list that
1304 * is the same as the set of output columns.
1307 has_distinct_clause(Query *query)
1309 /* Is there a DISTINCT clause at all? */
1310 if (query->distinctClause == NIL)
1313 /* It's DISTINCT if it's not DISTINCT ON */
1314 return !has_distinct_on_clause(query);
1318 /*****************************************************************************
1320 * General clause-manipulating routines *
1322 *****************************************************************************/
1326 * (formerly clause_relids)
1328 * Returns the number of different relations referenced in 'clause'.
1331 NumRelids(Node *clause)
1333 Relids varnos = pull_varnos(clause);
1334 int result = bms_num_members(varnos);
1341 * CommuteOpExpr: commute a binary operator clause
1343 * XXX the clause is destructively modified!
1346 CommuteOpExpr(OpExpr *clause)
1351 /* Sanity checks: caller is at fault if these fail */
1352 if (!is_opclause(clause) ||
1353 list_length(clause->args) != 2)
1354 elog(ERROR, "cannot commute non-binary-operator clause");
1356 opoid = get_commutator(clause->opno);
1358 if (!OidIsValid(opoid))
1359 elog(ERROR, "could not find commutator for operator %u",
1363 * modify the clause in-place!
1365 clause->opno = opoid;
1366 clause->opfuncid = InvalidOid;
1367 /* opresulttype and opretset are assumed not to change */
1369 temp = linitial(clause->args);
1370 linitial(clause->args) = lsecond(clause->args);
1371 lsecond(clause->args) = temp;
1375 * CommuteRowCompareExpr: commute a RowCompareExpr clause
1377 * XXX the clause is destructively modified!
1380 CommuteRowCompareExpr(RowCompareExpr *clause)
1386 /* Sanity checks: caller is at fault if these fail */
1387 if (!IsA(clause, RowCompareExpr))
1388 elog(ERROR, "expected a RowCompareExpr");
1390 /* Build list of commuted operators */
1392 foreach(l, clause->opnos)
1394 Oid opoid = lfirst_oid(l);
1396 opoid = get_commutator(opoid);
1397 if (!OidIsValid(opoid))
1398 elog(ERROR, "could not find commutator for operator %u",
1400 newops = lappend_oid(newops, opoid);
1404 * modify the clause in-place!
1406 switch (clause->rctype)
1409 clause->rctype = ROWCOMPARE_GT;
1412 clause->rctype = ROWCOMPARE_GE;
1415 clause->rctype = ROWCOMPARE_LE;
1418 clause->rctype = ROWCOMPARE_LT;
1421 elog(ERROR, "unexpected RowCompare type: %d",
1422 (int) clause->rctype);
1426 clause->opnos = newops;
1429 * Note: we need not change the opfamilies list; we assume any btree
1430 * opfamily containing an operator will also contain its commutator.
1433 temp = clause->largs;
1434 clause->largs = clause->rargs;
1435 clause->rargs = temp;
1439 * strip_implicit_coercions: remove implicit coercions at top level of tree
1441 * Note: there isn't any useful thing we can do with a RowExpr here, so
1442 * just return it unchanged, even if it's marked as an implicit coercion.
1445 strip_implicit_coercions(Node *node)
1449 if (IsA(node, FuncExpr))
1451 FuncExpr *f = (FuncExpr *) node;
1453 if (f->funcformat == COERCE_IMPLICIT_CAST)
1454 return strip_implicit_coercions(linitial(f->args));
1456 else if (IsA(node, RelabelType))
1458 RelabelType *r = (RelabelType *) node;
1460 if (r->relabelformat == COERCE_IMPLICIT_CAST)
1461 return strip_implicit_coercions((Node *) r->arg);
1463 else if (IsA(node, ConvertRowtypeExpr))
1465 ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
1467 if (c->convertformat == COERCE_IMPLICIT_CAST)
1468 return strip_implicit_coercions((Node *) c->arg);
1470 else if (IsA(node, CoerceToDomain))
1472 CoerceToDomain *c = (CoerceToDomain *) node;
1474 if (c->coercionformat == COERCE_IMPLICIT_CAST)
1475 return strip_implicit_coercions((Node *) c->arg);
1481 * set_coercionform_dontcare: set all CoercionForm fields to COERCE_DONTCARE
1483 * This is used to make index expressions and index predicates more easily
1484 * comparable to clauses of queries. CoercionForm is not semantically
1485 * significant (for cases where it does matter, the significant info is
1486 * coded into the coercion function arguments) so we can ignore it during
1487 * comparisons. Thus, for example, an index on "foo::int4" can match an
1488 * implicit coercion to int4.
1490 * Caution: the passed expression tree is modified in-place.
1493 set_coercionform_dontcare(Node *node)
1495 (void) set_coercionform_dontcare_walker(node, NULL);
1499 set_coercionform_dontcare_walker(Node *node, void *context)
1503 if (IsA(node, FuncExpr))
1504 ((FuncExpr *) node)->funcformat = COERCE_DONTCARE;
1505 else if (IsA(node, RelabelType))
1506 ((RelabelType *) node)->relabelformat = COERCE_DONTCARE;
1507 else if (IsA(node, ConvertRowtypeExpr))
1508 ((ConvertRowtypeExpr *) node)->convertformat = COERCE_DONTCARE;
1509 else if (IsA(node, RowExpr))
1510 ((RowExpr *) node)->row_format = COERCE_DONTCARE;
1511 else if (IsA(node, CoerceToDomain))
1512 ((CoerceToDomain *) node)->coercionformat = COERCE_DONTCARE;
1513 return expression_tree_walker(node, set_coercionform_dontcare_walker,
1518 * Helper for eval_const_expressions: check that datatype of an attribute
1519 * is still what it was when the expression was parsed. This is needed to
1520 * guard against improper simplification after ALTER COLUMN TYPE. (XXX we
1521 * may well need to make similar checks elsewhere?)
1524 rowtype_field_matches(Oid rowtypeid, int fieldnum,
1525 Oid expectedtype, int32 expectedtypmod)
1528 Form_pg_attribute attr;
1530 /* No issue for RECORD, since there is no way to ALTER such a type */
1531 if (rowtypeid == RECORDOID)
1533 tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
1534 if (fieldnum <= 0 || fieldnum > tupdesc->natts)
1536 ReleaseTupleDesc(tupdesc);
1539 attr = tupdesc->attrs[fieldnum - 1];
1540 if (attr->attisdropped ||
1541 attr->atttypid != expectedtype ||
1542 attr->atttypmod != expectedtypmod)
1544 ReleaseTupleDesc(tupdesc);
1547 ReleaseTupleDesc(tupdesc);
1552 /*--------------------
1553 * eval_const_expressions
1555 * Reduce any recognizably constant subexpressions of the given
1556 * expression tree, for example "2 + 2" => "4". More interestingly,
1557 * we can reduce certain boolean expressions even when they contain
1558 * non-constant subexpressions: "x OR true" => "true" no matter what
1559 * the subexpression x is. (XXX We assume that no such subexpression
1560 * will have important side-effects, which is not necessarily a good
1561 * assumption in the presence of user-defined functions; do we need a
1562 * pg_proc flag that prevents discarding the execution of a function?)
1564 * We do understand that certain functions may deliver non-constant
1565 * results even with constant inputs, "nextval()" being the classic
1566 * example. Functions that are not marked "immutable" in pg_proc
1567 * will not be pre-evaluated here, although we will reduce their
1568 * arguments as far as possible.
1570 * We assume that the tree has already been type-checked and contains
1571 * only operators and functions that are reasonable to try to execute.
1573 * NOTE: the planner assumes that this will always flatten nested AND and
1574 * OR clauses into N-argument form. See comments in prepqual.c.
1575 *--------------------
1578 eval_const_expressions(Node *node)
1580 eval_const_expressions_context context;
1582 context.boundParams = NULL; /* don't use any bound params */
1583 context.active_fns = NIL; /* nothing being recursively simplified */
1584 context.case_val = NULL; /* no CASE being examined */
1585 context.estimate = false; /* safe transformations only */
1586 return eval_const_expressions_mutator(node, &context);
1589 /*--------------------
1590 * estimate_expression_value
1592 * This function attempts to estimate the value of an expression for
1593 * planning purposes. It is in essence a more aggressive version of
1594 * eval_const_expressions(): we will perform constant reductions that are
1595 * not necessarily 100% safe, but are reasonable for estimation purposes.
1597 * Currently the extra steps that are taken in this mode are:
1598 * 1. Substitute values for Params, where a bound Param value has been made
1599 * available by the caller of planner(), even if the Param isn't marked
1600 * constant. This effectively means that we plan using the first supplied
1601 * value of the Param.
1602 * 2. Fold stable, as well as immutable, functions to constants.
1603 *--------------------
1606 estimate_expression_value(PlannerInfo *root, Node *node)
1608 eval_const_expressions_context context;
1610 context.boundParams = root->glob->boundParams; /* bound Params */
1611 context.active_fns = NIL; /* nothing being recursively simplified */
1612 context.case_val = NULL; /* no CASE being examined */
1613 context.estimate = true; /* unsafe transformations OK */
1614 return eval_const_expressions_mutator(node, &context);
1618 eval_const_expressions_mutator(Node *node,
1619 eval_const_expressions_context *context)
1623 if (IsA(node, Param))
1625 Param *param = (Param *) node;
1627 /* Look to see if we've been given a value for this Param */
1628 if (param->paramkind == PARAM_EXTERN &&
1629 context->boundParams != NULL &&
1630 param->paramid > 0 &&
1631 param->paramid <= context->boundParams->numParams)
1633 ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
1635 if (OidIsValid(prm->ptype))
1637 /* OK to substitute parameter value? */
1638 if (context->estimate || (prm->pflags & PARAM_FLAG_CONST))
1641 * Return a Const representing the param value. Must copy
1642 * pass-by-ref datatypes, since the Param might be in a
1643 * memory context shorter-lived than our output plan
1650 Assert(prm->ptype == param->paramtype);
1651 get_typlenbyval(param->paramtype, &typLen, &typByVal);
1652 if (prm->isnull || typByVal)
1655 pval = datumCopy(prm->value, typByVal, typLen);
1656 return (Node *) makeConst(param->paramtype,
1664 /* Not replaceable, so just copy the Param (no need to recurse) */
1665 return (Node *) copyObject(param);
1667 if (IsA(node, FuncExpr))
1669 FuncExpr *expr = (FuncExpr *) node;
1675 * Reduce constants in the FuncExpr's arguments. We know args is
1676 * either NIL or a List node, so we can call expression_tree_mutator
1677 * directly rather than recursing to self.
1679 args = (List *) expression_tree_mutator((Node *) expr->args,
1680 eval_const_expressions_mutator,
1684 * Code for op/func reduction is pretty bulky, so split it out as a
1685 * separate function.
1687 simple = simplify_function(expr->funcid, expr->funcresulttype, args,
1689 if (simple) /* successfully simplified it */
1690 return (Node *) simple;
1693 * The expression cannot be simplified any further, so build and
1694 * return a replacement FuncExpr node using the possibly-simplified
1697 newexpr = makeNode(FuncExpr);
1698 newexpr->funcid = expr->funcid;
1699 newexpr->funcresulttype = expr->funcresulttype;
1700 newexpr->funcretset = expr->funcretset;
1701 newexpr->funcformat = expr->funcformat;
1702 newexpr->args = args;
1703 return (Node *) newexpr;
1705 if (IsA(node, OpExpr))
1707 OpExpr *expr = (OpExpr *) node;
1713 * Reduce constants in the OpExpr's arguments. We know args is either
1714 * NIL or a List node, so we can call expression_tree_mutator directly
1715 * rather than recursing to self.
1717 args = (List *) expression_tree_mutator((Node *) expr->args,
1718 eval_const_expressions_mutator,
1722 * Need to get OID of underlying function. Okay to scribble on input
1728 * Code for op/func reduction is pretty bulky, so split it out as a
1729 * separate function.
1731 simple = simplify_function(expr->opfuncid, expr->opresulttype, args,
1733 if (simple) /* successfully simplified it */
1734 return (Node *) simple;
1737 * If the operator is boolean equality, we know how to simplify cases
1738 * involving one constant and one non-constant argument.
1740 if (expr->opno == BooleanEqualOperator)
1742 simple = simplify_boolean_equality(args);
1743 if (simple) /* successfully simplified it */
1744 return (Node *) simple;
1748 * The expression cannot be simplified any further, so build and
1749 * return a replacement OpExpr node using the possibly-simplified
1752 newexpr = makeNode(OpExpr);
1753 newexpr->opno = expr->opno;
1754 newexpr->opfuncid = expr->opfuncid;
1755 newexpr->opresulttype = expr->opresulttype;
1756 newexpr->opretset = expr->opretset;
1757 newexpr->args = args;
1758 return (Node *) newexpr;
1760 if (IsA(node, DistinctExpr))
1762 DistinctExpr *expr = (DistinctExpr *) node;
1765 bool has_null_input = false;
1766 bool all_null_input = true;
1767 bool has_nonconst_input = false;
1769 DistinctExpr *newexpr;
1772 * Reduce constants in the DistinctExpr's arguments. We know args is
1773 * either NIL or a List node, so we can call expression_tree_mutator
1774 * directly rather than recursing to self.
1776 args = (List *) expression_tree_mutator((Node *) expr->args,
1777 eval_const_expressions_mutator,
1781 * We must do our own check for NULLs because DistinctExpr has
1782 * different results for NULL input than the underlying operator does.
1786 if (IsA(lfirst(arg), Const))
1788 has_null_input |= ((Const *) lfirst(arg))->constisnull;
1789 all_null_input &= ((Const *) lfirst(arg))->constisnull;
1792 has_nonconst_input = true;
1795 /* all constants? then can optimize this out */
1796 if (!has_nonconst_input)
1798 /* all nulls? then not distinct */
1800 return makeBoolConst(false, false);
1802 /* one null? then distinct */
1804 return makeBoolConst(true, false);
1806 /* otherwise try to evaluate the '=' operator */
1807 /* (NOT okay to try to inline it, though!) */
1810 * Need to get OID of underlying function. Okay to scribble on
1811 * input to this extent.
1813 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
1816 * Code for op/func reduction is pretty bulky, so split it out as
1817 * a separate function.
1819 simple = simplify_function(expr->opfuncid, expr->opresulttype,
1820 args, false, context);
1821 if (simple) /* successfully simplified it */
1824 * Since the underlying operator is "=", must negate its
1827 Const *csimple = (Const *) simple;
1829 Assert(IsA(csimple, Const));
1830 csimple->constvalue =
1831 BoolGetDatum(!DatumGetBool(csimple->constvalue));
1832 return (Node *) csimple;
1837 * The expression cannot be simplified any further, so build and
1838 * return a replacement DistinctExpr node using the
1839 * possibly-simplified arguments.
1841 newexpr = makeNode(DistinctExpr);
1842 newexpr->opno = expr->opno;
1843 newexpr->opfuncid = expr->opfuncid;
1844 newexpr->opresulttype = expr->opresulttype;
1845 newexpr->opretset = expr->opretset;
1846 newexpr->args = args;
1847 return (Node *) newexpr;
1849 if (IsA(node, BoolExpr))
1851 BoolExpr *expr = (BoolExpr *) node;
1853 switch (expr->boolop)
1858 bool haveNull = false;
1859 bool forceTrue = false;
1861 newargs = simplify_or_arguments(expr->args, context,
1862 &haveNull, &forceTrue);
1864 return makeBoolConst(true, false);
1866 newargs = lappend(newargs, makeBoolConst(false, true));
1867 /* If all the inputs are FALSE, result is FALSE */
1869 return makeBoolConst(false, false);
1870 /* If only one nonconst-or-NULL input, it's the result */
1871 if (list_length(newargs) == 1)
1872 return (Node *) linitial(newargs);
1873 /* Else we still need an OR node */
1874 return (Node *) make_orclause(newargs);
1879 bool haveNull = false;
1880 bool forceFalse = false;
1882 newargs = simplify_and_arguments(expr->args, context,
1883 &haveNull, &forceFalse);
1885 return makeBoolConst(false, false);
1887 newargs = lappend(newargs, makeBoolConst(false, true));
1888 /* If all the inputs are TRUE, result is TRUE */
1890 return makeBoolConst(true, false);
1891 /* If only one nonconst-or-NULL input, it's the result */
1892 if (list_length(newargs) == 1)
1893 return (Node *) linitial(newargs);
1894 /* Else we still need an AND node */
1895 return (Node *) make_andclause(newargs);
1901 Assert(list_length(expr->args) == 1);
1902 arg = eval_const_expressions_mutator(linitial(expr->args),
1904 if (IsA(arg, Const))
1906 Const *const_input = (Const *) arg;
1908 /* NOT NULL => NULL */
1909 if (const_input->constisnull)
1910 return makeBoolConst(false, true);
1911 /* otherwise pretty easy */
1912 return makeBoolConst(!DatumGetBool(const_input->constvalue),
1915 else if (not_clause(arg))
1917 /* Cancel NOT/NOT */
1918 return (Node *) get_notclausearg((Expr *) arg);
1920 /* Else we still need a NOT node */
1921 return (Node *) make_notclause((Expr *) arg);
1924 elog(ERROR, "unrecognized boolop: %d",
1925 (int) expr->boolop);
1929 if (IsA(node, SubPlan))
1932 * Return a SubPlan unchanged --- too late to do anything with it.
1934 * XXX should we ereport() here instead? Probably this routine should
1935 * never be invoked after SubPlan creation.
1939 if (IsA(node, RelabelType))
1942 * If we can simplify the input to a constant, then we don't need the
1943 * RelabelType node anymore: just change the type field of the Const
1944 * node. Otherwise, must copy the RelabelType node.
1946 RelabelType *relabel = (RelabelType *) node;
1949 arg = eval_const_expressions_mutator((Node *) relabel->arg,
1953 * If we find stacked RelabelTypes (eg, from foo :: int :: oid) we can
1954 * discard all but the top one.
1956 while (arg && IsA(arg, RelabelType))
1957 arg = (Node *) ((RelabelType *) arg)->arg;
1959 if (arg && IsA(arg, Const))
1961 Const *con = (Const *) arg;
1963 con->consttype = relabel->resulttype;
1966 * relabel's resulttypmod is discarded, which is OK for now; if
1967 * the type actually needs a runtime length coercion then there
1968 * should be a function call to do it just above this node.
1970 return (Node *) con;
1974 RelabelType *newrelabel = makeNode(RelabelType);
1976 newrelabel->arg = (Expr *) arg;
1977 newrelabel->resulttype = relabel->resulttype;
1978 newrelabel->resulttypmod = relabel->resulttypmod;
1979 return (Node *) newrelabel;
1982 if (IsA(node, CaseExpr))
1985 * CASE expressions can be simplified if there are constant
1986 * condition clauses:
1987 * FALSE (or NULL): drop the alternative
1988 * TRUE: drop all remaining alternatives
1989 * If the first non-FALSE alternative is a constant TRUE, we can
1990 * simplify the entire CASE to that alternative's expression.
1991 * If there are no non-FALSE alternatives, we simplify the entire
1992 * CASE to the default result (ELSE result).
1994 * If we have a simple-form CASE with constant test expression,
1995 * we substitute the constant value for contained CaseTestExpr
1996 * placeholder nodes, so that we have the opportunity to reduce
1997 * constant test conditions. For example this allows
1998 * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
1999 * to reduce to 1 rather than drawing a divide-by-0 error.
2002 CaseExpr *caseexpr = (CaseExpr *) node;
2004 Node *save_case_val;
2007 bool const_true_cond;
2008 Node *defresult = NULL;
2011 /* Simplify the test expression, if any */
2012 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
2015 /* Set up for contained CaseTestExpr nodes */
2016 save_case_val = context->case_val;
2017 if (newarg && IsA(newarg, Const))
2018 context->case_val = newarg;
2020 context->case_val = NULL;
2022 /* Simplify the WHEN clauses */
2024 const_true_cond = false;
2025 foreach(arg, caseexpr->args)
2027 CaseWhen *oldcasewhen = (CaseWhen *) lfirst(arg);
2031 Assert(IsA(oldcasewhen, CaseWhen));
2033 /* Simplify this alternative's test condition */
2035 eval_const_expressions_mutator((Node *) oldcasewhen->expr,
2039 * If the test condition is constant FALSE (or NULL), then drop
2040 * this WHEN clause completely, without processing the result.
2042 if (casecond && IsA(casecond, Const))
2044 Const *const_input = (Const *) casecond;
2046 if (const_input->constisnull ||
2047 !DatumGetBool(const_input->constvalue))
2048 continue; /* drop alternative with FALSE condition */
2049 /* Else it's constant TRUE */
2050 const_true_cond = true;
2053 /* Simplify this alternative's result value */
2055 eval_const_expressions_mutator((Node *) oldcasewhen->result,
2058 /* If non-constant test condition, emit a new WHEN node */
2059 if (!const_true_cond)
2061 CaseWhen *newcasewhen = makeNode(CaseWhen);
2063 newcasewhen->expr = (Expr *) casecond;
2064 newcasewhen->result = (Expr *) caseresult;
2065 newargs = lappend(newargs, newcasewhen);
2070 * Found a TRUE condition, so none of the remaining alternatives
2071 * can be reached. We treat the result as the default result.
2073 defresult = caseresult;
2077 /* Simplify the default result, unless we replaced it above */
2078 if (!const_true_cond)
2080 eval_const_expressions_mutator((Node *) caseexpr->defresult,
2083 context->case_val = save_case_val;
2085 /* If no non-FALSE alternatives, CASE reduces to the default result */
2088 /* Otherwise we need a new CASE node */
2089 newcase = makeNode(CaseExpr);
2090 newcase->casetype = caseexpr->casetype;
2091 newcase->arg = (Expr *) newarg;
2092 newcase->args = newargs;
2093 newcase->defresult = (Expr *) defresult;
2094 return (Node *) newcase;
2096 if (IsA(node, CaseTestExpr))
2099 * If we know a constant test value for the current CASE construct,
2100 * substitute it for the placeholder. Else just return the
2101 * placeholder as-is.
2103 if (context->case_val)
2104 return copyObject(context->case_val);
2106 return copyObject(node);
2108 if (IsA(node, ArrayExpr))
2110 ArrayExpr *arrayexpr = (ArrayExpr *) node;
2111 ArrayExpr *newarray;
2112 bool all_const = true;
2117 foreach(element, arrayexpr->elements)
2121 e = eval_const_expressions_mutator((Node *) lfirst(element),
2125 newelems = lappend(newelems, e);
2128 newarray = makeNode(ArrayExpr);
2129 newarray->array_typeid = arrayexpr->array_typeid;
2130 newarray->element_typeid = arrayexpr->element_typeid;
2131 newarray->elements = newelems;
2132 newarray->multidims = arrayexpr->multidims;
2135 return (Node *) evaluate_expr((Expr *) newarray,
2136 newarray->array_typeid);
2138 return (Node *) newarray;
2140 if (IsA(node, CoalesceExpr))
2142 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2143 CoalesceExpr *newcoalesce;
2148 foreach(arg, coalesceexpr->args)
2152 e = eval_const_expressions_mutator((Node *) lfirst(arg),
2156 * We can remove null constants from the list. For a non-null
2157 * constant, if it has not been preceded by any other
2158 * non-null-constant expressions then that is the result.
2162 if (((Const *) e)->constisnull)
2163 continue; /* drop null constant */
2165 return e; /* first expr */
2167 newargs = lappend(newargs, e);
2170 /* If all the arguments were constant null, the result is just null */
2172 return (Node *) makeNullConst(coalesceexpr->coalescetype);
2174 newcoalesce = makeNode(CoalesceExpr);
2175 newcoalesce->coalescetype = coalesceexpr->coalescetype;
2176 newcoalesce->args = newargs;
2177 return (Node *) newcoalesce;
2179 if (IsA(node, FieldSelect))
2182 * We can optimize field selection from a whole-row Var into a simple
2183 * Var. (This case won't be generated directly by the parser, because
2184 * ParseComplexProjection short-circuits it. But it can arise while
2185 * simplifying functions.) Also, we can optimize field selection from
2186 * a RowExpr construct.
2188 * We must however check that the declared type of the field is still
2189 * the same as when the FieldSelect was created --- this can change if
2190 * someone did ALTER COLUMN TYPE on the rowtype.
2192 FieldSelect *fselect = (FieldSelect *) node;
2193 FieldSelect *newfselect;
2196 arg = eval_const_expressions_mutator((Node *) fselect->arg,
2198 if (arg && IsA(arg, Var) &&
2199 ((Var *) arg)->varattno == InvalidAttrNumber)
2201 if (rowtype_field_matches(((Var *) arg)->vartype,
2203 fselect->resulttype,
2204 fselect->resulttypmod))
2205 return (Node *) makeVar(((Var *) arg)->varno,
2207 fselect->resulttype,
2208 fselect->resulttypmod,
2209 ((Var *) arg)->varlevelsup);
2211 if (arg && IsA(arg, RowExpr))
2213 RowExpr *rowexpr = (RowExpr *) arg;
2215 if (fselect->fieldnum > 0 &&
2216 fselect->fieldnum <= list_length(rowexpr->args))
2218 Node *fld = (Node *) list_nth(rowexpr->args,
2219 fselect->fieldnum - 1);
2221 if (rowtype_field_matches(rowexpr->row_typeid,
2223 fselect->resulttype,
2224 fselect->resulttypmod) &&
2225 fselect->resulttype == exprType(fld) &&
2226 fselect->resulttypmod == exprTypmod(fld))
2230 newfselect = makeNode(FieldSelect);
2231 newfselect->arg = (Expr *) arg;
2232 newfselect->fieldnum = fselect->fieldnum;
2233 newfselect->resulttype = fselect->resulttype;
2234 newfselect->resulttypmod = fselect->resulttypmod;
2235 return (Node *) newfselect;
2237 if (IsA(node, NullTest))
2239 NullTest *ntest = (NullTest *) node;
2243 arg = eval_const_expressions_mutator((Node *) ntest->arg,
2245 if (arg && IsA(arg, RowExpr))
2247 RowExpr *rarg = (RowExpr *) arg;
2248 List *newargs = NIL;
2252 * We break ROW(...) IS [NOT] NULL into separate tests on its
2253 * component fields. This form is usually more efficient to
2254 * evaluate, as well as being more amenable to optimization.
2256 foreach(l, rarg->args)
2258 Node *relem = (Node *) lfirst(l);
2261 * A constant field refutes the whole NullTest if it's of the
2262 * wrong nullness; else we can discard it.
2264 if (relem && IsA(relem, Const))
2266 Const *carg = (Const *) relem;
2268 if (carg->constisnull ?
2269 (ntest->nulltesttype == IS_NOT_NULL) :
2270 (ntest->nulltesttype == IS_NULL))
2271 return makeBoolConst(false, false);
2274 newntest = makeNode(NullTest);
2275 newntest->arg = (Expr *) relem;
2276 newntest->nulltesttype = ntest->nulltesttype;
2277 newargs = lappend(newargs, newntest);
2279 /* If all the inputs were constants, result is TRUE */
2281 return makeBoolConst(true, false);
2282 /* If only one nonconst input, it's the result */
2283 if (list_length(newargs) == 1)
2284 return (Node *) linitial(newargs);
2285 /* Else we need an AND node */
2286 return (Node *) make_andclause(newargs);
2288 if (arg && IsA(arg, Const))
2290 Const *carg = (Const *) arg;
2293 switch (ntest->nulltesttype)
2296 result = carg->constisnull;
2299 result = !carg->constisnull;
2302 elog(ERROR, "unrecognized nulltesttype: %d",
2303 (int) ntest->nulltesttype);
2304 result = false; /* keep compiler quiet */
2308 return makeBoolConst(result, false);
2311 newntest = makeNode(NullTest);
2312 newntest->arg = (Expr *) arg;
2313 newntest->nulltesttype = ntest->nulltesttype;
2314 return (Node *) newntest;
2316 if (IsA(node, BooleanTest))
2318 BooleanTest *btest = (BooleanTest *) node;
2319 BooleanTest *newbtest;
2322 arg = eval_const_expressions_mutator((Node *) btest->arg,
2324 if (arg && IsA(arg, Const))
2326 Const *carg = (Const *) arg;
2329 switch (btest->booltesttype)
2332 result = (!carg->constisnull &&
2333 DatumGetBool(carg->constvalue));
2336 result = (carg->constisnull ||
2337 !DatumGetBool(carg->constvalue));
2340 result = (!carg->constisnull &&
2341 !DatumGetBool(carg->constvalue));
2344 result = (carg->constisnull ||
2345 DatumGetBool(carg->constvalue));
2348 result = carg->constisnull;
2350 case IS_NOT_UNKNOWN:
2351 result = !carg->constisnull;
2354 elog(ERROR, "unrecognized booltesttype: %d",
2355 (int) btest->booltesttype);
2356 result = false; /* keep compiler quiet */
2360 return makeBoolConst(result, false);
2363 newbtest = makeNode(BooleanTest);
2364 newbtest->arg = (Expr *) arg;
2365 newbtest->booltesttype = btest->booltesttype;
2366 return (Node *) newbtest;
2370 * For any node type not handled above, we recurse using
2371 * expression_tree_mutator, which will copy the node unchanged but try to
2372 * simplify its arguments (if any) using this routine. For example: we
2373 * cannot eliminate an ArrayRef node, but we might be able to simplify
2374 * constant expressions in its subscripts.
2376 return expression_tree_mutator(node, eval_const_expressions_mutator,
2381 * Subroutine for eval_const_expressions: process arguments of an OR clause
2383 * This includes flattening of nested ORs as well as recursion to
2384 * eval_const_expressions to simplify the OR arguments.
2386 * After simplification, OR arguments are handled as follows:
2387 * non constant: keep
2388 * FALSE: drop (does not affect result)
2389 * TRUE: force result to TRUE
2390 * NULL: keep only one
2391 * We must keep one NULL input because ExecEvalOr returns NULL when no input
2392 * is TRUE and at least one is NULL. We don't actually include the NULL
2393 * here, that's supposed to be done by the caller.
2395 * The output arguments *haveNull and *forceTrue must be initialized FALSE
2396 * by the caller. They will be set TRUE if a null constant or true constant,
2397 * respectively, is detected anywhere in the argument list.
2400 simplify_or_arguments(List *args,
2401 eval_const_expressions_context *context,
2402 bool *haveNull, bool *forceTrue)
2404 List *newargs = NIL;
2405 List *unprocessed_args;
2408 * Since the parser considers OR to be a binary operator, long OR lists
2409 * become deeply nested expressions. We must flatten these into long
2410 * argument lists of a single OR operator. To avoid blowing out the stack
2411 * with recursion of eval_const_expressions, we resort to some tenseness
2412 * here: we keep a list of not-yet-processed inputs, and handle flattening
2413 * of nested ORs by prepending to the to-do list instead of recursing.
2415 unprocessed_args = list_copy(args);
2416 while (unprocessed_args)
2418 Node *arg = (Node *) linitial(unprocessed_args);
2420 unprocessed_args = list_delete_first(unprocessed_args);
2422 /* flatten nested ORs as per above comment */
2425 List *subargs = list_copy(((BoolExpr *) arg)->args);
2427 /* overly tense code to avoid leaking unused list header */
2428 if (!unprocessed_args)
2429 unprocessed_args = subargs;
2432 List *oldhdr = unprocessed_args;
2434 unprocessed_args = list_concat(subargs, unprocessed_args);
2440 /* If it's not an OR, simplify it */
2441 arg = eval_const_expressions_mutator(arg, context);
2444 * It is unlikely but not impossible for simplification of a non-OR
2445 * clause to produce an OR. Recheck, but don't be too tense about it
2446 * since it's not a mainstream case. In particular we don't worry
2447 * about const-simplifying the input twice.
2451 List *subargs = list_copy(((BoolExpr *) arg)->args);
2453 unprocessed_args = list_concat(subargs, unprocessed_args);
2458 * OK, we have a const-simplified non-OR argument. Process it per
2461 if (IsA(arg, Const))
2463 Const *const_input = (Const *) arg;
2465 if (const_input->constisnull)
2467 else if (DatumGetBool(const_input->constvalue))
2472 * Once we detect a TRUE result we can just exit the loop
2473 * immediately. However, if we ever add a notion of
2474 * non-removable functions, we'd need to keep scanning.
2478 /* otherwise, we can drop the constant-false input */
2482 /* else emit the simplified arg into the result list */
2483 newargs = lappend(newargs, arg);
2490 * Subroutine for eval_const_expressions: process arguments of an AND clause
2492 * This includes flattening of nested ANDs as well as recursion to
2493 * eval_const_expressions to simplify the AND arguments.
2495 * After simplification, AND arguments are handled as follows:
2496 * non constant: keep
2497 * TRUE: drop (does not affect result)
2498 * FALSE: force result to FALSE
2499 * NULL: keep only one
2500 * We must keep one NULL input because ExecEvalAnd returns NULL when no input
2501 * is FALSE and at least one is NULL. We don't actually include the NULL
2502 * here, that's supposed to be done by the caller.
2504 * The output arguments *haveNull and *forceFalse must be initialized FALSE
2505 * by the caller. They will be set TRUE if a null constant or false constant,
2506 * respectively, is detected anywhere in the argument list.
2509 simplify_and_arguments(List *args,
2510 eval_const_expressions_context *context,
2511 bool *haveNull, bool *forceFalse)
2513 List *newargs = NIL;
2514 List *unprocessed_args;
2516 /* See comments in simplify_or_arguments */
2517 unprocessed_args = list_copy(args);
2518 while (unprocessed_args)
2520 Node *arg = (Node *) linitial(unprocessed_args);
2522 unprocessed_args = list_delete_first(unprocessed_args);
2524 /* flatten nested ANDs as per above comment */
2525 if (and_clause(arg))
2527 List *subargs = list_copy(((BoolExpr *) arg)->args);
2529 /* overly tense code to avoid leaking unused list header */
2530 if (!unprocessed_args)
2531 unprocessed_args = subargs;
2534 List *oldhdr = unprocessed_args;
2536 unprocessed_args = list_concat(subargs, unprocessed_args);
2542 /* If it's not an AND, simplify it */
2543 arg = eval_const_expressions_mutator(arg, context);
2546 * It is unlikely but not impossible for simplification of a non-AND
2547 * clause to produce an AND. Recheck, but don't be too tense about it
2548 * since it's not a mainstream case. In particular we don't worry
2549 * about const-simplifying the input twice.
2551 if (and_clause(arg))
2553 List *subargs = list_copy(((BoolExpr *) arg)->args);
2555 unprocessed_args = list_concat(subargs, unprocessed_args);
2560 * OK, we have a const-simplified non-AND argument. Process it per
2563 if (IsA(arg, Const))
2565 Const *const_input = (Const *) arg;
2567 if (const_input->constisnull)
2569 else if (!DatumGetBool(const_input->constvalue))
2574 * Once we detect a FALSE result we can just exit the loop
2575 * immediately. However, if we ever add a notion of
2576 * non-removable functions, we'd need to keep scanning.
2580 /* otherwise, we can drop the constant-true input */
2584 /* else emit the simplified arg into the result list */
2585 newargs = lappend(newargs, arg);
2592 * Subroutine for eval_const_expressions: try to simplify boolean equality
2594 * Input is the list of simplified arguments to the operator.
2595 * Returns a simplified expression if successful, or NULL if cannot
2596 * simplify the expression.
2598 * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x".
2599 * This is only marginally useful in itself, but doing it in constant folding
2600 * ensures that we will recognize the two forms as being equivalent in, for
2601 * example, partial index matching.
2603 * We come here only if simplify_function has failed; therefore we cannot
2604 * see two constant inputs, nor a constant-NULL input.
2607 simplify_boolean_equality(List *args)
2612 Assert(list_length(args) == 2);
2613 leftop = linitial(args);
2614 rightop = lsecond(args);
2615 if (leftop && IsA(leftop, Const))
2617 Assert(!((Const *) leftop)->constisnull);
2618 if (DatumGetBool(((Const *) leftop)->constvalue))
2619 return rightop; /* true = foo */
2621 return make_notclause(rightop); /* false = foo */
2623 if (rightop && IsA(rightop, Const))
2625 Assert(!((Const *) rightop)->constisnull);
2626 if (DatumGetBool(((Const *) rightop)->constvalue))
2627 return leftop; /* foo = true */
2629 return make_notclause(leftop); /* foo = false */
2635 * Subroutine for eval_const_expressions: try to simplify a function call
2636 * (which might originally have been an operator; we don't care)
2638 * Inputs are the function OID, actual result type OID (which is needed for
2639 * polymorphic functions), and the pre-simplified argument list;
2640 * also the context data for eval_const_expressions.
2642 * Returns a simplified expression if successful, or NULL if cannot
2643 * simplify the function call.
2646 simplify_function(Oid funcid, Oid result_type, List *args,
2648 eval_const_expressions_context *context)
2650 HeapTuple func_tuple;
2654 * We have two strategies for simplification: either execute the function
2655 * to deliver a constant result, or expand in-line the body of the
2656 * function definition (which only works for simple SQL-language
2657 * functions, but that is a common case). In either case we need access
2658 * to the function's pg_proc tuple, so fetch it just once to use in both
2661 func_tuple = SearchSysCache(PROCOID,
2662 ObjectIdGetDatum(funcid),
2664 if (!HeapTupleIsValid(func_tuple))
2665 elog(ERROR, "cache lookup failed for function %u", funcid);
2667 newexpr = evaluate_function(funcid, result_type, args,
2668 func_tuple, context);
2670 if (!newexpr && allow_inline)
2671 newexpr = inline_function(funcid, result_type, args,
2672 func_tuple, context);
2674 ReleaseSysCache(func_tuple);
2680 * evaluate_function: try to pre-evaluate a function call
2682 * We can do this if the function is strict and has any constant-null inputs
2683 * (just return a null constant), or if the function is immutable and has all
2684 * constant inputs (call it and return the result as a Const node). In
2685 * estimation mode we are willing to pre-evaluate stable functions too.
2687 * Returns a simplified expression if successful, or NULL if cannot
2688 * simplify the function.
2691 evaluate_function(Oid funcid, Oid result_type, List *args,
2692 HeapTuple func_tuple,
2693 eval_const_expressions_context *context)
2695 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
2696 bool has_nonconst_input = false;
2697 bool has_null_input = false;
2702 * Can't simplify if it returns a set.
2704 if (funcform->proretset)
2708 * Can't simplify if it returns RECORD. The immediate problem is that it
2709 * will be needing an expected tupdesc which we can't supply here.
2711 * In the case where it has OUT parameters, it could get by without an
2712 * expected tupdesc, but we still have issues: get_expr_result_type()
2713 * doesn't know how to extract type info from a RECORD constant, and in
2714 * the case of a NULL function result there doesn't seem to be any clean
2715 * way to fix that. In view of the likelihood of there being still other
2716 * gotchas, seems best to leave the function call unreduced.
2718 if (funcform->prorettype == RECORDOID)
2722 * Check for constant inputs and especially constant-NULL inputs.
2726 if (IsA(lfirst(arg), Const))
2727 has_null_input |= ((Const *) lfirst(arg))->constisnull;
2729 has_nonconst_input = true;
2733 * If the function is strict and has a constant-NULL input, it will never
2734 * be called at all, so we can replace the call by a NULL constant, even
2735 * if there are other inputs that aren't constant, and even if the
2736 * function is not otherwise immutable.
2738 if (funcform->proisstrict && has_null_input)
2739 return (Expr *) makeNullConst(result_type);
2742 * Otherwise, can simplify only if all inputs are constants. (For a
2743 * non-strict function, constant NULL inputs are treated the same as
2744 * constant non-NULL inputs.)
2746 if (has_nonconst_input)
2750 * Ordinarily we are only allowed to simplify immutable functions. But for
2751 * purposes of estimation, we consider it okay to simplify functions that
2752 * are merely stable; the risk that the result might change from planning
2753 * time to execution time is worth taking in preference to not being able
2754 * to estimate the value at all.
2756 if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
2758 else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
2764 * OK, looks like we can simplify this operator/function.
2766 * Build a new FuncExpr node containing the already-simplified arguments.
2768 newexpr = makeNode(FuncExpr);
2769 newexpr->funcid = funcid;
2770 newexpr->funcresulttype = result_type;
2771 newexpr->funcretset = false;
2772 newexpr->funcformat = COERCE_DONTCARE; /* doesn't matter */
2773 newexpr->args = args;
2775 return evaluate_expr((Expr *) newexpr, result_type);
2779 * inline_function: try to expand a function call inline
2781 * If the function is a sufficiently simple SQL-language function
2782 * (just "SELECT expression"), then we can inline it and avoid the rather
2783 * high per-call overhead of SQL functions. Furthermore, this can expose
2784 * opportunities for constant-folding within the function expression.
2786 * We have to beware of some special cases however. A directly or
2787 * indirectly recursive function would cause us to recurse forever,
2788 * so we keep track of which functions we are already expanding and
2789 * do not re-expand them. Also, if a parameter is used more than once
2790 * in the SQL-function body, we require it not to contain any volatile
2791 * functions (volatiles might deliver inconsistent answers) nor to be
2792 * unreasonably expensive to evaluate. The expensiveness check not only
2793 * prevents us from doing multiple evaluations of an expensive parameter
2794 * at runtime, but is a safety value to limit growth of an expression due
2795 * to repeated inlining.
2797 * We must also beware of changing the volatility or strictness status of
2798 * functions by inlining them.
2800 * Returns a simplified expression if successful, or NULL if cannot
2801 * simplify the function.
2804 inline_function(Oid funcid, Oid result_type, List *args,
2805 HeapTuple func_tuple,
2806 eval_const_expressions_context *context)
2808 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
2813 MemoryContext oldcxt;
2814 MemoryContext mycxt;
2815 ErrorContextCallback sqlerrcontext;
2816 List *raw_parsetree_list;
2817 List *querytree_list;
2825 * Forget it if the function is not SQL-language or has other showstopper
2826 * properties. (The nargs check is just paranoia.)
2828 if (funcform->prolang != SQLlanguageId ||
2829 funcform->prosecdef ||
2830 funcform->proretset ||
2831 funcform->pronargs != list_length(args))
2834 /* Check for recursive function, and give up trying to expand if so */
2835 if (list_member_oid(context->active_fns, funcid))
2838 /* Check permission to call function (fail later, if not) */
2839 if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
2843 * Setup error traceback support for ereport(). This is so that we can
2844 * finger the function that bad information came from.
2846 sqlerrcontext.callback = sql_inline_error_callback;
2847 sqlerrcontext.arg = func_tuple;
2848 sqlerrcontext.previous = error_context_stack;
2849 error_context_stack = &sqlerrcontext;
2852 * Make a temporary memory context, so that we don't leak all the stuff
2853 * that parsing might create.
2855 mycxt = AllocSetContextCreate(CurrentMemoryContext,
2857 ALLOCSET_DEFAULT_MINSIZE,
2858 ALLOCSET_DEFAULT_INITSIZE,
2859 ALLOCSET_DEFAULT_MAXSIZE);
2860 oldcxt = MemoryContextSwitchTo(mycxt);
2862 /* Check for polymorphic arguments, and substitute actual arg types */
2863 argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
2864 memcpy(argtypes, funcform->proargtypes.values,
2865 funcform->pronargs * sizeof(Oid));
2866 for (i = 0; i < funcform->pronargs; i++)
2868 if (argtypes[i] == ANYARRAYOID ||
2869 argtypes[i] == ANYELEMENTOID)
2871 argtypes[i] = exprType((Node *) list_nth(args, i));
2875 /* Fetch and parse the function body */
2876 tmp = SysCacheGetAttr(PROCOID,
2878 Anum_pg_proc_prosrc,
2881 elog(ERROR, "null prosrc for function %u", funcid);
2882 src = DatumGetCString(DirectFunctionCall1(textout, tmp));
2885 * We just do parsing and parse analysis, not rewriting, because rewriting
2886 * will not affect table-free-SELECT-only queries, which is all that we
2887 * care about. Also, we can punt as soon as we detect more than one
2888 * command in the function body.
2890 raw_parsetree_list = pg_parse_query(src);
2891 if (list_length(raw_parsetree_list) != 1)
2894 querytree_list = parse_analyze(linitial(raw_parsetree_list), src,
2895 argtypes, funcform->pronargs);
2897 if (list_length(querytree_list) != 1)
2900 querytree = (Query *) linitial(querytree_list);
2903 * The single command must be a simple "SELECT expression".
2905 if (!IsA(querytree, Query) ||
2906 querytree->commandType != CMD_SELECT ||
2908 querytree->hasAggs ||
2909 querytree->hasSubLinks ||
2910 querytree->rtable ||
2911 querytree->jointree->fromlist ||
2912 querytree->jointree->quals ||
2913 querytree->groupClause ||
2914 querytree->havingQual ||
2915 querytree->distinctClause ||
2916 querytree->sortClause ||
2917 querytree->limitOffset ||
2918 querytree->limitCount ||
2919 querytree->setOperations ||
2920 list_length(querytree->targetList) != 1)
2923 newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
2926 * Make sure the function (still) returns what it's declared to. This will
2927 * raise an error if wrong, but that's okay since the function would fail
2928 * at runtime anyway. Note we do not try this until we have verified that
2929 * no rewriting was needed; that's probably not important, but let's be
2932 (void) check_sql_fn_retval(funcid, result_type, querytree_list, NULL);
2935 * Additional validity checks on the expression. It mustn't return a set,
2936 * and it mustn't be more volatile than the surrounding function (this is
2937 * to avoid breaking hacks that involve pretending a function is immutable
2938 * when it really ain't). If the surrounding function is declared strict,
2939 * then the expression must contain only strict constructs and must use
2940 * all of the function parameters (this is overkill, but an exact analysis
2943 if (expression_returns_set(newexpr))
2946 if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
2947 contain_mutable_functions(newexpr))
2949 else if (funcform->provolatile == PROVOLATILE_STABLE &&
2950 contain_volatile_functions(newexpr))
2953 if (funcform->proisstrict &&
2954 contain_nonstrict_functions(newexpr))
2958 * We may be able to do it; there are still checks on parameter usage to
2959 * make, but those are most easily done in combination with the actual
2960 * substitution of the inputs. So start building expression with inputs
2963 usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
2964 newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
2967 /* Now check for parameter usage */
2971 Node *param = lfirst(arg);
2973 if (usecounts[i] == 0)
2975 /* Param not used at all: uncool if func is strict */
2976 if (funcform->proisstrict)
2979 else if (usecounts[i] != 1)
2981 /* Param used multiple times: uncool if expensive or volatile */
2985 * We define "expensive" as "contains any subplan or more than 10
2986 * operators". Note that the subplan search has to be done
2987 * explicitly, since cost_qual_eval() will barf on unplanned
2990 if (contain_subplans(param))
2992 cost_qual_eval(&eval_cost, list_make1(param));
2993 if (eval_cost.startup + eval_cost.per_tuple >
2994 10 * cpu_operator_cost)
2998 * Check volatility last since this is more expensive than the
3001 if (contain_volatile_functions(param))
3008 * Whew --- we can make the substitution. Copy the modified expression
3009 * out of the temporary memory context, and clean up.
3011 MemoryContextSwitchTo(oldcxt);
3013 newexpr = copyObject(newexpr);
3015 MemoryContextDelete(mycxt);
3018 * Recursively try to simplify the modified expression. Here we must add
3019 * the current function to the context list of active functions.
3021 context->active_fns = lcons_oid(funcid, context->active_fns);
3022 newexpr = eval_const_expressions_mutator(newexpr, context);
3023 context->active_fns = list_delete_first(context->active_fns);
3025 error_context_stack = sqlerrcontext.previous;
3027 return (Expr *) newexpr;
3029 /* Here if func is not inlinable: release temp memory and return NULL */
3031 MemoryContextSwitchTo(oldcxt);
3032 MemoryContextDelete(mycxt);
3033 error_context_stack = sqlerrcontext.previous;
3039 * Replace Param nodes by appropriate actual parameters
3042 substitute_actual_parameters(Node *expr, int nargs, List *args,
3045 substitute_actual_parameters_context context;
3047 context.nargs = nargs;
3048 context.args = args;
3049 context.usecounts = usecounts;
3051 return substitute_actual_parameters_mutator(expr, &context);
3055 substitute_actual_parameters_mutator(Node *node,
3056 substitute_actual_parameters_context *context)
3060 if (IsA(node, Param))
3062 Param *param = (Param *) node;
3064 if (param->paramkind != PARAM_EXTERN)
3065 elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
3066 if (param->paramid <= 0 || param->paramid > context->nargs)
3067 elog(ERROR, "invalid paramid: %d", param->paramid);
3069 /* Count usage of parameter */
3070 context->usecounts[param->paramid - 1]++;
3072 /* Select the appropriate actual arg and replace the Param with it */
3073 /* We don't need to copy at this time (it'll get done later) */
3074 return list_nth(context->args, param->paramid - 1);
3076 return expression_tree_mutator(node, substitute_actual_parameters_mutator,
3081 * error context callback to let us supply a call-stack traceback
3084 sql_inline_error_callback(void *arg)
3086 HeapTuple func_tuple = (HeapTuple) arg;
3087 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3088 int syntaxerrposition;
3090 /* If it's a syntax error, convert to internal syntax error report */
3091 syntaxerrposition = geterrposition();
3092 if (syntaxerrposition > 0)
3098 tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
3101 elog(ERROR, "null prosrc");
3102 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
3104 internalerrposition(syntaxerrposition);
3105 internalerrquery(prosrc);
3108 errcontext("SQL function \"%s\" during inlining",
3109 NameStr(funcform->proname));
3113 * evaluate_expr: pre-evaluate a constant expression
3115 * We use the executor's routine ExecEvalExpr() to avoid duplication of
3116 * code and ensure we get the same result as the executor would get.
3119 evaluate_expr(Expr *expr, Oid result_type)
3122 ExprState *exprstate;
3123 MemoryContext oldcontext;
3127 bool resultTypByVal;
3130 * To use the executor, we need an EState.
3132 estate = CreateExecutorState();
3134 /* We can use the estate's working context to avoid memory leaks. */
3135 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3138 * Prepare expr for execution.
3140 exprstate = ExecPrepareExpr(expr, estate);
3145 * It is OK to use a default econtext because none of the ExecEvalExpr()
3146 * code used in this situation will use econtext. That might seem
3147 * fortuitous, but it's not so unreasonable --- a constant expression does
3148 * not depend on context, by definition, n'est ce pas?
3150 const_val = ExecEvalExprSwitchContext(exprstate,
3151 GetPerTupleExprContext(estate),
3152 &const_is_null, NULL);
3154 /* Get info needed about result datatype */
3155 get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
3157 /* Get back to outer memory context */
3158 MemoryContextSwitchTo(oldcontext);
3160 /* Must copy result out of sub-context used by expression eval */
3162 const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
3164 /* Release all the junk we just created */
3165 FreeExecutorState(estate);
3168 * Make the constant result node.
3170 return (Expr *) makeConst(result_type, resultTypLen,
3171 const_val, const_is_null,
3177 * Standard expression-tree walking support
3179 * We used to have near-duplicate code in many different routines that
3180 * understood how to recurse through an expression node tree. That was
3181 * a pain to maintain, and we frequently had bugs due to some particular
3182 * routine neglecting to support a particular node type. In most cases,
3183 * these routines only actually care about certain node types, and don't
3184 * care about other types except insofar as they have to recurse through
3185 * non-primitive node types. Therefore, we now provide generic tree-walking
3186 * logic to consolidate the redundant "boilerplate" code. There are
3187 * two versions: expression_tree_walker() and expression_tree_mutator().
3190 /*--------------------
3191 * expression_tree_walker() is designed to support routines that traverse
3192 * a tree in a read-only fashion (although it will also work for routines
3193 * that modify nodes in-place but never add/delete/replace nodes).
3194 * A walker routine should look like this:
3196 * bool my_walker (Node *node, my_struct *context)
3200 * // check for nodes that special work is required for, eg:
3201 * if (IsA(node, Var))
3203 * ... do special actions for Var nodes
3205 * else if (IsA(node, ...))
3207 * ... do special actions for other node types
3209 * // for any node type not specially processed, do:
3210 * return expression_tree_walker(node, my_walker, (void *) context);
3213 * The "context" argument points to a struct that holds whatever context
3214 * information the walker routine needs --- it can be used to return data
3215 * gathered by the walker, too. This argument is not touched by
3216 * expression_tree_walker, but it is passed down to recursive sub-invocations
3217 * of my_walker. The tree walk is started from a setup routine that
3218 * fills in the appropriate context struct, calls my_walker with the top-level
3219 * node of the tree, and then examines the results.
3221 * The walker routine should return "false" to continue the tree walk, or
3222 * "true" to abort the walk and immediately return "true" to the top-level
3223 * caller. This can be used to short-circuit the traversal if the walker
3224 * has found what it came for. "false" is returned to the top-level caller
3225 * iff no invocation of the walker returned "true".
3227 * The node types handled by expression_tree_walker include all those
3228 * normally found in target lists and qualifier clauses during the planning
3229 * stage. In particular, it handles List nodes since a cnf-ified qual clause
3230 * will have List structure at the top level, and it handles TargetEntry nodes
3231 * so that a scan of a target list can be handled without additional code.
3232 * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
3233 * handled, so that query jointrees and setOperation trees can be processed
3234 * without additional code.
3236 * expression_tree_walker will handle SubLink nodes by recursing normally
3237 * into the "testexpr" subtree (which is an expression belonging to the outer
3238 * plan). It will also call the walker on the sub-Query node; however, when
3239 * expression_tree_walker itself is called on a Query node, it does nothing
3240 * and returns "false". The net effect is that unless the walker does
3241 * something special at a Query node, sub-selects will not be visited during
3242 * an expression tree walk. This is exactly the behavior wanted in many cases
3243 * --- and for those walkers that do want to recurse into sub-selects, special
3244 * behavior is typically needed anyway at the entry to a sub-select (such as
3245 * incrementing a depth counter). A walker that wants to examine sub-selects
3246 * should include code along the lines of:
3248 * if (IsA(node, Query))
3250 * adjust context for subquery;
3251 * result = query_tree_walker((Query *) node, my_walker, context,
3252 * 0); // adjust flags as needed
3253 * restore context if needed;
3257 * query_tree_walker is a convenience routine (see below) that calls the
3258 * walker on all the expression subtrees of the given Query node.
3260 * expression_tree_walker will handle SubPlan nodes by recursing normally
3261 * into the "testexpr" and the "args" list (which are expressions belonging to
3262 * the outer plan). It will not touch the completed subplan, however. Since
3263 * there is no link to the original Query, it is not possible to recurse into
3264 * subselects of an already-planned expression tree. This is OK for current
3265 * uses, but may need to be revisited in future.
3266 *--------------------
3270 expression_tree_walker(Node *node,
3277 * The walker has already visited the current node, and so we need only
3278 * recurse into any sub-nodes it has.
3280 * We assume that the walker is not interested in List nodes per se, so
3281 * when we expect a List we just recurse directly to self without
3282 * bothering to call the walker.
3287 /* Guard against stack overflow due to overly complex expressions */
3288 check_stack_depth();
3290 switch (nodeTag(node))
3295 case T_CoerceToDomainValue:
3296 case T_CaseTestExpr:
3297 case T_SetToDefault:
3299 case T_OuterJoinInfo:
3300 /* primitive node types with no expression subnodes */
3304 Aggref *expr = (Aggref *) node;
3306 /* recurse directly on List */
3307 if (expression_tree_walker((Node *) expr->args,
3314 ArrayRef *aref = (ArrayRef *) node;
3316 /* recurse directly for upper/lower array index lists */
3317 if (expression_tree_walker((Node *) aref->refupperindexpr,
3320 if (expression_tree_walker((Node *) aref->reflowerindexpr,
3323 /* walker must see the refexpr and refassgnexpr, however */
3324 if (walker(aref->refexpr, context))
3326 if (walker(aref->refassgnexpr, context))
3332 FuncExpr *expr = (FuncExpr *) node;
3334 if (expression_tree_walker((Node *) expr->args,
3341 OpExpr *expr = (OpExpr *) node;
3343 if (expression_tree_walker((Node *) expr->args,
3348 case T_DistinctExpr:
3350 DistinctExpr *expr = (DistinctExpr *) node;
3352 if (expression_tree_walker((Node *) expr->args,
3357 case T_ScalarArrayOpExpr:
3359 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3361 if (expression_tree_walker((Node *) expr->args,
3368 BoolExpr *expr = (BoolExpr *) node;
3370 if (expression_tree_walker((Node *) expr->args,
3377 SubLink *sublink = (SubLink *) node;
3379 if (walker(sublink->testexpr, context))
3383 * Also invoke the walker on the sublink's Query node, so it
3384 * can recurse into the sub-query if it wants to.
3386 return walker(sublink->subselect, context);
3391 SubPlan *subplan = (SubPlan *) node;
3393 /* recurse into the testexpr, but not into the Plan */
3394 if (walker(subplan->testexpr, context))
3396 /* also examine args list */
3397 if (expression_tree_walker((Node *) subplan->args,
3403 return walker(((FieldSelect *) node)->arg, context);
3406 FieldStore *fstore = (FieldStore *) node;
3408 if (walker(fstore->arg, context))
3410 if (walker(fstore->newvals, context))
3415 return walker(((RelabelType *) node)->arg, context);
3416 case T_ConvertRowtypeExpr:
3417 return walker(((ConvertRowtypeExpr *) node)->arg, context);
3420 CaseExpr *caseexpr = (CaseExpr *) node;
3422 if (walker(caseexpr->arg, context))
3424 /* we assume walker doesn't care about CaseWhens, either */
3425 foreach(temp, caseexpr->args)
3427 CaseWhen *when = (CaseWhen *) lfirst(temp);
3429 Assert(IsA(when, CaseWhen));
3430 if (walker(when->expr, context))
3432 if (walker(when->result, context))
3435 if (walker(caseexpr->defresult, context))
3440 return walker(((ArrayExpr *) node)->elements, context);
3442 return walker(((RowExpr *) node)->args, context);
3443 case T_RowCompareExpr:
3445 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3447 if (walker(rcexpr->largs, context))
3449 if (walker(rcexpr->rargs, context))
3453 case T_CoalesceExpr:
3454 return walker(((CoalesceExpr *) node)->args, context);
3456 return walker(((MinMaxExpr *) node)->args, context);
3459 XmlExpr *xexpr = (XmlExpr *) node;
3461 if (walker(xexpr->named_args, context))
3463 /* we assume walker doesn't care about arg_names */
3464 if (walker(xexpr->args, context))
3469 return walker(((NullIfExpr *) node)->args, context);
3471 return walker(((NullTest *) node)->arg, context);
3473 return walker(((BooleanTest *) node)->arg, context);
3474 case T_CoerceToDomain:
3475 return walker(((CoerceToDomain *) node)->arg, context);
3477 return walker(((TargetEntry *) node)->expr, context);
3479 /* Do nothing with a sub-Query, per discussion above */
3482 foreach(temp, (List *) node)
3484 if (walker((Node *) lfirst(temp), context))
3490 FromExpr *from = (FromExpr *) node;
3492 if (walker(from->fromlist, context))
3494 if (walker(from->quals, context))
3500 JoinExpr *join = (JoinExpr *) node;
3502 if (walker(join->larg, context))
3504 if (walker(join->rarg, context))
3506 if (walker(join->quals, context))
3510 * alias clause, using list are deemed uninteresting.
3514 case T_SetOperationStmt:
3516 SetOperationStmt *setop = (SetOperationStmt *) node;
3518 if (walker(setop->larg, context))
3520 if (walker(setop->rarg, context))
3524 case T_InClauseInfo:
3526 InClauseInfo *ininfo = (InClauseInfo *) node;
3528 if (expression_tree_walker((Node *) ininfo->sub_targetlist,
3533 case T_AppendRelInfo:
3535 AppendRelInfo *appinfo = (AppendRelInfo *) node;
3537 if (expression_tree_walker((Node *) appinfo->translated_vars,
3543 elog(ERROR, "unrecognized node type: %d",
3544 (int) nodeTag(node));
3551 * query_tree_walker --- initiate a walk of a Query's expressions
3553 * This routine exists just to reduce the number of places that need to know
3554 * where all the expression subtrees of a Query are. Note it can be used
3555 * for starting a walk at top level of a Query regardless of whether the
3556 * walker intends to descend into subqueries. It is also useful for
3557 * descending into subqueries within a walker.
3559 * Some callers want to suppress visitation of certain items in the sub-Query,
3560 * typically because they need to process them specially, or don't actually
3561 * want to recurse into subqueries. This is supported by the flags argument,
3562 * which is the bitwise OR of flag values to suppress visitation of
3563 * indicated items. (More flag bits may be added as needed.)
3566 query_tree_walker(Query *query,
3571 Assert(query != NULL && IsA(query, Query));
3573 if (walker((Node *) query->targetList, context))
3575 if (walker((Node *) query->returningList, context))
3577 if (walker((Node *) query->jointree, context))
3579 if (walker(query->setOperations, context))
3581 if (walker(query->havingQual, context))
3583 if (walker(query->limitOffset, context))
3585 if (walker(query->limitCount, context))
3587 if (range_table_walker(query->rtable, walker, context, flags))
3589 if (query->utilityStmt)
3592 * Certain utility commands contain general-purpose Querys embedded in
3593 * them --- if this is one, invoke the walker on the sub-Query.
3595 if (IsA(query->utilityStmt, CopyStmt))
3597 if (walker(((CopyStmt *) query->utilityStmt)->query, context))
3600 if (IsA(query->utilityStmt, DeclareCursorStmt))
3602 if (walker(((DeclareCursorStmt *) query->utilityStmt)->query, context))
3605 if (IsA(query->utilityStmt, ExplainStmt))
3607 if (walker(((ExplainStmt *) query->utilityStmt)->query, context))
3610 if (IsA(query->utilityStmt, PrepareStmt))
3612 if (walker(((PrepareStmt *) query->utilityStmt)->query, context))
3615 if (IsA(query->utilityStmt, ViewStmt))
3617 if (walker(((ViewStmt *) query->utilityStmt)->query, context))
3625 * range_table_walker is just the part of query_tree_walker that scans
3626 * a query's rangetable. This is split out since it can be useful on
3630 range_table_walker(List *rtable,
3639 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3641 switch (rte->rtekind)
3648 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3649 if (walker(rte->subquery, context))
3653 if (!(flags & QTW_IGNORE_JOINALIASES))
3654 if (walker(rte->joinaliasvars, context))
3658 if (walker(rte->funcexpr, context))
3662 if (walker(rte->values_lists, context))
3671 /*--------------------
3672 * expression_tree_mutator() is designed to support routines that make a
3673 * modified copy of an expression tree, with some nodes being added,
3674 * removed, or replaced by new subtrees. The original tree is (normally)
3675 * not changed. Each recursion level is responsible for returning a copy of
3676 * (or appropriately modified substitute for) the subtree it is handed.
3677 * A mutator routine should look like this:
3679 * Node * my_mutator (Node *node, my_struct *context)
3683 * // check for nodes that special work is required for, eg:
3684 * if (IsA(node, Var))
3686 * ... create and return modified copy of Var node
3688 * else if (IsA(node, ...))
3690 * ... do special transformations of other node types
3692 * // for any node type not specially processed, do:
3693 * return expression_tree_mutator(node, my_mutator, (void *) context);
3696 * The "context" argument points to a struct that holds whatever context
3697 * information the mutator routine needs --- it can be used to return extra
3698 * data gathered by the mutator, too. This argument is not touched by
3699 * expression_tree_mutator, but it is passed down to recursive sub-invocations
3700 * of my_mutator. The tree walk is started from a setup routine that
3701 * fills in the appropriate context struct, calls my_mutator with the
3702 * top-level node of the tree, and does any required post-processing.
3704 * Each level of recursion must return an appropriately modified Node.
3705 * If expression_tree_mutator() is called, it will make an exact copy
3706 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
3707 * of that Node. In this way, my_mutator() has full control over the
3708 * copying process but need not directly deal with expression trees
3709 * that it has no interest in.
3711 * Just as for expression_tree_walker, the node types handled by
3712 * expression_tree_mutator include all those normally found in target lists
3713 * and qualifier clauses during the planning stage.
3715 * expression_tree_mutator will handle SubLink nodes by recursing normally
3716 * into the "testexpr" subtree (which is an expression belonging to the outer
3717 * plan). It will also call the mutator on the sub-Query node; however, when
3718 * expression_tree_mutator itself is called on a Query node, it does nothing
3719 * and returns the unmodified Query node. The net effect is that unless the
3720 * mutator does something special at a Query node, sub-selects will not be
3721 * visited or modified; the original sub-select will be linked to by the new
3722 * SubLink node. Mutators that want to descend into sub-selects will usually
3723 * do so by recognizing Query nodes and calling query_tree_mutator (below).
3725 * expression_tree_mutator will handle a SubPlan node by recursing into the
3726 * "testexpr" and the "args" list (which belong to the outer plan), but it
3727 * will simply copy the link to the inner plan, since that's typically what
3728 * expression tree mutators want. A mutator that wants to modify the subplan
3729 * can force appropriate behavior by recognizing SubPlan expression nodes
3730 * and doing the right thing.
3731 *--------------------
3735 expression_tree_mutator(Node *node,
3736 Node *(*mutator) (),
3740 * The mutator has already decided not to modify the current node, but we
3741 * must call the mutator for any sub-nodes.
3744 #define FLATCOPY(newnode, node, nodetype) \
3745 ( (newnode) = makeNode(nodetype), \
3746 memcpy((newnode), (node), sizeof(nodetype)) )
3748 #define CHECKFLATCOPY(newnode, node, nodetype) \
3749 ( AssertMacro(IsA((node), nodetype)), \
3750 (newnode) = makeNode(nodetype), \
3751 memcpy((newnode), (node), sizeof(nodetype)) )
3753 #define MUTATE(newfield, oldfield, fieldtype) \
3754 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
3759 /* Guard against stack overflow due to overly complex expressions */
3760 check_stack_depth();
3762 switch (nodeTag(node))
3767 case T_CoerceToDomainValue:
3768 case T_CaseTestExpr:
3769 case T_SetToDefault:
3771 case T_OuterJoinInfo:
3772 /* primitive node types with no expression subnodes */
3773 return (Node *) copyObject(node);
3776 Aggref *aggref = (Aggref *) node;
3779 FLATCOPY(newnode, aggref, Aggref);
3780 MUTATE(newnode->args, aggref->args, List *);
3781 return (Node *) newnode;
3786 ArrayRef *arrayref = (ArrayRef *) node;
3789 FLATCOPY(newnode, arrayref, ArrayRef);
3790 MUTATE(newnode->refupperindexpr, arrayref->refupperindexpr,
3792 MUTATE(newnode->reflowerindexpr, arrayref->reflowerindexpr,
3794 MUTATE(newnode->refexpr, arrayref->refexpr,
3796 MUTATE(newnode->refassgnexpr, arrayref->refassgnexpr,
3798 return (Node *) newnode;
3803 FuncExpr *expr = (FuncExpr *) node;
3806 FLATCOPY(newnode, expr, FuncExpr);
3807 MUTATE(newnode->args, expr->args, List *);
3808 return (Node *) newnode;
3813 OpExpr *expr = (OpExpr *) node;
3816 FLATCOPY(newnode, expr, OpExpr);
3817 MUTATE(newnode->args, expr->args, List *);
3818 return (Node *) newnode;
3821 case T_DistinctExpr:
3823 DistinctExpr *expr = (DistinctExpr *) node;
3824 DistinctExpr *newnode;
3826 FLATCOPY(newnode, expr, DistinctExpr);
3827 MUTATE(newnode->args, expr->args, List *);
3828 return (Node *) newnode;
3831 case T_ScalarArrayOpExpr:
3833 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3834 ScalarArrayOpExpr *newnode;
3836 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
3837 MUTATE(newnode->args, expr->args, List *);
3838 return (Node *) newnode;
3843 BoolExpr *expr = (BoolExpr *) node;
3846 FLATCOPY(newnode, expr, BoolExpr);
3847 MUTATE(newnode->args, expr->args, List *);
3848 return (Node *) newnode;
3853 SubLink *sublink = (SubLink *) node;
3856 FLATCOPY(newnode, sublink, SubLink);
3857 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
3860 * Also invoke the mutator on the sublink's Query node, so it
3861 * can recurse into the sub-query if it wants to.
3863 MUTATE(newnode->subselect, sublink->subselect, Node *);
3864 return (Node *) newnode;
3869 SubPlan *subplan = (SubPlan *) node;
3872 FLATCOPY(newnode, subplan, SubPlan);
3873 /* transform testexpr */
3874 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
3875 /* transform args list (params to be passed to subplan) */
3876 MUTATE(newnode->args, subplan->args, List *);
3877 /* but not the sub-Plan itself, which is referenced as-is */
3878 return (Node *) newnode;
3883 FieldSelect *fselect = (FieldSelect *) node;
3884 FieldSelect *newnode;
3886 FLATCOPY(newnode, fselect, FieldSelect);
3887 MUTATE(newnode->arg, fselect->arg, Expr *);
3888 return (Node *) newnode;
3893 FieldStore *fstore = (FieldStore *) node;
3894 FieldStore *newnode;
3896 FLATCOPY(newnode, fstore, FieldStore);
3897 MUTATE(newnode->arg, fstore->arg, Expr *);
3898 MUTATE(newnode->newvals, fstore->newvals, List *);
3899 newnode->fieldnums = list_copy(fstore->fieldnums);
3900 return (Node *) newnode;
3905 RelabelType *relabel = (RelabelType *) node;
3906 RelabelType *newnode;
3908 FLATCOPY(newnode, relabel, RelabelType);
3909 MUTATE(newnode->arg, relabel->arg, Expr *);
3910 return (Node *) newnode;
3913 case T_ConvertRowtypeExpr:
3915 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
3916 ConvertRowtypeExpr *newnode;
3918 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
3919 MUTATE(newnode->arg, convexpr->arg, Expr *);
3920 return (Node *) newnode;
3925 CaseExpr *caseexpr = (CaseExpr *) node;
3928 FLATCOPY(newnode, caseexpr, CaseExpr);
3929 MUTATE(newnode->arg, caseexpr->arg, Expr *);
3930 MUTATE(newnode->args, caseexpr->args, List *);
3931 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
3932 return (Node *) newnode;
3937 CaseWhen *casewhen = (CaseWhen *) node;
3940 FLATCOPY(newnode, casewhen, CaseWhen);
3941 MUTATE(newnode->expr, casewhen->expr, Expr *);
3942 MUTATE(newnode->result, casewhen->result, Expr *);
3943 return (Node *) newnode;
3948 ArrayExpr *arrayexpr = (ArrayExpr *) node;
3951 FLATCOPY(newnode, arrayexpr, ArrayExpr);
3952 MUTATE(newnode->elements, arrayexpr->elements, List *);
3953 return (Node *) newnode;
3958 RowExpr *rowexpr = (RowExpr *) node;
3961 FLATCOPY(newnode, rowexpr, RowExpr);
3962 MUTATE(newnode->args, rowexpr->args, List *);
3963 return (Node *) newnode;
3966 case T_RowCompareExpr:
3968 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3969 RowCompareExpr *newnode;
3971 FLATCOPY(newnode, rcexpr, RowCompareExpr);
3972 MUTATE(newnode->largs, rcexpr->largs, List *);
3973 MUTATE(newnode->rargs, rcexpr->rargs, List *);
3974 return (Node *) newnode;
3977 case T_CoalesceExpr:
3979 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3980 CoalesceExpr *newnode;
3982 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
3983 MUTATE(newnode->args, coalesceexpr->args, List *);
3984 return (Node *) newnode;
3989 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3990 MinMaxExpr *newnode;
3992 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
3993 MUTATE(newnode->args, minmaxexpr->args, List *);
3994 return (Node *) newnode;
3999 XmlExpr *xexpr = (XmlExpr *) node;
4002 FLATCOPY(newnode, xexpr, XmlExpr);
4003 MUTATE(newnode->named_args, xexpr->named_args, List *);
4004 /* assume mutator does not care about arg_names */
4005 MUTATE(newnode->args, xexpr->args, List *);
4006 return (Node *) newnode;
4011 NullIfExpr *expr = (NullIfExpr *) node;
4012 NullIfExpr *newnode;
4014 FLATCOPY(newnode, expr, NullIfExpr);
4015 MUTATE(newnode->args, expr->args, List *);
4016 return (Node *) newnode;
4021 NullTest *ntest = (NullTest *) node;
4024 FLATCOPY(newnode, ntest, NullTest);
4025 MUTATE(newnode->arg, ntest->arg, Expr *);
4026 return (Node *) newnode;
4031 BooleanTest *btest = (BooleanTest *) node;
4032 BooleanTest *newnode;
4034 FLATCOPY(newnode, btest, BooleanTest);
4035 MUTATE(newnode->arg, btest->arg, Expr *);
4036 return (Node *) newnode;
4039 case T_CoerceToDomain:
4041 CoerceToDomain *ctest = (CoerceToDomain *) node;
4042 CoerceToDomain *newnode;
4044 FLATCOPY(newnode, ctest, CoerceToDomain);
4045 MUTATE(newnode->arg, ctest->arg, Expr *);
4046 return (Node *) newnode;
4051 TargetEntry *targetentry = (TargetEntry *) node;
4052 TargetEntry *newnode;
4054 FLATCOPY(newnode, targetentry, TargetEntry);
4055 MUTATE(newnode->expr, targetentry->expr, Expr *);
4056 return (Node *) newnode;
4060 /* Do nothing with a sub-Query, per discussion above */
4065 * We assume the mutator isn't interested in the list nodes
4066 * per se, so just invoke it on each list element. NOTE: this
4067 * would fail badly on a list with integer elements!
4073 foreach(temp, (List *) node)
4075 resultlist = lappend(resultlist,
4076 mutator((Node *) lfirst(temp),
4079 return (Node *) resultlist;
4084 FromExpr *from = (FromExpr *) node;
4087 FLATCOPY(newnode, from, FromExpr);
4088 MUTATE(newnode->fromlist, from->fromlist, List *);
4089 MUTATE(newnode->quals, from->quals, Node *);
4090 return (Node *) newnode;
4095 JoinExpr *join = (JoinExpr *) node;
4098 FLATCOPY(newnode, join, JoinExpr);
4099 MUTATE(newnode->larg, join->larg, Node *);
4100 MUTATE(newnode->rarg, join->rarg, Node *);
4101 MUTATE(newnode->quals, join->quals, Node *);
4102 /* We do not mutate alias or using by default */
4103 return (Node *) newnode;
4106 case T_SetOperationStmt:
4108 SetOperationStmt *setop = (SetOperationStmt *) node;
4109 SetOperationStmt *newnode;
4111 FLATCOPY(newnode, setop, SetOperationStmt);
4112 MUTATE(newnode->larg, setop->larg, Node *);
4113 MUTATE(newnode->rarg, setop->rarg, Node *);
4114 return (Node *) newnode;
4117 case T_InClauseInfo:
4119 InClauseInfo *ininfo = (InClauseInfo *) node;
4120 InClauseInfo *newnode;
4122 FLATCOPY(newnode, ininfo, InClauseInfo);
4123 MUTATE(newnode->sub_targetlist, ininfo->sub_targetlist, List *);
4124 /* Assume we need not make a copy of in_operators list */
4125 return (Node *) newnode;
4128 case T_AppendRelInfo:
4130 AppendRelInfo *appinfo = (AppendRelInfo *) node;
4131 AppendRelInfo *newnode;
4133 FLATCOPY(newnode, appinfo, AppendRelInfo);
4134 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
4135 return (Node *) newnode;
4139 elog(ERROR, "unrecognized node type: %d",
4140 (int) nodeTag(node));
4143 /* can't get here, but keep compiler happy */
4149 * query_tree_mutator --- initiate modification of a Query's expressions
4151 * This routine exists just to reduce the number of places that need to know
4152 * where all the expression subtrees of a Query are. Note it can be used
4153 * for starting a walk at top level of a Query regardless of whether the
4154 * mutator intends to descend into subqueries. It is also useful for
4155 * descending into subqueries within a mutator.
4157 * Some callers want to suppress mutating of certain items in the Query,
4158 * typically because they need to process them specially, or don't actually
4159 * want to recurse into subqueries. This is supported by the flags argument,
4160 * which is the bitwise OR of flag values to suppress mutating of
4161 * indicated items. (More flag bits may be added as needed.)
4163 * Normally the Query node itself is copied, but some callers want it to be
4164 * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags. All
4165 * modified substructure is safely copied in any case.
4168 query_tree_mutator(Query *query,
4169 Node *(*mutator) (),
4173 Assert(query != NULL && IsA(query, Query));
4175 if (!(flags & QTW_DONT_COPY_QUERY))
4179 FLATCOPY(newquery, query, Query);
4183 MUTATE(query->targetList, query->targetList, List *);
4184 MUTATE(query->returningList, query->returningList, List *);
4185 MUTATE(query->jointree, query->jointree, FromExpr *);
4186 MUTATE(query->setOperations, query->setOperations, Node *);
4187 MUTATE(query->havingQual, query->havingQual, Node *);
4188 MUTATE(query->limitOffset, query->limitOffset, Node *);
4189 MUTATE(query->limitCount, query->limitCount, Node *);
4190 query->rtable = range_table_mutator(query->rtable,
4191 mutator, context, flags);
4196 * range_table_mutator is just the part of query_tree_mutator that processes
4197 * a query's rangetable. This is split out since it can be useful on
4201 range_table_mutator(List *rtable,
4202 Node *(*mutator) (),
4211 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
4212 RangeTblEntry *newrte;
4214 FLATCOPY(newrte, rte, RangeTblEntry);
4215 switch (rte->rtekind)
4219 /* we don't bother to copy eref, aliases, etc; OK? */
4222 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
4224 CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
4225 MUTATE(newrte->subquery, newrte->subquery, Query *);
4229 /* else, copy RT subqueries as-is */
4230 newrte->subquery = copyObject(rte->subquery);
4234 if (!(flags & QTW_IGNORE_JOINALIASES))
4235 MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
4238 /* else, copy join aliases as-is */
4239 newrte->joinaliasvars = copyObject(rte->joinaliasvars);
4243 MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
4246 MUTATE(newrte->values_lists, rte->values_lists, List *);
4249 newrt = lappend(newrt, newrte);
4255 * query_or_expression_tree_walker --- hybrid form
4257 * This routine will invoke query_tree_walker if called on a Query node,
4258 * else will invoke the walker directly. This is a useful way of starting
4259 * the recursion when the walker's normal change of state is not appropriate
4260 * for the outermost Query node.
4263 query_or_expression_tree_walker(Node *node,
4268 if (node && IsA(node, Query))
4269 return query_tree_walker((Query *) node,
4274 return walker(node, context);
4278 * query_or_expression_tree_mutator --- hybrid form
4280 * This routine will invoke query_tree_mutator if called on a Query node,
4281 * else will invoke the mutator directly. This is a useful way of starting
4282 * the recursion when the mutator's normal change of state is not appropriate
4283 * for the outermost Query node.
4286 query_or_expression_tree_mutator(Node *node,
4287 Node *(*mutator) (),
4291 if (node && IsA(node, Query))
4292 return (Node *) query_tree_mutator((Query *) node,
4297 return mutator(node, context);