Get rid of the rather fuzzily defined FlattenedSubLink node type in favor of
[PostgreSQL.git] / src / backend / optimizer / util / clauses.c
blob4e08912344acf1e52918b53791d58b2320888eb4
1 /*-------------------------------------------------------------------------
3 * clauses.c
4 * routines to manipulate qualification clauses
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 * HISTORY
14 * AUTHOR DATE MAJOR EVENT
15 * Andrew Yu Nov 3, 1994 clause.c and clauses.c combined
17 *-------------------------------------------------------------------------
20 #include "postgres.h"
22 #include "catalog/pg_aggregate.h"
23 #include "catalog/pg_language.h"
24 #include "catalog/pg_operator.h"
25 #include "catalog/pg_proc.h"
26 #include "catalog/pg_type.h"
27 #include "executor/executor.h"
28 #include "executor/functions.h"
29 #include "miscadmin.h"
30 #include "nodes/makefuncs.h"
31 #include "nodes/nodeFuncs.h"
32 #include "optimizer/clauses.h"
33 #include "optimizer/cost.h"
34 #include "optimizer/planmain.h"
35 #include "optimizer/prep.h"
36 #include "optimizer/var.h"
37 #include "parser/analyze.h"
38 #include "parser/parse_coerce.h"
39 #include "parser/parse_func.h"
40 #include "rewrite/rewriteManip.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"
51 typedef struct
53 ParamListInfo boundParams;
54 PlannerGlobal *glob;
55 List *active_fns;
56 Node *case_val;
57 bool estimate;
58 } eval_const_expressions_context;
60 typedef struct
62 int nargs;
63 List *args;
64 int *usecounts;
65 } substitute_actual_parameters_context;
67 typedef struct
69 int nargs;
70 List *args;
71 int sublevels_up;
72 } substitute_actual_srf_parameters_context;
74 static bool contain_agg_clause_walker(Node *node, void *context);
75 static bool pull_agg_clause_walker(Node *node, List **context);
76 static bool count_agg_clauses_walker(Node *node, AggClauseCounts *counts);
77 static bool find_window_functions_walker(Node *node, WindowFuncLists *lists);
78 static bool expression_returns_set_rows_walker(Node *node, double *count);
79 static bool contain_subplans_walker(Node *node, void *context);
80 static bool contain_mutable_functions_walker(Node *node, void *context);
81 static bool contain_volatile_functions_walker(Node *node, void *context);
82 static bool contain_nonstrict_functions_walker(Node *node, void *context);
83 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
84 static List *find_nonnullable_vars_walker(Node *node, bool top_level);
85 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
86 static bool set_coercionform_dontcare_walker(Node *node, void *context);
87 static Node *eval_const_expressions_mutator(Node *node,
88 eval_const_expressions_context *context);
89 static List *simplify_or_arguments(List *args,
90 eval_const_expressions_context *context,
91 bool *haveNull, bool *forceTrue);
92 static List *simplify_and_arguments(List *args,
93 eval_const_expressions_context *context,
94 bool *haveNull, bool *forceFalse);
95 static Expr *simplify_boolean_equality(List *args);
96 static Expr *simplify_function(Oid funcid,
97 Oid result_type, int32 result_typmod, List **args,
98 bool allow_inline,
99 eval_const_expressions_context *context);
100 static List *add_function_defaults(List *args, Oid result_type,
101 HeapTuple func_tuple,
102 eval_const_expressions_context *context);
103 static Expr *evaluate_function(Oid funcid,
104 Oid result_type, int32 result_typmod, List *args,
105 HeapTuple func_tuple,
106 eval_const_expressions_context *context);
107 static Expr *inline_function(Oid funcid, Oid result_type, List *args,
108 HeapTuple func_tuple,
109 eval_const_expressions_context *context);
110 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
111 int *usecounts);
112 static Node *substitute_actual_parameters_mutator(Node *node,
113 substitute_actual_parameters_context *context);
114 static void sql_inline_error_callback(void *arg);
115 static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod);
116 static Query *substitute_actual_srf_parameters(Query *expr,
117 int nargs, List *args);
118 static Node *substitute_actual_srf_parameters_mutator(Node *node,
119 substitute_actual_srf_parameters_context *context);
120 static bool tlist_matches_coltypelist(List *tlist, List *coltypelist);
123 /*****************************************************************************
124 * OPERATOR clause functions
125 *****************************************************************************/
128 * make_opclause
129 * Creates an operator clause given its operator info, left operand,
130 * and right operand (pass NULL to create single-operand clause).
132 Expr *
133 make_opclause(Oid opno, Oid opresulttype, bool opretset,
134 Expr *leftop, Expr *rightop)
136 OpExpr *expr = makeNode(OpExpr);
138 expr->opno = opno;
139 expr->opfuncid = InvalidOid;
140 expr->opresulttype = opresulttype;
141 expr->opretset = opretset;
142 if (rightop)
143 expr->args = list_make2(leftop, rightop);
144 else
145 expr->args = list_make1(leftop);
146 expr->location = -1;
147 return (Expr *) expr;
151 * get_leftop
153 * Returns the left operand of a clause of the form (op expr expr)
154 * or (op expr)
156 Node *
157 get_leftop(Expr *clause)
159 OpExpr *expr = (OpExpr *) clause;
161 if (expr->args != NIL)
162 return linitial(expr->args);
163 else
164 return NULL;
168 * get_rightop
170 * Returns the right operand in a clause of the form (op expr expr).
171 * NB: result will be NULL if applied to a unary op clause.
173 Node *
174 get_rightop(Expr *clause)
176 OpExpr *expr = (OpExpr *) clause;
178 if (list_length(expr->args) >= 2)
179 return lsecond(expr->args);
180 else
181 return NULL;
184 /*****************************************************************************
185 * NOT clause functions
186 *****************************************************************************/
189 * not_clause
191 * Returns t iff this is a 'not' clause: (NOT expr).
193 bool
194 not_clause(Node *clause)
196 return (clause != NULL &&
197 IsA(clause, BoolExpr) &&
198 ((BoolExpr *) clause)->boolop == NOT_EXPR);
202 * make_notclause
204 * Create a 'not' clause given the expression to be negated.
206 Expr *
207 make_notclause(Expr *notclause)
209 BoolExpr *expr = makeNode(BoolExpr);
211 expr->boolop = NOT_EXPR;
212 expr->args = list_make1(notclause);
213 expr->location = -1;
214 return (Expr *) expr;
218 * get_notclausearg
220 * Retrieve the clause within a 'not' clause
222 Expr *
223 get_notclausearg(Expr *notclause)
225 return linitial(((BoolExpr *) notclause)->args);
228 /*****************************************************************************
229 * OR clause functions
230 *****************************************************************************/
233 * or_clause
235 * Returns t iff the clause is an 'or' clause: (OR { expr }).
237 bool
238 or_clause(Node *clause)
240 return (clause != NULL &&
241 IsA(clause, BoolExpr) &&
242 ((BoolExpr *) clause)->boolop == OR_EXPR);
246 * make_orclause
248 * Creates an 'or' clause given a list of its subclauses.
250 Expr *
251 make_orclause(List *orclauses)
253 BoolExpr *expr = makeNode(BoolExpr);
255 expr->boolop = OR_EXPR;
256 expr->args = orclauses;
257 expr->location = -1;
258 return (Expr *) expr;
261 /*****************************************************************************
262 * AND clause functions
263 *****************************************************************************/
267 * and_clause
269 * Returns t iff its argument is an 'and' clause: (AND { expr }).
271 bool
272 and_clause(Node *clause)
274 return (clause != NULL &&
275 IsA(clause, BoolExpr) &&
276 ((BoolExpr *) clause)->boolop == AND_EXPR);
280 * make_andclause
282 * Creates an 'and' clause given a list of its subclauses.
284 Expr *
285 make_andclause(List *andclauses)
287 BoolExpr *expr = makeNode(BoolExpr);
289 expr->boolop = AND_EXPR;
290 expr->args = andclauses;
291 expr->location = -1;
292 return (Expr *) expr;
296 * make_and_qual
298 * Variant of make_andclause for ANDing two qual conditions together.
299 * Qual conditions have the property that a NULL nodetree is interpreted
300 * as 'true'.
302 * NB: this makes no attempt to preserve AND/OR flatness; so it should not
303 * be used on a qual that has already been run through prepqual.c.
305 Node *
306 make_and_qual(Node *qual1, Node *qual2)
308 if (qual1 == NULL)
309 return qual2;
310 if (qual2 == NULL)
311 return qual1;
312 return (Node *) make_andclause(list_make2(qual1, qual2));
316 * Sometimes (such as in the input of ExecQual), we use lists of expression
317 * nodes with implicit AND semantics.
319 * These functions convert between an AND-semantics expression list and the
320 * ordinary representation of a boolean expression.
322 * Note that an empty list is considered equivalent to TRUE.
324 Expr *
325 make_ands_explicit(List *andclauses)
327 if (andclauses == NIL)
328 return (Expr *) makeBoolConst(true, false);
329 else if (list_length(andclauses) == 1)
330 return (Expr *) linitial(andclauses);
331 else
332 return make_andclause(andclauses);
335 List *
336 make_ands_implicit(Expr *clause)
339 * NB: because the parser sets the qual field to NULL in a query that has
340 * no WHERE clause, we must consider a NULL input clause as TRUE, even
341 * though one might more reasonably think it FALSE. Grumble. If this
342 * causes trouble, consider changing the parser's behavior.
344 if (clause == NULL)
345 return NIL; /* NULL -> NIL list == TRUE */
346 else if (and_clause((Node *) clause))
347 return ((BoolExpr *) clause)->args;
348 else if (IsA(clause, Const) &&
349 !((Const *) clause)->constisnull &&
350 DatumGetBool(((Const *) clause)->constvalue))
351 return NIL; /* constant TRUE input -> NIL list */
352 else
353 return list_make1(clause);
357 /*****************************************************************************
358 * Aggregate-function clause manipulation
359 *****************************************************************************/
362 * contain_agg_clause
363 * Recursively search for Aggref nodes within a clause.
365 * Returns true if any aggregate found.
367 * This does not descend into subqueries, and so should be used only after
368 * reduction of sublinks to subplans, or in contexts where it's known there
369 * are no subqueries. There mustn't be outer-aggregate references either.
371 * (If you want something like this but able to deal with subqueries,
372 * see rewriteManip.c's contain_aggs_of_level().)
374 bool
375 contain_agg_clause(Node *clause)
377 return contain_agg_clause_walker(clause, NULL);
380 static bool
381 contain_agg_clause_walker(Node *node, void *context)
383 if (node == NULL)
384 return false;
385 if (IsA(node, Aggref))
387 Assert(((Aggref *) node)->agglevelsup == 0);
388 return true; /* abort the tree traversal and return true */
390 Assert(!IsA(node, SubLink));
391 return expression_tree_walker(node, contain_agg_clause_walker, context);
395 * pull_agg_clause
396 * Recursively search for Aggref nodes within a clause.
398 * Returns a List of all Aggrefs found.
400 * This does not descend into subqueries, and so should be used only after
401 * reduction of sublinks to subplans, or in contexts where it's known there
402 * are no subqueries. There mustn't be outer-aggregate references either.
404 List *
405 pull_agg_clause(Node *clause)
407 List *result = NIL;
409 (void) pull_agg_clause_walker(clause, &result);
410 return result;
413 static bool
414 pull_agg_clause_walker(Node *node, List **context)
416 if (node == NULL)
417 return false;
418 if (IsA(node, Aggref))
420 Assert(((Aggref *) node)->agglevelsup == 0);
421 *context = lappend(*context, node);
422 return false; /* no need to descend into arguments */
424 Assert(!IsA(node, SubLink));
425 return expression_tree_walker(node, pull_agg_clause_walker,
426 (void *) context);
430 * count_agg_clauses
431 * Recursively count the Aggref nodes in an expression tree.
433 * Note: this also checks for nested aggregates, which are an error.
435 * We not only count the nodes, but attempt to estimate the total space
436 * needed for their transition state values if all are evaluated in parallel
437 * (as would be done in a HashAgg plan). See AggClauseCounts for the exact
438 * set of statistics returned.
440 * NOTE that the counts are ADDED to those already in *counts ... so the
441 * caller is responsible for zeroing the struct initially.
443 * This does not descend into subqueries, and so should be used only after
444 * reduction of sublinks to subplans, or in contexts where it's known there
445 * are no subqueries. There mustn't be outer-aggregate references either.
447 void
448 count_agg_clauses(Node *clause, AggClauseCounts *counts)
450 /* no setup needed */
451 count_agg_clauses_walker(clause, counts);
454 static bool
455 count_agg_clauses_walker(Node *node, AggClauseCounts *counts)
457 if (node == NULL)
458 return false;
459 if (IsA(node, Aggref))
461 Aggref *aggref = (Aggref *) node;
462 Oid *inputTypes;
463 int numArguments;
464 HeapTuple aggTuple;
465 Form_pg_aggregate aggform;
466 Oid aggtranstype;
467 int i;
468 ListCell *l;
470 Assert(aggref->agglevelsup == 0);
471 counts->numAggs++;
472 if (aggref->aggdistinct)
473 counts->numDistinctAggs++;
475 /* extract argument types */
476 numArguments = list_length(aggref->args);
477 inputTypes = (Oid *) palloc(sizeof(Oid) * numArguments);
478 i = 0;
479 foreach(l, aggref->args)
481 inputTypes[i++] = exprType((Node *) lfirst(l));
484 /* fetch aggregate transition datatype from pg_aggregate */
485 aggTuple = SearchSysCache(AGGFNOID,
486 ObjectIdGetDatum(aggref->aggfnoid),
487 0, 0, 0);
488 if (!HeapTupleIsValid(aggTuple))
489 elog(ERROR, "cache lookup failed for aggregate %u",
490 aggref->aggfnoid);
491 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
492 aggtranstype = aggform->aggtranstype;
493 ReleaseSysCache(aggTuple);
495 /* resolve actual type of transition state, if polymorphic */
496 if (IsPolymorphicType(aggtranstype))
498 /* have to fetch the agg's declared input types... */
499 Oid *declaredArgTypes;
500 int agg_nargs;
502 (void) get_func_signature(aggref->aggfnoid,
503 &declaredArgTypes, &agg_nargs);
504 Assert(agg_nargs == numArguments);
505 aggtranstype = enforce_generic_type_consistency(inputTypes,
506 declaredArgTypes,
507 agg_nargs,
508 aggtranstype,
509 false);
510 pfree(declaredArgTypes);
514 * If the transition type is pass-by-value then it doesn't add
515 * anything to the required size of the hashtable. If it is
516 * pass-by-reference then we have to add the estimated size of the
517 * value itself, plus palloc overhead.
519 if (!get_typbyval(aggtranstype))
521 int32 aggtranstypmod;
522 int32 avgwidth;
525 * If transition state is of same type as first input, assume it's
526 * the same typmod (same width) as well. This works for cases
527 * like MAX/MIN and is probably somewhat reasonable otherwise.
529 if (numArguments > 0 && aggtranstype == inputTypes[0])
530 aggtranstypmod = exprTypmod((Node *) linitial(aggref->args));
531 else
532 aggtranstypmod = -1;
534 avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
535 avgwidth = MAXALIGN(avgwidth);
537 counts->transitionSpace += avgwidth + 2 * sizeof(void *);
541 * Complain if the aggregate's arguments contain any aggregates;
542 * nested agg functions are semantically nonsensical.
544 if (contain_agg_clause((Node *) aggref->args))
545 ereport(ERROR,
546 (errcode(ERRCODE_GROUPING_ERROR),
547 errmsg("aggregate function calls cannot be nested")));
550 * Having checked that, we need not recurse into the argument.
552 return false;
554 Assert(!IsA(node, SubLink));
555 return expression_tree_walker(node, count_agg_clauses_walker,
556 (void *) counts);
560 /*****************************************************************************
561 * Window-function clause manipulation
562 *****************************************************************************/
565 * contain_window_function
566 * Recursively search for WindowFunc nodes within a clause.
568 * Since window functions don't have level fields, but are hard-wired to
569 * be associated with the current query level, this is just the same as
570 * rewriteManip.c's function.
572 bool
573 contain_window_function(Node *clause)
575 return checkExprHasWindowFuncs(clause);
579 * find_window_functions
580 * Locate all the WindowFunc nodes in an expression tree, and organize
581 * them by winref ID number.
583 * Caller must provide an upper bound on the winref IDs expected in the tree.
585 WindowFuncLists *
586 find_window_functions(Node *clause, Index maxWinRef)
588 WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
590 lists->numWindowFuncs = 0;
591 lists->maxWinRef = maxWinRef;
592 lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
593 (void) find_window_functions_walker(clause, lists);
594 return lists;
597 static bool
598 find_window_functions_walker(Node *node, WindowFuncLists *lists)
600 if (node == NULL)
601 return false;
602 if (IsA(node, WindowFunc))
604 WindowFunc *wfunc = (WindowFunc *) node;
606 /* winref is unsigned, so one-sided test is OK */
607 if (wfunc->winref > lists->maxWinRef)
608 elog(ERROR, "WindowFunc contains out-of-range winref %u",
609 wfunc->winref);
610 lists->windowFuncs[wfunc->winref] =
611 lappend(lists->windowFuncs[wfunc->winref], wfunc);
612 lists->numWindowFuncs++;
615 * Complain if the window function's arguments contain window functions
617 if (contain_window_function((Node *) wfunc->args))
618 ereport(ERROR,
619 (errcode(ERRCODE_WINDOWING_ERROR),
620 errmsg("window function calls cannot be nested")));
623 * Having checked that, we need not recurse into the argument.
625 return false;
627 Assert(!IsA(node, SubLink));
628 return expression_tree_walker(node, find_window_functions_walker,
629 (void *) lists);
633 /*****************************************************************************
634 * Support for expressions returning sets
635 *****************************************************************************/
638 * expression_returns_set_rows
639 * Estimate the number of rows in a set result.
641 * We use the product of the rowcount estimates of all the functions in
642 * the given tree. The result is 1 if there are no set-returning functions.
644 * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
646 double
647 expression_returns_set_rows(Node *clause)
649 double result = 1;
651 (void) expression_returns_set_rows_walker(clause, &result);
652 return result;
655 static bool
656 expression_returns_set_rows_walker(Node *node, double *count)
658 if (node == NULL)
659 return false;
660 if (IsA(node, FuncExpr))
662 FuncExpr *expr = (FuncExpr *) node;
664 if (expr->funcretset)
665 *count *= get_func_rows(expr->funcid);
667 if (IsA(node, OpExpr))
669 OpExpr *expr = (OpExpr *) node;
671 if (expr->opretset)
673 set_opfuncid(expr);
674 *count *= get_func_rows(expr->opfuncid);
678 /* Avoid recursion for some cases that can't return a set */
679 if (IsA(node, Aggref))
680 return false;
681 if (IsA(node, WindowFunc))
682 return false;
683 if (IsA(node, DistinctExpr))
684 return false;
685 if (IsA(node, ScalarArrayOpExpr))
686 return false;
687 if (IsA(node, BoolExpr))
688 return false;
689 if (IsA(node, SubLink))
690 return false;
691 if (IsA(node, SubPlan))
692 return false;
693 if (IsA(node, AlternativeSubPlan))
694 return false;
695 if (IsA(node, ArrayExpr))
696 return false;
697 if (IsA(node, RowExpr))
698 return false;
699 if (IsA(node, RowCompareExpr))
700 return false;
701 if (IsA(node, CoalesceExpr))
702 return false;
703 if (IsA(node, MinMaxExpr))
704 return false;
705 if (IsA(node, XmlExpr))
706 return false;
707 if (IsA(node, NullIfExpr))
708 return false;
710 return expression_tree_walker(node, expression_returns_set_rows_walker,
711 (void *) count);
715 /*****************************************************************************
716 * Subplan clause manipulation
717 *****************************************************************************/
720 * contain_subplans
721 * Recursively search for subplan nodes within a clause.
723 * If we see a SubLink node, we will return TRUE. This is only possible if
724 * the expression tree hasn't yet been transformed by subselect.c. We do not
725 * know whether the node will produce a true subplan or just an initplan,
726 * but we make the conservative assumption that it will be a subplan.
728 * Returns true if any subplan found.
730 bool
731 contain_subplans(Node *clause)
733 return contain_subplans_walker(clause, NULL);
736 static bool
737 contain_subplans_walker(Node *node, void *context)
739 if (node == NULL)
740 return false;
741 if (IsA(node, SubPlan) ||
742 IsA(node, AlternativeSubPlan) ||
743 IsA(node, SubLink))
744 return true; /* abort the tree traversal and return true */
745 return expression_tree_walker(node, contain_subplans_walker, context);
749 /*****************************************************************************
750 * Check clauses for mutable functions
751 *****************************************************************************/
754 * contain_mutable_functions
755 * Recursively search for mutable functions within a clause.
757 * Returns true if any mutable function (or operator implemented by a
758 * mutable function) is found. This test is needed so that we don't
759 * mistakenly think that something like "WHERE random() < 0.5" can be treated
760 * as a constant qualification.
762 * XXX we do not examine sub-selects to see if they contain uses of
763 * mutable functions. It's not real clear if that is correct or not...
765 bool
766 contain_mutable_functions(Node *clause)
768 return contain_mutable_functions_walker(clause, NULL);
771 static bool
772 contain_mutable_functions_walker(Node *node, void *context)
774 if (node == NULL)
775 return false;
776 if (IsA(node, FuncExpr))
778 FuncExpr *expr = (FuncExpr *) node;
780 if (func_volatile(expr->funcid) != PROVOLATILE_IMMUTABLE)
781 return true;
782 /* else fall through to check args */
784 else if (IsA(node, OpExpr))
786 OpExpr *expr = (OpExpr *) node;
788 set_opfuncid(expr);
789 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
790 return true;
791 /* else fall through to check args */
793 else if (IsA(node, DistinctExpr))
795 DistinctExpr *expr = (DistinctExpr *) node;
797 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
798 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
799 return true;
800 /* else fall through to check args */
802 else if (IsA(node, ScalarArrayOpExpr))
804 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
806 set_sa_opfuncid(expr);
807 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
808 return true;
809 /* else fall through to check args */
811 else if (IsA(node, CoerceViaIO))
813 CoerceViaIO *expr = (CoerceViaIO *) node;
814 Oid iofunc;
815 Oid typioparam;
816 bool typisvarlena;
818 /* check the result type's input function */
819 getTypeInputInfo(expr->resulttype,
820 &iofunc, &typioparam);
821 if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
822 return true;
823 /* check the input type's output function */
824 getTypeOutputInfo(exprType((Node *) expr->arg),
825 &iofunc, &typisvarlena);
826 if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
827 return true;
828 /* else fall through to check args */
830 else if (IsA(node, ArrayCoerceExpr))
832 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
834 if (OidIsValid(expr->elemfuncid) &&
835 func_volatile(expr->elemfuncid) != PROVOLATILE_IMMUTABLE)
836 return true;
837 /* else fall through to check args */
839 else if (IsA(node, NullIfExpr))
841 NullIfExpr *expr = (NullIfExpr *) node;
843 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
844 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
845 return true;
846 /* else fall through to check args */
848 else if (IsA(node, RowCompareExpr))
850 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
851 ListCell *opid;
853 foreach(opid, rcexpr->opnos)
855 if (op_volatile(lfirst_oid(opid)) != PROVOLATILE_IMMUTABLE)
856 return true;
858 /* else fall through to check args */
860 return expression_tree_walker(node, contain_mutable_functions_walker,
861 context);
865 /*****************************************************************************
866 * Check clauses for volatile functions
867 *****************************************************************************/
870 * contain_volatile_functions
871 * Recursively search for volatile functions within a clause.
873 * Returns true if any volatile function (or operator implemented by a
874 * volatile function) is found. This test prevents invalid conversions
875 * of volatile expressions into indexscan quals.
877 * XXX we do not examine sub-selects to see if they contain uses of
878 * volatile functions. It's not real clear if that is correct or not...
880 bool
881 contain_volatile_functions(Node *clause)
883 return contain_volatile_functions_walker(clause, NULL);
886 static bool
887 contain_volatile_functions_walker(Node *node, void *context)
889 if (node == NULL)
890 return false;
891 if (IsA(node, FuncExpr))
893 FuncExpr *expr = (FuncExpr *) node;
895 if (func_volatile(expr->funcid) == PROVOLATILE_VOLATILE)
896 return true;
897 /* else fall through to check args */
899 else if (IsA(node, OpExpr))
901 OpExpr *expr = (OpExpr *) node;
903 set_opfuncid(expr);
904 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
905 return true;
906 /* else fall through to check args */
908 else if (IsA(node, DistinctExpr))
910 DistinctExpr *expr = (DistinctExpr *) node;
912 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
913 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
914 return true;
915 /* else fall through to check args */
917 else if (IsA(node, ScalarArrayOpExpr))
919 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
921 set_sa_opfuncid(expr);
922 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
923 return true;
924 /* else fall through to check args */
926 else if (IsA(node, CoerceViaIO))
928 CoerceViaIO *expr = (CoerceViaIO *) node;
929 Oid iofunc;
930 Oid typioparam;
931 bool typisvarlena;
933 /* check the result type's input function */
934 getTypeInputInfo(expr->resulttype,
935 &iofunc, &typioparam);
936 if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
937 return true;
938 /* check the input type's output function */
939 getTypeOutputInfo(exprType((Node *) expr->arg),
940 &iofunc, &typisvarlena);
941 if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
942 return true;
943 /* else fall through to check args */
945 else if (IsA(node, ArrayCoerceExpr))
947 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
949 if (OidIsValid(expr->elemfuncid) &&
950 func_volatile(expr->elemfuncid) == PROVOLATILE_VOLATILE)
951 return true;
952 /* else fall through to check args */
954 else if (IsA(node, NullIfExpr))
956 NullIfExpr *expr = (NullIfExpr *) node;
958 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
959 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
960 return true;
961 /* else fall through to check args */
963 else if (IsA(node, RowCompareExpr))
965 /* RowCompare probably can't have volatile ops, but check anyway */
966 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
967 ListCell *opid;
969 foreach(opid, rcexpr->opnos)
971 if (op_volatile(lfirst_oid(opid)) == PROVOLATILE_VOLATILE)
972 return true;
974 /* else fall through to check args */
976 return expression_tree_walker(node, contain_volatile_functions_walker,
977 context);
981 /*****************************************************************************
982 * Check clauses for nonstrict functions
983 *****************************************************************************/
986 * contain_nonstrict_functions
987 * Recursively search for nonstrict functions within a clause.
989 * Returns true if any nonstrict construct is found --- ie, anything that
990 * could produce non-NULL output with a NULL input.
992 * The idea here is that the caller has verified that the expression contains
993 * one or more Var or Param nodes (as appropriate for the caller's need), and
994 * now wishes to prove that the expression result will be NULL if any of these
995 * inputs is NULL. If we return false, then the proof succeeded.
997 bool
998 contain_nonstrict_functions(Node *clause)
1000 return contain_nonstrict_functions_walker(clause, NULL);
1003 static bool
1004 contain_nonstrict_functions_walker(Node *node, void *context)
1006 if (node == NULL)
1007 return false;
1008 if (IsA(node, Aggref))
1010 /* an aggregate could return non-null with null input */
1011 return true;
1013 if (IsA(node, WindowFunc))
1015 /* a window function could return non-null with null input */
1016 return true;
1018 if (IsA(node, ArrayRef))
1020 /* array assignment is nonstrict, but subscripting is strict */
1021 if (((ArrayRef *) node)->refassgnexpr != NULL)
1022 return true;
1023 /* else fall through to check args */
1025 if (IsA(node, FuncExpr))
1027 FuncExpr *expr = (FuncExpr *) node;
1029 if (!func_strict(expr->funcid))
1030 return true;
1031 /* else fall through to check args */
1033 if (IsA(node, OpExpr))
1035 OpExpr *expr = (OpExpr *) node;
1037 set_opfuncid(expr);
1038 if (!func_strict(expr->opfuncid))
1039 return true;
1040 /* else fall through to check args */
1042 if (IsA(node, DistinctExpr))
1044 /* IS DISTINCT FROM is inherently non-strict */
1045 return true;
1047 if (IsA(node, ScalarArrayOpExpr))
1049 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1051 if (!is_strict_saop(expr, false))
1052 return true;
1053 /* else fall through to check args */
1055 if (IsA(node, BoolExpr))
1057 BoolExpr *expr = (BoolExpr *) node;
1059 switch (expr->boolop)
1061 case AND_EXPR:
1062 case OR_EXPR:
1063 /* AND, OR are inherently non-strict */
1064 return true;
1065 default:
1066 break;
1069 if (IsA(node, SubLink))
1071 /* In some cases a sublink might be strict, but in general not */
1072 return true;
1074 if (IsA(node, SubPlan))
1075 return true;
1076 if (IsA(node, AlternativeSubPlan))
1077 return true;
1078 /* ArrayCoerceExpr is strict at the array level, regardless of elemfunc */
1079 if (IsA(node, FieldStore))
1080 return true;
1081 if (IsA(node, CaseExpr))
1082 return true;
1083 if (IsA(node, ArrayExpr))
1084 return true;
1085 if (IsA(node, RowExpr))
1086 return true;
1087 if (IsA(node, RowCompareExpr))
1088 return true;
1089 if (IsA(node, CoalesceExpr))
1090 return true;
1091 if (IsA(node, MinMaxExpr))
1092 return true;
1093 if (IsA(node, XmlExpr))
1094 return true;
1095 if (IsA(node, NullIfExpr))
1096 return true;
1097 if (IsA(node, NullTest))
1098 return true;
1099 if (IsA(node, BooleanTest))
1100 return true;
1101 return expression_tree_walker(node, contain_nonstrict_functions_walker,
1102 context);
1107 * find_nonnullable_rels
1108 * Determine which base rels are forced nonnullable by given clause.
1110 * Returns the set of all Relids that are referenced in the clause in such
1111 * a way that the clause cannot possibly return TRUE if any of these Relids
1112 * is an all-NULL row. (It is OK to err on the side of conservatism; hence
1113 * the analysis here is simplistic.)
1115 * The semantics here are subtly different from contain_nonstrict_functions:
1116 * that function is concerned with NULL results from arbitrary expressions,
1117 * but here we assume that the input is a Boolean expression, and wish to
1118 * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1119 * the expression to have been AND/OR flattened and converted to implicit-AND
1120 * format.
1122 * Note: this function is largely duplicative of find_nonnullable_vars().
1123 * The reason not to simplify this function into a thin wrapper around
1124 * find_nonnullable_vars() is that the tested conditions really are different:
1125 * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
1126 * that either v1 or v2 can't be NULL, but it does prove that the t1 row
1127 * as a whole can't be all-NULL.
1129 * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1130 * the result is either FALSE or NULL is good enough. top_level is FALSE when
1131 * we have descended below a NOT or a strict function: now we must be able to
1132 * prove that the subexpression goes to NULL.
1134 * We don't use expression_tree_walker here because we don't want to descend
1135 * through very many kinds of nodes; only the ones we can be sure are strict.
1137 Relids
1138 find_nonnullable_rels(Node *clause)
1140 return find_nonnullable_rels_walker(clause, true);
1143 static Relids
1144 find_nonnullable_rels_walker(Node *node, bool top_level)
1146 Relids result = NULL;
1147 ListCell *l;
1149 if (node == NULL)
1150 return NULL;
1151 if (IsA(node, Var))
1153 Var *var = (Var *) node;
1155 if (var->varlevelsup == 0)
1156 result = bms_make_singleton(var->varno);
1158 else if (IsA(node, List))
1161 * At top level, we are examining an implicit-AND list: if any of the
1162 * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1163 * not at top level, we are examining the arguments of a strict
1164 * function: if any of them produce NULL then the result of the
1165 * function must be NULL. So in both cases, the set of nonnullable
1166 * rels is the union of those found in the arms, and we pass down the
1167 * top_level flag unmodified.
1169 foreach(l, (List *) node)
1171 result = bms_join(result,
1172 find_nonnullable_rels_walker(lfirst(l),
1173 top_level));
1176 else if (IsA(node, FuncExpr))
1178 FuncExpr *expr = (FuncExpr *) node;
1180 if (func_strict(expr->funcid))
1181 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1183 else if (IsA(node, OpExpr))
1185 OpExpr *expr = (OpExpr *) node;
1187 set_opfuncid(expr);
1188 if (func_strict(expr->opfuncid))
1189 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1191 else if (IsA(node, ScalarArrayOpExpr))
1193 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1195 if (is_strict_saop(expr, true))
1196 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1198 else if (IsA(node, BoolExpr))
1200 BoolExpr *expr = (BoolExpr *) node;
1202 switch (expr->boolop)
1204 case AND_EXPR:
1205 /* At top level we can just recurse (to the List case) */
1206 if (top_level)
1208 result = find_nonnullable_rels_walker((Node *) expr->args,
1209 top_level);
1210 break;
1214 * Below top level, even if one arm produces NULL, the result
1215 * could be FALSE (hence not NULL). However, if *all* the
1216 * arms produce NULL then the result is NULL, so we can take
1217 * the intersection of the sets of nonnullable rels, just as
1218 * for OR. Fall through to share code.
1220 /* FALL THRU */
1221 case OR_EXPR:
1224 * OR is strict if all of its arms are, so we can take the
1225 * intersection of the sets of nonnullable rels for each arm.
1226 * This works for both values of top_level.
1228 foreach(l, expr->args)
1230 Relids subresult;
1232 subresult = find_nonnullable_rels_walker(lfirst(l),
1233 top_level);
1234 if (result == NULL) /* first subresult? */
1235 result = subresult;
1236 else
1237 result = bms_int_members(result, subresult);
1240 * If the intersection is empty, we can stop looking. This
1241 * also justifies the test for first-subresult above.
1243 if (bms_is_empty(result))
1244 break;
1246 break;
1247 case NOT_EXPR:
1248 /* NOT will return null if its arg is null */
1249 result = find_nonnullable_rels_walker((Node *) expr->args,
1250 false);
1251 break;
1252 default:
1253 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1254 break;
1257 else if (IsA(node, RelabelType))
1259 RelabelType *expr = (RelabelType *) node;
1261 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1263 else if (IsA(node, CoerceViaIO))
1265 /* not clear this is useful, but it can't hurt */
1266 CoerceViaIO *expr = (CoerceViaIO *) node;
1268 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1270 else if (IsA(node, ArrayCoerceExpr))
1272 /* ArrayCoerceExpr is strict at the array level */
1273 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1275 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1277 else if (IsA(node, ConvertRowtypeExpr))
1279 /* not clear this is useful, but it can't hurt */
1280 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1282 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1284 else if (IsA(node, NullTest))
1286 /* IS NOT NULL can be considered strict, but only at top level */
1287 NullTest *expr = (NullTest *) node;
1289 if (top_level && expr->nulltesttype == IS_NOT_NULL)
1290 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1292 else if (IsA(node, BooleanTest))
1294 /* Boolean tests that reject NULL are strict at top level */
1295 BooleanTest *expr = (BooleanTest *) node;
1297 if (top_level &&
1298 (expr->booltesttype == IS_TRUE ||
1299 expr->booltesttype == IS_FALSE ||
1300 expr->booltesttype == IS_NOT_UNKNOWN))
1301 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1303 else if (IsA(node, PlaceHolderVar))
1305 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1307 result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
1309 return result;
1313 * find_nonnullable_vars
1314 * Determine which Vars are forced nonnullable by given clause.
1316 * Returns a list of all level-zero Vars that are referenced in the clause in
1317 * such a way that the clause cannot possibly return TRUE if any of these Vars
1318 * is NULL. (It is OK to err on the side of conservatism; hence the analysis
1319 * here is simplistic.)
1321 * The semantics here are subtly different from contain_nonstrict_functions:
1322 * that function is concerned with NULL results from arbitrary expressions,
1323 * but here we assume that the input is a Boolean expression, and wish to
1324 * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1325 * the expression to have been AND/OR flattened and converted to implicit-AND
1326 * format.
1328 * The result is a palloc'd List, but we have not copied the member Var nodes.
1329 * Also, we don't bother trying to eliminate duplicate entries.
1331 * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1332 * the result is either FALSE or NULL is good enough. top_level is FALSE when
1333 * we have descended below a NOT or a strict function: now we must be able to
1334 * prove that the subexpression goes to NULL.
1336 * We don't use expression_tree_walker here because we don't want to descend
1337 * through very many kinds of nodes; only the ones we can be sure are strict.
1339 List *
1340 find_nonnullable_vars(Node *clause)
1342 return find_nonnullable_vars_walker(clause, true);
1345 static List *
1346 find_nonnullable_vars_walker(Node *node, bool top_level)
1348 List *result = NIL;
1349 ListCell *l;
1351 if (node == NULL)
1352 return NIL;
1353 if (IsA(node, Var))
1355 Var *var = (Var *) node;
1357 if (var->varlevelsup == 0)
1358 result = list_make1(var);
1360 else if (IsA(node, List))
1363 * At top level, we are examining an implicit-AND list: if any of the
1364 * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1365 * not at top level, we are examining the arguments of a strict
1366 * function: if any of them produce NULL then the result of the
1367 * function must be NULL. So in both cases, the set of nonnullable
1368 * vars is the union of those found in the arms, and we pass down the
1369 * top_level flag unmodified.
1371 foreach(l, (List *) node)
1373 result = list_concat(result,
1374 find_nonnullable_vars_walker(lfirst(l),
1375 top_level));
1378 else if (IsA(node, FuncExpr))
1380 FuncExpr *expr = (FuncExpr *) node;
1382 if (func_strict(expr->funcid))
1383 result = find_nonnullable_vars_walker((Node *) expr->args, false);
1385 else if (IsA(node, OpExpr))
1387 OpExpr *expr = (OpExpr *) node;
1389 set_opfuncid(expr);
1390 if (func_strict(expr->opfuncid))
1391 result = find_nonnullable_vars_walker((Node *) expr->args, false);
1393 else if (IsA(node, ScalarArrayOpExpr))
1395 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1397 if (is_strict_saop(expr, true))
1398 result = find_nonnullable_vars_walker((Node *) expr->args, false);
1400 else if (IsA(node, BoolExpr))
1402 BoolExpr *expr = (BoolExpr *) node;
1404 switch (expr->boolop)
1406 case AND_EXPR:
1407 /* At top level we can just recurse (to the List case) */
1408 if (top_level)
1410 result = find_nonnullable_vars_walker((Node *) expr->args,
1411 top_level);
1412 break;
1416 * Below top level, even if one arm produces NULL, the result
1417 * could be FALSE (hence not NULL). However, if *all* the
1418 * arms produce NULL then the result is NULL, so we can take
1419 * the intersection of the sets of nonnullable vars, just as
1420 * for OR. Fall through to share code.
1422 /* FALL THRU */
1423 case OR_EXPR:
1426 * OR is strict if all of its arms are, so we can take the
1427 * intersection of the sets of nonnullable vars for each arm.
1428 * This works for both values of top_level.
1430 foreach(l, expr->args)
1432 List *subresult;
1434 subresult = find_nonnullable_vars_walker(lfirst(l),
1435 top_level);
1436 if (result == NIL) /* first subresult? */
1437 result = subresult;
1438 else
1439 result = list_intersection(result, subresult);
1442 * If the intersection is empty, we can stop looking. This
1443 * also justifies the test for first-subresult above.
1445 if (result == NIL)
1446 break;
1448 break;
1449 case NOT_EXPR:
1450 /* NOT will return null if its arg is null */
1451 result = find_nonnullable_vars_walker((Node *) expr->args,
1452 false);
1453 break;
1454 default:
1455 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1456 break;
1459 else if (IsA(node, RelabelType))
1461 RelabelType *expr = (RelabelType *) node;
1463 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1465 else if (IsA(node, CoerceViaIO))
1467 /* not clear this is useful, but it can't hurt */
1468 CoerceViaIO *expr = (CoerceViaIO *) node;
1470 result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1472 else if (IsA(node, ArrayCoerceExpr))
1474 /* ArrayCoerceExpr is strict at the array level */
1475 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1477 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1479 else if (IsA(node, ConvertRowtypeExpr))
1481 /* not clear this is useful, but it can't hurt */
1482 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1484 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1486 else if (IsA(node, NullTest))
1488 /* IS NOT NULL can be considered strict, but only at top level */
1489 NullTest *expr = (NullTest *) node;
1491 if (top_level && expr->nulltesttype == IS_NOT_NULL)
1492 result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1494 else if (IsA(node, BooleanTest))
1496 /* Boolean tests that reject NULL are strict at top level */
1497 BooleanTest *expr = (BooleanTest *) node;
1499 if (top_level &&
1500 (expr->booltesttype == IS_TRUE ||
1501 expr->booltesttype == IS_FALSE ||
1502 expr->booltesttype == IS_NOT_UNKNOWN))
1503 result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1505 else if (IsA(node, PlaceHolderVar))
1507 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1509 result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
1511 return result;
1515 * find_forced_null_vars
1516 * Determine which Vars must be NULL for the given clause to return TRUE.
1518 * This is the complement of find_nonnullable_vars: find the level-zero Vars
1519 * that must be NULL for the clause to return TRUE. (It is OK to err on the
1520 * side of conservatism; hence the analysis here is simplistic. In fact,
1521 * we only detect simple "var IS NULL" tests at the top level.)
1523 * The result is a palloc'd List, but we have not copied the member Var nodes.
1524 * Also, we don't bother trying to eliminate duplicate entries.
1526 List *
1527 find_forced_null_vars(Node *node)
1529 List *result = NIL;
1530 Var *var;
1531 ListCell *l;
1533 if (node == NULL)
1534 return NIL;
1535 /* Check single-clause cases using subroutine */
1536 var = find_forced_null_var(node);
1537 if (var)
1539 result = list_make1(var);
1541 /* Otherwise, handle AND-conditions */
1542 else if (IsA(node, List))
1545 * At top level, we are examining an implicit-AND list: if any of the
1546 * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1548 foreach(l, (List *) node)
1550 result = list_concat(result,
1551 find_forced_null_vars(lfirst(l)));
1554 else if (IsA(node, BoolExpr))
1556 BoolExpr *expr = (BoolExpr *) node;
1559 * We don't bother considering the OR case, because it's fairly
1560 * unlikely anyone would write "v1 IS NULL OR v1 IS NULL".
1561 * Likewise, the NOT case isn't worth expending code on.
1563 if (expr->boolop == AND_EXPR)
1565 /* At top level we can just recurse (to the List case) */
1566 result = find_forced_null_vars((Node *) expr->args);
1569 return result;
1573 * find_forced_null_var
1574 * Return the Var forced null by the given clause, or NULL if it's
1575 * not an IS NULL-type clause. For success, the clause must enforce
1576 * *only* nullness of the particular Var, not any other conditions.
1578 * This is just the single-clause case of find_forced_null_vars(), without
1579 * any allowance for AND conditions. It's used by initsplan.c on individual
1580 * qual clauses. The reason for not just applying find_forced_null_vars()
1581 * is that if an AND of an IS NULL clause with something else were to somehow
1582 * survive AND/OR flattening, initsplan.c might get fooled into discarding
1583 * the whole clause when only the IS NULL part of it had been proved redundant.
1585 Var *
1586 find_forced_null_var(Node *node)
1588 if (node == NULL)
1589 return NULL;
1590 if (IsA(node, NullTest))
1592 /* check for var IS NULL */
1593 NullTest *expr = (NullTest *) node;
1595 if (expr->nulltesttype == IS_NULL)
1597 Var *var = (Var *) expr->arg;
1599 if (var && IsA(var, Var) &&
1600 var->varlevelsup == 0)
1601 return var;
1604 else if (IsA(node, BooleanTest))
1606 /* var IS UNKNOWN is equivalent to var IS NULL */
1607 BooleanTest *expr = (BooleanTest *) node;
1609 if (expr->booltesttype == IS_UNKNOWN)
1611 Var *var = (Var *) expr->arg;
1613 if (var && IsA(var, Var) &&
1614 var->varlevelsup == 0)
1615 return var;
1618 return NULL;
1622 * Can we treat a ScalarArrayOpExpr as strict?
1624 * If "falseOK" is true, then a "false" result can be considered strict,
1625 * else we need to guarantee an actual NULL result for NULL input.
1627 * "foo op ALL array" is strict if the op is strict *and* we can prove
1628 * that the array input isn't an empty array. We can check that
1629 * for the cases of an array constant and an ARRAY[] construct.
1631 * "foo op ANY array" is strict in the falseOK sense if the op is strict.
1632 * If not falseOK, the test is the same as for "foo op ALL array".
1634 static bool
1635 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
1637 Node *rightop;
1639 /* The contained operator must be strict. */
1640 set_sa_opfuncid(expr);
1641 if (!func_strict(expr->opfuncid))
1642 return false;
1643 /* If ANY and falseOK, that's all we need to check. */
1644 if (expr->useOr && falseOK)
1645 return true;
1646 /* Else, we have to see if the array is provably non-empty. */
1647 Assert(list_length(expr->args) == 2);
1648 rightop = (Node *) lsecond(expr->args);
1649 if (rightop && IsA(rightop, Const))
1651 Datum arraydatum = ((Const *) rightop)->constvalue;
1652 bool arrayisnull = ((Const *) rightop)->constisnull;
1653 ArrayType *arrayval;
1654 int nitems;
1656 if (arrayisnull)
1657 return false;
1658 arrayval = DatumGetArrayTypeP(arraydatum);
1659 nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1660 if (nitems > 0)
1661 return true;
1663 else if (rightop && IsA(rightop, ArrayExpr))
1665 ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1667 if (arrayexpr->elements != NIL && !arrayexpr->multidims)
1668 return true;
1670 return false;
1674 /*****************************************************************************
1675 * Check for "pseudo-constant" clauses
1676 *****************************************************************************/
1679 * is_pseudo_constant_clause
1680 * Detect whether an expression is "pseudo constant", ie, it contains no
1681 * variables of the current query level and no uses of volatile functions.
1682 * Such an expr is not necessarily a true constant: it can still contain
1683 * Params and outer-level Vars, not to mention functions whose results
1684 * may vary from one statement to the next. However, the expr's value
1685 * will be constant over any one scan of the current query, so it can be
1686 * used as, eg, an indexscan key.
1688 * CAUTION: this function omits to test for one very important class of
1689 * not-constant expressions, namely aggregates (Aggrefs). In current usage
1690 * this is only applied to WHERE clauses and so a check for Aggrefs would be
1691 * a waste of cycles; but be sure to also check contain_agg_clause() if you
1692 * want to know about pseudo-constness in other contexts. The same goes
1693 * for window functions (WindowFuncs).
1695 bool
1696 is_pseudo_constant_clause(Node *clause)
1699 * We could implement this check in one recursive scan. But since the
1700 * check for volatile functions is both moderately expensive and unlikely
1701 * to fail, it seems better to look for Vars first and only check for
1702 * volatile functions if we find no Vars.
1704 if (!contain_var_clause(clause) &&
1705 !contain_volatile_functions(clause))
1706 return true;
1707 return false;
1711 * is_pseudo_constant_clause_relids
1712 * Same as above, except caller already has available the var membership
1713 * of the expression; this lets us avoid the contain_var_clause() scan.
1715 bool
1716 is_pseudo_constant_clause_relids(Node *clause, Relids relids)
1718 if (bms_is_empty(relids) &&
1719 !contain_volatile_functions(clause))
1720 return true;
1721 return false;
1725 /*****************************************************************************
1727 * General clause-manipulating routines *
1729 *****************************************************************************/
1732 * NumRelids
1733 * (formerly clause_relids)
1735 * Returns the number of different relations referenced in 'clause'.
1738 NumRelids(Node *clause)
1740 Relids varnos = pull_varnos(clause);
1741 int result = bms_num_members(varnos);
1743 bms_free(varnos);
1744 return result;
1748 * CommuteOpExpr: commute a binary operator clause
1750 * XXX the clause is destructively modified!
1752 void
1753 CommuteOpExpr(OpExpr *clause)
1755 Oid opoid;
1756 Node *temp;
1758 /* Sanity checks: caller is at fault if these fail */
1759 if (!is_opclause(clause) ||
1760 list_length(clause->args) != 2)
1761 elog(ERROR, "cannot commute non-binary-operator clause");
1763 opoid = get_commutator(clause->opno);
1765 if (!OidIsValid(opoid))
1766 elog(ERROR, "could not find commutator for operator %u",
1767 clause->opno);
1770 * modify the clause in-place!
1772 clause->opno = opoid;
1773 clause->opfuncid = InvalidOid;
1774 /* opresulttype and opretset are assumed not to change */
1776 temp = linitial(clause->args);
1777 linitial(clause->args) = lsecond(clause->args);
1778 lsecond(clause->args) = temp;
1782 * CommuteRowCompareExpr: commute a RowCompareExpr clause
1784 * XXX the clause is destructively modified!
1786 void
1787 CommuteRowCompareExpr(RowCompareExpr *clause)
1789 List *newops;
1790 List *temp;
1791 ListCell *l;
1793 /* Sanity checks: caller is at fault if these fail */
1794 if (!IsA(clause, RowCompareExpr))
1795 elog(ERROR, "expected a RowCompareExpr");
1797 /* Build list of commuted operators */
1798 newops = NIL;
1799 foreach(l, clause->opnos)
1801 Oid opoid = lfirst_oid(l);
1803 opoid = get_commutator(opoid);
1804 if (!OidIsValid(opoid))
1805 elog(ERROR, "could not find commutator for operator %u",
1806 lfirst_oid(l));
1807 newops = lappend_oid(newops, opoid);
1811 * modify the clause in-place!
1813 switch (clause->rctype)
1815 case ROWCOMPARE_LT:
1816 clause->rctype = ROWCOMPARE_GT;
1817 break;
1818 case ROWCOMPARE_LE:
1819 clause->rctype = ROWCOMPARE_GE;
1820 break;
1821 case ROWCOMPARE_GE:
1822 clause->rctype = ROWCOMPARE_LE;
1823 break;
1824 case ROWCOMPARE_GT:
1825 clause->rctype = ROWCOMPARE_LT;
1826 break;
1827 default:
1828 elog(ERROR, "unexpected RowCompare type: %d",
1829 (int) clause->rctype);
1830 break;
1833 clause->opnos = newops;
1836 * Note: we need not change the opfamilies list; we assume any btree
1837 * opfamily containing an operator will also contain its commutator.
1840 temp = clause->largs;
1841 clause->largs = clause->rargs;
1842 clause->rargs = temp;
1846 * strip_implicit_coercions: remove implicit coercions at top level of tree
1848 * Note: there isn't any useful thing we can do with a RowExpr here, so
1849 * just return it unchanged, even if it's marked as an implicit coercion.
1851 Node *
1852 strip_implicit_coercions(Node *node)
1854 if (node == NULL)
1855 return NULL;
1856 if (IsA(node, FuncExpr))
1858 FuncExpr *f = (FuncExpr *) node;
1860 if (f->funcformat == COERCE_IMPLICIT_CAST)
1861 return strip_implicit_coercions(linitial(f->args));
1863 else if (IsA(node, RelabelType))
1865 RelabelType *r = (RelabelType *) node;
1867 if (r->relabelformat == COERCE_IMPLICIT_CAST)
1868 return strip_implicit_coercions((Node *) r->arg);
1870 else if (IsA(node, CoerceViaIO))
1872 CoerceViaIO *c = (CoerceViaIO *) node;
1874 if (c->coerceformat == COERCE_IMPLICIT_CAST)
1875 return strip_implicit_coercions((Node *) c->arg);
1877 else if (IsA(node, ArrayCoerceExpr))
1879 ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
1881 if (c->coerceformat == COERCE_IMPLICIT_CAST)
1882 return strip_implicit_coercions((Node *) c->arg);
1884 else if (IsA(node, ConvertRowtypeExpr))
1886 ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
1888 if (c->convertformat == COERCE_IMPLICIT_CAST)
1889 return strip_implicit_coercions((Node *) c->arg);
1891 else if (IsA(node, CoerceToDomain))
1893 CoerceToDomain *c = (CoerceToDomain *) node;
1895 if (c->coercionformat == COERCE_IMPLICIT_CAST)
1896 return strip_implicit_coercions((Node *) c->arg);
1898 return node;
1902 * set_coercionform_dontcare: set all CoercionForm fields to COERCE_DONTCARE
1904 * This is used to make index expressions and index predicates more easily
1905 * comparable to clauses of queries. CoercionForm is not semantically
1906 * significant (for cases where it does matter, the significant info is
1907 * coded into the coercion function arguments) so we can ignore it during
1908 * comparisons. Thus, for example, an index on "foo::int4" can match an
1909 * implicit coercion to int4.
1911 * Caution: the passed expression tree is modified in-place.
1913 void
1914 set_coercionform_dontcare(Node *node)
1916 (void) set_coercionform_dontcare_walker(node, NULL);
1919 static bool
1920 set_coercionform_dontcare_walker(Node *node, void *context)
1922 if (node == NULL)
1923 return false;
1924 if (IsA(node, FuncExpr))
1925 ((FuncExpr *) node)->funcformat = COERCE_DONTCARE;
1926 else if (IsA(node, RelabelType))
1927 ((RelabelType *) node)->relabelformat = COERCE_DONTCARE;
1928 else if (IsA(node, CoerceViaIO))
1929 ((CoerceViaIO *) node)->coerceformat = COERCE_DONTCARE;
1930 else if (IsA(node, ArrayCoerceExpr))
1931 ((ArrayCoerceExpr *) node)->coerceformat = COERCE_DONTCARE;
1932 else if (IsA(node, ConvertRowtypeExpr))
1933 ((ConvertRowtypeExpr *) node)->convertformat = COERCE_DONTCARE;
1934 else if (IsA(node, RowExpr))
1935 ((RowExpr *) node)->row_format = COERCE_DONTCARE;
1936 else if (IsA(node, CoerceToDomain))
1937 ((CoerceToDomain *) node)->coercionformat = COERCE_DONTCARE;
1938 return expression_tree_walker(node, set_coercionform_dontcare_walker,
1939 context);
1943 * Helper for eval_const_expressions: check that datatype of an attribute
1944 * is still what it was when the expression was parsed. This is needed to
1945 * guard against improper simplification after ALTER COLUMN TYPE. (XXX we
1946 * may well need to make similar checks elsewhere?)
1948 static bool
1949 rowtype_field_matches(Oid rowtypeid, int fieldnum,
1950 Oid expectedtype, int32 expectedtypmod)
1952 TupleDesc tupdesc;
1953 Form_pg_attribute attr;
1955 /* No issue for RECORD, since there is no way to ALTER such a type */
1956 if (rowtypeid == RECORDOID)
1957 return true;
1958 tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
1959 if (fieldnum <= 0 || fieldnum > tupdesc->natts)
1961 ReleaseTupleDesc(tupdesc);
1962 return false;
1964 attr = tupdesc->attrs[fieldnum - 1];
1965 if (attr->attisdropped ||
1966 attr->atttypid != expectedtype ||
1967 attr->atttypmod != expectedtypmod)
1969 ReleaseTupleDesc(tupdesc);
1970 return false;
1972 ReleaseTupleDesc(tupdesc);
1973 return true;
1977 /*--------------------
1978 * eval_const_expressions
1980 * Reduce any recognizably constant subexpressions of the given
1981 * expression tree, for example "2 + 2" => "4". More interestingly,
1982 * we can reduce certain boolean expressions even when they contain
1983 * non-constant subexpressions: "x OR true" => "true" no matter what
1984 * the subexpression x is. (XXX We assume that no such subexpression
1985 * will have important side-effects, which is not necessarily a good
1986 * assumption in the presence of user-defined functions; do we need a
1987 * pg_proc flag that prevents discarding the execution of a function?)
1989 * We do understand that certain functions may deliver non-constant
1990 * results even with constant inputs, "nextval()" being the classic
1991 * example. Functions that are not marked "immutable" in pg_proc
1992 * will not be pre-evaluated here, although we will reduce their
1993 * arguments as far as possible.
1995 * We assume that the tree has already been type-checked and contains
1996 * only operators and functions that are reasonable to try to execute.
1998 * NOTE: "root" can be passed as NULL if the caller never wants to do any
1999 * Param substitutions.
2001 * NOTE: the planner assumes that this will always flatten nested AND and
2002 * OR clauses into N-argument form. See comments in prepqual.c.
2004 * NOTE: another critical effect is that any function calls that require
2005 * default arguments will be expanded.
2006 *--------------------
2008 Node *
2009 eval_const_expressions(PlannerInfo *root, Node *node)
2011 eval_const_expressions_context context;
2013 if (root)
2015 context.boundParams = root->glob->boundParams; /* bound Params */
2016 context.glob = root->glob; /* for inlined-function dependencies */
2018 else
2020 context.boundParams = NULL;
2021 context.glob = NULL;
2023 context.active_fns = NIL; /* nothing being recursively simplified */
2024 context.case_val = NULL; /* no CASE being examined */
2025 context.estimate = false; /* safe transformations only */
2026 return eval_const_expressions_mutator(node, &context);
2029 /*--------------------
2030 * estimate_expression_value
2032 * This function attempts to estimate the value of an expression for
2033 * planning purposes. It is in essence a more aggressive version of
2034 * eval_const_expressions(): we will perform constant reductions that are
2035 * not necessarily 100% safe, but are reasonable for estimation purposes.
2037 * Currently the extra steps that are taken in this mode are:
2038 * 1. Substitute values for Params, where a bound Param value has been made
2039 * available by the caller of planner(), even if the Param isn't marked
2040 * constant. This effectively means that we plan using the first supplied
2041 * value of the Param.
2042 * 2. Fold stable, as well as immutable, functions to constants.
2043 * 3. Reduce PlaceHolderVar nodes to their contained expressions.
2044 *--------------------
2046 Node *
2047 estimate_expression_value(PlannerInfo *root, Node *node)
2049 eval_const_expressions_context context;
2051 context.boundParams = root->glob->boundParams; /* bound Params */
2052 /* we do not need to mark the plan as depending on inlined functions */
2053 context.glob = NULL;
2054 context.active_fns = NIL; /* nothing being recursively simplified */
2055 context.case_val = NULL; /* no CASE being examined */
2056 context.estimate = true; /* unsafe transformations OK */
2057 return eval_const_expressions_mutator(node, &context);
2060 static Node *
2061 eval_const_expressions_mutator(Node *node,
2062 eval_const_expressions_context *context)
2064 if (node == NULL)
2065 return NULL;
2066 if (IsA(node, Param))
2068 Param *param = (Param *) node;
2070 /* Look to see if we've been given a value for this Param */
2071 if (param->paramkind == PARAM_EXTERN &&
2072 context->boundParams != NULL &&
2073 param->paramid > 0 &&
2074 param->paramid <= context->boundParams->numParams)
2076 ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
2078 if (OidIsValid(prm->ptype))
2080 /* OK to substitute parameter value? */
2081 if (context->estimate || (prm->pflags & PARAM_FLAG_CONST))
2084 * Return a Const representing the param value. Must copy
2085 * pass-by-ref datatypes, since the Param might be in a
2086 * memory context shorter-lived than our output plan
2087 * should be.
2089 int16 typLen;
2090 bool typByVal;
2091 Datum pval;
2093 Assert(prm->ptype == param->paramtype);
2094 get_typlenbyval(param->paramtype, &typLen, &typByVal);
2095 if (prm->isnull || typByVal)
2096 pval = prm->value;
2097 else
2098 pval = datumCopy(prm->value, typByVal, typLen);
2099 return (Node *) makeConst(param->paramtype,
2100 param->paramtypmod,
2101 (int) typLen,
2102 pval,
2103 prm->isnull,
2104 typByVal);
2108 /* Not replaceable, so just copy the Param (no need to recurse) */
2109 return (Node *) copyObject(param);
2111 if (IsA(node, FuncExpr))
2113 FuncExpr *expr = (FuncExpr *) node;
2114 List *args;
2115 Expr *simple;
2116 FuncExpr *newexpr;
2119 * Reduce constants in the FuncExpr's arguments. We know args is
2120 * either NIL or a List node, so we can call expression_tree_mutator
2121 * directly rather than recursing to self.
2123 args = (List *) expression_tree_mutator((Node *) expr->args,
2124 eval_const_expressions_mutator,
2125 (void *) context);
2128 * Code for op/func reduction is pretty bulky, so split it out as a
2129 * separate function. Note: exprTypmod normally returns -1 for a
2130 * FuncExpr, but not when the node is recognizably a length coercion;
2131 * we want to preserve the typmod in the eventual Const if so.
2133 simple = simplify_function(expr->funcid,
2134 expr->funcresulttype, exprTypmod(node),
2135 &args,
2136 true, context);
2137 if (simple) /* successfully simplified it */
2138 return (Node *) simple;
2141 * The expression cannot be simplified any further, so build and
2142 * return a replacement FuncExpr node using the possibly-simplified
2143 * arguments.
2145 newexpr = makeNode(FuncExpr);
2146 newexpr->funcid = expr->funcid;
2147 newexpr->funcresulttype = expr->funcresulttype;
2148 newexpr->funcretset = expr->funcretset;
2149 newexpr->funcformat = expr->funcformat;
2150 newexpr->args = args;
2151 newexpr->location = expr->location;
2152 return (Node *) newexpr;
2154 if (IsA(node, OpExpr))
2156 OpExpr *expr = (OpExpr *) node;
2157 List *args;
2158 Expr *simple;
2159 OpExpr *newexpr;
2162 * Reduce constants in the OpExpr's arguments. We know args is either
2163 * NIL or a List node, so we can call expression_tree_mutator directly
2164 * rather than recursing to self.
2166 args = (List *) expression_tree_mutator((Node *) expr->args,
2167 eval_const_expressions_mutator,
2168 (void *) context);
2171 * Need to get OID of underlying function. Okay to scribble on input
2172 * to this extent.
2174 set_opfuncid(expr);
2177 * Code for op/func reduction is pretty bulky, so split it out as a
2178 * separate function.
2180 simple = simplify_function(expr->opfuncid,
2181 expr->opresulttype, -1,
2182 &args,
2183 true, context);
2184 if (simple) /* successfully simplified it */
2185 return (Node *) simple;
2188 * If the operator is boolean equality, we know how to simplify cases
2189 * involving one constant and one non-constant argument.
2191 if (expr->opno == BooleanEqualOperator)
2193 simple = simplify_boolean_equality(args);
2194 if (simple) /* successfully simplified it */
2195 return (Node *) simple;
2199 * The expression cannot be simplified any further, so build and
2200 * return a replacement OpExpr node using the possibly-simplified
2201 * arguments.
2203 newexpr = makeNode(OpExpr);
2204 newexpr->opno = expr->opno;
2205 newexpr->opfuncid = expr->opfuncid;
2206 newexpr->opresulttype = expr->opresulttype;
2207 newexpr->opretset = expr->opretset;
2208 newexpr->args = args;
2209 newexpr->location = expr->location;
2210 return (Node *) newexpr;
2212 if (IsA(node, DistinctExpr))
2214 DistinctExpr *expr = (DistinctExpr *) node;
2215 List *args;
2216 ListCell *arg;
2217 bool has_null_input = false;
2218 bool all_null_input = true;
2219 bool has_nonconst_input = false;
2220 Expr *simple;
2221 DistinctExpr *newexpr;
2224 * Reduce constants in the DistinctExpr's arguments. We know args is
2225 * either NIL or a List node, so we can call expression_tree_mutator
2226 * directly rather than recursing to self.
2228 args = (List *) expression_tree_mutator((Node *) expr->args,
2229 eval_const_expressions_mutator,
2230 (void *) context);
2233 * We must do our own check for NULLs because DistinctExpr has
2234 * different results for NULL input than the underlying operator does.
2236 foreach(arg, args)
2238 if (IsA(lfirst(arg), Const))
2240 has_null_input |= ((Const *) lfirst(arg))->constisnull;
2241 all_null_input &= ((Const *) lfirst(arg))->constisnull;
2243 else
2244 has_nonconst_input = true;
2247 /* all constants? then can optimize this out */
2248 if (!has_nonconst_input)
2250 /* all nulls? then not distinct */
2251 if (all_null_input)
2252 return makeBoolConst(false, false);
2254 /* one null? then distinct */
2255 if (has_null_input)
2256 return makeBoolConst(true, false);
2258 /* otherwise try to evaluate the '=' operator */
2259 /* (NOT okay to try to inline it, though!) */
2262 * Need to get OID of underlying function. Okay to scribble on
2263 * input to this extent.
2265 set_opfuncid((OpExpr *) expr); /* rely on struct equivalence */
2268 * Code for op/func reduction is pretty bulky, so split it out as
2269 * a separate function.
2271 simple = simplify_function(expr->opfuncid,
2272 expr->opresulttype, -1,
2273 &args,
2274 false, context);
2275 if (simple) /* successfully simplified it */
2278 * Since the underlying operator is "=", must negate its
2279 * result
2281 Const *csimple = (Const *) simple;
2283 Assert(IsA(csimple, Const));
2284 csimple->constvalue =
2285 BoolGetDatum(!DatumGetBool(csimple->constvalue));
2286 return (Node *) csimple;
2291 * The expression cannot be simplified any further, so build and
2292 * return a replacement DistinctExpr node using the
2293 * possibly-simplified arguments.
2295 newexpr = makeNode(DistinctExpr);
2296 newexpr->opno = expr->opno;
2297 newexpr->opfuncid = expr->opfuncid;
2298 newexpr->opresulttype = expr->opresulttype;
2299 newexpr->opretset = expr->opretset;
2300 newexpr->args = args;
2301 newexpr->location = expr->location;
2302 return (Node *) newexpr;
2304 if (IsA(node, BoolExpr))
2306 BoolExpr *expr = (BoolExpr *) node;
2308 switch (expr->boolop)
2310 case OR_EXPR:
2312 List *newargs;
2313 bool haveNull = false;
2314 bool forceTrue = false;
2316 newargs = simplify_or_arguments(expr->args, context,
2317 &haveNull, &forceTrue);
2318 if (forceTrue)
2319 return makeBoolConst(true, false);
2320 if (haveNull)
2321 newargs = lappend(newargs, makeBoolConst(false, true));
2322 /* If all the inputs are FALSE, result is FALSE */
2323 if (newargs == NIL)
2324 return makeBoolConst(false, false);
2325 /* If only one nonconst-or-NULL input, it's the result */
2326 if (list_length(newargs) == 1)
2327 return (Node *) linitial(newargs);
2328 /* Else we still need an OR node */
2329 return (Node *) make_orclause(newargs);
2331 case AND_EXPR:
2333 List *newargs;
2334 bool haveNull = false;
2335 bool forceFalse = false;
2337 newargs = simplify_and_arguments(expr->args, context,
2338 &haveNull, &forceFalse);
2339 if (forceFalse)
2340 return makeBoolConst(false, false);
2341 if (haveNull)
2342 newargs = lappend(newargs, makeBoolConst(false, true));
2343 /* If all the inputs are TRUE, result is TRUE */
2344 if (newargs == NIL)
2345 return makeBoolConst(true, false);
2346 /* If only one nonconst-or-NULL input, it's the result */
2347 if (list_length(newargs) == 1)
2348 return (Node *) linitial(newargs);
2349 /* Else we still need an AND node */
2350 return (Node *) make_andclause(newargs);
2352 case NOT_EXPR:
2354 Node *arg;
2356 Assert(list_length(expr->args) == 1);
2357 arg = eval_const_expressions_mutator(linitial(expr->args),
2358 context);
2359 if (IsA(arg, Const))
2361 Const *const_input = (Const *) arg;
2363 /* NOT NULL => NULL */
2364 if (const_input->constisnull)
2365 return makeBoolConst(false, true);
2366 /* otherwise pretty easy */
2367 return makeBoolConst(!DatumGetBool(const_input->constvalue),
2368 false);
2370 else if (not_clause(arg))
2372 /* Cancel NOT/NOT */
2373 return (Node *) get_notclausearg((Expr *) arg);
2375 /* Else we still need a NOT node */
2376 return (Node *) make_notclause((Expr *) arg);
2378 default:
2379 elog(ERROR, "unrecognized boolop: %d",
2380 (int) expr->boolop);
2381 break;
2384 if (IsA(node, SubPlan) ||
2385 IsA(node, AlternativeSubPlan))
2388 * Return a SubPlan unchanged --- too late to do anything with it.
2390 * XXX should we ereport() here instead? Probably this routine should
2391 * never be invoked after SubPlan creation.
2393 return node;
2395 if (IsA(node, RelabelType))
2398 * If we can simplify the input to a constant, then we don't need the
2399 * RelabelType node anymore: just change the type field of the Const
2400 * node. Otherwise, must copy the RelabelType node.
2402 RelabelType *relabel = (RelabelType *) node;
2403 Node *arg;
2405 arg = eval_const_expressions_mutator((Node *) relabel->arg,
2406 context);
2409 * If we find stacked RelabelTypes (eg, from foo :: int :: oid) we can
2410 * discard all but the top one.
2412 while (arg && IsA(arg, RelabelType))
2413 arg = (Node *) ((RelabelType *) arg)->arg;
2415 if (arg && IsA(arg, Const))
2417 Const *con = (Const *) arg;
2419 con->consttype = relabel->resulttype;
2420 con->consttypmod = relabel->resulttypmod;
2421 return (Node *) con;
2423 else
2425 RelabelType *newrelabel = makeNode(RelabelType);
2427 newrelabel->arg = (Expr *) arg;
2428 newrelabel->resulttype = relabel->resulttype;
2429 newrelabel->resulttypmod = relabel->resulttypmod;
2430 newrelabel->relabelformat = relabel->relabelformat;
2431 newrelabel->location = relabel->location;
2432 return (Node *) newrelabel;
2435 if (IsA(node, CoerceViaIO))
2437 CoerceViaIO *expr = (CoerceViaIO *) node;
2438 Expr *arg;
2439 List *args;
2440 Oid outfunc;
2441 bool outtypisvarlena;
2442 Oid infunc;
2443 Oid intypioparam;
2444 Expr *simple;
2445 CoerceViaIO *newexpr;
2448 * Reduce constants in the CoerceViaIO's argument.
2450 arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg,
2451 context);
2452 args = list_make1(arg);
2455 * CoerceViaIO represents calling the source type's output function
2456 * then the result type's input function. So, try to simplify it
2457 * as though it were a stack of two such function calls. First we
2458 * need to know what the functions are.
2460 getTypeOutputInfo(exprType((Node *) arg), &outfunc, &outtypisvarlena);
2461 getTypeInputInfo(expr->resulttype, &infunc, &intypioparam);
2463 simple = simplify_function(outfunc,
2464 CSTRINGOID, -1,
2465 &args,
2466 true, context);
2467 if (simple) /* successfully simplified output fn */
2470 * Input functions may want 1 to 3 arguments. We always supply
2471 * all three, trusting that nothing downstream will complain.
2473 args = list_make3(simple,
2474 makeConst(OIDOID, -1, sizeof(Oid),
2475 ObjectIdGetDatum(intypioparam),
2476 false, true),
2477 makeConst(INT4OID, -1, sizeof(int32),
2478 Int32GetDatum(-1),
2479 false, true));
2481 simple = simplify_function(infunc,
2482 expr->resulttype, -1,
2483 &args,
2484 true, context);
2485 if (simple) /* successfully simplified input fn */
2486 return (Node *) simple;
2490 * The expression cannot be simplified any further, so build and
2491 * return a replacement CoerceViaIO node using the possibly-simplified
2492 * argument.
2494 newexpr = makeNode(CoerceViaIO);
2495 newexpr->arg = arg;
2496 newexpr->resulttype = expr->resulttype;
2497 newexpr->coerceformat = expr->coerceformat;
2498 newexpr->location = expr->location;
2499 return (Node *) newexpr;
2501 if (IsA(node, ArrayCoerceExpr))
2503 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
2504 Expr *arg;
2505 ArrayCoerceExpr *newexpr;
2508 * Reduce constants in the ArrayCoerceExpr's argument, then build
2509 * a new ArrayCoerceExpr.
2511 arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg,
2512 context);
2514 newexpr = makeNode(ArrayCoerceExpr);
2515 newexpr->arg = arg;
2516 newexpr->elemfuncid = expr->elemfuncid;
2517 newexpr->resulttype = expr->resulttype;
2518 newexpr->resulttypmod = expr->resulttypmod;
2519 newexpr->isExplicit = expr->isExplicit;
2520 newexpr->coerceformat = expr->coerceformat;
2521 newexpr->location = expr->location;
2524 * If constant argument and it's a binary-coercible or immutable
2525 * conversion, we can simplify it to a constant.
2527 if (arg && IsA(arg, Const) &&
2528 (!OidIsValid(newexpr->elemfuncid) ||
2529 func_volatile(newexpr->elemfuncid) == PROVOLATILE_IMMUTABLE))
2530 return (Node *) evaluate_expr((Expr *) newexpr,
2531 newexpr->resulttype,
2532 newexpr->resulttypmod);
2534 /* Else we must return the partially-simplified node */
2535 return (Node *) newexpr;
2537 if (IsA(node, CaseExpr))
2539 /*----------
2540 * CASE expressions can be simplified if there are constant
2541 * condition clauses:
2542 * FALSE (or NULL): drop the alternative
2543 * TRUE: drop all remaining alternatives
2544 * If the first non-FALSE alternative is a constant TRUE, we can
2545 * simplify the entire CASE to that alternative's expression.
2546 * If there are no non-FALSE alternatives, we simplify the entire
2547 * CASE to the default result (ELSE result).
2549 * If we have a simple-form CASE with constant test expression,
2550 * we substitute the constant value for contained CaseTestExpr
2551 * placeholder nodes, so that we have the opportunity to reduce
2552 * constant test conditions. For example this allows
2553 * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
2554 * to reduce to 1 rather than drawing a divide-by-0 error.
2555 *----------
2557 CaseExpr *caseexpr = (CaseExpr *) node;
2558 CaseExpr *newcase;
2559 Node *save_case_val;
2560 Node *newarg;
2561 List *newargs;
2562 bool const_true_cond;
2563 Node *defresult = NULL;
2564 ListCell *arg;
2566 /* Simplify the test expression, if any */
2567 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
2568 context);
2570 /* Set up for contained CaseTestExpr nodes */
2571 save_case_val = context->case_val;
2572 if (newarg && IsA(newarg, Const))
2573 context->case_val = newarg;
2574 else
2575 context->case_val = NULL;
2577 /* Simplify the WHEN clauses */
2578 newargs = NIL;
2579 const_true_cond = false;
2580 foreach(arg, caseexpr->args)
2582 CaseWhen *oldcasewhen = (CaseWhen *) lfirst(arg);
2583 Node *casecond;
2584 Node *caseresult;
2586 Assert(IsA(oldcasewhen, CaseWhen));
2588 /* Simplify this alternative's test condition */
2589 casecond =
2590 eval_const_expressions_mutator((Node *) oldcasewhen->expr,
2591 context);
2594 * If the test condition is constant FALSE (or NULL), then drop
2595 * this WHEN clause completely, without processing the result.
2597 if (casecond && IsA(casecond, Const))
2599 Const *const_input = (Const *) casecond;
2601 if (const_input->constisnull ||
2602 !DatumGetBool(const_input->constvalue))
2603 continue; /* drop alternative with FALSE condition */
2604 /* Else it's constant TRUE */
2605 const_true_cond = true;
2608 /* Simplify this alternative's result value */
2609 caseresult =
2610 eval_const_expressions_mutator((Node *) oldcasewhen->result,
2611 context);
2613 /* If non-constant test condition, emit a new WHEN node */
2614 if (!const_true_cond)
2616 CaseWhen *newcasewhen = makeNode(CaseWhen);
2618 newcasewhen->expr = (Expr *) casecond;
2619 newcasewhen->result = (Expr *) caseresult;
2620 newcasewhen->location = oldcasewhen->location;
2621 newargs = lappend(newargs, newcasewhen);
2622 continue;
2626 * Found a TRUE condition, so none of the remaining alternatives
2627 * can be reached. We treat the result as the default result.
2629 defresult = caseresult;
2630 break;
2633 /* Simplify the default result, unless we replaced it above */
2634 if (!const_true_cond)
2635 defresult =
2636 eval_const_expressions_mutator((Node *) caseexpr->defresult,
2637 context);
2639 context->case_val = save_case_val;
2641 /* If no non-FALSE alternatives, CASE reduces to the default result */
2642 if (newargs == NIL)
2643 return defresult;
2644 /* Otherwise we need a new CASE node */
2645 newcase = makeNode(CaseExpr);
2646 newcase->casetype = caseexpr->casetype;
2647 newcase->arg = (Expr *) newarg;
2648 newcase->args = newargs;
2649 newcase->defresult = (Expr *) defresult;
2650 newcase->location = caseexpr->location;
2651 return (Node *) newcase;
2653 if (IsA(node, CaseTestExpr))
2656 * If we know a constant test value for the current CASE construct,
2657 * substitute it for the placeholder. Else just return the
2658 * placeholder as-is.
2660 if (context->case_val)
2661 return copyObject(context->case_val);
2662 else
2663 return copyObject(node);
2665 if (IsA(node, ArrayExpr))
2667 ArrayExpr *arrayexpr = (ArrayExpr *) node;
2668 ArrayExpr *newarray;
2669 bool all_const = true;
2670 List *newelems;
2671 ListCell *element;
2673 newelems = NIL;
2674 foreach(element, arrayexpr->elements)
2676 Node *e;
2678 e = eval_const_expressions_mutator((Node *) lfirst(element),
2679 context);
2680 if (!IsA(e, Const))
2681 all_const = false;
2682 newelems = lappend(newelems, e);
2685 newarray = makeNode(ArrayExpr);
2686 newarray->array_typeid = arrayexpr->array_typeid;
2687 newarray->element_typeid = arrayexpr->element_typeid;
2688 newarray->elements = newelems;
2689 newarray->multidims = arrayexpr->multidims;
2690 newarray->location = arrayexpr->location;
2692 if (all_const)
2693 return (Node *) evaluate_expr((Expr *) newarray,
2694 newarray->array_typeid,
2695 exprTypmod(node));
2697 return (Node *) newarray;
2699 if (IsA(node, CoalesceExpr))
2701 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2702 CoalesceExpr *newcoalesce;
2703 List *newargs;
2704 ListCell *arg;
2706 newargs = NIL;
2707 foreach(arg, coalesceexpr->args)
2709 Node *e;
2711 e = eval_const_expressions_mutator((Node *) lfirst(arg),
2712 context);
2715 * We can remove null constants from the list. For a non-null
2716 * constant, if it has not been preceded by any other
2717 * non-null-constant expressions then that is the result.
2719 if (IsA(e, Const))
2721 if (((Const *) e)->constisnull)
2722 continue; /* drop null constant */
2723 if (newargs == NIL)
2724 return e; /* first expr */
2726 newargs = lappend(newargs, e);
2729 /* If all the arguments were constant null, the result is just null */
2730 if (newargs == NIL)
2731 return (Node *) makeNullConst(coalesceexpr->coalescetype, -1);
2733 newcoalesce = makeNode(CoalesceExpr);
2734 newcoalesce->coalescetype = coalesceexpr->coalescetype;
2735 newcoalesce->args = newargs;
2736 newcoalesce->location = coalesceexpr->location;
2737 return (Node *) newcoalesce;
2739 if (IsA(node, FieldSelect))
2742 * We can optimize field selection from a whole-row Var into a simple
2743 * Var. (This case won't be generated directly by the parser, because
2744 * ParseComplexProjection short-circuits it. But it can arise while
2745 * simplifying functions.) Also, we can optimize field selection from
2746 * a RowExpr construct.
2748 * We must however check that the declared type of the field is still
2749 * the same as when the FieldSelect was created --- this can change if
2750 * someone did ALTER COLUMN TYPE on the rowtype.
2752 FieldSelect *fselect = (FieldSelect *) node;
2753 FieldSelect *newfselect;
2754 Node *arg;
2756 arg = eval_const_expressions_mutator((Node *) fselect->arg,
2757 context);
2758 if (arg && IsA(arg, Var) &&
2759 ((Var *) arg)->varattno == InvalidAttrNumber)
2761 if (rowtype_field_matches(((Var *) arg)->vartype,
2762 fselect->fieldnum,
2763 fselect->resulttype,
2764 fselect->resulttypmod))
2765 return (Node *) makeVar(((Var *) arg)->varno,
2766 fselect->fieldnum,
2767 fselect->resulttype,
2768 fselect->resulttypmod,
2769 ((Var *) arg)->varlevelsup);
2771 if (arg && IsA(arg, RowExpr))
2773 RowExpr *rowexpr = (RowExpr *) arg;
2775 if (fselect->fieldnum > 0 &&
2776 fselect->fieldnum <= list_length(rowexpr->args))
2778 Node *fld = (Node *) list_nth(rowexpr->args,
2779 fselect->fieldnum - 1);
2781 if (rowtype_field_matches(rowexpr->row_typeid,
2782 fselect->fieldnum,
2783 fselect->resulttype,
2784 fselect->resulttypmod) &&
2785 fselect->resulttype == exprType(fld) &&
2786 fselect->resulttypmod == exprTypmod(fld))
2787 return fld;
2790 newfselect = makeNode(FieldSelect);
2791 newfselect->arg = (Expr *) arg;
2792 newfselect->fieldnum = fselect->fieldnum;
2793 newfselect->resulttype = fselect->resulttype;
2794 newfselect->resulttypmod = fselect->resulttypmod;
2795 return (Node *) newfselect;
2797 if (IsA(node, NullTest))
2799 NullTest *ntest = (NullTest *) node;
2800 NullTest *newntest;
2801 Node *arg;
2803 arg = eval_const_expressions_mutator((Node *) ntest->arg,
2804 context);
2805 if (arg && IsA(arg, RowExpr))
2807 RowExpr *rarg = (RowExpr *) arg;
2808 List *newargs = NIL;
2809 ListCell *l;
2812 * We break ROW(...) IS [NOT] NULL into separate tests on its
2813 * component fields. This form is usually more efficient to
2814 * evaluate, as well as being more amenable to optimization.
2816 foreach(l, rarg->args)
2818 Node *relem = (Node *) lfirst(l);
2821 * A constant field refutes the whole NullTest if it's of the
2822 * wrong nullness; else we can discard it.
2824 if (relem && IsA(relem, Const))
2826 Const *carg = (Const *) relem;
2828 if (carg->constisnull ?
2829 (ntest->nulltesttype == IS_NOT_NULL) :
2830 (ntest->nulltesttype == IS_NULL))
2831 return makeBoolConst(false, false);
2832 continue;
2834 newntest = makeNode(NullTest);
2835 newntest->arg = (Expr *) relem;
2836 newntest->nulltesttype = ntest->nulltesttype;
2837 newargs = lappend(newargs, newntest);
2839 /* If all the inputs were constants, result is TRUE */
2840 if (newargs == NIL)
2841 return makeBoolConst(true, false);
2842 /* If only one nonconst input, it's the result */
2843 if (list_length(newargs) == 1)
2844 return (Node *) linitial(newargs);
2845 /* Else we need an AND node */
2846 return (Node *) make_andclause(newargs);
2848 if (arg && IsA(arg, Const))
2850 Const *carg = (Const *) arg;
2851 bool result;
2853 switch (ntest->nulltesttype)
2855 case IS_NULL:
2856 result = carg->constisnull;
2857 break;
2858 case IS_NOT_NULL:
2859 result = !carg->constisnull;
2860 break;
2861 default:
2862 elog(ERROR, "unrecognized nulltesttype: %d",
2863 (int) ntest->nulltesttype);
2864 result = false; /* keep compiler quiet */
2865 break;
2868 return makeBoolConst(result, false);
2871 newntest = makeNode(NullTest);
2872 newntest->arg = (Expr *) arg;
2873 newntest->nulltesttype = ntest->nulltesttype;
2874 return (Node *) newntest;
2876 if (IsA(node, BooleanTest))
2878 BooleanTest *btest = (BooleanTest *) node;
2879 BooleanTest *newbtest;
2880 Node *arg;
2882 arg = eval_const_expressions_mutator((Node *) btest->arg,
2883 context);
2884 if (arg && IsA(arg, Const))
2886 Const *carg = (Const *) arg;
2887 bool result;
2889 switch (btest->booltesttype)
2891 case IS_TRUE:
2892 result = (!carg->constisnull &&
2893 DatumGetBool(carg->constvalue));
2894 break;
2895 case IS_NOT_TRUE:
2896 result = (carg->constisnull ||
2897 !DatumGetBool(carg->constvalue));
2898 break;
2899 case IS_FALSE:
2900 result = (!carg->constisnull &&
2901 !DatumGetBool(carg->constvalue));
2902 break;
2903 case IS_NOT_FALSE:
2904 result = (carg->constisnull ||
2905 DatumGetBool(carg->constvalue));
2906 break;
2907 case IS_UNKNOWN:
2908 result = carg->constisnull;
2909 break;
2910 case IS_NOT_UNKNOWN:
2911 result = !carg->constisnull;
2912 break;
2913 default:
2914 elog(ERROR, "unrecognized booltesttype: %d",
2915 (int) btest->booltesttype);
2916 result = false; /* keep compiler quiet */
2917 break;
2920 return makeBoolConst(result, false);
2923 newbtest = makeNode(BooleanTest);
2924 newbtest->arg = (Expr *) arg;
2925 newbtest->booltesttype = btest->booltesttype;
2926 return (Node *) newbtest;
2928 if (IsA(node, PlaceHolderVar) && context->estimate)
2931 * In estimation mode, just strip the PlaceHolderVar node altogether;
2932 * this amounts to estimating that the contained value won't be forced
2933 * to null by an outer join. In regular mode we just use the default
2934 * behavior (ie, simplify the expression but leave the PlaceHolderVar
2935 * node intact).
2937 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2939 return eval_const_expressions_mutator((Node *) phv->phexpr,
2940 context);
2944 * For any node type not handled above, we recurse using
2945 * expression_tree_mutator, which will copy the node unchanged but try to
2946 * simplify its arguments (if any) using this routine. For example: we
2947 * cannot eliminate an ArrayRef node, but we might be able to simplify
2948 * constant expressions in its subscripts.
2950 return expression_tree_mutator(node, eval_const_expressions_mutator,
2951 (void *) context);
2955 * Subroutine for eval_const_expressions: process arguments of an OR clause
2957 * This includes flattening of nested ORs as well as recursion to
2958 * eval_const_expressions to simplify the OR arguments.
2960 * After simplification, OR arguments are handled as follows:
2961 * non constant: keep
2962 * FALSE: drop (does not affect result)
2963 * TRUE: force result to TRUE
2964 * NULL: keep only one
2965 * We must keep one NULL input because ExecEvalOr returns NULL when no input
2966 * is TRUE and at least one is NULL. We don't actually include the NULL
2967 * here, that's supposed to be done by the caller.
2969 * The output arguments *haveNull and *forceTrue must be initialized FALSE
2970 * by the caller. They will be set TRUE if a null constant or true constant,
2971 * respectively, is detected anywhere in the argument list.
2973 static List *
2974 simplify_or_arguments(List *args,
2975 eval_const_expressions_context *context,
2976 bool *haveNull, bool *forceTrue)
2978 List *newargs = NIL;
2979 List *unprocessed_args;
2982 * Since the parser considers OR to be a binary operator, long OR lists
2983 * become deeply nested expressions. We must flatten these into long
2984 * argument lists of a single OR operator. To avoid blowing out the stack
2985 * with recursion of eval_const_expressions, we resort to some tenseness
2986 * here: we keep a list of not-yet-processed inputs, and handle flattening
2987 * of nested ORs by prepending to the to-do list instead of recursing.
2989 unprocessed_args = list_copy(args);
2990 while (unprocessed_args)
2992 Node *arg = (Node *) linitial(unprocessed_args);
2994 unprocessed_args = list_delete_first(unprocessed_args);
2996 /* flatten nested ORs as per above comment */
2997 if (or_clause(arg))
2999 List *subargs = list_copy(((BoolExpr *) arg)->args);
3001 /* overly tense code to avoid leaking unused list header */
3002 if (!unprocessed_args)
3003 unprocessed_args = subargs;
3004 else
3006 List *oldhdr = unprocessed_args;
3008 unprocessed_args = list_concat(subargs, unprocessed_args);
3009 pfree(oldhdr);
3011 continue;
3014 /* If it's not an OR, simplify it */
3015 arg = eval_const_expressions_mutator(arg, context);
3018 * It is unlikely but not impossible for simplification of a non-OR
3019 * clause to produce an OR. Recheck, but don't be too tense about it
3020 * since it's not a mainstream case. In particular we don't worry
3021 * about const-simplifying the input twice.
3023 if (or_clause(arg))
3025 List *subargs = list_copy(((BoolExpr *) arg)->args);
3027 unprocessed_args = list_concat(subargs, unprocessed_args);
3028 continue;
3032 * OK, we have a const-simplified non-OR argument. Process it per
3033 * comments above.
3035 if (IsA(arg, Const))
3037 Const *const_input = (Const *) arg;
3039 if (const_input->constisnull)
3040 *haveNull = true;
3041 else if (DatumGetBool(const_input->constvalue))
3043 *forceTrue = true;
3046 * Once we detect a TRUE result we can just exit the loop
3047 * immediately. However, if we ever add a notion of
3048 * non-removable functions, we'd need to keep scanning.
3050 return NIL;
3052 /* otherwise, we can drop the constant-false input */
3053 continue;
3056 /* else emit the simplified arg into the result list */
3057 newargs = lappend(newargs, arg);
3060 return newargs;
3064 * Subroutine for eval_const_expressions: process arguments of an AND clause
3066 * This includes flattening of nested ANDs as well as recursion to
3067 * eval_const_expressions to simplify the AND arguments.
3069 * After simplification, AND arguments are handled as follows:
3070 * non constant: keep
3071 * TRUE: drop (does not affect result)
3072 * FALSE: force result to FALSE
3073 * NULL: keep only one
3074 * We must keep one NULL input because ExecEvalAnd returns NULL when no input
3075 * is FALSE and at least one is NULL. We don't actually include the NULL
3076 * here, that's supposed to be done by the caller.
3078 * The output arguments *haveNull and *forceFalse must be initialized FALSE
3079 * by the caller. They will be set TRUE if a null constant or false constant,
3080 * respectively, is detected anywhere in the argument list.
3082 static List *
3083 simplify_and_arguments(List *args,
3084 eval_const_expressions_context *context,
3085 bool *haveNull, bool *forceFalse)
3087 List *newargs = NIL;
3088 List *unprocessed_args;
3090 /* See comments in simplify_or_arguments */
3091 unprocessed_args = list_copy(args);
3092 while (unprocessed_args)
3094 Node *arg = (Node *) linitial(unprocessed_args);
3096 unprocessed_args = list_delete_first(unprocessed_args);
3098 /* flatten nested ANDs as per above comment */
3099 if (and_clause(arg))
3101 List *subargs = list_copy(((BoolExpr *) arg)->args);
3103 /* overly tense code to avoid leaking unused list header */
3104 if (!unprocessed_args)
3105 unprocessed_args = subargs;
3106 else
3108 List *oldhdr = unprocessed_args;
3110 unprocessed_args = list_concat(subargs, unprocessed_args);
3111 pfree(oldhdr);
3113 continue;
3116 /* If it's not an AND, simplify it */
3117 arg = eval_const_expressions_mutator(arg, context);
3120 * It is unlikely but not impossible for simplification of a non-AND
3121 * clause to produce an AND. Recheck, but don't be too tense about it
3122 * since it's not a mainstream case. In particular we don't worry
3123 * about const-simplifying the input twice.
3125 if (and_clause(arg))
3127 List *subargs = list_copy(((BoolExpr *) arg)->args);
3129 unprocessed_args = list_concat(subargs, unprocessed_args);
3130 continue;
3134 * OK, we have a const-simplified non-AND argument. Process it per
3135 * comments above.
3137 if (IsA(arg, Const))
3139 Const *const_input = (Const *) arg;
3141 if (const_input->constisnull)
3142 *haveNull = true;
3143 else if (!DatumGetBool(const_input->constvalue))
3145 *forceFalse = true;
3148 * Once we detect a FALSE result we can just exit the loop
3149 * immediately. However, if we ever add a notion of
3150 * non-removable functions, we'd need to keep scanning.
3152 return NIL;
3154 /* otherwise, we can drop the constant-true input */
3155 continue;
3158 /* else emit the simplified arg into the result list */
3159 newargs = lappend(newargs, arg);
3162 return newargs;
3166 * Subroutine for eval_const_expressions: try to simplify boolean equality
3168 * Input is the list of simplified arguments to the operator.
3169 * Returns a simplified expression if successful, or NULL if cannot
3170 * simplify the expression.
3172 * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x".
3173 * This is only marginally useful in itself, but doing it in constant folding
3174 * ensures that we will recognize the two forms as being equivalent in, for
3175 * example, partial index matching.
3177 * We come here only if simplify_function has failed; therefore we cannot
3178 * see two constant inputs, nor a constant-NULL input.
3180 static Expr *
3181 simplify_boolean_equality(List *args)
3183 Expr *leftop;
3184 Expr *rightop;
3186 Assert(list_length(args) == 2);
3187 leftop = linitial(args);
3188 rightop = lsecond(args);
3189 if (leftop && IsA(leftop, Const))
3191 Assert(!((Const *) leftop)->constisnull);
3192 if (DatumGetBool(((Const *) leftop)->constvalue))
3193 return rightop; /* true = foo */
3194 else
3195 return make_notclause(rightop); /* false = foo */
3197 if (rightop && IsA(rightop, Const))
3199 Assert(!((Const *) rightop)->constisnull);
3200 if (DatumGetBool(((Const *) rightop)->constvalue))
3201 return leftop; /* foo = true */
3202 else
3203 return make_notclause(leftop); /* foo = false */
3205 return NULL;
3209 * Subroutine for eval_const_expressions: try to simplify a function call
3210 * (which might originally have been an operator; we don't care)
3212 * Inputs are the function OID, actual result type OID (which is needed for
3213 * polymorphic functions) and typmod, and the pre-simplified argument list;
3214 * also the context data for eval_const_expressions.
3216 * Returns a simplified expression if successful, or NULL if cannot
3217 * simplify the function call.
3219 * This function is also responsible for adding any default argument
3220 * expressions onto the function argument list; which is a bit grotty,
3221 * but it avoids an extra fetch of the function's pg_proc tuple. For this
3222 * reason, the args list is pass-by-reference, and it may get modified
3223 * even if simplification fails.
3225 static Expr *
3226 simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
3227 List **args,
3228 bool allow_inline,
3229 eval_const_expressions_context *context)
3231 HeapTuple func_tuple;
3232 Expr *newexpr;
3235 * We have two strategies for simplification: either execute the function
3236 * to deliver a constant result, or expand in-line the body of the
3237 * function definition (which only works for simple SQL-language
3238 * functions, but that is a common case). In either case we need access
3239 * to the function's pg_proc tuple, so fetch it just once to use in both
3240 * attempts.
3242 func_tuple = SearchSysCache(PROCOID,
3243 ObjectIdGetDatum(funcid),
3244 0, 0, 0);
3245 if (!HeapTupleIsValid(func_tuple))
3246 elog(ERROR, "cache lookup failed for function %u", funcid);
3248 /* While we have the tuple, check if we need to add defaults */
3249 if (((Form_pg_proc) GETSTRUCT(func_tuple))->pronargs > list_length(*args))
3250 *args = add_function_defaults(*args, result_type, func_tuple, context);
3252 newexpr = evaluate_function(funcid, result_type, result_typmod, *args,
3253 func_tuple, context);
3255 if (!newexpr && allow_inline)
3256 newexpr = inline_function(funcid, result_type, *args,
3257 func_tuple, context);
3259 ReleaseSysCache(func_tuple);
3261 return newexpr;
3265 * add_function_defaults: add missing function arguments from its defaults
3267 * It is possible for some of the defaulted arguments to be polymorphic;
3268 * therefore we can't assume that the default expressions have the correct
3269 * data types already. We have to re-resolve polymorphics and do coercion
3270 * just like the parser did.
3272 static List *
3273 add_function_defaults(List *args, Oid result_type, HeapTuple func_tuple,
3274 eval_const_expressions_context *context)
3276 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3277 int nargsprovided = list_length(args);
3278 Datum proargdefaults;
3279 bool isnull;
3280 char *str;
3281 List *defaults;
3282 int ndelete;
3283 int nargs;
3284 Oid actual_arg_types[FUNC_MAX_ARGS];
3285 Oid declared_arg_types[FUNC_MAX_ARGS];
3286 Oid rettype;
3287 ListCell *lc;
3289 /* The error cases here shouldn't happen, but check anyway */
3290 proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
3291 Anum_pg_proc_proargdefaults,
3292 &isnull);
3293 if (isnull)
3294 elog(ERROR, "not enough default arguments");
3295 str = TextDatumGetCString(proargdefaults);
3296 defaults = (List *) stringToNode(str);
3297 Assert(IsA(defaults, List));
3298 pfree(str);
3299 /* Delete any unused defaults from the list */
3300 ndelete = nargsprovided + list_length(defaults) - funcform->pronargs;
3301 if (ndelete < 0)
3302 elog(ERROR, "not enough default arguments");
3303 while (ndelete-- > 0)
3304 defaults = list_delete_first(defaults);
3305 /* And form the combined argument list */
3306 args = list_concat(args, defaults);
3307 Assert(list_length(args) == funcform->pronargs);
3310 * The next part should be a no-op if there are no polymorphic arguments,
3311 * but we do it anyway to be sure.
3313 if (list_length(args) > FUNC_MAX_ARGS)
3314 elog(ERROR, "too many function arguments");
3315 nargs = 0;
3316 foreach(lc, args)
3318 actual_arg_types[nargs++] = exprType((Node *) lfirst(lc));
3320 memcpy(declared_arg_types, funcform->proargtypes.values,
3321 funcform->pronargs * sizeof(Oid));
3322 rettype = enforce_generic_type_consistency(actual_arg_types,
3323 declared_arg_types,
3324 nargs,
3325 funcform->prorettype,
3326 false);
3327 /* let's just check we got the same answer as the parser did ... */
3328 if (rettype != result_type)
3329 elog(ERROR, "function's resolved result type changed during planning");
3331 /* perform any necessary typecasting of arguments */
3332 make_fn_arguments(NULL, args, actual_arg_types, declared_arg_types);
3335 * Lastly, we have to recursively simplify the arguments we just added
3336 * (but don't recurse on the ones passed in, as we already did those).
3337 * This isn't merely an optimization, it's *necessary* since there could
3338 * be functions with defaulted arguments down in there.
3340 foreach(lc, args)
3342 if (nargsprovided-- > 0)
3343 continue; /* skip original arg positions */
3344 lfirst(lc) = eval_const_expressions_mutator((Node *) lfirst(lc),
3345 context);
3348 return args;
3352 * evaluate_function: try to pre-evaluate a function call
3354 * We can do this if the function is strict and has any constant-null inputs
3355 * (just return a null constant), or if the function is immutable and has all
3356 * constant inputs (call it and return the result as a Const node). In
3357 * estimation mode we are willing to pre-evaluate stable functions too.
3359 * Returns a simplified expression if successful, or NULL if cannot
3360 * simplify the function.
3362 static Expr *
3363 evaluate_function(Oid funcid, Oid result_type, int32 result_typmod, List *args,
3364 HeapTuple func_tuple,
3365 eval_const_expressions_context *context)
3367 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3368 bool has_nonconst_input = false;
3369 bool has_null_input = false;
3370 ListCell *arg;
3371 FuncExpr *newexpr;
3374 * Can't simplify if it returns a set.
3376 if (funcform->proretset)
3377 return NULL;
3380 * Can't simplify if it returns RECORD. The immediate problem is that it
3381 * will be needing an expected tupdesc which we can't supply here.
3383 * In the case where it has OUT parameters, it could get by without an
3384 * expected tupdesc, but we still have issues: get_expr_result_type()
3385 * doesn't know how to extract type info from a RECORD constant, and in
3386 * the case of a NULL function result there doesn't seem to be any clean
3387 * way to fix that. In view of the likelihood of there being still other
3388 * gotchas, seems best to leave the function call unreduced.
3390 if (funcform->prorettype == RECORDOID)
3391 return NULL;
3394 * Check for constant inputs and especially constant-NULL inputs.
3396 foreach(arg, args)
3398 if (IsA(lfirst(arg), Const))
3399 has_null_input |= ((Const *) lfirst(arg))->constisnull;
3400 else
3401 has_nonconst_input = true;
3405 * If the function is strict and has a constant-NULL input, it will never
3406 * be called at all, so we can replace the call by a NULL constant, even
3407 * if there are other inputs that aren't constant, and even if the
3408 * function is not otherwise immutable.
3410 if (funcform->proisstrict && has_null_input)
3411 return (Expr *) makeNullConst(result_type, result_typmod);
3414 * Otherwise, can simplify only if all inputs are constants. (For a
3415 * non-strict function, constant NULL inputs are treated the same as
3416 * constant non-NULL inputs.)
3418 if (has_nonconst_input)
3419 return NULL;
3422 * Ordinarily we are only allowed to simplify immutable functions. But for
3423 * purposes of estimation, we consider it okay to simplify functions that
3424 * are merely stable; the risk that the result might change from planning
3425 * time to execution time is worth taking in preference to not being able
3426 * to estimate the value at all.
3428 if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
3429 /* okay */ ;
3430 else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
3431 /* okay */ ;
3432 else
3433 return NULL;
3436 * OK, looks like we can simplify this operator/function.
3438 * Build a new FuncExpr node containing the already-simplified arguments.
3440 newexpr = makeNode(FuncExpr);
3441 newexpr->funcid = funcid;
3442 newexpr->funcresulttype = result_type;
3443 newexpr->funcretset = false;
3444 newexpr->funcformat = COERCE_DONTCARE; /* doesn't matter */
3445 newexpr->args = args;
3446 newexpr->location = -1;
3448 return evaluate_expr((Expr *) newexpr, result_type, result_typmod);
3452 * inline_function: try to expand a function call inline
3454 * If the function is a sufficiently simple SQL-language function
3455 * (just "SELECT expression"), then we can inline it and avoid the rather
3456 * high per-call overhead of SQL functions. Furthermore, this can expose
3457 * opportunities for constant-folding within the function expression.
3459 * We have to beware of some special cases however. A directly or
3460 * indirectly recursive function would cause us to recurse forever,
3461 * so we keep track of which functions we are already expanding and
3462 * do not re-expand them. Also, if a parameter is used more than once
3463 * in the SQL-function body, we require it not to contain any volatile
3464 * functions (volatiles might deliver inconsistent answers) nor to be
3465 * unreasonably expensive to evaluate. The expensiveness check not only
3466 * prevents us from doing multiple evaluations of an expensive parameter
3467 * at runtime, but is a safety value to limit growth of an expression due
3468 * to repeated inlining.
3470 * We must also beware of changing the volatility or strictness status of
3471 * functions by inlining them.
3473 * Returns a simplified expression if successful, or NULL if cannot
3474 * simplify the function.
3476 static Expr *
3477 inline_function(Oid funcid, Oid result_type, List *args,
3478 HeapTuple func_tuple,
3479 eval_const_expressions_context *context)
3481 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3482 Oid *argtypes;
3483 char *src;
3484 Datum tmp;
3485 bool isNull;
3486 MemoryContext oldcxt;
3487 MemoryContext mycxt;
3488 ErrorContextCallback sqlerrcontext;
3489 List *raw_parsetree_list;
3490 Query *querytree;
3491 Node *newexpr;
3492 int *usecounts;
3493 ListCell *arg;
3494 int i;
3497 * Forget it if the function is not SQL-language or has other showstopper
3498 * properties. (The nargs check is just paranoia.)
3500 if (funcform->prolang != SQLlanguageId ||
3501 funcform->prosecdef ||
3502 funcform->proretset ||
3503 !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
3504 funcform->pronargs != list_length(args))
3505 return NULL;
3507 /* Check for recursive function, and give up trying to expand if so */
3508 if (list_member_oid(context->active_fns, funcid))
3509 return NULL;
3511 /* Check permission to call function (fail later, if not) */
3512 if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
3513 return NULL;
3516 * Setup error traceback support for ereport(). This is so that we can
3517 * finger the function that bad information came from.
3519 sqlerrcontext.callback = sql_inline_error_callback;
3520 sqlerrcontext.arg = func_tuple;
3521 sqlerrcontext.previous = error_context_stack;
3522 error_context_stack = &sqlerrcontext;
3525 * Make a temporary memory context, so that we don't leak all the stuff
3526 * that parsing might create.
3528 mycxt = AllocSetContextCreate(CurrentMemoryContext,
3529 "inline_function",
3530 ALLOCSET_DEFAULT_MINSIZE,
3531 ALLOCSET_DEFAULT_INITSIZE,
3532 ALLOCSET_DEFAULT_MAXSIZE);
3533 oldcxt = MemoryContextSwitchTo(mycxt);
3535 /* Check for polymorphic arguments, and substitute actual arg types */
3536 argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
3537 memcpy(argtypes, funcform->proargtypes.values,
3538 funcform->pronargs * sizeof(Oid));
3539 for (i = 0; i < funcform->pronargs; i++)
3541 if (IsPolymorphicType(argtypes[i]))
3543 argtypes[i] = exprType((Node *) list_nth(args, i));
3547 /* Fetch and parse the function body */
3548 tmp = SysCacheGetAttr(PROCOID,
3549 func_tuple,
3550 Anum_pg_proc_prosrc,
3551 &isNull);
3552 if (isNull)
3553 elog(ERROR, "null prosrc for function %u", funcid);
3554 src = TextDatumGetCString(tmp);
3557 * We just do parsing and parse analysis, not rewriting, because rewriting
3558 * will not affect table-free-SELECT-only queries, which is all that we
3559 * care about. Also, we can punt as soon as we detect more than one
3560 * command in the function body.
3562 raw_parsetree_list = pg_parse_query(src);
3563 if (list_length(raw_parsetree_list) != 1)
3564 goto fail;
3566 querytree = parse_analyze(linitial(raw_parsetree_list), src,
3567 argtypes, funcform->pronargs);
3570 * The single command must be a simple "SELECT expression".
3572 if (!IsA(querytree, Query) ||
3573 querytree->commandType != CMD_SELECT ||
3574 querytree->utilityStmt ||
3575 querytree->intoClause ||
3576 querytree->hasAggs ||
3577 querytree->hasWindowFuncs ||
3578 querytree->hasSubLinks ||
3579 querytree->cteList ||
3580 querytree->rtable ||
3581 querytree->jointree->fromlist ||
3582 querytree->jointree->quals ||
3583 querytree->groupClause ||
3584 querytree->havingQual ||
3585 querytree->windowClause ||
3586 querytree->distinctClause ||
3587 querytree->sortClause ||
3588 querytree->limitOffset ||
3589 querytree->limitCount ||
3590 querytree->setOperations ||
3591 list_length(querytree->targetList) != 1)
3592 goto fail;
3595 * Make sure the function (still) returns what it's declared to. This
3596 * will raise an error if wrong, but that's okay since the function would
3597 * fail at runtime anyway. Note that check_sql_fn_retval will also insert
3598 * a RelabelType if needed to make the tlist expression match the declared
3599 * type of the function.
3601 * Note: we do not try this until we have verified that no rewriting was
3602 * needed; that's probably not important, but let's be careful.
3604 if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
3605 true, NULL))
3606 goto fail; /* reject whole-tuple-result cases */
3608 /* Now we can grab the tlist expression */
3609 newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
3611 /* Assert that check_sql_fn_retval did the right thing */
3612 Assert(exprType(newexpr) == result_type);
3615 * Additional validity checks on the expression. It mustn't return a set,
3616 * and it mustn't be more volatile than the surrounding function (this is
3617 * to avoid breaking hacks that involve pretending a function is immutable
3618 * when it really ain't). If the surrounding function is declared strict,
3619 * then the expression must contain only strict constructs and must use
3620 * all of the function parameters (this is overkill, but an exact analysis
3621 * is hard).
3623 if (expression_returns_set(newexpr))
3624 goto fail;
3626 if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
3627 contain_mutable_functions(newexpr))
3628 goto fail;
3629 else if (funcform->provolatile == PROVOLATILE_STABLE &&
3630 contain_volatile_functions(newexpr))
3631 goto fail;
3633 if (funcform->proisstrict &&
3634 contain_nonstrict_functions(newexpr))
3635 goto fail;
3638 * We may be able to do it; there are still checks on parameter usage to
3639 * make, but those are most easily done in combination with the actual
3640 * substitution of the inputs. So start building expression with inputs
3641 * substituted.
3643 usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
3644 newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
3645 args, usecounts);
3647 /* Now check for parameter usage */
3648 i = 0;
3649 foreach(arg, args)
3651 Node *param = lfirst(arg);
3653 if (usecounts[i] == 0)
3655 /* Param not used at all: uncool if func is strict */
3656 if (funcform->proisstrict)
3657 goto fail;
3659 else if (usecounts[i] != 1)
3661 /* Param used multiple times: uncool if expensive or volatile */
3662 QualCost eval_cost;
3665 * We define "expensive" as "contains any subplan or more than 10
3666 * operators". Note that the subplan search has to be done
3667 * explicitly, since cost_qual_eval() will barf on unplanned
3668 * subselects.
3670 if (contain_subplans(param))
3671 goto fail;
3672 cost_qual_eval(&eval_cost, list_make1(param), NULL);
3673 if (eval_cost.startup + eval_cost.per_tuple >
3674 10 * cpu_operator_cost)
3675 goto fail;
3678 * Check volatility last since this is more expensive than the
3679 * above tests
3681 if (contain_volatile_functions(param))
3682 goto fail;
3684 i++;
3688 * Whew --- we can make the substitution. Copy the modified expression
3689 * out of the temporary memory context, and clean up.
3691 MemoryContextSwitchTo(oldcxt);
3693 newexpr = copyObject(newexpr);
3695 MemoryContextDelete(mycxt);
3698 * Since there is now no trace of the function in the plan tree, we
3699 * must explicitly record the plan's dependency on the function.
3701 if (context->glob)
3702 record_plan_function_dependency(context->glob, funcid);
3705 * Recursively try to simplify the modified expression. Here we must add
3706 * the current function to the context list of active functions.
3708 context->active_fns = lcons_oid(funcid, context->active_fns);
3709 newexpr = eval_const_expressions_mutator(newexpr, context);
3710 context->active_fns = list_delete_first(context->active_fns);
3712 error_context_stack = sqlerrcontext.previous;
3714 return (Expr *) newexpr;
3716 /* Here if func is not inlinable: release temp memory and return NULL */
3717 fail:
3718 MemoryContextSwitchTo(oldcxt);
3719 MemoryContextDelete(mycxt);
3720 error_context_stack = sqlerrcontext.previous;
3722 return NULL;
3726 * Replace Param nodes by appropriate actual parameters
3728 static Node *
3729 substitute_actual_parameters(Node *expr, int nargs, List *args,
3730 int *usecounts)
3732 substitute_actual_parameters_context context;
3734 context.nargs = nargs;
3735 context.args = args;
3736 context.usecounts = usecounts;
3738 return substitute_actual_parameters_mutator(expr, &context);
3741 static Node *
3742 substitute_actual_parameters_mutator(Node *node,
3743 substitute_actual_parameters_context *context)
3745 if (node == NULL)
3746 return NULL;
3747 if (IsA(node, Param))
3749 Param *param = (Param *) node;
3751 if (param->paramkind != PARAM_EXTERN)
3752 elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
3753 if (param->paramid <= 0 || param->paramid > context->nargs)
3754 elog(ERROR, "invalid paramid: %d", param->paramid);
3756 /* Count usage of parameter */
3757 context->usecounts[param->paramid - 1]++;
3759 /* Select the appropriate actual arg and replace the Param with it */
3760 /* We don't need to copy at this time (it'll get done later) */
3761 return list_nth(context->args, param->paramid - 1);
3763 return expression_tree_mutator(node, substitute_actual_parameters_mutator,
3764 (void *) context);
3768 * error context callback to let us supply a call-stack traceback
3770 static void
3771 sql_inline_error_callback(void *arg)
3773 HeapTuple func_tuple = (HeapTuple) arg;
3774 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3775 int syntaxerrposition;
3777 /* If it's a syntax error, convert to internal syntax error report */
3778 syntaxerrposition = geterrposition();
3779 if (syntaxerrposition > 0)
3781 bool isnull;
3782 Datum tmp;
3783 char *prosrc;
3785 tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
3786 &isnull);
3787 if (isnull)
3788 elog(ERROR, "null prosrc");
3789 prosrc = TextDatumGetCString(tmp);
3790 errposition(0);
3791 internalerrposition(syntaxerrposition);
3792 internalerrquery(prosrc);
3795 errcontext("SQL function \"%s\" during inlining",
3796 NameStr(funcform->proname));
3800 * evaluate_expr: pre-evaluate a constant expression
3802 * We use the executor's routine ExecEvalExpr() to avoid duplication of
3803 * code and ensure we get the same result as the executor would get.
3805 static Expr *
3806 evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod)
3808 EState *estate;
3809 ExprState *exprstate;
3810 MemoryContext oldcontext;
3811 Datum const_val;
3812 bool const_is_null;
3813 int16 resultTypLen;
3814 bool resultTypByVal;
3817 * To use the executor, we need an EState.
3819 estate = CreateExecutorState();
3821 /* We can use the estate's working context to avoid memory leaks. */
3822 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3824 /* Make sure any opfuncids are filled in. */
3825 fix_opfuncids((Node *) expr);
3828 * Prepare expr for execution. (Note: we can't use ExecPrepareExpr
3829 * because it'd result in recursively invoking eval_const_expressions.)
3831 exprstate = ExecInitExpr(expr, NULL);
3834 * And evaluate it.
3836 * It is OK to use a default econtext because none of the ExecEvalExpr()
3837 * code used in this situation will use econtext. That might seem
3838 * fortuitous, but it's not so unreasonable --- a constant expression does
3839 * not depend on context, by definition, n'est ce pas?
3841 const_val = ExecEvalExprSwitchContext(exprstate,
3842 GetPerTupleExprContext(estate),
3843 &const_is_null, NULL);
3845 /* Get info needed about result datatype */
3846 get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
3848 /* Get back to outer memory context */
3849 MemoryContextSwitchTo(oldcontext);
3852 * Must copy result out of sub-context used by expression eval.
3854 * Also, if it's varlena, forcibly detoast it. This protects us against
3855 * storing TOAST pointers into plans that might outlive the referenced
3856 * data.
3858 if (!const_is_null)
3860 if (resultTypLen == -1)
3861 const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
3862 else
3863 const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
3866 /* Release all the junk we just created */
3867 FreeExecutorState(estate);
3870 * Make the constant result node.
3872 return (Expr *) makeConst(result_type, result_typmod, resultTypLen,
3873 const_val, const_is_null,
3874 resultTypByVal);
3879 * inline_set_returning_function
3880 * Attempt to "inline" a set-returning function in the FROM clause.
3882 * "rte" is an RTE_FUNCTION rangetable entry. If it represents a call of a
3883 * set-returning SQL function that can safely be inlined, expand the function
3884 * and return the substitute Query structure. Otherwise, return NULL.
3886 * This has a good deal of similarity to inline_function(), but that's
3887 * for the non-set-returning case, and there are enough differences to
3888 * justify separate functions.
3890 Query *
3891 inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
3893 FuncExpr *fexpr;
3894 HeapTuple func_tuple;
3895 Form_pg_proc funcform;
3896 Oid *argtypes;
3897 char *src;
3898 Datum tmp;
3899 bool isNull;
3900 MemoryContext oldcxt;
3901 MemoryContext mycxt;
3902 ErrorContextCallback sqlerrcontext;
3903 List *raw_parsetree_list;
3904 List *querytree_list;
3905 Query *querytree;
3906 int i;
3908 Assert(rte->rtekind == RTE_FUNCTION);
3911 * It doesn't make a lot of sense for a SQL SRF to refer to itself
3912 * in its own FROM clause, since that must cause infinite recursion
3913 * at runtime. It will cause this code to recurse too, so check
3914 * for stack overflow. (There's no need to do more.)
3916 check_stack_depth();
3918 /* Fail if FROM item isn't a simple FuncExpr */
3919 fexpr = (FuncExpr *) rte->funcexpr;
3920 if (fexpr == NULL || !IsA(fexpr, FuncExpr))
3921 return NULL;
3924 * The function must be declared to return a set, else inlining would
3925 * change the results if the contained SELECT didn't return exactly
3926 * one row.
3928 if (!fexpr->funcretset)
3929 return NULL;
3932 * Refuse to inline if the arguments contain any volatile functions or
3933 * sub-selects. Volatile functions are rejected because inlining may
3934 * result in the arguments being evaluated multiple times, risking a
3935 * change in behavior. Sub-selects are rejected partly for implementation
3936 * reasons (pushing them down another level might change their behavior)
3937 * and partly because they're likely to be expensive and so multiple
3938 * evaluation would be bad.
3940 if (contain_volatile_functions((Node *) fexpr->args) ||
3941 contain_subplans((Node *) fexpr->args))
3942 return NULL;
3944 /* Check permission to call function (fail later, if not) */
3945 if (pg_proc_aclcheck(fexpr->funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
3946 return NULL;
3949 * OK, let's take a look at the function's pg_proc entry.
3951 func_tuple = SearchSysCache(PROCOID,
3952 ObjectIdGetDatum(fexpr->funcid),
3953 0, 0, 0);
3954 if (!HeapTupleIsValid(func_tuple))
3955 elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3956 funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3959 * Forget it if the function is not SQL-language or has other showstopper
3960 * properties. In particular it mustn't be declared STRICT, since we
3961 * couldn't enforce that. It also mustn't be VOLATILE, because that is
3962 * supposed to cause it to be executed with its own snapshot, rather than
3963 * sharing the snapshot of the calling query. (The nargs check is just
3964 * paranoia, ditto rechecking proretset.)
3966 if (funcform->prolang != SQLlanguageId ||
3967 funcform->proisstrict ||
3968 funcform->provolatile == PROVOLATILE_VOLATILE ||
3969 funcform->prosecdef ||
3970 !funcform->proretset ||
3971 !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
3972 funcform->pronargs != list_length(fexpr->args))
3974 ReleaseSysCache(func_tuple);
3975 return NULL;
3979 * Setup error traceback support for ereport(). This is so that we can
3980 * finger the function that bad information came from.
3982 sqlerrcontext.callback = sql_inline_error_callback;
3983 sqlerrcontext.arg = func_tuple;
3984 sqlerrcontext.previous = error_context_stack;
3985 error_context_stack = &sqlerrcontext;
3988 * Make a temporary memory context, so that we don't leak all the stuff
3989 * that parsing might create.
3991 mycxt = AllocSetContextCreate(CurrentMemoryContext,
3992 "inline_set_returning_function",
3993 ALLOCSET_DEFAULT_MINSIZE,
3994 ALLOCSET_DEFAULT_INITSIZE,
3995 ALLOCSET_DEFAULT_MAXSIZE);
3996 oldcxt = MemoryContextSwitchTo(mycxt);
3998 /* Check for polymorphic arguments, and substitute actual arg types */
3999 argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
4000 memcpy(argtypes, funcform->proargtypes.values,
4001 funcform->pronargs * sizeof(Oid));
4002 for (i = 0; i < funcform->pronargs; i++)
4004 if (IsPolymorphicType(argtypes[i]))
4006 argtypes[i] = exprType((Node *) list_nth(fexpr->args, i));
4010 /* Fetch and parse the function body */
4011 tmp = SysCacheGetAttr(PROCOID,
4012 func_tuple,
4013 Anum_pg_proc_prosrc,
4014 &isNull);
4015 if (isNull)
4016 elog(ERROR, "null prosrc for function %u", fexpr->funcid);
4017 src = TextDatumGetCString(tmp);
4020 * Parse, analyze, and rewrite (unlike inline_function(), we can't
4021 * skip rewriting here). We can fail as soon as we find more than
4022 * one query, though.
4024 raw_parsetree_list = pg_parse_query(src);
4025 if (list_length(raw_parsetree_list) != 1)
4026 goto fail;
4028 querytree_list = pg_analyze_and_rewrite(linitial(raw_parsetree_list), src,
4029 argtypes, funcform->pronargs);
4030 if (list_length(querytree_list) != 1)
4031 goto fail;
4032 querytree = linitial(querytree_list);
4035 * The single command must be a regular results-returning SELECT.
4037 if (!IsA(querytree, Query) ||
4038 querytree->commandType != CMD_SELECT ||
4039 querytree->utilityStmt ||
4040 querytree->intoClause)
4041 goto fail;
4044 * Make sure the function (still) returns what it's declared to. This
4045 * will raise an error if wrong, but that's okay since the function would
4046 * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4047 * RelabelType(s) if needed to make the tlist expression(s) match the
4048 * declared type of the function.
4050 * If the function returns a composite type, don't inline unless the
4051 * check shows it's returning a whole tuple result; otherwise what
4052 * it's returning is a single composite column which is not what we need.
4054 if (!check_sql_fn_retval(fexpr->funcid, fexpr->funcresulttype,
4055 querytree_list,
4056 true, NULL) &&
4057 (get_typtype(fexpr->funcresulttype) == TYPTYPE_COMPOSITE ||
4058 fexpr->funcresulttype == RECORDOID))
4059 goto fail; /* reject not-whole-tuple-result cases */
4062 * If it returns RECORD, we have to check against the column type list
4063 * provided in the RTE; check_sql_fn_retval can't do that. (If no match,
4064 * we just fail to inline, rather than complaining; see notes for
4065 * tlist_matches_coltypelist.)
4067 if (fexpr->funcresulttype == RECORDOID &&
4068 !tlist_matches_coltypelist(querytree->targetList, rte->funccoltypes))
4069 goto fail;
4072 * Looks good --- substitute parameters into the query.
4074 querytree = substitute_actual_srf_parameters(querytree,
4075 funcform->pronargs,
4076 fexpr->args);
4079 * Copy the modified query out of the temporary memory context,
4080 * and clean up.
4082 MemoryContextSwitchTo(oldcxt);
4084 querytree = copyObject(querytree);
4086 MemoryContextDelete(mycxt);
4087 error_context_stack = sqlerrcontext.previous;
4088 ReleaseSysCache(func_tuple);
4091 * Since there is now no trace of the function in the plan tree, we
4092 * must explicitly record the plan's dependency on the function.
4094 record_plan_function_dependency(root->glob, fexpr->funcid);
4096 return querytree;
4098 /* Here if func is not inlinable: release temp memory and return NULL */
4099 fail:
4100 MemoryContextSwitchTo(oldcxt);
4101 MemoryContextDelete(mycxt);
4102 error_context_stack = sqlerrcontext.previous;
4103 ReleaseSysCache(func_tuple);
4105 return NULL;
4109 * Replace Param nodes by appropriate actual parameters
4111 * This is just enough different from substitute_actual_parameters()
4112 * that it needs its own code.
4114 static Query *
4115 substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
4117 substitute_actual_srf_parameters_context context;
4119 context.nargs = nargs;
4120 context.args = args;
4121 context.sublevels_up = 1;
4123 return query_tree_mutator(expr,
4124 substitute_actual_srf_parameters_mutator,
4125 &context,
4129 static Node *
4130 substitute_actual_srf_parameters_mutator(Node *node,
4131 substitute_actual_srf_parameters_context *context)
4133 Node *result;
4135 if (node == NULL)
4136 return NULL;
4137 if (IsA(node, Query))
4139 context->sublevels_up++;
4140 result = (Node *) query_tree_mutator((Query *) node,
4141 substitute_actual_srf_parameters_mutator,
4142 (void *) context,
4144 context->sublevels_up--;
4145 return result;
4147 if (IsA(node, Param))
4149 Param *param = (Param *) node;
4151 if (param->paramkind == PARAM_EXTERN)
4153 if (param->paramid <= 0 || param->paramid > context->nargs)
4154 elog(ERROR, "invalid paramid: %d", param->paramid);
4157 * Since the parameter is being inserted into a subquery,
4158 * we must adjust levels.
4160 result = copyObject(list_nth(context->args, param->paramid - 1));
4161 IncrementVarSublevelsUp(result, context->sublevels_up, 0);
4162 return result;
4165 return expression_tree_mutator(node,
4166 substitute_actual_srf_parameters_mutator,
4167 (void *) context);
4171 * Check whether a SELECT targetlist emits the specified column types,
4172 * to see if it's safe to inline a function returning record.
4174 * We insist on exact match here. The executor allows binary-coercible
4175 * cases too, but we don't have a way to preserve the correct column types
4176 * in the correct places if we inline the function in such a case.
4178 * Note that we only check type OIDs not typmods; this agrees with what the
4179 * executor would do at runtime, and attributing a specific typmod to a
4180 * function result is largely wishful thinking anyway.
4182 static bool
4183 tlist_matches_coltypelist(List *tlist, List *coltypelist)
4185 ListCell *tlistitem;
4186 ListCell *clistitem;
4188 clistitem = list_head(coltypelist);
4189 foreach(tlistitem, tlist)
4191 TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
4192 Oid coltype;
4194 if (tle->resjunk)
4195 continue; /* ignore junk columns */
4197 if (clistitem == NULL)
4198 return false; /* too many tlist items */
4200 coltype = lfirst_oid(clistitem);
4201 clistitem = lnext(clistitem);
4203 if (exprType((Node *) tle->expr) != coltype)
4204 return false; /* column type mismatch */
4207 if (clistitem != NULL)
4208 return false; /* too few tlist items */
4210 return true;