Header And Logo

PostgreSQL
| The world's most advanced open source database.

clauses.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * clauses.c
00004  *    routines to manipulate qualification clauses
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/optimizer/util/clauses.c
00012  *
00013  * HISTORY
00014  *    AUTHOR            DATE            MAJOR EVENT
00015  *    Andrew Yu         Nov 3, 1994     clause.c and clauses.c combined
00016  *
00017  *-------------------------------------------------------------------------
00018  */
00019 
00020 #include "postgres.h"
00021 
00022 #include "access/htup_details.h"
00023 #include "catalog/pg_aggregate.h"
00024 #include "catalog/pg_language.h"
00025 #include "catalog/pg_operator.h"
00026 #include "catalog/pg_proc.h"
00027 #include "catalog/pg_type.h"
00028 #include "executor/executor.h"
00029 #include "executor/functions.h"
00030 #include "funcapi.h"
00031 #include "miscadmin.h"
00032 #include "nodes/makefuncs.h"
00033 #include "nodes/nodeFuncs.h"
00034 #include "optimizer/clauses.h"
00035 #include "optimizer/cost.h"
00036 #include "optimizer/planmain.h"
00037 #include "optimizer/prep.h"
00038 #include "optimizer/var.h"
00039 #include "parser/analyze.h"
00040 #include "parser/parse_coerce.h"
00041 #include "parser/parse_func.h"
00042 #include "rewrite/rewriteManip.h"
00043 #include "tcop/tcopprot.h"
00044 #include "utils/acl.h"
00045 #include "utils/builtins.h"
00046 #include "utils/datum.h"
00047 #include "utils/lsyscache.h"
00048 #include "utils/memutils.h"
00049 #include "utils/syscache.h"
00050 #include "utils/typcache.h"
00051 
00052 
00053 typedef struct
00054 {
00055     PlannerInfo *root;
00056     AggClauseCosts *costs;
00057 } count_agg_clauses_context;
00058 
00059 typedef struct
00060 {
00061     ParamListInfo boundParams;
00062     PlannerInfo *root;
00063     List       *active_fns;
00064     Node       *case_val;
00065     bool        estimate;
00066 } eval_const_expressions_context;
00067 
00068 typedef struct
00069 {
00070     int         nargs;
00071     List       *args;
00072     int        *usecounts;
00073 } substitute_actual_parameters_context;
00074 
00075 typedef struct
00076 {
00077     int         nargs;
00078     List       *args;
00079     int         sublevels_up;
00080 } substitute_actual_srf_parameters_context;
00081 
00082 typedef struct
00083 {
00084     char       *proname;
00085     char       *prosrc;
00086 } inline_error_callback_arg;
00087 
00088 static bool contain_agg_clause_walker(Node *node, void *context);
00089 static bool count_agg_clauses_walker(Node *node,
00090                          count_agg_clauses_context *context);
00091 static bool find_window_functions_walker(Node *node, WindowFuncLists *lists);
00092 static bool expression_returns_set_rows_walker(Node *node, double *count);
00093 static bool contain_subplans_walker(Node *node, void *context);
00094 static bool contain_mutable_functions_walker(Node *node, void *context);
00095 static bool contain_volatile_functions_walker(Node *node, void *context);
00096 static bool contain_nonstrict_functions_walker(Node *node, void *context);
00097 static bool contain_leaky_functions_walker(Node *node, void *context);
00098 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
00099 static List *find_nonnullable_vars_walker(Node *node, bool top_level);
00100 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
00101 static Node *eval_const_expressions_mutator(Node *node,
00102                                eval_const_expressions_context *context);
00103 static List *simplify_or_arguments(List *args,
00104                       eval_const_expressions_context *context,
00105                       bool *haveNull, bool *forceTrue);
00106 static List *simplify_and_arguments(List *args,
00107                        eval_const_expressions_context *context,
00108                        bool *haveNull, bool *forceFalse);
00109 static Node *simplify_boolean_equality(Oid opno, List *args);
00110 static Expr *simplify_function(Oid funcid,
00111                   Oid result_type, int32 result_typmod,
00112                   Oid result_collid, Oid input_collid, List **args_p,
00113                   bool funcvariadic, bool process_args, bool allow_non_const,
00114                   eval_const_expressions_context *context);
00115 static List *expand_function_arguments(List *args, Oid result_type,
00116                           HeapTuple func_tuple);
00117 static List *reorder_function_arguments(List *args, HeapTuple func_tuple);
00118 static List *add_function_defaults(List *args, HeapTuple func_tuple);
00119 static List *fetch_function_defaults(HeapTuple func_tuple);
00120 static void recheck_cast_function_args(List *args, Oid result_type,
00121                            HeapTuple func_tuple);
00122 static Expr *evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
00123                   Oid result_collid, Oid input_collid, List *args,
00124                   bool funcvariadic,
00125                   HeapTuple func_tuple,
00126                   eval_const_expressions_context *context);
00127 static Expr *inline_function(Oid funcid, Oid result_type, Oid result_collid,
00128                 Oid input_collid, List *args,
00129                 bool funcvariadic,
00130                 HeapTuple func_tuple,
00131                 eval_const_expressions_context *context);
00132 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
00133                              int *usecounts);
00134 static Node *substitute_actual_parameters_mutator(Node *node,
00135                               substitute_actual_parameters_context *context);
00136 static void sql_inline_error_callback(void *arg);
00137 static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
00138               Oid result_collation);
00139 static Query *substitute_actual_srf_parameters(Query *expr,
00140                                  int nargs, List *args);
00141 static Node *substitute_actual_srf_parameters_mutator(Node *node,
00142                           substitute_actual_srf_parameters_context *context);
00143 static bool tlist_matches_coltypelist(List *tlist, List *coltypelist);
00144 
00145 
00146 /*****************************************************************************
00147  *      OPERATOR clause functions
00148  *****************************************************************************/
00149 
00150 /*
00151  * make_opclause
00152  *    Creates an operator clause given its operator info, left operand
00153  *    and right operand (pass NULL to create single-operand clause),
00154  *    and collation info.
00155  */
00156 Expr *
00157 make_opclause(Oid opno, Oid opresulttype, bool opretset,
00158               Expr *leftop, Expr *rightop,
00159               Oid opcollid, Oid inputcollid)
00160 {
00161     OpExpr     *expr = makeNode(OpExpr);
00162 
00163     expr->opno = opno;
00164     expr->opfuncid = InvalidOid;
00165     expr->opresulttype = opresulttype;
00166     expr->opretset = opretset;
00167     expr->opcollid = opcollid;
00168     expr->inputcollid = inputcollid;
00169     if (rightop)
00170         expr->args = list_make2(leftop, rightop);
00171     else
00172         expr->args = list_make1(leftop);
00173     expr->location = -1;
00174     return (Expr *) expr;
00175 }
00176 
00177 /*
00178  * get_leftop
00179  *
00180  * Returns the left operand of a clause of the form (op expr expr)
00181  *      or (op expr)
00182  */
00183 Node *
00184 get_leftop(const Expr *clause)
00185 {
00186     const OpExpr *expr = (const OpExpr *) clause;
00187 
00188     if (expr->args != NIL)
00189         return linitial(expr->args);
00190     else
00191         return NULL;
00192 }
00193 
00194 /*
00195  * get_rightop
00196  *
00197  * Returns the right operand in a clause of the form (op expr expr).
00198  * NB: result will be NULL if applied to a unary op clause.
00199  */
00200 Node *
00201 get_rightop(const Expr *clause)
00202 {
00203     const OpExpr *expr = (const OpExpr *) clause;
00204 
00205     if (list_length(expr->args) >= 2)
00206         return lsecond(expr->args);
00207     else
00208         return NULL;
00209 }
00210 
00211 /*****************************************************************************
00212  *      NOT clause functions
00213  *****************************************************************************/
00214 
00215 /*
00216  * not_clause
00217  *
00218  * Returns t iff this is a 'not' clause: (NOT expr).
00219  */
00220 bool
00221 not_clause(Node *clause)
00222 {
00223     return (clause != NULL &&
00224             IsA(clause, BoolExpr) &&
00225             ((BoolExpr *) clause)->boolop == NOT_EXPR);
00226 }
00227 
00228 /*
00229  * make_notclause
00230  *
00231  * Create a 'not' clause given the expression to be negated.
00232  */
00233 Expr *
00234 make_notclause(Expr *notclause)
00235 {
00236     BoolExpr   *expr = makeNode(BoolExpr);
00237 
00238     expr->boolop = NOT_EXPR;
00239     expr->args = list_make1(notclause);
00240     expr->location = -1;
00241     return (Expr *) expr;
00242 }
00243 
00244 /*
00245  * get_notclausearg
00246  *
00247  * Retrieve the clause within a 'not' clause
00248  */
00249 Expr *
00250 get_notclausearg(Expr *notclause)
00251 {
00252     return linitial(((BoolExpr *) notclause)->args);
00253 }
00254 
00255 /*****************************************************************************
00256  *      OR clause functions
00257  *****************************************************************************/
00258 
00259 /*
00260  * or_clause
00261  *
00262  * Returns t iff the clause is an 'or' clause: (OR { expr }).
00263  */
00264 bool
00265 or_clause(Node *clause)
00266 {
00267     return (clause != NULL &&
00268             IsA(clause, BoolExpr) &&
00269             ((BoolExpr *) clause)->boolop == OR_EXPR);
00270 }
00271 
00272 /*
00273  * make_orclause
00274  *
00275  * Creates an 'or' clause given a list of its subclauses.
00276  */
00277 Expr *
00278 make_orclause(List *orclauses)
00279 {
00280     BoolExpr   *expr = makeNode(BoolExpr);
00281 
00282     expr->boolop = OR_EXPR;
00283     expr->args = orclauses;
00284     expr->location = -1;
00285     return (Expr *) expr;
00286 }
00287 
00288 /*****************************************************************************
00289  *      AND clause functions
00290  *****************************************************************************/
00291 
00292 
00293 /*
00294  * and_clause
00295  *
00296  * Returns t iff its argument is an 'and' clause: (AND { expr }).
00297  */
00298 bool
00299 and_clause(Node *clause)
00300 {
00301     return (clause != NULL &&
00302             IsA(clause, BoolExpr) &&
00303             ((BoolExpr *) clause)->boolop == AND_EXPR);
00304 }
00305 
00306 /*
00307  * make_andclause
00308  *
00309  * Creates an 'and' clause given a list of its subclauses.
00310  */
00311 Expr *
00312 make_andclause(List *andclauses)
00313 {
00314     BoolExpr   *expr = makeNode(BoolExpr);
00315 
00316     expr->boolop = AND_EXPR;
00317     expr->args = andclauses;
00318     expr->location = -1;
00319     return (Expr *) expr;
00320 }
00321 
00322 /*
00323  * make_and_qual
00324  *
00325  * Variant of make_andclause for ANDing two qual conditions together.
00326  * Qual conditions have the property that a NULL nodetree is interpreted
00327  * as 'true'.
00328  *
00329  * NB: this makes no attempt to preserve AND/OR flatness; so it should not
00330  * be used on a qual that has already been run through prepqual.c.
00331  */
00332 Node *
00333 make_and_qual(Node *qual1, Node *qual2)
00334 {
00335     if (qual1 == NULL)
00336         return qual2;
00337     if (qual2 == NULL)
00338         return qual1;
00339     return (Node *) make_andclause(list_make2(qual1, qual2));
00340 }
00341 
00342 /*
00343  * Sometimes (such as in the input of ExecQual), we use lists of expression
00344  * nodes with implicit AND semantics.
00345  *
00346  * These functions convert between an AND-semantics expression list and the
00347  * ordinary representation of a boolean expression.
00348  *
00349  * Note that an empty list is considered equivalent to TRUE.
00350  */
00351 Expr *
00352 make_ands_explicit(List *andclauses)
00353 {
00354     if (andclauses == NIL)
00355         return (Expr *) makeBoolConst(true, false);
00356     else if (list_length(andclauses) == 1)
00357         return (Expr *) linitial(andclauses);
00358     else
00359         return make_andclause(andclauses);
00360 }
00361 
00362 List *
00363 make_ands_implicit(Expr *clause)
00364 {
00365     /*
00366      * NB: because the parser sets the qual field to NULL in a query that has
00367      * no WHERE clause, we must consider a NULL input clause as TRUE, even
00368      * though one might more reasonably think it FALSE.  Grumble. If this
00369      * causes trouble, consider changing the parser's behavior.
00370      */
00371     if (clause == NULL)
00372         return NIL;             /* NULL -> NIL list == TRUE */
00373     else if (and_clause((Node *) clause))
00374         return ((BoolExpr *) clause)->args;
00375     else if (IsA(clause, Const) &&
00376              !((Const *) clause)->constisnull &&
00377              DatumGetBool(((Const *) clause)->constvalue))
00378         return NIL;             /* constant TRUE input -> NIL list */
00379     else
00380         return list_make1(clause);
00381 }
00382 
00383 
00384 /*****************************************************************************
00385  *      Aggregate-function clause manipulation
00386  *****************************************************************************/
00387 
00388 /*
00389  * contain_agg_clause
00390  *    Recursively search for Aggref nodes within a clause.
00391  *
00392  *    Returns true if any aggregate found.
00393  *
00394  * This does not descend into subqueries, and so should be used only after
00395  * reduction of sublinks to subplans, or in contexts where it's known there
00396  * are no subqueries.  There mustn't be outer-aggregate references either.
00397  *
00398  * (If you want something like this but able to deal with subqueries,
00399  * see rewriteManip.c's contain_aggs_of_level().)
00400  */
00401 bool
00402 contain_agg_clause(Node *clause)
00403 {
00404     return contain_agg_clause_walker(clause, NULL);
00405 }
00406 
00407 static bool
00408 contain_agg_clause_walker(Node *node, void *context)
00409 {
00410     if (node == NULL)
00411         return false;
00412     if (IsA(node, Aggref))
00413     {
00414         Assert(((Aggref *) node)->agglevelsup == 0);
00415         return true;            /* abort the tree traversal and return true */
00416     }
00417     Assert(!IsA(node, SubLink));
00418     return expression_tree_walker(node, contain_agg_clause_walker, context);
00419 }
00420 
00421 /*
00422  * count_agg_clauses
00423  *    Recursively count the Aggref nodes in an expression tree, and
00424  *    accumulate other cost information about them too.
00425  *
00426  *    Note: this also checks for nested aggregates, which are an error.
00427  *
00428  * We not only count the nodes, but estimate their execution costs, and
00429  * attempt to estimate the total space needed for their transition state
00430  * values if all are evaluated in parallel (as would be done in a HashAgg
00431  * plan).  See AggClauseCosts for the exact set of statistics collected.
00432  *
00433  * NOTE that the counts/costs are ADDED to those already in *costs ... so
00434  * the caller is responsible for zeroing the struct initially.
00435  *
00436  * This does not descend into subqueries, and so should be used only after
00437  * reduction of sublinks to subplans, or in contexts where it's known there
00438  * are no subqueries.  There mustn't be outer-aggregate references either.
00439  */
00440 void
00441 count_agg_clauses(PlannerInfo *root, Node *clause, AggClauseCosts *costs)
00442 {
00443     count_agg_clauses_context context;
00444 
00445     context.root = root;
00446     context.costs = costs;
00447     (void) count_agg_clauses_walker(clause, &context);
00448 }
00449 
00450 static bool
00451 count_agg_clauses_walker(Node *node, count_agg_clauses_context *context)
00452 {
00453     if (node == NULL)
00454         return false;
00455     if (IsA(node, Aggref))
00456     {
00457         Aggref     *aggref = (Aggref *) node;
00458         AggClauseCosts *costs = context->costs;
00459         HeapTuple   aggTuple;
00460         Form_pg_aggregate aggform;
00461         Oid         aggtransfn;
00462         Oid         aggfinalfn;
00463         Oid         aggtranstype;
00464         QualCost    argcosts;
00465         Oid        *inputTypes;
00466         int         numArguments;
00467         ListCell   *l;
00468 
00469         Assert(aggref->agglevelsup == 0);
00470 
00471         /* fetch info about aggregate from pg_aggregate */
00472         aggTuple = SearchSysCache1(AGGFNOID,
00473                                    ObjectIdGetDatum(aggref->aggfnoid));
00474         if (!HeapTupleIsValid(aggTuple))
00475             elog(ERROR, "cache lookup failed for aggregate %u",
00476                  aggref->aggfnoid);
00477         aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
00478         aggtransfn = aggform->aggtransfn;
00479         aggfinalfn = aggform->aggfinalfn;
00480         aggtranstype = aggform->aggtranstype;
00481         ReleaseSysCache(aggTuple);
00482 
00483         /* count it */
00484         costs->numAggs++;
00485         if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
00486             costs->numOrderedAggs++;
00487 
00488         /* add component function execution costs to appropriate totals */
00489         costs->transCost.per_tuple += get_func_cost(aggtransfn) * cpu_operator_cost;
00490         if (OidIsValid(aggfinalfn))
00491             costs->finalCost += get_func_cost(aggfinalfn) * cpu_operator_cost;
00492 
00493         /* also add the input expressions' cost to per-input-row costs */
00494         cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
00495         costs->transCost.startup += argcosts.startup;
00496         costs->transCost.per_tuple += argcosts.per_tuple;
00497 
00498         /* extract argument types (ignoring any ORDER BY expressions) */
00499         inputTypes = (Oid *) palloc(sizeof(Oid) * list_length(aggref->args));
00500         numArguments = 0;
00501         foreach(l, aggref->args)
00502         {
00503             TargetEntry *tle = (TargetEntry *) lfirst(l);
00504 
00505             if (!tle->resjunk)
00506                 inputTypes[numArguments++] = exprType((Node *) tle->expr);
00507         }
00508 
00509         /* resolve actual type of transition state, if polymorphic */
00510         if (IsPolymorphicType(aggtranstype))
00511         {
00512             /* have to fetch the agg's declared input types... */
00513             Oid        *declaredArgTypes;
00514             int         agg_nargs;
00515 
00516             (void) get_func_signature(aggref->aggfnoid,
00517                                       &declaredArgTypes, &agg_nargs);
00518             Assert(agg_nargs == numArguments);
00519             aggtranstype = enforce_generic_type_consistency(inputTypes,
00520                                                             declaredArgTypes,
00521                                                             agg_nargs,
00522                                                             aggtranstype,
00523                                                             false);
00524             pfree(declaredArgTypes);
00525         }
00526 
00527         /*
00528          * If the transition type is pass-by-value then it doesn't add
00529          * anything to the required size of the hashtable.  If it is
00530          * pass-by-reference then we have to add the estimated size of the
00531          * value itself, plus palloc overhead.
00532          */
00533         if (!get_typbyval(aggtranstype))
00534         {
00535             int32       aggtranstypmod;
00536             int32       avgwidth;
00537 
00538             /*
00539              * If transition state is of same type as first input, assume it's
00540              * the same typmod (same width) as well.  This works for cases
00541              * like MAX/MIN and is probably somewhat reasonable otherwise.
00542              */
00543             if (numArguments > 0 && aggtranstype == inputTypes[0])
00544                 aggtranstypmod = exprTypmod((Node *) linitial(aggref->args));
00545             else
00546                 aggtranstypmod = -1;
00547 
00548             avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
00549             avgwidth = MAXALIGN(avgwidth);
00550 
00551             costs->transitionSpace += avgwidth + 2 * sizeof(void *);
00552         }
00553         else if (aggtranstype == INTERNALOID)
00554         {
00555             /*
00556              * INTERNAL transition type is a special case: although INTERNAL
00557              * is pass-by-value, it's almost certainly being used as a pointer
00558              * to some large data structure.  We assume usage of
00559              * ALLOCSET_DEFAULT_INITSIZE, which is a good guess if the data is
00560              * being kept in a private memory context, as is done by
00561              * array_agg() for instance.
00562              */
00563             costs->transitionSpace += ALLOCSET_DEFAULT_INITSIZE;
00564         }
00565 
00566         /*
00567          * Complain if the aggregate's arguments contain any aggregates;
00568          * nested agg functions are semantically nonsensical.
00569          */
00570         if (contain_agg_clause((Node *) aggref->args))
00571             ereport(ERROR,
00572                     (errcode(ERRCODE_GROUPING_ERROR),
00573                      errmsg("aggregate function calls cannot be nested")));
00574 
00575         /*
00576          * Having checked that, we need not recurse into the argument.
00577          */
00578         return false;
00579     }
00580     Assert(!IsA(node, SubLink));
00581     return expression_tree_walker(node, count_agg_clauses_walker,
00582                                   (void *) context);
00583 }
00584 
00585 
00586 /*****************************************************************************
00587  *      Window-function clause manipulation
00588  *****************************************************************************/
00589 
00590 /*
00591  * contain_window_function
00592  *    Recursively search for WindowFunc nodes within a clause.
00593  *
00594  * Since window functions don't have level fields, but are hard-wired to
00595  * be associated with the current query level, this is just the same as
00596  * rewriteManip.c's function.
00597  */
00598 bool
00599 contain_window_function(Node *clause)
00600 {
00601     return contain_windowfuncs(clause);
00602 }
00603 
00604 /*
00605  * find_window_functions
00606  *    Locate all the WindowFunc nodes in an expression tree, and organize
00607  *    them by winref ID number.
00608  *
00609  * Caller must provide an upper bound on the winref IDs expected in the tree.
00610  */
00611 WindowFuncLists *
00612 find_window_functions(Node *clause, Index maxWinRef)
00613 {
00614     WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
00615 
00616     lists->numWindowFuncs = 0;
00617     lists->maxWinRef = maxWinRef;
00618     lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
00619     (void) find_window_functions_walker(clause, lists);
00620     return lists;
00621 }
00622 
00623 static bool
00624 find_window_functions_walker(Node *node, WindowFuncLists *lists)
00625 {
00626     if (node == NULL)
00627         return false;
00628     if (IsA(node, WindowFunc))
00629     {
00630         WindowFunc *wfunc = (WindowFunc *) node;
00631 
00632         /* winref is unsigned, so one-sided test is OK */
00633         if (wfunc->winref > lists->maxWinRef)
00634             elog(ERROR, "WindowFunc contains out-of-range winref %u",
00635                  wfunc->winref);
00636         lists->windowFuncs[wfunc->winref] =
00637             lappend(lists->windowFuncs[wfunc->winref], wfunc);
00638         lists->numWindowFuncs++;
00639 
00640         /*
00641          * Complain if the window function's arguments contain window
00642          * functions
00643          */
00644         if (contain_window_function((Node *) wfunc->args))
00645             ereport(ERROR,
00646                     (errcode(ERRCODE_WINDOWING_ERROR),
00647                      errmsg("window function calls cannot be nested")));
00648 
00649         /*
00650          * Having checked that, we need not recurse into the argument.
00651          */
00652         return false;
00653     }
00654     Assert(!IsA(node, SubLink));
00655     return expression_tree_walker(node, find_window_functions_walker,
00656                                   (void *) lists);
00657 }
00658 
00659 
00660 /*****************************************************************************
00661  *      Support for expressions returning sets
00662  *****************************************************************************/
00663 
00664 /*
00665  * expression_returns_set_rows
00666  *    Estimate the number of rows returned by a set-returning expression.
00667  *    The result is 1 if there are no set-returning functions.
00668  *
00669  * We use the product of the rowcount estimates of all the functions in
00670  * the given tree (this corresponds to the behavior of ExecMakeFunctionResult
00671  * for nested set-returning functions).
00672  *
00673  * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
00674  */
00675 double
00676 expression_returns_set_rows(Node *clause)
00677 {
00678     double      result = 1;
00679 
00680     (void) expression_returns_set_rows_walker(clause, &result);
00681     return clamp_row_est(result);
00682 }
00683 
00684 static bool
00685 expression_returns_set_rows_walker(Node *node, double *count)
00686 {
00687     if (node == NULL)
00688         return false;
00689     if (IsA(node, FuncExpr))
00690     {
00691         FuncExpr   *expr = (FuncExpr *) node;
00692 
00693         if (expr->funcretset)
00694             *count *= get_func_rows(expr->funcid);
00695     }
00696     if (IsA(node, OpExpr))
00697     {
00698         OpExpr     *expr = (OpExpr *) node;
00699 
00700         if (expr->opretset)
00701         {
00702             set_opfuncid(expr);
00703             *count *= get_func_rows(expr->opfuncid);
00704         }
00705     }
00706 
00707     /* Avoid recursion for some cases that can't return a set */
00708     if (IsA(node, Aggref))
00709         return false;
00710     if (IsA(node, WindowFunc))
00711         return false;
00712     if (IsA(node, DistinctExpr))
00713         return false;
00714     if (IsA(node, NullIfExpr))
00715         return false;
00716     if (IsA(node, ScalarArrayOpExpr))
00717         return false;
00718     if (IsA(node, BoolExpr))
00719         return false;
00720     if (IsA(node, SubLink))
00721         return false;
00722     if (IsA(node, SubPlan))
00723         return false;
00724     if (IsA(node, AlternativeSubPlan))
00725         return false;
00726     if (IsA(node, ArrayExpr))
00727         return false;
00728     if (IsA(node, RowExpr))
00729         return false;
00730     if (IsA(node, RowCompareExpr))
00731         return false;
00732     if (IsA(node, CoalesceExpr))
00733         return false;
00734     if (IsA(node, MinMaxExpr))
00735         return false;
00736     if (IsA(node, XmlExpr))
00737         return false;
00738 
00739     return expression_tree_walker(node, expression_returns_set_rows_walker,
00740                                   (void *) count);
00741 }
00742 
00743 /*
00744  * tlist_returns_set_rows
00745  *    Estimate the number of rows returned by a set-returning targetlist.
00746  *    The result is 1 if there are no set-returning functions.
00747  *
00748  * Here, the result is the largest rowcount estimate of any of the tlist's
00749  * expressions, not the product as you would get from naively applying
00750  * expression_returns_set_rows() to the whole tlist.  The behavior actually
00751  * implemented by ExecTargetList produces a number of rows equal to the least
00752  * common multiple of the expression rowcounts, so that the product would be
00753  * a worst-case estimate that is typically not realistic.  Taking the max as
00754  * we do here is a best-case estimate that might not be realistic either,
00755  * but it's probably closer for typical usages.  We don't try to compute the
00756  * actual LCM because we're working with very approximate estimates, so their
00757  * LCM would be unduly noisy.
00758  */
00759 double
00760 tlist_returns_set_rows(List *tlist)
00761 {
00762     double      result = 1;
00763     ListCell   *lc;
00764 
00765     foreach(lc, tlist)
00766     {
00767         TargetEntry *tle = (TargetEntry *) lfirst(lc);
00768         double      colresult;
00769 
00770         colresult = expression_returns_set_rows((Node *) tle->expr);
00771         if (result < colresult)
00772             result = colresult;
00773     }
00774     return result;
00775 }
00776 
00777 
00778 /*****************************************************************************
00779  *      Subplan clause manipulation
00780  *****************************************************************************/
00781 
00782 /*
00783  * contain_subplans
00784  *    Recursively search for subplan nodes within a clause.
00785  *
00786  * If we see a SubLink node, we will return TRUE.  This is only possible if
00787  * the expression tree hasn't yet been transformed by subselect.c.  We do not
00788  * know whether the node will produce a true subplan or just an initplan,
00789  * but we make the conservative assumption that it will be a subplan.
00790  *
00791  * Returns true if any subplan found.
00792  */
00793 bool
00794 contain_subplans(Node *clause)
00795 {
00796     return contain_subplans_walker(clause, NULL);
00797 }
00798 
00799 static bool
00800 contain_subplans_walker(Node *node, void *context)
00801 {
00802     if (node == NULL)
00803         return false;
00804     if (IsA(node, SubPlan) ||
00805         IsA(node, AlternativeSubPlan) ||
00806         IsA(node, SubLink))
00807         return true;            /* abort the tree traversal and return true */
00808     return expression_tree_walker(node, contain_subplans_walker, context);
00809 }
00810 
00811 
00812 /*****************************************************************************
00813  *      Check clauses for mutable functions
00814  *****************************************************************************/
00815 
00816 /*
00817  * contain_mutable_functions
00818  *    Recursively search for mutable functions within a clause.
00819  *
00820  * Returns true if any mutable function (or operator implemented by a
00821  * mutable function) is found.  This test is needed so that we don't
00822  * mistakenly think that something like "WHERE random() < 0.5" can be treated
00823  * as a constant qualification.
00824  *
00825  * XXX we do not examine sub-selects to see if they contain uses of
00826  * mutable functions.  It's not real clear if that is correct or not...
00827  */
00828 bool
00829 contain_mutable_functions(Node *clause)
00830 {
00831     return contain_mutable_functions_walker(clause, NULL);
00832 }
00833 
00834 static bool
00835 contain_mutable_functions_walker(Node *node, void *context)
00836 {
00837     if (node == NULL)
00838         return false;
00839     if (IsA(node, FuncExpr))
00840     {
00841         FuncExpr   *expr = (FuncExpr *) node;
00842 
00843         if (func_volatile(expr->funcid) != PROVOLATILE_IMMUTABLE)
00844             return true;
00845         /* else fall through to check args */
00846     }
00847     else if (IsA(node, OpExpr))
00848     {
00849         OpExpr     *expr = (OpExpr *) node;
00850 
00851         set_opfuncid(expr);
00852         if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
00853             return true;
00854         /* else fall through to check args */
00855     }
00856     else if (IsA(node, DistinctExpr))
00857     {
00858         DistinctExpr *expr = (DistinctExpr *) node;
00859 
00860         set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
00861         if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
00862             return true;
00863         /* else fall through to check args */
00864     }
00865     else if (IsA(node, NullIfExpr))
00866     {
00867         NullIfExpr *expr = (NullIfExpr *) node;
00868 
00869         set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
00870         if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
00871             return true;
00872         /* else fall through to check args */
00873     }
00874     else if (IsA(node, ScalarArrayOpExpr))
00875     {
00876         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
00877 
00878         set_sa_opfuncid(expr);
00879         if (func_volatile(expr->opfuncid) != PROVOLATILE_IMMUTABLE)
00880             return true;
00881         /* else fall through to check args */
00882     }
00883     else if (IsA(node, CoerceViaIO))
00884     {
00885         CoerceViaIO *expr = (CoerceViaIO *) node;
00886         Oid         iofunc;
00887         Oid         typioparam;
00888         bool        typisvarlena;
00889 
00890         /* check the result type's input function */
00891         getTypeInputInfo(expr->resulttype,
00892                          &iofunc, &typioparam);
00893         if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
00894             return true;
00895         /* check the input type's output function */
00896         getTypeOutputInfo(exprType((Node *) expr->arg),
00897                           &iofunc, &typisvarlena);
00898         if (func_volatile(iofunc) != PROVOLATILE_IMMUTABLE)
00899             return true;
00900         /* else fall through to check args */
00901     }
00902     else if (IsA(node, ArrayCoerceExpr))
00903     {
00904         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
00905 
00906         if (OidIsValid(expr->elemfuncid) &&
00907             func_volatile(expr->elemfuncid) != PROVOLATILE_IMMUTABLE)
00908             return true;
00909         /* else fall through to check args */
00910     }
00911     else if (IsA(node, RowCompareExpr))
00912     {
00913         RowCompareExpr *rcexpr = (RowCompareExpr *) node;
00914         ListCell   *opid;
00915 
00916         foreach(opid, rcexpr->opnos)
00917         {
00918             if (op_volatile(lfirst_oid(opid)) != PROVOLATILE_IMMUTABLE)
00919                 return true;
00920         }
00921         /* else fall through to check args */
00922     }
00923     return expression_tree_walker(node, contain_mutable_functions_walker,
00924                                   context);
00925 }
00926 
00927 
00928 /*****************************************************************************
00929  *      Check clauses for volatile functions
00930  *****************************************************************************/
00931 
00932 /*
00933  * contain_volatile_functions
00934  *    Recursively search for volatile functions within a clause.
00935  *
00936  * Returns true if any volatile function (or operator implemented by a
00937  * volatile function) is found. This test prevents invalid conversions
00938  * of volatile expressions into indexscan quals.
00939  *
00940  * XXX we do not examine sub-selects to see if they contain uses of
00941  * volatile functions.  It's not real clear if that is correct or not...
00942  */
00943 bool
00944 contain_volatile_functions(Node *clause)
00945 {
00946     return contain_volatile_functions_walker(clause, NULL);
00947 }
00948 
00949 static bool
00950 contain_volatile_functions_walker(Node *node, void *context)
00951 {
00952     if (node == NULL)
00953         return false;
00954     if (IsA(node, FuncExpr))
00955     {
00956         FuncExpr   *expr = (FuncExpr *) node;
00957 
00958         if (func_volatile(expr->funcid) == PROVOLATILE_VOLATILE)
00959             return true;
00960         /* else fall through to check args */
00961     }
00962     else if (IsA(node, OpExpr))
00963     {
00964         OpExpr     *expr = (OpExpr *) node;
00965 
00966         set_opfuncid(expr);
00967         if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
00968             return true;
00969         /* else fall through to check args */
00970     }
00971     else if (IsA(node, DistinctExpr))
00972     {
00973         DistinctExpr *expr = (DistinctExpr *) node;
00974 
00975         set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
00976         if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
00977             return true;
00978         /* else fall through to check args */
00979     }
00980     else if (IsA(node, NullIfExpr))
00981     {
00982         NullIfExpr *expr = (NullIfExpr *) node;
00983 
00984         set_opfuncid((OpExpr *) expr);  /* rely on struct equivalence */
00985         if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
00986             return true;
00987         /* else fall through to check args */
00988     }
00989     else if (IsA(node, ScalarArrayOpExpr))
00990     {
00991         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
00992 
00993         set_sa_opfuncid(expr);
00994         if (func_volatile(expr->opfuncid) == PROVOLATILE_VOLATILE)
00995             return true;
00996         /* else fall through to check args */
00997     }
00998     else if (IsA(node, CoerceViaIO))
00999     {
01000         CoerceViaIO *expr = (CoerceViaIO *) node;
01001         Oid         iofunc;
01002         Oid         typioparam;
01003         bool        typisvarlena;
01004 
01005         /* check the result type's input function */
01006         getTypeInputInfo(expr->resulttype,
01007                          &iofunc, &typioparam);
01008         if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
01009             return true;
01010         /* check the input type's output function */
01011         getTypeOutputInfo(exprType((Node *) expr->arg),
01012                           &iofunc, &typisvarlena);
01013         if (func_volatile(iofunc) == PROVOLATILE_VOLATILE)
01014             return true;
01015         /* else fall through to check args */
01016     }
01017     else if (IsA(node, ArrayCoerceExpr))
01018     {
01019         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
01020 
01021         if (OidIsValid(expr->elemfuncid) &&
01022             func_volatile(expr->elemfuncid) == PROVOLATILE_VOLATILE)
01023             return true;
01024         /* else fall through to check args */
01025     }
01026     else if (IsA(node, RowCompareExpr))
01027     {
01028         /* RowCompare probably can't have volatile ops, but check anyway */
01029         RowCompareExpr *rcexpr = (RowCompareExpr *) node;
01030         ListCell   *opid;
01031 
01032         foreach(opid, rcexpr->opnos)
01033         {
01034             if (op_volatile(lfirst_oid(opid)) == PROVOLATILE_VOLATILE)
01035                 return true;
01036         }
01037         /* else fall through to check args */
01038     }
01039     return expression_tree_walker(node, contain_volatile_functions_walker,
01040                                   context);
01041 }
01042 
01043 
01044 /*****************************************************************************
01045  *      Check clauses for nonstrict functions
01046  *****************************************************************************/
01047 
01048 /*
01049  * contain_nonstrict_functions
01050  *    Recursively search for nonstrict functions within a clause.
01051  *
01052  * Returns true if any nonstrict construct is found --- ie, anything that
01053  * could produce non-NULL output with a NULL input.
01054  *
01055  * The idea here is that the caller has verified that the expression contains
01056  * one or more Var or Param nodes (as appropriate for the caller's need), and
01057  * now wishes to prove that the expression result will be NULL if any of these
01058  * inputs is NULL.  If we return false, then the proof succeeded.
01059  */
01060 bool
01061 contain_nonstrict_functions(Node *clause)
01062 {
01063     return contain_nonstrict_functions_walker(clause, NULL);
01064 }
01065 
01066 static bool
01067 contain_nonstrict_functions_walker(Node *node, void *context)
01068 {
01069     if (node == NULL)
01070         return false;
01071     if (IsA(node, Aggref))
01072     {
01073         /* an aggregate could return non-null with null input */
01074         return true;
01075     }
01076     if (IsA(node, WindowFunc))
01077     {
01078         /* a window function could return non-null with null input */
01079         return true;
01080     }
01081     if (IsA(node, ArrayRef))
01082     {
01083         /* array assignment is nonstrict, but subscripting is strict */
01084         if (((ArrayRef *) node)->refassgnexpr != NULL)
01085             return true;
01086         /* else fall through to check args */
01087     }
01088     if (IsA(node, FuncExpr))
01089     {
01090         FuncExpr   *expr = (FuncExpr *) node;
01091 
01092         if (!func_strict(expr->funcid))
01093             return true;
01094         /* else fall through to check args */
01095     }
01096     if (IsA(node, OpExpr))
01097     {
01098         OpExpr     *expr = (OpExpr *) node;
01099 
01100         set_opfuncid(expr);
01101         if (!func_strict(expr->opfuncid))
01102             return true;
01103         /* else fall through to check args */
01104     }
01105     if (IsA(node, DistinctExpr))
01106     {
01107         /* IS DISTINCT FROM is inherently non-strict */
01108         return true;
01109     }
01110     if (IsA(node, NullIfExpr))
01111         return true;
01112     if (IsA(node, ScalarArrayOpExpr))
01113     {
01114         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
01115 
01116         if (!is_strict_saop(expr, false))
01117             return true;
01118         /* else fall through to check args */
01119     }
01120     if (IsA(node, BoolExpr))
01121     {
01122         BoolExpr   *expr = (BoolExpr *) node;
01123 
01124         switch (expr->boolop)
01125         {
01126             case AND_EXPR:
01127             case OR_EXPR:
01128                 /* AND, OR are inherently non-strict */
01129                 return true;
01130             default:
01131                 break;
01132         }
01133     }
01134     if (IsA(node, SubLink))
01135     {
01136         /* In some cases a sublink might be strict, but in general not */
01137         return true;
01138     }
01139     if (IsA(node, SubPlan))
01140         return true;
01141     if (IsA(node, AlternativeSubPlan))
01142         return true;
01143     /* ArrayCoerceExpr is strict at the array level, regardless of elemfunc */
01144     if (IsA(node, FieldStore))
01145         return true;
01146     if (IsA(node, CaseExpr))
01147         return true;
01148     if (IsA(node, ArrayExpr))
01149         return true;
01150     if (IsA(node, RowExpr))
01151         return true;
01152     if (IsA(node, RowCompareExpr))
01153         return true;
01154     if (IsA(node, CoalesceExpr))
01155         return true;
01156     if (IsA(node, MinMaxExpr))
01157         return true;
01158     if (IsA(node, XmlExpr))
01159         return true;
01160     if (IsA(node, NullTest))
01161         return true;
01162     if (IsA(node, BooleanTest))
01163         return true;
01164     return expression_tree_walker(node, contain_nonstrict_functions_walker,
01165                                   context);
01166 }
01167 
01168 /*****************************************************************************
01169  *        Check clauses for non-leakproof functions
01170  *****************************************************************************/
01171 
01172 /*
01173  * contain_leaky_functions
01174  *      Recursively search for leaky functions within a clause.
01175  *
01176  * Returns true if any function call with side-effect may be present in the
01177  * clause.  Qualifiers from outside the a security_barrier view should not
01178  * be pushed down into the view, lest the contents of tuples intended to be
01179  * filtered out be revealed via side effects.
01180  */
01181 bool
01182 contain_leaky_functions(Node *clause)
01183 {
01184     return contain_leaky_functions_walker(clause, NULL);
01185 }
01186 
01187 static bool
01188 contain_leaky_functions_walker(Node *node, void *context)
01189 {
01190     if (node == NULL)
01191         return false;
01192 
01193     switch (nodeTag(node))
01194     {
01195         case T_Var:
01196         case T_Const:
01197         case T_Param:
01198         case T_ArrayExpr:
01199         case T_NamedArgExpr:
01200         case T_BoolExpr:
01201         case T_RelabelType:
01202         case T_CaseExpr:
01203         case T_CaseTestExpr:
01204         case T_RowExpr:
01205         case T_MinMaxExpr:
01206         case T_NullTest:
01207         case T_BooleanTest:
01208         case T_List:
01209 
01210             /*
01211              * We know these node types don't contain function calls; but
01212              * something further down in the node tree might.
01213              */
01214             break;
01215 
01216         case T_FuncExpr:
01217             {
01218                 FuncExpr   *expr = (FuncExpr *) node;
01219 
01220                 if (!get_func_leakproof(expr->funcid))
01221                     return true;
01222             }
01223             break;
01224 
01225         case T_OpExpr:
01226         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
01227         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
01228             {
01229                 OpExpr     *expr = (OpExpr *) node;
01230 
01231                 set_opfuncid(expr);
01232                 if (!get_func_leakproof(expr->opfuncid))
01233                     return true;
01234             }
01235             break;
01236 
01237         case T_ScalarArrayOpExpr:
01238             {
01239                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
01240 
01241                 set_sa_opfuncid(expr);
01242                 if (!get_func_leakproof(expr->opfuncid))
01243                     return true;
01244             }
01245             break;
01246 
01247         case T_CoerceViaIO:
01248             {
01249                 CoerceViaIO *expr = (CoerceViaIO *) node;
01250                 Oid         funcid;
01251                 Oid         ioparam;
01252                 bool        varlena;
01253 
01254                 getTypeInputInfo(exprType((Node *) expr->arg),
01255                                  &funcid, &ioparam);
01256                 if (!get_func_leakproof(funcid))
01257                     return true;
01258 
01259                 getTypeOutputInfo(expr->resulttype, &funcid, &varlena);
01260                 if (!get_func_leakproof(funcid))
01261                     return true;
01262             }
01263             break;
01264 
01265         case T_ArrayCoerceExpr:
01266             {
01267                 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
01268                 Oid         funcid;
01269                 Oid         ioparam;
01270                 bool        varlena;
01271 
01272                 getTypeInputInfo(exprType((Node *) expr->arg),
01273                                  &funcid, &ioparam);
01274                 if (!get_func_leakproof(funcid))
01275                     return true;
01276                 getTypeOutputInfo(expr->resulttype, &funcid, &varlena);
01277                 if (!get_func_leakproof(funcid))
01278                     return true;
01279             }
01280             break;
01281 
01282         case T_RowCompareExpr:
01283             {
01284                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
01285                 ListCell   *opid;
01286 
01287                 foreach(opid, rcexpr->opnos)
01288                 {
01289                     Oid         funcid = get_opcode(lfirst_oid(opid));
01290 
01291                     if (!get_func_leakproof(funcid))
01292                         return true;
01293                 }
01294             }
01295             break;
01296 
01297         default:
01298 
01299             /*
01300              * If we don't recognize the node tag, assume it might be leaky.
01301              * This prevents an unexpected security hole if someone adds a new
01302              * node type that can call a function.
01303              */
01304             return true;
01305     }
01306     return expression_tree_walker(node, contain_leaky_functions_walker,
01307                                   context);
01308 }
01309 
01310 /*
01311  * find_nonnullable_rels
01312  *      Determine which base rels are forced nonnullable by given clause.
01313  *
01314  * Returns the set of all Relids that are referenced in the clause in such
01315  * a way that the clause cannot possibly return TRUE if any of these Relids
01316  * is an all-NULL row.  (It is OK to err on the side of conservatism; hence
01317  * the analysis here is simplistic.)
01318  *
01319  * The semantics here are subtly different from contain_nonstrict_functions:
01320  * that function is concerned with NULL results from arbitrary expressions,
01321  * but here we assume that the input is a Boolean expression, and wish to
01322  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
01323  * the expression to have been AND/OR flattened and converted to implicit-AND
01324  * format.
01325  *
01326  * Note: this function is largely duplicative of find_nonnullable_vars().
01327  * The reason not to simplify this function into a thin wrapper around
01328  * find_nonnullable_vars() is that the tested conditions really are different:
01329  * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
01330  * that either v1 or v2 can't be NULL, but it does prove that the t1 row
01331  * as a whole can't be all-NULL.
01332  *
01333  * top_level is TRUE while scanning top-level AND/OR structure; here, showing
01334  * the result is either FALSE or NULL is good enough.  top_level is FALSE when
01335  * we have descended below a NOT or a strict function: now we must be able to
01336  * prove that the subexpression goes to NULL.
01337  *
01338  * We don't use expression_tree_walker here because we don't want to descend
01339  * through very many kinds of nodes; only the ones we can be sure are strict.
01340  */
01341 Relids
01342 find_nonnullable_rels(Node *clause)
01343 {
01344     return find_nonnullable_rels_walker(clause, true);
01345 }
01346 
01347 static Relids
01348 find_nonnullable_rels_walker(Node *node, bool top_level)
01349 {
01350     Relids      result = NULL;
01351     ListCell   *l;
01352 
01353     if (node == NULL)
01354         return NULL;
01355     if (IsA(node, Var))
01356     {
01357         Var        *var = (Var *) node;
01358 
01359         if (var->varlevelsup == 0)
01360             result = bms_make_singleton(var->varno);
01361     }
01362     else if (IsA(node, List))
01363     {
01364         /*
01365          * At top level, we are examining an implicit-AND list: if any of the
01366          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
01367          * not at top level, we are examining the arguments of a strict
01368          * function: if any of them produce NULL then the result of the
01369          * function must be NULL.  So in both cases, the set of nonnullable
01370          * rels is the union of those found in the arms, and we pass down the
01371          * top_level flag unmodified.
01372          */
01373         foreach(l, (List *) node)
01374         {
01375             result = bms_join(result,
01376                               find_nonnullable_rels_walker(lfirst(l),
01377                                                            top_level));
01378         }
01379     }
01380     else if (IsA(node, FuncExpr))
01381     {
01382         FuncExpr   *expr = (FuncExpr *) node;
01383 
01384         if (func_strict(expr->funcid))
01385             result = find_nonnullable_rels_walker((Node *) expr->args, false);
01386     }
01387     else if (IsA(node, OpExpr))
01388     {
01389         OpExpr     *expr = (OpExpr *) node;
01390 
01391         set_opfuncid(expr);
01392         if (func_strict(expr->opfuncid))
01393             result = find_nonnullable_rels_walker((Node *) expr->args, false);
01394     }
01395     else if (IsA(node, ScalarArrayOpExpr))
01396     {
01397         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
01398 
01399         if (is_strict_saop(expr, true))
01400             result = find_nonnullable_rels_walker((Node *) expr->args, false);
01401     }
01402     else if (IsA(node, BoolExpr))
01403     {
01404         BoolExpr   *expr = (BoolExpr *) node;
01405 
01406         switch (expr->boolop)
01407         {
01408             case AND_EXPR:
01409                 /* At top level we can just recurse (to the List case) */
01410                 if (top_level)
01411                 {
01412                     result = find_nonnullable_rels_walker((Node *) expr->args,
01413                                                           top_level);
01414                     break;
01415                 }
01416 
01417                 /*
01418                  * Below top level, even if one arm produces NULL, the result
01419                  * could be FALSE (hence not NULL).  However, if *all* the
01420                  * arms produce NULL then the result is NULL, so we can take
01421                  * the intersection of the sets of nonnullable rels, just as
01422                  * for OR.  Fall through to share code.
01423                  */
01424                 /* FALL THRU */
01425             case OR_EXPR:
01426 
01427                 /*
01428                  * OR is strict if all of its arms are, so we can take the
01429                  * intersection of the sets of nonnullable rels for each arm.
01430                  * This works for both values of top_level.
01431                  */
01432                 foreach(l, expr->args)
01433                 {
01434                     Relids      subresult;
01435 
01436                     subresult = find_nonnullable_rels_walker(lfirst(l),
01437                                                              top_level);
01438                     if (result == NULL) /* first subresult? */
01439                         result = subresult;
01440                     else
01441                         result = bms_int_members(result, subresult);
01442 
01443                     /*
01444                      * If the intersection is empty, we can stop looking. This
01445                      * also justifies the test for first-subresult above.
01446                      */
01447                     if (bms_is_empty(result))
01448                         break;
01449                 }
01450                 break;
01451             case NOT_EXPR:
01452                 /* NOT will return null if its arg is null */
01453                 result = find_nonnullable_rels_walker((Node *) expr->args,
01454                                                       false);
01455                 break;
01456             default:
01457                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
01458                 break;
01459         }
01460     }
01461     else if (IsA(node, RelabelType))
01462     {
01463         RelabelType *expr = (RelabelType *) node;
01464 
01465         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
01466     }
01467     else if (IsA(node, CoerceViaIO))
01468     {
01469         /* not clear this is useful, but it can't hurt */
01470         CoerceViaIO *expr = (CoerceViaIO *) node;
01471 
01472         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
01473     }
01474     else if (IsA(node, ArrayCoerceExpr))
01475     {
01476         /* ArrayCoerceExpr is strict at the array level */
01477         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
01478 
01479         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
01480     }
01481     else if (IsA(node, ConvertRowtypeExpr))
01482     {
01483         /* not clear this is useful, but it can't hurt */
01484         ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
01485 
01486         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
01487     }
01488     else if (IsA(node, CollateExpr))
01489     {
01490         CollateExpr *expr = (CollateExpr *) node;
01491 
01492         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
01493     }
01494     else if (IsA(node, NullTest))
01495     {
01496         /* IS NOT NULL can be considered strict, but only at top level */
01497         NullTest   *expr = (NullTest *) node;
01498 
01499         if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
01500             result = find_nonnullable_rels_walker((Node *) expr->arg, false);
01501     }
01502     else if (IsA(node, BooleanTest))
01503     {
01504         /* Boolean tests that reject NULL are strict at top level */
01505         BooleanTest *expr = (BooleanTest *) node;
01506 
01507         if (top_level &&
01508             (expr->booltesttype == IS_TRUE ||
01509              expr->booltesttype == IS_FALSE ||
01510              expr->booltesttype == IS_NOT_UNKNOWN))
01511             result = find_nonnullable_rels_walker((Node *) expr->arg, false);
01512     }
01513     else if (IsA(node, PlaceHolderVar))
01514     {
01515         PlaceHolderVar *phv = (PlaceHolderVar *) node;
01516 
01517         result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
01518     }
01519     return result;
01520 }
01521 
01522 /*
01523  * find_nonnullable_vars
01524  *      Determine which Vars are forced nonnullable by given clause.
01525  *
01526  * Returns a list of all level-zero Vars that are referenced in the clause in
01527  * such a way that the clause cannot possibly return TRUE if any of these Vars
01528  * is NULL.  (It is OK to err on the side of conservatism; hence the analysis
01529  * here is simplistic.)
01530  *
01531  * The semantics here are subtly different from contain_nonstrict_functions:
01532  * that function is concerned with NULL results from arbitrary expressions,
01533  * but here we assume that the input is a Boolean expression, and wish to
01534  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
01535  * the expression to have been AND/OR flattened and converted to implicit-AND
01536  * format.
01537  *
01538  * The result is a palloc'd List, but we have not copied the member Var nodes.
01539  * Also, we don't bother trying to eliminate duplicate entries.
01540  *
01541  * top_level is TRUE while scanning top-level AND/OR structure; here, showing
01542  * the result is either FALSE or NULL is good enough.  top_level is FALSE when
01543  * we have descended below a NOT or a strict function: now we must be able to
01544  * prove that the subexpression goes to NULL.
01545  *
01546  * We don't use expression_tree_walker here because we don't want to descend
01547  * through very many kinds of nodes; only the ones we can be sure are strict.
01548  */
01549 List *
01550 find_nonnullable_vars(Node *clause)
01551 {
01552     return find_nonnullable_vars_walker(clause, true);
01553 }
01554 
01555 static List *
01556 find_nonnullable_vars_walker(Node *node, bool top_level)
01557 {
01558     List       *result = NIL;
01559     ListCell   *l;
01560 
01561     if (node == NULL)
01562         return NIL;
01563     if (IsA(node, Var))
01564     {
01565         Var        *var = (Var *) node;
01566 
01567         if (var->varlevelsup == 0)
01568             result = list_make1(var);
01569     }
01570     else if (IsA(node, List))
01571     {
01572         /*
01573          * At top level, we are examining an implicit-AND list: if any of the
01574          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
01575          * not at top level, we are examining the arguments of a strict
01576          * function: if any of them produce NULL then the result of the
01577          * function must be NULL.  So in both cases, the set of nonnullable
01578          * vars is the union of those found in the arms, and we pass down the
01579          * top_level flag unmodified.
01580          */
01581         foreach(l, (List *) node)
01582         {
01583             result = list_concat(result,
01584                                  find_nonnullable_vars_walker(lfirst(l),
01585                                                               top_level));
01586         }
01587     }
01588     else if (IsA(node, FuncExpr))
01589     {
01590         FuncExpr   *expr = (FuncExpr *) node;
01591 
01592         if (func_strict(expr->funcid))
01593             result = find_nonnullable_vars_walker((Node *) expr->args, false);
01594     }
01595     else if (IsA(node, OpExpr))
01596     {
01597         OpExpr     *expr = (OpExpr *) node;
01598 
01599         set_opfuncid(expr);
01600         if (func_strict(expr->opfuncid))
01601             result = find_nonnullable_vars_walker((Node *) expr->args, false);
01602     }
01603     else if (IsA(node, ScalarArrayOpExpr))
01604     {
01605         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
01606 
01607         if (is_strict_saop(expr, true))
01608             result = find_nonnullable_vars_walker((Node *) expr->args, false);
01609     }
01610     else if (IsA(node, BoolExpr))
01611     {
01612         BoolExpr   *expr = (BoolExpr *) node;
01613 
01614         switch (expr->boolop)
01615         {
01616             case AND_EXPR:
01617                 /* At top level we can just recurse (to the List case) */
01618                 if (top_level)
01619                 {
01620                     result = find_nonnullable_vars_walker((Node *) expr->args,
01621                                                           top_level);
01622                     break;
01623                 }
01624 
01625                 /*
01626                  * Below top level, even if one arm produces NULL, the result
01627                  * could be FALSE (hence not NULL).  However, if *all* the
01628                  * arms produce NULL then the result is NULL, so we can take
01629                  * the intersection of the sets of nonnullable vars, just as
01630                  * for OR.  Fall through to share code.
01631                  */
01632                 /* FALL THRU */
01633             case OR_EXPR:
01634 
01635                 /*
01636                  * OR is strict if all of its arms are, so we can take the
01637                  * intersection of the sets of nonnullable vars for each arm.
01638                  * This works for both values of top_level.
01639                  */
01640                 foreach(l, expr->args)
01641                 {
01642                     List       *subresult;
01643 
01644                     subresult = find_nonnullable_vars_walker(lfirst(l),
01645                                                              top_level);
01646                     if (result == NIL)  /* first subresult? */
01647                         result = subresult;
01648                     else
01649                         result = list_intersection(result, subresult);
01650 
01651                     /*
01652                      * If the intersection is empty, we can stop looking. This
01653                      * also justifies the test for first-subresult above.
01654                      */
01655                     if (result == NIL)
01656                         break;
01657                 }
01658                 break;
01659             case NOT_EXPR:
01660                 /* NOT will return null if its arg is null */
01661                 result = find_nonnullable_vars_walker((Node *) expr->args,
01662                                                       false);
01663                 break;
01664             default:
01665                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
01666                 break;
01667         }
01668     }
01669     else if (IsA(node, RelabelType))
01670     {
01671         RelabelType *expr = (RelabelType *) node;
01672 
01673         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
01674     }
01675     else if (IsA(node, CoerceViaIO))
01676     {
01677         /* not clear this is useful, but it can't hurt */
01678         CoerceViaIO *expr = (CoerceViaIO *) node;
01679 
01680         result = find_nonnullable_vars_walker((Node *) expr->arg, false);
01681     }
01682     else if (IsA(node, ArrayCoerceExpr))
01683     {
01684         /* ArrayCoerceExpr is strict at the array level */
01685         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
01686 
01687         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
01688     }
01689     else if (IsA(node, ConvertRowtypeExpr))
01690     {
01691         /* not clear this is useful, but it can't hurt */
01692         ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
01693 
01694         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
01695     }
01696     else if (IsA(node, CollateExpr))
01697     {
01698         CollateExpr *expr = (CollateExpr *) node;
01699 
01700         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
01701     }
01702     else if (IsA(node, NullTest))
01703     {
01704         /* IS NOT NULL can be considered strict, but only at top level */
01705         NullTest   *expr = (NullTest *) node;
01706 
01707         if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
01708             result = find_nonnullable_vars_walker((Node *) expr->arg, false);
01709     }
01710     else if (IsA(node, BooleanTest))
01711     {
01712         /* Boolean tests that reject NULL are strict at top level */
01713         BooleanTest *expr = (BooleanTest *) node;
01714 
01715         if (top_level &&
01716             (expr->booltesttype == IS_TRUE ||
01717              expr->booltesttype == IS_FALSE ||
01718              expr->booltesttype == IS_NOT_UNKNOWN))
01719             result = find_nonnullable_vars_walker((Node *) expr->arg, false);
01720     }
01721     else if (IsA(node, PlaceHolderVar))
01722     {
01723         PlaceHolderVar *phv = (PlaceHolderVar *) node;
01724 
01725         result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
01726     }
01727     return result;
01728 }
01729 
01730 /*
01731  * find_forced_null_vars
01732  *      Determine which Vars must be NULL for the given clause to return TRUE.
01733  *
01734  * This is the complement of find_nonnullable_vars: find the level-zero Vars
01735  * that must be NULL for the clause to return TRUE.  (It is OK to err on the
01736  * side of conservatism; hence the analysis here is simplistic.  In fact,
01737  * we only detect simple "var IS NULL" tests at the top level.)
01738  *
01739  * The result is a palloc'd List, but we have not copied the member Var nodes.
01740  * Also, we don't bother trying to eliminate duplicate entries.
01741  */
01742 List *
01743 find_forced_null_vars(Node *node)
01744 {
01745     List       *result = NIL;
01746     Var        *var;
01747     ListCell   *l;
01748 
01749     if (node == NULL)
01750         return NIL;
01751     /* Check single-clause cases using subroutine */
01752     var = find_forced_null_var(node);
01753     if (var)
01754     {
01755         result = list_make1(var);
01756     }
01757     /* Otherwise, handle AND-conditions */
01758     else if (IsA(node, List))
01759     {
01760         /*
01761          * At top level, we are examining an implicit-AND list: if any of the
01762          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
01763          */
01764         foreach(l, (List *) node)
01765         {
01766             result = list_concat(result,
01767                                  find_forced_null_vars(lfirst(l)));
01768         }
01769     }
01770     else if (IsA(node, BoolExpr))
01771     {
01772         BoolExpr   *expr = (BoolExpr *) node;
01773 
01774         /*
01775          * We don't bother considering the OR case, because it's fairly
01776          * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
01777          * the NOT case isn't worth expending code on.
01778          */
01779         if (expr->boolop == AND_EXPR)
01780         {
01781             /* At top level we can just recurse (to the List case) */
01782             result = find_forced_null_vars((Node *) expr->args);
01783         }
01784     }
01785     return result;
01786 }
01787 
01788 /*
01789  * find_forced_null_var
01790  *      Return the Var forced null by the given clause, or NULL if it's
01791  *      not an IS NULL-type clause.  For success, the clause must enforce
01792  *      *only* nullness of the particular Var, not any other conditions.
01793  *
01794  * This is just the single-clause case of find_forced_null_vars(), without
01795  * any allowance for AND conditions.  It's used by initsplan.c on individual
01796  * qual clauses.  The reason for not just applying find_forced_null_vars()
01797  * is that if an AND of an IS NULL clause with something else were to somehow
01798  * survive AND/OR flattening, initsplan.c might get fooled into discarding
01799  * the whole clause when only the IS NULL part of it had been proved redundant.
01800  */
01801 Var *
01802 find_forced_null_var(Node *node)
01803 {
01804     if (node == NULL)
01805         return NULL;
01806     if (IsA(node, NullTest))
01807     {
01808         /* check for var IS NULL */
01809         NullTest   *expr = (NullTest *) node;
01810 
01811         if (expr->nulltesttype == IS_NULL && !expr->argisrow)
01812         {
01813             Var        *var = (Var *) expr->arg;
01814 
01815             if (var && IsA(var, Var) &&
01816                 var->varlevelsup == 0)
01817                 return var;
01818         }
01819     }
01820     else if (IsA(node, BooleanTest))
01821     {
01822         /* var IS UNKNOWN is equivalent to var IS NULL */
01823         BooleanTest *expr = (BooleanTest *) node;
01824 
01825         if (expr->booltesttype == IS_UNKNOWN)
01826         {
01827             Var        *var = (Var *) expr->arg;
01828 
01829             if (var && IsA(var, Var) &&
01830                 var->varlevelsup == 0)
01831                 return var;
01832         }
01833     }
01834     return NULL;
01835 }
01836 
01837 /*
01838  * Can we treat a ScalarArrayOpExpr as strict?
01839  *
01840  * If "falseOK" is true, then a "false" result can be considered strict,
01841  * else we need to guarantee an actual NULL result for NULL input.
01842  *
01843  * "foo op ALL array" is strict if the op is strict *and* we can prove
01844  * that the array input isn't an empty array.  We can check that
01845  * for the cases of an array constant and an ARRAY[] construct.
01846  *
01847  * "foo op ANY array" is strict in the falseOK sense if the op is strict.
01848  * If not falseOK, the test is the same as for "foo op ALL array".
01849  */
01850 static bool
01851 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
01852 {
01853     Node       *rightop;
01854 
01855     /* The contained operator must be strict. */
01856     set_sa_opfuncid(expr);
01857     if (!func_strict(expr->opfuncid))
01858         return false;
01859     /* If ANY and falseOK, that's all we need to check. */
01860     if (expr->useOr && falseOK)
01861         return true;
01862     /* Else, we have to see if the array is provably non-empty. */
01863     Assert(list_length(expr->args) == 2);
01864     rightop = (Node *) lsecond(expr->args);
01865     if (rightop && IsA(rightop, Const))
01866     {
01867         Datum       arraydatum = ((Const *) rightop)->constvalue;
01868         bool        arrayisnull = ((Const *) rightop)->constisnull;
01869         ArrayType  *arrayval;
01870         int         nitems;
01871 
01872         if (arrayisnull)
01873             return false;
01874         arrayval = DatumGetArrayTypeP(arraydatum);
01875         nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
01876         if (nitems > 0)
01877             return true;
01878     }
01879     else if (rightop && IsA(rightop, ArrayExpr))
01880     {
01881         ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
01882 
01883         if (arrayexpr->elements != NIL && !arrayexpr->multidims)
01884             return true;
01885     }
01886     return false;
01887 }
01888 
01889 
01890 /*****************************************************************************
01891  *      Check for "pseudo-constant" clauses
01892  *****************************************************************************/
01893 
01894 /*
01895  * is_pseudo_constant_clause
01896  *    Detect whether an expression is "pseudo constant", ie, it contains no
01897  *    variables of the current query level and no uses of volatile functions.
01898  *    Such an expr is not necessarily a true constant: it can still contain
01899  *    Params and outer-level Vars, not to mention functions whose results
01900  *    may vary from one statement to the next.  However, the expr's value
01901  *    will be constant over any one scan of the current query, so it can be
01902  *    used as, eg, an indexscan key.
01903  *
01904  * CAUTION: this function omits to test for one very important class of
01905  * not-constant expressions, namely aggregates (Aggrefs).  In current usage
01906  * this is only applied to WHERE clauses and so a check for Aggrefs would be
01907  * a waste of cycles; but be sure to also check contain_agg_clause() if you
01908  * want to know about pseudo-constness in other contexts.  The same goes
01909  * for window functions (WindowFuncs).
01910  */
01911 bool
01912 is_pseudo_constant_clause(Node *clause)
01913 {
01914     /*
01915      * We could implement this check in one recursive scan.  But since the
01916      * check for volatile functions is both moderately expensive and unlikely
01917      * to fail, it seems better to look for Vars first and only check for
01918      * volatile functions if we find no Vars.
01919      */
01920     if (!contain_var_clause(clause) &&
01921         !contain_volatile_functions(clause))
01922         return true;
01923     return false;
01924 }
01925 
01926 /*
01927  * is_pseudo_constant_clause_relids
01928  *    Same as above, except caller already has available the var membership
01929  *    of the expression; this lets us avoid the contain_var_clause() scan.
01930  */
01931 bool
01932 is_pseudo_constant_clause_relids(Node *clause, Relids relids)
01933 {
01934     if (bms_is_empty(relids) &&
01935         !contain_volatile_functions(clause))
01936         return true;
01937     return false;
01938 }
01939 
01940 
01941 /*****************************************************************************
01942  *                                                                           *
01943  *      General clause-manipulating routines                                 *
01944  *                                                                           *
01945  *****************************************************************************/
01946 
01947 /*
01948  * NumRelids
01949  *      (formerly clause_relids)
01950  *
01951  * Returns the number of different relations referenced in 'clause'.
01952  */
01953 int
01954 NumRelids(Node *clause)
01955 {
01956     Relids      varnos = pull_varnos(clause);
01957     int         result = bms_num_members(varnos);
01958 
01959     bms_free(varnos);
01960     return result;
01961 }
01962 
01963 /*
01964  * CommuteOpExpr: commute a binary operator clause
01965  *
01966  * XXX the clause is destructively modified!
01967  */
01968 void
01969 CommuteOpExpr(OpExpr *clause)
01970 {
01971     Oid         opoid;
01972     Node       *temp;
01973 
01974     /* Sanity checks: caller is at fault if these fail */
01975     if (!is_opclause(clause) ||
01976         list_length(clause->args) != 2)
01977         elog(ERROR, "cannot commute non-binary-operator clause");
01978 
01979     opoid = get_commutator(clause->opno);
01980 
01981     if (!OidIsValid(opoid))
01982         elog(ERROR, "could not find commutator for operator %u",
01983              clause->opno);
01984 
01985     /*
01986      * modify the clause in-place!
01987      */
01988     clause->opno = opoid;
01989     clause->opfuncid = InvalidOid;
01990     /* opresulttype, opretset, opcollid, inputcollid need not change */
01991 
01992     temp = linitial(clause->args);
01993     linitial(clause->args) = lsecond(clause->args);
01994     lsecond(clause->args) = temp;
01995 }
01996 
01997 /*
01998  * CommuteRowCompareExpr: commute a RowCompareExpr clause
01999  *
02000  * XXX the clause is destructively modified!
02001  */
02002 void
02003 CommuteRowCompareExpr(RowCompareExpr *clause)
02004 {
02005     List       *newops;
02006     List       *temp;
02007     ListCell   *l;
02008 
02009     /* Sanity checks: caller is at fault if these fail */
02010     if (!IsA(clause, RowCompareExpr))
02011         elog(ERROR, "expected a RowCompareExpr");
02012 
02013     /* Build list of commuted operators */
02014     newops = NIL;
02015     foreach(l, clause->opnos)
02016     {
02017         Oid         opoid = lfirst_oid(l);
02018 
02019         opoid = get_commutator(opoid);
02020         if (!OidIsValid(opoid))
02021             elog(ERROR, "could not find commutator for operator %u",
02022                  lfirst_oid(l));
02023         newops = lappend_oid(newops, opoid);
02024     }
02025 
02026     /*
02027      * modify the clause in-place!
02028      */
02029     switch (clause->rctype)
02030     {
02031         case ROWCOMPARE_LT:
02032             clause->rctype = ROWCOMPARE_GT;
02033             break;
02034         case ROWCOMPARE_LE:
02035             clause->rctype = ROWCOMPARE_GE;
02036             break;
02037         case ROWCOMPARE_GE:
02038             clause->rctype = ROWCOMPARE_LE;
02039             break;
02040         case ROWCOMPARE_GT:
02041             clause->rctype = ROWCOMPARE_LT;
02042             break;
02043         default:
02044             elog(ERROR, "unexpected RowCompare type: %d",
02045                  (int) clause->rctype);
02046             break;
02047     }
02048 
02049     clause->opnos = newops;
02050 
02051     /*
02052      * Note: we need not change the opfamilies list; we assume any btree
02053      * opfamily containing an operator will also contain its commutator.
02054      * Collations don't change either.
02055      */
02056 
02057     temp = clause->largs;
02058     clause->largs = clause->rargs;
02059     clause->rargs = temp;
02060 }
02061 
02062 /*
02063  * strip_implicit_coercions: remove implicit coercions at top level of tree
02064  *
02065  * Note: there isn't any useful thing we can do with a RowExpr here, so
02066  * just return it unchanged, even if it's marked as an implicit coercion.
02067  */
02068 Node *
02069 strip_implicit_coercions(Node *node)
02070 {
02071     if (node == NULL)
02072         return NULL;
02073     if (IsA(node, FuncExpr))
02074     {
02075         FuncExpr   *f = (FuncExpr *) node;
02076 
02077         if (f->funcformat == COERCE_IMPLICIT_CAST)
02078             return strip_implicit_coercions(linitial(f->args));
02079     }
02080     else if (IsA(node, RelabelType))
02081     {
02082         RelabelType *r = (RelabelType *) node;
02083 
02084         if (r->relabelformat == COERCE_IMPLICIT_CAST)
02085             return strip_implicit_coercions((Node *) r->arg);
02086     }
02087     else if (IsA(node, CoerceViaIO))
02088     {
02089         CoerceViaIO *c = (CoerceViaIO *) node;
02090 
02091         if (c->coerceformat == COERCE_IMPLICIT_CAST)
02092             return strip_implicit_coercions((Node *) c->arg);
02093     }
02094     else if (IsA(node, ArrayCoerceExpr))
02095     {
02096         ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
02097 
02098         if (c->coerceformat == COERCE_IMPLICIT_CAST)
02099             return strip_implicit_coercions((Node *) c->arg);
02100     }
02101     else if (IsA(node, ConvertRowtypeExpr))
02102     {
02103         ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
02104 
02105         if (c->convertformat == COERCE_IMPLICIT_CAST)
02106             return strip_implicit_coercions((Node *) c->arg);
02107     }
02108     else if (IsA(node, CoerceToDomain))
02109     {
02110         CoerceToDomain *c = (CoerceToDomain *) node;
02111 
02112         if (c->coercionformat == COERCE_IMPLICIT_CAST)
02113             return strip_implicit_coercions((Node *) c->arg);
02114     }
02115     return node;
02116 }
02117 
02118 /*
02119  * Helper for eval_const_expressions: check that datatype of an attribute
02120  * is still what it was when the expression was parsed.  This is needed to
02121  * guard against improper simplification after ALTER COLUMN TYPE.  (XXX we
02122  * may well need to make similar checks elsewhere?)
02123  */
02124 static bool
02125 rowtype_field_matches(Oid rowtypeid, int fieldnum,
02126                       Oid expectedtype, int32 expectedtypmod,
02127                       Oid expectedcollation)
02128 {
02129     TupleDesc   tupdesc;
02130     Form_pg_attribute attr;
02131 
02132     /* No issue for RECORD, since there is no way to ALTER such a type */
02133     if (rowtypeid == RECORDOID)
02134         return true;
02135     tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
02136     if (fieldnum <= 0 || fieldnum > tupdesc->natts)
02137     {
02138         ReleaseTupleDesc(tupdesc);
02139         return false;
02140     }
02141     attr = tupdesc->attrs[fieldnum - 1];
02142     if (attr->attisdropped ||
02143         attr->atttypid != expectedtype ||
02144         attr->atttypmod != expectedtypmod ||
02145         attr->attcollation != expectedcollation)
02146     {
02147         ReleaseTupleDesc(tupdesc);
02148         return false;
02149     }
02150     ReleaseTupleDesc(tupdesc);
02151     return true;
02152 }
02153 
02154 
02155 /*--------------------
02156  * eval_const_expressions
02157  *
02158  * Reduce any recognizably constant subexpressions of the given
02159  * expression tree, for example "2 + 2" => "4".  More interestingly,
02160  * we can reduce certain boolean expressions even when they contain
02161  * non-constant subexpressions: "x OR true" => "true" no matter what
02162  * the subexpression x is.  (XXX We assume that no such subexpression
02163  * will have important side-effects, which is not necessarily a good
02164  * assumption in the presence of user-defined functions; do we need a
02165  * pg_proc flag that prevents discarding the execution of a function?)
02166  *
02167  * We do understand that certain functions may deliver non-constant
02168  * results even with constant inputs, "nextval()" being the classic
02169  * example.  Functions that are not marked "immutable" in pg_proc
02170  * will not be pre-evaluated here, although we will reduce their
02171  * arguments as far as possible.
02172  *
02173  * Whenever a function is eliminated from the expression by means of
02174  * constant-expression evaluation or inlining, we add the function to
02175  * root->glob->invalItems.  This ensures the plan is known to depend on
02176  * such functions, even though they aren't referenced anymore.
02177  *
02178  * We assume that the tree has already been type-checked and contains
02179  * only operators and functions that are reasonable to try to execute.
02180  *
02181  * NOTE: "root" can be passed as NULL if the caller never wants to do any
02182  * Param substitutions nor receive info about inlined functions.
02183  *
02184  * NOTE: the planner assumes that this will always flatten nested AND and
02185  * OR clauses into N-argument form.  See comments in prepqual.c.
02186  *
02187  * NOTE: another critical effect is that any function calls that require
02188  * default arguments will be expanded, and named-argument calls will be
02189  * converted to positional notation.  The executor won't handle either.
02190  *--------------------
02191  */
02192 Node *
02193 eval_const_expressions(PlannerInfo *root, Node *node)
02194 {
02195     eval_const_expressions_context context;
02196 
02197     if (root)
02198         context.boundParams = root->glob->boundParams;  /* bound Params */
02199     else
02200         context.boundParams = NULL;
02201     context.root = root;        /* for inlined-function dependencies */
02202     context.active_fns = NIL;   /* nothing being recursively simplified */
02203     context.case_val = NULL;    /* no CASE being examined */
02204     context.estimate = false;   /* safe transformations only */
02205     return eval_const_expressions_mutator(node, &context);
02206 }
02207 
02208 /*--------------------
02209  * estimate_expression_value
02210  *
02211  * This function attempts to estimate the value of an expression for
02212  * planning purposes.  It is in essence a more aggressive version of
02213  * eval_const_expressions(): we will perform constant reductions that are
02214  * not necessarily 100% safe, but are reasonable for estimation purposes.
02215  *
02216  * Currently the extra steps that are taken in this mode are:
02217  * 1. Substitute values for Params, where a bound Param value has been made
02218  *    available by the caller of planner(), even if the Param isn't marked
02219  *    constant.  This effectively means that we plan using the first supplied
02220  *    value of the Param.
02221  * 2. Fold stable, as well as immutable, functions to constants.
02222  * 3. Reduce PlaceHolderVar nodes to their contained expressions.
02223  *--------------------
02224  */
02225 Node *
02226 estimate_expression_value(PlannerInfo *root, Node *node)
02227 {
02228     eval_const_expressions_context context;
02229 
02230     context.boundParams = root->glob->boundParams;      /* bound Params */
02231     /* we do not need to mark the plan as depending on inlined functions */
02232     context.root = NULL;
02233     context.active_fns = NIL;   /* nothing being recursively simplified */
02234     context.case_val = NULL;    /* no CASE being examined */
02235     context.estimate = true;    /* unsafe transformations OK */
02236     return eval_const_expressions_mutator(node, &context);
02237 }
02238 
02239 static Node *
02240 eval_const_expressions_mutator(Node *node,
02241                                eval_const_expressions_context *context)
02242 {
02243     if (node == NULL)
02244         return NULL;
02245     switch (nodeTag(node))
02246     {
02247         case T_Param:
02248             {
02249                 Param      *param = (Param *) node;
02250 
02251                 /* Look to see if we've been given a value for this Param */
02252                 if (param->paramkind == PARAM_EXTERN &&
02253                     context->boundParams != NULL &&
02254                     param->paramid > 0 &&
02255                     param->paramid <= context->boundParams->numParams)
02256                 {
02257                     ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
02258 
02259                     if (OidIsValid(prm->ptype))
02260                     {
02261                         /* OK to substitute parameter value? */
02262                         if (context->estimate ||
02263                             (prm->pflags & PARAM_FLAG_CONST))
02264                         {
02265                             /*
02266                              * Return a Const representing the param value.
02267                              * Must copy pass-by-ref datatypes, since the
02268                              * Param might be in a memory context
02269                              * shorter-lived than our output plan should be.
02270                              */
02271                             int16       typLen;
02272                             bool        typByVal;
02273                             Datum       pval;
02274 
02275                             Assert(prm->ptype == param->paramtype);
02276                             get_typlenbyval(param->paramtype,
02277                                             &typLen, &typByVal);
02278                             if (prm->isnull || typByVal)
02279                                 pval = prm->value;
02280                             else
02281                                 pval = datumCopy(prm->value, typByVal, typLen);
02282                             return (Node *) makeConst(param->paramtype,
02283                                                       param->paramtypmod,
02284                                                       param->paramcollid,
02285                                                       (int) typLen,
02286                                                       pval,
02287                                                       prm->isnull,
02288                                                       typByVal);
02289                         }
02290                     }
02291                 }
02292 
02293                 /*
02294                  * Not replaceable, so just copy the Param (no need to
02295                  * recurse)
02296                  */
02297                 return (Node *) copyObject(param);
02298             }
02299         case T_FuncExpr:
02300             {
02301                 FuncExpr   *expr = (FuncExpr *) node;
02302                 List       *args = expr->args;
02303                 Expr       *simple;
02304                 FuncExpr   *newexpr;
02305 
02306                 /*
02307                  * Code for op/func reduction is pretty bulky, so split it out
02308                  * as a separate function.  Note: exprTypmod normally returns
02309                  * -1 for a FuncExpr, but not when the node is recognizably a
02310                  * length coercion; we want to preserve the typmod in the
02311                  * eventual Const if so.
02312                  */
02313                 simple = simplify_function(expr->funcid,
02314                                            expr->funcresulttype,
02315                                            exprTypmod(node),
02316                                            expr->funccollid,
02317                                            expr->inputcollid,
02318                                            &args,
02319                                            expr->funcvariadic,
02320                                            true,
02321                                            true,
02322                                            context);
02323                 if (simple)     /* successfully simplified it */
02324                     return (Node *) simple;
02325 
02326                 /*
02327                  * The expression cannot be simplified any further, so build
02328                  * and return a replacement FuncExpr node using the
02329                  * possibly-simplified arguments.  Note that we have also
02330                  * converted the argument list to positional notation.
02331                  */
02332                 newexpr = makeNode(FuncExpr);
02333                 newexpr->funcid = expr->funcid;
02334                 newexpr->funcresulttype = expr->funcresulttype;
02335                 newexpr->funcretset = expr->funcretset;
02336                 newexpr->funcvariadic = expr->funcvariadic;
02337                 newexpr->funcformat = expr->funcformat;
02338                 newexpr->funccollid = expr->funccollid;
02339                 newexpr->inputcollid = expr->inputcollid;
02340                 newexpr->args = args;
02341                 newexpr->location = expr->location;
02342                 return (Node *) newexpr;
02343             }
02344         case T_OpExpr:
02345             {
02346                 OpExpr     *expr = (OpExpr *) node;
02347                 List       *args = expr->args;
02348                 Expr       *simple;
02349                 OpExpr     *newexpr;
02350 
02351                 /*
02352                  * Need to get OID of underlying function.  Okay to scribble
02353                  * on input to this extent.
02354                  */
02355                 set_opfuncid(expr);
02356 
02357                 /*
02358                  * Code for op/func reduction is pretty bulky, so split it out
02359                  * as a separate function.
02360                  */
02361                 simple = simplify_function(expr->opfuncid,
02362                                            expr->opresulttype, -1,
02363                                            expr->opcollid,
02364                                            expr->inputcollid,
02365                                            &args,
02366                                            false,
02367                                            true,
02368                                            true,
02369                                            context);
02370                 if (simple)     /* successfully simplified it */
02371                     return (Node *) simple;
02372 
02373                 /*
02374                  * If the operator is boolean equality or inequality, we know
02375                  * how to simplify cases involving one constant and one
02376                  * non-constant argument.
02377                  */
02378                 if (expr->opno == BooleanEqualOperator ||
02379                     expr->opno == BooleanNotEqualOperator)
02380                 {
02381                     simple = (Expr *) simplify_boolean_equality(expr->opno,
02382                                                                 args);
02383                     if (simple) /* successfully simplified it */
02384                         return (Node *) simple;
02385                 }
02386 
02387                 /*
02388                  * The expression cannot be simplified any further, so build
02389                  * and return a replacement OpExpr node using the
02390                  * possibly-simplified arguments.
02391                  */
02392                 newexpr = makeNode(OpExpr);
02393                 newexpr->opno = expr->opno;
02394                 newexpr->opfuncid = expr->opfuncid;
02395                 newexpr->opresulttype = expr->opresulttype;
02396                 newexpr->opretset = expr->opretset;
02397                 newexpr->opcollid = expr->opcollid;
02398                 newexpr->inputcollid = expr->inputcollid;
02399                 newexpr->args = args;
02400                 newexpr->location = expr->location;
02401                 return (Node *) newexpr;
02402             }
02403         case T_DistinctExpr:
02404             {
02405                 DistinctExpr *expr = (DistinctExpr *) node;
02406                 List       *args;
02407                 ListCell   *arg;
02408                 bool        has_null_input = false;
02409                 bool        all_null_input = true;
02410                 bool        has_nonconst_input = false;
02411                 Expr       *simple;
02412                 DistinctExpr *newexpr;
02413 
02414                 /*
02415                  * Reduce constants in the DistinctExpr's arguments.  We know
02416                  * args is either NIL or a List node, so we can call
02417                  * expression_tree_mutator directly rather than recursing to
02418                  * self.
02419                  */
02420                 args = (List *) expression_tree_mutator((Node *) expr->args,
02421                                               eval_const_expressions_mutator,
02422                                                         (void *) context);
02423 
02424                 /*
02425                  * We must do our own check for NULLs because DistinctExpr has
02426                  * different results for NULL input than the underlying
02427                  * operator does.
02428                  */
02429                 foreach(arg, args)
02430                 {
02431                     if (IsA(lfirst(arg), Const))
02432                     {
02433                         has_null_input |= ((Const *) lfirst(arg))->constisnull;
02434                         all_null_input &= ((Const *) lfirst(arg))->constisnull;
02435                     }
02436                     else
02437                         has_nonconst_input = true;
02438                 }
02439 
02440                 /* all constants? then can optimize this out */
02441                 if (!has_nonconst_input)
02442                 {
02443                     /* all nulls? then not distinct */
02444                     if (all_null_input)
02445                         return makeBoolConst(false, false);
02446 
02447                     /* one null? then distinct */
02448                     if (has_null_input)
02449                         return makeBoolConst(true, false);
02450 
02451                     /* otherwise try to evaluate the '=' operator */
02452                     /* (NOT okay to try to inline it, though!) */
02453 
02454                     /*
02455                      * Need to get OID of underlying function.  Okay to
02456                      * scribble on input to this extent.
02457                      */
02458                     set_opfuncid((OpExpr *) expr);      /* rely on struct
02459                                                          * equivalence */
02460 
02461                     /*
02462                      * Code for op/func reduction is pretty bulky, so split it
02463                      * out as a separate function.
02464                      */
02465                     simple = simplify_function(expr->opfuncid,
02466                                                expr->opresulttype, -1,
02467                                                expr->opcollid,
02468                                                expr->inputcollid,
02469                                                &args,
02470                                                false,
02471                                                false,
02472                                                false,
02473                                                context);
02474                     if (simple) /* successfully simplified it */
02475                     {
02476                         /*
02477                          * Since the underlying operator is "=", must negate
02478                          * its result
02479                          */
02480                         Const      *csimple = (Const *) simple;
02481 
02482                         Assert(IsA(csimple, Const));
02483                         csimple->constvalue =
02484                             BoolGetDatum(!DatumGetBool(csimple->constvalue));
02485                         return (Node *) csimple;
02486                     }
02487                 }
02488 
02489                 /*
02490                  * The expression cannot be simplified any further, so build
02491                  * and return a replacement DistinctExpr node using the
02492                  * possibly-simplified arguments.
02493                  */
02494                 newexpr = makeNode(DistinctExpr);
02495                 newexpr->opno = expr->opno;
02496                 newexpr->opfuncid = expr->opfuncid;
02497                 newexpr->opresulttype = expr->opresulttype;
02498                 newexpr->opretset = expr->opretset;
02499                 newexpr->opcollid = expr->opcollid;
02500                 newexpr->inputcollid = expr->inputcollid;
02501                 newexpr->args = args;
02502                 newexpr->location = expr->location;
02503                 return (Node *) newexpr;
02504             }
02505         case T_BoolExpr:
02506             {
02507                 BoolExpr   *expr = (BoolExpr *) node;
02508 
02509                 switch (expr->boolop)
02510                 {
02511                     case OR_EXPR:
02512                         {
02513                             List       *newargs;
02514                             bool        haveNull = false;
02515                             bool        forceTrue = false;
02516 
02517                             newargs = simplify_or_arguments(expr->args,
02518                                                             context,
02519                                                             &haveNull,
02520                                                             &forceTrue);
02521                             if (forceTrue)
02522                                 return makeBoolConst(true, false);
02523                             if (haveNull)
02524                                 newargs = lappend(newargs,
02525                                                   makeBoolConst(false, true));
02526                             /* If all the inputs are FALSE, result is FALSE */
02527                             if (newargs == NIL)
02528                                 return makeBoolConst(false, false);
02529 
02530                             /*
02531                              * If only one nonconst-or-NULL input, it's the
02532                              * result
02533                              */
02534                             if (list_length(newargs) == 1)
02535                                 return (Node *) linitial(newargs);
02536                             /* Else we still need an OR node */
02537                             return (Node *) make_orclause(newargs);
02538                         }
02539                     case AND_EXPR:
02540                         {
02541                             List       *newargs;
02542                             bool        haveNull = false;
02543                             bool        forceFalse = false;
02544 
02545                             newargs = simplify_and_arguments(expr->args,
02546                                                              context,
02547                                                              &haveNull,
02548                                                              &forceFalse);
02549                             if (forceFalse)
02550                                 return makeBoolConst(false, false);
02551                             if (haveNull)
02552                                 newargs = lappend(newargs,
02553                                                   makeBoolConst(false, true));
02554                             /* If all the inputs are TRUE, result is TRUE */
02555                             if (newargs == NIL)
02556                                 return makeBoolConst(true, false);
02557 
02558                             /*
02559                              * If only one nonconst-or-NULL input, it's the
02560                              * result
02561                              */
02562                             if (list_length(newargs) == 1)
02563                                 return (Node *) linitial(newargs);
02564                             /* Else we still need an AND node */
02565                             return (Node *) make_andclause(newargs);
02566                         }
02567                     case NOT_EXPR:
02568                         {
02569                             Node       *arg;
02570 
02571                             Assert(list_length(expr->args) == 1);
02572                             arg = eval_const_expressions_mutator(linitial(expr->args),
02573                                                                  context);
02574 
02575                             /*
02576                              * Use negate_clause() to see if we can simplify
02577                              * away the NOT.
02578                              */
02579                             return negate_clause(arg);
02580                         }
02581                     default:
02582                         elog(ERROR, "unrecognized boolop: %d",
02583                              (int) expr->boolop);
02584                         break;
02585                 }
02586                 break;
02587             }
02588         case T_SubPlan:
02589         case T_AlternativeSubPlan:
02590 
02591             /*
02592              * Return a SubPlan unchanged --- too late to do anything with it.
02593              *
02594              * XXX should we ereport() here instead?  Probably this routine
02595              * should never be invoked after SubPlan creation.
02596              */
02597             return node;
02598         case T_RelabelType:
02599             {
02600                 /*
02601                  * If we can simplify the input to a constant, then we don't
02602                  * need the RelabelType node anymore: just change the type
02603                  * field of the Const node.  Otherwise, must copy the
02604                  * RelabelType node.
02605                  */
02606                 RelabelType *relabel = (RelabelType *) node;
02607                 Node       *arg;
02608 
02609                 arg = eval_const_expressions_mutator((Node *) relabel->arg,
02610                                                      context);
02611 
02612                 /*
02613                  * If we find stacked RelabelTypes (eg, from foo :: int ::
02614                  * oid) we can discard all but the top one.
02615                  */
02616                 while (arg && IsA(arg, RelabelType))
02617                     arg = (Node *) ((RelabelType *) arg)->arg;
02618 
02619                 if (arg && IsA(arg, Const))
02620                 {
02621                     Const      *con = (Const *) arg;
02622 
02623                     con->consttype = relabel->resulttype;
02624                     con->consttypmod = relabel->resulttypmod;
02625                     con->constcollid = relabel->resultcollid;
02626                     return (Node *) con;
02627                 }
02628                 else
02629                 {
02630                     RelabelType *newrelabel = makeNode(RelabelType);
02631 
02632                     newrelabel->arg = (Expr *) arg;
02633                     newrelabel->resulttype = relabel->resulttype;
02634                     newrelabel->resulttypmod = relabel->resulttypmod;
02635                     newrelabel->resultcollid = relabel->resultcollid;
02636                     newrelabel->relabelformat = relabel->relabelformat;
02637                     newrelabel->location = relabel->location;
02638                     return (Node *) newrelabel;
02639                 }
02640             }
02641         case T_CoerceViaIO:
02642             {
02643                 CoerceViaIO *expr = (CoerceViaIO *) node;
02644                 List       *args;
02645                 Oid         outfunc;
02646                 bool        outtypisvarlena;
02647                 Oid         infunc;
02648                 Oid         intypioparam;
02649                 Expr       *simple;
02650                 CoerceViaIO *newexpr;
02651 
02652                 /* Make a List so we can use simplify_function */
02653                 args = list_make1(expr->arg);
02654 
02655                 /*
02656                  * CoerceViaIO represents calling the source type's output
02657                  * function then the result type's input function.  So, try to
02658                  * simplify it as though it were a stack of two such function
02659                  * calls.  First we need to know what the functions are.
02660                  *
02661                  * Note that the coercion functions are assumed not to care
02662                  * about input collation, so we just pass InvalidOid for that.
02663                  */
02664                 getTypeOutputInfo(exprType((Node *) expr->arg),
02665                                   &outfunc, &outtypisvarlena);
02666                 getTypeInputInfo(expr->resulttype,
02667                                  &infunc, &intypioparam);
02668 
02669                 simple = simplify_function(outfunc,
02670                                            CSTRINGOID, -1,
02671                                            InvalidOid,
02672                                            InvalidOid,
02673                                            &args,
02674                                            false,
02675                                            true,
02676                                            true,
02677                                            context);
02678                 if (simple)     /* successfully simplified output fn */
02679                 {
02680                     /*
02681                      * Input functions may want 1 to 3 arguments.  We always
02682                      * supply all three, trusting that nothing downstream will
02683                      * complain.
02684                      */
02685                     args = list_make3(simple,
02686                                       makeConst(OIDOID,
02687                                                 -1,
02688                                                 InvalidOid,
02689                                                 sizeof(Oid),
02690                                               ObjectIdGetDatum(intypioparam),
02691                                                 false,
02692                                                 true),
02693                                       makeConst(INT4OID,
02694                                                 -1,
02695                                                 InvalidOid,
02696                                                 sizeof(int32),
02697                                                 Int32GetDatum(-1),
02698                                                 false,
02699                                                 true));
02700 
02701                     simple = simplify_function(infunc,
02702                                                expr->resulttype, -1,
02703                                                expr->resultcollid,
02704                                                InvalidOid,
02705                                                &args,
02706                                                false,
02707                                                false,
02708                                                true,
02709                                                context);
02710                     if (simple) /* successfully simplified input fn */
02711                         return (Node *) simple;
02712                 }
02713 
02714                 /*
02715                  * The expression cannot be simplified any further, so build
02716                  * and return a replacement CoerceViaIO node using the
02717                  * possibly-simplified argument.
02718                  */
02719                 newexpr = makeNode(CoerceViaIO);
02720                 newexpr->arg = (Expr *) linitial(args);
02721                 newexpr->resulttype = expr->resulttype;
02722                 newexpr->resultcollid = expr->resultcollid;
02723                 newexpr->coerceformat = expr->coerceformat;
02724                 newexpr->location = expr->location;
02725                 return (Node *) newexpr;
02726             }
02727         case T_ArrayCoerceExpr:
02728             {
02729                 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
02730                 Expr       *arg;
02731                 ArrayCoerceExpr *newexpr;
02732 
02733                 /*
02734                  * Reduce constants in the ArrayCoerceExpr's argument, then
02735                  * build a new ArrayCoerceExpr.
02736                  */
02737                 arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg,
02738                                                               context);
02739 
02740                 newexpr = makeNode(ArrayCoerceExpr);
02741                 newexpr->arg = arg;
02742                 newexpr->elemfuncid = expr->elemfuncid;
02743                 newexpr->resulttype = expr->resulttype;
02744                 newexpr->resulttypmod = expr->resulttypmod;
02745                 newexpr->resultcollid = expr->resultcollid;
02746                 newexpr->isExplicit = expr->isExplicit;
02747                 newexpr->coerceformat = expr->coerceformat;
02748                 newexpr->location = expr->location;
02749 
02750                 /*
02751                  * If constant argument and it's a binary-coercible or
02752                  * immutable conversion, we can simplify it to a constant.
02753                  */
02754                 if (arg && IsA(arg, Const) &&
02755                     (!OidIsValid(newexpr->elemfuncid) ||
02756                 func_volatile(newexpr->elemfuncid) == PROVOLATILE_IMMUTABLE))
02757                     return (Node *) evaluate_expr((Expr *) newexpr,
02758                                                   newexpr->resulttype,
02759                                                   newexpr->resulttypmod,
02760                                                   newexpr->resultcollid);
02761 
02762                 /* Else we must return the partially-simplified node */
02763                 return (Node *) newexpr;
02764             }
02765         case T_CollateExpr:
02766             {
02767                 /*
02768                  * If we can simplify the input to a constant, then we don't
02769                  * need the CollateExpr node at all: just change the
02770                  * constcollid field of the Const node.  Otherwise, replace
02771                  * the CollateExpr with a RelabelType. (We do that so as to
02772                  * improve uniformity of expression representation and thus
02773                  * simplify comparison of expressions.)
02774                  */
02775                 CollateExpr *collate = (CollateExpr *) node;
02776                 Node       *arg;
02777 
02778                 arg = eval_const_expressions_mutator((Node *) collate->arg,
02779                                                      context);
02780 
02781                 if (arg && IsA(arg, Const))
02782                 {
02783                     Const      *con = (Const *) arg;
02784 
02785                     con->constcollid = collate->collOid;
02786                     return (Node *) con;
02787                 }
02788                 else if (collate->collOid == exprCollation(arg))
02789                 {
02790                     /* Don't need a RelabelType either... */
02791                     return arg;
02792                 }
02793                 else
02794                 {
02795                     RelabelType *relabel = makeNode(RelabelType);
02796 
02797                     relabel->resulttype = exprType(arg);
02798                     relabel->resulttypmod = exprTypmod(arg);
02799                     relabel->resultcollid = collate->collOid;
02800                     relabel->relabelformat = COERCE_IMPLICIT_CAST;
02801                     relabel->location = collate->location;
02802 
02803                     /* Don't create stacked RelabelTypes */
02804                     while (arg && IsA(arg, RelabelType))
02805                         arg = (Node *) ((RelabelType *) arg)->arg;
02806                     relabel->arg = (Expr *) arg;
02807 
02808                     return (Node *) relabel;
02809                 }
02810             }
02811         case T_CaseExpr:
02812             {
02813                 /*----------
02814                  * CASE expressions can be simplified if there are constant
02815                  * condition clauses:
02816                  *      FALSE (or NULL): drop the alternative
02817                  *      TRUE: drop all remaining alternatives
02818                  * If the first non-FALSE alternative is a constant TRUE,
02819                  * we can simplify the entire CASE to that alternative's
02820                  * expression.  If there are no non-FALSE alternatives,
02821                  * we simplify the entire CASE to the default result (ELSE).
02822                  *
02823                  * If we have a simple-form CASE with constant test
02824                  * expression, we substitute the constant value for contained
02825                  * CaseTestExpr placeholder nodes, so that we have the
02826                  * opportunity to reduce constant test conditions.  For
02827                  * example this allows
02828                  *      CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
02829                  * to reduce to 1 rather than drawing a divide-by-0 error.
02830                  * Note that when the test expression is constant, we don't
02831                  * have to include it in the resulting CASE; for example
02832                  *      CASE 0 WHEN x THEN y ELSE z END
02833                  * is transformed by the parser to
02834                  *      CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
02835                  * which we can simplify to
02836                  *      CASE WHEN 0 = x THEN y ELSE z END
02837                  * It is not necessary for the executor to evaluate the "arg"
02838                  * expression when executing the CASE, since any contained
02839                  * CaseTestExprs that might have referred to it will have been
02840                  * replaced by the constant.
02841                  *----------
02842                  */
02843                 CaseExpr   *caseexpr = (CaseExpr *) node;
02844                 CaseExpr   *newcase;
02845                 Node       *save_case_val;
02846                 Node       *newarg;
02847                 List       *newargs;
02848                 bool        const_true_cond;
02849                 Node       *defresult = NULL;
02850                 ListCell   *arg;
02851 
02852                 /* Simplify the test expression, if any */
02853                 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
02854                                                         context);
02855 
02856                 /* Set up for contained CaseTestExpr nodes */
02857                 save_case_val = context->case_val;
02858                 if (newarg && IsA(newarg, Const))
02859                 {
02860                     context->case_val = newarg;
02861                     newarg = NULL;      /* not needed anymore, see above */
02862                 }
02863                 else
02864                     context->case_val = NULL;
02865 
02866                 /* Simplify the WHEN clauses */
02867                 newargs = NIL;
02868                 const_true_cond = false;
02869                 foreach(arg, caseexpr->args)
02870                 {
02871                     CaseWhen   *oldcasewhen = (CaseWhen *) lfirst(arg);
02872                     Node       *casecond;
02873                     Node       *caseresult;
02874 
02875                     Assert(IsA(oldcasewhen, CaseWhen));
02876 
02877                     /* Simplify this alternative's test condition */
02878                     casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
02879                                                               context);
02880 
02881                     /*
02882                      * If the test condition is constant FALSE (or NULL), then
02883                      * drop this WHEN clause completely, without processing
02884                      * the result.
02885                      */
02886                     if (casecond && IsA(casecond, Const))
02887                     {
02888                         Const      *const_input = (Const *) casecond;
02889 
02890                         if (const_input->constisnull ||
02891                             !DatumGetBool(const_input->constvalue))
02892                             continue;   /* drop alternative with FALSE cond */
02893                         /* Else it's constant TRUE */
02894                         const_true_cond = true;
02895                     }
02896 
02897                     /* Simplify this alternative's result value */
02898                     caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
02899                                                                 context);
02900 
02901                     /* If non-constant test condition, emit a new WHEN node */
02902                     if (!const_true_cond)
02903                     {
02904                         CaseWhen   *newcasewhen = makeNode(CaseWhen);
02905 
02906                         newcasewhen->expr = (Expr *) casecond;
02907                         newcasewhen->result = (Expr *) caseresult;
02908                         newcasewhen->location = oldcasewhen->location;
02909                         newargs = lappend(newargs, newcasewhen);
02910                         continue;
02911                     }
02912 
02913                     /*
02914                      * Found a TRUE condition, so none of the remaining
02915                      * alternatives can be reached.  We treat the result as
02916                      * the default result.
02917                      */
02918                     defresult = caseresult;
02919                     break;
02920                 }
02921 
02922                 /* Simplify the default result, unless we replaced it above */
02923                 if (!const_true_cond)
02924                     defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
02925                                                                context);
02926 
02927                 context->case_val = save_case_val;
02928 
02929                 /*
02930                  * If no non-FALSE alternatives, CASE reduces to the default
02931                  * result
02932                  */
02933                 if (newargs == NIL)
02934                     return defresult;
02935                 /* Otherwise we need a new CASE node */
02936                 newcase = makeNode(CaseExpr);
02937                 newcase->casetype = caseexpr->casetype;
02938                 newcase->casecollid = caseexpr->casecollid;
02939                 newcase->arg = (Expr *) newarg;
02940                 newcase->args = newargs;
02941                 newcase->defresult = (Expr *) defresult;
02942                 newcase->location = caseexpr->location;
02943                 return (Node *) newcase;
02944             }
02945         case T_CaseTestExpr:
02946             {
02947                 /*
02948                  * If we know a constant test value for the current CASE
02949                  * construct, substitute it for the placeholder.  Else just
02950                  * return the placeholder as-is.
02951                  */
02952                 if (context->case_val)
02953                     return copyObject(context->case_val);
02954                 else
02955                     return copyObject(node);
02956             }
02957         case T_ArrayExpr:
02958             {
02959                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
02960                 ArrayExpr  *newarray;
02961                 bool        all_const = true;
02962                 List       *newelems;
02963                 ListCell   *element;
02964 
02965                 newelems = NIL;
02966                 foreach(element, arrayexpr->elements)
02967                 {
02968                     Node       *e;
02969 
02970                     e = eval_const_expressions_mutator((Node *) lfirst(element),
02971                                                        context);
02972                     if (!IsA(e, Const))
02973                         all_const = false;
02974                     newelems = lappend(newelems, e);
02975                 }
02976 
02977                 newarray = makeNode(ArrayExpr);
02978                 newarray->array_typeid = arrayexpr->array_typeid;
02979                 newarray->array_collid = arrayexpr->array_collid;
02980                 newarray->element_typeid = arrayexpr->element_typeid;
02981                 newarray->elements = newelems;
02982                 newarray->multidims = arrayexpr->multidims;
02983                 newarray->location = arrayexpr->location;
02984 
02985                 if (all_const)
02986                     return (Node *) evaluate_expr((Expr *) newarray,
02987                                                   newarray->array_typeid,
02988                                                   exprTypmod(node),
02989                                                   newarray->array_collid);
02990 
02991                 return (Node *) newarray;
02992             }
02993         case T_CoalesceExpr:
02994             {
02995                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
02996                 CoalesceExpr *newcoalesce;
02997                 List       *newargs;
02998                 ListCell   *arg;
02999 
03000                 newargs = NIL;
03001                 foreach(arg, coalesceexpr->args)
03002                 {
03003                     Node       *e;
03004 
03005                     e = eval_const_expressions_mutator((Node *) lfirst(arg),
03006                                                        context);
03007 
03008                     /*
03009                      * We can remove null constants from the list. For a
03010                      * non-null constant, if it has not been preceded by any
03011                      * other non-null-constant expressions then it is the
03012                      * result. Otherwise, it's the next argument, but we can
03013                      * drop following arguments since they will never be
03014                      * reached.
03015                      */
03016                     if (IsA(e, Const))
03017                     {
03018                         if (((Const *) e)->constisnull)
03019                             continue;   /* drop null constant */
03020                         if (newargs == NIL)
03021                             return e;   /* first expr */
03022                         newargs = lappend(newargs, e);
03023                         break;
03024                     }
03025                     newargs = lappend(newargs, e);
03026                 }
03027 
03028                 /*
03029                  * If all the arguments were constant null, the result is just
03030                  * null
03031                  */
03032                 if (newargs == NIL)
03033                     return (Node *) makeNullConst(coalesceexpr->coalescetype,
03034                                                   -1,
03035                                                coalesceexpr->coalescecollid);
03036 
03037                 newcoalesce = makeNode(CoalesceExpr);
03038                 newcoalesce->coalescetype = coalesceexpr->coalescetype;
03039                 newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
03040                 newcoalesce->args = newargs;
03041                 newcoalesce->location = coalesceexpr->location;
03042                 return (Node *) newcoalesce;
03043             }
03044         case T_FieldSelect:
03045             {
03046                 /*
03047                  * We can optimize field selection from a whole-row Var into a
03048                  * simple Var.  (This case won't be generated directly by the
03049                  * parser, because ParseComplexProjection short-circuits it.
03050                  * But it can arise while simplifying functions.)  Also, we
03051                  * can optimize field selection from a RowExpr construct.
03052                  *
03053                  * We must however check that the declared type of the field
03054                  * is still the same as when the FieldSelect was created ---
03055                  * this can change if someone did ALTER COLUMN TYPE on the
03056                  * rowtype.
03057                  */
03058                 FieldSelect *fselect = (FieldSelect *) node;
03059                 FieldSelect *newfselect;
03060                 Node       *arg;
03061 
03062                 arg = eval_const_expressions_mutator((Node *) fselect->arg,
03063                                                      context);
03064                 if (arg && IsA(arg, Var) &&
03065                     ((Var *) arg)->varattno == InvalidAttrNumber)
03066                 {
03067                     if (rowtype_field_matches(((Var *) arg)->vartype,
03068                                               fselect->fieldnum,
03069                                               fselect->resulttype,
03070                                               fselect->resulttypmod,
03071                                               fselect->resultcollid))
03072                         return (Node *) makeVar(((Var *) arg)->varno,
03073                                                 fselect->fieldnum,
03074                                                 fselect->resulttype,
03075                                                 fselect->resulttypmod,
03076                                                 fselect->resultcollid,
03077                                                 ((Var *) arg)->varlevelsup);
03078                 }
03079                 if (arg && IsA(arg, RowExpr))
03080                 {
03081                     RowExpr    *rowexpr = (RowExpr *) arg;
03082 
03083                     if (fselect->fieldnum > 0 &&
03084                         fselect->fieldnum <= list_length(rowexpr->args))
03085                     {
03086                         Node       *fld = (Node *) list_nth(rowexpr->args,
03087                                                       fselect->fieldnum - 1);
03088 
03089                         if (rowtype_field_matches(rowexpr->row_typeid,
03090                                                   fselect->fieldnum,
03091                                                   fselect->resulttype,
03092                                                   fselect->resulttypmod,
03093                                                   fselect->resultcollid) &&
03094                             fselect->resulttype == exprType(fld) &&
03095                             fselect->resulttypmod == exprTypmod(fld) &&
03096                             fselect->resultcollid == exprCollation(fld))
03097                             return fld;
03098                     }
03099                 }
03100                 newfselect = makeNode(FieldSelect);
03101                 newfselect->arg = (Expr *) arg;
03102                 newfselect->fieldnum = fselect->fieldnum;
03103                 newfselect->resulttype = fselect->resulttype;
03104                 newfselect->resulttypmod = fselect->resulttypmod;
03105                 newfselect->resultcollid = fselect->resultcollid;
03106                 return (Node *) newfselect;
03107             }
03108         case T_NullTest:
03109             {
03110                 NullTest   *ntest = (NullTest *) node;
03111                 NullTest   *newntest;
03112                 Node       *arg;
03113 
03114                 arg = eval_const_expressions_mutator((Node *) ntest->arg,
03115                                                      context);
03116                 if (arg && IsA(arg, RowExpr))
03117                 {
03118                     /*
03119                      * We break ROW(...) IS [NOT] NULL into separate tests on
03120                      * its component fields.  This form is usually more
03121                      * efficient to evaluate, as well as being more amenable
03122                      * to optimization.
03123                      */
03124                     RowExpr    *rarg = (RowExpr *) arg;
03125                     List       *newargs = NIL;
03126                     ListCell   *l;
03127 
03128                     Assert(ntest->argisrow);
03129 
03130                     foreach(l, rarg->args)
03131                     {
03132                         Node       *relem = (Node *) lfirst(l);
03133 
03134                         /*
03135                          * A constant field refutes the whole NullTest if it's
03136                          * of the wrong nullness; else we can discard it.
03137                          */
03138                         if (relem && IsA(relem, Const))
03139                         {
03140                             Const      *carg = (Const *) relem;
03141 
03142                             if (carg->constisnull ?
03143                                 (ntest->nulltesttype == IS_NOT_NULL) :
03144                                 (ntest->nulltesttype == IS_NULL))
03145                                 return makeBoolConst(false, false);
03146                             continue;
03147                         }
03148                         newntest = makeNode(NullTest);
03149                         newntest->arg = (Expr *) relem;
03150                         newntest->nulltesttype = ntest->nulltesttype;
03151                         newntest->argisrow = type_is_rowtype(exprType(relem));
03152                         newargs = lappend(newargs, newntest);
03153                     }
03154                     /* If all the inputs were constants, result is TRUE */
03155                     if (newargs == NIL)
03156                         return makeBoolConst(true, false);
03157                     /* If only one nonconst input, it's the result */
03158                     if (list_length(newargs) == 1)
03159                         return (Node *) linitial(newargs);
03160                     /* Else we need an AND node */
03161                     return (Node *) make_andclause(newargs);
03162                 }
03163                 if (!ntest->argisrow && arg && IsA(arg, Const))
03164                 {
03165                     Const      *carg = (Const *) arg;
03166                     bool        result;
03167 
03168                     switch (ntest->nulltesttype)
03169                     {
03170                         case IS_NULL:
03171                             result = carg->constisnull;
03172                             break;
03173                         case IS_NOT_NULL:
03174                             result = !carg->constisnull;
03175                             break;
03176                         default:
03177                             elog(ERROR, "unrecognized nulltesttype: %d",
03178                                  (int) ntest->nulltesttype);
03179                             result = false;     /* keep compiler quiet */
03180                             break;
03181                     }
03182 
03183                     return makeBoolConst(result, false);
03184                 }
03185 
03186                 newntest = makeNode(NullTest);
03187                 newntest->arg = (Expr *) arg;
03188                 newntest->nulltesttype = ntest->nulltesttype;
03189                 newntest->argisrow = ntest->argisrow;
03190                 return (Node *) newntest;
03191             }
03192         case T_BooleanTest:
03193             {
03194                 BooleanTest *btest = (BooleanTest *) node;
03195                 BooleanTest *newbtest;
03196                 Node       *arg;
03197 
03198                 arg = eval_const_expressions_mutator((Node *) btest->arg,
03199                                                      context);
03200                 if (arg && IsA(arg, Const))
03201                 {
03202                     Const      *carg = (Const *) arg;
03203                     bool        result;
03204 
03205                     switch (btest->booltesttype)
03206                     {
03207                         case IS_TRUE:
03208                             result = (!carg->constisnull &&
03209                                       DatumGetBool(carg->constvalue));
03210                             break;
03211                         case IS_NOT_TRUE:
03212                             result = (carg->constisnull ||
03213                                       !DatumGetBool(carg->constvalue));
03214                             break;
03215                         case IS_FALSE:
03216                             result = (!carg->constisnull &&
03217                                       !DatumGetBool(carg->constvalue));
03218                             break;
03219                         case IS_NOT_FALSE:
03220                             result = (carg->constisnull ||
03221                                       DatumGetBool(carg->constvalue));
03222                             break;
03223                         case IS_UNKNOWN:
03224                             result = carg->constisnull;
03225                             break;
03226                         case IS_NOT_UNKNOWN:
03227                             result = !carg->constisnull;
03228                             break;
03229                         default:
03230                             elog(ERROR, "unrecognized booltesttype: %d",
03231                                  (int) btest->booltesttype);
03232                             result = false;     /* keep compiler quiet */
03233                             break;
03234                     }
03235 
03236                     return makeBoolConst(result, false);
03237                 }
03238 
03239                 newbtest = makeNode(BooleanTest);
03240                 newbtest->arg = (Expr *) arg;
03241                 newbtest->booltesttype = btest->booltesttype;
03242                 return (Node *) newbtest;
03243             }
03244         case T_PlaceHolderVar:
03245 
03246             /*
03247              * In estimation mode, just strip the PlaceHolderVar node
03248              * altogether; this amounts to estimating that the contained value
03249              * won't be forced to null by an outer join.  In regular mode we
03250              * just use the default behavior (ie, simplify the expression but
03251              * leave the PlaceHolderVar node intact).
03252              */
03253             if (context->estimate)
03254             {
03255                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
03256 
03257                 return eval_const_expressions_mutator((Node *) phv->phexpr,
03258                                                       context);
03259             }
03260             break;
03261         default:
03262             break;
03263     }
03264 
03265     /*
03266      * For any node type not handled above, we recurse using
03267      * expression_tree_mutator, which will copy the node unchanged but try to
03268      * simplify its arguments (if any) using this routine. For example: we
03269      * cannot eliminate an ArrayRef node, but we might be able to simplify
03270      * constant expressions in its subscripts.
03271      */
03272     return expression_tree_mutator(node, eval_const_expressions_mutator,
03273                                    (void *) context);
03274 }
03275 
03276 /*
03277  * Subroutine for eval_const_expressions: process arguments of an OR clause
03278  *
03279  * This includes flattening of nested ORs as well as recursion to
03280  * eval_const_expressions to simplify the OR arguments.
03281  *
03282  * After simplification, OR arguments are handled as follows:
03283  *      non constant: keep
03284  *      FALSE: drop (does not affect result)
03285  *      TRUE: force result to TRUE
03286  *      NULL: keep only one
03287  * We must keep one NULL input because ExecEvalOr returns NULL when no input
03288  * is TRUE and at least one is NULL.  We don't actually include the NULL
03289  * here, that's supposed to be done by the caller.
03290  *
03291  * The output arguments *haveNull and *forceTrue must be initialized FALSE
03292  * by the caller.  They will be set TRUE if a null constant or true constant,
03293  * respectively, is detected anywhere in the argument list.
03294  */
03295 static List *
03296 simplify_or_arguments(List *args,
03297                       eval_const_expressions_context *context,
03298                       bool *haveNull, bool *forceTrue)
03299 {
03300     List       *newargs = NIL;
03301     List       *unprocessed_args;
03302 
03303     /*
03304      * Since the parser considers OR to be a binary operator, long OR lists
03305      * become deeply nested expressions.  We must flatten these into long
03306      * argument lists of a single OR operator.  To avoid blowing out the stack
03307      * with recursion of eval_const_expressions, we resort to some tenseness
03308      * here: we keep a list of not-yet-processed inputs, and handle flattening
03309      * of nested ORs by prepending to the to-do list instead of recursing.
03310      */
03311     unprocessed_args = list_copy(args);
03312     while (unprocessed_args)
03313     {
03314         Node       *arg = (Node *) linitial(unprocessed_args);
03315 
03316         unprocessed_args = list_delete_first(unprocessed_args);
03317 
03318         /* flatten nested ORs as per above comment */
03319         if (or_clause(arg))
03320         {
03321             List       *subargs = list_copy(((BoolExpr *) arg)->args);
03322 
03323             /* overly tense code to avoid leaking unused list header */
03324             if (!unprocessed_args)
03325                 unprocessed_args = subargs;
03326             else
03327             {
03328                 List       *oldhdr = unprocessed_args;
03329 
03330                 unprocessed_args = list_concat(subargs, unprocessed_args);
03331                 pfree(oldhdr);
03332             }
03333             continue;
03334         }
03335 
03336         /* If it's not an OR, simplify it */
03337         arg = eval_const_expressions_mutator(arg, context);
03338 
03339         /*
03340          * It is unlikely but not impossible for simplification of a non-OR
03341          * clause to produce an OR.  Recheck, but don't be too tense about it
03342          * since it's not a mainstream case. In particular we don't worry
03343          * about const-simplifying the input twice.
03344          */
03345         if (or_clause(arg))
03346         {
03347             List       *subargs = list_copy(((BoolExpr *) arg)->args);
03348 
03349             unprocessed_args = list_concat(subargs, unprocessed_args);
03350             continue;
03351         }
03352 
03353         /*
03354          * OK, we have a const-simplified non-OR argument.  Process it per
03355          * comments above.
03356          */
03357         if (IsA(arg, Const))
03358         {
03359             Const      *const_input = (Const *) arg;
03360 
03361             if (const_input->constisnull)
03362                 *haveNull = true;
03363             else if (DatumGetBool(const_input->constvalue))
03364             {
03365                 *forceTrue = true;
03366 
03367                 /*
03368                  * Once we detect a TRUE result we can just exit the loop
03369                  * immediately.  However, if we ever add a notion of
03370                  * non-removable functions, we'd need to keep scanning.
03371                  */
03372                 return NIL;
03373             }
03374             /* otherwise, we can drop the constant-false input */
03375             continue;
03376         }
03377 
03378         /* else emit the simplified arg into the result list */
03379         newargs = lappend(newargs, arg);
03380     }
03381 
03382     return newargs;
03383 }
03384 
03385 /*
03386  * Subroutine for eval_const_expressions: process arguments of an AND clause
03387  *
03388  * This includes flattening of nested ANDs as well as recursion to
03389  * eval_const_expressions to simplify the AND arguments.
03390  *
03391  * After simplification, AND arguments are handled as follows:
03392  *      non constant: keep
03393  *      TRUE: drop (does not affect result)
03394  *      FALSE: force result to FALSE
03395  *      NULL: keep only one
03396  * We must keep one NULL input because ExecEvalAnd returns NULL when no input
03397  * is FALSE and at least one is NULL.  We don't actually include the NULL
03398  * here, that's supposed to be done by the caller.
03399  *
03400  * The output arguments *haveNull and *forceFalse must be initialized FALSE
03401  * by the caller.  They will be set TRUE if a null constant or false constant,
03402  * respectively, is detected anywhere in the argument list.
03403  */
03404 static List *
03405 simplify_and_arguments(List *args,
03406                        eval_const_expressions_context *context,
03407                        bool *haveNull, bool *forceFalse)
03408 {
03409     List       *newargs = NIL;
03410     List       *unprocessed_args;
03411 
03412     /* See comments in simplify_or_arguments */
03413     unprocessed_args = list_copy(args);
03414     while (unprocessed_args)
03415     {
03416         Node       *arg = (Node *) linitial(unprocessed_args);
03417 
03418         unprocessed_args = list_delete_first(unprocessed_args);
03419 
03420         /* flatten nested ANDs as per above comment */
03421         if (and_clause(arg))
03422         {
03423             List       *subargs = list_copy(((BoolExpr *) arg)->args);
03424 
03425             /* overly tense code to avoid leaking unused list header */
03426             if (!unprocessed_args)
03427                 unprocessed_args = subargs;
03428             else
03429             {
03430                 List       *oldhdr = unprocessed_args;
03431 
03432                 unprocessed_args = list_concat(subargs, unprocessed_args);
03433                 pfree(oldhdr);
03434             }
03435             continue;
03436         }
03437 
03438         /* If it's not an AND, simplify it */
03439         arg = eval_const_expressions_mutator(arg, context);
03440 
03441         /*
03442          * It is unlikely but not impossible for simplification of a non-AND
03443          * clause to produce an AND.  Recheck, but don't be too tense about it
03444          * since it's not a mainstream case. In particular we don't worry
03445          * about const-simplifying the input twice.
03446          */
03447         if (and_clause(arg))
03448         {
03449             List       *subargs = list_copy(((BoolExpr *) arg)->args);
03450 
03451             unprocessed_args = list_concat(subargs, unprocessed_args);
03452             continue;
03453         }
03454 
03455         /*
03456          * OK, we have a const-simplified non-AND argument.  Process it per
03457          * comments above.
03458          */
03459         if (IsA(arg, Const))
03460         {
03461             Const      *const_input = (Const *) arg;
03462 
03463             if (const_input->constisnull)
03464                 *haveNull = true;
03465             else if (!DatumGetBool(const_input->constvalue))
03466             {
03467                 *forceFalse = true;
03468 
03469                 /*
03470                  * Once we detect a FALSE result we can just exit the loop
03471                  * immediately.  However, if we ever add a notion of
03472                  * non-removable functions, we'd need to keep scanning.
03473                  */
03474                 return NIL;
03475             }
03476             /* otherwise, we can drop the constant-true input */
03477             continue;
03478         }
03479 
03480         /* else emit the simplified arg into the result list */
03481         newargs = lappend(newargs, arg);
03482     }
03483 
03484     return newargs;
03485 }
03486 
03487 /*
03488  * Subroutine for eval_const_expressions: try to simplify boolean equality
03489  * or inequality condition
03490  *
03491  * Inputs are the operator OID and the simplified arguments to the operator.
03492  * Returns a simplified expression if successful, or NULL if cannot
03493  * simplify the expression.
03494  *
03495  * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x",
03496  * or similarly "x <> true" to "NOT x" and "x <> false" to "x".
03497  * This is only marginally useful in itself, but doing it in constant folding
03498  * ensures that we will recognize these forms as being equivalent in, for
03499  * example, partial index matching.
03500  *
03501  * We come here only if simplify_function has failed; therefore we cannot
03502  * see two constant inputs, nor a constant-NULL input.
03503  */
03504 static Node *
03505 simplify_boolean_equality(Oid opno, List *args)
03506 {
03507     Node       *leftop;
03508     Node       *rightop;
03509 
03510     Assert(list_length(args) == 2);
03511     leftop = linitial(args);
03512     rightop = lsecond(args);
03513     if (leftop && IsA(leftop, Const))
03514     {
03515         Assert(!((Const *) leftop)->constisnull);
03516         if (opno == BooleanEqualOperator)
03517         {
03518             if (DatumGetBool(((Const *) leftop)->constvalue))
03519                 return rightop; /* true = foo */
03520             else
03521                 return negate_clause(rightop);  /* false = foo */
03522         }
03523         else
03524         {
03525             if (DatumGetBool(((Const *) leftop)->constvalue))
03526                 return negate_clause(rightop);  /* true <> foo */
03527             else
03528                 return rightop; /* false <> foo */
03529         }
03530     }
03531     if (rightop && IsA(rightop, Const))
03532     {
03533         Assert(!((Const *) rightop)->constisnull);
03534         if (opno == BooleanEqualOperator)
03535         {
03536             if (DatumGetBool(((Const *) rightop)->constvalue))
03537                 return leftop;  /* foo = true */
03538             else
03539                 return negate_clause(leftop);   /* foo = false */
03540         }
03541         else
03542         {
03543             if (DatumGetBool(((Const *) rightop)->constvalue))
03544                 return negate_clause(leftop);   /* foo <> true */
03545             else
03546                 return leftop;  /* foo <> false */
03547         }
03548     }
03549     return NULL;
03550 }
03551 
03552 /*
03553  * Subroutine for eval_const_expressions: try to simplify a function call
03554  * (which might originally have been an operator; we don't care)
03555  *
03556  * Inputs are the function OID, actual result type OID (which is needed for
03557  * polymorphic functions), result typmod, result collation, the input
03558  * collation to use for the function, the original argument list (not
03559  * const-simplified yet, unless process_args is false), and some flags;
03560  * also the context data for eval_const_expressions.
03561  *
03562  * Returns a simplified expression if successful, or NULL if cannot
03563  * simplify the function call.
03564  *
03565  * This function is also responsible for converting named-notation argument
03566  * lists into positional notation and/or adding any needed default argument
03567  * expressions; which is a bit grotty, but it avoids extra fetches of the
03568  * function's pg_proc tuple.  For this reason, the args list is
03569  * pass-by-reference.  Conversion and const-simplification of the args list
03570  * will be done even if simplification of the function call itself is not
03571  * possible.
03572  */
03573 static Expr *
03574 simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
03575                   Oid result_collid, Oid input_collid, List **args_p,
03576                   bool funcvariadic, bool process_args, bool allow_non_const,
03577                   eval_const_expressions_context *context)
03578 {
03579     List       *args = *args_p;
03580     HeapTuple   func_tuple;
03581     Form_pg_proc func_form;
03582     Expr       *newexpr;
03583 
03584     /*
03585      * We have three strategies for simplification: execute the function to
03586      * deliver a constant result, use a transform function to generate a
03587      * substitute node tree, or expand in-line the body of the function
03588      * definition (which only works for simple SQL-language functions, but
03589      * that is a common case).  Each case needs access to the function's
03590      * pg_proc tuple, so fetch it just once.
03591      *
03592      * Note: the allow_non_const flag suppresses both the second and third
03593      * strategies; so if !allow_non_const, simplify_function can only return a
03594      * Const or NULL.  Argument-list rewriting happens anyway, though.
03595      */
03596     func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
03597     if (!HeapTupleIsValid(func_tuple))
03598         elog(ERROR, "cache lookup failed for function %u", funcid);
03599     func_form = (Form_pg_proc) GETSTRUCT(func_tuple);
03600 
03601     /*
03602      * Process the function arguments, unless the caller did it already.
03603      *
03604      * Here we must deal with named or defaulted arguments, and then
03605      * recursively apply eval_const_expressions to the whole argument list.
03606      */
03607     if (process_args)
03608     {
03609         args = expand_function_arguments(args, result_type, func_tuple);
03610         args = (List *) expression_tree_mutator((Node *) args,
03611                                               eval_const_expressions_mutator,
03612                                                 (void *) context);
03613         /* Argument processing done, give it back to the caller */
03614         *args_p = args;
03615     }
03616 
03617     /* Now attempt simplification of the function call proper. */
03618 
03619     newexpr = evaluate_function(funcid, result_type, result_typmod,
03620                                 result_collid, input_collid,
03621                                 args, funcvariadic,
03622                                 func_tuple, context);
03623 
03624     if (!newexpr && allow_non_const && OidIsValid(func_form->protransform))
03625     {
03626         /*
03627          * Build a dummy FuncExpr node containing the simplified arg list.  We
03628          * use this approach to present a uniform interface to the transform
03629          * function regardless of how the function is actually being invoked.
03630          */
03631         FuncExpr    fexpr;
03632 
03633         fexpr.xpr.type = T_FuncExpr;
03634         fexpr.funcid = funcid;
03635         fexpr.funcresulttype = result_type;
03636         fexpr.funcretset = func_form->proretset;
03637         fexpr.funcvariadic = funcvariadic;
03638         fexpr.funcformat = COERCE_EXPLICIT_CALL;
03639         fexpr.funccollid = result_collid;
03640         fexpr.inputcollid = input_collid;
03641         fexpr.args = args;
03642         fexpr.location = -1;
03643 
03644         newexpr = (Expr *)
03645             DatumGetPointer(OidFunctionCall1(func_form->protransform,
03646                                              PointerGetDatum(&fexpr)));
03647     }
03648 
03649     if (!newexpr && allow_non_const)
03650         newexpr = inline_function(funcid, result_type, result_collid,
03651                                   input_collid, args, funcvariadic,
03652                                   func_tuple, context);
03653 
03654     ReleaseSysCache(func_tuple);
03655 
03656     return newexpr;
03657 }
03658 
03659 /*
03660  * expand_function_arguments: convert named-notation args to positional args
03661  * and/or insert default args, as needed
03662  *
03663  * If we need to change anything, the input argument list is copied, not
03664  * modified.
03665  *
03666  * Note: this gets applied to operator argument lists too, even though the
03667  * cases it handles should never occur there.  This should be OK since it
03668  * will fall through very quickly if there's nothing to do.
03669  */
03670 static List *
03671 expand_function_arguments(List *args, Oid result_type, HeapTuple func_tuple)
03672 {
03673     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
03674     bool        has_named_args = false;
03675     ListCell   *lc;
03676 
03677     /* Do we have any named arguments? */
03678     foreach(lc, args)
03679     {
03680         Node       *arg = (Node *) lfirst(lc);
03681 
03682         if (IsA(arg, NamedArgExpr))
03683         {
03684             has_named_args = true;
03685             break;
03686         }
03687     }
03688 
03689     /* If so, we must apply reorder_function_arguments */
03690     if (has_named_args)
03691     {
03692         args = reorder_function_arguments(args, func_tuple);
03693         /* Recheck argument types and add casts if needed */
03694         recheck_cast_function_args(args, result_type, func_tuple);
03695     }
03696     else if (list_length(args) < funcform->pronargs)
03697     {
03698         /* No named args, but we seem to be short some defaults */
03699         args = add_function_defaults(args, func_tuple);
03700         /* Recheck argument types and add casts if needed */
03701         recheck_cast_function_args(args, result_type, func_tuple);
03702     }
03703 
03704     return args;
03705 }
03706 
03707 /*
03708  * reorder_function_arguments: convert named-notation args to positional args
03709  *
03710  * This function also inserts default argument values as needed, since it's
03711  * impossible to form a truly valid positional call without that.
03712  */
03713 static List *
03714 reorder_function_arguments(List *args, HeapTuple func_tuple)
03715 {
03716     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
03717     int         pronargs = funcform->pronargs;
03718     int         nargsprovided = list_length(args);
03719     Node       *argarray[FUNC_MAX_ARGS];
03720     ListCell   *lc;
03721     int         i;
03722 
03723     Assert(nargsprovided <= pronargs);
03724     if (pronargs > FUNC_MAX_ARGS)
03725         elog(ERROR, "too many function arguments");
03726     MemSet(argarray, 0, pronargs * sizeof(Node *));
03727 
03728     /* Deconstruct the argument list into an array indexed by argnumber */
03729     i = 0;
03730     foreach(lc, args)
03731     {
03732         Node       *arg = (Node *) lfirst(lc);
03733 
03734         if (!IsA(arg, NamedArgExpr))
03735         {
03736             /* positional argument, assumed to precede all named args */
03737             Assert(argarray[i] == NULL);
03738             argarray[i++] = arg;
03739         }
03740         else
03741         {
03742             NamedArgExpr *na = (NamedArgExpr *) arg;
03743 
03744             Assert(argarray[na->argnumber] == NULL);
03745             argarray[na->argnumber] = (Node *) na->arg;
03746         }
03747     }
03748 
03749     /*
03750      * Fetch default expressions, if needed, and insert into array at proper
03751      * locations (they aren't necessarily consecutive or all used)
03752      */
03753     if (nargsprovided < pronargs)
03754     {
03755         List       *defaults = fetch_function_defaults(func_tuple);
03756 
03757         i = pronargs - funcform->pronargdefaults;
03758         foreach(lc, defaults)
03759         {
03760             if (argarray[i] == NULL)
03761                 argarray[i] = (Node *) lfirst(lc);
03762             i++;
03763         }
03764     }
03765 
03766     /* Now reconstruct the args list in proper order */
03767     args = NIL;
03768     for (i = 0; i < pronargs; i++)
03769     {
03770         Assert(argarray[i] != NULL);
03771         args = lappend(args, argarray[i]);
03772     }
03773 
03774     return args;
03775 }
03776 
03777 /*
03778  * add_function_defaults: add missing function arguments from its defaults
03779  *
03780  * This is used only when the argument list was positional to begin with,
03781  * and so we know we just need to add defaults at the end.
03782  */
03783 static List *
03784 add_function_defaults(List *args, HeapTuple func_tuple)
03785 {
03786     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
03787     int         nargsprovided = list_length(args);
03788     List       *defaults;
03789     int         ndelete;
03790 
03791     /* Get all the default expressions from the pg_proc tuple */
03792     defaults = fetch_function_defaults(func_tuple);
03793 
03794     /* Delete any unused defaults from the list */
03795     ndelete = nargsprovided + list_length(defaults) - funcform->pronargs;
03796     if (ndelete < 0)
03797         elog(ERROR, "not enough default arguments");
03798     while (ndelete-- > 0)
03799         defaults = list_delete_first(defaults);
03800 
03801     /* And form the combined argument list, not modifying the input list */
03802     return list_concat(list_copy(args), defaults);
03803 }
03804 
03805 /*
03806  * fetch_function_defaults: get function's default arguments as expression list
03807  */
03808 static List *
03809 fetch_function_defaults(HeapTuple func_tuple)
03810 {
03811     List       *defaults;
03812     Datum       proargdefaults;
03813     bool        isnull;
03814     char       *str;
03815 
03816     /* The error cases here shouldn't happen, but check anyway */
03817     proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
03818                                      Anum_pg_proc_proargdefaults,
03819                                      &isnull);
03820     if (isnull)
03821         elog(ERROR, "not enough default arguments");
03822     str = TextDatumGetCString(proargdefaults);
03823     defaults = (List *) stringToNode(str);
03824     Assert(IsA(defaults, List));
03825     pfree(str);
03826     return defaults;
03827 }
03828 
03829 /*
03830  * recheck_cast_function_args: recheck function args and typecast as needed
03831  * after adding defaults.
03832  *
03833  * It is possible for some of the defaulted arguments to be polymorphic;
03834  * therefore we can't assume that the default expressions have the correct
03835  * data types already.  We have to re-resolve polymorphics and do coercion
03836  * just like the parser did.
03837  *
03838  * This should be a no-op if there are no polymorphic arguments,
03839  * but we do it anyway to be sure.
03840  *
03841  * Note: if any casts are needed, the args list is modified in-place;
03842  * caller should have already copied the list structure.
03843  */
03844 static void
03845 recheck_cast_function_args(List *args, Oid result_type, HeapTuple func_tuple)
03846 {
03847     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
03848     int         nargs;
03849     Oid         actual_arg_types[FUNC_MAX_ARGS];
03850     Oid         declared_arg_types[FUNC_MAX_ARGS];
03851     Oid         rettype;
03852     ListCell   *lc;
03853 
03854     if (list_length(args) > FUNC_MAX_ARGS)
03855         elog(ERROR, "too many function arguments");
03856     nargs = 0;
03857     foreach(lc, args)
03858     {
03859         actual_arg_types[nargs++] = exprType((Node *) lfirst(lc));
03860     }
03861     Assert(nargs == funcform->pronargs);
03862     memcpy(declared_arg_types, funcform->proargtypes.values,
03863            funcform->pronargs * sizeof(Oid));
03864     rettype = enforce_generic_type_consistency(actual_arg_types,
03865                                                declared_arg_types,
03866                                                nargs,
03867                                                funcform->prorettype,
03868                                                false);
03869     /* let's just check we got the same answer as the parser did ... */
03870     if (rettype != result_type)
03871         elog(ERROR, "function's resolved result type changed during planning");
03872 
03873     /* perform any necessary typecasting of arguments */
03874     make_fn_arguments(NULL, args, actual_arg_types, declared_arg_types);
03875 }
03876 
03877 /*
03878  * evaluate_function: try to pre-evaluate a function call
03879  *
03880  * We can do this if the function is strict and has any constant-null inputs
03881  * (just return a null constant), or if the function is immutable and has all
03882  * constant inputs (call it and return the result as a Const node).  In
03883  * estimation mode we are willing to pre-evaluate stable functions too.
03884  *
03885  * Returns a simplified expression if successful, or NULL if cannot
03886  * simplify the function.
03887  */
03888 static Expr *
03889 evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
03890                   Oid result_collid, Oid input_collid, List *args,
03891                   bool funcvariadic,
03892                   HeapTuple func_tuple,
03893                   eval_const_expressions_context *context)
03894 {
03895     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
03896     bool        has_nonconst_input = false;
03897     bool        has_null_input = false;
03898     ListCell   *arg;
03899     FuncExpr   *newexpr;
03900 
03901     /*
03902      * Can't simplify if it returns a set.
03903      */
03904     if (funcform->proretset)
03905         return NULL;
03906 
03907     /*
03908      * Can't simplify if it returns RECORD.  The immediate problem is that it
03909      * will be needing an expected tupdesc which we can't supply here.
03910      *
03911      * In the case where it has OUT parameters, it could get by without an
03912      * expected tupdesc, but we still have issues: get_expr_result_type()
03913      * doesn't know how to extract type info from a RECORD constant, and in
03914      * the case of a NULL function result there doesn't seem to be any clean
03915      * way to fix that.  In view of the likelihood of there being still other
03916      * gotchas, seems best to leave the function call unreduced.
03917      */
03918     if (funcform->prorettype == RECORDOID)
03919         return NULL;
03920 
03921     /*
03922      * Check for constant inputs and especially constant-NULL inputs.
03923      */
03924     foreach(arg, args)
03925     {
03926         if (IsA(lfirst(arg), Const))
03927             has_null_input |= ((Const *) lfirst(arg))->constisnull;
03928         else
03929             has_nonconst_input = true;
03930     }
03931 
03932     /*
03933      * If the function is strict and has a constant-NULL input, it will never
03934      * be called at all, so we can replace the call by a NULL constant, even
03935      * if there are other inputs that aren't constant, and even if the
03936      * function is not otherwise immutable.
03937      */
03938     if (funcform->proisstrict && has_null_input)
03939         return (Expr *) makeNullConst(result_type, result_typmod,
03940                                       result_collid);
03941 
03942     /*
03943      * Otherwise, can simplify only if all inputs are constants. (For a
03944      * non-strict function, constant NULL inputs are treated the same as
03945      * constant non-NULL inputs.)
03946      */
03947     if (has_nonconst_input)
03948         return NULL;
03949 
03950     /*
03951      * Ordinarily we are only allowed to simplify immutable functions. But for
03952      * purposes of estimation, we consider it okay to simplify functions that
03953      * are merely stable; the risk that the result might change from planning
03954      * time to execution time is worth taking in preference to not being able
03955      * to estimate the value at all.
03956      */
03957     if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
03958          /* okay */ ;
03959     else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
03960          /* okay */ ;
03961     else
03962         return NULL;
03963 
03964     /*
03965      * OK, looks like we can simplify this operator/function.
03966      *
03967      * Build a new FuncExpr node containing the already-simplified arguments.
03968      */
03969     newexpr = makeNode(FuncExpr);
03970     newexpr->funcid = funcid;
03971     newexpr->funcresulttype = result_type;
03972     newexpr->funcretset = false;
03973     newexpr->funcvariadic = funcvariadic;
03974     newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
03975     newexpr->funccollid = result_collid;        /* doesn't matter */
03976     newexpr->inputcollid = input_collid;
03977     newexpr->args = args;
03978     newexpr->location = -1;
03979 
03980     return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
03981                          result_collid);
03982 }
03983 
03984 /*
03985  * inline_function: try to expand a function call inline
03986  *
03987  * If the function is a sufficiently simple SQL-language function
03988  * (just "SELECT expression"), then we can inline it and avoid the rather
03989  * high per-call overhead of SQL functions.  Furthermore, this can expose
03990  * opportunities for constant-folding within the function expression.
03991  *
03992  * We have to beware of some special cases however.  A directly or
03993  * indirectly recursive function would cause us to recurse forever,
03994  * so we keep track of which functions we are already expanding and
03995  * do not re-expand them.  Also, if a parameter is used more than once
03996  * in the SQL-function body, we require it not to contain any volatile
03997  * functions (volatiles might deliver inconsistent answers) nor to be
03998  * unreasonably expensive to evaluate.  The expensiveness check not only
03999  * prevents us from doing multiple evaluations of an expensive parameter
04000  * at runtime, but is a safety value to limit growth of an expression due
04001  * to repeated inlining.
04002  *
04003  * We must also beware of changing the volatility or strictness status of
04004  * functions by inlining them.
04005  *
04006  * Also, at the moment we can't inline functions returning RECORD.  This
04007  * doesn't work in the general case because it discards information such
04008  * as OUT-parameter declarations.
04009  *
04010  * Returns a simplified expression if successful, or NULL if cannot
04011  * simplify the function.
04012  */
04013 static Expr *
04014 inline_function(Oid funcid, Oid result_type, Oid result_collid,
04015                 Oid input_collid, List *args,
04016                 bool funcvariadic,
04017                 HeapTuple func_tuple,
04018                 eval_const_expressions_context *context)
04019 {
04020     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
04021     char       *src;
04022     Datum       tmp;
04023     bool        isNull;
04024     bool        modifyTargetList;
04025     MemoryContext oldcxt;
04026     MemoryContext mycxt;
04027     inline_error_callback_arg callback_arg;
04028     ErrorContextCallback sqlerrcontext;
04029     FuncExpr   *fexpr;
04030     SQLFunctionParseInfoPtr pinfo;
04031     ParseState *pstate;
04032     List       *raw_parsetree_list;
04033     Query      *querytree;
04034     Node       *newexpr;
04035     int        *usecounts;
04036     ListCell   *arg;
04037     int         i;
04038 
04039     /*
04040      * Forget it if the function is not SQL-language or has other showstopper
04041      * properties.  (The nargs check is just paranoia.)
04042      */
04043     if (funcform->prolang != SQLlanguageId ||
04044         funcform->prosecdef ||
04045         funcform->proretset ||
04046         funcform->prorettype == RECORDOID ||
04047         !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
04048         funcform->pronargs != list_length(args))
04049         return NULL;
04050 
04051     /* Check for recursive function, and give up trying to expand if so */
04052     if (list_member_oid(context->active_fns, funcid))
04053         return NULL;
04054 
04055     /* Check permission to call function (fail later, if not) */
04056     if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
04057         return NULL;
04058 
04059     /* Check whether a plugin wants to hook function entry/exit */
04060     if (FmgrHookIsNeeded(funcid))
04061         return NULL;
04062 
04063     /*
04064      * Make a temporary memory context, so that we don't leak all the stuff
04065      * that parsing might create.
04066      */
04067     mycxt = AllocSetContextCreate(CurrentMemoryContext,
04068                                   "inline_function",
04069                                   ALLOCSET_DEFAULT_MINSIZE,
04070                                   ALLOCSET_DEFAULT_INITSIZE,
04071                                   ALLOCSET_DEFAULT_MAXSIZE);
04072     oldcxt = MemoryContextSwitchTo(mycxt);
04073 
04074     /* Fetch the function body */
04075     tmp = SysCacheGetAttr(PROCOID,
04076                           func_tuple,
04077                           Anum_pg_proc_prosrc,
04078                           &isNull);
04079     if (isNull)
04080         elog(ERROR, "null prosrc for function %u", funcid);
04081     src = TextDatumGetCString(tmp);
04082 
04083     /*
04084      * Setup error traceback support for ereport().  This is so that we can
04085      * finger the function that bad information came from.
04086      */
04087     callback_arg.proname = NameStr(funcform->proname);
04088     callback_arg.prosrc = src;
04089 
04090     sqlerrcontext.callback = sql_inline_error_callback;
04091     sqlerrcontext.arg = (void *) &callback_arg;
04092     sqlerrcontext.previous = error_context_stack;
04093     error_context_stack = &sqlerrcontext;
04094 
04095     /*
04096      * Set up to handle parameters while parsing the function body.  We need a
04097      * dummy FuncExpr node containing the already-simplified arguments to pass
04098      * to prepare_sql_fn_parse_info.  (It is really only needed if there are
04099      * some polymorphic arguments, but for simplicity we always build it.)
04100      */
04101     fexpr = makeNode(FuncExpr);
04102     fexpr->funcid = funcid;
04103     fexpr->funcresulttype = result_type;
04104     fexpr->funcretset = false;
04105     fexpr->funcvariadic = funcvariadic;
04106     fexpr->funcformat = COERCE_EXPLICIT_CALL;   /* doesn't matter */
04107     fexpr->funccollid = result_collid;  /* doesn't matter */
04108     fexpr->inputcollid = input_collid;
04109     fexpr->args = args;
04110     fexpr->location = -1;
04111 
04112     pinfo = prepare_sql_fn_parse_info(func_tuple,
04113                                       (Node *) fexpr,
04114                                       input_collid);
04115 
04116     /*
04117      * We just do parsing and parse analysis, not rewriting, because rewriting
04118      * will not affect table-free-SELECT-only queries, which is all that we
04119      * care about.  Also, we can punt as soon as we detect more than one
04120      * command in the function body.
04121      */
04122     raw_parsetree_list = pg_parse_query(src);
04123     if (list_length(raw_parsetree_list) != 1)
04124         goto fail;
04125 
04126     pstate = make_parsestate(NULL);
04127     pstate->p_sourcetext = src;
04128     sql_fn_parser_setup(pstate, pinfo);
04129 
04130     querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
04131 
04132     free_parsestate(pstate);
04133 
04134     /*
04135      * The single command must be a simple "SELECT expression".
04136      */
04137     if (!IsA(querytree, Query) ||
04138         querytree->commandType != CMD_SELECT ||
04139         querytree->utilityStmt ||
04140         querytree->hasAggs ||
04141         querytree->hasWindowFuncs ||
04142         querytree->hasSubLinks ||
04143         querytree->cteList ||
04144         querytree->rtable ||
04145         querytree->jointree->fromlist ||
04146         querytree->jointree->quals ||
04147         querytree->groupClause ||
04148         querytree->havingQual ||
04149         querytree->windowClause ||
04150         querytree->distinctClause ||
04151         querytree->sortClause ||
04152         querytree->limitOffset ||
04153         querytree->limitCount ||
04154         querytree->setOperations ||
04155         list_length(querytree->targetList) != 1)
04156         goto fail;
04157 
04158     /*
04159      * Make sure the function (still) returns what it's declared to.  This
04160      * will raise an error if wrong, but that's okay since the function would
04161      * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
04162      * a RelabelType if needed to make the tlist expression match the declared
04163      * type of the function.
04164      *
04165      * Note: we do not try this until we have verified that no rewriting was
04166      * needed; that's probably not important, but let's be careful.
04167      */
04168     if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
04169                             &modifyTargetList, NULL))
04170         goto fail;              /* reject whole-tuple-result cases */
04171 
04172     /* Now we can grab the tlist expression */
04173     newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
04174 
04175     /* Assert that check_sql_fn_retval did the right thing */
04176     Assert(exprType(newexpr) == result_type);
04177     /* It couldn't have made any dangerous tlist changes, either */
04178     Assert(!modifyTargetList);
04179 
04180     /*
04181      * Additional validity checks on the expression.  It mustn't return a set,
04182      * and it mustn't be more volatile than the surrounding function (this is
04183      * to avoid breaking hacks that involve pretending a function is immutable
04184      * when it really ain't).  If the surrounding function is declared strict,
04185      * then the expression must contain only strict constructs and must use
04186      * all of the function parameters (this is overkill, but an exact analysis
04187      * is hard).
04188      */
04189     if (expression_returns_set(newexpr))
04190         goto fail;
04191 
04192     if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
04193         contain_mutable_functions(newexpr))
04194         goto fail;
04195     else if (funcform->provolatile == PROVOLATILE_STABLE &&
04196              contain_volatile_functions(newexpr))
04197         goto fail;
04198 
04199     if (funcform->proisstrict &&
04200         contain_nonstrict_functions(newexpr))
04201         goto fail;
04202 
04203     /*
04204      * We may be able to do it; there are still checks on parameter usage to
04205      * make, but those are most easily done in combination with the actual
04206      * substitution of the inputs.  So start building expression with inputs
04207      * substituted.
04208      */
04209     usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
04210     newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
04211                                            args, usecounts);
04212 
04213     /* Now check for parameter usage */
04214     i = 0;
04215     foreach(arg, args)
04216     {
04217         Node       *param = lfirst(arg);
04218 
04219         if (usecounts[i] == 0)
04220         {
04221             /* Param not used at all: uncool if func is strict */
04222             if (funcform->proisstrict)
04223                 goto fail;
04224         }
04225         else if (usecounts[i] != 1)
04226         {
04227             /* Param used multiple times: uncool if expensive or volatile */
04228             QualCost    eval_cost;
04229 
04230             /*
04231              * We define "expensive" as "contains any subplan or more than 10
04232              * operators".  Note that the subplan search has to be done
04233              * explicitly, since cost_qual_eval() will barf on unplanned
04234              * subselects.
04235              */
04236             if (contain_subplans(param))
04237                 goto fail;
04238             cost_qual_eval(&eval_cost, list_make1(param), NULL);
04239             if (eval_cost.startup + eval_cost.per_tuple >
04240                 10 * cpu_operator_cost)
04241                 goto fail;
04242 
04243             /*
04244              * Check volatility last since this is more expensive than the
04245              * above tests
04246              */
04247             if (contain_volatile_functions(param))
04248                 goto fail;
04249         }
04250         i++;
04251     }
04252 
04253     /*
04254      * Whew --- we can make the substitution.  Copy the modified expression
04255      * out of the temporary memory context, and clean up.
04256      */
04257     MemoryContextSwitchTo(oldcxt);
04258 
04259     newexpr = copyObject(newexpr);
04260 
04261     MemoryContextDelete(mycxt);
04262 
04263     /*
04264      * If the result is of a collatable type, force the result to expose the
04265      * correct collation.  In most cases this does not matter, but it's
04266      * possible that the function result is used directly as a sort key or in
04267      * other places where we expect exprCollation() to tell the truth.
04268      */
04269     if (OidIsValid(result_collid))
04270     {
04271         Oid         exprcoll = exprCollation(newexpr);
04272 
04273         if (OidIsValid(exprcoll) && exprcoll != result_collid)
04274         {
04275             CollateExpr *newnode = makeNode(CollateExpr);
04276 
04277             newnode->arg = (Expr *) newexpr;
04278             newnode->collOid = result_collid;
04279             newnode->location = -1;
04280 
04281             newexpr = (Node *) newnode;
04282         }
04283     }
04284 
04285     /*
04286      * Since there is now no trace of the function in the plan tree, we must
04287      * explicitly record the plan's dependency on the function.
04288      */
04289     if (context->root)
04290         record_plan_function_dependency(context->root, funcid);
04291 
04292     /*
04293      * Recursively try to simplify the modified expression.  Here we must add
04294      * the current function to the context list of active functions.
04295      */
04296     context->active_fns = lcons_oid(funcid, context->active_fns);
04297     newexpr = eval_const_expressions_mutator(newexpr, context);
04298     context->active_fns = list_delete_first(context->active_fns);
04299 
04300     error_context_stack = sqlerrcontext.previous;
04301 
04302     return (Expr *) newexpr;
04303 
04304     /* Here if func is not inlinable: release temp memory and return NULL */
04305 fail:
04306     MemoryContextSwitchTo(oldcxt);
04307     MemoryContextDelete(mycxt);
04308     error_context_stack = sqlerrcontext.previous;
04309 
04310     return NULL;
04311 }
04312 
04313 /*
04314  * Replace Param nodes by appropriate actual parameters
04315  */
04316 static Node *
04317 substitute_actual_parameters(Node *expr, int nargs, List *args,
04318                              int *usecounts)
04319 {
04320     substitute_actual_parameters_context context;
04321 
04322     context.nargs = nargs;
04323     context.args = args;
04324     context.usecounts = usecounts;
04325 
04326     return substitute_actual_parameters_mutator(expr, &context);
04327 }
04328 
04329 static Node *
04330 substitute_actual_parameters_mutator(Node *node,
04331                                substitute_actual_parameters_context *context)
04332 {
04333     if (node == NULL)
04334         return NULL;
04335     if (IsA(node, Param))
04336     {
04337         Param      *param = (Param *) node;
04338 
04339         if (param->paramkind != PARAM_EXTERN)
04340             elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
04341         if (param->paramid <= 0 || param->paramid > context->nargs)
04342             elog(ERROR, "invalid paramid: %d", param->paramid);
04343 
04344         /* Count usage of parameter */
04345         context->usecounts[param->paramid - 1]++;
04346 
04347         /* Select the appropriate actual arg and replace the Param with it */
04348         /* We don't need to copy at this time (it'll get done later) */
04349         return list_nth(context->args, param->paramid - 1);
04350     }
04351     return expression_tree_mutator(node, substitute_actual_parameters_mutator,
04352                                    (void *) context);
04353 }
04354 
04355 /*
04356  * error context callback to let us supply a call-stack traceback
04357  */
04358 static void
04359 sql_inline_error_callback(void *arg)
04360 {
04361     inline_error_callback_arg *callback_arg = (inline_error_callback_arg *) arg;
04362     int         syntaxerrposition;
04363 
04364     /* If it's a syntax error, convert to internal syntax error report */
04365     syntaxerrposition = geterrposition();
04366     if (syntaxerrposition > 0)
04367     {
04368         errposition(0);
04369         internalerrposition(syntaxerrposition);
04370         internalerrquery(callback_arg->prosrc);
04371     }
04372 
04373     errcontext("SQL function \"%s\" during inlining", callback_arg->proname);
04374 }
04375 
04376 /*
04377  * evaluate_expr: pre-evaluate a constant expression
04378  *
04379  * We use the executor's routine ExecEvalExpr() to avoid duplication of
04380  * code and ensure we get the same result as the executor would get.
04381  */
04382 static Expr *
04383 evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
04384               Oid result_collation)
04385 {
04386     EState     *estate;
04387     ExprState  *exprstate;
04388     MemoryContext oldcontext;
04389     Datum       const_val;
04390     bool        const_is_null;
04391     int16       resultTypLen;
04392     bool        resultTypByVal;
04393 
04394     /*
04395      * To use the executor, we need an EState.
04396      */
04397     estate = CreateExecutorState();
04398 
04399     /* We can use the estate's working context to avoid memory leaks. */
04400     oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
04401 
04402     /* Make sure any opfuncids are filled in. */
04403     fix_opfuncids((Node *) expr);
04404 
04405     /*
04406      * Prepare expr for execution.  (Note: we can't use ExecPrepareExpr
04407      * because it'd result in recursively invoking eval_const_expressions.)
04408      */
04409     exprstate = ExecInitExpr(expr, NULL);
04410 
04411     /*
04412      * And evaluate it.
04413      *
04414      * It is OK to use a default econtext because none of the ExecEvalExpr()
04415      * code used in this situation will use econtext.  That might seem
04416      * fortuitous, but it's not so unreasonable --- a constant expression does
04417      * not depend on context, by definition, n'est ce pas?
04418      */
04419     const_val = ExecEvalExprSwitchContext(exprstate,
04420                                           GetPerTupleExprContext(estate),
04421                                           &const_is_null, NULL);
04422 
04423     /* Get info needed about result datatype */
04424     get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
04425 
04426     /* Get back to outer memory context */
04427     MemoryContextSwitchTo(oldcontext);
04428 
04429     /*
04430      * Must copy result out of sub-context used by expression eval.
04431      *
04432      * Also, if it's varlena, forcibly detoast it.  This protects us against
04433      * storing TOAST pointers into plans that might outlive the referenced
04434      * data.
04435      */
04436     if (!const_is_null)
04437     {
04438         if (resultTypLen == -1)
04439             const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
04440         else
04441             const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
04442     }
04443 
04444     /* Release all the junk we just created */
04445     FreeExecutorState(estate);
04446 
04447     /*
04448      * Make the constant result node.
04449      */
04450     return (Expr *) makeConst(result_type, result_typmod, result_collation,
04451                               resultTypLen,
04452                               const_val, const_is_null,
04453                               resultTypByVal);
04454 }
04455 
04456 
04457 /*
04458  * inline_set_returning_function
04459  *      Attempt to "inline" a set-returning function in the FROM clause.
04460  *
04461  * "rte" is an RTE_FUNCTION rangetable entry.  If it represents a call of a
04462  * set-returning SQL function that can safely be inlined, expand the function
04463  * and return the substitute Query structure.  Otherwise, return NULL.
04464  *
04465  * This has a good deal of similarity to inline_function(), but that's
04466  * for the non-set-returning case, and there are enough differences to
04467  * justify separate functions.
04468  */
04469 Query *
04470 inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
04471 {
04472     FuncExpr   *fexpr;
04473     Oid         func_oid;
04474     HeapTuple   func_tuple;
04475     Form_pg_proc funcform;
04476     char       *src;
04477     Datum       tmp;
04478     bool        isNull;
04479     bool        modifyTargetList;
04480     MemoryContext oldcxt;
04481     MemoryContext mycxt;
04482     List       *saveInvalItems;
04483     inline_error_callback_arg callback_arg;
04484     ErrorContextCallback sqlerrcontext;
04485     SQLFunctionParseInfoPtr pinfo;
04486     List       *raw_parsetree_list;
04487     List       *querytree_list;
04488     Query      *querytree;
04489 
04490     Assert(rte->rtekind == RTE_FUNCTION);
04491 
04492     /*
04493      * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
04494      * own FROM clause, since that must cause infinite recursion at runtime.
04495      * It will cause this code to recurse too, so check for stack overflow.
04496      * (There's no need to do more.)
04497      */
04498     check_stack_depth();
04499 
04500     /* Fail if FROM item isn't a simple FuncExpr */
04501     fexpr = (FuncExpr *) rte->funcexpr;
04502     if (fexpr == NULL || !IsA(fexpr, FuncExpr))
04503         return NULL;
04504     func_oid = fexpr->funcid;
04505 
04506     /*
04507      * The function must be declared to return a set, else inlining would
04508      * change the results if the contained SELECT didn't return exactly one
04509      * row.
04510      */
04511     if (!fexpr->funcretset)
04512         return NULL;
04513 
04514     /*
04515      * Refuse to inline if the arguments contain any volatile functions or
04516      * sub-selects.  Volatile functions are rejected because inlining may
04517      * result in the arguments being evaluated multiple times, risking a
04518      * change in behavior.  Sub-selects are rejected partly for implementation
04519      * reasons (pushing them down another level might change their behavior)
04520      * and partly because they're likely to be expensive and so multiple
04521      * evaluation would be bad.
04522      */
04523     if (contain_volatile_functions((Node *) fexpr->args) ||
04524         contain_subplans((Node *) fexpr->args))
04525         return NULL;
04526 
04527     /* Check permission to call function (fail later, if not) */
04528     if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
04529         return NULL;
04530 
04531     /* Check whether a plugin wants to hook function entry/exit */
04532     if (FmgrHookIsNeeded(func_oid))
04533         return NULL;
04534 
04535     /*
04536      * OK, let's take a look at the function's pg_proc entry.
04537      */
04538     func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
04539     if (!HeapTupleIsValid(func_tuple))
04540         elog(ERROR, "cache lookup failed for function %u", func_oid);
04541     funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
04542 
04543     /*
04544      * Forget it if the function is not SQL-language or has other showstopper
04545      * properties.  In particular it mustn't be declared STRICT, since we
04546      * couldn't enforce that.  It also mustn't be VOLATILE, because that is
04547      * supposed to cause it to be executed with its own snapshot, rather than
04548      * sharing the snapshot of the calling query.  (Rechecking proretset is
04549      * just paranoia.)
04550      */
04551     if (funcform->prolang != SQLlanguageId ||
04552         funcform->proisstrict ||
04553         funcform->provolatile == PROVOLATILE_VOLATILE ||
04554         funcform->prosecdef ||
04555         !funcform->proretset ||
04556         !heap_attisnull(func_tuple, Anum_pg_proc_proconfig))
04557     {
04558         ReleaseSysCache(func_tuple);
04559         return NULL;
04560     }
04561 
04562     /*
04563      * Make a temporary memory context, so that we don't leak all the stuff
04564      * that parsing might create.
04565      */
04566     mycxt = AllocSetContextCreate(CurrentMemoryContext,
04567                                   "inline_set_returning_function",
04568                                   ALLOCSET_DEFAULT_MINSIZE,
04569                                   ALLOCSET_DEFAULT_INITSIZE,
04570                                   ALLOCSET_DEFAULT_MAXSIZE);
04571     oldcxt = MemoryContextSwitchTo(mycxt);
04572 
04573     /*
04574      * When we call eval_const_expressions below, it might try to add items to
04575      * root->glob->invalItems.  Since it is running in the temp context, those
04576      * items will be in that context, and will need to be copied out if we're
04577      * successful.  Temporarily reset the list so that we can keep those items
04578      * separate from the pre-existing list contents.
04579      */
04580     saveInvalItems = root->glob->invalItems;
04581     root->glob->invalItems = NIL;
04582 
04583     /* Fetch the function body */
04584     tmp = SysCacheGetAttr(PROCOID,
04585                           func_tuple,
04586                           Anum_pg_proc_prosrc,
04587                           &isNull);
04588     if (isNull)
04589         elog(ERROR, "null prosrc for function %u", func_oid);
04590     src = TextDatumGetCString(tmp);
04591 
04592     /*
04593      * Setup error traceback support for ereport().  This is so that we can
04594      * finger the function that bad information came from.
04595      */
04596     callback_arg.proname = NameStr(funcform->proname);
04597     callback_arg.prosrc = src;
04598 
04599     sqlerrcontext.callback = sql_inline_error_callback;
04600     sqlerrcontext.arg = (void *) &callback_arg;
04601     sqlerrcontext.previous = error_context_stack;
04602     error_context_stack = &sqlerrcontext;
04603 
04604     /*
04605      * Run eval_const_expressions on the function call.  This is necessary to
04606      * ensure that named-argument notation is converted to positional notation
04607      * and any default arguments are inserted.  It's a bit of overkill for the
04608      * arguments, since they'll get processed again later, but no harm will be
04609      * done.
04610      */
04611     fexpr = (FuncExpr *) eval_const_expressions(root, (Node *) fexpr);
04612 
04613     /* It should still be a call of the same function, but let's check */
04614     if (!IsA(fexpr, FuncExpr) ||
04615         fexpr->funcid != func_oid)
04616         goto fail;
04617 
04618     /* Arg list length should now match the function */
04619     if (list_length(fexpr->args) != funcform->pronargs)
04620         goto fail;
04621 
04622     /*
04623      * Set up to handle parameters while parsing the function body.  We can
04624      * use the FuncExpr just created as the input for
04625      * prepare_sql_fn_parse_info.
04626      */
04627     pinfo = prepare_sql_fn_parse_info(func_tuple,
04628                                       (Node *) fexpr,
04629                                       fexpr->inputcollid);
04630 
04631     /*
04632      * Parse, analyze, and rewrite (unlike inline_function(), we can't skip
04633      * rewriting here).  We can fail as soon as we find more than one query,
04634      * though.
04635      */
04636     raw_parsetree_list = pg_parse_query(src);
04637     if (list_length(raw_parsetree_list) != 1)
04638         goto fail;
04639 
04640     querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
04641                                                    src,
04642                                        (ParserSetupHook) sql_fn_parser_setup,
04643                                                    pinfo);
04644     if (list_length(querytree_list) != 1)
04645         goto fail;
04646     querytree = linitial(querytree_list);
04647 
04648     /*
04649      * The single command must be a plain SELECT.
04650      */
04651     if (!IsA(querytree, Query) ||
04652         querytree->commandType != CMD_SELECT ||
04653         querytree->utilityStmt)
04654         goto fail;
04655 
04656     /*
04657      * Make sure the function (still) returns what it's declared to.  This
04658      * will raise an error if wrong, but that's okay since the function would
04659      * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
04660      * RelabelType(s) and/or NULL columns if needed to make the tlist
04661      * expression(s) match the declared type of the function.
04662      *
04663      * If the function returns a composite type, don't inline unless the check
04664      * shows it's returning a whole tuple result; otherwise what it's
04665      * returning is a single composite column which is not what we need.
04666      */
04667     if (!check_sql_fn_retval(func_oid, fexpr->funcresulttype,
04668                              querytree_list,
04669                              &modifyTargetList, NULL) &&
04670         (get_typtype(fexpr->funcresulttype) == TYPTYPE_COMPOSITE ||
04671          fexpr->funcresulttype == RECORDOID))
04672         goto fail;              /* reject not-whole-tuple-result cases */
04673 
04674     /*
04675      * If we had to modify the tlist to make it match, and the statement is
04676      * one in which changing the tlist contents could change semantics, we
04677      * have to punt and not inline.
04678      */
04679     if (modifyTargetList)
04680         goto fail;
04681 
04682     /*
04683      * If it returns RECORD, we have to check against the column type list
04684      * provided in the RTE; check_sql_fn_retval can't do that.  (If no match,
04685      * we just fail to inline, rather than complaining; see notes for
04686      * tlist_matches_coltypelist.)  We don't have to do this for functions
04687      * with declared OUT parameters, even though their funcresulttype is
04688      * RECORDOID, so check get_func_result_type too.
04689      */
04690     if (fexpr->funcresulttype == RECORDOID &&
04691         get_func_result_type(func_oid, NULL, NULL) == TYPEFUNC_RECORD &&
04692         !tlist_matches_coltypelist(querytree->targetList, rte->funccoltypes))
04693         goto fail;
04694 
04695     /*
04696      * Looks good --- substitute parameters into the query.
04697      */
04698     querytree = substitute_actual_srf_parameters(querytree,
04699                                                  funcform->pronargs,
04700                                                  fexpr->args);
04701 
04702     /*
04703      * Copy the modified query out of the temporary memory context, and clean
04704      * up.
04705      */
04706     MemoryContextSwitchTo(oldcxt);
04707 
04708     querytree = copyObject(querytree);
04709 
04710     /* copy up any new invalItems, too */
04711     root->glob->invalItems = list_concat(saveInvalItems,
04712                                          copyObject(root->glob->invalItems));
04713 
04714     MemoryContextDelete(mycxt);
04715     error_context_stack = sqlerrcontext.previous;
04716     ReleaseSysCache(func_tuple);
04717 
04718     /*
04719      * We don't have to fix collations here because the upper query is already
04720      * parsed, ie, the collations in the RTE are what count.
04721      */
04722 
04723     /*
04724      * Since there is now no trace of the function in the plan tree, we must
04725      * explicitly record the plan's dependency on the function.
04726      */
04727     record_plan_function_dependency(root, func_oid);
04728 
04729     return querytree;
04730 
04731     /* Here if func is not inlinable: release temp memory and return NULL */
04732 fail:
04733     MemoryContextSwitchTo(oldcxt);
04734     root->glob->invalItems = saveInvalItems;
04735     MemoryContextDelete(mycxt);
04736     error_context_stack = sqlerrcontext.previous;
04737     ReleaseSysCache(func_tuple);
04738 
04739     return NULL;
04740 }
04741 
04742 /*
04743  * Replace Param nodes by appropriate actual parameters
04744  *
04745  * This is just enough different from substitute_actual_parameters()
04746  * that it needs its own code.
04747  */
04748 static Query *
04749 substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
04750 {
04751     substitute_actual_srf_parameters_context context;
04752 
04753     context.nargs = nargs;
04754     context.args = args;
04755     context.sublevels_up = 1;
04756 
04757     return query_tree_mutator(expr,
04758                               substitute_actual_srf_parameters_mutator,
04759                               &context,
04760                               0);
04761 }
04762 
04763 static Node *
04764 substitute_actual_srf_parameters_mutator(Node *node,
04765                            substitute_actual_srf_parameters_context *context)
04766 {
04767     Node       *result;
04768 
04769     if (node == NULL)
04770         return NULL;
04771     if (IsA(node, Query))
04772     {
04773         context->sublevels_up++;
04774         result = (Node *) query_tree_mutator((Query *) node,
04775                                     substitute_actual_srf_parameters_mutator,
04776                                              (void *) context,
04777                                              0);
04778         context->sublevels_up--;
04779         return result;
04780     }
04781     if (IsA(node, Param))
04782     {
04783         Param      *param = (Param *) node;
04784 
04785         if (param->paramkind == PARAM_EXTERN)
04786         {
04787             if (param->paramid <= 0 || param->paramid > context->nargs)
04788                 elog(ERROR, "invalid paramid: %d", param->paramid);
04789 
04790             /*
04791              * Since the parameter is being inserted into a subquery, we must
04792              * adjust levels.
04793              */
04794             result = copyObject(list_nth(context->args, param->paramid - 1));
04795             IncrementVarSublevelsUp(result, context->sublevels_up, 0);
04796             return result;
04797         }
04798     }
04799     return expression_tree_mutator(node,
04800                                    substitute_actual_srf_parameters_mutator,
04801                                    (void *) context);
04802 }
04803 
04804 /*
04805  * Check whether a SELECT targetlist emits the specified column types,
04806  * to see if it's safe to inline a function returning record.
04807  *
04808  * We insist on exact match here.  The executor allows binary-coercible
04809  * cases too, but we don't have a way to preserve the correct column types
04810  * in the correct places if we inline the function in such a case.
04811  *
04812  * Note that we only check type OIDs not typmods; this agrees with what the
04813  * executor would do at runtime, and attributing a specific typmod to a
04814  * function result is largely wishful thinking anyway.
04815  */
04816 static bool
04817 tlist_matches_coltypelist(List *tlist, List *coltypelist)
04818 {
04819     ListCell   *tlistitem;
04820     ListCell   *clistitem;
04821 
04822     clistitem = list_head(coltypelist);
04823     foreach(tlistitem, tlist)
04824     {
04825         TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
04826         Oid         coltype;
04827 
04828         if (tle->resjunk)
04829             continue;           /* ignore junk columns */
04830 
04831         if (clistitem == NULL)
04832             return false;       /* too many tlist items */
04833 
04834         coltype = lfirst_oid(clistitem);
04835         clistitem = lnext(clistitem);
04836 
04837         if (exprType((Node *) tle->expr) != coltype)
04838             return false;       /* column type mismatch */
04839     }
04840 
04841     if (clistitem != NULL)
04842         return false;           /* too few tlist items */
04843 
04844     return true;
04845 }