]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/util/clauses.c
Implement SQL-standard WITH clauses, including WITH RECURSIVE.
[postgresql] / src / backend / optimizer / util / clauses.c
1 /*-------------------------------------------------------------------------
2  *
3  * clauses.c
4  *        routines to manipulate qualification clauses
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.268 2008/10/04 21:56:53 tgl Exp $
12  *
13  * HISTORY
14  *        AUTHOR                        DATE                    MAJOR EVENT
15  *        Andrew Yu                     Nov 3, 1994             clause.c and clauses.c combined
16  *
17  *-------------------------------------------------------------------------
18  */
19
20 #include "postgres.h"
21
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 "rewrite/rewriteManip.h"
40 #include "tcop/tcopprot.h"
41 #include "utils/acl.h"
42 #include "utils/builtins.h"
43 #include "utils/datum.h"
44 #include "utils/lsyscache.h"
45 #include "utils/memutils.h"
46 #include "utils/syscache.h"
47 #include "utils/typcache.h"
48
49
50 typedef struct
51 {
52         ParamListInfo boundParams;
53         PlannerGlobal *glob;
54         List       *active_fns;
55         Node       *case_val;
56         bool            estimate;
57 } eval_const_expressions_context;
58
59 typedef struct
60 {
61         int                     nargs;
62         List       *args;
63         int                *usecounts;
64 } substitute_actual_parameters_context;
65
66 typedef struct
67 {
68         int                     nargs;
69         List       *args;
70         int                     sublevels_up;
71 } substitute_actual_srf_parameters_context;
72
73 static bool contain_agg_clause_walker(Node *node, void *context);
74 static bool count_agg_clauses_walker(Node *node, AggClauseCounts *counts);
75 static bool expression_returns_set_rows_walker(Node *node, double *count);
76 static bool contain_subplans_walker(Node *node, void *context);
77 static bool contain_mutable_functions_walker(Node *node, void *context);
78 static bool contain_volatile_functions_walker(Node *node, void *context);
79 static bool contain_nonstrict_functions_walker(Node *node, void *context);
80 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
81 static List *find_nonnullable_vars_walker(Node *node, bool top_level);
82 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
83 static bool set_coercionform_dontcare_walker(Node *node, void *context);
84 static Node *eval_const_expressions_mutator(Node *node,
85                                                            eval_const_expressions_context *context);
86 static List *simplify_or_arguments(List *args,
87                                           eval_const_expressions_context *context,
88                                           bool *haveNull, bool *forceTrue);
89 static List *simplify_and_arguments(List *args,
90                                            eval_const_expressions_context *context,
91                                            bool *haveNull, bool *forceFalse);
92 static Expr *simplify_boolean_equality(List *args);
93 static Expr *simplify_function(Oid funcid,
94                                   Oid result_type, int32 result_typmod, List *args,
95                                   bool allow_inline,
96                                   eval_const_expressions_context *context);
97 static Expr *evaluate_function(Oid funcid,
98                                   Oid result_type, int32 result_typmod, List *args,
99                                   HeapTuple func_tuple,
100                                   eval_const_expressions_context *context);
101 static Expr *inline_function(Oid funcid, Oid result_type, List *args,
102                                 HeapTuple func_tuple,
103                                 eval_const_expressions_context *context);
104 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
105                                                          int *usecounts);
106 static Node *substitute_actual_parameters_mutator(Node *node,
107                                                           substitute_actual_parameters_context *context);
108 static void sql_inline_error_callback(void *arg);
109 static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod);
110 static Query *substitute_actual_srf_parameters(Query *expr,
111                                                                                            int nargs, List *args);
112 static Node *substitute_actual_srf_parameters_mutator(Node *node,
113                                                         substitute_actual_srf_parameters_context *context);
114
115
116 /*****************************************************************************
117  *              OPERATOR clause functions
118  *****************************************************************************/
119
120 /*
121  * make_opclause
122  *        Creates an operator clause given its operator info, left operand,
123  *        and right operand (pass NULL to create single-operand clause).
124  */
125 Expr *
126 make_opclause(Oid opno, Oid opresulttype, bool opretset,
127                           Expr *leftop, Expr *rightop)
128 {
129         OpExpr     *expr = makeNode(OpExpr);
130
131         expr->opno = opno;
132         expr->opfuncid = InvalidOid;
133         expr->opresulttype = opresulttype;
134         expr->opretset = opretset;
135         if (rightop)
136                 expr->args = list_make2(leftop, rightop);
137         else
138                 expr->args = list_make1(leftop);
139         expr->location = -1;
140         return (Expr *) expr;
141 }
142
143 /*
144  * get_leftop
145  *
146  * Returns the left operand of a clause of the form (op expr expr)
147  *              or (op expr)
148  */
149 Node *
150 get_leftop(Expr *clause)
151 {
152         OpExpr     *expr = (OpExpr *) clause;
153
154         if (expr->args != NIL)
155                 return linitial(expr->args);
156         else
157                 return NULL;
158 }
159
160 /*
161  * get_rightop
162  *
163  * Returns the right operand in a clause of the form (op expr expr).
164  * NB: result will be NULL if applied to a unary op clause.
165  */
166 Node *
167 get_rightop(Expr *clause)
168 {
169         OpExpr     *expr = (OpExpr *) clause;
170
171         if (list_length(expr->args) >= 2)
172                 return lsecond(expr->args);
173         else
174                 return NULL;
175 }
176
177 /*****************************************************************************
178  *              NOT clause functions
179  *****************************************************************************/
180
181 /*
182  * not_clause
183  *
184  * Returns t iff this is a 'not' clause: (NOT expr).
185  */
186 bool
187 not_clause(Node *clause)
188 {
189         return (clause != NULL &&
190                         IsA(clause, BoolExpr) &&
191                         ((BoolExpr *) clause)->boolop == NOT_EXPR);
192 }
193
194 /*
195  * make_notclause
196  *
197  * Create a 'not' clause given the expression to be negated.
198  */
199 Expr *
200 make_notclause(Expr *notclause)
201 {
202         BoolExpr   *expr = makeNode(BoolExpr);
203
204         expr->boolop = NOT_EXPR;
205         expr->args = list_make1(notclause);
206         expr->location = -1;
207         return (Expr *) expr;
208 }
209
210 /*
211  * get_notclausearg
212  *
213  * Retrieve the clause within a 'not' clause
214  */
215 Expr *
216 get_notclausearg(Expr *notclause)
217 {
218         return linitial(((BoolExpr *) notclause)->args);
219 }
220
221 /*****************************************************************************
222  *              OR clause functions
223  *****************************************************************************/
224
225 /*
226  * or_clause
227  *
228  * Returns t iff the clause is an 'or' clause: (OR { expr }).
229  */
230 bool
231 or_clause(Node *clause)
232 {
233         return (clause != NULL &&
234                         IsA(clause, BoolExpr) &&
235                         ((BoolExpr *) clause)->boolop == OR_EXPR);
236 }
237
238 /*
239  * make_orclause
240  *
241  * Creates an 'or' clause given a list of its subclauses.
242  */
243 Expr *
244 make_orclause(List *orclauses)
245 {
246         BoolExpr   *expr = makeNode(BoolExpr);
247
248         expr->boolop = OR_EXPR;
249         expr->args = orclauses;
250         expr->location = -1;
251         return (Expr *) expr;
252 }
253
254 /*****************************************************************************
255  *              AND clause functions
256  *****************************************************************************/
257
258
259 /*
260  * and_clause
261  *
262  * Returns t iff its argument is an 'and' clause: (AND { expr }).
263  */
264 bool
265 and_clause(Node *clause)
266 {
267         return (clause != NULL &&
268                         IsA(clause, BoolExpr) &&
269                         ((BoolExpr *) clause)->boolop == AND_EXPR);
270 }
271
272 /*
273  * make_andclause
274  *
275  * Creates an 'and' clause given a list of its subclauses.
276  */
277 Expr *
278 make_andclause(List *andclauses)
279 {
280         BoolExpr   *expr = makeNode(BoolExpr);
281
282         expr->boolop = AND_EXPR;
283         expr->args = andclauses;
284         expr->location = -1;
285         return (Expr *) expr;
286 }
287
288 /*
289  * make_and_qual
290  *
291  * Variant of make_andclause for ANDing two qual conditions together.
292  * Qual conditions have the property that a NULL nodetree is interpreted
293  * as 'true'.
294  *
295  * NB: this makes no attempt to preserve AND/OR flatness; so it should not
296  * be used on a qual that has already been run through prepqual.c.
297  */
298 Node *
299 make_and_qual(Node *qual1, Node *qual2)
300 {
301         if (qual1 == NULL)
302                 return qual2;
303         if (qual2 == NULL)
304                 return qual1;
305         return (Node *) make_andclause(list_make2(qual1, qual2));
306 }
307
308 /*
309  * Sometimes (such as in the input of ExecQual), we use lists of expression
310  * nodes with implicit AND semantics.
311  *
312  * These functions convert between an AND-semantics expression list and the
313  * ordinary representation of a boolean expression.
314  *
315  * Note that an empty list is considered equivalent to TRUE.
316  */
317 Expr *
318 make_ands_explicit(List *andclauses)
319 {
320         if (andclauses == NIL)
321                 return (Expr *) makeBoolConst(true, false);
322         else if (list_length(andclauses) == 1)
323                 return (Expr *) linitial(andclauses);
324         else
325                 return make_andclause(andclauses);
326 }
327
328 List *
329 make_ands_implicit(Expr *clause)
330 {
331         /*
332          * NB: because the parser sets the qual field to NULL in a query that has
333          * no WHERE clause, we must consider a NULL input clause as TRUE, even
334          * though one might more reasonably think it FALSE.  Grumble. If this
335          * causes trouble, consider changing the parser's behavior.
336          */
337         if (clause == NULL)
338                 return NIL;                             /* NULL -> NIL list == TRUE */
339         else if (and_clause((Node *) clause))
340                 return ((BoolExpr *) clause)->args;
341         else if (IsA(clause, Const) &&
342                          !((Const *) clause)->constisnull &&
343                          DatumGetBool(((Const *) clause)->constvalue))
344                 return NIL;                             /* constant TRUE input -> NIL list */
345         else
346                 return list_make1(clause);
347 }
348
349
350 /*****************************************************************************
351  *              Aggregate-function clause manipulation
352  *****************************************************************************/
353
354 /*
355  * contain_agg_clause
356  *        Recursively search for Aggref nodes within a clause.
357  *
358  *        Returns true if any aggregate found.
359  *
360  * This does not descend into subqueries, and so should be used only after
361  * reduction of sublinks to subplans, or in contexts where it's known there
362  * are no subqueries.  There mustn't be outer-aggregate references either.
363  *
364  * (If you want something like this but able to deal with subqueries,
365  * see rewriteManip.c's contain_aggs_of_level().)
366  */
367 bool
368 contain_agg_clause(Node *clause)
369 {
370         return contain_agg_clause_walker(clause, NULL);
371 }
372
373 static bool
374 contain_agg_clause_walker(Node *node, void *context)
375 {
376         if (node == NULL)
377                 return false;
378         if (IsA(node, Aggref))
379         {
380                 Assert(((Aggref *) node)->agglevelsup == 0);
381                 return true;                    /* abort the tree traversal and return true */
382         }
383         Assert(!IsA(node, SubLink));
384         return expression_tree_walker(node, contain_agg_clause_walker, context);
385 }
386
387 /*
388  * count_agg_clauses
389  *        Recursively count the Aggref nodes in an expression tree.
390  *
391  *        Note: this also checks for nested aggregates, which are an error.
392  *
393  * We not only count the nodes, but attempt to estimate the total space
394  * needed for their transition state values if all are evaluated in parallel
395  * (as would be done in a HashAgg plan).  See AggClauseCounts for the exact
396  * set of statistics returned.
397  *
398  * NOTE that the counts are ADDED to those already in *counts ... so the
399  * caller is responsible for zeroing the struct initially.
400  *
401  * This does not descend into subqueries, and so should be used only after
402  * reduction of sublinks to subplans, or in contexts where it's known there
403  * are no subqueries.  There mustn't be outer-aggregate references either.
404  */
405 void
406 count_agg_clauses(Node *clause, AggClauseCounts *counts)
407 {
408         /* no setup needed */
409         count_agg_clauses_walker(clause, counts);
410 }
411
412 static bool
413 count_agg_clauses_walker(Node *node, AggClauseCounts *counts)
414 {
415         if (node == NULL)
416                 return false;
417         if (IsA(node, Aggref))
418         {
419                 Aggref     *aggref = (Aggref *) node;
420                 Oid                *inputTypes;
421                 int                     numArguments;
422                 HeapTuple       aggTuple;
423                 Form_pg_aggregate aggform;
424                 Oid                     aggtranstype;
425                 int                     i;
426                 ListCell   *l;
427
428                 Assert(aggref->agglevelsup == 0);
429                 counts->numAggs++;
430                 if (aggref->aggdistinct)
431                         counts->numDistinctAggs++;
432
433                 /* extract argument types */
434                 numArguments = list_length(aggref->args);
435                 inputTypes = (Oid *) palloc(sizeof(Oid) * numArguments);
436                 i = 0;
437                 foreach(l, aggref->args)
438                 {
439                         inputTypes[i++] = exprType((Node *) lfirst(l));
440                 }
441
442                 /* fetch aggregate transition datatype from pg_aggregate */
443                 aggTuple = SearchSysCache(AGGFNOID,
444                                                                   ObjectIdGetDatum(aggref->aggfnoid),
445                                                                   0, 0, 0);
446                 if (!HeapTupleIsValid(aggTuple))
447                         elog(ERROR, "cache lookup failed for aggregate %u",
448                                  aggref->aggfnoid);
449                 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
450                 aggtranstype = aggform->aggtranstype;
451                 ReleaseSysCache(aggTuple);
452
453                 /* resolve actual type of transition state, if polymorphic */
454                 if (IsPolymorphicType(aggtranstype))
455                 {
456                         /* have to fetch the agg's declared input types... */
457                         Oid                *declaredArgTypes;
458                         int                     agg_nargs;
459
460                         (void) get_func_signature(aggref->aggfnoid,
461                                                                           &declaredArgTypes, &agg_nargs);
462                         Assert(agg_nargs == numArguments);
463                         aggtranstype = enforce_generic_type_consistency(inputTypes,
464                                                                                                                         declaredArgTypes,
465                                                                                                                         agg_nargs,
466                                                                                                                         aggtranstype,
467                                                                                                                         false);
468                         pfree(declaredArgTypes);
469                 }
470
471                 /*
472                  * If the transition type is pass-by-value then it doesn't add
473                  * anything to the required size of the hashtable.      If it is
474                  * pass-by-reference then we have to add the estimated size of the
475                  * value itself, plus palloc overhead.
476                  */
477                 if (!get_typbyval(aggtranstype))
478                 {
479                         int32           aggtranstypmod;
480                         int32           avgwidth;
481
482                         /*
483                          * If transition state is of same type as first input, assume it's
484                          * the same typmod (same width) as well.  This works for cases
485                          * like MAX/MIN and is probably somewhat reasonable otherwise.
486                          */
487                         if (numArguments > 0 && aggtranstype == inputTypes[0])
488                                 aggtranstypmod = exprTypmod((Node *) linitial(aggref->args));
489                         else
490                                 aggtranstypmod = -1;
491
492                         avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
493                         avgwidth = MAXALIGN(avgwidth);
494
495                         counts->transitionSpace += avgwidth + 2 * sizeof(void *);
496                 }
497
498                 /*
499                  * Complain if the aggregate's arguments contain any aggregates;
500                  * nested agg functions are semantically nonsensical.
501                  */
502                 if (contain_agg_clause((Node *) aggref->args))
503                         ereport(ERROR,
504                                         (errcode(ERRCODE_GROUPING_ERROR),
505                                          errmsg("aggregate function calls cannot be nested")));
506
507                 /*
508                  * Having checked that, we need not recurse into the argument.
509                  */
510                 return false;
511         }
512         Assert(!IsA(node, SubLink));
513         return expression_tree_walker(node, count_agg_clauses_walker,
514                                                                   (void *) counts);
515 }
516
517
518 /*****************************************************************************
519  *              Support for expressions returning sets
520  *****************************************************************************/
521
522 /*
523  * expression_returns_set_rows
524  *        Estimate the number of rows in a set result.
525  *
526  * We use the product of the rowcount estimates of all the functions in
527  * the given tree.      The result is 1 if there are no set-returning functions.
528  *
529  * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
530  */
531 double
532 expression_returns_set_rows(Node *clause)
533 {
534         double          result = 1;
535
536         (void) expression_returns_set_rows_walker(clause, &result);
537         return result;
538 }
539
540 static bool
541 expression_returns_set_rows_walker(Node *node, double *count)
542 {
543         if (node == NULL)
544                 return false;
545         if (IsA(node, FuncExpr))
546         {
547                 FuncExpr   *expr = (FuncExpr *) node;
548
549                 if (expr->funcretset)
550                         *count *= get_func_rows(expr->funcid);
551         }
552         if (IsA(node, OpExpr))
553         {
554                 OpExpr     *expr = (OpExpr *) node;
555
556                 if (expr->opretset)
557                 {
558                         set_opfuncid(expr);
559                         *count *= get_func_rows(expr->opfuncid);
560                 }
561         }
562
563         /* Avoid recursion for some cases that can't return a set */
564         if (IsA(node, Aggref))
565                 return false;
566         if (IsA(node, DistinctExpr))
567                 return false;
568         if (IsA(node, ScalarArrayOpExpr))
569                 return false;
570         if (IsA(node, BoolExpr))
571                 return false;
572         if (IsA(node, SubLink))
573                 return false;
574         if (IsA(node, SubPlan))
575                 return false;
576         if (IsA(node, AlternativeSubPlan))
577                 return false;
578         if (IsA(node, ArrayExpr))
579                 return false;
580         if (IsA(node, RowExpr))
581                 return false;
582         if (IsA(node, RowCompareExpr))
583                 return false;
584         if (IsA(node, CoalesceExpr))
585                 return false;
586         if (IsA(node, MinMaxExpr))
587                 return false;
588         if (IsA(node, XmlExpr))
589                 return false;
590         if (IsA(node, NullIfExpr))
591                 return false;
592
593         return expression_tree_walker(node, expression_returns_set_rows_walker,
594                                                                   (void *) count);
595 }
596
597
598 /*****************************************************************************
599  *              Subplan clause manipulation
600  *****************************************************************************/
601
602 /*
603  * contain_subplans
604  *        Recursively search for subplan nodes within a clause.
605  *
606  * If we see a SubLink node, we will return TRUE.  This is only possible if
607  * the expression tree hasn't yet been transformed by subselect.c.  We do not
608  * know whether the node will produce a true subplan or just an initplan,
609  * but we make the conservative assumption that it will be a subplan.
610  *
611  * Returns true if any subplan found.
612  */
613 bool
614 contain_subplans(Node *clause)
615 {
616         return contain_subplans_walker(clause, NULL);
617 }
618
619 static bool
620 contain_subplans_walker(Node *node, void *context)
621 {
622         if (node == NULL)
623                 return false;
624         if (IsA(node, SubPlan) ||
625                 IsA(node, AlternativeSubPlan) ||
626                 IsA(node, SubLink))
627                 return true;                    /* abort the tree traversal and return true */
628         return expression_tree_walker(node, contain_subplans_walker, context);
629 }
630
631
632 /*****************************************************************************
633  *              Check clauses for mutable functions
634  *****************************************************************************/
635
636 /*
637  * contain_mutable_functions
638  *        Recursively search for mutable functions within a clause.
639  *
640  * Returns true if any mutable function (or operator implemented by a
641  * mutable function) is found.  This test is needed so that we don't
642  * mistakenly think that something like "WHERE random() < 0.5" can be treated
643  * as a constant qualification.
644  *
645  * XXX we do not examine sub-selects to see if they contain uses of
646  * mutable functions.  It's not real clear if that is correct or not...
647  */
648 bool
649 contain_mutable_functions(Node *clause)
650 {
651         return contain_mutable_functions_walker(clause, NULL);
652 }
653
654 static bool
655 contain_mutable_functions_walker(Node *node, void *context)
656 {
657         if (node == NULL)
658                 return false;
659         if (IsA(node, FuncExpr))
660         {
661                 FuncExpr   *expr = (FuncExpr *) node;
662
663                 if (func_volatile(expr->funcid) != PROVOLATILE_IMMUTABLE)
664                         return true;
665                 /* else fall through to check args */
666         }
667         else if (IsA(node, OpExpr))
668         {
669                 OpExpr     *expr = (OpExpr *) node;
670
671                 set_opfuncid(expr);
672                 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
673                         return true;
674                 /* else fall through to check args */
675         }
676         else if (IsA(node, DistinctExpr))
677         {
678                 DistinctExpr *expr = (DistinctExpr *) node;
679
680                 set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
681                 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
682                         return true;
683                 /* else fall through to check args */
684         }
685         else if (IsA(node, ScalarArrayOpExpr))
686         {
687                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
688
689                 set_sa_opfuncid(expr);
690                 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
691                         return true;
692                 /* else fall through to check args */
693         }
694         else if (IsA(node, CoerceViaIO))
695         {
696                 CoerceViaIO *expr = (CoerceViaIO *) node;
697                 Oid                     iofunc;
698                 Oid                     typioparam;
699                 bool            typisvarlena;
700
701                 /* check the result type's input function */
702                 getTypeInputInfo(expr->resulttype,
703                                                  &iofunc, &typioparam);
704                 if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
705                         return true;
706                 /* check the input type's output function */
707                 getTypeOutputInfo(exprType((Node *) expr->arg),
708                                                   &iofunc, &typisvarlena);
709                 if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
710                         return true;
711                 /* else fall through to check args */
712         }
713         else if (IsA(node, ArrayCoerceExpr))
714         {
715                 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
716
717                 if (OidIsValid(expr->elemfuncid) &&
718                         func_volatile(expr->elemfuncid) != PROVOLATILE_IMMUTABLE)
719                         return true;
720                 /* else fall through to check args */
721         }
722         else if (IsA(node, NullIfExpr))
723         {
724                 NullIfExpr *expr = (NullIfExpr *) node;
725
726                 set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
727                 if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
728                         return true;
729                 /* else fall through to check args */
730         }
731         else if (IsA(node, RowCompareExpr))
732         {
733                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
734                 ListCell   *opid;
735
736                 foreach(opid, rcexpr->opnos)
737                 {
738                         if (op_volatile(lfirst_oid(opid)) != PROVOLATILE_IMMUTABLE)
739                                 return true;
740                 }
741                 /* else fall through to check args */
742         }
743         return expression_tree_walker(node, contain_mutable_functions_walker,
744                                                                   context);
745 }
746
747
748 /*****************************************************************************
749  *              Check clauses for volatile functions
750  *****************************************************************************/
751
752 /*
753  * contain_volatile_functions
754  *        Recursively search for volatile functions within a clause.
755  *
756  * Returns true if any volatile function (or operator implemented by a
757  * volatile function) is found. This test prevents invalid conversions
758  * of volatile expressions into indexscan quals.
759  *
760  * XXX we do not examine sub-selects to see if they contain uses of
761  * volatile functions.  It's not real clear if that is correct or not...
762  */
763 bool
764 contain_volatile_functions(Node *clause)
765 {
766         return contain_volatile_functions_walker(clause, NULL);
767 }
768
769 static bool
770 contain_volatile_functions_walker(Node *node, void *context)
771 {
772         if (node == NULL)
773                 return false;
774         if (IsA(node, FuncExpr))
775         {
776                 FuncExpr   *expr = (FuncExpr *) node;
777
778                 if (func_volatile(expr->funcid) == PROVOLATILE_VOLATILE)
779                         return true;
780                 /* else fall through to check args */
781         }
782         else if (IsA(node, OpExpr))
783         {
784                 OpExpr     *expr = (OpExpr *) node;
785
786                 set_opfuncid(expr);
787                 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
788                         return true;
789                 /* else fall through to check args */
790         }
791         else if (IsA(node, DistinctExpr))
792         {
793                 DistinctExpr *expr = (DistinctExpr *) node;
794
795                 set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
796                 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
797                         return true;
798                 /* else fall through to check args */
799         }
800         else if (IsA(node, ScalarArrayOpExpr))
801         {
802                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
803
804                 set_sa_opfuncid(expr);
805                 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
806                         return true;
807                 /* else fall through to check args */
808         }
809         else if (IsA(node, CoerceViaIO))
810         {
811                 CoerceViaIO *expr = (CoerceViaIO *) node;
812                 Oid                     iofunc;
813                 Oid                     typioparam;
814                 bool            typisvarlena;
815
816                 /* check the result type's input function */
817                 getTypeInputInfo(expr->resulttype,
818                                                  &iofunc, &typioparam);
819                 if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
820                         return true;
821                 /* check the input type's output function */
822                 getTypeOutputInfo(exprType((Node *) expr->arg),
823                                                   &iofunc, &typisvarlena);
824                 if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
825                         return true;
826                 /* else fall through to check args */
827         }
828         else if (IsA(node, ArrayCoerceExpr))
829         {
830                 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
831
832                 if (OidIsValid(expr->elemfuncid) &&
833                         func_volatile(expr->elemfuncid) == PROVOLATILE_VOLATILE)
834                         return true;
835                 /* else fall through to check args */
836         }
837         else if (IsA(node, NullIfExpr))
838         {
839                 NullIfExpr *expr = (NullIfExpr *) node;
840
841                 set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
842                 if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
843                         return true;
844                 /* else fall through to check args */
845         }
846         else if (IsA(node, RowCompareExpr))
847         {
848                 /* RowCompare probably can't have volatile ops, but check anyway */
849                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
850                 ListCell   *opid;
851
852                 foreach(opid, rcexpr->opnos)
853                 {
854                         if (op_volatile(lfirst_oid(opid)) == PROVOLATILE_VOLATILE)
855                                 return true;
856                 }
857                 /* else fall through to check args */
858         }
859         return expression_tree_walker(node, contain_volatile_functions_walker,
860                                                                   context);
861 }
862
863
864 /*****************************************************************************
865  *              Check clauses for nonstrict functions
866  *****************************************************************************/
867
868 /*
869  * contain_nonstrict_functions
870  *        Recursively search for nonstrict functions within a clause.
871  *
872  * Returns true if any nonstrict construct is found --- ie, anything that
873  * could produce non-NULL output with a NULL input.
874  *
875  * The idea here is that the caller has verified that the expression contains
876  * one or more Var or Param nodes (as appropriate for the caller's need), and
877  * now wishes to prove that the expression result will be NULL if any of these
878  * inputs is NULL.      If we return false, then the proof succeeded.
879  */
880 bool
881 contain_nonstrict_functions(Node *clause)
882 {
883         return contain_nonstrict_functions_walker(clause, NULL);
884 }
885
886 static bool
887 contain_nonstrict_functions_walker(Node *node, void *context)
888 {
889         if (node == NULL)
890                 return false;
891         if (IsA(node, Aggref))
892         {
893                 /* an aggregate could return non-null with null input */
894                 return true;
895         }
896         if (IsA(node, ArrayRef))
897         {
898                 /* array assignment is nonstrict, but subscripting is strict */
899                 if (((ArrayRef *) node)->refassgnexpr != NULL)
900                         return true;
901                 /* else fall through to check args */
902         }
903         if (IsA(node, FuncExpr))
904         {
905                 FuncExpr   *expr = (FuncExpr *) node;
906
907                 if (!func_strict(expr->funcid))
908                         return true;
909                 /* else fall through to check args */
910         }
911         if (IsA(node, OpExpr))
912         {
913                 OpExpr     *expr = (OpExpr *) node;
914
915                 set_opfuncid(expr);
916                 if (!func_strict(expr->opfuncid))
917                         return true;
918                 /* else fall through to check args */
919         }
920         if (IsA(node, DistinctExpr))
921         {
922                 /* IS DISTINCT FROM is inherently non-strict */
923                 return true;
924         }
925         if (IsA(node, ScalarArrayOpExpr))
926         {
927                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
928
929                 if (!is_strict_saop(expr, false))
930                         return true;
931                 /* else fall through to check args */
932         }
933         if (IsA(node, BoolExpr))
934         {
935                 BoolExpr   *expr = (BoolExpr *) node;
936
937                 switch (expr->boolop)
938                 {
939                         case AND_EXPR:
940                         case OR_EXPR:
941                                 /* AND, OR are inherently non-strict */
942                                 return true;
943                         default:
944                                 break;
945                 }
946         }
947         if (IsA(node, SubLink))
948         {
949                 /* In some cases a sublink might be strict, but in general not */
950                 return true;
951         }
952         if (IsA(node, SubPlan))
953                 return true;
954         if (IsA(node, AlternativeSubPlan))
955                 return true;
956         /* ArrayCoerceExpr is strict at the array level, regardless of elemfunc */
957         if (IsA(node, FieldStore))
958                 return true;
959         if (IsA(node, CaseExpr))
960                 return true;
961         if (IsA(node, ArrayExpr))
962                 return true;
963         if (IsA(node, RowExpr))
964                 return true;
965         if (IsA(node, RowCompareExpr))
966                 return true;
967         if (IsA(node, CoalesceExpr))
968                 return true;
969         if (IsA(node, MinMaxExpr))
970                 return true;
971         if (IsA(node, XmlExpr))
972                 return true;
973         if (IsA(node, NullIfExpr))
974                 return true;
975         if (IsA(node, NullTest))
976                 return true;
977         if (IsA(node, BooleanTest))
978                 return true;
979         return expression_tree_walker(node, contain_nonstrict_functions_walker,
980                                                                   context);
981 }
982
983
984 /*
985  * find_nonnullable_rels
986  *              Determine which base rels are forced nonnullable by given clause.
987  *
988  * Returns the set of all Relids that are referenced in the clause in such
989  * a way that the clause cannot possibly return TRUE if any of these Relids
990  * is an all-NULL row.  (It is OK to err on the side of conservatism; hence
991  * the analysis here is simplistic.)
992  *
993  * The semantics here are subtly different from contain_nonstrict_functions:
994  * that function is concerned with NULL results from arbitrary expressions,
995  * but here we assume that the input is a Boolean expression, and wish to
996  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
997  * the expression to have been AND/OR flattened and converted to implicit-AND
998  * format.
999  *
1000  * Note: this function is largely duplicative of find_nonnullable_vars().
1001  * The reason not to simplify this function into a thin wrapper around
1002  * find_nonnullable_vars() is that the tested conditions really are different:
1003  * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
1004  * that either v1 or v2 can't be NULL, but it does prove that the t1 row
1005  * as a whole can't be all-NULL.
1006  *
1007  * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1008  * the result is either FALSE or NULL is good enough.  top_level is FALSE when
1009  * we have descended below a NOT or a strict function: now we must be able to
1010  * prove that the subexpression goes to NULL.
1011  *
1012  * We don't use expression_tree_walker here because we don't want to descend
1013  * through very many kinds of nodes; only the ones we can be sure are strict.
1014  */
1015 Relids
1016 find_nonnullable_rels(Node *clause)
1017 {
1018         return find_nonnullable_rels_walker(clause, true);
1019 }
1020
1021 static Relids
1022 find_nonnullable_rels_walker(Node *node, bool top_level)
1023 {
1024         Relids          result = NULL;
1025         ListCell   *l;
1026
1027         if (node == NULL)
1028                 return NULL;
1029         if (IsA(node, Var))
1030         {
1031                 Var                *var = (Var *) node;
1032
1033                 if (var->varlevelsup == 0)
1034                         result = bms_make_singleton(var->varno);
1035         }
1036         else if (IsA(node, List))
1037         {
1038                 /*
1039                  * At top level, we are examining an implicit-AND list: if any of the
1040                  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1041                  * not at top level, we are examining the arguments of a strict
1042                  * function: if any of them produce NULL then the result of the
1043                  * function must be NULL.  So in both cases, the set of nonnullable
1044                  * rels is the union of those found in the arms, and we pass down the
1045                  * top_level flag unmodified.
1046                  */
1047                 foreach(l, (List *) node)
1048                 {
1049                         result = bms_join(result,
1050                                                           find_nonnullable_rels_walker(lfirst(l),
1051                                                                                                                    top_level));
1052                 }
1053         }
1054         else if (IsA(node, FuncExpr))
1055         {
1056                 FuncExpr   *expr = (FuncExpr *) node;
1057
1058                 if (func_strict(expr->funcid))
1059                         result = find_nonnullable_rels_walker((Node *) expr->args, false);
1060         }
1061         else if (IsA(node, OpExpr))
1062         {
1063                 OpExpr     *expr = (OpExpr *) node;
1064
1065                 set_opfuncid(expr);
1066                 if (func_strict(expr->opfuncid))
1067                         result = find_nonnullable_rels_walker((Node *) expr->args, false);
1068         }
1069         else if (IsA(node, ScalarArrayOpExpr))
1070         {
1071                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1072
1073                 if (is_strict_saop(expr, true))
1074                         result = find_nonnullable_rels_walker((Node *) expr->args, false);
1075         }
1076         else if (IsA(node, BoolExpr))
1077         {
1078                 BoolExpr   *expr = (BoolExpr *) node;
1079
1080                 switch (expr->boolop)
1081                 {
1082                         case AND_EXPR:
1083                                 /* At top level we can just recurse (to the List case) */
1084                                 if (top_level)
1085                                 {
1086                                         result = find_nonnullable_rels_walker((Node *) expr->args,
1087                                                                                                                   top_level);
1088                                         break;
1089                                 }
1090
1091                                 /*
1092                                  * Below top level, even if one arm produces NULL, the result
1093                                  * could be FALSE (hence not NULL).  However, if *all* the
1094                                  * arms produce NULL then the result is NULL, so we can take
1095                                  * the intersection of the sets of nonnullable rels, just as
1096                                  * for OR.      Fall through to share code.
1097                                  */
1098                                 /* FALL THRU */
1099                         case OR_EXPR:
1100
1101                                 /*
1102                                  * OR is strict if all of its arms are, so we can take the
1103                                  * intersection of the sets of nonnullable rels for each arm.
1104                                  * This works for both values of top_level.
1105                                  */
1106                                 foreach(l, expr->args)
1107                                 {
1108                                         Relids          subresult;
1109
1110                                         subresult = find_nonnullable_rels_walker(lfirst(l),
1111                                                                                                                          top_level);
1112                                         if (result == NULL) /* first subresult? */
1113                                                 result = subresult;
1114                                         else
1115                                                 result = bms_int_members(result, subresult);
1116
1117                                         /*
1118                                          * If the intersection is empty, we can stop looking. This
1119                                          * also justifies the test for first-subresult above.
1120                                          */
1121                                         if (bms_is_empty(result))
1122                                                 break;
1123                                 }
1124                                 break;
1125                         case NOT_EXPR:
1126                                 /* NOT will return null if its arg is null */
1127                                 result = find_nonnullable_rels_walker((Node *) expr->args,
1128                                                                                                           false);
1129                                 break;
1130                         default:
1131                                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1132                                 break;
1133                 }
1134         }
1135         else if (IsA(node, RelabelType))
1136         {
1137                 RelabelType *expr = (RelabelType *) node;
1138
1139                 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1140         }
1141         else if (IsA(node, CoerceViaIO))
1142         {
1143                 /* not clear this is useful, but it can't hurt */
1144                 CoerceViaIO *expr = (CoerceViaIO *) node;
1145
1146                 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1147         }
1148         else if (IsA(node, ArrayCoerceExpr))
1149         {
1150                 /* ArrayCoerceExpr is strict at the array level */
1151                 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1152
1153                 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1154         }
1155         else if (IsA(node, ConvertRowtypeExpr))
1156         {
1157                 /* not clear this is useful, but it can't hurt */
1158                 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1159
1160                 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1161         }
1162         else if (IsA(node, NullTest))
1163         {
1164                 /* IS NOT NULL can be considered strict, but only at top level */
1165                 NullTest   *expr = (NullTest *) node;
1166
1167                 if (top_level && expr->nulltesttype == IS_NOT_NULL)
1168                         result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1169         }
1170         else if (IsA(node, BooleanTest))
1171         {
1172                 /* Boolean tests that reject NULL are strict at top level */
1173                 BooleanTest *expr = (BooleanTest *) node;
1174
1175                 if (top_level &&
1176                         (expr->booltesttype == IS_TRUE ||
1177                          expr->booltesttype == IS_FALSE ||
1178                          expr->booltesttype == IS_NOT_UNKNOWN))
1179                         result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1180         }
1181         else if (IsA(node, FlattenedSubLink))
1182         {
1183                 /* JOIN_SEMI sublinks preserve strictness, but JOIN_ANTI ones don't */
1184                 FlattenedSubLink *expr = (FlattenedSubLink *) node;
1185
1186                 if (expr->jointype == JOIN_SEMI)
1187                         result = find_nonnullable_rels_walker((Node *) expr->quals,
1188                                                                                                   top_level);
1189         }
1190         return result;
1191 }
1192
1193 /*
1194  * find_nonnullable_vars
1195  *              Determine which Vars are forced nonnullable by given clause.
1196  *
1197  * Returns a list of all level-zero Vars that are referenced in the clause in
1198  * such a way that the clause cannot possibly return TRUE if any of these Vars
1199  * is NULL.  (It is OK to err on the side of conservatism; hence the analysis
1200  * here is simplistic.)
1201  *
1202  * The semantics here are subtly different from contain_nonstrict_functions:
1203  * that function is concerned with NULL results from arbitrary expressions,
1204  * but here we assume that the input is a Boolean expression, and wish to
1205  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
1206  * the expression to have been AND/OR flattened and converted to implicit-AND
1207  * format.
1208  *
1209  * The result is a palloc'd List, but we have not copied the member Var nodes.
1210  * Also, we don't bother trying to eliminate duplicate entries.
1211  *
1212  * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1213  * the result is either FALSE or NULL is good enough.  top_level is FALSE when
1214  * we have descended below a NOT or a strict function: now we must be able to
1215  * prove that the subexpression goes to NULL.
1216  *
1217  * We don't use expression_tree_walker here because we don't want to descend
1218  * through very many kinds of nodes; only the ones we can be sure are strict.
1219  */
1220 List *
1221 find_nonnullable_vars(Node *clause)
1222 {
1223         return find_nonnullable_vars_walker(clause, true);
1224 }
1225
1226 static List *
1227 find_nonnullable_vars_walker(Node *node, bool top_level)
1228 {
1229         List       *result = NIL;
1230         ListCell   *l;
1231
1232         if (node == NULL)
1233                 return NIL;
1234         if (IsA(node, Var))
1235         {
1236                 Var                *var = (Var *) node;
1237
1238                 if (var->varlevelsup == 0)
1239                         result = list_make1(var);
1240         }
1241         else if (IsA(node, List))
1242         {
1243                 /*
1244                  * At top level, we are examining an implicit-AND list: if any of the
1245                  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1246                  * not at top level, we are examining the arguments of a strict
1247                  * function: if any of them produce NULL then the result of the
1248                  * function must be NULL.  So in both cases, the set of nonnullable
1249                  * vars is the union of those found in the arms, and we pass down the
1250                  * top_level flag unmodified.
1251                  */
1252                 foreach(l, (List *) node)
1253                 {
1254                         result = list_concat(result,
1255                                                                  find_nonnullable_vars_walker(lfirst(l),
1256                                                                                                                           top_level));
1257                 }
1258         }
1259         else if (IsA(node, FuncExpr))
1260         {
1261                 FuncExpr   *expr = (FuncExpr *) node;
1262
1263                 if (func_strict(expr->funcid))
1264                         result = find_nonnullable_vars_walker((Node *) expr->args, false);
1265         }
1266         else if (IsA(node, OpExpr))
1267         {
1268                 OpExpr     *expr = (OpExpr *) node;
1269
1270                 set_opfuncid(expr);
1271                 if (func_strict(expr->opfuncid))
1272                         result = find_nonnullable_vars_walker((Node *) expr->args, false);
1273         }
1274         else if (IsA(node, ScalarArrayOpExpr))
1275         {
1276                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1277
1278                 if (is_strict_saop(expr, true))
1279                         result = find_nonnullable_vars_walker((Node *) expr->args, false);
1280         }
1281         else if (IsA(node, BoolExpr))
1282         {
1283                 BoolExpr   *expr = (BoolExpr *) node;
1284
1285                 switch (expr->boolop)
1286                 {
1287                         case AND_EXPR:
1288                                 /* At top level we can just recurse (to the List case) */
1289                                 if (top_level)
1290                                 {
1291                                         result = find_nonnullable_vars_walker((Node *) expr->args,
1292                                                                                                                   top_level);
1293                                         break;
1294                                 }
1295
1296                                 /*
1297                                  * Below top level, even if one arm produces NULL, the result
1298                                  * could be FALSE (hence not NULL).  However, if *all* the
1299                                  * arms produce NULL then the result is NULL, so we can take
1300                                  * the intersection of the sets of nonnullable vars, just as
1301                                  * for OR.      Fall through to share code.
1302                                  */
1303                                 /* FALL THRU */
1304                         case OR_EXPR:
1305
1306                                 /*
1307                                  * OR is strict if all of its arms are, so we can take the
1308                                  * intersection of the sets of nonnullable vars for each arm.
1309                                  * This works for both values of top_level.
1310                                  */
1311                                 foreach(l, expr->args)
1312                                 {
1313                                         List       *subresult;
1314
1315                                         subresult = find_nonnullable_vars_walker(lfirst(l),
1316                                                                                                                          top_level);
1317                                         if (result == NIL)      /* first subresult? */
1318                                                 result = subresult;
1319                                         else
1320                                                 result = list_intersection(result, subresult);
1321
1322                                         /*
1323                                          * If the intersection is empty, we can stop looking. This
1324                                          * also justifies the test for first-subresult above.
1325                                          */
1326                                         if (result == NIL)
1327                                                 break;
1328                                 }
1329                                 break;
1330                         case NOT_EXPR:
1331                                 /* NOT will return null if its arg is null */
1332                                 result = find_nonnullable_vars_walker((Node *) expr->args,
1333                                                                                                           false);
1334                                 break;
1335                         default:
1336                                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1337                                 break;
1338                 }
1339         }
1340         else if (IsA(node, RelabelType))
1341         {
1342                 RelabelType *expr = (RelabelType *) node;
1343
1344                 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1345         }
1346         else if (IsA(node, CoerceViaIO))
1347         {
1348                 /* not clear this is useful, but it can't hurt */
1349                 CoerceViaIO *expr = (CoerceViaIO *) node;
1350
1351                 result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1352         }
1353         else if (IsA(node, ArrayCoerceExpr))
1354         {
1355                 /* ArrayCoerceExpr is strict at the array level */
1356                 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1357
1358                 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1359         }
1360         else if (IsA(node, ConvertRowtypeExpr))
1361         {
1362                 /* not clear this is useful, but it can't hurt */
1363                 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1364
1365                 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1366         }
1367         else if (IsA(node, NullTest))
1368         {
1369                 /* IS NOT NULL can be considered strict, but only at top level */
1370                 NullTest   *expr = (NullTest *) node;
1371
1372                 if (top_level && expr->nulltesttype == IS_NOT_NULL)
1373                         result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1374         }
1375         else if (IsA(node, BooleanTest))
1376         {
1377                 /* Boolean tests that reject NULL are strict at top level */
1378                 BooleanTest *expr = (BooleanTest *) node;
1379
1380                 if (top_level &&
1381                         (expr->booltesttype == IS_TRUE ||
1382                          expr->booltesttype == IS_FALSE ||
1383                          expr->booltesttype == IS_NOT_UNKNOWN))
1384                         result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1385         }
1386         else if (IsA(node, FlattenedSubLink))
1387         {
1388                 /* JOIN_SEMI sublinks preserve strictness, but JOIN_ANTI ones don't */
1389                 FlattenedSubLink *expr = (FlattenedSubLink *) node;
1390
1391                 if (expr->jointype == JOIN_SEMI)
1392                         result = find_nonnullable_vars_walker((Node *) expr->quals,
1393                                                                                                   top_level);
1394         }
1395         return result;
1396 }
1397
1398 /*
1399  * find_forced_null_vars
1400  *              Determine which Vars must be NULL for the given clause to return TRUE.
1401  *
1402  * This is the complement of find_nonnullable_vars: find the level-zero Vars
1403  * that must be NULL for the clause to return TRUE.  (It is OK to err on the
1404  * side of conservatism; hence the analysis here is simplistic.  In fact,
1405  * we only detect simple "var IS NULL" tests at the top level.)
1406  *
1407  * The result is a palloc'd List, but we have not copied the member Var nodes.
1408  * Also, we don't bother trying to eliminate duplicate entries.
1409  */
1410 List *
1411 find_forced_null_vars(Node *node)
1412 {
1413         List       *result = NIL;
1414         Var                *var;
1415         ListCell   *l;
1416
1417         if (node == NULL)
1418                 return NIL;
1419         /* Check single-clause cases using subroutine */
1420         var = find_forced_null_var(node);
1421         if (var)
1422         {
1423                 result = list_make1(var);
1424         }
1425         /* Otherwise, handle AND-conditions */
1426         else if (IsA(node, List))
1427         {
1428                 /*
1429                  * At top level, we are examining an implicit-AND list: if any of the
1430                  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1431                  */
1432                 foreach(l, (List *) node)
1433                 {
1434                         result = list_concat(result,
1435                                                                  find_forced_null_vars(lfirst(l)));
1436                 }
1437         }
1438         else if (IsA(node, BoolExpr))
1439         {
1440                 BoolExpr   *expr = (BoolExpr *) node;
1441
1442                 /*
1443                  * We don't bother considering the OR case, because it's fairly
1444                  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL".
1445                  * Likewise, the NOT case isn't worth expending code on.
1446                  */
1447                 if (expr->boolop == AND_EXPR)
1448                 {
1449                         /* At top level we can just recurse (to the List case) */
1450                         result = find_forced_null_vars((Node *) expr->args);
1451                 }
1452         }
1453         return result;
1454 }
1455
1456 /*
1457  * find_forced_null_var
1458  *              Return the Var forced null by the given clause, or NULL if it's
1459  *              not an IS NULL-type clause.  For success, the clause must enforce
1460  *              *only* nullness of the particular Var, not any other conditions.
1461  *
1462  * This is just the single-clause case of find_forced_null_vars(), without
1463  * any allowance for AND conditions.  It's used by initsplan.c on individual
1464  * qual clauses.  The reason for not just applying find_forced_null_vars()
1465  * is that if an AND of an IS NULL clause with something else were to somehow
1466  * survive AND/OR flattening, initsplan.c might get fooled into discarding
1467  * the whole clause when only the IS NULL part of it had been proved redundant.
1468  */
1469 Var *
1470 find_forced_null_var(Node *node)
1471 {
1472         if (node == NULL)
1473                 return NULL;
1474         if (IsA(node, NullTest))
1475         {
1476                 /* check for var IS NULL */
1477                 NullTest   *expr = (NullTest *) node;
1478
1479                 if (expr->nulltesttype == IS_NULL)
1480                 {
1481                         Var        *var = (Var *) expr->arg;
1482
1483                         if (var && IsA(var, Var) &&
1484                                 var->varlevelsup == 0)
1485                                 return var;
1486                 }
1487         }
1488         else if (IsA(node, BooleanTest))
1489         {
1490                 /* var IS UNKNOWN is equivalent to var IS NULL */
1491                 BooleanTest *expr = (BooleanTest *) node;
1492
1493                 if (expr->booltesttype == IS_UNKNOWN)
1494                 {
1495                         Var        *var = (Var *) expr->arg;
1496
1497                         if (var && IsA(var, Var) &&
1498                                 var->varlevelsup == 0)
1499                                 return var;
1500                 }
1501         }
1502         return NULL;
1503 }
1504
1505 /*
1506  * Can we treat a ScalarArrayOpExpr as strict?
1507  *
1508  * If "falseOK" is true, then a "false" result can be considered strict,
1509  * else we need to guarantee an actual NULL result for NULL input.
1510  *
1511  * "foo op ALL array" is strict if the op is strict *and* we can prove
1512  * that the array input isn't an empty array.  We can check that
1513  * for the cases of an array constant and an ARRAY[] construct.
1514  *
1515  * "foo op ANY array" is strict in the falseOK sense if the op is strict.
1516  * If not falseOK, the test is the same as for "foo op ALL array".
1517  */
1518 static bool
1519 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
1520 {
1521         Node       *rightop;
1522
1523         /* The contained operator must be strict. */
1524         set_sa_opfuncid(expr);
1525         if (!func_strict(expr->opfuncid))
1526                 return false;
1527         /* If ANY and falseOK, that's all we need to check. */
1528         if (expr->useOr && falseOK)
1529                 return true;
1530         /* Else, we have to see if the array is provably non-empty. */
1531         Assert(list_length(expr->args) == 2);
1532         rightop = (Node *) lsecond(expr->args);
1533         if (rightop && IsA(rightop, Const))
1534         {
1535                 Datum           arraydatum = ((Const *) rightop)->constvalue;
1536                 bool            arrayisnull = ((Const *) rightop)->constisnull;
1537                 ArrayType  *arrayval;
1538                 int                     nitems;
1539
1540                 if (arrayisnull)
1541                         return false;
1542                 arrayval = DatumGetArrayTypeP(arraydatum);
1543                 nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1544                 if (nitems > 0)
1545                         return true;
1546         }
1547         else if (rightop && IsA(rightop, ArrayExpr))
1548         {
1549                 ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
1550
1551                 if (arrayexpr->elements != NIL && !arrayexpr->multidims)
1552                         return true;
1553         }
1554         return false;
1555 }
1556
1557
1558 /*****************************************************************************
1559  *              Check for "pseudo-constant" clauses
1560  *****************************************************************************/
1561
1562 /*
1563  * is_pseudo_constant_clause
1564  *        Detect whether an expression is "pseudo constant", ie, it contains no
1565  *        variables of the current query level and no uses of volatile functions.
1566  *        Such an expr is not necessarily a true constant: it can still contain
1567  *        Params and outer-level Vars, not to mention functions whose results
1568  *        may vary from one statement to the next.      However, the expr's value
1569  *        will be constant over any one scan of the current query, so it can be
1570  *        used as, eg, an indexscan key.
1571  *
1572  * CAUTION: this function omits to test for one very important class of
1573  * not-constant expressions, namely aggregates (Aggrefs).  In current usage
1574  * this is only applied to WHERE clauses and so a check for Aggrefs would be
1575  * a waste of cycles; but be sure to also check contain_agg_clause() if you
1576  * want to know about pseudo-constness in other contexts.
1577  */
1578 bool
1579 is_pseudo_constant_clause(Node *clause)
1580 {
1581         /*
1582          * We could implement this check in one recursive scan.  But since the
1583          * check for volatile functions is both moderately expensive and unlikely
1584          * to fail, it seems better to look for Vars first and only check for
1585          * volatile functions if we find no Vars.
1586          */
1587         if (!contain_var_clause(clause) &&
1588                 !contain_volatile_functions(clause))
1589                 return true;
1590         return false;
1591 }
1592
1593 /*
1594  * is_pseudo_constant_clause_relids
1595  *        Same as above, except caller already has available the var membership
1596  *        of the expression; this lets us avoid the contain_var_clause() scan.
1597  */
1598 bool
1599 is_pseudo_constant_clause_relids(Node *clause, Relids relids)
1600 {
1601         if (bms_is_empty(relids) &&
1602                 !contain_volatile_functions(clause))
1603                 return true;
1604         return false;
1605 }
1606
1607
1608 /*****************************************************************************
1609  *                                                                                                                                                       *
1610  *              General clause-manipulating routines                                                             *
1611  *                                                                                                                                                       *
1612  *****************************************************************************/
1613
1614 /*
1615  * NumRelids
1616  *              (formerly clause_relids)
1617  *
1618  * Returns the number of different relations referenced in 'clause'.
1619  */
1620 int
1621 NumRelids(Node *clause)
1622 {
1623         Relids          varnos = pull_varnos(clause);
1624         int                     result = bms_num_members(varnos);
1625
1626         bms_free(varnos);
1627         return result;
1628 }
1629
1630 /*
1631  * CommuteOpExpr: commute a binary operator clause
1632  *
1633  * XXX the clause is destructively modified!
1634  */
1635 void
1636 CommuteOpExpr(OpExpr *clause)
1637 {
1638         Oid                     opoid;
1639         Node       *temp;
1640
1641         /* Sanity checks: caller is at fault if these fail */
1642         if (!is_opclause(clause) ||
1643                 list_length(clause->args) != 2)
1644                 elog(ERROR, "cannot commute non-binary-operator clause");
1645
1646         opoid = get_commutator(clause->opno);
1647
1648         if (!OidIsValid(opoid))
1649                 elog(ERROR, "could not find commutator for operator %u",
1650                          clause->opno);
1651
1652         /*
1653          * modify the clause in-place!
1654          */
1655         clause->opno = opoid;
1656         clause->opfuncid = InvalidOid;
1657         /* opresulttype and opretset are assumed not to change */
1658
1659         temp = linitial(clause->args);
1660         linitial(clause->args) = lsecond(clause->args);
1661         lsecond(clause->args) = temp;
1662 }
1663
1664 /*
1665  * CommuteRowCompareExpr: commute a RowCompareExpr clause
1666  *
1667  * XXX the clause is destructively modified!
1668  */
1669 void
1670 CommuteRowCompareExpr(RowCompareExpr *clause)
1671 {
1672         List       *newops;
1673         List       *temp;
1674         ListCell   *l;
1675
1676         /* Sanity checks: caller is at fault if these fail */
1677         if (!IsA(clause, RowCompareExpr))
1678                 elog(ERROR, "expected a RowCompareExpr");
1679
1680         /* Build list of commuted operators */
1681         newops = NIL;
1682         foreach(l, clause->opnos)
1683         {
1684                 Oid                     opoid = lfirst_oid(l);
1685
1686                 opoid = get_commutator(opoid);
1687                 if (!OidIsValid(opoid))
1688                         elog(ERROR, "could not find commutator for operator %u",
1689                                  lfirst_oid(l));
1690                 newops = lappend_oid(newops, opoid);
1691         }
1692
1693         /*
1694          * modify the clause in-place!
1695          */
1696         switch (clause->rctype)
1697         {
1698                 case ROWCOMPARE_LT:
1699                         clause->rctype = ROWCOMPARE_GT;
1700                         break;
1701                 case ROWCOMPARE_LE:
1702                         clause->rctype = ROWCOMPARE_GE;
1703                         break;
1704                 case ROWCOMPARE_GE:
1705                         clause->rctype = ROWCOMPARE_LE;
1706                         break;
1707                 case ROWCOMPARE_GT:
1708                         clause->rctype = ROWCOMPARE_LT;
1709                         break;
1710                 default:
1711                         elog(ERROR, "unexpected RowCompare type: %d",
1712                                  (int) clause->rctype);
1713                         break;
1714         }
1715
1716         clause->opnos = newops;
1717
1718         /*
1719          * Note: we need not change the opfamilies list; we assume any btree
1720          * opfamily containing an operator will also contain its commutator.
1721          */
1722
1723         temp = clause->largs;
1724         clause->largs = clause->rargs;
1725         clause->rargs = temp;
1726 }
1727
1728 /*
1729  * strip_implicit_coercions: remove implicit coercions at top level of tree
1730  *
1731  * Note: there isn't any useful thing we can do with a RowExpr here, so
1732  * just return it unchanged, even if it's marked as an implicit coercion.
1733  */
1734 Node *
1735 strip_implicit_coercions(Node *node)
1736 {
1737         if (node == NULL)
1738                 return NULL;
1739         if (IsA(node, FuncExpr))
1740         {
1741                 FuncExpr   *f = (FuncExpr *) node;
1742
1743                 if (f->funcformat == COERCE_IMPLICIT_CAST)
1744                         return strip_implicit_coercions(linitial(f->args));
1745         }
1746         else if (IsA(node, RelabelType))
1747         {
1748                 RelabelType *r = (RelabelType *) node;
1749
1750                 if (r->relabelformat == COERCE_IMPLICIT_CAST)
1751                         return strip_implicit_coercions((Node *) r->arg);
1752         }
1753         else if (IsA(node, CoerceViaIO))
1754         {
1755                 CoerceViaIO *c = (CoerceViaIO *) node;
1756
1757                 if (c->coerceformat == COERCE_IMPLICIT_CAST)
1758                         return strip_implicit_coercions((Node *) c->arg);
1759         }
1760         else if (IsA(node, ArrayCoerceExpr))
1761         {
1762                 ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
1763
1764                 if (c->coerceformat == COERCE_IMPLICIT_CAST)
1765                         return strip_implicit_coercions((Node *) c->arg);
1766         }
1767         else if (IsA(node, ConvertRowtypeExpr))
1768         {
1769                 ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
1770
1771                 if (c->convertformat == COERCE_IMPLICIT_CAST)
1772                         return strip_implicit_coercions((Node *) c->arg);
1773         }
1774         else if (IsA(node, CoerceToDomain))
1775         {
1776                 CoerceToDomain *c = (CoerceToDomain *) node;
1777
1778                 if (c->coercionformat == COERCE_IMPLICIT_CAST)
1779                         return strip_implicit_coercions((Node *) c->arg);
1780         }
1781         return node;
1782 }
1783
1784 /*
1785  * set_coercionform_dontcare: set all CoercionForm fields to COERCE_DONTCARE
1786  *
1787  * This is used to make index expressions and index predicates more easily
1788  * comparable to clauses of queries.  CoercionForm is not semantically
1789  * significant (for cases where it does matter, the significant info is
1790  * coded into the coercion function arguments) so we can ignore it during
1791  * comparisons.  Thus, for example, an index on "foo::int4" can match an
1792  * implicit coercion to int4.
1793  *
1794  * Caution: the passed expression tree is modified in-place.
1795  */
1796 void
1797 set_coercionform_dontcare(Node *node)
1798 {
1799         (void) set_coercionform_dontcare_walker(node, NULL);
1800 }
1801
1802 static bool
1803 set_coercionform_dontcare_walker(Node *node, void *context)
1804 {
1805         if (node == NULL)
1806                 return false;
1807         if (IsA(node, FuncExpr))
1808                 ((FuncExpr *) node)->funcformat = COERCE_DONTCARE;
1809         else if (IsA(node, RelabelType))
1810                 ((RelabelType *) node)->relabelformat = COERCE_DONTCARE;
1811         else if (IsA(node, CoerceViaIO))
1812                 ((CoerceViaIO *) node)->coerceformat = COERCE_DONTCARE;
1813         else if (IsA(node, ArrayCoerceExpr))
1814                 ((ArrayCoerceExpr *) node)->coerceformat = COERCE_DONTCARE;
1815         else if (IsA(node, ConvertRowtypeExpr))
1816                 ((ConvertRowtypeExpr *) node)->convertformat = COERCE_DONTCARE;
1817         else if (IsA(node, RowExpr))
1818                 ((RowExpr *) node)->row_format = COERCE_DONTCARE;
1819         else if (IsA(node, CoerceToDomain))
1820                 ((CoerceToDomain *) node)->coercionformat = COERCE_DONTCARE;
1821         return expression_tree_walker(node, set_coercionform_dontcare_walker,
1822                                                                   context);
1823 }
1824
1825 /*
1826  * Helper for eval_const_expressions: check that datatype of an attribute
1827  * is still what it was when the expression was parsed.  This is needed to
1828  * guard against improper simplification after ALTER COLUMN TYPE.  (XXX we
1829  * may well need to make similar checks elsewhere?)
1830  */
1831 static bool
1832 rowtype_field_matches(Oid rowtypeid, int fieldnum,
1833                                           Oid expectedtype, int32 expectedtypmod)
1834 {
1835         TupleDesc       tupdesc;
1836         Form_pg_attribute attr;
1837
1838         /* No issue for RECORD, since there is no way to ALTER such a type */
1839         if (rowtypeid == RECORDOID)
1840                 return true;
1841         tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
1842         if (fieldnum <= 0 || fieldnum > tupdesc->natts)
1843         {
1844                 ReleaseTupleDesc(tupdesc);
1845                 return false;
1846         }
1847         attr = tupdesc->attrs[fieldnum - 1];
1848         if (attr->attisdropped ||
1849                 attr->atttypid != expectedtype ||
1850                 attr->atttypmod != expectedtypmod)
1851         {
1852                 ReleaseTupleDesc(tupdesc);
1853                 return false;
1854         }
1855         ReleaseTupleDesc(tupdesc);
1856         return true;
1857 }
1858
1859
1860 /*--------------------
1861  * eval_const_expressions
1862  *
1863  * Reduce any recognizably constant subexpressions of the given
1864  * expression tree, for example "2 + 2" => "4".  More interestingly,
1865  * we can reduce certain boolean expressions even when they contain
1866  * non-constant subexpressions: "x OR true" => "true" no matter what
1867  * the subexpression x is.      (XXX We assume that no such subexpression
1868  * will have important side-effects, which is not necessarily a good
1869  * assumption in the presence of user-defined functions; do we need a
1870  * pg_proc flag that prevents discarding the execution of a function?)
1871  *
1872  * We do understand that certain functions may deliver non-constant
1873  * results even with constant inputs, "nextval()" being the classic
1874  * example.  Functions that are not marked "immutable" in pg_proc
1875  * will not be pre-evaluated here, although we will reduce their
1876  * arguments as far as possible.
1877  *
1878  * We assume that the tree has already been type-checked and contains
1879  * only operators and functions that are reasonable to try to execute.
1880  *
1881  * NOTE: "root" can be passed as NULL if the caller never wants to do any
1882  * Param substitutions.
1883  *
1884  * NOTE: the planner assumes that this will always flatten nested AND and
1885  * OR clauses into N-argument form.  See comments in prepqual.c.
1886  *--------------------
1887  */
1888 Node *
1889 eval_const_expressions(PlannerInfo *root, Node *node)
1890 {
1891         eval_const_expressions_context context;
1892
1893         if (root)
1894         {
1895                 context.boundParams = root->glob->boundParams;  /* bound Params */
1896                 context.glob = root->glob; /* for inlined-function dependencies */
1897         }
1898         else
1899         {
1900                 context.boundParams = NULL;
1901                 context.glob = NULL;
1902         }
1903         context.active_fns = NIL;       /* nothing being recursively simplified */
1904         context.case_val = NULL;        /* no CASE being examined */
1905         context.estimate = false;       /* safe transformations only */
1906         return eval_const_expressions_mutator(node, &context);
1907 }
1908
1909 /*--------------------
1910  * estimate_expression_value
1911  *
1912  * This function attempts to estimate the value of an expression for
1913  * planning purposes.  It is in essence a more aggressive version of
1914  * eval_const_expressions(): we will perform constant reductions that are
1915  * not necessarily 100% safe, but are reasonable for estimation purposes.
1916  *
1917  * Currently the extra steps that are taken in this mode are:
1918  * 1. Substitute values for Params, where a bound Param value has been made
1919  *        available by the caller of planner(), even if the Param isn't marked
1920  *        constant.  This effectively means that we plan using the first supplied
1921  *        value of the Param.
1922  * 2. Fold stable, as well as immutable, functions to constants.
1923  *--------------------
1924  */
1925 Node *
1926 estimate_expression_value(PlannerInfo *root, Node *node)
1927 {
1928         eval_const_expressions_context context;
1929
1930         context.boundParams = root->glob->boundParams;          /* bound Params */
1931         /* we do not need to mark the plan as depending on inlined functions */
1932         context.glob = NULL;
1933         context.active_fns = NIL;       /* nothing being recursively simplified */
1934         context.case_val = NULL;        /* no CASE being examined */
1935         context.estimate = true;        /* unsafe transformations OK */
1936         return eval_const_expressions_mutator(node, &context);
1937 }
1938
1939 static Node *
1940 eval_const_expressions_mutator(Node *node,
1941                                                            eval_const_expressions_context *context)
1942 {
1943         if (node == NULL)
1944                 return NULL;
1945         if (IsA(node, Param))
1946         {
1947                 Param      *param = (Param *) node;
1948
1949                 /* Look to see if we've been given a value for this Param */
1950                 if (param->paramkind == PARAM_EXTERN &&
1951                         context->boundParams != NULL &&
1952                         param->paramid > 0 &&
1953                         param->paramid <= context->boundParams->numParams)
1954                 {
1955                         ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
1956
1957                         if (OidIsValid(prm->ptype))
1958                         {
1959                                 /* OK to substitute parameter value? */
1960                                 if (context->estimate || (prm->pflags & PARAM_FLAG_CONST))
1961                                 {
1962                                         /*
1963                                          * Return a Const representing the param value.  Must copy
1964                                          * pass-by-ref datatypes, since the Param might be in a
1965                                          * memory context shorter-lived than our output plan
1966                                          * should be.
1967                                          */
1968                                         int16           typLen;
1969                                         bool            typByVal;
1970                                         Datum           pval;
1971
1972                                         Assert(prm->ptype == param->paramtype);
1973                                         get_typlenbyval(param->paramtype, &typLen, &typByVal);
1974                                         if (prm->isnull || typByVal)
1975                                                 pval = prm->value;
1976                                         else
1977                                                 pval = datumCopy(prm->value, typByVal, typLen);
1978                                         return (Node *) makeConst(param->paramtype,
1979                                                                                           param->paramtypmod,
1980                                                                                           (int) typLen,
1981                                                                                           pval,
1982                                                                                           prm->isnull,
1983                                                                                           typByVal);
1984                                 }
1985                         }
1986                 }
1987                 /* Not replaceable, so just copy the Param (no need to recurse) */
1988                 return (Node *) copyObject(param);
1989         }
1990         if (IsA(node, FuncExpr))
1991         {
1992                 FuncExpr   *expr = (FuncExpr *) node;
1993                 List       *args;
1994                 Expr       *simple;
1995                 FuncExpr   *newexpr;
1996
1997                 /*
1998                  * Reduce constants in the FuncExpr's arguments.  We know args is
1999                  * either NIL or a List node, so we can call expression_tree_mutator
2000                  * directly rather than recursing to self.
2001                  */
2002                 args = (List *) expression_tree_mutator((Node *) expr->args,
2003                                                                                           eval_const_expressions_mutator,
2004                                                                                                 (void *) context);
2005
2006                 /*
2007                  * Code for op/func reduction is pretty bulky, so split it out as a
2008                  * separate function.  Note: exprTypmod normally returns -1 for a
2009                  * FuncExpr, but not when the node is recognizably a length coercion;
2010                  * we want to preserve the typmod in the eventual Const if so.
2011                  */
2012                 simple = simplify_function(expr->funcid,
2013                                                                    expr->funcresulttype, exprTypmod(node),
2014                                                                    args,
2015                                                                    true, context);
2016                 if (simple)                             /* successfully simplified it */
2017                         return (Node *) simple;
2018
2019                 /*
2020                  * The expression cannot be simplified any further, so build and
2021                  * return a replacement FuncExpr node using the possibly-simplified
2022                  * arguments.
2023                  */
2024                 newexpr = makeNode(FuncExpr);
2025                 newexpr->funcid = expr->funcid;
2026                 newexpr->funcresulttype = expr->funcresulttype;
2027                 newexpr->funcretset = expr->funcretset;
2028                 newexpr->funcformat = expr->funcformat;
2029                 newexpr->args = args;
2030                 newexpr->location = expr->location;
2031                 return (Node *) newexpr;
2032         }
2033         if (IsA(node, OpExpr))
2034         {
2035                 OpExpr     *expr = (OpExpr *) node;
2036                 List       *args;
2037                 Expr       *simple;
2038                 OpExpr     *newexpr;
2039
2040                 /*
2041                  * Reduce constants in the OpExpr's arguments.  We know args is either
2042                  * NIL or a List node, so we can call expression_tree_mutator directly
2043                  * rather than recursing to self.
2044                  */
2045                 args = (List *) expression_tree_mutator((Node *) expr->args,
2046                                                                                           eval_const_expressions_mutator,
2047                                                                                                 (void *) context);
2048
2049                 /*
2050                  * Need to get OID of underlying function.      Okay to scribble on input
2051                  * to this extent.
2052                  */
2053                 set_opfuncid(expr);
2054
2055                 /*
2056                  * Code for op/func reduction is pretty bulky, so split it out as a
2057                  * separate function.
2058                  */
2059                 simple = simplify_function(expr->opfuncid,
2060                                                                    expr->opresulttype, -1,
2061                                                                    args,
2062                                                                    true, context);
2063                 if (simple)                             /* successfully simplified it */
2064                         return (Node *) simple;
2065
2066                 /*
2067                  * If the operator is boolean equality, we know how to simplify cases
2068                  * involving one constant and one non-constant argument.
2069                  */
2070                 if (expr->opno == BooleanEqualOperator)
2071                 {
2072                         simple = simplify_boolean_equality(args);
2073                         if (simple)                     /* successfully simplified it */
2074                                 return (Node *) simple;
2075                 }
2076
2077                 /*
2078                  * The expression cannot be simplified any further, so build and
2079                  * return a replacement OpExpr node using the possibly-simplified
2080                  * arguments.
2081                  */
2082                 newexpr = makeNode(OpExpr);
2083                 newexpr->opno = expr->opno;
2084                 newexpr->opfuncid = expr->opfuncid;
2085                 newexpr->opresulttype = expr->opresulttype;
2086                 newexpr->opretset = expr->opretset;
2087                 newexpr->args = args;
2088                 newexpr->location = expr->location;
2089                 return (Node *) newexpr;
2090         }
2091         if (IsA(node, DistinctExpr))
2092         {
2093                 DistinctExpr *expr = (DistinctExpr *) node;
2094                 List       *args;
2095                 ListCell   *arg;
2096                 bool            has_null_input = false;
2097                 bool            all_null_input = true;
2098                 bool            has_nonconst_input = false;
2099                 Expr       *simple;
2100                 DistinctExpr *newexpr;
2101
2102                 /*
2103                  * Reduce constants in the DistinctExpr's arguments.  We know args is
2104                  * either NIL or a List node, so we can call expression_tree_mutator
2105                  * directly rather than recursing to self.
2106                  */
2107                 args = (List *) expression_tree_mutator((Node *) expr->args,
2108                                                                                           eval_const_expressions_mutator,
2109                                                                                                 (void *) context);
2110
2111                 /*
2112                  * We must do our own check for NULLs because DistinctExpr has
2113                  * different results for NULL input than the underlying operator does.
2114                  */
2115                 foreach(arg, args)
2116                 {
2117                         if (IsA(lfirst(arg), Const))
2118                         {
2119                                 has_null_input |= ((Const *) lfirst(arg))->constisnull;
2120                                 all_null_input &= ((Const *) lfirst(arg))->constisnull;
2121                         }
2122                         else
2123                                 has_nonconst_input = true;
2124                 }
2125
2126                 /* all constants? then can optimize this out */
2127                 if (!has_nonconst_input)
2128                 {
2129                         /* all nulls? then not distinct */
2130                         if (all_null_input)
2131                                 return makeBoolConst(false, false);
2132
2133                         /* one null? then distinct */
2134                         if (has_null_input)
2135                                 return makeBoolConst(true, false);
2136
2137                         /* otherwise try to evaluate the '=' operator */
2138                         /* (NOT okay to try to inline it, though!) */
2139
2140                         /*
2141                          * Need to get OID of underlying function.      Okay to scribble on
2142                          * input to this extent.
2143                          */
2144                         set_opfuncid((OpExpr *) expr);          /* rely on struct equivalence */
2145
2146                         /*
2147                          * Code for op/func reduction is pretty bulky, so split it out as
2148                          * a separate function.
2149                          */
2150                         simple = simplify_function(expr->opfuncid,
2151                                                                            expr->opresulttype, -1,
2152                                                                            args,
2153                                                                            false, context);
2154                         if (simple)                     /* successfully simplified it */
2155                         {
2156                                 /*
2157                                  * Since the underlying operator is "=", must negate its
2158                                  * result
2159                                  */
2160                                 Const      *csimple = (Const *) simple;
2161
2162                                 Assert(IsA(csimple, Const));
2163                                 csimple->constvalue =
2164                                         BoolGetDatum(!DatumGetBool(csimple->constvalue));
2165                                 return (Node *) csimple;
2166                         }
2167                 }
2168
2169                 /*
2170                  * The expression cannot be simplified any further, so build and
2171                  * return a replacement DistinctExpr node using the
2172                  * possibly-simplified arguments.
2173                  */
2174                 newexpr = makeNode(DistinctExpr);
2175                 newexpr->opno = expr->opno;
2176                 newexpr->opfuncid = expr->opfuncid;
2177                 newexpr->opresulttype = expr->opresulttype;
2178                 newexpr->opretset = expr->opretset;
2179                 newexpr->args = args;
2180                 newexpr->location = expr->location;
2181                 return (Node *) newexpr;
2182         }
2183         if (IsA(node, BoolExpr))
2184         {
2185                 BoolExpr   *expr = (BoolExpr *) node;
2186
2187                 switch (expr->boolop)
2188                 {
2189                         case OR_EXPR:
2190                                 {
2191                                         List       *newargs;
2192                                         bool            haveNull = false;
2193                                         bool            forceTrue = false;
2194
2195                                         newargs = simplify_or_arguments(expr->args, context,
2196                                                                                                         &haveNull, &forceTrue);
2197                                         if (forceTrue)
2198                                                 return makeBoolConst(true, false);
2199                                         if (haveNull)
2200                                                 newargs = lappend(newargs, makeBoolConst(false, true));
2201                                         /* If all the inputs are FALSE, result is FALSE */
2202                                         if (newargs == NIL)
2203                                                 return makeBoolConst(false, false);
2204                                         /* If only one nonconst-or-NULL input, it's the result */
2205                                         if (list_length(newargs) == 1)
2206                                                 return (Node *) linitial(newargs);
2207                                         /* Else we still need an OR node */
2208                                         return (Node *) make_orclause(newargs);
2209                                 }
2210                         case AND_EXPR:
2211                                 {
2212                                         List       *newargs;
2213                                         bool            haveNull = false;
2214                                         bool            forceFalse = false;
2215
2216                                         newargs = simplify_and_arguments(expr->args, context,
2217                                                                                                          &haveNull, &forceFalse);
2218                                         if (forceFalse)
2219                                                 return makeBoolConst(false, false);
2220                                         if (haveNull)
2221                                                 newargs = lappend(newargs, makeBoolConst(false, true));
2222                                         /* If all the inputs are TRUE, result is TRUE */
2223                                         if (newargs == NIL)
2224                                                 return makeBoolConst(true, false);
2225                                         /* If only one nonconst-or-NULL input, it's the result */
2226                                         if (list_length(newargs) == 1)
2227                                                 return (Node *) linitial(newargs);
2228                                         /* Else we still need an AND node */
2229                                         return (Node *) make_andclause(newargs);
2230                                 }
2231                         case NOT_EXPR:
2232                                 {
2233                                         Node       *arg;
2234
2235                                         Assert(list_length(expr->args) == 1);
2236                                         arg = eval_const_expressions_mutator(linitial(expr->args),
2237                                                                                                                  context);
2238                                         if (IsA(arg, Const))
2239                                         {
2240                                                 Const      *const_input = (Const *) arg;
2241
2242                                                 /* NOT NULL => NULL */
2243                                                 if (const_input->constisnull)
2244                                                         return makeBoolConst(false, true);
2245                                                 /* otherwise pretty easy */
2246                                                 return makeBoolConst(!DatumGetBool(const_input->constvalue),
2247                                                                                          false);
2248                                         }
2249                                         else if (not_clause(arg))
2250                                         {
2251                                                 /* Cancel NOT/NOT */
2252                                                 return (Node *) get_notclausearg((Expr *) arg);
2253                                         }
2254                                         /* Else we still need a NOT node */
2255                                         return (Node *) make_notclause((Expr *) arg);
2256                                 }
2257                         default:
2258                                 elog(ERROR, "unrecognized boolop: %d",
2259                                          (int) expr->boolop);
2260                                 break;
2261                 }
2262         }
2263         if (IsA(node, SubPlan) ||
2264                 IsA(node, AlternativeSubPlan))
2265         {
2266                 /*
2267                  * Return a SubPlan unchanged --- too late to do anything with it.
2268                  *
2269                  * XXX should we ereport() here instead?  Probably this routine should
2270                  * never be invoked after SubPlan creation.
2271                  */
2272                 return node;
2273         }
2274         if (IsA(node, RelabelType))
2275         {
2276                 /*
2277                  * If we can simplify the input to a constant, then we don't need the
2278                  * RelabelType node anymore: just change the type field of the Const
2279                  * node.  Otherwise, must copy the RelabelType node.
2280                  */
2281                 RelabelType *relabel = (RelabelType *) node;
2282                 Node       *arg;
2283
2284                 arg = eval_const_expressions_mutator((Node *) relabel->arg,
2285                                                                                          context);
2286
2287                 /*
2288                  * If we find stacked RelabelTypes (eg, from foo :: int :: oid) we can
2289                  * discard all but the top one.
2290                  */
2291                 while (arg && IsA(arg, RelabelType))
2292                         arg = (Node *) ((RelabelType *) arg)->arg;
2293
2294                 if (arg && IsA(arg, Const))
2295                 {
2296                         Const      *con = (Const *) arg;
2297
2298                         con->consttype = relabel->resulttype;
2299                         con->consttypmod = relabel->resulttypmod;
2300                         return (Node *) con;
2301                 }
2302                 else
2303                 {
2304                         RelabelType *newrelabel = makeNode(RelabelType);
2305
2306                         newrelabel->arg = (Expr *) arg;
2307                         newrelabel->resulttype = relabel->resulttype;
2308                         newrelabel->resulttypmod = relabel->resulttypmod;
2309                         newrelabel->relabelformat = relabel->relabelformat;
2310                         newrelabel->location = relabel->location;
2311                         return (Node *) newrelabel;
2312                 }
2313         }
2314         if (IsA(node, CoerceViaIO))
2315         {
2316                 CoerceViaIO *expr = (CoerceViaIO *) node;
2317                 Expr       *arg;
2318                 Oid                     outfunc;
2319                 bool            outtypisvarlena;
2320                 Oid                     infunc;
2321                 Oid                     intypioparam;
2322                 Expr       *simple;
2323                 CoerceViaIO *newexpr;
2324
2325                 /*
2326                  * Reduce constants in the CoerceViaIO's argument.
2327                  */
2328                 arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg,
2329                                                                                                           context);
2330
2331                 /*
2332                  * CoerceViaIO represents calling the source type's output function
2333                  * then the result type's input function.  So, try to simplify it
2334                  * as though it were a stack of two such function calls.  First we
2335                  * need to know what the functions are.
2336                  */
2337                 getTypeOutputInfo(exprType((Node *) arg), &outfunc, &outtypisvarlena);
2338                 getTypeInputInfo(expr->resulttype, &infunc, &intypioparam);
2339
2340                 simple = simplify_function(outfunc,
2341                                                                    CSTRINGOID, -1,
2342                                                                    list_make1(arg),
2343                                                                    true, context);
2344                 if (simple)                             /* successfully simplified output fn */
2345                 {
2346                         /*
2347                          * Input functions may want 1 to 3 arguments.  We always supply
2348                          * all three, trusting that nothing downstream will complain.
2349                          */
2350                         List       *args;
2351
2352                         args = list_make3(simple,
2353                                                           makeConst(OIDOID, -1, sizeof(Oid),
2354                                                                                 ObjectIdGetDatum(intypioparam),
2355                                                                                 false, true),
2356                                                           makeConst(INT4OID, -1, sizeof(int32),
2357                                                                                 Int32GetDatum(-1),
2358                                                                                 false, true));
2359
2360                         simple = simplify_function(infunc,
2361                                                                            expr->resulttype, -1,
2362                                                                            args,
2363                                                                            true, context);
2364                         if (simple)                     /* successfully simplified input fn */
2365                                 return (Node *) simple;
2366                 }
2367
2368                 /*
2369                  * The expression cannot be simplified any further, so build and
2370                  * return a replacement CoerceViaIO node using the possibly-simplified
2371                  * argument.
2372                  */
2373                 newexpr = makeNode(CoerceViaIO);
2374                 newexpr->arg = arg;
2375                 newexpr->resulttype = expr->resulttype;
2376                 newexpr->coerceformat = expr->coerceformat;
2377                 newexpr->location = expr->location;
2378                 return (Node *) newexpr;
2379         }
2380         if (IsA(node, ArrayCoerceExpr))
2381         {
2382                 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
2383                 Expr       *arg;
2384                 ArrayCoerceExpr *newexpr;
2385
2386                 /*
2387                  * Reduce constants in the ArrayCoerceExpr's argument, then build
2388                  * a new ArrayCoerceExpr.
2389                  */
2390                 arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg,
2391                                                                                                           context);
2392
2393                 newexpr = makeNode(ArrayCoerceExpr);
2394                 newexpr->arg = arg;
2395                 newexpr->elemfuncid = expr->elemfuncid;
2396                 newexpr->resulttype = expr->resulttype;
2397                 newexpr->resulttypmod = expr->resulttypmod;
2398                 newexpr->isExplicit = expr->isExplicit;
2399                 newexpr->coerceformat = expr->coerceformat;
2400                 newexpr->location = expr->location;
2401
2402                 /*
2403                  * If constant argument and it's a binary-coercible or immutable
2404                  * conversion, we can simplify it to a constant.
2405                  */
2406                 if (arg && IsA(arg, Const) &&
2407                         (!OidIsValid(newexpr->elemfuncid) ||
2408                          func_volatile(newexpr->elemfuncid) == PROVOLATILE_IMMUTABLE))
2409                         return (Node *) evaluate_expr((Expr *) newexpr,
2410                                                                                   newexpr->resulttype,
2411                                                                                   newexpr->resulttypmod);
2412
2413                 /* Else we must return the partially-simplified node */
2414                 return (Node *) newexpr;
2415         }
2416         if (IsA(node, CaseExpr))
2417         {
2418                 /*----------
2419                  * CASE expressions can be simplified if there are constant
2420                  * condition clauses:
2421                  *              FALSE (or NULL): drop the alternative
2422                  *              TRUE: drop all remaining alternatives
2423                  * If the first non-FALSE alternative is a constant TRUE, we can
2424                  * simplify the entire CASE to that alternative's expression.
2425                  * If there are no non-FALSE alternatives, we simplify the entire
2426                  * CASE to the default result (ELSE result).
2427                  *
2428                  * If we have a simple-form CASE with constant test expression,
2429                  * we substitute the constant value for contained CaseTestExpr
2430                  * placeholder nodes, so that we have the opportunity to reduce
2431                  * constant test conditions.  For example this allows
2432                  *              CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
2433                  * to reduce to 1 rather than drawing a divide-by-0 error.
2434                  *----------
2435                  */
2436                 CaseExpr   *caseexpr = (CaseExpr *) node;
2437                 CaseExpr   *newcase;
2438                 Node       *save_case_val;
2439                 Node       *newarg;
2440                 List       *newargs;
2441                 bool            const_true_cond;
2442                 Node       *defresult = NULL;
2443                 ListCell   *arg;
2444
2445                 /* Simplify the test expression, if any */
2446                 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
2447                                                                                                 context);
2448
2449                 /* Set up for contained CaseTestExpr nodes */
2450                 save_case_val = context->case_val;
2451                 if (newarg && IsA(newarg, Const))
2452                         context->case_val = newarg;
2453                 else
2454                         context->case_val = NULL;
2455
2456                 /* Simplify the WHEN clauses */
2457                 newargs = NIL;
2458                 const_true_cond = false;
2459                 foreach(arg, caseexpr->args)
2460                 {
2461                         CaseWhen   *oldcasewhen = (CaseWhen *) lfirst(arg);
2462                         Node       *casecond;
2463                         Node       *caseresult;
2464
2465                         Assert(IsA(oldcasewhen, CaseWhen));
2466
2467                         /* Simplify this alternative's test condition */
2468                         casecond =
2469                                 eval_const_expressions_mutator((Node *) oldcasewhen->expr,
2470                                                                                            context);
2471
2472                         /*
2473                          * If the test condition is constant FALSE (or NULL), then drop
2474                          * this WHEN clause completely, without processing the result.
2475                          */
2476                         if (casecond && IsA(casecond, Const))
2477                         {
2478                                 Const      *const_input = (Const *) casecond;
2479
2480                                 if (const_input->constisnull ||
2481                                         !DatumGetBool(const_input->constvalue))
2482                                         continue;       /* drop alternative with FALSE condition */
2483                                 /* Else it's constant TRUE */
2484                                 const_true_cond = true;
2485                         }
2486
2487                         /* Simplify this alternative's result value */
2488                         caseresult =
2489                                 eval_const_expressions_mutator((Node *) oldcasewhen->result,
2490                                                                                            context);
2491
2492                         /* If non-constant test condition, emit a new WHEN node */
2493                         if (!const_true_cond)
2494                         {
2495                                 CaseWhen   *newcasewhen = makeNode(CaseWhen);
2496
2497                                 newcasewhen->expr = (Expr *) casecond;
2498                                 newcasewhen->result = (Expr *) caseresult;
2499                                 newcasewhen->location = oldcasewhen->location;
2500                                 newargs = lappend(newargs, newcasewhen);
2501                                 continue;
2502                         }
2503
2504                         /*
2505                          * Found a TRUE condition, so none of the remaining alternatives
2506                          * can be reached.      We treat the result as the default result.
2507                          */
2508                         defresult = caseresult;
2509                         break;
2510                 }
2511
2512                 /* Simplify the default result, unless we replaced it above */
2513                 if (!const_true_cond)
2514                         defresult =
2515                                 eval_const_expressions_mutator((Node *) caseexpr->defresult,
2516                                                                                            context);
2517
2518                 context->case_val = save_case_val;
2519
2520                 /* If no non-FALSE alternatives, CASE reduces to the default result */
2521                 if (newargs == NIL)
2522                         return defresult;
2523                 /* Otherwise we need a new CASE node */
2524                 newcase = makeNode(CaseExpr);
2525                 newcase->casetype = caseexpr->casetype;
2526                 newcase->arg = (Expr *) newarg;
2527                 newcase->args = newargs;
2528                 newcase->defresult = (Expr *) defresult;
2529                 newcase->location = caseexpr->location;
2530                 return (Node *) newcase;
2531         }
2532         if (IsA(node, CaseTestExpr))
2533         {
2534                 /*
2535                  * If we know a constant test value for the current CASE construct,
2536                  * substitute it for the placeholder.  Else just return the
2537                  * placeholder as-is.
2538                  */
2539                 if (context->case_val)
2540                         return copyObject(context->case_val);
2541                 else
2542                         return copyObject(node);
2543         }
2544         if (IsA(node, ArrayExpr))
2545         {
2546                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
2547                 ArrayExpr  *newarray;
2548                 bool            all_const = true;
2549                 List       *newelems;
2550                 ListCell   *element;
2551
2552                 newelems = NIL;
2553                 foreach(element, arrayexpr->elements)
2554                 {
2555                         Node       *e;
2556
2557                         e = eval_const_expressions_mutator((Node *) lfirst(element),
2558                                                                                            context);
2559                         if (!IsA(e, Const))
2560                                 all_const = false;
2561                         newelems = lappend(newelems, e);
2562                 }
2563
2564                 newarray = makeNode(ArrayExpr);
2565                 newarray->array_typeid = arrayexpr->array_typeid;
2566                 newarray->element_typeid = arrayexpr->element_typeid;
2567                 newarray->elements = newelems;
2568                 newarray->multidims = arrayexpr->multidims;
2569                 newarray->location = arrayexpr->location;
2570
2571                 if (all_const)
2572                         return (Node *) evaluate_expr((Expr *) newarray,
2573                                                                                   newarray->array_typeid,
2574                                                                                   exprTypmod(node));
2575
2576                 return (Node *) newarray;
2577         }
2578         if (IsA(node, CoalesceExpr))
2579         {
2580                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2581                 CoalesceExpr *newcoalesce;
2582                 List       *newargs;
2583                 ListCell   *arg;
2584
2585                 newargs = NIL;
2586                 foreach(arg, coalesceexpr->args)
2587                 {
2588                         Node       *e;
2589
2590                         e = eval_const_expressions_mutator((Node *) lfirst(arg),
2591                                                                                            context);
2592
2593                         /*
2594                          * We can remove null constants from the list. For a non-null
2595                          * constant, if it has not been preceded by any other
2596                          * non-null-constant expressions then that is the result.
2597                          */
2598                         if (IsA(e, Const))
2599                         {
2600                                 if (((Const *) e)->constisnull)
2601                                         continue;       /* drop null constant */
2602                                 if (newargs == NIL)
2603                                         return e;       /* first expr */
2604                         }
2605                         newargs = lappend(newargs, e);
2606                 }
2607
2608                 /* If all the arguments were constant null, the result is just null */
2609                 if (newargs == NIL)
2610                         return (Node *) makeNullConst(coalesceexpr->coalescetype, -1);
2611
2612                 newcoalesce = makeNode(CoalesceExpr);
2613                 newcoalesce->coalescetype = coalesceexpr->coalescetype;
2614                 newcoalesce->args = newargs;
2615                 newcoalesce->location = coalesceexpr->location;
2616                 return (Node *) newcoalesce;
2617         }
2618         if (IsA(node, FieldSelect))
2619         {
2620                 /*
2621                  * We can optimize field selection from a whole-row Var into a simple
2622                  * Var.  (This case won't be generated directly by the parser, because
2623                  * ParseComplexProjection short-circuits it. But it can arise while
2624                  * simplifying functions.)      Also, we can optimize field selection from
2625                  * a RowExpr construct.
2626                  *
2627                  * We must however check that the declared type of the field is still
2628                  * the same as when the FieldSelect was created --- this can change if
2629                  * someone did ALTER COLUMN TYPE on the rowtype.
2630                  */
2631                 FieldSelect *fselect = (FieldSelect *) node;
2632                 FieldSelect *newfselect;
2633                 Node       *arg;
2634
2635                 arg = eval_const_expressions_mutator((Node *) fselect->arg,
2636                                                                                          context);
2637                 if (arg && IsA(arg, Var) &&
2638                         ((Var *) arg)->varattno == InvalidAttrNumber)
2639                 {
2640                         if (rowtype_field_matches(((Var *) arg)->vartype,
2641                                                                           fselect->fieldnum,
2642                                                                           fselect->resulttype,
2643                                                                           fselect->resulttypmod))
2644                                 return (Node *) makeVar(((Var *) arg)->varno,
2645                                                                                 fselect->fieldnum,
2646                                                                                 fselect->resulttype,
2647                                                                                 fselect->resulttypmod,
2648                                                                                 ((Var *) arg)->varlevelsup);
2649                 }
2650                 if (arg && IsA(arg, RowExpr))
2651                 {
2652                         RowExpr    *rowexpr = (RowExpr *) arg;
2653
2654                         if (fselect->fieldnum > 0 &&
2655                                 fselect->fieldnum <= list_length(rowexpr->args))
2656                         {
2657                                 Node       *fld = (Node *) list_nth(rowexpr->args,
2658                                                                                                         fselect->fieldnum - 1);
2659
2660                                 if (rowtype_field_matches(rowexpr->row_typeid,
2661                                                                                   fselect->fieldnum,
2662                                                                                   fselect->resulttype,
2663                                                                                   fselect->resulttypmod) &&
2664                                         fselect->resulttype == exprType(fld) &&
2665                                         fselect->resulttypmod == exprTypmod(fld))
2666                                         return fld;
2667                         }
2668                 }
2669                 newfselect = makeNode(FieldSelect);
2670                 newfselect->arg = (Expr *) arg;
2671                 newfselect->fieldnum = fselect->fieldnum;
2672                 newfselect->resulttype = fselect->resulttype;
2673                 newfselect->resulttypmod = fselect->resulttypmod;
2674                 return (Node *) newfselect;
2675         }
2676         if (IsA(node, NullTest))
2677         {
2678                 NullTest   *ntest = (NullTest *) node;
2679                 NullTest   *newntest;
2680                 Node       *arg;
2681
2682                 arg = eval_const_expressions_mutator((Node *) ntest->arg,
2683                                                                                          context);
2684                 if (arg && IsA(arg, RowExpr))
2685                 {
2686                         RowExpr    *rarg = (RowExpr *) arg;
2687                         List       *newargs = NIL;
2688                         ListCell   *l;
2689
2690                         /*
2691                          * We break ROW(...) IS [NOT] NULL into separate tests on its
2692                          * component fields.  This form is usually more efficient to
2693                          * evaluate, as well as being more amenable to optimization.
2694                          */
2695                         foreach(l, rarg->args)
2696                         {
2697                                 Node       *relem = (Node *) lfirst(l);
2698
2699                                 /*
2700                                  * A constant field refutes the whole NullTest if it's of the
2701                                  * wrong nullness; else we can discard it.
2702                                  */
2703                                 if (relem && IsA(relem, Const))
2704                                 {
2705                                         Const      *carg = (Const *) relem;
2706
2707                                         if (carg->constisnull ?
2708                                                 (ntest->nulltesttype == IS_NOT_NULL) :
2709                                                 (ntest->nulltesttype == IS_NULL))
2710                                                 return makeBoolConst(false, false);
2711                                         continue;
2712                                 }
2713                                 newntest = makeNode(NullTest);
2714                                 newntest->arg = (Expr *) relem;
2715                                 newntest->nulltesttype = ntest->nulltesttype;
2716                                 newargs = lappend(newargs, newntest);
2717                         }
2718                         /* If all the inputs were constants, result is TRUE */
2719                         if (newargs == NIL)
2720                                 return makeBoolConst(true, false);
2721                         /* If only one nonconst input, it's the result */
2722                         if (list_length(newargs) == 1)
2723                                 return (Node *) linitial(newargs);
2724                         /* Else we need an AND node */
2725                         return (Node *) make_andclause(newargs);
2726                 }
2727                 if (arg && IsA(arg, Const))
2728                 {
2729                         Const      *carg = (Const *) arg;
2730                         bool            result;
2731
2732                         switch (ntest->nulltesttype)
2733                         {
2734                                 case IS_NULL:
2735                                         result = carg->constisnull;
2736                                         break;
2737                                 case IS_NOT_NULL:
2738                                         result = !carg->constisnull;
2739                                         break;
2740                                 default:
2741                                         elog(ERROR, "unrecognized nulltesttype: %d",
2742                                                  (int) ntest->nulltesttype);
2743                                         result = false;         /* keep compiler quiet */
2744                                         break;
2745                         }
2746
2747                         return makeBoolConst(result, false);
2748                 }
2749
2750                 newntest = makeNode(NullTest);
2751                 newntest->arg = (Expr *) arg;
2752                 newntest->nulltesttype = ntest->nulltesttype;
2753                 return (Node *) newntest;
2754         }
2755         if (IsA(node, BooleanTest))
2756         {
2757                 BooleanTest *btest = (BooleanTest *) node;
2758                 BooleanTest *newbtest;
2759                 Node       *arg;
2760
2761                 arg = eval_const_expressions_mutator((Node *) btest->arg,
2762                                                                                          context);
2763                 if (arg && IsA(arg, Const))
2764                 {
2765                         Const      *carg = (Const *) arg;
2766                         bool            result;
2767
2768                         switch (btest->booltesttype)
2769                         {
2770                                 case IS_TRUE:
2771                                         result = (!carg->constisnull &&
2772                                                           DatumGetBool(carg->constvalue));
2773                                         break;
2774                                 case IS_NOT_TRUE:
2775                                         result = (carg->constisnull ||
2776                                                           !DatumGetBool(carg->constvalue));
2777                                         break;
2778                                 case IS_FALSE:
2779                                         result = (!carg->constisnull &&
2780                                                           !DatumGetBool(carg->constvalue));
2781                                         break;
2782                                 case IS_NOT_FALSE:
2783                                         result = (carg->constisnull ||
2784                                                           DatumGetBool(carg->constvalue));
2785                                         break;
2786                                 case IS_UNKNOWN:
2787                                         result = carg->constisnull;
2788                                         break;
2789                                 case IS_NOT_UNKNOWN:
2790                                         result = !carg->constisnull;
2791                                         break;
2792                                 default:
2793                                         elog(ERROR, "unrecognized booltesttype: %d",
2794                                                  (int) btest->booltesttype);
2795                                         result = false;         /* keep compiler quiet */
2796                                         break;
2797                         }
2798
2799                         return makeBoolConst(result, false);
2800                 }
2801
2802                 newbtest = makeNode(BooleanTest);
2803                 newbtest->arg = (Expr *) arg;
2804                 newbtest->booltesttype = btest->booltesttype;
2805                 return (Node *) newbtest;
2806         }
2807         if (IsA(node, FlattenedSubLink))
2808         {
2809                 FlattenedSubLink *fslink = (FlattenedSubLink *) node;
2810                 FlattenedSubLink *newfslink;
2811                 Expr       *quals;
2812
2813                 /* Simplify and also canonicalize the arguments */
2814                 quals = (Expr *) eval_const_expressions_mutator((Node *) fslink->quals,
2815                                                                                                                 context);
2816                 quals = canonicalize_qual(quals);
2817
2818                 newfslink = makeNode(FlattenedSubLink);
2819                 newfslink->jointype = fslink->jointype;
2820                 newfslink->lefthand = fslink->lefthand;
2821                 newfslink->righthand = fslink->righthand;
2822                 newfslink->quals = quals;
2823                 return (Node *) newfslink;
2824         }
2825
2826         /*
2827          * For any node type not handled above, we recurse using
2828          * expression_tree_mutator, which will copy the node unchanged but try to
2829          * simplify its arguments (if any) using this routine. For example: we
2830          * cannot eliminate an ArrayRef node, but we might be able to simplify
2831          * constant expressions in its subscripts.
2832          */
2833         return expression_tree_mutator(node, eval_const_expressions_mutator,
2834                                                                    (void *) context);
2835 }
2836
2837 /*
2838  * Subroutine for eval_const_expressions: process arguments of an OR clause
2839  *
2840  * This includes flattening of nested ORs as well as recursion to
2841  * eval_const_expressions to simplify the OR arguments.
2842  *
2843  * After simplification, OR arguments are handled as follows:
2844  *              non constant: keep
2845  *              FALSE: drop (does not affect result)
2846  *              TRUE: force result to TRUE
2847  *              NULL: keep only one
2848  * We must keep one NULL input because ExecEvalOr returns NULL when no input
2849  * is TRUE and at least one is NULL.  We don't actually include the NULL
2850  * here, that's supposed to be done by the caller.
2851  *
2852  * The output arguments *haveNull and *forceTrue must be initialized FALSE
2853  * by the caller.  They will be set TRUE if a null constant or true constant,
2854  * respectively, is detected anywhere in the argument list.
2855  */
2856 static List *
2857 simplify_or_arguments(List *args,
2858                                           eval_const_expressions_context *context,
2859                                           bool *haveNull, bool *forceTrue)
2860 {
2861         List       *newargs = NIL;
2862         List       *unprocessed_args;
2863
2864         /*
2865          * Since the parser considers OR to be a binary operator, long OR lists
2866          * become deeply nested expressions.  We must flatten these into long
2867          * argument lists of a single OR operator.      To avoid blowing out the stack
2868          * with recursion of eval_const_expressions, we resort to some tenseness
2869          * here: we keep a list of not-yet-processed inputs, and handle flattening
2870          * of nested ORs by prepending to the to-do list instead of recursing.
2871          */
2872         unprocessed_args = list_copy(args);
2873         while (unprocessed_args)
2874         {
2875                 Node       *arg = (Node *) linitial(unprocessed_args);
2876
2877                 unprocessed_args = list_delete_first(unprocessed_args);
2878
2879                 /* flatten nested ORs as per above comment */
2880                 if (or_clause(arg))
2881                 {
2882                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
2883
2884                         /* overly tense code to avoid leaking unused list header */
2885                         if (!unprocessed_args)
2886                                 unprocessed_args = subargs;
2887                         else
2888                         {
2889                                 List       *oldhdr = unprocessed_args;
2890
2891                                 unprocessed_args = list_concat(subargs, unprocessed_args);
2892                                 pfree(oldhdr);
2893                         }
2894                         continue;
2895                 }
2896
2897                 /* If it's not an OR, simplify it */
2898                 arg = eval_const_expressions_mutator(arg, context);
2899
2900                 /*
2901                  * It is unlikely but not impossible for simplification of a non-OR
2902                  * clause to produce an OR.  Recheck, but don't be too tense about it
2903                  * since it's not a mainstream case. In particular we don't worry
2904                  * about const-simplifying the input twice.
2905                  */
2906                 if (or_clause(arg))
2907                 {
2908                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
2909
2910                         unprocessed_args = list_concat(subargs, unprocessed_args);
2911                         continue;
2912                 }
2913
2914                 /*
2915                  * OK, we have a const-simplified non-OR argument.      Process it per
2916                  * comments above.
2917                  */
2918                 if (IsA(arg, Const))
2919                 {
2920                         Const      *const_input = (Const *) arg;
2921
2922                         if (const_input->constisnull)
2923                                 *haveNull = true;
2924                         else if (DatumGetBool(const_input->constvalue))
2925                         {
2926                                 *forceTrue = true;
2927
2928                                 /*
2929                                  * Once we detect a TRUE result we can just exit the loop
2930                                  * immediately.  However, if we ever add a notion of
2931                                  * non-removable functions, we'd need to keep scanning.
2932                                  */
2933                                 return NIL;
2934                         }
2935                         /* otherwise, we can drop the constant-false input */
2936                         continue;
2937                 }
2938
2939                 /* else emit the simplified arg into the result list */
2940                 newargs = lappend(newargs, arg);
2941         }
2942
2943         return newargs;
2944 }
2945
2946 /*
2947  * Subroutine for eval_const_expressions: process arguments of an AND clause
2948  *
2949  * This includes flattening of nested ANDs as well as recursion to
2950  * eval_const_expressions to simplify the AND arguments.
2951  *
2952  * After simplification, AND arguments are handled as follows:
2953  *              non constant: keep
2954  *              TRUE: drop (does not affect result)
2955  *              FALSE: force result to FALSE
2956  *              NULL: keep only one
2957  * We must keep one NULL input because ExecEvalAnd returns NULL when no input
2958  * is FALSE and at least one is NULL.  We don't actually include the NULL
2959  * here, that's supposed to be done by the caller.
2960  *
2961  * The output arguments *haveNull and *forceFalse must be initialized FALSE
2962  * by the caller.  They will be set TRUE if a null constant or false constant,
2963  * respectively, is detected anywhere in the argument list.
2964  */
2965 static List *
2966 simplify_and_arguments(List *args,
2967                                            eval_const_expressions_context *context,
2968                                            bool *haveNull, bool *forceFalse)
2969 {
2970         List       *newargs = NIL;
2971         List       *unprocessed_args;
2972
2973         /* See comments in simplify_or_arguments */
2974         unprocessed_args = list_copy(args);
2975         while (unprocessed_args)
2976         {
2977                 Node       *arg = (Node *) linitial(unprocessed_args);
2978
2979                 unprocessed_args = list_delete_first(unprocessed_args);
2980
2981                 /* flatten nested ANDs as per above comment */
2982                 if (and_clause(arg))
2983                 {
2984                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
2985
2986                         /* overly tense code to avoid leaking unused list header */
2987                         if (!unprocessed_args)
2988                                 unprocessed_args = subargs;
2989                         else
2990                         {
2991                                 List       *oldhdr = unprocessed_args;
2992
2993                                 unprocessed_args = list_concat(subargs, unprocessed_args);
2994                                 pfree(oldhdr);
2995                         }
2996                         continue;
2997                 }
2998
2999                 /* If it's not an AND, simplify it */
3000                 arg = eval_const_expressions_mutator(arg, context);
3001
3002                 /*
3003                  * It is unlikely but not impossible for simplification of a non-AND
3004                  * clause to produce an AND.  Recheck, but don't be too tense about it
3005                  * since it's not a mainstream case. In particular we don't worry
3006                  * about const-simplifying the input twice.
3007                  */
3008                 if (and_clause(arg))
3009                 {
3010                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
3011
3012                         unprocessed_args = list_concat(subargs, unprocessed_args);
3013                         continue;
3014                 }
3015
3016                 /*
3017                  * OK, we have a const-simplified non-AND argument.  Process it per
3018                  * comments above.
3019                  */
3020                 if (IsA(arg, Const))
3021                 {
3022                         Const      *const_input = (Const *) arg;
3023
3024                         if (const_input->constisnull)
3025                                 *haveNull = true;
3026                         else if (!DatumGetBool(const_input->constvalue))
3027                         {
3028                                 *forceFalse = true;
3029
3030                                 /*
3031                                  * Once we detect a FALSE result we can just exit the loop
3032                                  * immediately.  However, if we ever add a notion of
3033                                  * non-removable functions, we'd need to keep scanning.
3034                                  */
3035                                 return NIL;
3036                         }
3037                         /* otherwise, we can drop the constant-true input */
3038                         continue;
3039                 }
3040
3041                 /* else emit the simplified arg into the result list */
3042                 newargs = lappend(newargs, arg);
3043         }
3044
3045         return newargs;
3046 }
3047
3048 /*
3049  * Subroutine for eval_const_expressions: try to simplify boolean equality
3050  *
3051  * Input is the list of simplified arguments to the operator.
3052  * Returns a simplified expression if successful, or NULL if cannot
3053  * simplify the expression.
3054  *
3055  * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x".
3056  * This is only marginally useful in itself, but doing it in constant folding
3057  * ensures that we will recognize the two forms as being equivalent in, for
3058  * example, partial index matching.
3059  *
3060  * We come here only if simplify_function has failed; therefore we cannot
3061  * see two constant inputs, nor a constant-NULL input.
3062  */
3063 static Expr *
3064 simplify_boolean_equality(List *args)
3065 {
3066         Expr       *leftop;
3067         Expr       *rightop;
3068
3069         Assert(list_length(args) == 2);
3070         leftop = linitial(args);
3071         rightop = lsecond(args);
3072         if (leftop && IsA(leftop, Const))
3073         {
3074                 Assert(!((Const *) leftop)->constisnull);
3075                 if (DatumGetBool(((Const *) leftop)->constvalue))
3076                         return rightop;         /* true = foo */
3077                 else
3078                         return make_notclause(rightop);         /* false = foo */
3079         }
3080         if (rightop && IsA(rightop, Const))
3081         {
3082                 Assert(!((Const *) rightop)->constisnull);
3083                 if (DatumGetBool(((Const *) rightop)->constvalue))
3084                         return leftop;          /* foo = true */
3085                 else
3086                         return make_notclause(leftop);          /* foo = false */
3087         }
3088         return NULL;
3089 }
3090
3091 /*
3092  * Subroutine for eval_const_expressions: try to simplify a function call
3093  * (which might originally have been an operator; we don't care)
3094  *
3095  * Inputs are the function OID, actual result type OID (which is needed for
3096  * polymorphic functions) and typmod, and the pre-simplified argument list;
3097  * also the context data for eval_const_expressions.
3098  *
3099  * Returns a simplified expression if successful, or NULL if cannot
3100  * simplify the function call.
3101  */
3102 static Expr *
3103 simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
3104                                   List *args,
3105                                   bool allow_inline,
3106                                   eval_const_expressions_context *context)
3107 {
3108         HeapTuple       func_tuple;
3109         Expr       *newexpr;
3110
3111         /*
3112          * We have two strategies for simplification: either execute the function
3113          * to deliver a constant result, or expand in-line the body of the
3114          * function definition (which only works for simple SQL-language
3115          * functions, but that is a common case).  In either case we need access
3116          * to the function's pg_proc tuple, so fetch it just once to use in both
3117          * attempts.
3118          */
3119         func_tuple = SearchSysCache(PROCOID,
3120                                                                 ObjectIdGetDatum(funcid),
3121                                                                 0, 0, 0);
3122         if (!HeapTupleIsValid(func_tuple))
3123                 elog(ERROR, "cache lookup failed for function %u", funcid);
3124
3125         newexpr = evaluate_function(funcid, result_type, result_typmod, args,
3126                                                                 func_tuple, context);
3127
3128         if (!newexpr && allow_inline)
3129                 newexpr = inline_function(funcid, result_type, args,
3130                                                                   func_tuple, context);
3131
3132         ReleaseSysCache(func_tuple);
3133
3134         return newexpr;
3135 }
3136
3137 /*
3138  * evaluate_function: try to pre-evaluate a function call
3139  *
3140  * We can do this if the function is strict and has any constant-null inputs
3141  * (just return a null constant), or if the function is immutable and has all
3142  * constant inputs (call it and return the result as a Const node).  In
3143  * estimation mode we are willing to pre-evaluate stable functions too.
3144  *
3145  * Returns a simplified expression if successful, or NULL if cannot
3146  * simplify the function.
3147  */
3148 static Expr *
3149 evaluate_function(Oid funcid, Oid result_type, int32 result_typmod, List *args,
3150                                   HeapTuple func_tuple,
3151                                   eval_const_expressions_context *context)
3152 {
3153         Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3154         bool            has_nonconst_input = false;
3155         bool            has_null_input = false;
3156         ListCell   *arg;
3157         FuncExpr   *newexpr;
3158
3159         /*
3160          * Can't simplify if it returns a set.
3161          */
3162         if (funcform->proretset)
3163                 return NULL;
3164
3165         /*
3166          * Can't simplify if it returns RECORD.  The immediate problem is that it
3167          * will be needing an expected tupdesc which we can't supply here.
3168          *
3169          * In the case where it has OUT parameters, it could get by without an
3170          * expected tupdesc, but we still have issues: get_expr_result_type()
3171          * doesn't know how to extract type info from a RECORD constant, and in
3172          * the case of a NULL function result there doesn't seem to be any clean
3173          * way to fix that.  In view of the likelihood of there being still other
3174          * gotchas, seems best to leave the function call unreduced.
3175          */
3176         if (funcform->prorettype == RECORDOID)
3177                 return NULL;
3178
3179         /*
3180          * Check for constant inputs and especially constant-NULL inputs.
3181          */
3182         foreach(arg, args)
3183         {
3184                 if (IsA(lfirst(arg), Const))
3185                         has_null_input |= ((Const *) lfirst(arg))->constisnull;
3186                 else
3187                         has_nonconst_input = true;
3188         }
3189
3190         /*
3191          * If the function is strict and has a constant-NULL input, it will never
3192          * be called at all, so we can replace the call by a NULL constant, even
3193          * if there are other inputs that aren't constant, and even if the
3194          * function is not otherwise immutable.
3195          */
3196         if (funcform->proisstrict && has_null_input)
3197                 return (Expr *) makeNullConst(result_type, result_typmod);
3198
3199         /*
3200          * Otherwise, can simplify only if all inputs are constants. (For a
3201          * non-strict function, constant NULL inputs are treated the same as
3202          * constant non-NULL inputs.)
3203          */
3204         if (has_nonconst_input)
3205                 return NULL;
3206
3207         /*
3208          * Ordinarily we are only allowed to simplify immutable functions. But for
3209          * purposes of estimation, we consider it okay to simplify functions that
3210          * are merely stable; the risk that the result might change from planning
3211          * time to execution time is worth taking in preference to not being able
3212          * to estimate the value at all.
3213          */
3214         if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
3215                  /* okay */ ;
3216         else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
3217                  /* okay */ ;
3218         else
3219                 return NULL;
3220
3221         /*
3222          * OK, looks like we can simplify this operator/function.
3223          *
3224          * Build a new FuncExpr node containing the already-simplified arguments.
3225          */
3226         newexpr = makeNode(FuncExpr);
3227         newexpr->funcid = funcid;
3228         newexpr->funcresulttype = result_type;
3229         newexpr->funcretset = false;
3230         newexpr->funcformat = COERCE_DONTCARE;          /* doesn't matter */
3231         newexpr->args = args;
3232         newexpr->location = -1;
3233
3234         return evaluate_expr((Expr *) newexpr, result_type, result_typmod);
3235 }
3236
3237 /*
3238  * inline_function: try to expand a function call inline
3239  *
3240  * If the function is a sufficiently simple SQL-language function
3241  * (just "SELECT expression"), then we can inline it and avoid the rather
3242  * high per-call overhead of SQL functions.  Furthermore, this can expose
3243  * opportunities for constant-folding within the function expression.
3244  *
3245  * We have to beware of some special cases however.  A directly or
3246  * indirectly recursive function would cause us to recurse forever,
3247  * so we keep track of which functions we are already expanding and
3248  * do not re-expand them.  Also, if a parameter is used more than once
3249  * in the SQL-function body, we require it not to contain any volatile
3250  * functions (volatiles might deliver inconsistent answers) nor to be
3251  * unreasonably expensive to evaluate.  The expensiveness check not only
3252  * prevents us from doing multiple evaluations of an expensive parameter
3253  * at runtime, but is a safety value to limit growth of an expression due
3254  * to repeated inlining.
3255  *
3256  * We must also beware of changing the volatility or strictness status of
3257  * functions by inlining them.
3258  *
3259  * Returns a simplified expression if successful, or NULL if cannot
3260  * simplify the function.
3261  */
3262 static Expr *
3263 inline_function(Oid funcid, Oid result_type, List *args,
3264                                 HeapTuple func_tuple,
3265                                 eval_const_expressions_context *context)
3266 {
3267         Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3268         Oid                *argtypes;
3269         char       *src;
3270         Datum           tmp;
3271         bool            isNull;
3272         MemoryContext oldcxt;
3273         MemoryContext mycxt;
3274         ErrorContextCallback sqlerrcontext;
3275         List       *raw_parsetree_list;
3276         Query      *querytree;
3277         Node       *newexpr;
3278         int                *usecounts;
3279         ListCell   *arg;
3280         int                     i;
3281
3282         /*
3283          * Forget it if the function is not SQL-language or has other showstopper
3284          * properties.  (The nargs check is just paranoia.)
3285          */
3286         if (funcform->prolang != SQLlanguageId ||
3287                 funcform->prosecdef ||
3288                 funcform->proretset ||
3289                 !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
3290                 funcform->pronargs != list_length(args))
3291                 return NULL;
3292
3293         /* Check for recursive function, and give up trying to expand if so */
3294         if (list_member_oid(context->active_fns, funcid))
3295                 return NULL;
3296
3297         /* Check permission to call function (fail later, if not) */
3298         if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
3299                 return NULL;
3300
3301         /*
3302          * Setup error traceback support for ereport().  This is so that we can
3303          * finger the function that bad information came from.
3304          */
3305         sqlerrcontext.callback = sql_inline_error_callback;
3306         sqlerrcontext.arg = func_tuple;
3307         sqlerrcontext.previous = error_context_stack;
3308         error_context_stack = &sqlerrcontext;
3309
3310         /*
3311          * Make a temporary memory context, so that we don't leak all the stuff
3312          * that parsing might create.
3313          */
3314         mycxt = AllocSetContextCreate(CurrentMemoryContext,
3315                                                                   "inline_function",
3316                                                                   ALLOCSET_DEFAULT_MINSIZE,
3317                                                                   ALLOCSET_DEFAULT_INITSIZE,
3318                                                                   ALLOCSET_DEFAULT_MAXSIZE);
3319         oldcxt = MemoryContextSwitchTo(mycxt);
3320
3321         /* Check for polymorphic arguments, and substitute actual arg types */
3322         argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
3323         memcpy(argtypes, funcform->proargtypes.values,
3324                    funcform->pronargs * sizeof(Oid));
3325         for (i = 0; i < funcform->pronargs; i++)
3326         {
3327                 if (IsPolymorphicType(argtypes[i]))
3328                 {
3329                         argtypes[i] = exprType((Node *) list_nth(args, i));
3330                 }
3331         }
3332
3333         /* Fetch and parse the function body */
3334         tmp = SysCacheGetAttr(PROCOID,
3335                                                   func_tuple,
3336                                                   Anum_pg_proc_prosrc,
3337                                                   &isNull);
3338         if (isNull)
3339                 elog(ERROR, "null prosrc for function %u", funcid);
3340         src = TextDatumGetCString(tmp);
3341
3342         /*
3343          * We just do parsing and parse analysis, not rewriting, because rewriting
3344          * will not affect table-free-SELECT-only queries, which is all that we
3345          * care about.  Also, we can punt as soon as we detect more than one
3346          * command in the function body.
3347          */
3348         raw_parsetree_list = pg_parse_query(src);
3349         if (list_length(raw_parsetree_list) != 1)
3350                 goto fail;
3351
3352         querytree = parse_analyze(linitial(raw_parsetree_list), src,
3353                                                           argtypes, funcform->pronargs);
3354
3355         /*
3356          * The single command must be a simple "SELECT expression".
3357          */
3358         if (!IsA(querytree, Query) ||
3359                 querytree->commandType != CMD_SELECT ||
3360                 querytree->utilityStmt ||
3361                 querytree->intoClause ||
3362                 querytree->hasAggs ||
3363                 querytree->hasSubLinks ||
3364                 querytree->cteList ||
3365                 querytree->rtable ||
3366                 querytree->jointree->fromlist ||
3367                 querytree->jointree->quals ||
3368                 querytree->groupClause ||
3369                 querytree->havingQual ||
3370                 querytree->distinctClause ||
3371                 querytree->sortClause ||
3372                 querytree->limitOffset ||
3373                 querytree->limitCount ||
3374                 querytree->setOperations ||
3375                 list_length(querytree->targetList) != 1)
3376                 goto fail;
3377
3378         /*
3379          * Make sure the function (still) returns what it's declared to.  This
3380          * will raise an error if wrong, but that's okay since the function would
3381          * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
3382          * a RelabelType if needed to make the tlist expression match the declared
3383          * type of the function.
3384          *
3385          * Note: we do not try this until we have verified that no rewriting was
3386          * needed; that's probably not important, but let's be careful.
3387          */
3388         if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
3389                                                         true, NULL))
3390                 goto fail;                              /* reject whole-tuple-result cases */
3391
3392         /* Now we can grab the tlist expression */
3393         newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
3394
3395         /* Assert that check_sql_fn_retval did the right thing */
3396         Assert(exprType(newexpr) == result_type);
3397
3398         /*
3399          * Additional validity checks on the expression.  It mustn't return a set,
3400          * and it mustn't be more volatile than the surrounding function (this is
3401          * to avoid breaking hacks that involve pretending a function is immutable
3402          * when it really ain't).  If the surrounding function is declared strict,
3403          * then the expression must contain only strict constructs and must use
3404          * all of the function parameters (this is overkill, but an exact analysis
3405          * is hard).
3406          */
3407         if (expression_returns_set(newexpr))
3408                 goto fail;
3409
3410         if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
3411                 contain_mutable_functions(newexpr))
3412                 goto fail;
3413         else if (funcform->provolatile == PROVOLATILE_STABLE &&
3414                          contain_volatile_functions(newexpr))
3415                 goto fail;
3416
3417         if (funcform->proisstrict &&
3418                 contain_nonstrict_functions(newexpr))
3419                 goto fail;
3420
3421         /*
3422          * We may be able to do it; there are still checks on parameter usage to
3423          * make, but those are most easily done in combination with the actual
3424          * substitution of the inputs.  So start building expression with inputs
3425          * substituted.
3426          */
3427         usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
3428         newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
3429                                                                                    args, usecounts);
3430
3431         /* Now check for parameter usage */
3432         i = 0;
3433         foreach(arg, args)
3434         {
3435                 Node       *param = lfirst(arg);
3436
3437                 if (usecounts[i] == 0)
3438                 {
3439                         /* Param not used at all: uncool if func is strict */
3440                         if (funcform->proisstrict)
3441                                 goto fail;
3442                 }
3443                 else if (usecounts[i] != 1)
3444                 {
3445                         /* Param used multiple times: uncool if expensive or volatile */
3446                         QualCost        eval_cost;
3447
3448                         /*
3449                          * We define "expensive" as "contains any subplan or more than 10
3450                          * operators".  Note that the subplan search has to be done
3451                          * explicitly, since cost_qual_eval() will barf on unplanned
3452                          * subselects.
3453                          */
3454                         if (contain_subplans(param))
3455                                 goto fail;
3456                         cost_qual_eval(&eval_cost, list_make1(param), NULL);
3457                         if (eval_cost.startup + eval_cost.per_tuple >
3458                                 10 * cpu_operator_cost)
3459                                 goto fail;
3460
3461                         /*
3462                          * Check volatility last since this is more expensive than the
3463                          * above tests
3464                          */
3465                         if (contain_volatile_functions(param))
3466                                 goto fail;
3467                 }
3468                 i++;
3469         }
3470
3471         /*
3472          * Whew --- we can make the substitution.  Copy the modified expression
3473          * out of the temporary memory context, and clean up.
3474          */
3475         MemoryContextSwitchTo(oldcxt);
3476
3477         newexpr = copyObject(newexpr);
3478
3479         MemoryContextDelete(mycxt);
3480
3481         /*
3482          * Since there is now no trace of the function in the plan tree, we
3483          * must explicitly record the plan's dependency on the function.
3484          */
3485         if (context->glob)
3486                 record_plan_function_dependency(context->glob, funcid);
3487
3488         /*
3489          * Recursively try to simplify the modified expression.  Here we must add
3490          * the current function to the context list of active functions.
3491          */
3492         context->active_fns = lcons_oid(funcid, context->active_fns);
3493         newexpr = eval_const_expressions_mutator(newexpr, context);
3494         context->active_fns = list_delete_first(context->active_fns);
3495
3496         error_context_stack = sqlerrcontext.previous;
3497
3498         return (Expr *) newexpr;
3499
3500         /* Here if func is not inlinable: release temp memory and return NULL */
3501 fail:
3502         MemoryContextSwitchTo(oldcxt);
3503         MemoryContextDelete(mycxt);
3504         error_context_stack = sqlerrcontext.previous;
3505
3506         return NULL;
3507 }
3508
3509 /*
3510  * Replace Param nodes by appropriate actual parameters
3511  */
3512 static Node *
3513 substitute_actual_parameters(Node *expr, int nargs, List *args,
3514                                                          int *usecounts)
3515 {
3516         substitute_actual_parameters_context context;
3517
3518         context.nargs = nargs;
3519         context.args = args;
3520         context.usecounts = usecounts;
3521
3522         return substitute_actual_parameters_mutator(expr, &context);
3523 }
3524
3525 static Node *
3526 substitute_actual_parameters_mutator(Node *node,
3527                                                            substitute_actual_parameters_context *context)
3528 {
3529         if (node == NULL)
3530                 return NULL;
3531         if (IsA(node, Param))
3532         {
3533                 Param      *param = (Param *) node;
3534
3535                 if (param->paramkind != PARAM_EXTERN)
3536                         elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
3537                 if (param->paramid <= 0 || param->paramid > context->nargs)
3538                         elog(ERROR, "invalid paramid: %d", param->paramid);
3539
3540                 /* Count usage of parameter */
3541                 context->usecounts[param->paramid - 1]++;
3542
3543                 /* Select the appropriate actual arg and replace the Param with it */
3544                 /* We don't need to copy at this time (it'll get done later) */
3545                 return list_nth(context->args, param->paramid - 1);
3546         }
3547         return expression_tree_mutator(node, substitute_actual_parameters_mutator,
3548                                                                    (void *) context);
3549 }
3550
3551 /*
3552  * error context callback to let us supply a call-stack traceback
3553  */
3554 static void
3555 sql_inline_error_callback(void *arg)
3556 {
3557         HeapTuple       func_tuple = (HeapTuple) arg;
3558         Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3559         int                     syntaxerrposition;
3560
3561         /* If it's a syntax error, convert to internal syntax error report */
3562         syntaxerrposition = geterrposition();
3563         if (syntaxerrposition > 0)
3564         {
3565                 bool            isnull;
3566                 Datum           tmp;
3567                 char       *prosrc;
3568
3569                 tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
3570                                                           &isnull);
3571                 if (isnull)
3572                         elog(ERROR, "null prosrc");
3573                 prosrc = TextDatumGetCString(tmp);
3574                 errposition(0);
3575                 internalerrposition(syntaxerrposition);
3576                 internalerrquery(prosrc);
3577         }
3578
3579         errcontext("SQL function \"%s\" during inlining",
3580                            NameStr(funcform->proname));
3581 }
3582
3583 /*
3584  * evaluate_expr: pre-evaluate a constant expression
3585  *
3586  * We use the executor's routine ExecEvalExpr() to avoid duplication of
3587  * code and ensure we get the same result as the executor would get.
3588  */
3589 static Expr *
3590 evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod)
3591 {
3592         EState     *estate;
3593         ExprState  *exprstate;
3594         MemoryContext oldcontext;
3595         Datum           const_val;
3596         bool            const_is_null;
3597         int16           resultTypLen;
3598         bool            resultTypByVal;
3599
3600         /*
3601          * To use the executor, we need an EState.
3602          */
3603         estate = CreateExecutorState();
3604
3605         /* We can use the estate's working context to avoid memory leaks. */
3606         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3607
3608         /*
3609          * Prepare expr for execution.
3610          */
3611         exprstate = ExecPrepareExpr(expr, estate);
3612
3613         /*
3614          * And evaluate it.
3615          *
3616          * It is OK to use a default econtext because none of the ExecEvalExpr()
3617          * code used in this situation will use econtext.  That might seem
3618          * fortuitous, but it's not so unreasonable --- a constant expression does
3619          * not depend on context, by definition, n'est ce pas?
3620          */
3621         const_val = ExecEvalExprSwitchContext(exprstate,
3622                                                                                   GetPerTupleExprContext(estate),
3623                                                                                   &const_is_null, NULL);
3624
3625         /* Get info needed about result datatype */
3626         get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
3627
3628         /* Get back to outer memory context */
3629         MemoryContextSwitchTo(oldcontext);
3630
3631         /*
3632          * Must copy result out of sub-context used by expression eval.
3633          *
3634          * Also, if it's varlena, forcibly detoast it.  This protects us against
3635          * storing TOAST pointers into plans that might outlive the referenced
3636          * data.
3637          */
3638         if (!const_is_null)
3639         {
3640                 if (resultTypLen == -1)
3641                         const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
3642                 else
3643                         const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
3644         }
3645
3646         /* Release all the junk we just created */
3647         FreeExecutorState(estate);
3648
3649         /*
3650          * Make the constant result node.
3651          */
3652         return (Expr *) makeConst(result_type, result_typmod, resultTypLen,
3653                                                           const_val, const_is_null,
3654                                                           resultTypByVal);
3655 }
3656
3657
3658 /*
3659  * inline_set_returning_function
3660  *              Attempt to "inline" a set-returning function in the FROM clause.
3661  *
3662  * "node" is the expression from an RTE_FUNCTION rangetable entry.  If it
3663  * represents a call of a set-returning SQL function that can safely be
3664  * inlined, expand the function and return the substitute Query structure.
3665  * Otherwise, return NULL.
3666  *
3667  * This has a good deal of similarity to inline_function(), but that's
3668  * for the non-set-returning case, and there are enough differences to
3669  * justify separate functions.
3670  */
3671 Query *
3672 inline_set_returning_function(PlannerInfo *root, Node *node)
3673 {
3674         FuncExpr   *fexpr;
3675         HeapTuple       func_tuple;
3676         Form_pg_proc funcform;
3677         Oid                *argtypes;
3678         char       *src;
3679         Datum           tmp;
3680         bool            isNull;
3681         MemoryContext oldcxt;
3682         MemoryContext mycxt;
3683         ErrorContextCallback sqlerrcontext;
3684         List       *raw_parsetree_list;
3685         List       *querytree_list;
3686         Query      *querytree;
3687         int                     i;
3688
3689         /*
3690          * It doesn't make a lot of sense for a SQL SRF to refer to itself
3691          * in its own FROM clause, since that must cause infinite recursion
3692          * at runtime.  It will cause this code to recurse too, so check
3693          * for stack overflow.  (There's no need to do more.)
3694          */
3695         check_stack_depth();
3696
3697         /* Fail if FROM item isn't a simple FuncExpr */
3698         if (node == NULL || !IsA(node, FuncExpr))
3699                 return NULL;
3700         fexpr = (FuncExpr *) node;
3701
3702         /*
3703          * The function must be declared to return a set, else inlining would
3704          * change the results if the contained SELECT didn't return exactly
3705          * one row.
3706          */
3707         if (!fexpr->funcretset)
3708                 return NULL;
3709
3710         /* Fail if function returns RECORD ... we don't have enough context */
3711         if (fexpr->funcresulttype == RECORDOID)
3712                 return NULL;
3713
3714         /*
3715          * Refuse to inline if the arguments contain any volatile functions or
3716          * sub-selects.  Volatile functions are rejected because inlining may
3717          * result in the arguments being evaluated multiple times, risking a
3718          * change in behavior.  Sub-selects are rejected partly for implementation
3719          * reasons (pushing them down another level might change their behavior)
3720          * and partly because they're likely to be expensive and so multiple
3721          * evaluation would be bad.
3722          */
3723         if (contain_volatile_functions((Node *) fexpr->args) ||
3724                 contain_subplans((Node *) fexpr->args))
3725                 return NULL;
3726
3727         /* Check permission to call function (fail later, if not) */
3728         if (pg_proc_aclcheck(fexpr->funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
3729                 return NULL;
3730
3731         /*
3732          * OK, let's take a look at the function's pg_proc entry.
3733          */
3734         func_tuple = SearchSysCache(PROCOID,
3735                                                                 ObjectIdGetDatum(fexpr->funcid),
3736                                                                 0, 0, 0);
3737         if (!HeapTupleIsValid(func_tuple))
3738                 elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3739         funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3740
3741         /*
3742          * Forget it if the function is not SQL-language or has other showstopper
3743          * properties.  In particular it mustn't be declared STRICT, since we
3744          * couldn't enforce that.  It also mustn't be VOLATILE, because that is
3745          * supposed to cause it to be executed with its own snapshot, rather than
3746          * sharing the snapshot of the calling query.  (The nargs check is just
3747          * paranoia, ditto rechecking proretset.)
3748          */
3749         if (funcform->prolang != SQLlanguageId ||
3750                 funcform->proisstrict ||
3751                 funcform->provolatile == PROVOLATILE_VOLATILE ||
3752                 funcform->prosecdef ||
3753                 !funcform->proretset ||
3754                 !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
3755                 funcform->pronargs != list_length(fexpr->args))
3756         {
3757                 ReleaseSysCache(func_tuple);
3758                 return NULL;
3759         }
3760
3761         /*
3762          * Setup error traceback support for ereport().  This is so that we can
3763          * finger the function that bad information came from.
3764          */
3765         sqlerrcontext.callback = sql_inline_error_callback;
3766         sqlerrcontext.arg = func_tuple;
3767         sqlerrcontext.previous = error_context_stack;
3768         error_context_stack = &sqlerrcontext;
3769
3770         /*
3771          * Make a temporary memory context, so that we don't leak all the stuff
3772          * that parsing might create.
3773          */
3774         mycxt = AllocSetContextCreate(CurrentMemoryContext,
3775                                                                   "inline_set_returning_function",
3776                                                                   ALLOCSET_DEFAULT_MINSIZE,
3777                                                                   ALLOCSET_DEFAULT_INITSIZE,
3778                                                                   ALLOCSET_DEFAULT_MAXSIZE);
3779         oldcxt = MemoryContextSwitchTo(mycxt);
3780
3781         /* Check for polymorphic arguments, and substitute actual arg types */
3782         argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
3783         memcpy(argtypes, funcform->proargtypes.values,
3784                    funcform->pronargs * sizeof(Oid));
3785         for (i = 0; i < funcform->pronargs; i++)
3786         {
3787                 if (IsPolymorphicType(argtypes[i]))
3788                 {
3789                         argtypes[i] = exprType((Node *) list_nth(fexpr->args, i));
3790                 }
3791         }
3792
3793         /* Fetch and parse the function body */
3794         tmp = SysCacheGetAttr(PROCOID,
3795                                                   func_tuple,
3796                                                   Anum_pg_proc_prosrc,
3797                                                   &isNull);
3798         if (isNull)
3799                 elog(ERROR, "null prosrc for function %u", fexpr->funcid);
3800         src = TextDatumGetCString(tmp);
3801
3802         /*
3803          * Parse, analyze, and rewrite (unlike inline_function(), we can't
3804          * skip rewriting here).  We can fail as soon as we find more than
3805          * one query, though.
3806          */
3807         raw_parsetree_list = pg_parse_query(src);
3808         if (list_length(raw_parsetree_list) != 1)
3809                 goto fail;
3810
3811         querytree_list = pg_analyze_and_rewrite(linitial(raw_parsetree_list), src,
3812                                                           argtypes, funcform->pronargs);
3813         if (list_length(querytree_list) != 1)
3814                 goto fail;
3815         querytree = linitial(querytree_list);
3816
3817         /*
3818          * The single command must be a regular results-returning SELECT.
3819          */
3820         if (!IsA(querytree, Query) ||
3821                 querytree->commandType != CMD_SELECT ||
3822                 querytree->utilityStmt ||
3823                 querytree->intoClause)
3824                 goto fail;
3825
3826         /*
3827          * Make sure the function (still) returns what it's declared to.  This
3828          * will raise an error if wrong, but that's okay since the function would
3829          * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
3830          * RelabelType(s) if needed to make the tlist expression(s) match the
3831          * declared type of the function.
3832          *
3833          * If the function returns a composite type, don't inline unless the
3834          * check shows it's returning a whole tuple result; otherwise what
3835          * it's returning is a single composite column which is not what we need.
3836          */
3837         if (!check_sql_fn_retval(fexpr->funcid, fexpr->funcresulttype,
3838                                                          querytree_list,
3839                                                          true, NULL) &&
3840                 get_typtype(fexpr->funcresulttype) == TYPTYPE_COMPOSITE)
3841                 goto fail;                              /* reject not-whole-tuple-result cases */
3842
3843         /*
3844          * Looks good --- substitute parameters into the query.
3845          */
3846         querytree = substitute_actual_srf_parameters(querytree,
3847                                                                                                  funcform->pronargs,
3848                                                                                                  fexpr->args);
3849
3850         /*
3851          * Copy the modified query out of the temporary memory context,
3852          * and clean up.
3853          */
3854         MemoryContextSwitchTo(oldcxt);
3855
3856         querytree = copyObject(querytree);
3857
3858         MemoryContextDelete(mycxt);
3859         error_context_stack = sqlerrcontext.previous;
3860         ReleaseSysCache(func_tuple);
3861
3862         /*
3863          * Since there is now no trace of the function in the plan tree, we
3864          * must explicitly record the plan's dependency on the function.
3865          */
3866         record_plan_function_dependency(root->glob, fexpr->funcid);
3867
3868         return querytree;
3869
3870         /* Here if func is not inlinable: release temp memory and return NULL */
3871 fail:
3872         MemoryContextSwitchTo(oldcxt);
3873         MemoryContextDelete(mycxt);
3874         error_context_stack = sqlerrcontext.previous;
3875         ReleaseSysCache(func_tuple);
3876
3877         return NULL;
3878 }
3879
3880 /*
3881  * Replace Param nodes by appropriate actual parameters
3882  *
3883  * This is just enough different from substitute_actual_parameters()
3884  * that it needs its own code.
3885  */
3886 static Query *
3887 substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
3888 {
3889         substitute_actual_srf_parameters_context context;
3890
3891         context.nargs = nargs;
3892         context.args = args;
3893         context.sublevels_up = 1;
3894
3895         return query_tree_mutator(expr,
3896                                                           substitute_actual_srf_parameters_mutator,
3897                                                           &context,
3898                                                           0);
3899 }
3900
3901 static Node *
3902 substitute_actual_srf_parameters_mutator(Node *node,
3903                                                         substitute_actual_srf_parameters_context *context)
3904 {
3905         Node   *result;
3906
3907         if (node == NULL)
3908                 return NULL;
3909         if (IsA(node, Query))
3910         {
3911                 context->sublevels_up++;
3912                 result = (Node *) query_tree_mutator((Query *) node,
3913                                                                           substitute_actual_srf_parameters_mutator,
3914                                                                                          (void *) context,
3915                                                                                          0);
3916                 context->sublevels_up--;
3917                 return result;
3918         }
3919         if (IsA(node, Param))
3920         {
3921                 Param      *param = (Param *) node;
3922
3923                 if (param->paramkind == PARAM_EXTERN)
3924                 {
3925                         if (param->paramid <= 0 || param->paramid > context->nargs)
3926                                 elog(ERROR, "invalid paramid: %d", param->paramid);
3927
3928                         /*
3929                          * Since the parameter is being inserted into a subquery,
3930                          * we must adjust levels.
3931                          */
3932                         result = copyObject(list_nth(context->args, param->paramid - 1));
3933                         IncrementVarSublevelsUp(result, context->sublevels_up, 0);
3934                         return result;
3935                 }
3936         }
3937         return expression_tree_mutator(node,
3938                                                                    substitute_actual_srf_parameters_mutator,
3939                                                                    (void *) context);
3940 }