1 /*-------------------------------------------------------------------------
4 * routines to manipulate qualification clauses
6 * Portions Copyright (c) 1996-2008, 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.254 2008/01/11 18:39:40 tgl Exp $
14 * AUTHOR DATE MAJOR EVENT
15 * Andrew Yu Nov 3, 1994 clause.c and clauses.c combined
17 *-------------------------------------------------------------------------
22 #include "access/heapam.h"
23 #include "catalog/pg_aggregate.h"
24 #include "catalog/pg_language.h"
25 #include "catalog/pg_operator.h"
26 #include "catalog/pg_proc.h"
27 #include "catalog/pg_type.h"
28 #include "executor/executor.h"
29 #include "executor/functions.h"
30 #include "miscadmin.h"
31 #include "nodes/makefuncs.h"
32 #include "optimizer/clauses.h"
33 #include "optimizer/cost.h"
34 #include "optimizer/planmain.h"
35 #include "optimizer/planner.h"
36 #include "optimizer/var.h"
37 #include "parser/analyze.h"
38 #include "parser/parse_clause.h"
39 #include "parser/parse_coerce.h"
40 #include "parser/parse_expr.h"
41 #include "tcop/tcopprot.h"
42 #include "utils/acl.h"
43 #include "utils/builtins.h"
44 #include "utils/datum.h"
45 #include "utils/lsyscache.h"
46 #include "utils/memutils.h"
47 #include "utils/syscache.h"
48 #include "utils/typcache.h"
53 ParamListInfo boundParams;
57 } eval_const_expressions_context;
64 } substitute_actual_parameters_context;
66 static bool contain_agg_clause_walker(Node *node, void *context);
67 static bool count_agg_clauses_walker(Node *node, AggClauseCounts *counts);
68 static bool expression_returns_set_walker(Node *node, void *context);
69 static bool expression_returns_set_rows_walker(Node *node, double *count);
70 static bool contain_subplans_walker(Node *node, void *context);
71 static bool contain_mutable_functions_walker(Node *node, void *context);
72 static bool contain_volatile_functions_walker(Node *node, void *context);
73 static bool contain_nonstrict_functions_walker(Node *node, void *context);
74 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
75 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
76 static bool set_coercionform_dontcare_walker(Node *node, void *context);
77 static Node *eval_const_expressions_mutator(Node *node,
78 eval_const_expressions_context *context);
79 static List *simplify_or_arguments(List *args,
80 eval_const_expressions_context *context,
81 bool *haveNull, bool *forceTrue);
82 static List *simplify_and_arguments(List *args,
83 eval_const_expressions_context *context,
84 bool *haveNull, bool *forceFalse);
85 static Expr *simplify_boolean_equality(List *args);
86 static Expr *simplify_function(Oid funcid,
87 Oid result_type, int32 result_typmod, List *args,
89 eval_const_expressions_context *context);
90 static Expr *evaluate_function(Oid funcid,
91 Oid result_type, int32 result_typmod, List *args,
93 eval_const_expressions_context *context);
94 static Expr *inline_function(Oid funcid, Oid result_type, List *args,
96 eval_const_expressions_context *context);
97 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
99 static Node *substitute_actual_parameters_mutator(Node *node,
100 substitute_actual_parameters_context *context);
101 static void sql_inline_error_callback(void *arg);
102 static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod);
105 /*****************************************************************************
106 * OPERATOR clause functions
107 *****************************************************************************/
111 * Creates an operator clause given its operator info, left operand,
112 * and right operand (pass NULL to create single-operand clause).
115 make_opclause(Oid opno, Oid opresulttype, bool opretset,
116 Expr *leftop, Expr *rightop)
118 OpExpr *expr = makeNode(OpExpr);
121 expr->opfuncid = InvalidOid;
122 expr->opresulttype = opresulttype;
123 expr->opretset = opretset;
125 expr->args = list_make2(leftop, rightop);
127 expr->args = list_make1(leftop);
128 return (Expr *) expr;
134 * Returns the left operand of a clause of the form (op expr expr)
138 get_leftop(Expr *clause)
140 OpExpr *expr = (OpExpr *) clause;
142 if (expr->args != NIL)
143 return linitial(expr->args);
151 * Returns the right operand in a clause of the form (op expr expr).
152 * NB: result will be NULL if applied to a unary op clause.
155 get_rightop(Expr *clause)
157 OpExpr *expr = (OpExpr *) clause;
159 if (list_length(expr->args) >= 2)
160 return lsecond(expr->args);
165 /*****************************************************************************
166 * NOT clause functions
167 *****************************************************************************/
172 * Returns t iff this is a 'not' clause: (NOT expr).
175 not_clause(Node *clause)
177 return (clause != NULL &&
178 IsA(clause, BoolExpr) &&
179 ((BoolExpr *) clause)->boolop == NOT_EXPR);
185 * Create a 'not' clause given the expression to be negated.
188 make_notclause(Expr *notclause)
190 BoolExpr *expr = makeNode(BoolExpr);
192 expr->boolop = NOT_EXPR;
193 expr->args = list_make1(notclause);
194 return (Expr *) expr;
200 * Retrieve the clause within a 'not' clause
203 get_notclausearg(Expr *notclause)
205 return linitial(((BoolExpr *) notclause)->args);
208 /*****************************************************************************
209 * OR clause functions
210 *****************************************************************************/
215 * Returns t iff the clause is an 'or' clause: (OR { expr }).
218 or_clause(Node *clause)
220 return (clause != NULL &&
221 IsA(clause, BoolExpr) &&
222 ((BoolExpr *) clause)->boolop == OR_EXPR);
228 * Creates an 'or' clause given a list of its subclauses.
231 make_orclause(List *orclauses)
233 BoolExpr *expr = makeNode(BoolExpr);
235 expr->boolop = OR_EXPR;
236 expr->args = orclauses;
237 return (Expr *) expr;
240 /*****************************************************************************
241 * AND clause functions
242 *****************************************************************************/
248 * Returns t iff its argument is an 'and' clause: (AND { expr }).
251 and_clause(Node *clause)
253 return (clause != NULL &&
254 IsA(clause, BoolExpr) &&
255 ((BoolExpr *) clause)->boolop == AND_EXPR);
261 * Creates an 'and' clause given a list of its subclauses.
264 make_andclause(List *andclauses)
266 BoolExpr *expr = makeNode(BoolExpr);
268 expr->boolop = AND_EXPR;
269 expr->args = andclauses;
270 return (Expr *) expr;
276 * Variant of make_andclause for ANDing two qual conditions together.
277 * Qual conditions have the property that a NULL nodetree is interpreted
280 * NB: this makes no attempt to preserve AND/OR flatness; so it should not
281 * be used on a qual that has already been run through prepqual.c.
284 make_and_qual(Node *qual1, Node *qual2)
290 return (Node *) make_andclause(list_make2(qual1, qual2));
294 * Sometimes (such as in the input of ExecQual), we use lists of expression
295 * nodes with implicit AND semantics.
297 * These functions convert between an AND-semantics expression list and the
298 * ordinary representation of a boolean expression.
300 * Note that an empty list is considered equivalent to TRUE.
303 make_ands_explicit(List *andclauses)
305 if (andclauses == NIL)
306 return (Expr *) makeBoolConst(true, false);
307 else if (list_length(andclauses) == 1)
308 return (Expr *) linitial(andclauses);
310 return make_andclause(andclauses);
314 make_ands_implicit(Expr *clause)
317 * NB: because the parser sets the qual field to NULL in a query that has
318 * no WHERE clause, we must consider a NULL input clause as TRUE, even
319 * though one might more reasonably think it FALSE. Grumble. If this
320 * causes trouble, consider changing the parser's behavior.
323 return NIL; /* NULL -> NIL list == TRUE */
324 else if (and_clause((Node *) clause))
325 return ((BoolExpr *) clause)->args;
326 else if (IsA(clause, Const) &&
327 !((Const *) clause)->constisnull &&
328 DatumGetBool(((Const *) clause)->constvalue))
329 return NIL; /* constant TRUE input -> NIL list */
331 return list_make1(clause);
335 /*****************************************************************************
336 * Aggregate-function clause manipulation
337 *****************************************************************************/
341 * Recursively search for Aggref nodes within a clause.
343 * Returns true if any aggregate found.
345 * This does not descend into subqueries, and so should be used only after
346 * reduction of sublinks to subplans, or in contexts where it's known there
347 * are no subqueries. There mustn't be outer-aggregate references either.
349 * (If you want something like this but able to deal with subqueries,
350 * see rewriteManip.c's checkExprHasAggs().)
353 contain_agg_clause(Node *clause)
355 return contain_agg_clause_walker(clause, NULL);
359 contain_agg_clause_walker(Node *node, void *context)
363 if (IsA(node, Aggref))
365 Assert(((Aggref *) node)->agglevelsup == 0);
366 return true; /* abort the tree traversal and return true */
368 Assert(!IsA(node, SubLink));
369 return expression_tree_walker(node, contain_agg_clause_walker, context);
374 * Recursively count the Aggref nodes in an expression tree.
376 * Note: this also checks for nested aggregates, which are an error.
378 * We not only count the nodes, but attempt to estimate the total space
379 * needed for their transition state values if all are evaluated in parallel
380 * (as would be done in a HashAgg plan). See AggClauseCounts for the exact
381 * set of statistics returned.
383 * NOTE that the counts are ADDED to those already in *counts ... so the
384 * caller is responsible for zeroing the struct initially.
386 * This does not descend into subqueries, and so should be used only after
387 * reduction of sublinks to subplans, or in contexts where it's known there
388 * are no subqueries. There mustn't be outer-aggregate references either.
391 count_agg_clauses(Node *clause, AggClauseCounts *counts)
393 /* no setup needed */
394 count_agg_clauses_walker(clause, counts);
398 count_agg_clauses_walker(Node *node, AggClauseCounts *counts)
402 if (IsA(node, Aggref))
404 Aggref *aggref = (Aggref *) node;
408 Form_pg_aggregate aggform;
413 Assert(aggref->agglevelsup == 0);
415 if (aggref->aggdistinct)
416 counts->numDistinctAggs++;
418 /* extract argument types */
419 numArguments = list_length(aggref->args);
420 inputTypes = (Oid *) palloc(sizeof(Oid) * numArguments);
422 foreach(l, aggref->args)
424 inputTypes[i++] = exprType((Node *) lfirst(l));
427 /* fetch aggregate transition datatype from pg_aggregate */
428 aggTuple = SearchSysCache(AGGFNOID,
429 ObjectIdGetDatum(aggref->aggfnoid),
431 if (!HeapTupleIsValid(aggTuple))
432 elog(ERROR, "cache lookup failed for aggregate %u",
434 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
435 aggtranstype = aggform->aggtranstype;
436 ReleaseSysCache(aggTuple);
438 /* resolve actual type of transition state, if polymorphic */
439 if (IsPolymorphicType(aggtranstype))
441 /* have to fetch the agg's declared input types... */
442 Oid *declaredArgTypes;
445 (void) get_func_signature(aggref->aggfnoid,
446 &declaredArgTypes, &agg_nargs);
447 Assert(agg_nargs == numArguments);
448 aggtranstype = enforce_generic_type_consistency(inputTypes,
453 pfree(declaredArgTypes);
457 * If the transition type is pass-by-value then it doesn't add
458 * anything to the required size of the hashtable. If it is
459 * pass-by-reference then we have to add the estimated size of the
460 * value itself, plus palloc overhead.
462 if (!get_typbyval(aggtranstype))
464 int32 aggtranstypmod;
468 * If transition state is of same type as first input, assume it's
469 * the same typmod (same width) as well. This works for cases
470 * like MAX/MIN and is probably somewhat reasonable otherwise.
472 if (numArguments > 0 && aggtranstype == inputTypes[0])
473 aggtranstypmod = exprTypmod((Node *) linitial(aggref->args));
477 avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
478 avgwidth = MAXALIGN(avgwidth);
480 counts->transitionSpace += avgwidth + 2 * sizeof(void *);
484 * Complain if the aggregate's arguments contain any aggregates;
485 * nested agg functions are semantically nonsensical.
487 if (contain_agg_clause((Node *) aggref->args))
489 (errcode(ERRCODE_GROUPING_ERROR),
490 errmsg("aggregate function calls cannot be nested")));
493 * Having checked that, we need not recurse into the argument.
497 Assert(!IsA(node, SubLink));
498 return expression_tree_walker(node, count_agg_clauses_walker,
503 /*****************************************************************************
504 * Support for expressions returning sets
505 *****************************************************************************/
508 * expression_returns_set
509 * Test whether an expression returns a set result.
511 * Because we use expression_tree_walker(), this can also be applied to
512 * whole targetlists; it'll produce TRUE if any one of the tlist items
516 expression_returns_set(Node *clause)
518 return expression_returns_set_walker(clause, NULL);
522 expression_returns_set_walker(Node *node, void *context)
526 if (IsA(node, FuncExpr))
528 FuncExpr *expr = (FuncExpr *) node;
530 if (expr->funcretset)
532 /* else fall through to check args */
534 if (IsA(node, OpExpr))
536 OpExpr *expr = (OpExpr *) node;
540 /* else fall through to check args */
543 /* Avoid recursion for some cases that can't return a set */
544 if (IsA(node, Aggref))
546 if (IsA(node, DistinctExpr))
548 if (IsA(node, ScalarArrayOpExpr))
550 if (IsA(node, BoolExpr))
552 if (IsA(node, SubLink))
554 if (IsA(node, SubPlan))
556 if (IsA(node, ArrayExpr))
558 if (IsA(node, RowExpr))
560 if (IsA(node, RowCompareExpr))
562 if (IsA(node, CoalesceExpr))
564 if (IsA(node, MinMaxExpr))
566 if (IsA(node, XmlExpr))
568 if (IsA(node, NullIfExpr))
571 return expression_tree_walker(node, expression_returns_set_walker,
576 * expression_returns_set_rows
577 * Estimate the number of rows in a set result.
579 * We use the product of the rowcount estimates of all the functions in
580 * the given tree. The result is 1 if there are no set-returning functions.
583 expression_returns_set_rows(Node *clause)
587 (void) expression_returns_set_rows_walker(clause, &result);
592 expression_returns_set_rows_walker(Node *node, double *count)
596 if (IsA(node, FuncExpr))
598 FuncExpr *expr = (FuncExpr *) node;
600 if (expr->funcretset)
601 *count *= get_func_rows(expr->funcid);
603 if (IsA(node, OpExpr))
605 OpExpr *expr = (OpExpr *) node;
610 *count *= get_func_rows(expr->opfuncid);
614 /* Avoid recursion for some cases that can't return a set */
615 if (IsA(node, Aggref))
617 if (IsA(node, DistinctExpr))
619 if (IsA(node, ScalarArrayOpExpr))
621 if (IsA(node, BoolExpr))
623 if (IsA(node, SubLink))
625 if (IsA(node, SubPlan))
627 if (IsA(node, ArrayExpr))
629 if (IsA(node, RowExpr))
631 if (IsA(node, RowCompareExpr))
633 if (IsA(node, CoalesceExpr))
635 if (IsA(node, MinMaxExpr))
637 if (IsA(node, XmlExpr))
639 if (IsA(node, NullIfExpr))
642 return expression_tree_walker(node, expression_returns_set_rows_walker,
647 /*****************************************************************************
648 * Subplan clause manipulation
649 *****************************************************************************/
653 * Recursively search for subplan nodes within a clause.
655 * If we see a SubLink node, we will return TRUE. This is only possible if
656 * the expression tree hasn't yet been transformed by subselect.c. We do not
657 * know whether the node will produce a true subplan or just an initplan,
658 * but we make the conservative assumption that it will be a subplan.
660 * Returns true if any subplan found.
663 contain_subplans(Node *clause)
665 return contain_subplans_walker(clause, NULL);
669 contain_subplans_walker(Node *node, void *context)
673 if (IsA(node, SubPlan) ||
675 return true; /* abort the tree traversal and return true */
676 return expression_tree_walker(node, contain_subplans_walker, context);
680 /*****************************************************************************
681 * Check clauses for mutable functions
682 *****************************************************************************/
685 * contain_mutable_functions
686 * Recursively search for mutable functions within a clause.
688 * Returns true if any mutable function (or operator implemented by a
689 * mutable function) is found. This test is needed so that we don't
690 * mistakenly think that something like "WHERE random() < 0.5" can be treated
691 * as a constant qualification.
693 * XXX we do not examine sub-selects to see if they contain uses of
694 * mutable functions. It's not real clear if that is correct or not...
697 contain_mutable_functions(Node *clause)
699 return contain_mutable_functions_walker(clause, NULL);
703 contain_mutable_functions_walker(Node *node, void *context)
707 if (IsA(node, FuncExpr))
709 FuncExpr *expr = (FuncExpr *) node;
711 if (func_volatile(expr->funcid) != PROVOLATILE_IMMUTABLE)
713 /* else fall through to check args */
715 else if (IsA(node, OpExpr))
717 OpExpr *expr = (OpExpr *) node;
720 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
722 /* else fall through to check args */
724 else if (IsA(node, DistinctExpr))
726 DistinctExpr *expr = (DistinctExpr *) node;
728 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
729 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
731 /* else fall through to check args */
733 else if (IsA(node, ScalarArrayOpExpr))
735 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
737 set_sa_opfuncid(expr);
738 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
740 /* else fall through to check args */
742 else if (IsA(node, CoerceViaIO))
744 CoerceViaIO *expr = (CoerceViaIO *) node;
749 /* check the result type's input function */
750 getTypeInputInfo(expr->resulttype,
751 &iofunc, &typioparam);
752 if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
754 /* check the input type's output function */
755 getTypeOutputInfo(exprType((Node *) expr->arg),
756 &iofunc, &typisvarlena);
757 if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
759 /* else fall through to check args */
761 else if (IsA(node, ArrayCoerceExpr))
763 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
765 if (OidIsValid(expr->elemfuncid) &&
766 func_volatile(expr->elemfuncid) != PROVOLATILE_IMMUTABLE)
768 /* else fall through to check args */
770 else if (IsA(node, NullIfExpr))
772 NullIfExpr *expr = (NullIfExpr *) node;
774 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
775 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
777 /* else fall through to check args */
779 else if (IsA(node, RowCompareExpr))
781 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
784 foreach(opid, rcexpr->opnos)
786 if (op_volatile(lfirst_oid(opid)) != PROVOLATILE_IMMUTABLE)
789 /* else fall through to check args */
791 return expression_tree_walker(node, contain_mutable_functions_walker,
796 /*****************************************************************************
797 * Check clauses for volatile functions
798 *****************************************************************************/
801 * contain_volatile_functions
802 * Recursively search for volatile functions within a clause.
804 * Returns true if any volatile function (or operator implemented by a
805 * volatile function) is found. This test prevents invalid conversions
806 * of volatile expressions into indexscan quals.
808 * XXX we do not examine sub-selects to see if they contain uses of
809 * volatile functions. It's not real clear if that is correct or not...
812 contain_volatile_functions(Node *clause)
814 return contain_volatile_functions_walker(clause, NULL);
818 contain_volatile_functions_walker(Node *node, void *context)
822 if (IsA(node, FuncExpr))
824 FuncExpr *expr = (FuncExpr *) node;
826 if (func_volatile(expr->funcid) == PROVOLATILE_VOLATILE)
828 /* else fall through to check args */
830 else if (IsA(node, OpExpr))
832 OpExpr *expr = (OpExpr *) node;
835 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
837 /* else fall through to check args */
839 else if (IsA(node, DistinctExpr))
841 DistinctExpr *expr = (DistinctExpr *) node;
843 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
844 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
846 /* else fall through to check args */
848 else if (IsA(node, ScalarArrayOpExpr))
850 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
852 set_sa_opfuncid(expr);
853 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
855 /* else fall through to check args */
857 else if (IsA(node, CoerceViaIO))
859 CoerceViaIO *expr = (CoerceViaIO *) node;
864 /* check the result type's input function */
865 getTypeInputInfo(expr->resulttype,
866 &iofunc, &typioparam);
867 if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
869 /* check the input type's output function */
870 getTypeOutputInfo(exprType((Node *) expr->arg),
871 &iofunc, &typisvarlena);
872 if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
874 /* else fall through to check args */
876 else if (IsA(node, ArrayCoerceExpr))
878 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
880 if (OidIsValid(expr->elemfuncid) &&
881 func_volatile(expr->elemfuncid) == PROVOLATILE_VOLATILE)
883 /* else fall through to check args */
885 else if (IsA(node, NullIfExpr))
887 NullIfExpr *expr = (NullIfExpr *) node;
889 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
890 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
892 /* else fall through to check args */
894 else if (IsA(node, RowCompareExpr))
896 /* RowCompare probably can't have volatile ops, but check anyway */
897 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
900 foreach(opid, rcexpr->opnos)
902 if (op_volatile(lfirst_oid(opid)) == PROVOLATILE_VOLATILE)
905 /* else fall through to check args */
907 return expression_tree_walker(node, contain_volatile_functions_walker,
912 /*****************************************************************************
913 * Check clauses for nonstrict functions
914 *****************************************************************************/
917 * contain_nonstrict_functions
918 * Recursively search for nonstrict functions within a clause.
920 * Returns true if any nonstrict construct is found --- ie, anything that
921 * could produce non-NULL output with a NULL input.
923 * The idea here is that the caller has verified that the expression contains
924 * one or more Var or Param nodes (as appropriate for the caller's need), and
925 * now wishes to prove that the expression result will be NULL if any of these
926 * inputs is NULL. If we return false, then the proof succeeded.
929 contain_nonstrict_functions(Node *clause)
931 return contain_nonstrict_functions_walker(clause, NULL);
935 contain_nonstrict_functions_walker(Node *node, void *context)
939 if (IsA(node, Aggref))
941 /* an aggregate could return non-null with null input */
944 if (IsA(node, ArrayRef))
946 /* array assignment is nonstrict, but subscripting is strict */
947 if (((ArrayRef *) node)->refassgnexpr != NULL)
949 /* else fall through to check args */
951 if (IsA(node, FuncExpr))
953 FuncExpr *expr = (FuncExpr *) node;
955 if (!func_strict(expr->funcid))
957 /* else fall through to check args */
959 if (IsA(node, OpExpr))
961 OpExpr *expr = (OpExpr *) node;
964 if (!func_strict(expr->opfuncid))
966 /* else fall through to check args */
968 if (IsA(node, DistinctExpr))
970 /* IS DISTINCT FROM is inherently non-strict */
973 if (IsA(node, ScalarArrayOpExpr))
975 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
977 if (!is_strict_saop(expr, false))
979 /* else fall through to check args */
981 if (IsA(node, BoolExpr))
983 BoolExpr *expr = (BoolExpr *) node;
985 switch (expr->boolop)
989 /* AND, OR are inherently non-strict */
995 if (IsA(node, SubLink))
997 /* In some cases a sublink might be strict, but in general not */
1000 if (IsA(node, SubPlan))
1002 /* ArrayCoerceExpr is strict at the array level, regardless of elemfunc */
1003 if (IsA(node, FieldStore))
1005 if (IsA(node, CaseExpr))
1007 if (IsA(node, ArrayExpr))
1009 if (IsA(node, RowExpr))
1011 if (IsA(node, RowCompareExpr))
1013 if (IsA(node, CoalesceExpr))
1015 if (IsA(node, MinMaxExpr))
1017 if (IsA(node, XmlExpr))
1019 if (IsA(node, NullIfExpr))
1021 if (IsA(node, NullTest))
1023 if (IsA(node, BooleanTest))
1025 return expression_tree_walker(node, contain_nonstrict_functions_walker,
1031 * find_nonnullable_rels
1032 * Determine which base rels are forced nonnullable by given clause.
1034 * Returns the set of all Relids that are referenced in the clause in such
1035 * a way that the clause cannot possibly return TRUE if any of these Relids
1036 * is an all-NULL row. (It is OK to err on the side of conservatism; hence
1037 * the analysis here is simplistic.)
1039 * The semantics here are subtly different from contain_nonstrict_functions:
1040 * that function is concerned with NULL results from arbitrary expressions,
1041 * but here we assume that the input is a Boolean expression, and wish to
1042 * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1043 * the expression to have been AND/OR flattened and converted to implicit-AND
1046 * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1047 * the result is either FALSE or NULL is good enough. top_level is FALSE when
1048 * we have descended below a NOT or a strict function: now we must be able to
1049 * prove that the subexpression goes to NULL.
1051 * We don't use expression_tree_walker here because we don't want to descend
1052 * through very many kinds of nodes; only the ones we can be sure are strict.
1055 find_nonnullable_rels(Node *clause)
1057 return find_nonnullable_rels_walker(clause, true);
1061 find_nonnullable_rels_walker(Node *node, bool top_level)
1063 Relids result = NULL;
1070 Var *var = (Var *) node;
1072 if (var->varlevelsup == 0)
1073 result = bms_make_singleton(var->varno);
1075 else if (IsA(node, List))
1078 * At top level, we are examining an implicit-AND list: if any of the
1079 * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1080 * not at top level, we are examining the arguments of a strict
1081 * function: if any of them produce NULL then the result of the
1082 * function must be NULL. So in both cases, the set of nonnullable
1083 * rels is the union of those found in the arms, and we pass down the
1084 * top_level flag unmodified.
1086 foreach(l, (List *) node)
1088 result = bms_join(result,
1089 find_nonnullable_rels_walker(lfirst(l),
1093 else if (IsA(node, FuncExpr))
1095 FuncExpr *expr = (FuncExpr *) node;
1097 if (func_strict(expr->funcid))
1098 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1100 else if (IsA(node, OpExpr))
1102 OpExpr *expr = (OpExpr *) node;
1105 if (func_strict(expr->opfuncid))
1106 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1108 else if (IsA(node, ScalarArrayOpExpr))
1110 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1112 if (is_strict_saop(expr, true))
1113 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1115 else if (IsA(node, BoolExpr))
1117 BoolExpr *expr = (BoolExpr *) node;
1119 switch (expr->boolop)
1122 /* At top level we can just recurse (to the List case) */
1125 result = find_nonnullable_rels_walker((Node *) expr->args,
1131 * Below top level, even if one arm produces NULL, the result
1132 * could be FALSE (hence not NULL). However, if *all* the
1133 * arms produce NULL then the result is NULL, so we can take
1134 * the intersection of the sets of nonnullable rels, just as
1135 * for OR. Fall through to share code.
1141 * OR is strict if all of its arms are, so we can take the
1142 * intersection of the sets of nonnullable rels for each arm.
1143 * This works for both values of top_level.
1145 foreach(l, expr->args)
1149 subresult = find_nonnullable_rels_walker(lfirst(l),
1151 if (result == NULL) /* first subresult? */
1154 result = bms_int_members(result, subresult);
1157 * If the intersection is empty, we can stop looking. This
1158 * also justifies the test for first-subresult above.
1160 if (bms_is_empty(result))
1165 /* NOT will return null if its arg is null */
1166 result = find_nonnullable_rels_walker((Node *) expr->args,
1170 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1174 else if (IsA(node, RelabelType))
1176 RelabelType *expr = (RelabelType *) node;
1178 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1180 else if (IsA(node, CoerceViaIO))
1182 /* not clear this is useful, but it can't hurt */
1183 CoerceViaIO *expr = (CoerceViaIO *) node;
1185 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1187 else if (IsA(node, ArrayCoerceExpr))
1189 /* ArrayCoerceExpr is strict at the array level */
1190 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1192 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1194 else if (IsA(node, ConvertRowtypeExpr))
1196 /* not clear this is useful, but it can't hurt */
1197 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1199 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1201 else if (IsA(node, NullTest))
1203 /* IS NOT NULL can be considered strict, but only at top level */
1204 NullTest *expr = (NullTest *) node;
1206 if (top_level && expr->nulltesttype == IS_NOT_NULL)
1207 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1209 else if (IsA(node, BooleanTest))
1211 /* Boolean tests that reject NULL are strict at top level */
1212 BooleanTest *expr = (BooleanTest *) node;
1215 (expr->booltesttype == IS_TRUE ||
1216 expr->booltesttype == IS_FALSE ||
1217 expr->booltesttype == IS_NOT_UNKNOWN))
1218 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1224 * Can we treat a ScalarArrayOpExpr as strict?
1226 * If "falseOK" is true, then a "false" result can be considered strict,
1227 * else we need to guarantee an actual NULL result for NULL input.
1229 * "foo op ALL array" is strict if the op is strict *and* we can prove
1230 * that the array input isn't an empty array. We can check that
1231 * for the cases of an array constant and an ARRAY[] construct.
1233 * "foo op ANY array" is strict in the falseOK sense if the op is strict.
1234 * If not falseOK, the test is the same as for "foo op ALL array".
1237 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
1241 /* The contained operator must be strict. */
1242 set_sa_opfuncid(expr);
1243 if (!func_strict(expr->opfuncid))
1245 /* If ANY and falseOK, that's all we need to check. */
1246 if (expr->useOr && falseOK)
1248 /* Else, we have to see if the array is provably non-empty. */
1249 Assert(list_length(expr->args) == 2);
1250 rightop = (Node *) lsecond(expr->args);
1251 if (rightop && IsA(rightop, Const))
1253 Datum arraydatum = ((Const *) rightop)->constvalue;
1254 bool arrayisnull = ((Const *) rightop)->constisnull;
1255 ArrayType *arrayval;
1260 arrayval = DatumGetArrayTypeP(arraydatum);
1261 nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1265 else if (rightop && IsA(rightop, ArrayExpr))
1267 ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1269 if (arrayexpr->elements != NIL && !arrayexpr->multidims)
1276 /*****************************************************************************
1277 * Check for "pseudo-constant" clauses
1278 *****************************************************************************/
1281 * is_pseudo_constant_clause
1282 * Detect whether an expression is "pseudo constant", ie, it contains no
1283 * variables of the current query level and no uses of volatile functions.
1284 * Such an expr is not necessarily a true constant: it can still contain
1285 * Params and outer-level Vars, not to mention functions whose results
1286 * may vary from one statement to the next. However, the expr's value
1287 * will be constant over any one scan of the current query, so it can be
1288 * used as, eg, an indexscan key.
1290 * CAUTION: this function omits to test for one very important class of
1291 * not-constant expressions, namely aggregates (Aggrefs). In current usage
1292 * this is only applied to WHERE clauses and so a check for Aggrefs would be
1293 * a waste of cycles; but be sure to also check contain_agg_clause() if you
1294 * want to know about pseudo-constness in other contexts.
1297 is_pseudo_constant_clause(Node *clause)
1300 * We could implement this check in one recursive scan. But since the
1301 * check for volatile functions is both moderately expensive and unlikely
1302 * to fail, it seems better to look for Vars first and only check for
1303 * volatile functions if we find no Vars.
1305 if (!contain_var_clause(clause) &&
1306 !contain_volatile_functions(clause))
1312 * is_pseudo_constant_clause_relids
1313 * Same as above, except caller already has available the var membership
1314 * of the expression; this lets us avoid the contain_var_clause() scan.
1317 is_pseudo_constant_clause_relids(Node *clause, Relids relids)
1319 if (bms_is_empty(relids) &&
1320 !contain_volatile_functions(clause))
1326 /*****************************************************************************
1327 * Tests on clauses of queries
1329 * Possibly this code should go someplace else, since this isn't quite the
1330 * same meaning of "clause" as is used elsewhere in this module. But I can't
1331 * think of a better place for it...
1332 *****************************************************************************/
1335 * Test whether a query uses DISTINCT ON, ie, has a distinct-list that is
1336 * not the same as the set of output columns.
1339 has_distinct_on_clause(Query *query)
1343 /* Is there a DISTINCT clause at all? */
1344 if (query->distinctClause == NIL)
1348 * If the DISTINCT list contains all the nonjunk targetlist items, and
1349 * nothing else (ie, no junk tlist items), then it's a simple DISTINCT,
1350 * else it's DISTINCT ON. We do not require the lists to be in the same
1351 * order (since the parser may have adjusted the DISTINCT clause ordering
1352 * to agree with ORDER BY). Furthermore, a non-DISTINCT junk tlist item
1353 * that is in the sortClause is also evidence of DISTINCT ON, since we
1354 * don't allow ORDER BY on junk tlist items when plain DISTINCT is used.
1356 * This code assumes that the DISTINCT list is valid, ie, all its entries
1357 * match some entry of the tlist.
1359 foreach(l, query->targetList)
1361 TargetEntry *tle = (TargetEntry *) lfirst(l);
1363 if (tle->ressortgroupref == 0)
1366 continue; /* we can ignore unsorted junk cols */
1367 return true; /* definitely not in DISTINCT list */
1369 if (targetIsInSortList(tle, InvalidOid, query->distinctClause))
1372 return true; /* junk TLE in DISTINCT means DISTINCT ON */
1373 /* else this TLE is okay, keep looking */
1377 /* This TLE is not in DISTINCT list */
1379 return true; /* non-junk, non-DISTINCT, so DISTINCT ON */
1380 if (targetIsInSortList(tle, InvalidOid, query->sortClause))
1381 return true; /* sorted, non-distinct junk */
1382 /* unsorted junk is okay, keep looking */
1385 /* It's a simple DISTINCT */
1390 * Test whether a query uses simple DISTINCT, ie, has a distinct-list that
1391 * is the same as the set of output columns.
1394 has_distinct_clause(Query *query)
1396 /* Is there a DISTINCT clause at all? */
1397 if (query->distinctClause == NIL)
1400 /* It's DISTINCT if it's not DISTINCT ON */
1401 return !has_distinct_on_clause(query);
1405 /*****************************************************************************
1407 * General clause-manipulating routines *
1409 *****************************************************************************/
1413 * (formerly clause_relids)
1415 * Returns the number of different relations referenced in 'clause'.
1418 NumRelids(Node *clause)
1420 Relids varnos = pull_varnos(clause);
1421 int result = bms_num_members(varnos);
1428 * CommuteOpExpr: commute a binary operator clause
1430 * XXX the clause is destructively modified!
1433 CommuteOpExpr(OpExpr *clause)
1438 /* Sanity checks: caller is at fault if these fail */
1439 if (!is_opclause(clause) ||
1440 list_length(clause->args) != 2)
1441 elog(ERROR, "cannot commute non-binary-operator clause");
1443 opoid = get_commutator(clause->opno);
1445 if (!OidIsValid(opoid))
1446 elog(ERROR, "could not find commutator for operator %u",
1450 * modify the clause in-place!
1452 clause->opno = opoid;
1453 clause->opfuncid = InvalidOid;
1454 /* opresulttype and opretset are assumed not to change */
1456 temp = linitial(clause->args);
1457 linitial(clause->args) = lsecond(clause->args);
1458 lsecond(clause->args) = temp;
1462 * CommuteRowCompareExpr: commute a RowCompareExpr clause
1464 * XXX the clause is destructively modified!
1467 CommuteRowCompareExpr(RowCompareExpr *clause)
1473 /* Sanity checks: caller is at fault if these fail */
1474 if (!IsA(clause, RowCompareExpr))
1475 elog(ERROR, "expected a RowCompareExpr");
1477 /* Build list of commuted operators */
1479 foreach(l, clause->opnos)
1481 Oid opoid = lfirst_oid(l);
1483 opoid = get_commutator(opoid);
1484 if (!OidIsValid(opoid))
1485 elog(ERROR, "could not find commutator for operator %u",
1487 newops = lappend_oid(newops, opoid);
1491 * modify the clause in-place!
1493 switch (clause->rctype)
1496 clause->rctype = ROWCOMPARE_GT;
1499 clause->rctype = ROWCOMPARE_GE;
1502 clause->rctype = ROWCOMPARE_LE;
1505 clause->rctype = ROWCOMPARE_LT;
1508 elog(ERROR, "unexpected RowCompare type: %d",
1509 (int) clause->rctype);
1513 clause->opnos = newops;
1516 * Note: we need not change the opfamilies list; we assume any btree
1517 * opfamily containing an operator will also contain its commutator.
1520 temp = clause->largs;
1521 clause->largs = clause->rargs;
1522 clause->rargs = temp;
1526 * strip_implicit_coercions: remove implicit coercions at top level of tree
1528 * Note: there isn't any useful thing we can do with a RowExpr here, so
1529 * just return it unchanged, even if it's marked as an implicit coercion.
1532 strip_implicit_coercions(Node *node)
1536 if (IsA(node, FuncExpr))
1538 FuncExpr *f = (FuncExpr *) node;
1540 if (f->funcformat == COERCE_IMPLICIT_CAST)
1541 return strip_implicit_coercions(linitial(f->args));
1543 else if (IsA(node, RelabelType))
1545 RelabelType *r = (RelabelType *) node;
1547 if (r->relabelformat == COERCE_IMPLICIT_CAST)
1548 return strip_implicit_coercions((Node *) r->arg);
1550 else if (IsA(node, CoerceViaIO))
1552 CoerceViaIO *c = (CoerceViaIO *) node;
1554 if (c->coerceformat == COERCE_IMPLICIT_CAST)
1555 return strip_implicit_coercions((Node *) c->arg);
1557 else if (IsA(node, ArrayCoerceExpr))
1559 ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
1561 if (c->coerceformat == COERCE_IMPLICIT_CAST)
1562 return strip_implicit_coercions((Node *) c->arg);
1564 else if (IsA(node, ConvertRowtypeExpr))
1566 ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
1568 if (c->convertformat == COERCE_IMPLICIT_CAST)
1569 return strip_implicit_coercions((Node *) c->arg);
1571 else if (IsA(node, CoerceToDomain))
1573 CoerceToDomain *c = (CoerceToDomain *) node;
1575 if (c->coercionformat == COERCE_IMPLICIT_CAST)
1576 return strip_implicit_coercions((Node *) c->arg);
1582 * set_coercionform_dontcare: set all CoercionForm fields to COERCE_DONTCARE
1584 * This is used to make index expressions and index predicates more easily
1585 * comparable to clauses of queries. CoercionForm is not semantically
1586 * significant (for cases where it does matter, the significant info is
1587 * coded into the coercion function arguments) so we can ignore it during
1588 * comparisons. Thus, for example, an index on "foo::int4" can match an
1589 * implicit coercion to int4.
1591 * Caution: the passed expression tree is modified in-place.
1594 set_coercionform_dontcare(Node *node)
1596 (void) set_coercionform_dontcare_walker(node, NULL);
1600 set_coercionform_dontcare_walker(Node *node, void *context)
1604 if (IsA(node, FuncExpr))
1605 ((FuncExpr *) node)->funcformat = COERCE_DONTCARE;
1606 else if (IsA(node, RelabelType))
1607 ((RelabelType *) node)->relabelformat = COERCE_DONTCARE;
1608 else if (IsA(node, CoerceViaIO))
1609 ((CoerceViaIO *) node)->coerceformat = COERCE_DONTCARE;
1610 else if (IsA(node, ArrayCoerceExpr))
1611 ((ArrayCoerceExpr *) node)->coerceformat = COERCE_DONTCARE;
1612 else if (IsA(node, ConvertRowtypeExpr))
1613 ((ConvertRowtypeExpr *) node)->convertformat = COERCE_DONTCARE;
1614 else if (IsA(node, RowExpr))
1615 ((RowExpr *) node)->row_format = COERCE_DONTCARE;
1616 else if (IsA(node, CoerceToDomain))
1617 ((CoerceToDomain *) node)->coercionformat = COERCE_DONTCARE;
1618 return expression_tree_walker(node, set_coercionform_dontcare_walker,
1623 * Helper for eval_const_expressions: check that datatype of an attribute
1624 * is still what it was when the expression was parsed. This is needed to
1625 * guard against improper simplification after ALTER COLUMN TYPE. (XXX we
1626 * may well need to make similar checks elsewhere?)
1629 rowtype_field_matches(Oid rowtypeid, int fieldnum,
1630 Oid expectedtype, int32 expectedtypmod)
1633 Form_pg_attribute attr;
1635 /* No issue for RECORD, since there is no way to ALTER such a type */
1636 if (rowtypeid == RECORDOID)
1638 tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
1639 if (fieldnum <= 0 || fieldnum > tupdesc->natts)
1641 ReleaseTupleDesc(tupdesc);
1644 attr = tupdesc->attrs[fieldnum - 1];
1645 if (attr->attisdropped ||
1646 attr->atttypid != expectedtype ||
1647 attr->atttypmod != expectedtypmod)
1649 ReleaseTupleDesc(tupdesc);
1652 ReleaseTupleDesc(tupdesc);
1657 /*--------------------
1658 * eval_const_expressions
1660 * Reduce any recognizably constant subexpressions of the given
1661 * expression tree, for example "2 + 2" => "4". More interestingly,
1662 * we can reduce certain boolean expressions even when they contain
1663 * non-constant subexpressions: "x OR true" => "true" no matter what
1664 * the subexpression x is. (XXX We assume that no such subexpression
1665 * will have important side-effects, which is not necessarily a good
1666 * assumption in the presence of user-defined functions; do we need a
1667 * pg_proc flag that prevents discarding the execution of a function?)
1669 * We do understand that certain functions may deliver non-constant
1670 * results even with constant inputs, "nextval()" being the classic
1671 * example. Functions that are not marked "immutable" in pg_proc
1672 * will not be pre-evaluated here, although we will reduce their
1673 * arguments as far as possible.
1675 * We assume that the tree has already been type-checked and contains
1676 * only operators and functions that are reasonable to try to execute.
1678 * NOTE: the planner assumes that this will always flatten nested AND and
1679 * OR clauses into N-argument form. See comments in prepqual.c.
1680 *--------------------
1683 eval_const_expressions(Node *node)
1685 eval_const_expressions_context context;
1687 context.boundParams = NULL; /* don't use any bound params */
1688 context.active_fns = NIL; /* nothing being recursively simplified */
1689 context.case_val = NULL; /* no CASE being examined */
1690 context.estimate = false; /* safe transformations only */
1691 return eval_const_expressions_mutator(node, &context);
1694 /*--------------------
1695 * estimate_expression_value
1697 * This function attempts to estimate the value of an expression for
1698 * planning purposes. It is in essence a more aggressive version of
1699 * eval_const_expressions(): we will perform constant reductions that are
1700 * not necessarily 100% safe, but are reasonable for estimation purposes.
1702 * Currently the extra steps that are taken in this mode are:
1703 * 1. Substitute values for Params, where a bound Param value has been made
1704 * available by the caller of planner(), even if the Param isn't marked
1705 * constant. This effectively means that we plan using the first supplied
1706 * value of the Param.
1707 * 2. Fold stable, as well as immutable, functions to constants.
1708 *--------------------
1711 estimate_expression_value(PlannerInfo *root, Node *node)
1713 eval_const_expressions_context context;
1715 context.boundParams = root->glob->boundParams; /* bound Params */
1716 context.active_fns = NIL; /* nothing being recursively simplified */
1717 context.case_val = NULL; /* no CASE being examined */
1718 context.estimate = true; /* unsafe transformations OK */
1719 return eval_const_expressions_mutator(node, &context);
1723 eval_const_expressions_mutator(Node *node,
1724 eval_const_expressions_context *context)
1728 if (IsA(node, Param))
1730 Param *param = (Param *) node;
1732 /* Look to see if we've been given a value for this Param */
1733 if (param->paramkind == PARAM_EXTERN &&
1734 context->boundParams != NULL &&
1735 param->paramid > 0 &&
1736 param->paramid <= context->boundParams->numParams)
1738 ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
1740 if (OidIsValid(prm->ptype))
1742 /* OK to substitute parameter value? */
1743 if (context->estimate || (prm->pflags & PARAM_FLAG_CONST))
1746 * Return a Const representing the param value. Must copy
1747 * pass-by-ref datatypes, since the Param might be in a
1748 * memory context shorter-lived than our output plan
1755 Assert(prm->ptype == param->paramtype);
1756 get_typlenbyval(param->paramtype, &typLen, &typByVal);
1757 if (prm->isnull || typByVal)
1760 pval = datumCopy(prm->value, typByVal, typLen);
1761 return (Node *) makeConst(param->paramtype,
1770 /* Not replaceable, so just copy the Param (no need to recurse) */
1771 return (Node *) copyObject(param);
1773 if (IsA(node, FuncExpr))
1775 FuncExpr *expr = (FuncExpr *) node;
1781 * Reduce constants in the FuncExpr's arguments. We know args is
1782 * either NIL or a List node, so we can call expression_tree_mutator
1783 * directly rather than recursing to self.
1785 args = (List *) expression_tree_mutator((Node *) expr->args,
1786 eval_const_expressions_mutator,
1790 * Code for op/func reduction is pretty bulky, so split it out as a
1791 * separate function. Note: exprTypmod normally returns -1 for a
1792 * FuncExpr, but not when the node is recognizably a length coercion;
1793 * we want to preserve the typmod in the eventual Const if so.
1795 simple = simplify_function(expr->funcid,
1796 expr->funcresulttype, exprTypmod(node),
1799 if (simple) /* successfully simplified it */
1800 return (Node *) simple;
1803 * The expression cannot be simplified any further, so build and
1804 * return a replacement FuncExpr node using the possibly-simplified
1807 newexpr = makeNode(FuncExpr);
1808 newexpr->funcid = expr->funcid;
1809 newexpr->funcresulttype = expr->funcresulttype;
1810 newexpr->funcretset = expr->funcretset;
1811 newexpr->funcformat = expr->funcformat;
1812 newexpr->args = args;
1813 return (Node *) newexpr;
1815 if (IsA(node, OpExpr))
1817 OpExpr *expr = (OpExpr *) node;
1823 * Reduce constants in the OpExpr's arguments. We know args is either
1824 * NIL or a List node, so we can call expression_tree_mutator directly
1825 * rather than recursing to self.
1827 args = (List *) expression_tree_mutator((Node *) expr->args,
1828 eval_const_expressions_mutator,
1832 * Need to get OID of underlying function. Okay to scribble on input
1838 * Code for op/func reduction is pretty bulky, so split it out as a
1839 * separate function.
1841 simple = simplify_function(expr->opfuncid,
1842 expr->opresulttype, -1,
1845 if (simple) /* successfully simplified it */
1846 return (Node *) simple;
1849 * If the operator is boolean equality, we know how to simplify cases
1850 * involving one constant and one non-constant argument.
1852 if (expr->opno == BooleanEqualOperator)
1854 simple = simplify_boolean_equality(args);
1855 if (simple) /* successfully simplified it */
1856 return (Node *) simple;
1860 * The expression cannot be simplified any further, so build and
1861 * return a replacement OpExpr node using the possibly-simplified
1864 newexpr = makeNode(OpExpr);
1865 newexpr->opno = expr->opno;
1866 newexpr->opfuncid = expr->opfuncid;
1867 newexpr->opresulttype = expr->opresulttype;
1868 newexpr->opretset = expr->opretset;
1869 newexpr->args = args;
1870 return (Node *) newexpr;
1872 if (IsA(node, DistinctExpr))
1874 DistinctExpr *expr = (DistinctExpr *) node;
1877 bool has_null_input = false;
1878 bool all_null_input = true;
1879 bool has_nonconst_input = false;
1881 DistinctExpr *newexpr;
1884 * Reduce constants in the DistinctExpr's arguments. We know args is
1885 * either NIL or a List node, so we can call expression_tree_mutator
1886 * directly rather than recursing to self.
1888 args = (List *) expression_tree_mutator((Node *) expr->args,
1889 eval_const_expressions_mutator,
1893 * We must do our own check for NULLs because DistinctExpr has
1894 * different results for NULL input than the underlying operator does.
1898 if (IsA(lfirst(arg), Const))
1900 has_null_input |= ((Const *) lfirst(arg))->constisnull;
1901 all_null_input &= ((Const *) lfirst(arg))->constisnull;
1904 has_nonconst_input = true;
1907 /* all constants? then can optimize this out */
1908 if (!has_nonconst_input)
1910 /* all nulls? then not distinct */
1912 return makeBoolConst(false, false);
1914 /* one null? then distinct */
1916 return makeBoolConst(true, false);
1918 /* otherwise try to evaluate the '=' operator */
1919 /* (NOT okay to try to inline it, though!) */
1922 * Need to get OID of underlying function. Okay to scribble on
1923 * input to this extent.
1925 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
1928 * Code for op/func reduction is pretty bulky, so split it out as
1929 * a separate function.
1931 simple = simplify_function(expr->opfuncid,
1932 expr->opresulttype, -1,
1935 if (simple) /* successfully simplified it */
1938 * Since the underlying operator is "=", must negate its
1941 Const *csimple = (Const *) simple;
1943 Assert(IsA(csimple, Const));
1944 csimple->constvalue =
1945 BoolGetDatum(!DatumGetBool(csimple->constvalue));
1946 return (Node *) csimple;
1951 * The expression cannot be simplified any further, so build and
1952 * return a replacement DistinctExpr node using the
1953 * possibly-simplified arguments.
1955 newexpr = makeNode(DistinctExpr);
1956 newexpr->opno = expr->opno;
1957 newexpr->opfuncid = expr->opfuncid;
1958 newexpr->opresulttype = expr->opresulttype;
1959 newexpr->opretset = expr->opretset;
1960 newexpr->args = args;
1961 return (Node *) newexpr;
1963 if (IsA(node, BoolExpr))
1965 BoolExpr *expr = (BoolExpr *) node;
1967 switch (expr->boolop)
1972 bool haveNull = false;
1973 bool forceTrue = false;
1975 newargs = simplify_or_arguments(expr->args, context,
1976 &haveNull, &forceTrue);
1978 return makeBoolConst(true, false);
1980 newargs = lappend(newargs, makeBoolConst(false, true));
1981 /* If all the inputs are FALSE, result is FALSE */
1983 return makeBoolConst(false, false);
1984 /* If only one nonconst-or-NULL input, it's the result */
1985 if (list_length(newargs) == 1)
1986 return (Node *) linitial(newargs);
1987 /* Else we still need an OR node */
1988 return (Node *) make_orclause(newargs);
1993 bool haveNull = false;
1994 bool forceFalse = false;
1996 newargs = simplify_and_arguments(expr->args, context,
1997 &haveNull, &forceFalse);
1999 return makeBoolConst(false, false);
2001 newargs = lappend(newargs, makeBoolConst(false, true));
2002 /* If all the inputs are TRUE, result is TRUE */
2004 return makeBoolConst(true, false);
2005 /* If only one nonconst-or-NULL input, it's the result */
2006 if (list_length(newargs) == 1)
2007 return (Node *) linitial(newargs);
2008 /* Else we still need an AND node */
2009 return (Node *) make_andclause(newargs);
2015 Assert(list_length(expr->args) == 1);
2016 arg = eval_const_expressions_mutator(linitial(expr->args),
2018 if (IsA(arg, Const))
2020 Const *const_input = (Const *) arg;
2022 /* NOT NULL => NULL */
2023 if (const_input->constisnull)
2024 return makeBoolConst(false, true);
2025 /* otherwise pretty easy */
2026 return makeBoolConst(!DatumGetBool(const_input->constvalue),
2029 else if (not_clause(arg))
2031 /* Cancel NOT/NOT */
2032 return (Node *) get_notclausearg((Expr *) arg);
2034 /* Else we still need a NOT node */
2035 return (Node *) make_notclause((Expr *) arg);
2038 elog(ERROR, "unrecognized boolop: %d",
2039 (int) expr->boolop);
2043 if (IsA(node, SubPlan))
2046 * Return a SubPlan unchanged --- too late to do anything with it.
2048 * XXX should we ereport() here instead? Probably this routine should
2049 * never be invoked after SubPlan creation.
2053 if (IsA(node, RelabelType))
2056 * If we can simplify the input to a constant, then we don't need the
2057 * RelabelType node anymore: just change the type field of the Const
2058 * node. Otherwise, must copy the RelabelType node.
2060 RelabelType *relabel = (RelabelType *) node;
2063 arg = eval_const_expressions_mutator((Node *) relabel->arg,
2067 * If we find stacked RelabelTypes (eg, from foo :: int :: oid) we can
2068 * discard all but the top one.
2070 while (arg && IsA(arg, RelabelType))
2071 arg = (Node *) ((RelabelType *) arg)->arg;
2073 if (arg && IsA(arg, Const))
2075 Const *con = (Const *) arg;
2077 con->consttype = relabel->resulttype;
2078 con->consttypmod = relabel->resulttypmod;
2079 return (Node *) con;
2083 RelabelType *newrelabel = makeNode(RelabelType);
2085 newrelabel->arg = (Expr *) arg;
2086 newrelabel->resulttype = relabel->resulttype;
2087 newrelabel->resulttypmod = relabel->resulttypmod;
2088 newrelabel->relabelformat = relabel->relabelformat;
2089 return (Node *) newrelabel;
2092 if (IsA(node, CaseExpr))
2095 * CASE expressions can be simplified if there are constant
2096 * condition clauses:
2097 * FALSE (or NULL): drop the alternative
2098 * TRUE: drop all remaining alternatives
2099 * If the first non-FALSE alternative is a constant TRUE, we can
2100 * simplify the entire CASE to that alternative's expression.
2101 * If there are no non-FALSE alternatives, we simplify the entire
2102 * CASE to the default result (ELSE result).
2104 * If we have a simple-form CASE with constant test expression,
2105 * we substitute the constant value for contained CaseTestExpr
2106 * placeholder nodes, so that we have the opportunity to reduce
2107 * constant test conditions. For example this allows
2108 * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
2109 * to reduce to 1 rather than drawing a divide-by-0 error.
2112 CaseExpr *caseexpr = (CaseExpr *) node;
2114 Node *save_case_val;
2117 bool const_true_cond;
2118 Node *defresult = NULL;
2121 /* Simplify the test expression, if any */
2122 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
2125 /* Set up for contained CaseTestExpr nodes */
2126 save_case_val = context->case_val;
2127 if (newarg && IsA(newarg, Const))
2128 context->case_val = newarg;
2130 context->case_val = NULL;
2132 /* Simplify the WHEN clauses */
2134 const_true_cond = false;
2135 foreach(arg, caseexpr->args)
2137 CaseWhen *oldcasewhen = (CaseWhen *) lfirst(arg);
2141 Assert(IsA(oldcasewhen, CaseWhen));
2143 /* Simplify this alternative's test condition */
2145 eval_const_expressions_mutator((Node *) oldcasewhen->expr,
2149 * If the test condition is constant FALSE (or NULL), then drop
2150 * this WHEN clause completely, without processing the result.
2152 if (casecond && IsA(casecond, Const))
2154 Const *const_input = (Const *) casecond;
2156 if (const_input->constisnull ||
2157 !DatumGetBool(const_input->constvalue))
2158 continue; /* drop alternative with FALSE condition */
2159 /* Else it's constant TRUE */
2160 const_true_cond = true;
2163 /* Simplify this alternative's result value */
2165 eval_const_expressions_mutator((Node *) oldcasewhen->result,
2168 /* If non-constant test condition, emit a new WHEN node */
2169 if (!const_true_cond)
2171 CaseWhen *newcasewhen = makeNode(CaseWhen);
2173 newcasewhen->expr = (Expr *) casecond;
2174 newcasewhen->result = (Expr *) caseresult;
2175 newargs = lappend(newargs, newcasewhen);
2180 * Found a TRUE condition, so none of the remaining alternatives
2181 * can be reached. We treat the result as the default result.
2183 defresult = caseresult;
2187 /* Simplify the default result, unless we replaced it above */
2188 if (!const_true_cond)
2190 eval_const_expressions_mutator((Node *) caseexpr->defresult,
2193 context->case_val = save_case_val;
2195 /* If no non-FALSE alternatives, CASE reduces to the default result */
2198 /* Otherwise we need a new CASE node */
2199 newcase = makeNode(CaseExpr);
2200 newcase->casetype = caseexpr->casetype;
2201 newcase->arg = (Expr *) newarg;
2202 newcase->args = newargs;
2203 newcase->defresult = (Expr *) defresult;
2204 return (Node *) newcase;
2206 if (IsA(node, CaseTestExpr))
2209 * If we know a constant test value for the current CASE construct,
2210 * substitute it for the placeholder. Else just return the
2211 * placeholder as-is.
2213 if (context->case_val)
2214 return copyObject(context->case_val);
2216 return copyObject(node);
2218 if (IsA(node, ArrayExpr))
2220 ArrayExpr *arrayexpr = (ArrayExpr *) node;
2221 ArrayExpr *newarray;
2222 bool all_const = true;
2227 foreach(element, arrayexpr->elements)
2231 e = eval_const_expressions_mutator((Node *) lfirst(element),
2235 newelems = lappend(newelems, e);
2238 newarray = makeNode(ArrayExpr);
2239 newarray->array_typeid = arrayexpr->array_typeid;
2240 newarray->element_typeid = arrayexpr->element_typeid;
2241 newarray->elements = newelems;
2242 newarray->multidims = arrayexpr->multidims;
2245 return (Node *) evaluate_expr((Expr *) newarray,
2246 newarray->array_typeid,
2249 return (Node *) newarray;
2251 if (IsA(node, CoalesceExpr))
2253 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2254 CoalesceExpr *newcoalesce;
2259 foreach(arg, coalesceexpr->args)
2263 e = eval_const_expressions_mutator((Node *) lfirst(arg),
2267 * We can remove null constants from the list. For a non-null
2268 * constant, if it has not been preceded by any other
2269 * non-null-constant expressions then that is the result.
2273 if (((Const *) e)->constisnull)
2274 continue; /* drop null constant */
2276 return e; /* first expr */
2278 newargs = lappend(newargs, e);
2281 /* If all the arguments were constant null, the result is just null */
2283 return (Node *) makeNullConst(coalesceexpr->coalescetype, -1);
2285 newcoalesce = makeNode(CoalesceExpr);
2286 newcoalesce->coalescetype = coalesceexpr->coalescetype;
2287 newcoalesce->args = newargs;
2288 return (Node *) newcoalesce;
2290 if (IsA(node, FieldSelect))
2293 * We can optimize field selection from a whole-row Var into a simple
2294 * Var. (This case won't be generated directly by the parser, because
2295 * ParseComplexProjection short-circuits it. But it can arise while
2296 * simplifying functions.) Also, we can optimize field selection from
2297 * a RowExpr construct.
2299 * We must however check that the declared type of the field is still
2300 * the same as when the FieldSelect was created --- this can change if
2301 * someone did ALTER COLUMN TYPE on the rowtype.
2303 FieldSelect *fselect = (FieldSelect *) node;
2304 FieldSelect *newfselect;
2307 arg = eval_const_expressions_mutator((Node *) fselect->arg,
2309 if (arg && IsA(arg, Var) &&
2310 ((Var *) arg)->varattno == InvalidAttrNumber)
2312 if (rowtype_field_matches(((Var *) arg)->vartype,
2314 fselect->resulttype,
2315 fselect->resulttypmod))
2316 return (Node *) makeVar(((Var *) arg)->varno,
2318 fselect->resulttype,
2319 fselect->resulttypmod,
2320 ((Var *) arg)->varlevelsup);
2322 if (arg && IsA(arg, RowExpr))
2324 RowExpr *rowexpr = (RowExpr *) arg;
2326 if (fselect->fieldnum > 0 &&
2327 fselect->fieldnum <= list_length(rowexpr->args))
2329 Node *fld = (Node *) list_nth(rowexpr->args,
2330 fselect->fieldnum - 1);
2332 if (rowtype_field_matches(rowexpr->row_typeid,
2334 fselect->resulttype,
2335 fselect->resulttypmod) &&
2336 fselect->resulttype == exprType(fld) &&
2337 fselect->resulttypmod == exprTypmod(fld))
2341 newfselect = makeNode(FieldSelect);
2342 newfselect->arg = (Expr *) arg;
2343 newfselect->fieldnum = fselect->fieldnum;
2344 newfselect->resulttype = fselect->resulttype;
2345 newfselect->resulttypmod = fselect->resulttypmod;
2346 return (Node *) newfselect;
2348 if (IsA(node, NullTest))
2350 NullTest *ntest = (NullTest *) node;
2354 arg = eval_const_expressions_mutator((Node *) ntest->arg,
2356 if (arg && IsA(arg, RowExpr))
2358 RowExpr *rarg = (RowExpr *) arg;
2359 List *newargs = NIL;
2363 * We break ROW(...) IS [NOT] NULL into separate tests on its
2364 * component fields. This form is usually more efficient to
2365 * evaluate, as well as being more amenable to optimization.
2367 foreach(l, rarg->args)
2369 Node *relem = (Node *) lfirst(l);
2372 * A constant field refutes the whole NullTest if it's of the
2373 * wrong nullness; else we can discard it.
2375 if (relem && IsA(relem, Const))
2377 Const *carg = (Const *) relem;
2379 if (carg->constisnull ?
2380 (ntest->nulltesttype == IS_NOT_NULL) :
2381 (ntest->nulltesttype == IS_NULL))
2382 return makeBoolConst(false, false);
2385 newntest = makeNode(NullTest);
2386 newntest->arg = (Expr *) relem;
2387 newntest->nulltesttype = ntest->nulltesttype;
2388 newargs = lappend(newargs, newntest);
2390 /* If all the inputs were constants, result is TRUE */
2392 return makeBoolConst(true, false);
2393 /* If only one nonconst input, it's the result */
2394 if (list_length(newargs) == 1)
2395 return (Node *) linitial(newargs);
2396 /* Else we need an AND node */
2397 return (Node *) make_andclause(newargs);
2399 if (arg && IsA(arg, Const))
2401 Const *carg = (Const *) arg;
2404 switch (ntest->nulltesttype)
2407 result = carg->constisnull;
2410 result = !carg->constisnull;
2413 elog(ERROR, "unrecognized nulltesttype: %d",
2414 (int) ntest->nulltesttype);
2415 result = false; /* keep compiler quiet */
2419 return makeBoolConst(result, false);
2422 newntest = makeNode(NullTest);
2423 newntest->arg = (Expr *) arg;
2424 newntest->nulltesttype = ntest->nulltesttype;
2425 return (Node *) newntest;
2427 if (IsA(node, BooleanTest))
2429 BooleanTest *btest = (BooleanTest *) node;
2430 BooleanTest *newbtest;
2433 arg = eval_const_expressions_mutator((Node *) btest->arg,
2435 if (arg && IsA(arg, Const))
2437 Const *carg = (Const *) arg;
2440 switch (btest->booltesttype)
2443 result = (!carg->constisnull &&
2444 DatumGetBool(carg->constvalue));
2447 result = (carg->constisnull ||
2448 !DatumGetBool(carg->constvalue));
2451 result = (!carg->constisnull &&
2452 !DatumGetBool(carg->constvalue));
2455 result = (carg->constisnull ||
2456 DatumGetBool(carg->constvalue));
2459 result = carg->constisnull;
2461 case IS_NOT_UNKNOWN:
2462 result = !carg->constisnull;
2465 elog(ERROR, "unrecognized booltesttype: %d",
2466 (int) btest->booltesttype);
2467 result = false; /* keep compiler quiet */
2471 return makeBoolConst(result, false);
2474 newbtest = makeNode(BooleanTest);
2475 newbtest->arg = (Expr *) arg;
2476 newbtest->booltesttype = btest->booltesttype;
2477 return (Node *) newbtest;
2481 * For any node type not handled above, we recurse using
2482 * expression_tree_mutator, which will copy the node unchanged but try to
2483 * simplify its arguments (if any) using this routine. For example: we
2484 * cannot eliminate an ArrayRef node, but we might be able to simplify
2485 * constant expressions in its subscripts.
2487 return expression_tree_mutator(node, eval_const_expressions_mutator,
2492 * Subroutine for eval_const_expressions: process arguments of an OR clause
2494 * This includes flattening of nested ORs as well as recursion to
2495 * eval_const_expressions to simplify the OR arguments.
2497 * After simplification, OR arguments are handled as follows:
2498 * non constant: keep
2499 * FALSE: drop (does not affect result)
2500 * TRUE: force result to TRUE
2501 * NULL: keep only one
2502 * We must keep one NULL input because ExecEvalOr returns NULL when no input
2503 * is TRUE and at least one is NULL. We don't actually include the NULL
2504 * here, that's supposed to be done by the caller.
2506 * The output arguments *haveNull and *forceTrue must be initialized FALSE
2507 * by the caller. They will be set TRUE if a null constant or true constant,
2508 * respectively, is detected anywhere in the argument list.
2511 simplify_or_arguments(List *args,
2512 eval_const_expressions_context *context,
2513 bool *haveNull, bool *forceTrue)
2515 List *newargs = NIL;
2516 List *unprocessed_args;
2519 * Since the parser considers OR to be a binary operator, long OR lists
2520 * become deeply nested expressions. We must flatten these into long
2521 * argument lists of a single OR operator. To avoid blowing out the stack
2522 * with recursion of eval_const_expressions, we resort to some tenseness
2523 * here: we keep a list of not-yet-processed inputs, and handle flattening
2524 * of nested ORs by prepending to the to-do list instead of recursing.
2526 unprocessed_args = list_copy(args);
2527 while (unprocessed_args)
2529 Node *arg = (Node *) linitial(unprocessed_args);
2531 unprocessed_args = list_delete_first(unprocessed_args);
2533 /* flatten nested ORs as per above comment */
2536 List *subargs = list_copy(((BoolExpr *) arg)->args);
2538 /* overly tense code to avoid leaking unused list header */
2539 if (!unprocessed_args)
2540 unprocessed_args = subargs;
2543 List *oldhdr = unprocessed_args;
2545 unprocessed_args = list_concat(subargs, unprocessed_args);
2551 /* If it's not an OR, simplify it */
2552 arg = eval_const_expressions_mutator(arg, context);
2555 * It is unlikely but not impossible for simplification of a non-OR
2556 * clause to produce an OR. Recheck, but don't be too tense about it
2557 * since it's not a mainstream case. In particular we don't worry
2558 * about const-simplifying the input twice.
2562 List *subargs = list_copy(((BoolExpr *) arg)->args);
2564 unprocessed_args = list_concat(subargs, unprocessed_args);
2569 * OK, we have a const-simplified non-OR argument. Process it per
2572 if (IsA(arg, Const))
2574 Const *const_input = (Const *) arg;
2576 if (const_input->constisnull)
2578 else if (DatumGetBool(const_input->constvalue))
2583 * Once we detect a TRUE result we can just exit the loop
2584 * immediately. However, if we ever add a notion of
2585 * non-removable functions, we'd need to keep scanning.
2589 /* otherwise, we can drop the constant-false input */
2593 /* else emit the simplified arg into the result list */
2594 newargs = lappend(newargs, arg);
2601 * Subroutine for eval_const_expressions: process arguments of an AND clause
2603 * This includes flattening of nested ANDs as well as recursion to
2604 * eval_const_expressions to simplify the AND arguments.
2606 * After simplification, AND arguments are handled as follows:
2607 * non constant: keep
2608 * TRUE: drop (does not affect result)
2609 * FALSE: force result to FALSE
2610 * NULL: keep only one
2611 * We must keep one NULL input because ExecEvalAnd returns NULL when no input
2612 * is FALSE and at least one is NULL. We don't actually include the NULL
2613 * here, that's supposed to be done by the caller.
2615 * The output arguments *haveNull and *forceFalse must be initialized FALSE
2616 * by the caller. They will be set TRUE if a null constant or false constant,
2617 * respectively, is detected anywhere in the argument list.
2620 simplify_and_arguments(List *args,
2621 eval_const_expressions_context *context,
2622 bool *haveNull, bool *forceFalse)
2624 List *newargs = NIL;
2625 List *unprocessed_args;
2627 /* See comments in simplify_or_arguments */
2628 unprocessed_args = list_copy(args);
2629 while (unprocessed_args)
2631 Node *arg = (Node *) linitial(unprocessed_args);
2633 unprocessed_args = list_delete_first(unprocessed_args);
2635 /* flatten nested ANDs as per above comment */
2636 if (and_clause(arg))
2638 List *subargs = list_copy(((BoolExpr *) arg)->args);
2640 /* overly tense code to avoid leaking unused list header */
2641 if (!unprocessed_args)
2642 unprocessed_args = subargs;
2645 List *oldhdr = unprocessed_args;
2647 unprocessed_args = list_concat(subargs, unprocessed_args);
2653 /* If it's not an AND, simplify it */
2654 arg = eval_const_expressions_mutator(arg, context);
2657 * It is unlikely but not impossible for simplification of a non-AND
2658 * clause to produce an AND. Recheck, but don't be too tense about it
2659 * since it's not a mainstream case. In particular we don't worry
2660 * about const-simplifying the input twice.
2662 if (and_clause(arg))
2664 List *subargs = list_copy(((BoolExpr *) arg)->args);
2666 unprocessed_args = list_concat(subargs, unprocessed_args);
2671 * OK, we have a const-simplified non-AND argument. Process it per
2674 if (IsA(arg, Const))
2676 Const *const_input = (Const *) arg;
2678 if (const_input->constisnull)
2680 else if (!DatumGetBool(const_input->constvalue))
2685 * Once we detect a FALSE result we can just exit the loop
2686 * immediately. However, if we ever add a notion of
2687 * non-removable functions, we'd need to keep scanning.
2691 /* otherwise, we can drop the constant-true input */
2695 /* else emit the simplified arg into the result list */
2696 newargs = lappend(newargs, arg);
2703 * Subroutine for eval_const_expressions: try to simplify boolean equality
2705 * Input is the list of simplified arguments to the operator.
2706 * Returns a simplified expression if successful, or NULL if cannot
2707 * simplify the expression.
2709 * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x".
2710 * This is only marginally useful in itself, but doing it in constant folding
2711 * ensures that we will recognize the two forms as being equivalent in, for
2712 * example, partial index matching.
2714 * We come here only if simplify_function has failed; therefore we cannot
2715 * see two constant inputs, nor a constant-NULL input.
2718 simplify_boolean_equality(List *args)
2723 Assert(list_length(args) == 2);
2724 leftop = linitial(args);
2725 rightop = lsecond(args);
2726 if (leftop && IsA(leftop, Const))
2728 Assert(!((Const *) leftop)->constisnull);
2729 if (DatumGetBool(((Const *) leftop)->constvalue))
2730 return rightop; /* true = foo */
2732 return make_notclause(rightop); /* false = foo */
2734 if (rightop && IsA(rightop, Const))
2736 Assert(!((Const *) rightop)->constisnull);
2737 if (DatumGetBool(((Const *) rightop)->constvalue))
2738 return leftop; /* foo = true */
2740 return make_notclause(leftop); /* foo = false */
2746 * Subroutine for eval_const_expressions: try to simplify a function call
2747 * (which might originally have been an operator; we don't care)
2749 * Inputs are the function OID, actual result type OID (which is needed for
2750 * polymorphic functions) and typmod, and the pre-simplified argument list;
2751 * also the context data for eval_const_expressions.
2753 * Returns a simplified expression if successful, or NULL if cannot
2754 * simplify the function call.
2757 simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
2760 eval_const_expressions_context *context)
2762 HeapTuple func_tuple;
2766 * We have two strategies for simplification: either execute the function
2767 * to deliver a constant result, or expand in-line the body of the
2768 * function definition (which only works for simple SQL-language
2769 * functions, but that is a common case). In either case we need access
2770 * to the function's pg_proc tuple, so fetch it just once to use in both
2773 func_tuple = SearchSysCache(PROCOID,
2774 ObjectIdGetDatum(funcid),
2776 if (!HeapTupleIsValid(func_tuple))
2777 elog(ERROR, "cache lookup failed for function %u", funcid);
2779 newexpr = evaluate_function(funcid, result_type, result_typmod, args,
2780 func_tuple, context);
2782 if (!newexpr && allow_inline)
2783 newexpr = inline_function(funcid, result_type, args,
2784 func_tuple, context);
2786 ReleaseSysCache(func_tuple);
2792 * evaluate_function: try to pre-evaluate a function call
2794 * We can do this if the function is strict and has any constant-null inputs
2795 * (just return a null constant), or if the function is immutable and has all
2796 * constant inputs (call it and return the result as a Const node). In
2797 * estimation mode we are willing to pre-evaluate stable functions too.
2799 * Returns a simplified expression if successful, or NULL if cannot
2800 * simplify the function.
2803 evaluate_function(Oid funcid, Oid result_type, int32 result_typmod, List *args,
2804 HeapTuple func_tuple,
2805 eval_const_expressions_context *context)
2807 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
2808 bool has_nonconst_input = false;
2809 bool has_null_input = false;
2814 * Can't simplify if it returns a set.
2816 if (funcform->proretset)
2820 * Can't simplify if it returns RECORD. The immediate problem is that it
2821 * will be needing an expected tupdesc which we can't supply here.
2823 * In the case where it has OUT parameters, it could get by without an
2824 * expected tupdesc, but we still have issues: get_expr_result_type()
2825 * doesn't know how to extract type info from a RECORD constant, and in
2826 * the case of a NULL function result there doesn't seem to be any clean
2827 * way to fix that. In view of the likelihood of there being still other
2828 * gotchas, seems best to leave the function call unreduced.
2830 if (funcform->prorettype == RECORDOID)
2834 * Check for constant inputs and especially constant-NULL inputs.
2838 if (IsA(lfirst(arg), Const))
2839 has_null_input |= ((Const *) lfirst(arg))->constisnull;
2841 has_nonconst_input = true;
2845 * If the function is strict and has a constant-NULL input, it will never
2846 * be called at all, so we can replace the call by a NULL constant, even
2847 * if there are other inputs that aren't constant, and even if the
2848 * function is not otherwise immutable.
2850 if (funcform->proisstrict && has_null_input)
2851 return (Expr *) makeNullConst(result_type, result_typmod);
2854 * Otherwise, can simplify only if all inputs are constants. (For a
2855 * non-strict function, constant NULL inputs are treated the same as
2856 * constant non-NULL inputs.)
2858 if (has_nonconst_input)
2862 * Ordinarily we are only allowed to simplify immutable functions. But for
2863 * purposes of estimation, we consider it okay to simplify functions that
2864 * are merely stable; the risk that the result might change from planning
2865 * time to execution time is worth taking in preference to not being able
2866 * to estimate the value at all.
2868 if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
2870 else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
2876 * OK, looks like we can simplify this operator/function.
2878 * Build a new FuncExpr node containing the already-simplified arguments.
2880 newexpr = makeNode(FuncExpr);
2881 newexpr->funcid = funcid;
2882 newexpr->funcresulttype = result_type;
2883 newexpr->funcretset = false;
2884 newexpr->funcformat = COERCE_DONTCARE; /* doesn't matter */
2885 newexpr->args = args;
2887 return evaluate_expr((Expr *) newexpr, result_type, result_typmod);
2891 * inline_function: try to expand a function call inline
2893 * If the function is a sufficiently simple SQL-language function
2894 * (just "SELECT expression"), then we can inline it and avoid the rather
2895 * high per-call overhead of SQL functions. Furthermore, this can expose
2896 * opportunities for constant-folding within the function expression.
2898 * We have to beware of some special cases however. A directly or
2899 * indirectly recursive function would cause us to recurse forever,
2900 * so we keep track of which functions we are already expanding and
2901 * do not re-expand them. Also, if a parameter is used more than once
2902 * in the SQL-function body, we require it not to contain any volatile
2903 * functions (volatiles might deliver inconsistent answers) nor to be
2904 * unreasonably expensive to evaluate. The expensiveness check not only
2905 * prevents us from doing multiple evaluations of an expensive parameter
2906 * at runtime, but is a safety value to limit growth of an expression due
2907 * to repeated inlining.
2909 * We must also beware of changing the volatility or strictness status of
2910 * functions by inlining them.
2912 * Returns a simplified expression if successful, or NULL if cannot
2913 * simplify the function.
2916 inline_function(Oid funcid, Oid result_type, List *args,
2917 HeapTuple func_tuple,
2918 eval_const_expressions_context *context)
2920 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
2925 MemoryContext oldcxt;
2926 MemoryContext mycxt;
2927 ErrorContextCallback sqlerrcontext;
2928 List *raw_parsetree_list;
2936 * Forget it if the function is not SQL-language or has other showstopper
2937 * properties. (The nargs check is just paranoia.)
2939 if (funcform->prolang != SQLlanguageId ||
2940 funcform->prosecdef ||
2941 funcform->proretset ||
2942 !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
2943 funcform->pronargs != list_length(args))
2946 /* Check for recursive function, and give up trying to expand if so */
2947 if (list_member_oid(context->active_fns, funcid))
2950 /* Check permission to call function (fail later, if not) */
2951 if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
2955 * Setup error traceback support for ereport(). This is so that we can
2956 * finger the function that bad information came from.
2958 sqlerrcontext.callback = sql_inline_error_callback;
2959 sqlerrcontext.arg = func_tuple;
2960 sqlerrcontext.previous = error_context_stack;
2961 error_context_stack = &sqlerrcontext;
2964 * Make a temporary memory context, so that we don't leak all the stuff
2965 * that parsing might create.
2967 mycxt = AllocSetContextCreate(CurrentMemoryContext,
2969 ALLOCSET_DEFAULT_MINSIZE,
2970 ALLOCSET_DEFAULT_INITSIZE,
2971 ALLOCSET_DEFAULT_MAXSIZE);
2972 oldcxt = MemoryContextSwitchTo(mycxt);
2974 /* Check for polymorphic arguments, and substitute actual arg types */
2975 argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
2976 memcpy(argtypes, funcform->proargtypes.values,
2977 funcform->pronargs * sizeof(Oid));
2978 for (i = 0; i < funcform->pronargs; i++)
2980 if (IsPolymorphicType(argtypes[i]))
2982 argtypes[i] = exprType((Node *) list_nth(args, i));
2986 /* Fetch and parse the function body */
2987 tmp = SysCacheGetAttr(PROCOID,
2989 Anum_pg_proc_prosrc,
2992 elog(ERROR, "null prosrc for function %u", funcid);
2993 src = DatumGetCString(DirectFunctionCall1(textout, tmp));
2996 * We just do parsing and parse analysis, not rewriting, because rewriting
2997 * will not affect table-free-SELECT-only queries, which is all that we
2998 * care about. Also, we can punt as soon as we detect more than one
2999 * command in the function body.
3001 raw_parsetree_list = pg_parse_query(src);
3002 if (list_length(raw_parsetree_list) != 1)
3005 querytree = parse_analyze(linitial(raw_parsetree_list), src,
3006 argtypes, funcform->pronargs);
3009 * The single command must be a simple "SELECT expression".
3011 if (!IsA(querytree, Query) ||
3012 querytree->commandType != CMD_SELECT ||
3013 querytree->utilityStmt ||
3014 querytree->intoClause ||
3015 querytree->hasAggs ||
3016 querytree->hasSubLinks ||
3017 querytree->rtable ||
3018 querytree->jointree->fromlist ||
3019 querytree->jointree->quals ||
3020 querytree->groupClause ||
3021 querytree->havingQual ||
3022 querytree->distinctClause ||
3023 querytree->sortClause ||
3024 querytree->limitOffset ||
3025 querytree->limitCount ||
3026 querytree->setOperations ||
3027 list_length(querytree->targetList) != 1)
3030 newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
3033 * Make sure the function (still) returns what it's declared to. This
3034 * will raise an error if wrong, but that's okay since the function would
3035 * fail at runtime anyway. Note we do not try this until we have verified
3036 * that no rewriting was needed; that's probably not important, but let's
3039 if (check_sql_fn_retval(funcid, result_type, list_make1(querytree), NULL))
3040 goto fail; /* reject whole-tuple-result cases */
3043 * Additional validity checks on the expression. It mustn't return a set,
3044 * and it mustn't be more volatile than the surrounding function (this is
3045 * to avoid breaking hacks that involve pretending a function is immutable
3046 * when it really ain't). If the surrounding function is declared strict,
3047 * then the expression must contain only strict constructs and must use
3048 * all of the function parameters (this is overkill, but an exact analysis
3051 if (expression_returns_set(newexpr))
3054 if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
3055 contain_mutable_functions(newexpr))
3057 else if (funcform->provolatile == PROVOLATILE_STABLE &&
3058 contain_volatile_functions(newexpr))
3061 if (funcform->proisstrict &&
3062 contain_nonstrict_functions(newexpr))
3066 * We may be able to do it; there are still checks on parameter usage to
3067 * make, but those are most easily done in combination with the actual
3068 * substitution of the inputs. So start building expression with inputs
3071 usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
3072 newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
3075 /* Now check for parameter usage */
3079 Node *param = lfirst(arg);
3081 if (usecounts[i] == 0)
3083 /* Param not used at all: uncool if func is strict */
3084 if (funcform->proisstrict)
3087 else if (usecounts[i] != 1)
3089 /* Param used multiple times: uncool if expensive or volatile */
3093 * We define "expensive" as "contains any subplan or more than 10
3094 * operators". Note that the subplan search has to be done
3095 * explicitly, since cost_qual_eval() will barf on unplanned
3098 if (contain_subplans(param))
3100 cost_qual_eval(&eval_cost, list_make1(param), NULL);
3101 if (eval_cost.startup + eval_cost.per_tuple >
3102 10 * cpu_operator_cost)
3106 * Check volatility last since this is more expensive than the
3109 if (contain_volatile_functions(param))
3116 * Whew --- we can make the substitution. Copy the modified expression
3117 * out of the temporary memory context, and clean up.
3119 MemoryContextSwitchTo(oldcxt);
3121 newexpr = copyObject(newexpr);
3123 MemoryContextDelete(mycxt);
3126 * Since check_sql_fn_retval allows binary-compatibility cases, the
3127 * expression we now have might return some type that's only binary
3128 * compatible with the original expression result type. To avoid
3129 * confusing matters, insert a RelabelType in such cases.
3131 if (exprType(newexpr) != result_type)
3133 Assert(IsBinaryCoercible(exprType(newexpr), result_type));
3134 newexpr = (Node *) makeRelabelType((Expr *) newexpr,
3137 COERCE_IMPLICIT_CAST);
3141 * Recursively try to simplify the modified expression. Here we must add
3142 * the current function to the context list of active functions.
3144 context->active_fns = lcons_oid(funcid, context->active_fns);
3145 newexpr = eval_const_expressions_mutator(newexpr, context);
3146 context->active_fns = list_delete_first(context->active_fns);
3148 error_context_stack = sqlerrcontext.previous;
3150 return (Expr *) newexpr;
3152 /* Here if func is not inlinable: release temp memory and return NULL */
3154 MemoryContextSwitchTo(oldcxt);
3155 MemoryContextDelete(mycxt);
3156 error_context_stack = sqlerrcontext.previous;
3162 * Replace Param nodes by appropriate actual parameters
3165 substitute_actual_parameters(Node *expr, int nargs, List *args,
3168 substitute_actual_parameters_context context;
3170 context.nargs = nargs;
3171 context.args = args;
3172 context.usecounts = usecounts;
3174 return substitute_actual_parameters_mutator(expr, &context);
3178 substitute_actual_parameters_mutator(Node *node,
3179 substitute_actual_parameters_context *context)
3183 if (IsA(node, Param))
3185 Param *param = (Param *) node;
3187 if (param->paramkind != PARAM_EXTERN)
3188 elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
3189 if (param->paramid <= 0 || param->paramid > context->nargs)
3190 elog(ERROR, "invalid paramid: %d", param->paramid);
3192 /* Count usage of parameter */
3193 context->usecounts[param->paramid - 1]++;
3195 /* Select the appropriate actual arg and replace the Param with it */
3196 /* We don't need to copy at this time (it'll get done later) */
3197 return list_nth(context->args, param->paramid - 1);
3199 return expression_tree_mutator(node, substitute_actual_parameters_mutator,
3204 * error context callback to let us supply a call-stack traceback
3207 sql_inline_error_callback(void *arg)
3209 HeapTuple func_tuple = (HeapTuple) arg;
3210 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3211 int syntaxerrposition;
3213 /* If it's a syntax error, convert to internal syntax error report */
3214 syntaxerrposition = geterrposition();
3215 if (syntaxerrposition > 0)
3221 tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
3224 elog(ERROR, "null prosrc");
3225 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
3227 internalerrposition(syntaxerrposition);
3228 internalerrquery(prosrc);
3231 errcontext("SQL function \"%s\" during inlining",
3232 NameStr(funcform->proname));
3236 * evaluate_expr: pre-evaluate a constant expression
3238 * We use the executor's routine ExecEvalExpr() to avoid duplication of
3239 * code and ensure we get the same result as the executor would get.
3242 evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod)
3245 ExprState *exprstate;
3246 MemoryContext oldcontext;
3250 bool resultTypByVal;
3253 * To use the executor, we need an EState.
3255 estate = CreateExecutorState();
3257 /* We can use the estate's working context to avoid memory leaks. */
3258 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3261 * Prepare expr for execution.
3263 exprstate = ExecPrepareExpr(expr, estate);
3268 * It is OK to use a default econtext because none of the ExecEvalExpr()
3269 * code used in this situation will use econtext. That might seem
3270 * fortuitous, but it's not so unreasonable --- a constant expression does
3271 * not depend on context, by definition, n'est ce pas?
3273 const_val = ExecEvalExprSwitchContext(exprstate,
3274 GetPerTupleExprContext(estate),
3275 &const_is_null, NULL);
3277 /* Get info needed about result datatype */
3278 get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
3280 /* Get back to outer memory context */
3281 MemoryContextSwitchTo(oldcontext);
3284 * Must copy result out of sub-context used by expression eval.
3286 * Also, if it's varlena, forcibly detoast it. This protects us against
3287 * storing TOAST pointers into plans that might outlive the referenced
3292 if (resultTypLen == -1)
3293 const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
3295 const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
3298 /* Release all the junk we just created */
3299 FreeExecutorState(estate);
3302 * Make the constant result node.
3304 return (Expr *) makeConst(result_type, result_typmod, resultTypLen,
3305 const_val, const_is_null,
3311 * Standard expression-tree walking support
3313 * We used to have near-duplicate code in many different routines that
3314 * understood how to recurse through an expression node tree. That was
3315 * a pain to maintain, and we frequently had bugs due to some particular
3316 * routine neglecting to support a particular node type. In most cases,
3317 * these routines only actually care about certain node types, and don't
3318 * care about other types except insofar as they have to recurse through
3319 * non-primitive node types. Therefore, we now provide generic tree-walking
3320 * logic to consolidate the redundant "boilerplate" code. There are
3321 * two versions: expression_tree_walker() and expression_tree_mutator().
3324 /*--------------------
3325 * expression_tree_walker() is designed to support routines that traverse
3326 * a tree in a read-only fashion (although it will also work for routines
3327 * that modify nodes in-place but never add/delete/replace nodes).
3328 * A walker routine should look like this:
3330 * bool my_walker (Node *node, my_struct *context)
3334 * // check for nodes that special work is required for, eg:
3335 * if (IsA(node, Var))
3337 * ... do special actions for Var nodes
3339 * else if (IsA(node, ...))
3341 * ... do special actions for other node types
3343 * // for any node type not specially processed, do:
3344 * return expression_tree_walker(node, my_walker, (void *) context);
3347 * The "context" argument points to a struct that holds whatever context
3348 * information the walker routine needs --- it can be used to return data
3349 * gathered by the walker, too. This argument is not touched by
3350 * expression_tree_walker, but it is passed down to recursive sub-invocations
3351 * of my_walker. The tree walk is started from a setup routine that
3352 * fills in the appropriate context struct, calls my_walker with the top-level
3353 * node of the tree, and then examines the results.
3355 * The walker routine should return "false" to continue the tree walk, or
3356 * "true" to abort the walk and immediately return "true" to the top-level
3357 * caller. This can be used to short-circuit the traversal if the walker
3358 * has found what it came for. "false" is returned to the top-level caller
3359 * iff no invocation of the walker returned "true".
3361 * The node types handled by expression_tree_walker include all those
3362 * normally found in target lists and qualifier clauses during the planning
3363 * stage. In particular, it handles List nodes since a cnf-ified qual clause
3364 * will have List structure at the top level, and it handles TargetEntry nodes
3365 * so that a scan of a target list can be handled without additional code.
3366 * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
3367 * handled, so that query jointrees and setOperation trees can be processed
3368 * without additional code.
3370 * expression_tree_walker will handle SubLink nodes by recursing normally
3371 * into the "testexpr" subtree (which is an expression belonging to the outer
3372 * plan). It will also call the walker on the sub-Query node; however, when
3373 * expression_tree_walker itself is called on a Query node, it does nothing
3374 * and returns "false". The net effect is that unless the walker does
3375 * something special at a Query node, sub-selects will not be visited during
3376 * an expression tree walk. This is exactly the behavior wanted in many cases
3377 * --- and for those walkers that do want to recurse into sub-selects, special
3378 * behavior is typically needed anyway at the entry to a sub-select (such as
3379 * incrementing a depth counter). A walker that wants to examine sub-selects
3380 * should include code along the lines of:
3382 * if (IsA(node, Query))
3384 * adjust context for subquery;
3385 * result = query_tree_walker((Query *) node, my_walker, context,
3386 * 0); // adjust flags as needed
3387 * restore context if needed;
3391 * query_tree_walker is a convenience routine (see below) that calls the
3392 * walker on all the expression subtrees of the given Query node.
3394 * expression_tree_walker will handle SubPlan nodes by recursing normally
3395 * into the "testexpr" and the "args" list (which are expressions belonging to
3396 * the outer plan). It will not touch the completed subplan, however. Since
3397 * there is no link to the original Query, it is not possible to recurse into
3398 * subselects of an already-planned expression tree. This is OK for current
3399 * uses, but may need to be revisited in future.
3400 *--------------------
3404 expression_tree_walker(Node *node,
3411 * The walker has already visited the current node, and so we need only
3412 * recurse into any sub-nodes it has.
3414 * We assume that the walker is not interested in List nodes per se, so
3415 * when we expect a List we just recurse directly to self without
3416 * bothering to call the walker.
3421 /* Guard against stack overflow due to overly complex expressions */
3422 check_stack_depth();
3424 switch (nodeTag(node))
3429 case T_CoerceToDomainValue:
3430 case T_CaseTestExpr:
3431 case T_SetToDefault:
3432 case T_CurrentOfExpr:
3434 case T_OuterJoinInfo:
3435 /* primitive node types with no expression subnodes */
3439 Aggref *expr = (Aggref *) node;
3441 /* recurse directly on List */
3442 if (expression_tree_walker((Node *) expr->args,
3449 ArrayRef *aref = (ArrayRef *) node;
3451 /* recurse directly for upper/lower array index lists */
3452 if (expression_tree_walker((Node *) aref->refupperindexpr,
3455 if (expression_tree_walker((Node *) aref->reflowerindexpr,
3458 /* walker must see the refexpr and refassgnexpr, however */
3459 if (walker(aref->refexpr, context))
3461 if (walker(aref->refassgnexpr, context))
3467 FuncExpr *expr = (FuncExpr *) node;
3469 if (expression_tree_walker((Node *) expr->args,
3476 OpExpr *expr = (OpExpr *) node;
3478 if (expression_tree_walker((Node *) expr->args,
3483 case T_DistinctExpr:
3485 DistinctExpr *expr = (DistinctExpr *) node;
3487 if (expression_tree_walker((Node *) expr->args,
3492 case T_ScalarArrayOpExpr:
3494 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3496 if (expression_tree_walker((Node *) expr->args,
3503 BoolExpr *expr = (BoolExpr *) node;
3505 if (expression_tree_walker((Node *) expr->args,
3512 SubLink *sublink = (SubLink *) node;
3514 if (walker(sublink->testexpr, context))
3518 * Also invoke the walker on the sublink's Query node, so it
3519 * can recurse into the sub-query if it wants to.
3521 return walker(sublink->subselect, context);
3526 SubPlan *subplan = (SubPlan *) node;
3528 /* recurse into the testexpr, but not into the Plan */
3529 if (walker(subplan->testexpr, context))
3531 /* also examine args list */
3532 if (expression_tree_walker((Node *) subplan->args,
3538 return walker(((FieldSelect *) node)->arg, context);
3541 FieldStore *fstore = (FieldStore *) node;
3543 if (walker(fstore->arg, context))
3545 if (walker(fstore->newvals, context))
3550 return walker(((RelabelType *) node)->arg, context);
3552 return walker(((CoerceViaIO *) node)->arg, context);
3553 case T_ArrayCoerceExpr:
3554 return walker(((ArrayCoerceExpr *) node)->arg, context);
3555 case T_ConvertRowtypeExpr:
3556 return walker(((ConvertRowtypeExpr *) node)->arg, context);
3559 CaseExpr *caseexpr = (CaseExpr *) node;
3561 if (walker(caseexpr->arg, context))
3563 /* we assume walker doesn't care about CaseWhens, either */
3564 foreach(temp, caseexpr->args)
3566 CaseWhen *when = (CaseWhen *) lfirst(temp);
3568 Assert(IsA(when, CaseWhen));
3569 if (walker(when->expr, context))
3571 if (walker(when->result, context))
3574 if (walker(caseexpr->defresult, context))
3579 return walker(((ArrayExpr *) node)->elements, context);
3581 return walker(((RowExpr *) node)->args, context);
3582 case T_RowCompareExpr:
3584 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3586 if (walker(rcexpr->largs, context))
3588 if (walker(rcexpr->rargs, context))
3592 case T_CoalesceExpr:
3593 return walker(((CoalesceExpr *) node)->args, context);
3595 return walker(((MinMaxExpr *) node)->args, context);
3598 XmlExpr *xexpr = (XmlExpr *) node;
3600 if (walker(xexpr->named_args, context))
3602 /* we assume walker doesn't care about arg_names */
3603 if (walker(xexpr->args, context))
3608 return walker(((NullIfExpr *) node)->args, context);
3610 return walker(((NullTest *) node)->arg, context);
3612 return walker(((BooleanTest *) node)->arg, context);
3613 case T_CoerceToDomain:
3614 return walker(((CoerceToDomain *) node)->arg, context);
3616 return walker(((TargetEntry *) node)->expr, context);
3618 /* Do nothing with a sub-Query, per discussion above */
3621 foreach(temp, (List *) node)
3623 if (walker((Node *) lfirst(temp), context))
3629 FromExpr *from = (FromExpr *) node;
3631 if (walker(from->fromlist, context))
3633 if (walker(from->quals, context))
3639 JoinExpr *join = (JoinExpr *) node;
3641 if (walker(join->larg, context))
3643 if (walker(join->rarg, context))
3645 if (walker(join->quals, context))
3649 * alias clause, using list are deemed uninteresting.
3653 case T_SetOperationStmt:
3655 SetOperationStmt *setop = (SetOperationStmt *) node;
3657 if (walker(setop->larg, context))
3659 if (walker(setop->rarg, context))
3663 case T_InClauseInfo:
3665 InClauseInfo *ininfo = (InClauseInfo *) node;
3667 if (expression_tree_walker((Node *) ininfo->sub_targetlist,
3672 case T_AppendRelInfo:
3674 AppendRelInfo *appinfo = (AppendRelInfo *) node;
3676 if (expression_tree_walker((Node *) appinfo->translated_vars,
3682 elog(ERROR, "unrecognized node type: %d",
3683 (int) nodeTag(node));
3690 * query_tree_walker --- initiate a walk of a Query's expressions
3692 * This routine exists just to reduce the number of places that need to know
3693 * where all the expression subtrees of a Query are. Note it can be used
3694 * for starting a walk at top level of a Query regardless of whether the
3695 * walker intends to descend into subqueries. It is also useful for
3696 * descending into subqueries within a walker.
3698 * Some callers want to suppress visitation of certain items in the sub-Query,
3699 * typically because they need to process them specially, or don't actually
3700 * want to recurse into subqueries. This is supported by the flags argument,
3701 * which is the bitwise OR of flag values to suppress visitation of
3702 * indicated items. (More flag bits may be added as needed.)
3705 query_tree_walker(Query *query,
3710 Assert(query != NULL && IsA(query, Query));
3712 if (walker((Node *) query->targetList, context))
3714 if (walker((Node *) query->returningList, context))
3716 if (walker((Node *) query->jointree, context))
3718 if (walker(query->setOperations, context))
3720 if (walker(query->havingQual, context))
3722 if (walker(query->limitOffset, context))
3724 if (walker(query->limitCount, context))
3726 if (range_table_walker(query->rtable, walker, context, flags))
3732 * range_table_walker is just the part of query_tree_walker that scans
3733 * a query's rangetable. This is split out since it can be useful on
3737 range_table_walker(List *rtable,
3746 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3748 switch (rte->rtekind)
3755 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3756 if (walker(rte->subquery, context))
3760 if (!(flags & QTW_IGNORE_JOINALIASES))
3761 if (walker(rte->joinaliasvars, context))
3765 if (walker(rte->funcexpr, context))
3769 if (walker(rte->values_lists, context))
3778 /*--------------------
3779 * expression_tree_mutator() is designed to support routines that make a
3780 * modified copy of an expression tree, with some nodes being added,
3781 * removed, or replaced by new subtrees. The original tree is (normally)
3782 * not changed. Each recursion level is responsible for returning a copy of
3783 * (or appropriately modified substitute for) the subtree it is handed.
3784 * A mutator routine should look like this:
3786 * Node * my_mutator (Node *node, my_struct *context)
3790 * // check for nodes that special work is required for, eg:
3791 * if (IsA(node, Var))
3793 * ... create and return modified copy of Var node
3795 * else if (IsA(node, ...))
3797 * ... do special transformations of other node types
3799 * // for any node type not specially processed, do:
3800 * return expression_tree_mutator(node, my_mutator, (void *) context);
3803 * The "context" argument points to a struct that holds whatever context
3804 * information the mutator routine needs --- it can be used to return extra
3805 * data gathered by the mutator, too. This argument is not touched by
3806 * expression_tree_mutator, but it is passed down to recursive sub-invocations
3807 * of my_mutator. The tree walk is started from a setup routine that
3808 * fills in the appropriate context struct, calls my_mutator with the
3809 * top-level node of the tree, and does any required post-processing.
3811 * Each level of recursion must return an appropriately modified Node.
3812 * If expression_tree_mutator() is called, it will make an exact copy
3813 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
3814 * of that Node. In this way, my_mutator() has full control over the
3815 * copying process but need not directly deal with expression trees
3816 * that it has no interest in.
3818 * Just as for expression_tree_walker, the node types handled by
3819 * expression_tree_mutator include all those normally found in target lists
3820 * and qualifier clauses during the planning stage.
3822 * expression_tree_mutator will handle SubLink nodes by recursing normally
3823 * into the "testexpr" subtree (which is an expression belonging to the outer
3824 * plan). It will also call the mutator on the sub-Query node; however, when
3825 * expression_tree_mutator itself is called on a Query node, it does nothing
3826 * and returns the unmodified Query node. The net effect is that unless the
3827 * mutator does something special at a Query node, sub-selects will not be
3828 * visited or modified; the original sub-select will be linked to by the new
3829 * SubLink node. Mutators that want to descend into sub-selects will usually
3830 * do so by recognizing Query nodes and calling query_tree_mutator (below).
3832 * expression_tree_mutator will handle a SubPlan node by recursing into the
3833 * "testexpr" and the "args" list (which belong to the outer plan), but it
3834 * will simply copy the link to the inner plan, since that's typically what
3835 * expression tree mutators want. A mutator that wants to modify the subplan
3836 * can force appropriate behavior by recognizing SubPlan expression nodes
3837 * and doing the right thing.
3838 *--------------------
3842 expression_tree_mutator(Node *node,
3843 Node *(*mutator) (),
3847 * The mutator has already decided not to modify the current node, but we
3848 * must call the mutator for any sub-nodes.
3851 #define FLATCOPY(newnode, node, nodetype) \
3852 ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
3853 memcpy((newnode), (node), sizeof(nodetype)) )
3855 #define CHECKFLATCOPY(newnode, node, nodetype) \
3856 ( AssertMacro(IsA((node), nodetype)), \
3857 (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
3858 memcpy((newnode), (node), sizeof(nodetype)) )
3860 #define MUTATE(newfield, oldfield, fieldtype) \
3861 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
3866 /* Guard against stack overflow due to overly complex expressions */
3867 check_stack_depth();
3869 switch (nodeTag(node))
3872 * Primitive node types with no expression subnodes. Var and
3873 * Const are frequent enough to deserve special cases, the others
3874 * we just use copyObject for.
3878 Var *var = (Var *) node;
3881 FLATCOPY(newnode, var, Var);
3882 return (Node *) newnode;
3887 Const *oldnode = (Const *) node;
3890 FLATCOPY(newnode, oldnode, Const);
3891 /* XXX we don't bother with datumCopy; should we? */
3892 return (Node *) newnode;
3896 case T_CoerceToDomainValue:
3897 case T_CaseTestExpr:
3898 case T_SetToDefault:
3899 case T_CurrentOfExpr:
3901 case T_OuterJoinInfo:
3902 return (Node *) copyObject(node);
3905 Aggref *aggref = (Aggref *) node;
3908 FLATCOPY(newnode, aggref, Aggref);
3909 MUTATE(newnode->args, aggref->args, List *);
3910 return (Node *) newnode;
3915 ArrayRef *arrayref = (ArrayRef *) node;
3918 FLATCOPY(newnode, arrayref, ArrayRef);
3919 MUTATE(newnode->refupperindexpr, arrayref->refupperindexpr,
3921 MUTATE(newnode->reflowerindexpr, arrayref->reflowerindexpr,
3923 MUTATE(newnode->refexpr, arrayref->refexpr,
3925 MUTATE(newnode->refassgnexpr, arrayref->refassgnexpr,
3927 return (Node *) newnode;
3932 FuncExpr *expr = (FuncExpr *) node;
3935 FLATCOPY(newnode, expr, FuncExpr);
3936 MUTATE(newnode->args, expr->args, List *);
3937 return (Node *) newnode;
3942 OpExpr *expr = (OpExpr *) node;
3945 FLATCOPY(newnode, expr, OpExpr);
3946 MUTATE(newnode->args, expr->args, List *);
3947 return (Node *) newnode;
3950 case T_DistinctExpr:
3952 DistinctExpr *expr = (DistinctExpr *) node;
3953 DistinctExpr *newnode;
3955 FLATCOPY(newnode, expr, DistinctExpr);
3956 MUTATE(newnode->args, expr->args, List *);
3957 return (Node *) newnode;
3960 case T_ScalarArrayOpExpr:
3962 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3963 ScalarArrayOpExpr *newnode;
3965 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
3966 MUTATE(newnode->args, expr->args, List *);
3967 return (Node *) newnode;
3972 BoolExpr *expr = (BoolExpr *) node;
3975 FLATCOPY(newnode, expr, BoolExpr);
3976 MUTATE(newnode->args, expr->args, List *);
3977 return (Node *) newnode;
3982 SubLink *sublink = (SubLink *) node;
3985 FLATCOPY(newnode, sublink, SubLink);
3986 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
3989 * Also invoke the mutator on the sublink's Query node, so it
3990 * can recurse into the sub-query if it wants to.
3992 MUTATE(newnode->subselect, sublink->subselect, Node *);
3993 return (Node *) newnode;
3998 SubPlan *subplan = (SubPlan *) node;
4001 FLATCOPY(newnode, subplan, SubPlan);
4002 /* transform testexpr */
4003 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
4004 /* transform args list (params to be passed to subplan) */
4005 MUTATE(newnode->args, subplan->args, List *);
4006 /* but not the sub-Plan itself, which is referenced as-is */
4007 return (Node *) newnode;
4012 FieldSelect *fselect = (FieldSelect *) node;
4013 FieldSelect *newnode;
4015 FLATCOPY(newnode, fselect, FieldSelect);
4016 MUTATE(newnode->arg, fselect->arg, Expr *);
4017 return (Node *) newnode;
4022 FieldStore *fstore = (FieldStore *) node;
4023 FieldStore *newnode;
4025 FLATCOPY(newnode, fstore, FieldStore);
4026 MUTATE(newnode->arg, fstore->arg, Expr *);
4027 MUTATE(newnode->newvals, fstore->newvals, List *);
4028 newnode->fieldnums = list_copy(fstore->fieldnums);
4029 return (Node *) newnode;
4034 RelabelType *relabel = (RelabelType *) node;
4035 RelabelType *newnode;
4037 FLATCOPY(newnode, relabel, RelabelType);
4038 MUTATE(newnode->arg, relabel->arg, Expr *);
4039 return (Node *) newnode;
4044 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
4045 CoerceViaIO *newnode;
4047 FLATCOPY(newnode, iocoerce, CoerceViaIO);
4048 MUTATE(newnode->arg, iocoerce->arg, Expr *);
4049 return (Node *) newnode;
4052 case T_ArrayCoerceExpr:
4054 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
4055 ArrayCoerceExpr *newnode;
4057 FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
4058 MUTATE(newnode->arg, acoerce->arg, Expr *);
4059 return (Node *) newnode;
4062 case T_ConvertRowtypeExpr:
4064 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
4065 ConvertRowtypeExpr *newnode;
4067 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
4068 MUTATE(newnode->arg, convexpr->arg, Expr *);
4069 return (Node *) newnode;
4074 CaseExpr *caseexpr = (CaseExpr *) node;
4077 FLATCOPY(newnode, caseexpr, CaseExpr);
4078 MUTATE(newnode->arg, caseexpr->arg, Expr *);
4079 MUTATE(newnode->args, caseexpr->args, List *);
4080 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
4081 return (Node *) newnode;
4086 CaseWhen *casewhen = (CaseWhen *) node;
4089 FLATCOPY(newnode, casewhen, CaseWhen);
4090 MUTATE(newnode->expr, casewhen->expr, Expr *);
4091 MUTATE(newnode->result, casewhen->result, Expr *);
4092 return (Node *) newnode;
4097 ArrayExpr *arrayexpr = (ArrayExpr *) node;
4100 FLATCOPY(newnode, arrayexpr, ArrayExpr);
4101 MUTATE(newnode->elements, arrayexpr->elements, List *);
4102 return (Node *) newnode;
4107 RowExpr *rowexpr = (RowExpr *) node;
4110 FLATCOPY(newnode, rowexpr, RowExpr);
4111 MUTATE(newnode->args, rowexpr->args, List *);
4112 return (Node *) newnode;
4115 case T_RowCompareExpr:
4117 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
4118 RowCompareExpr *newnode;
4120 FLATCOPY(newnode, rcexpr, RowCompareExpr);
4121 MUTATE(newnode->largs, rcexpr->largs, List *);
4122 MUTATE(newnode->rargs, rcexpr->rargs, List *);
4123 return (Node *) newnode;
4126 case T_CoalesceExpr:
4128 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
4129 CoalesceExpr *newnode;
4131 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
4132 MUTATE(newnode->args, coalesceexpr->args, List *);
4133 return (Node *) newnode;
4138 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
4139 MinMaxExpr *newnode;
4141 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
4142 MUTATE(newnode->args, minmaxexpr->args, List *);
4143 return (Node *) newnode;
4148 XmlExpr *xexpr = (XmlExpr *) node;
4151 FLATCOPY(newnode, xexpr, XmlExpr);
4152 MUTATE(newnode->named_args, xexpr->named_args, List *);
4153 /* assume mutator does not care about arg_names */
4154 MUTATE(newnode->args, xexpr->args, List *);
4155 return (Node *) newnode;
4160 NullIfExpr *expr = (NullIfExpr *) node;
4161 NullIfExpr *newnode;
4163 FLATCOPY(newnode, expr, NullIfExpr);
4164 MUTATE(newnode->args, expr->args, List *);
4165 return (Node *) newnode;
4170 NullTest *ntest = (NullTest *) node;
4173 FLATCOPY(newnode, ntest, NullTest);
4174 MUTATE(newnode->arg, ntest->arg, Expr *);
4175 return (Node *) newnode;
4180 BooleanTest *btest = (BooleanTest *) node;
4181 BooleanTest *newnode;
4183 FLATCOPY(newnode, btest, BooleanTest);
4184 MUTATE(newnode->arg, btest->arg, Expr *);
4185 return (Node *) newnode;
4188 case T_CoerceToDomain:
4190 CoerceToDomain *ctest = (CoerceToDomain *) node;
4191 CoerceToDomain *newnode;
4193 FLATCOPY(newnode, ctest, CoerceToDomain);
4194 MUTATE(newnode->arg, ctest->arg, Expr *);
4195 return (Node *) newnode;
4200 TargetEntry *targetentry = (TargetEntry *) node;
4201 TargetEntry *newnode;
4203 FLATCOPY(newnode, targetentry, TargetEntry);
4204 MUTATE(newnode->expr, targetentry->expr, Expr *);
4205 return (Node *) newnode;
4209 /* Do nothing with a sub-Query, per discussion above */
4214 * We assume the mutator isn't interested in the list nodes
4215 * per se, so just invoke it on each list element. NOTE: this
4216 * would fail badly on a list with integer elements!
4222 foreach(temp, (List *) node)
4224 resultlist = lappend(resultlist,
4225 mutator((Node *) lfirst(temp),
4228 return (Node *) resultlist;
4233 FromExpr *from = (FromExpr *) node;
4236 FLATCOPY(newnode, from, FromExpr);
4237 MUTATE(newnode->fromlist, from->fromlist, List *);
4238 MUTATE(newnode->quals, from->quals, Node *);
4239 return (Node *) newnode;
4244 JoinExpr *join = (JoinExpr *) node;
4247 FLATCOPY(newnode, join, JoinExpr);
4248 MUTATE(newnode->larg, join->larg, Node *);
4249 MUTATE(newnode->rarg, join->rarg, Node *);
4250 MUTATE(newnode->quals, join->quals, Node *);
4251 /* We do not mutate alias or using by default */
4252 return (Node *) newnode;
4255 case T_SetOperationStmt:
4257 SetOperationStmt *setop = (SetOperationStmt *) node;
4258 SetOperationStmt *newnode;
4260 FLATCOPY(newnode, setop, SetOperationStmt);
4261 MUTATE(newnode->larg, setop->larg, Node *);
4262 MUTATE(newnode->rarg, setop->rarg, Node *);
4263 return (Node *) newnode;
4266 case T_InClauseInfo:
4268 InClauseInfo *ininfo = (InClauseInfo *) node;
4269 InClauseInfo *newnode;
4271 FLATCOPY(newnode, ininfo, InClauseInfo);
4272 MUTATE(newnode->sub_targetlist, ininfo->sub_targetlist, List *);
4273 /* Assume we need not make a copy of in_operators list */
4274 return (Node *) newnode;
4277 case T_AppendRelInfo:
4279 AppendRelInfo *appinfo = (AppendRelInfo *) node;
4280 AppendRelInfo *newnode;
4282 FLATCOPY(newnode, appinfo, AppendRelInfo);
4283 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
4284 return (Node *) newnode;
4288 elog(ERROR, "unrecognized node type: %d",
4289 (int) nodeTag(node));
4292 /* can't get here, but keep compiler happy */
4298 * query_tree_mutator --- initiate modification of a Query's expressions
4300 * This routine exists just to reduce the number of places that need to know
4301 * where all the expression subtrees of a Query are. Note it can be used
4302 * for starting a walk at top level of a Query regardless of whether the
4303 * mutator intends to descend into subqueries. It is also useful for
4304 * descending into subqueries within a mutator.
4306 * Some callers want to suppress mutating of certain items in the Query,
4307 * typically because they need to process them specially, or don't actually
4308 * want to recurse into subqueries. This is supported by the flags argument,
4309 * which is the bitwise OR of flag values to suppress mutating of
4310 * indicated items. (More flag bits may be added as needed.)
4312 * Normally the Query node itself is copied, but some callers want it to be
4313 * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags. All
4314 * modified substructure is safely copied in any case.
4317 query_tree_mutator(Query *query,
4318 Node *(*mutator) (),
4322 Assert(query != NULL && IsA(query, Query));
4324 if (!(flags & QTW_DONT_COPY_QUERY))
4328 FLATCOPY(newquery, query, Query);
4332 MUTATE(query->targetList, query->targetList, List *);
4333 MUTATE(query->returningList, query->returningList, List *);
4334 MUTATE(query->jointree, query->jointree, FromExpr *);
4335 MUTATE(query->setOperations, query->setOperations, Node *);
4336 MUTATE(query->havingQual, query->havingQual, Node *);
4337 MUTATE(query->limitOffset, query->limitOffset, Node *);
4338 MUTATE(query->limitCount, query->limitCount, Node *);
4339 query->rtable = range_table_mutator(query->rtable,
4340 mutator, context, flags);
4345 * range_table_mutator is just the part of query_tree_mutator that processes
4346 * a query's rangetable. This is split out since it can be useful on
4350 range_table_mutator(List *rtable,
4351 Node *(*mutator) (),
4360 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
4361 RangeTblEntry *newrte;
4363 FLATCOPY(newrte, rte, RangeTblEntry);
4364 switch (rte->rtekind)
4368 /* we don't bother to copy eref, aliases, etc; OK? */
4371 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
4373 CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
4374 MUTATE(newrte->subquery, newrte->subquery, Query *);
4378 /* else, copy RT subqueries as-is */
4379 newrte->subquery = copyObject(rte->subquery);
4383 if (!(flags & QTW_IGNORE_JOINALIASES))
4384 MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
4387 /* else, copy join aliases as-is */
4388 newrte->joinaliasvars = copyObject(rte->joinaliasvars);
4392 MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
4395 MUTATE(newrte->values_lists, rte->values_lists, List *);
4398 newrt = lappend(newrt, newrte);
4404 * query_or_expression_tree_walker --- hybrid form
4406 * This routine will invoke query_tree_walker if called on a Query node,
4407 * else will invoke the walker directly. This is a useful way of starting
4408 * the recursion when the walker's normal change of state is not appropriate
4409 * for the outermost Query node.
4412 query_or_expression_tree_walker(Node *node,
4417 if (node && IsA(node, Query))
4418 return query_tree_walker((Query *) node,
4423 return walker(node, context);
4427 * query_or_expression_tree_mutator --- hybrid form
4429 * This routine will invoke query_tree_mutator if called on a Query node,
4430 * else will invoke the mutator directly. This is a useful way of starting
4431 * the recursion when the mutator's normal change of state is not appropriate
4432 * for the outermost Query node.
4435 query_or_expression_tree_mutator(Node *node,
4436 Node *(*mutator) (),
4440 if (node && IsA(node, Query))
4441 return (Node *) query_tree_mutator((Query *) node,
4446 return mutator(node, context);