Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * clauses.c
4 : * routines to manipulate qualification clauses
5 : *
6 : * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/optimizer/util/clauses.c
12 : *
13 : * HISTORY
14 : * AUTHOR DATE MAJOR EVENT
15 : * Andrew Yu Nov 3, 1994 clause.c and clauses.c combined
16 : *
17 : *-------------------------------------------------------------------------
18 : */
19 :
20 : #include "postgres.h"
21 :
22 : #include "access/htup_details.h"
23 : #include "catalog/pg_aggregate.h"
24 : #include "catalog/pg_class.h"
25 : #include "catalog/pg_language.h"
26 : #include "catalog/pg_operator.h"
27 : #include "catalog/pg_proc.h"
28 : #include "catalog/pg_type.h"
29 : #include "executor/executor.h"
30 : #include "executor/functions.h"
31 : #include "funcapi.h"
32 : #include "miscadmin.h"
33 : #include "nodes/makefuncs.h"
34 : #include "nodes/nodeFuncs.h"
35 : #include "optimizer/clauses.h"
36 : #include "optimizer/cost.h"
37 : #include "optimizer/planmain.h"
38 : #include "optimizer/prep.h"
39 : #include "optimizer/var.h"
40 : #include "parser/analyze.h"
41 : #include "parser/parse_agg.h"
42 : #include "parser/parse_coerce.h"
43 : #include "parser/parse_func.h"
44 : #include "rewrite/rewriteManip.h"
45 : #include "tcop/tcopprot.h"
46 : #include "utils/acl.h"
47 : #include "utils/builtins.h"
48 : #include "utils/datum.h"
49 : #include "utils/fmgroids.h"
50 : #include "utils/lsyscache.h"
51 : #include "utils/memutils.h"
52 : #include "utils/syscache.h"
53 : #include "utils/typcache.h"
54 :
55 :
56 : typedef struct
57 : {
58 : PlannerInfo *root;
59 : AggSplit aggsplit;
60 : AggClauseCosts *costs;
61 : } get_agg_clause_costs_context;
62 :
63 : typedef struct
64 : {
65 : ParamListInfo boundParams;
66 : PlannerInfo *root;
67 : List *active_fns;
68 : Node *case_val;
69 : bool estimate;
70 : } eval_const_expressions_context;
71 :
72 : typedef struct
73 : {
74 : int nargs;
75 : List *args;
76 : int *usecounts;
77 : } substitute_actual_parameters_context;
78 :
79 : typedef struct
80 : {
81 : int nargs;
82 : List *args;
83 : int sublevels_up;
84 : } substitute_actual_srf_parameters_context;
85 :
86 : typedef struct
87 : {
88 : char *proname;
89 : char *prosrc;
90 : } inline_error_callback_arg;
91 :
92 : typedef struct
93 : {
94 : char max_hazard; /* worst proparallel hazard found so far */
95 : char max_interesting; /* worst proparallel hazard of interest */
96 : List *safe_param_ids; /* PARAM_EXEC Param IDs to treat as safe */
97 : } max_parallel_hazard_context;
98 :
99 : static bool contain_agg_clause_walker(Node *node, void *context);
100 : static bool get_agg_clause_costs_walker(Node *node,
101 : get_agg_clause_costs_context *context);
102 : static bool find_window_functions_walker(Node *node, WindowFuncLists *lists);
103 : static bool contain_subplans_walker(Node *node, void *context);
104 : static bool contain_mutable_functions_walker(Node *node, void *context);
105 : static bool contain_volatile_functions_walker(Node *node, void *context);
106 : static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context);
107 : static bool max_parallel_hazard_walker(Node *node,
108 : max_parallel_hazard_context *context);
109 : static bool contain_nonstrict_functions_walker(Node *node, void *context);
110 : static bool contain_context_dependent_node(Node *clause);
111 : static bool contain_context_dependent_node_walker(Node *node, int *flags);
112 : static bool contain_leaked_vars_walker(Node *node, void *context);
113 : static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
114 : static List *find_nonnullable_vars_walker(Node *node, bool top_level);
115 : static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
116 : static Node *eval_const_expressions_mutator(Node *node,
117 : eval_const_expressions_context *context);
118 : static List *simplify_or_arguments(List *args,
119 : eval_const_expressions_context *context,
120 : bool *haveNull, bool *forceTrue);
121 : static List *simplify_and_arguments(List *args,
122 : eval_const_expressions_context *context,
123 : bool *haveNull, bool *forceFalse);
124 : static Node *simplify_boolean_equality(Oid opno, List *args);
125 : static Expr *simplify_function(Oid funcid,
126 : Oid result_type, int32 result_typmod,
127 : Oid result_collid, Oid input_collid, List **args_p,
128 : bool funcvariadic, bool process_args, bool allow_non_const,
129 : eval_const_expressions_context *context);
130 : static List *expand_function_arguments(List *args, Oid result_type,
131 : HeapTuple func_tuple);
132 : static List *reorder_function_arguments(List *args, HeapTuple func_tuple);
133 : static List *add_function_defaults(List *args, HeapTuple func_tuple);
134 : static List *fetch_function_defaults(HeapTuple func_tuple);
135 : static void recheck_cast_function_args(List *args, Oid result_type,
136 : HeapTuple func_tuple);
137 : static Expr *evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
138 : Oid result_collid, Oid input_collid, List *args,
139 : bool funcvariadic,
140 : HeapTuple func_tuple,
141 : eval_const_expressions_context *context);
142 : static Expr *inline_function(Oid funcid, Oid result_type, Oid result_collid,
143 : Oid input_collid, List *args,
144 : bool funcvariadic,
145 : HeapTuple func_tuple,
146 : eval_const_expressions_context *context);
147 : static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
148 : int *usecounts);
149 : static Node *substitute_actual_parameters_mutator(Node *node,
150 : substitute_actual_parameters_context *context);
151 : static void sql_inline_error_callback(void *arg);
152 : static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
153 : Oid result_collation);
154 : static Query *substitute_actual_srf_parameters(Query *expr,
155 : int nargs, List *args);
156 : static Node *substitute_actual_srf_parameters_mutator(Node *node,
157 : substitute_actual_srf_parameters_context *context);
158 : static bool tlist_matches_coltypelist(List *tlist, List *coltypelist);
159 :
160 :
161 : /*****************************************************************************
162 : * OPERATOR clause functions
163 : *****************************************************************************/
164 :
165 : /*
166 : * make_opclause
167 : * Creates an operator clause given its operator info, left operand
168 : * and right operand (pass NULL to create single-operand clause),
169 : * and collation info.
170 : */
171 : Expr *
172 6473 : make_opclause(Oid opno, Oid opresulttype, bool opretset,
173 : Expr *leftop, Expr *rightop,
174 : Oid opcollid, Oid inputcollid)
175 : {
176 6473 : OpExpr *expr = makeNode(OpExpr);
177 :
178 6473 : expr->opno = opno;
179 6473 : expr->opfuncid = InvalidOid;
180 6473 : expr->opresulttype = opresulttype;
181 6473 : expr->opretset = opretset;
182 6473 : expr->opcollid = opcollid;
183 6473 : expr->inputcollid = inputcollid;
184 6473 : if (rightop)
185 6473 : expr->args = list_make2(leftop, rightop);
186 : else
187 0 : expr->args = list_make1(leftop);
188 6473 : expr->location = -1;
189 6473 : return (Expr *) expr;
190 : }
191 :
192 : /*
193 : * get_leftop
194 : *
195 : * Returns the left operand of a clause of the form (op expr expr)
196 : * or (op expr)
197 : */
198 : Node *
199 137068 : get_leftop(const Expr *clause)
200 : {
201 137068 : const OpExpr *expr = (const OpExpr *) clause;
202 :
203 137068 : if (expr->args != NIL)
204 137068 : return linitial(expr->args);
205 : else
206 0 : return NULL;
207 : }
208 :
209 : /*
210 : * get_rightop
211 : *
212 : * Returns the right operand in a clause of the form (op expr expr).
213 : * NB: result will be NULL if applied to a unary op clause.
214 : */
215 : Node *
216 135845 : get_rightop(const Expr *clause)
217 : {
218 135845 : const OpExpr *expr = (const OpExpr *) clause;
219 :
220 135845 : if (list_length(expr->args) >= 2)
221 135845 : return lsecond(expr->args);
222 : else
223 0 : return NULL;
224 : }
225 :
226 : /*****************************************************************************
227 : * NOT clause functions
228 : *****************************************************************************/
229 :
230 : /*
231 : * not_clause
232 : *
233 : * Returns t iff this is a 'not' clause: (NOT expr).
234 : */
235 : bool
236 33880 : not_clause(Node *clause)
237 : {
238 67760 : return (clause != NULL &&
239 36315 : IsA(clause, BoolExpr) &&
240 2435 : ((BoolExpr *) clause)->boolop == NOT_EXPR);
241 : }
242 :
243 : /*
244 : * make_notclause
245 : *
246 : * Create a 'not' clause given the expression to be negated.
247 : */
248 : Expr *
249 795 : make_notclause(Expr *notclause)
250 : {
251 795 : BoolExpr *expr = makeNode(BoolExpr);
252 :
253 795 : expr->boolop = NOT_EXPR;
254 795 : expr->args = list_make1(notclause);
255 795 : expr->location = -1;
256 795 : return (Expr *) expr;
257 : }
258 :
259 : /*
260 : * get_notclausearg
261 : *
262 : * Retrieve the clause within a 'not' clause
263 : */
264 : Expr *
265 1144 : get_notclausearg(Expr *notclause)
266 : {
267 1144 : return linitial(((BoolExpr *) notclause)->args);
268 : }
269 :
270 : /*****************************************************************************
271 : * OR clause functions
272 : *****************************************************************************/
273 :
274 : /*
275 : * or_clause
276 : *
277 : * Returns t iff the clause is an 'or' clause: (OR { expr }).
278 : */
279 : bool
280 153233 : or_clause(Node *clause)
281 : {
282 306466 : return (clause != NULL &&
283 163593 : IsA(clause, BoolExpr) &&
284 10360 : ((BoolExpr *) clause)->boolop == OR_EXPR);
285 : }
286 :
287 : /*
288 : * make_orclause
289 : *
290 : * Creates an 'or' clause given a list of its subclauses.
291 : */
292 : Expr *
293 1342 : make_orclause(List *orclauses)
294 : {
295 1342 : BoolExpr *expr = makeNode(BoolExpr);
296 :
297 1342 : expr->boolop = OR_EXPR;
298 1342 : expr->args = orclauses;
299 1342 : expr->location = -1;
300 1342 : return (Expr *) expr;
301 : }
302 :
303 : /*****************************************************************************
304 : * AND clause functions
305 : *****************************************************************************/
306 :
307 :
308 : /*
309 : * and_clause
310 : *
311 : * Returns t iff its argument is an 'and' clause: (AND { expr }).
312 : */
313 : bool
314 201226 : and_clause(Node *clause)
315 : {
316 402452 : return (clause != NULL &&
317 218362 : IsA(clause, BoolExpr) &&
318 17136 : ((BoolExpr *) clause)->boolop == AND_EXPR);
319 : }
320 :
321 : /*
322 : * make_andclause
323 : *
324 : * Creates an 'and' clause given a list of its subclauses.
325 : */
326 : Expr *
327 10040 : make_andclause(List *andclauses)
328 : {
329 10040 : BoolExpr *expr = makeNode(BoolExpr);
330 :
331 10040 : expr->boolop = AND_EXPR;
332 10040 : expr->args = andclauses;
333 10040 : expr->location = -1;
334 10040 : return (Expr *) expr;
335 : }
336 :
337 : /*
338 : * make_and_qual
339 : *
340 : * Variant of make_andclause for ANDing two qual conditions together.
341 : * Qual conditions have the property that a NULL nodetree is interpreted
342 : * as 'true'.
343 : *
344 : * NB: this makes no attempt to preserve AND/OR flatness; so it should not
345 : * be used on a qual that has already been run through prepqual.c.
346 : */
347 : Node *
348 411 : make_and_qual(Node *qual1, Node *qual2)
349 : {
350 411 : if (qual1 == NULL)
351 176 : return qual2;
352 235 : if (qual2 == NULL)
353 0 : return qual1;
354 235 : return (Node *) make_andclause(list_make2(qual1, qual2));
355 : }
356 :
357 : /*
358 : * The planner frequently prefers to represent qualification expressions
359 : * as lists of boolean expressions with implicit AND semantics.
360 : *
361 : * These functions convert between an AND-semantics expression list and the
362 : * ordinary representation of a boolean expression.
363 : *
364 : * Note that an empty list is considered equivalent to TRUE.
365 : */
366 : Expr *
367 2121 : make_ands_explicit(List *andclauses)
368 : {
369 2121 : if (andclauses == NIL)
370 0 : return (Expr *) makeBoolConst(true, false);
371 2121 : else if (list_length(andclauses) == 1)
372 1679 : return (Expr *) linitial(andclauses);
373 : else
374 442 : return make_andclause(andclauses);
375 : }
376 :
377 : List *
378 15548 : make_ands_implicit(Expr *clause)
379 : {
380 : /*
381 : * NB: because the parser sets the qual field to NULL in a query that has
382 : * no WHERE clause, we must consider a NULL input clause as TRUE, even
383 : * though one might more reasonably think it FALSE. Grumble. If this
384 : * causes trouble, consider changing the parser's behavior.
385 : */
386 15548 : if (clause == NULL)
387 2469 : return NIL; /* NULL -> NIL list == TRUE */
388 13079 : else if (and_clause((Node *) clause))
389 3949 : return ((BoolExpr *) clause)->args;
390 9303 : else if (IsA(clause, Const) &&
391 346 : !((Const *) clause)->constisnull &&
392 173 : DatumGetBool(((Const *) clause)->constvalue))
393 86 : return NIL; /* constant TRUE input -> NIL list */
394 : else
395 9044 : return list_make1(clause);
396 : }
397 :
398 :
399 : /*****************************************************************************
400 : * Aggregate-function clause manipulation
401 : *****************************************************************************/
402 :
403 : /*
404 : * contain_agg_clause
405 : * Recursively search for Aggref/GroupingFunc nodes within a clause.
406 : *
407 : * Returns true if any aggregate found.
408 : *
409 : * This does not descend into subqueries, and so should be used only after
410 : * reduction of sublinks to subplans, or in contexts where it's known there
411 : * are no subqueries. There mustn't be outer-aggregate references either.
412 : *
413 : * (If you want something like this but able to deal with subqueries,
414 : * see rewriteManip.c's contain_aggs_of_level().)
415 : */
416 : bool
417 117 : contain_agg_clause(Node *clause)
418 : {
419 117 : return contain_agg_clause_walker(clause, NULL);
420 : }
421 :
422 : static bool
423 215 : contain_agg_clause_walker(Node *node, void *context)
424 : {
425 215 : if (node == NULL)
426 3 : return false;
427 212 : if (IsA(node, Aggref))
428 : {
429 17 : Assert(((Aggref *) node)->agglevelsup == 0);
430 17 : return true; /* abort the tree traversal and return true */
431 : }
432 195 : if (IsA(node, GroupingFunc))
433 : {
434 2 : Assert(((GroupingFunc *) node)->agglevelsup == 0);
435 2 : return true; /* abort the tree traversal and return true */
436 : }
437 193 : Assert(!IsA(node, SubLink));
438 193 : return expression_tree_walker(node, contain_agg_clause_walker, context);
439 : }
440 :
441 : /*
442 : * get_agg_clause_costs
443 : * Recursively find the Aggref nodes in an expression tree, and
444 : * accumulate cost information about them.
445 : *
446 : * 'aggsplit' tells us the expected partial-aggregation mode, which affects
447 : * the cost estimates.
448 : *
449 : * NOTE that the counts/costs are ADDED to those already in *costs ... so
450 : * the caller is responsible for zeroing the struct initially.
451 : *
452 : * We count the nodes, estimate their execution costs, and estimate the total
453 : * space needed for their transition state values if all are evaluated in
454 : * parallel (as would be done in a HashAgg plan). Also, we check whether
455 : * partial aggregation is feasible. See AggClauseCosts for the exact set
456 : * of statistics collected.
457 : *
458 : * In addition, we mark Aggref nodes with the correct aggtranstype, so
459 : * that that doesn't need to be done repeatedly. (That makes this function's
460 : * name a bit of a misnomer.)
461 : *
462 : * This does not descend into subqueries, and so should be used only after
463 : * reduction of sublinks to subplans, or in contexts where it's known there
464 : * are no subqueries. There mustn't be outer-aggregate references either.
465 : */
466 : void
467 4853 : get_agg_clause_costs(PlannerInfo *root, Node *clause, AggSplit aggsplit,
468 : AggClauseCosts *costs)
469 : {
470 : get_agg_clause_costs_context context;
471 :
472 4853 : context.root = root;
473 4853 : context.aggsplit = aggsplit;
474 4853 : context.costs = costs;
475 4853 : (void) get_agg_clause_costs_walker(clause, &context);
476 4853 : }
477 :
478 : static bool
479 11943 : get_agg_clause_costs_walker(Node *node, get_agg_clause_costs_context *context)
480 : {
481 11943 : if (node == NULL)
482 2428 : return false;
483 9515 : if (IsA(node, Aggref))
484 : {
485 2662 : Aggref *aggref = (Aggref *) node;
486 2662 : AggClauseCosts *costs = context->costs;
487 : HeapTuple aggTuple;
488 : Form_pg_aggregate aggform;
489 : Oid aggtransfn;
490 : Oid aggfinalfn;
491 : Oid aggcombinefn;
492 : Oid aggserialfn;
493 : Oid aggdeserialfn;
494 : Oid aggtranstype;
495 : int32 aggtransspace;
496 : QualCost argcosts;
497 :
498 2662 : Assert(aggref->agglevelsup == 0);
499 :
500 : /*
501 : * Fetch info about aggregate from pg_aggregate. Note it's correct to
502 : * ignore the moving-aggregate variant, since what we're concerned
503 : * with here is aggregates not window functions.
504 : */
505 2662 : aggTuple = SearchSysCache1(AGGFNOID,
506 : ObjectIdGetDatum(aggref->aggfnoid));
507 2662 : if (!HeapTupleIsValid(aggTuple))
508 0 : elog(ERROR, "cache lookup failed for aggregate %u",
509 : aggref->aggfnoid);
510 2662 : aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
511 2662 : aggtransfn = aggform->aggtransfn;
512 2662 : aggfinalfn = aggform->aggfinalfn;
513 2662 : aggcombinefn = aggform->aggcombinefn;
514 2662 : aggserialfn = aggform->aggserialfn;
515 2662 : aggdeserialfn = aggform->aggdeserialfn;
516 2662 : aggtranstype = aggform->aggtranstype;
517 2662 : aggtransspace = aggform->aggtransspace;
518 2662 : ReleaseSysCache(aggTuple);
519 :
520 : /*
521 : * Resolve the possibly-polymorphic aggregate transition type, unless
522 : * already done in a previous pass over the expression.
523 : */
524 2662 : if (OidIsValid(aggref->aggtranstype))
525 96 : aggtranstype = aggref->aggtranstype;
526 : else
527 : {
528 : Oid inputTypes[FUNC_MAX_ARGS];
529 : int numArguments;
530 :
531 : /* extract argument types (ignoring any ORDER BY expressions) */
532 2566 : numArguments = get_aggregate_argtypes(aggref, inputTypes);
533 :
534 : /* resolve actual type of transition state, if polymorphic */
535 2566 : aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid,
536 : aggtranstype,
537 : inputTypes,
538 : numArguments);
539 2566 : aggref->aggtranstype = aggtranstype;
540 : }
541 :
542 : /*
543 : * Count it, and check for cases requiring ordered input. Note that
544 : * ordered-set aggs always have nonempty aggorder. Any ordered-input
545 : * case also defeats partial aggregation.
546 : */
547 2662 : costs->numAggs++;
548 2662 : if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
549 : {
550 118 : costs->numOrderedAggs++;
551 118 : costs->hasNonPartial = true;
552 : }
553 :
554 : /*
555 : * Check whether partial aggregation is feasible, unless we already
556 : * found out that we can't do it.
557 : */
558 2662 : if (!costs->hasNonPartial)
559 : {
560 : /*
561 : * If there is no combine function, then partial aggregation is
562 : * not possible.
563 : */
564 2529 : if (!OidIsValid(aggcombinefn))
565 1267 : costs->hasNonPartial = true;
566 :
567 : /*
568 : * If we have any aggs with transtype INTERNAL then we must check
569 : * whether they have serialization/deserialization functions; if
570 : * not, we can't serialize partial-aggregation results.
571 : */
572 1262 : else if (aggtranstype == INTERNALOID &&
573 50 : (!OidIsValid(aggserialfn) || !OidIsValid(aggdeserialfn)))
574 0 : costs->hasNonSerial = true;
575 : }
576 :
577 : /*
578 : * Add the appropriate component function execution costs to
579 : * appropriate totals.
580 : */
581 2662 : if (DO_AGGSPLIT_COMBINE(context->aggsplit))
582 : {
583 : /* charge for combining previously aggregated states */
584 48 : costs->transCost.per_tuple += get_func_cost(aggcombinefn) * cpu_operator_cost;
585 : }
586 : else
587 2614 : costs->transCost.per_tuple += get_func_cost(aggtransfn) * cpu_operator_cost;
588 2662 : if (DO_AGGSPLIT_DESERIALIZE(context->aggsplit) &&
589 : OidIsValid(aggdeserialfn))
590 0 : costs->transCost.per_tuple += get_func_cost(aggdeserialfn) * cpu_operator_cost;
591 2662 : if (DO_AGGSPLIT_SERIALIZE(context->aggsplit) &&
592 : OidIsValid(aggserialfn))
593 0 : costs->finalCost += get_func_cost(aggserialfn) * cpu_operator_cost;
594 2662 : if (!DO_AGGSPLIT_SKIPFINAL(context->aggsplit) &&
595 : OidIsValid(aggfinalfn))
596 1413 : costs->finalCost += get_func_cost(aggfinalfn) * cpu_operator_cost;
597 :
598 : /*
599 : * These costs are incurred only by the initial aggregate node, so we
600 : * mustn't include them again at upper levels.
601 : */
602 2662 : if (!DO_AGGSPLIT_COMBINE(context->aggsplit))
603 : {
604 : /* add the input expressions' cost to per-input-row costs */
605 2614 : cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
606 2614 : costs->transCost.startup += argcosts.startup;
607 2614 : costs->transCost.per_tuple += argcosts.per_tuple;
608 :
609 : /*
610 : * Add any filter's cost to per-input-row costs.
611 : *
612 : * XXX Ideally we should reduce input expression costs according
613 : * to filter selectivity, but it's not clear it's worth the
614 : * trouble.
615 : */
616 2614 : if (aggref->aggfilter)
617 : {
618 13 : cost_qual_eval_node(&argcosts, (Node *) aggref->aggfilter,
619 : context->root);
620 13 : costs->transCost.startup += argcosts.startup;
621 13 : costs->transCost.per_tuple += argcosts.per_tuple;
622 : }
623 : }
624 :
625 : /*
626 : * If there are direct arguments, treat their evaluation cost like the
627 : * cost of the finalfn.
628 : */
629 2662 : if (aggref->aggdirectargs)
630 : {
631 27 : cost_qual_eval_node(&argcosts, (Node *) aggref->aggdirectargs,
632 : context->root);
633 27 : costs->transCost.startup += argcosts.startup;
634 27 : costs->finalCost += argcosts.per_tuple;
635 : }
636 :
637 : /*
638 : * If the transition type is pass-by-value then it doesn't add
639 : * anything to the required size of the hashtable. If it is
640 : * pass-by-reference then we have to add the estimated size of the
641 : * value itself, plus palloc overhead.
642 : */
643 2662 : if (!get_typbyval(aggtranstype))
644 : {
645 : int32 avgwidth;
646 :
647 : /* Use average width if aggregate definition gave one */
648 1053 : if (aggtransspace > 0)
649 15 : avgwidth = aggtransspace;
650 1038 : else if (aggtransfn == F_ARRAY_APPEND)
651 : {
652 : /*
653 : * If the transition function is array_append(), it'll use an
654 : * expanded array as transvalue, which will occupy at least
655 : * ALLOCSET_SMALL_INITSIZE and possibly more. Use that as the
656 : * estimate for lack of a better idea.
657 : */
658 2 : avgwidth = ALLOCSET_SMALL_INITSIZE;
659 : }
660 : else
661 : {
662 : /*
663 : * If transition state is of same type as first aggregated
664 : * input, assume it's the same typmod (same width) as well.
665 : * This works for cases like MAX/MIN and is probably somewhat
666 : * reasonable otherwise.
667 : */
668 1036 : int32 aggtranstypmod = -1;
669 :
670 1036 : if (aggref->args)
671 : {
672 339 : TargetEntry *tle = (TargetEntry *) linitial(aggref->args);
673 :
674 339 : if (aggtranstype == exprType((Node *) tle->expr))
675 73 : aggtranstypmod = exprTypmod((Node *) tle->expr);
676 : }
677 :
678 1036 : avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
679 : }
680 :
681 1053 : avgwidth = MAXALIGN(avgwidth);
682 1053 : costs->transitionSpace += avgwidth + 2 * sizeof(void *);
683 : }
684 1609 : else if (aggtranstype == INTERNALOID)
685 : {
686 : /*
687 : * INTERNAL transition type is a special case: although INTERNAL
688 : * is pass-by-value, it's almost certainly being used as a pointer
689 : * to some large data structure. The aggregate definition can
690 : * provide an estimate of the size. If it doesn't, then we assume
691 : * ALLOCSET_DEFAULT_INITSIZE, which is a good guess if the data is
692 : * being kept in a private memory context, as is done by
693 : * array_agg() for instance.
694 : */
695 1354 : if (aggtransspace > 0)
696 50 : costs->transitionSpace += aggtransspace;
697 : else
698 1304 : costs->transitionSpace += ALLOCSET_DEFAULT_INITSIZE;
699 : }
700 :
701 : /*
702 : * We assume that the parser checked that there are no aggregates (of
703 : * this level anyway) in the aggregated arguments, direct arguments,
704 : * or filter clause. Hence, we need not recurse into any of them.
705 : */
706 2662 : return false;
707 : }
708 6853 : Assert(!IsA(node, SubLink));
709 6853 : return expression_tree_walker(node, get_agg_clause_costs_walker,
710 : (void *) context);
711 : }
712 :
713 :
714 : /*****************************************************************************
715 : * Window-function clause manipulation
716 : *****************************************************************************/
717 :
718 : /*
719 : * contain_window_function
720 : * Recursively search for WindowFunc nodes within a clause.
721 : *
722 : * Since window functions don't have level fields, but are hard-wired to
723 : * be associated with the current query level, this is just the same as
724 : * rewriteManip.c's function.
725 : */
726 : bool
727 242 : contain_window_function(Node *clause)
728 : {
729 242 : return contain_windowfuncs(clause);
730 : }
731 :
732 : /*
733 : * find_window_functions
734 : * Locate all the WindowFunc nodes in an expression tree, and organize
735 : * them by winref ID number.
736 : *
737 : * Caller must provide an upper bound on the winref IDs expected in the tree.
738 : */
739 : WindowFuncLists *
740 133 : find_window_functions(Node *clause, Index maxWinRef)
741 : {
742 133 : WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
743 :
744 133 : lists->numWindowFuncs = 0;
745 133 : lists->maxWinRef = maxWinRef;
746 133 : lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
747 133 : (void) find_window_functions_walker(clause, lists);
748 133 : return lists;
749 : }
750 :
751 : static bool
752 1073 : find_window_functions_walker(Node *node, WindowFuncLists *lists)
753 : {
754 1073 : if (node == NULL)
755 17 : return false;
756 1056 : if (IsA(node, WindowFunc))
757 : {
758 167 : WindowFunc *wfunc = (WindowFunc *) node;
759 :
760 : /* winref is unsigned, so one-sided test is OK */
761 167 : if (wfunc->winref > lists->maxWinRef)
762 0 : elog(ERROR, "WindowFunc contains out-of-range winref %u",
763 : wfunc->winref);
764 : /* eliminate duplicates, so that we avoid repeated computation */
765 167 : if (!list_member(lists->windowFuncs[wfunc->winref], wfunc))
766 : {
767 330 : lists->windowFuncs[wfunc->winref] =
768 165 : lappend(lists->windowFuncs[wfunc->winref], wfunc);
769 165 : lists->numWindowFuncs++;
770 : }
771 :
772 : /*
773 : * We assume that the parser checked that there are no window
774 : * functions in the arguments or filter clause. Hence, we need not
775 : * recurse into them. (If either the parser or the planner screws up
776 : * on this point, the executor will still catch it; see ExecInitExpr.)
777 : */
778 167 : return false;
779 : }
780 889 : Assert(!IsA(node, SubLink));
781 889 : return expression_tree_walker(node, find_window_functions_walker,
782 : (void *) lists);
783 : }
784 :
785 :
786 : /*****************************************************************************
787 : * Support for expressions returning sets
788 : *****************************************************************************/
789 :
790 : /*
791 : * expression_returns_set_rows
792 : * Estimate the number of rows returned by a set-returning expression.
793 : * The result is 1 if it's not a set-returning expression.
794 : *
795 : * We should only examine the top-level function or operator; it used to be
796 : * appropriate to recurse, but not anymore. (Even if there are more SRFs in
797 : * the function's inputs, their multipliers are accounted for separately.)
798 : *
799 : * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
800 : */
801 : double
802 1797 : expression_returns_set_rows(Node *clause)
803 : {
804 1797 : if (clause == NULL)
805 0 : return 1.0;
806 1797 : if (IsA(clause, FuncExpr))
807 : {
808 1603 : FuncExpr *expr = (FuncExpr *) clause;
809 :
810 1603 : if (expr->funcretset)
811 1346 : return clamp_row_est(get_func_rows(expr->funcid));
812 : }
813 451 : if (IsA(clause, OpExpr))
814 : {
815 1 : OpExpr *expr = (OpExpr *) clause;
816 :
817 1 : if (expr->opretset)
818 : {
819 1 : set_opfuncid(expr);
820 1 : return clamp_row_est(get_func_rows(expr->opfuncid));
821 : }
822 : }
823 450 : return 1.0;
824 : }
825 :
826 :
827 : /*****************************************************************************
828 : * Subplan clause manipulation
829 : *****************************************************************************/
830 :
831 : /*
832 : * contain_subplans
833 : * Recursively search for subplan nodes within a clause.
834 : *
835 : * If we see a SubLink node, we will return TRUE. This is only possible if
836 : * the expression tree hasn't yet been transformed by subselect.c. We do not
837 : * know whether the node will produce a true subplan or just an initplan,
838 : * but we make the conservative assumption that it will be a subplan.
839 : *
840 : * Returns true if any subplan found.
841 : */
842 : bool
843 1329 : contain_subplans(Node *clause)
844 : {
845 1329 : return contain_subplans_walker(clause, NULL);
846 : }
847 :
848 : static bool
849 3559 : contain_subplans_walker(Node *node, void *context)
850 : {
851 3559 : if (node == NULL)
852 188 : return false;
853 6732 : if (IsA(node, SubPlan) ||
854 6722 : IsA(node, AlternativeSubPlan) ||
855 3361 : IsA(node, SubLink))
856 28 : return true; /* abort the tree traversal and return true */
857 3343 : return expression_tree_walker(node, contain_subplans_walker, context);
858 : }
859 :
860 :
861 : /*****************************************************************************
862 : * Check clauses for mutable functions
863 : *****************************************************************************/
864 :
865 : /*
866 : * contain_mutable_functions
867 : * Recursively search for mutable functions within a clause.
868 : *
869 : * Returns true if any mutable function (or operator implemented by a
870 : * mutable function) is found. This test is needed so that we don't
871 : * mistakenly think that something like "WHERE random() < 0.5" can be treated
872 : * as a constant qualification.
873 : *
874 : * We will recursively look into Query nodes (i.e., SubLink sub-selects)
875 : * but not into SubPlans. See comments for contain_volatile_functions().
876 : */
877 : bool
878 4993 : contain_mutable_functions(Node *clause)
879 : {
880 4993 : return contain_mutable_functions_walker(clause, NULL);
881 : }
882 :
883 : static bool
884 3922 : contain_mutable_functions_checker(Oid func_id, void *context)
885 : {
886 3922 : return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE);
887 : }
888 :
889 : static bool
890 14269 : contain_mutable_functions_walker(Node *node, void *context)
891 : {
892 14269 : if (node == NULL)
893 44 : return false;
894 : /* Check for mutable functions in node itself */
895 14225 : if (check_functions_in_node(node, contain_mutable_functions_checker,
896 : context))
897 441 : return true;
898 :
899 13784 : if (IsA(node, SQLValueFunction))
900 : {
901 : /* all variants of SQLValueFunction are stable */
902 12 : return true;
903 : }
904 :
905 13772 : if (IsA(node, NextValueExpr))
906 : {
907 : /* NextValueExpr is volatile */
908 0 : return true;
909 : }
910 :
911 : /*
912 : * It should be safe to treat MinMaxExpr as immutable, because it will
913 : * depend on a non-cross-type btree comparison function, and those should
914 : * always be immutable. Treating XmlExpr as immutable is more dubious,
915 : * and treating CoerceToDomain as immutable is outright dangerous. But we
916 : * have done so historically, and changing this would probably cause more
917 : * problems than it would fix. In practice, if you have a non-immutable
918 : * domain constraint you are in for pain anyhow.
919 : */
920 :
921 : /* Recurse to check arguments */
922 13772 : if (IsA(node, Query))
923 : {
924 : /* Recurse into subselects */
925 0 : return query_tree_walker((Query *) node,
926 : contain_mutable_functions_walker,
927 : context, 0);
928 : }
929 13772 : return expression_tree_walker(node, contain_mutable_functions_walker,
930 : context);
931 : }
932 :
933 :
934 : /*****************************************************************************
935 : * Check clauses for volatile functions
936 : *****************************************************************************/
937 :
938 : /*
939 : * contain_volatile_functions
940 : * Recursively search for volatile functions within a clause.
941 : *
942 : * Returns true if any volatile function (or operator implemented by a
943 : * volatile function) is found. This test prevents, for example,
944 : * invalid conversions of volatile expressions into indexscan quals.
945 : *
946 : * We will recursively look into Query nodes (i.e., SubLink sub-selects)
947 : * but not into SubPlans. This is a bit odd, but intentional. If we are
948 : * looking at a SubLink, we are probably deciding whether a query tree
949 : * transformation is safe, and a contained sub-select should affect that;
950 : * for example, duplicating a sub-select containing a volatile function
951 : * would be bad. However, once we've got to the stage of having SubPlans,
952 : * subsequent planning need not consider volatility within those, since
953 : * the executor won't change its evaluation rules for a SubPlan based on
954 : * volatility.
955 : */
956 : bool
957 70097 : contain_volatile_functions(Node *clause)
958 : {
959 70097 : return contain_volatile_functions_walker(clause, NULL);
960 : }
961 :
962 : static bool
963 38394 : contain_volatile_functions_checker(Oid func_id, void *context)
964 : {
965 38394 : return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
966 : }
967 :
968 : static bool
969 208605 : contain_volatile_functions_walker(Node *node, void *context)
970 : {
971 208605 : if (node == NULL)
972 8562 : return false;
973 : /* Check for volatile functions in node itself */
974 200043 : if (check_functions_in_node(node, contain_volatile_functions_checker,
975 : context))
976 68 : return true;
977 :
978 199975 : if (IsA(node, NextValueExpr))
979 : {
980 : /* NextValueExpr is volatile */
981 0 : return true;
982 : }
983 :
984 : /*
985 : * See notes in contain_mutable_functions_walker about why we treat
986 : * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
987 : * SQLValueFunction is stable. Hence, none of them are of interest here.
988 : */
989 :
990 : /* Recurse to check arguments */
991 199975 : if (IsA(node, Query))
992 : {
993 : /* Recurse into subselects */
994 244 : return query_tree_walker((Query *) node,
995 : contain_volatile_functions_walker,
996 : context, 0);
997 : }
998 199731 : return expression_tree_walker(node, contain_volatile_functions_walker,
999 : context);
1000 : }
1001 :
1002 : /*
1003 : * Special purpose version of contain_volatile_functions() for use in COPY:
1004 : * ignore nextval(), but treat all other functions normally.
1005 : */
1006 : bool
1007 12 : contain_volatile_functions_not_nextval(Node *clause)
1008 : {
1009 12 : return contain_volatile_functions_not_nextval_walker(clause, NULL);
1010 : }
1011 :
1012 : static bool
1013 8 : contain_volatile_functions_not_nextval_checker(Oid func_id, void *context)
1014 : {
1015 12 : return (func_id != F_NEXTVAL_OID &&
1016 4 : func_volatile(func_id) == PROVOLATILE_VOLATILE);
1017 : }
1018 :
1019 : static bool
1020 27 : contain_volatile_functions_not_nextval_walker(Node *node, void *context)
1021 : {
1022 27 : if (node == NULL)
1023 0 : return false;
1024 : /* Check for volatile functions in node itself */
1025 27 : if (check_functions_in_node(node,
1026 : contain_volatile_functions_not_nextval_checker,
1027 : context))
1028 0 : return true;
1029 :
1030 : /*
1031 : * See notes in contain_mutable_functions_walker about why we treat
1032 : * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
1033 : * SQLValueFunction is stable. Hence, none of them are of interest here.
1034 : * Also, since we're intentionally ignoring nextval(), presumably we
1035 : * should ignore NextValueExpr.
1036 : */
1037 :
1038 : /* Recurse to check arguments */
1039 27 : if (IsA(node, Query))
1040 : {
1041 : /* Recurse into subselects */
1042 0 : return query_tree_walker((Query *) node,
1043 : contain_volatile_functions_not_nextval_walker,
1044 : context, 0);
1045 : }
1046 27 : return expression_tree_walker(node,
1047 : contain_volatile_functions_not_nextval_walker,
1048 : context);
1049 : }
1050 :
1051 :
1052 : /*****************************************************************************
1053 : * Check queries for parallel unsafe and/or restricted constructs
1054 : *****************************************************************************/
1055 :
1056 : /*
1057 : * max_parallel_hazard
1058 : * Find the worst parallel-hazard level in the given query
1059 : *
1060 : * Returns the worst function hazard property (the earliest in this list:
1061 : * PROPARALLEL_UNSAFE, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE) that can
1062 : * be found in the given parsetree. We use this to find out whether the query
1063 : * can be parallelized at all. The caller will also save the result in
1064 : * PlannerGlobal so as to short-circuit checks of portions of the querytree
1065 : * later, in the common case where everything is SAFE.
1066 : */
1067 : char
1068 16864 : max_parallel_hazard(Query *parse)
1069 : {
1070 : max_parallel_hazard_context context;
1071 :
1072 16864 : context.max_hazard = PROPARALLEL_SAFE;
1073 16864 : context.max_interesting = PROPARALLEL_UNSAFE;
1074 16864 : context.safe_param_ids = NIL;
1075 16864 : (void) max_parallel_hazard_walker((Node *) parse, &context);
1076 16864 : return context.max_hazard;
1077 : }
1078 :
1079 : /*
1080 : * is_parallel_safe
1081 : * Detect whether the given expr contains only parallel-safe functions
1082 : *
1083 : * root->glob->maxParallelHazard must previously have been set to the
1084 : * result of max_parallel_hazard() on the whole query.
1085 : */
1086 : bool
1087 85120 : is_parallel_safe(PlannerInfo *root, Node *node)
1088 : {
1089 : max_parallel_hazard_context context;
1090 :
1091 : /*
1092 : * Even if the original querytree contained nothing unsafe, we need to
1093 : * search the expression if we have generated any PARAM_EXEC Params while
1094 : * planning, because those are parallel-restricted and there might be one
1095 : * in this expression. But otherwise we don't need to look.
1096 : */
1097 148864 : if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
1098 63744 : root->glob->nParamExec == 0)
1099 62502 : return true;
1100 : /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
1101 22618 : context.max_hazard = PROPARALLEL_SAFE;
1102 22618 : context.max_interesting = PROPARALLEL_RESTRICTED;
1103 22618 : context.safe_param_ids = NIL;
1104 22618 : return !max_parallel_hazard_walker(node, &context);
1105 : }
1106 :
1107 : /* core logic for all parallel-hazard checks */
1108 : static bool
1109 58760 : max_parallel_hazard_test(char proparallel, max_parallel_hazard_context *context)
1110 : {
1111 58760 : switch (proparallel)
1112 : {
1113 : case PROPARALLEL_SAFE:
1114 : /* nothing to see here, move along */
1115 43293 : break;
1116 : case PROPARALLEL_RESTRICTED:
1117 : /* increase max_hazard to RESTRICTED */
1118 12038 : Assert(context->max_hazard != PROPARALLEL_UNSAFE);
1119 12038 : context->max_hazard = proparallel;
1120 : /* done if we are not expecting any unsafe functions */
1121 12038 : if (context->max_interesting == proparallel)
1122 5728 : return true;
1123 6310 : break;
1124 : case PROPARALLEL_UNSAFE:
1125 3429 : context->max_hazard = proparallel;
1126 : /* we're always done at the first unsafe construct */
1127 3429 : return true;
1128 : default:
1129 0 : elog(ERROR, "unrecognized proparallel value \"%c\"", proparallel);
1130 : break;
1131 : }
1132 49603 : return false;
1133 : }
1134 :
1135 : /* check_functions_in_node callback */
1136 : static bool
1137 47378 : max_parallel_hazard_checker(Oid func_id, void *context)
1138 : {
1139 47378 : return max_parallel_hazard_test(func_parallel(func_id),
1140 : (max_parallel_hazard_context *) context);
1141 : }
1142 :
1143 : static bool
1144 643344 : max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
1145 : {
1146 643344 : if (node == NULL)
1147 205528 : return false;
1148 :
1149 : /* Check for hazardous functions in node itself */
1150 437816 : if (check_functions_in_node(node, max_parallel_hazard_checker,
1151 : context))
1152 3694 : return true;
1153 :
1154 : /*
1155 : * It should be OK to treat MinMaxExpr as parallel-safe, since btree
1156 : * opclass support functions are generally parallel-safe. XmlExpr is a
1157 : * bit more dubious but we can probably get away with it. We err on the
1158 : * side of caution by treating CoerceToDomain as parallel-restricted.
1159 : * (Note: in principle that's wrong because a domain constraint could
1160 : * contain a parallel-unsafe function; but useful constraints probably
1161 : * never would have such, and assuming they do would cripple use of
1162 : * parallel query in the presence of domain types.) SQLValueFunction
1163 : * should be safe in all cases. NextValueExpr is parallel-unsafe.
1164 : */
1165 434122 : if (IsA(node, CoerceToDomain))
1166 : {
1167 1783 : if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1168 164 : return true;
1169 : }
1170 :
1171 433958 : if (IsA(node, NextValueExpr))
1172 : {
1173 0 : if (max_parallel_hazard_test(PROPARALLEL_UNSAFE, context))
1174 0 : return true;
1175 : }
1176 :
1177 : /*
1178 : * As a notational convenience for callers, look through RestrictInfo.
1179 : */
1180 433958 : else if (IsA(node, RestrictInfo))
1181 : {
1182 5470 : RestrictInfo *rinfo = (RestrictInfo *) node;
1183 :
1184 5470 : return max_parallel_hazard_walker((Node *) rinfo->clause, context);
1185 : }
1186 :
1187 : /*
1188 : * Really we should not see SubLink during a max_interesting == restricted
1189 : * scan, but if we do, return true.
1190 : */
1191 428488 : else if (IsA(node, SubLink))
1192 : {
1193 1721 : if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1194 0 : return true;
1195 : }
1196 :
1197 : /*
1198 : * Only parallel-safe SubPlans can be sent to workers. Within the
1199 : * testexpr of the SubPlan, Params representing the output columns of the
1200 : * subplan can be treated as parallel-safe, so temporarily add their IDs
1201 : * to the safe_param_ids list while examining the testexpr.
1202 : */
1203 426767 : else if (IsA(node, SubPlan))
1204 : {
1205 1840 : SubPlan *subplan = (SubPlan *) node;
1206 : List *save_safe_param_ids;
1207 :
1208 3665 : if (!subplan->parallel_safe &&
1209 1825 : max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1210 1825 : return true;
1211 15 : save_safe_param_ids = context->safe_param_ids;
1212 15 : context->safe_param_ids = list_concat(list_copy(subplan->paramIds),
1213 : context->safe_param_ids);
1214 15 : if (max_parallel_hazard_walker(subplan->testexpr, context))
1215 1 : return true; /* no need to restore safe_param_ids */
1216 14 : context->safe_param_ids = save_safe_param_ids;
1217 : /* we must also check args, but no special Param treatment there */
1218 14 : if (max_parallel_hazard_walker((Node *) subplan->args, context))
1219 0 : return true;
1220 : /* don't want to recurse normally, so we're done */
1221 14 : return false;
1222 : }
1223 :
1224 : /*
1225 : * We can't pass Params to workers at the moment either, so they are also
1226 : * parallel-restricted, unless they are PARAM_EXEC Params listed in
1227 : * safe_param_ids, meaning they could be generated within the worker.
1228 : */
1229 424927 : else if (IsA(node, Param))
1230 : {
1231 6070 : Param *param = (Param *) node;
1232 :
1233 7895 : if (param->paramkind != PARAM_EXEC ||
1234 1825 : !list_member_int(context->safe_param_ids, param->paramid))
1235 : {
1236 6053 : if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1237 3474 : return true;
1238 : }
1239 2596 : return false; /* nothing to recurse to */
1240 : }
1241 :
1242 : /*
1243 : * When we're first invoked on a completely unplanned tree, we must
1244 : * recurse into subqueries so to as to locate parallel-unsafe constructs
1245 : * anywhere in the tree.
1246 : */
1247 418857 : else if (IsA(node, Query))
1248 : {
1249 20831 : Query *query = (Query *) node;
1250 :
1251 : /* SELECT FOR UPDATE/SHARE must be treated as unsafe */
1252 20831 : if (query->rowMarks != NULL)
1253 : {
1254 15 : context->max_hazard = PROPARALLEL_UNSAFE;
1255 15 : return true;
1256 : }
1257 :
1258 : /* Recurse into subselects */
1259 20816 : return query_tree_walker(query,
1260 : max_parallel_hazard_walker,
1261 : context, 0);
1262 : }
1263 :
1264 : /* Recurse to check arguments */
1265 399747 : return expression_tree_walker(node,
1266 : max_parallel_hazard_walker,
1267 : context);
1268 : }
1269 :
1270 :
1271 : /*****************************************************************************
1272 : * Check clauses for nonstrict functions
1273 : *****************************************************************************/
1274 :
1275 : /*
1276 : * contain_nonstrict_functions
1277 : * Recursively search for nonstrict functions within a clause.
1278 : *
1279 : * Returns true if any nonstrict construct is found --- ie, anything that
1280 : * could produce non-NULL output with a NULL input.
1281 : *
1282 : * The idea here is that the caller has verified that the expression contains
1283 : * one or more Var or Param nodes (as appropriate for the caller's need), and
1284 : * now wishes to prove that the expression result will be NULL if any of these
1285 : * inputs is NULL. If we return false, then the proof succeeded.
1286 : */
1287 : bool
1288 667 : contain_nonstrict_functions(Node *clause)
1289 : {
1290 667 : return contain_nonstrict_functions_walker(clause, NULL);
1291 : }
1292 :
1293 : static bool
1294 178 : contain_nonstrict_functions_checker(Oid func_id, void *context)
1295 : {
1296 178 : return !func_strict(func_id);
1297 : }
1298 :
1299 : static bool
1300 881 : contain_nonstrict_functions_walker(Node *node, void *context)
1301 : {
1302 881 : if (node == NULL)
1303 1 : return false;
1304 880 : if (IsA(node, Aggref))
1305 : {
1306 : /* an aggregate could return non-null with null input */
1307 0 : return true;
1308 : }
1309 880 : if (IsA(node, GroupingFunc))
1310 : {
1311 : /*
1312 : * A GroupingFunc doesn't evaluate its arguments, and therefore must
1313 : * be treated as nonstrict.
1314 : */
1315 0 : return true;
1316 : }
1317 880 : if (IsA(node, WindowFunc))
1318 : {
1319 : /* a window function could return non-null with null input */
1320 0 : return true;
1321 : }
1322 880 : if (IsA(node, ArrayRef))
1323 : {
1324 : /* array assignment is nonstrict, but subscripting is strict */
1325 1 : if (((ArrayRef *) node)->refassgnexpr != NULL)
1326 0 : return true;
1327 : /* else fall through to check args */
1328 : }
1329 880 : if (IsA(node, DistinctExpr))
1330 : {
1331 : /* IS DISTINCT FROM is inherently non-strict */
1332 0 : return true;
1333 : }
1334 880 : if (IsA(node, NullIfExpr))
1335 : {
1336 : /* NULLIF is inherently non-strict */
1337 0 : return true;
1338 : }
1339 880 : if (IsA(node, BoolExpr))
1340 : {
1341 2 : BoolExpr *expr = (BoolExpr *) node;
1342 :
1343 2 : switch (expr->boolop)
1344 : {
1345 : case AND_EXPR:
1346 : case OR_EXPR:
1347 : /* AND, OR are inherently non-strict */
1348 2 : return true;
1349 : default:
1350 0 : break;
1351 : }
1352 : }
1353 878 : if (IsA(node, SubLink))
1354 : {
1355 : /* In some cases a sublink might be strict, but in general not */
1356 2 : return true;
1357 : }
1358 876 : if (IsA(node, SubPlan))
1359 0 : return true;
1360 876 : if (IsA(node, AlternativeSubPlan))
1361 0 : return true;
1362 876 : if (IsA(node, FieldStore))
1363 0 : return true;
1364 876 : if (IsA(node, CaseExpr))
1365 5 : return true;
1366 871 : if (IsA(node, ArrayExpr))
1367 0 : return true;
1368 871 : if (IsA(node, RowExpr))
1369 0 : return true;
1370 871 : if (IsA(node, RowCompareExpr))
1371 0 : return true;
1372 871 : if (IsA(node, CoalesceExpr))
1373 20 : return true;
1374 851 : if (IsA(node, MinMaxExpr))
1375 2 : return true;
1376 849 : if (IsA(node, XmlExpr))
1377 0 : return true;
1378 849 : if (IsA(node, NullTest))
1379 2 : return true;
1380 847 : if (IsA(node, BooleanTest))
1381 0 : return true;
1382 :
1383 : /*
1384 : * Check other function-containing nodes; but ArrayCoerceExpr is strict at
1385 : * the array level, regardless of elemfunc.
1386 : */
1387 1694 : if (!IsA(node, ArrayCoerceExpr) &&
1388 847 : check_functions_in_node(node, contain_nonstrict_functions_checker,
1389 : context))
1390 0 : return true;
1391 847 : return expression_tree_walker(node, contain_nonstrict_functions_walker,
1392 : context);
1393 : }
1394 :
1395 : /*****************************************************************************
1396 : * Check clauses for context-dependent nodes
1397 : *****************************************************************************/
1398 :
1399 : /*
1400 : * contain_context_dependent_node
1401 : * Recursively search for context-dependent nodes within a clause.
1402 : *
1403 : * CaseTestExpr nodes must appear directly within the corresponding CaseExpr,
1404 : * not nested within another one, or they'll see the wrong test value. If one
1405 : * appears "bare" in the arguments of a SQL function, then we can't inline the
1406 : * SQL function for fear of creating such a situation.
1407 : *
1408 : * CoerceToDomainValue would have the same issue if domain CHECK expressions
1409 : * could get inlined into larger expressions, but presently that's impossible.
1410 : * Still, it might be allowed in future, or other node types with similar
1411 : * issues might get invented. So give this function a generic name, and set
1412 : * up the recursion state to allow multiple flag bits.
1413 : */
1414 : static bool
1415 2467 : contain_context_dependent_node(Node *clause)
1416 : {
1417 2467 : int flags = 0;
1418 :
1419 2467 : return contain_context_dependent_node_walker(clause, &flags);
1420 : }
1421 :
1422 : #define CCDN_IN_CASEEXPR 0x0001 /* CaseTestExpr okay here? */
1423 :
1424 : static bool
1425 2834 : contain_context_dependent_node_walker(Node *node, int *flags)
1426 : {
1427 2834 : if (node == NULL)
1428 2299 : return false;
1429 535 : if (IsA(node, CaseTestExpr))
1430 1 : return !(*flags & CCDN_IN_CASEEXPR);
1431 534 : if (IsA(node, CaseExpr))
1432 : {
1433 0 : CaseExpr *caseexpr = (CaseExpr *) node;
1434 :
1435 : /*
1436 : * If this CASE doesn't have a test expression, then it doesn't create
1437 : * a context in which CaseTestExprs should appear, so just fall
1438 : * through and treat it as a generic expression node.
1439 : */
1440 0 : if (caseexpr->arg)
1441 : {
1442 0 : int save_flags = *flags;
1443 : bool res;
1444 :
1445 : /*
1446 : * Note: in principle, we could distinguish the various sub-parts
1447 : * of a CASE construct and set the flag bit only for some of them,
1448 : * since we are only expecting CaseTestExprs to appear in the
1449 : * "expr" subtree of the CaseWhen nodes. But it doesn't really
1450 : * seem worth any extra code. If there are any bare CaseTestExprs
1451 : * elsewhere in the CASE, something's wrong already.
1452 : */
1453 0 : *flags |= CCDN_IN_CASEEXPR;
1454 0 : res = expression_tree_walker(node,
1455 : contain_context_dependent_node_walker,
1456 : (void *) flags);
1457 0 : *flags = save_flags;
1458 0 : return res;
1459 : }
1460 : }
1461 534 : return expression_tree_walker(node, contain_context_dependent_node_walker,
1462 : (void *) flags);
1463 : }
1464 :
1465 : /*****************************************************************************
1466 : * Check clauses for Vars passed to non-leakproof functions
1467 : *****************************************************************************/
1468 :
1469 : /*
1470 : * contain_leaked_vars
1471 : * Recursively scan a clause to discover whether it contains any Var
1472 : * nodes (of the current query level) that are passed as arguments to
1473 : * leaky functions.
1474 : *
1475 : * Returns true if the clause contains any non-leakproof functions that are
1476 : * passed Var nodes of the current query level, and which might therefore leak
1477 : * data. Such clauses must be applied after any lower-level security barrier
1478 : * clauses.
1479 : */
1480 : bool
1481 634 : contain_leaked_vars(Node *clause)
1482 : {
1483 634 : return contain_leaked_vars_walker(clause, NULL);
1484 : }
1485 :
1486 : static bool
1487 634 : contain_leaked_vars_checker(Oid func_id, void *context)
1488 : {
1489 634 : return !get_func_leakproof(func_id);
1490 : }
1491 :
1492 : static bool
1493 1283 : contain_leaked_vars_walker(Node *node, void *context)
1494 : {
1495 1283 : if (node == NULL)
1496 0 : return false;
1497 :
1498 1283 : switch (nodeTag(node))
1499 : {
1500 : case T_Var:
1501 : case T_Const:
1502 : case T_Param:
1503 : case T_ArrayRef:
1504 : case T_ArrayExpr:
1505 : case T_FieldSelect:
1506 : case T_FieldStore:
1507 : case T_NamedArgExpr:
1508 : case T_BoolExpr:
1509 : case T_RelabelType:
1510 : case T_CollateExpr:
1511 : case T_CaseExpr:
1512 : case T_CaseTestExpr:
1513 : case T_RowExpr:
1514 : case T_MinMaxExpr:
1515 : case T_SQLValueFunction:
1516 : case T_NullTest:
1517 : case T_BooleanTest:
1518 : case T_NextValueExpr:
1519 : case T_List:
1520 :
1521 : /*
1522 : * We know these node types don't contain function calls; but
1523 : * something further down in the node tree might.
1524 : */
1525 640 : break;
1526 :
1527 : case T_FuncExpr:
1528 : case T_OpExpr:
1529 : case T_DistinctExpr:
1530 : case T_NullIfExpr:
1531 : case T_ScalarArrayOpExpr:
1532 : case T_CoerceViaIO:
1533 : case T_ArrayCoerceExpr:
1534 :
1535 : /*
1536 : * If node contains a leaky function call, and there's any Var
1537 : * underneath it, reject.
1538 : */
1539 634 : if (check_functions_in_node(node, contain_leaked_vars_checker,
1540 301 : context) &&
1541 301 : contain_var_clause(node))
1542 296 : return true;
1543 338 : break;
1544 :
1545 : case T_RowCompareExpr:
1546 : {
1547 : /*
1548 : * It's worth special-casing this because a leaky comparison
1549 : * function only compromises one pair of row elements, which
1550 : * might not contain Vars while others do.
1551 : */
1552 0 : RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1553 : ListCell *opid;
1554 : ListCell *larg;
1555 : ListCell *rarg;
1556 :
1557 0 : forthree(opid, rcexpr->opnos,
1558 : larg, rcexpr->largs,
1559 : rarg, rcexpr->rargs)
1560 : {
1561 0 : Oid funcid = get_opcode(lfirst_oid(opid));
1562 :
1563 0 : if (!get_func_leakproof(funcid) &&
1564 0 : (contain_var_clause((Node *) lfirst(larg)) ||
1565 0 : contain_var_clause((Node *) lfirst(rarg))))
1566 0 : return true;
1567 : }
1568 : }
1569 0 : break;
1570 :
1571 : case T_CurrentOfExpr:
1572 :
1573 : /*
1574 : * WHERE CURRENT OF doesn't contain leaky function calls.
1575 : * Moreover, it is essential that this is considered non-leaky,
1576 : * since the planner must always generate a TID scan when CURRENT
1577 : * OF is present -- c.f. cost_tidscan.
1578 : */
1579 5 : return false;
1580 :
1581 : default:
1582 :
1583 : /*
1584 : * If we don't recognize the node tag, assume it might be leaky.
1585 : * This prevents an unexpected security hole if someone adds a new
1586 : * node type that can call a function.
1587 : */
1588 4 : return true;
1589 : }
1590 978 : return expression_tree_walker(node, contain_leaked_vars_walker,
1591 : context);
1592 : }
1593 :
1594 : /*
1595 : * find_nonnullable_rels
1596 : * Determine which base rels are forced nonnullable by given clause.
1597 : *
1598 : * Returns the set of all Relids that are referenced in the clause in such
1599 : * a way that the clause cannot possibly return TRUE if any of these Relids
1600 : * is an all-NULL row. (It is OK to err on the side of conservatism; hence
1601 : * the analysis here is simplistic.)
1602 : *
1603 : * The semantics here are subtly different from contain_nonstrict_functions:
1604 : * that function is concerned with NULL results from arbitrary expressions,
1605 : * but here we assume that the input is a Boolean expression, and wish to
1606 : * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1607 : * the expression to have been AND/OR flattened and converted to implicit-AND
1608 : * format.
1609 : *
1610 : * Note: this function is largely duplicative of find_nonnullable_vars().
1611 : * The reason not to simplify this function into a thin wrapper around
1612 : * find_nonnullable_vars() is that the tested conditions really are different:
1613 : * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
1614 : * that either v1 or v2 can't be NULL, but it does prove that the t1 row
1615 : * as a whole can't be all-NULL.
1616 : *
1617 : * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1618 : * the result is either FALSE or NULL is good enough. top_level is FALSE when
1619 : * we have descended below a NOT or a strict function: now we must be able to
1620 : * prove that the subexpression goes to NULL.
1621 : *
1622 : * We don't use expression_tree_walker here because we don't want to descend
1623 : * through very many kinds of nodes; only the ones we can be sure are strict.
1624 : */
1625 : Relids
1626 3281 : find_nonnullable_rels(Node *clause)
1627 : {
1628 3281 : return find_nonnullable_rels_walker(clause, true);
1629 : }
1630 :
1631 : static Relids
1632 18857 : find_nonnullable_rels_walker(Node *node, bool top_level)
1633 : {
1634 18857 : Relids result = NULL;
1635 : ListCell *l;
1636 :
1637 18857 : if (node == NULL)
1638 561 : return NULL;
1639 18296 : if (IsA(node, Var))
1640 : {
1641 5806 : Var *var = (Var *) node;
1642 :
1643 5806 : if (var->varlevelsup == 0)
1644 5806 : result = bms_make_singleton(var->varno);
1645 : }
1646 12490 : else if (IsA(node, List))
1647 : {
1648 : /*
1649 : * At top level, we are examining an implicit-AND list: if any of the
1650 : * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1651 : * not at top level, we are examining the arguments of a strict
1652 : * function: if any of them produce NULL then the result of the
1653 : * function must be NULL. So in both cases, the set of nonnullable
1654 : * rels is the union of those found in the arms, and we pass down the
1655 : * top_level flag unmodified.
1656 : */
1657 17951 : foreach(l, (List *) node)
1658 : {
1659 22660 : result = bms_join(result,
1660 11330 : find_nonnullable_rels_walker(lfirst(l),
1661 : top_level));
1662 : }
1663 : }
1664 5869 : else if (IsA(node, FuncExpr))
1665 : {
1666 371 : FuncExpr *expr = (FuncExpr *) node;
1667 :
1668 371 : if (func_strict(expr->funcid))
1669 371 : result = find_nonnullable_rels_walker((Node *) expr->args, false);
1670 : }
1671 5498 : else if (IsA(node, OpExpr))
1672 : {
1673 3186 : OpExpr *expr = (OpExpr *) node;
1674 :
1675 3186 : set_opfuncid(expr);
1676 3186 : if (func_strict(expr->opfuncid))
1677 3186 : result = find_nonnullable_rels_walker((Node *) expr->args, false);
1678 : }
1679 2312 : else if (IsA(node, ScalarArrayOpExpr))
1680 : {
1681 272 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1682 :
1683 272 : if (is_strict_saop(expr, true))
1684 272 : result = find_nonnullable_rels_walker((Node *) expr->args, false);
1685 : }
1686 2040 : else if (IsA(node, BoolExpr))
1687 : {
1688 160 : BoolExpr *expr = (BoolExpr *) node;
1689 :
1690 160 : switch (expr->boolop)
1691 : {
1692 : case AND_EXPR:
1693 : /* At top level we can just recurse (to the List case) */
1694 3 : if (top_level)
1695 : {
1696 3 : result = find_nonnullable_rels_walker((Node *) expr->args,
1697 : top_level);
1698 3 : break;
1699 : }
1700 :
1701 : /*
1702 : * Below top level, even if one arm produces NULL, the result
1703 : * could be FALSE (hence not NULL). However, if *all* the
1704 : * arms produce NULL then the result is NULL, so we can take
1705 : * the intersection of the sets of nonnullable rels, just as
1706 : * for OR. Fall through to share code.
1707 : */
1708 : /* FALL THRU */
1709 : case OR_EXPR:
1710 :
1711 : /*
1712 : * OR is strict if all of its arms are, so we can take the
1713 : * intersection of the sets of nonnullable rels for each arm.
1714 : * This works for both values of top_level.
1715 : */
1716 221 : foreach(l, expr->args)
1717 : {
1718 : Relids subresult;
1719 :
1720 181 : subresult = find_nonnullable_rels_walker(lfirst(l),
1721 : top_level);
1722 181 : if (result == NULL) /* first subresult? */
1723 88 : result = subresult;
1724 : else
1725 93 : result = bms_int_members(result, subresult);
1726 :
1727 : /*
1728 : * If the intersection is empty, we can stop looking. This
1729 : * also justifies the test for first-subresult above.
1730 : */
1731 181 : if (bms_is_empty(result))
1732 48 : break;
1733 : }
1734 88 : break;
1735 : case NOT_EXPR:
1736 : /* NOT will return null if its arg is null */
1737 69 : result = find_nonnullable_rels_walker((Node *) expr->args,
1738 : false);
1739 69 : break;
1740 : default:
1741 0 : elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1742 : break;
1743 : }
1744 : }
1745 1880 : else if (IsA(node, RelabelType))
1746 : {
1747 87 : RelabelType *expr = (RelabelType *) node;
1748 :
1749 87 : result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1750 : }
1751 1793 : else if (IsA(node, CoerceViaIO))
1752 : {
1753 : /* not clear this is useful, but it can't hurt */
1754 0 : CoerceViaIO *expr = (CoerceViaIO *) node;
1755 :
1756 0 : result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1757 : }
1758 1793 : else if (IsA(node, ArrayCoerceExpr))
1759 : {
1760 : /* ArrayCoerceExpr is strict at the array level */
1761 0 : ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1762 :
1763 0 : result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1764 : }
1765 1793 : else if (IsA(node, ConvertRowtypeExpr))
1766 : {
1767 : /* not clear this is useful, but it can't hurt */
1768 0 : ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1769 :
1770 0 : result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1771 : }
1772 1793 : else if (IsA(node, CollateExpr))
1773 : {
1774 0 : CollateExpr *expr = (CollateExpr *) node;
1775 :
1776 0 : result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1777 : }
1778 1793 : else if (IsA(node, NullTest))
1779 : {
1780 : /* IS NOT NULL can be considered strict, but only at top level */
1781 112 : NullTest *expr = (NullTest *) node;
1782 :
1783 112 : if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1784 54 : result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1785 : }
1786 1681 : else if (IsA(node, BooleanTest))
1787 : {
1788 : /* Boolean tests that reject NULL are strict at top level */
1789 2 : BooleanTest *expr = (BooleanTest *) node;
1790 :
1791 4 : if (top_level &&
1792 4 : (expr->booltesttype == IS_TRUE ||
1793 4 : expr->booltesttype == IS_FALSE ||
1794 2 : expr->booltesttype == IS_NOT_UNKNOWN))
1795 0 : result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1796 : }
1797 1679 : else if (IsA(node, PlaceHolderVar))
1798 : {
1799 23 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
1800 :
1801 23 : result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
1802 : }
1803 18296 : return result;
1804 : }
1805 :
1806 : /*
1807 : * find_nonnullable_vars
1808 : * Determine which Vars are forced nonnullable by given clause.
1809 : *
1810 : * Returns a list of all level-zero Vars that are referenced in the clause in
1811 : * such a way that the clause cannot possibly return TRUE if any of these Vars
1812 : * is NULL. (It is OK to err on the side of conservatism; hence the analysis
1813 : * here is simplistic.)
1814 : *
1815 : * The semantics here are subtly different from contain_nonstrict_functions:
1816 : * that function is concerned with NULL results from arbitrary expressions,
1817 : * but here we assume that the input is a Boolean expression, and wish to
1818 : * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1819 : * the expression to have been AND/OR flattened and converted to implicit-AND
1820 : * format.
1821 : *
1822 : * The result is a palloc'd List, but we have not copied the member Var nodes.
1823 : * Also, we don't bother trying to eliminate duplicate entries.
1824 : *
1825 : * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1826 : * the result is either FALSE or NULL is good enough. top_level is FALSE when
1827 : * we have descended below a NOT or a strict function: now we must be able to
1828 : * prove that the subexpression goes to NULL.
1829 : *
1830 : * We don't use expression_tree_walker here because we don't want to descend
1831 : * through very many kinds of nodes; only the ones we can be sure are strict.
1832 : */
1833 : List *
1834 2850 : find_nonnullable_vars(Node *clause)
1835 : {
1836 2850 : return find_nonnullable_vars_walker(clause, true);
1837 : }
1838 :
1839 : static List *
1840 16012 : find_nonnullable_vars_walker(Node *node, bool top_level)
1841 : {
1842 16012 : List *result = NIL;
1843 : ListCell *l;
1844 :
1845 16012 : if (node == NULL)
1846 566 : return NIL;
1847 15446 : if (IsA(node, Var))
1848 : {
1849 4767 : Var *var = (Var *) node;
1850 :
1851 4767 : if (var->varlevelsup == 0)
1852 4767 : result = list_make1(var);
1853 : }
1854 10679 : else if (IsA(node, List))
1855 : {
1856 : /*
1857 : * At top level, we are examining an implicit-AND list: if any of the
1858 : * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1859 : * not at top level, we are examining the arguments of a strict
1860 : * function: if any of them produce NULL then the result of the
1861 : * function must be NULL. So in both cases, the set of nonnullable
1862 : * vars is the union of those found in the arms, and we pass down the
1863 : * top_level flag unmodified.
1864 : */
1865 15205 : foreach(l, (List *) node)
1866 : {
1867 19210 : result = list_concat(result,
1868 9605 : find_nonnullable_vars_walker(lfirst(l),
1869 : top_level));
1870 : }
1871 : }
1872 5079 : else if (IsA(node, FuncExpr))
1873 : {
1874 350 : FuncExpr *expr = (FuncExpr *) node;
1875 :
1876 350 : if (func_strict(expr->funcid))
1877 350 : result = find_nonnullable_vars_walker((Node *) expr->args, false);
1878 : }
1879 4729 : else if (IsA(node, OpExpr))
1880 : {
1881 2622 : OpExpr *expr = (OpExpr *) node;
1882 :
1883 2622 : set_opfuncid(expr);
1884 2622 : if (func_strict(expr->opfuncid))
1885 2622 : result = find_nonnullable_vars_walker((Node *) expr->args, false);
1886 : }
1887 2107 : else if (IsA(node, ScalarArrayOpExpr))
1888 : {
1889 272 : ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1890 :
1891 272 : if (is_strict_saop(expr, true))
1892 272 : result = find_nonnullable_vars_walker((Node *) expr->args, false);
1893 : }
1894 1835 : else if (IsA(node, BoolExpr))
1895 : {
1896 142 : BoolExpr *expr = (BoolExpr *) node;
1897 :
1898 142 : switch (expr->boolop)
1899 : {
1900 : case AND_EXPR:
1901 : /* At top level we can just recurse (to the List case) */
1902 3 : if (top_level)
1903 : {
1904 3 : result = find_nonnullable_vars_walker((Node *) expr->args,
1905 : top_level);
1906 3 : break;
1907 : }
1908 :
1909 : /*
1910 : * Below top level, even if one arm produces NULL, the result
1911 : * could be FALSE (hence not NULL). However, if *all* the
1912 : * arms produce NULL then the result is NULL, so we can take
1913 : * the intersection of the sets of nonnullable vars, just as
1914 : * for OR. Fall through to share code.
1915 : */
1916 : /* FALL THRU */
1917 : case OR_EXPR:
1918 :
1919 : /*
1920 : * OR is strict if all of its arms are, so we can take the
1921 : * intersection of the sets of nonnullable vars for each arm.
1922 : * This works for both values of top_level.
1923 : */
1924 131 : foreach(l, expr->args)
1925 : {
1926 : List *subresult;
1927 :
1928 131 : subresult = find_nonnullable_vars_walker(lfirst(l),
1929 : top_level);
1930 131 : if (result == NIL) /* first subresult? */
1931 70 : result = subresult;
1932 : else
1933 61 : result = list_intersection(result, subresult);
1934 :
1935 : /*
1936 : * If the intersection is empty, we can stop looking. This
1937 : * also justifies the test for first-subresult above.
1938 : */
1939 131 : if (result == NIL)
1940 70 : break;
1941 : }
1942 70 : break;
1943 : case NOT_EXPR:
1944 : /* NOT will return null if its arg is null */
1945 69 : result = find_nonnullable_vars_walker((Node *) expr->args,
1946 : false);
1947 69 : break;
1948 : default:
1949 0 : elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1950 : break;
1951 : }
1952 : }
1953 1693 : else if (IsA(node, RelabelType))
1954 : {
1955 43 : RelabelType *expr = (RelabelType *) node;
1956 :
1957 43 : result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1958 : }
1959 1650 : else if (IsA(node, CoerceViaIO))
1960 : {
1961 : /* not clear this is useful, but it can't hurt */
1962 0 : CoerceViaIO *expr = (CoerceViaIO *) node;
1963 :
1964 0 : result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1965 : }
1966 1650 : else if (IsA(node, ArrayCoerceExpr))
1967 : {
1968 : /* ArrayCoerceExpr is strict at the array level */
1969 0 : ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1970 :
1971 0 : result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1972 : }
1973 1650 : else if (IsA(node, ConvertRowtypeExpr))
1974 : {
1975 : /* not clear this is useful, but it can't hurt */
1976 0 : ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1977 :
1978 0 : result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1979 : }
1980 1650 : else if (IsA(node, CollateExpr))
1981 : {
1982 0 : CollateExpr *expr = (CollateExpr *) node;
1983 :
1984 0 : result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1985 : }
1986 1650 : else if (IsA(node, NullTest))
1987 : {
1988 : /* IS NOT NULL can be considered strict, but only at top level */
1989 103 : NullTest *expr = (NullTest *) node;
1990 :
1991 103 : if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1992 51 : result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1993 : }
1994 1547 : else if (IsA(node, BooleanTest))
1995 : {
1996 : /* Boolean tests that reject NULL are strict at top level */
1997 2 : BooleanTest *expr = (BooleanTest *) node;
1998 :
1999 4 : if (top_level &&
2000 4 : (expr->booltesttype == IS_TRUE ||
2001 4 : expr->booltesttype == IS_FALSE ||
2002 2 : expr->booltesttype == IS_NOT_UNKNOWN))
2003 0 : result = find_nonnullable_vars_walker((Node *) expr->arg, false);
2004 : }
2005 1545 : else if (IsA(node, PlaceHolderVar))
2006 : {
2007 16 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
2008 :
2009 16 : result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
2010 : }
2011 15446 : return result;
2012 : }
2013 :
2014 : /*
2015 : * find_forced_null_vars
2016 : * Determine which Vars must be NULL for the given clause to return TRUE.
2017 : *
2018 : * This is the complement of find_nonnullable_vars: find the level-zero Vars
2019 : * that must be NULL for the clause to return TRUE. (It is OK to err on the
2020 : * side of conservatism; hence the analysis here is simplistic. In fact,
2021 : * we only detect simple "var IS NULL" tests at the top level.)
2022 : *
2023 : * The result is a palloc'd List, but we have not copied the member Var nodes.
2024 : * Also, we don't bother trying to eliminate duplicate entries.
2025 : */
2026 : List *
2027 3653 : find_forced_null_vars(Node *node)
2028 : {
2029 3653 : List *result = NIL;
2030 : Var *var;
2031 : ListCell *l;
2032 :
2033 3653 : if (node == NULL)
2034 550 : return NIL;
2035 : /* Check single-clause cases using subroutine */
2036 3103 : var = find_forced_null_var(node);
2037 3103 : if (var)
2038 : {
2039 40 : result = list_make1(var);
2040 : }
2041 : /* Otherwise, handle AND-conditions */
2042 3063 : else if (IsA(node, List))
2043 : {
2044 : /*
2045 : * At top level, we are examining an implicit-AND list: if any of the
2046 : * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
2047 : */
2048 3103 : foreach(l, (List *) node)
2049 : {
2050 1865 : result = list_concat(result,
2051 1865 : find_forced_null_vars(lfirst(l)));
2052 : }
2053 : }
2054 1825 : else if (IsA(node, BoolExpr))
2055 : {
2056 127 : BoolExpr *expr = (BoolExpr *) node;
2057 :
2058 : /*
2059 : * We don't bother considering the OR case, because it's fairly
2060 : * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
2061 : * the NOT case isn't worth expending code on.
2062 : */
2063 127 : if (expr->boolop == AND_EXPR)
2064 : {
2065 : /* At top level we can just recurse (to the List case) */
2066 0 : result = find_forced_null_vars((Node *) expr->args);
2067 : }
2068 : }
2069 3103 : return result;
2070 : }
2071 :
2072 : /*
2073 : * find_forced_null_var
2074 : * Return the Var forced null by the given clause, or NULL if it's
2075 : * not an IS NULL-type clause. For success, the clause must enforce
2076 : * *only* nullness of the particular Var, not any other conditions.
2077 : *
2078 : * This is just the single-clause case of find_forced_null_vars(), without
2079 : * any allowance for AND conditions. It's used by initsplan.c on individual
2080 : * qual clauses. The reason for not just applying find_forced_null_vars()
2081 : * is that if an AND of an IS NULL clause with something else were to somehow
2082 : * survive AND/OR flattening, initsplan.c might get fooled into discarding
2083 : * the whole clause when only the IS NULL part of it had been proved redundant.
2084 : */
2085 : Var *
2086 3152 : find_forced_null_var(Node *node)
2087 : {
2088 3152 : if (node == NULL)
2089 0 : return NULL;
2090 3152 : if (IsA(node, NullTest))
2091 : {
2092 : /* check for var IS NULL */
2093 116 : NullTest *expr = (NullTest *) node;
2094 :
2095 116 : if (expr->nulltesttype == IS_NULL && !expr->argisrow)
2096 : {
2097 78 : Var *var = (Var *) expr->arg;
2098 :
2099 156 : if (var && IsA(var, Var) &&
2100 78 : var->varlevelsup == 0)
2101 78 : return var;
2102 : }
2103 : }
2104 3036 : else if (IsA(node, BooleanTest))
2105 : {
2106 : /* var IS UNKNOWN is equivalent to var IS NULL */
2107 4 : BooleanTest *expr = (BooleanTest *) node;
2108 :
2109 4 : if (expr->booltesttype == IS_UNKNOWN)
2110 : {
2111 0 : Var *var = (Var *) expr->arg;
2112 :
2113 0 : if (var && IsA(var, Var) &&
2114 0 : var->varlevelsup == 0)
2115 0 : return var;
2116 : }
2117 : }
2118 3074 : return NULL;
2119 : }
2120 :
2121 : /*
2122 : * Can we treat a ScalarArrayOpExpr as strict?
2123 : *
2124 : * If "falseOK" is true, then a "false" result can be considered strict,
2125 : * else we need to guarantee an actual NULL result for NULL input.
2126 : *
2127 : * "foo op ALL array" is strict if the op is strict *and* we can prove
2128 : * that the array input isn't an empty array. We can check that
2129 : * for the cases of an array constant and an ARRAY[] construct.
2130 : *
2131 : * "foo op ANY array" is strict in the falseOK sense if the op is strict.
2132 : * If not falseOK, the test is the same as for "foo op ALL array".
2133 : */
2134 : static bool
2135 544 : is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
2136 : {
2137 : Node *rightop;
2138 :
2139 : /* The contained operator must be strict. */
2140 544 : set_sa_opfuncid(expr);
2141 544 : if (!func_strict(expr->opfuncid))
2142 0 : return false;
2143 : /* If ANY and falseOK, that's all we need to check. */
2144 544 : if (expr->useOr && falseOK)
2145 528 : return true;
2146 : /* Else, we have to see if the array is provably non-empty. */
2147 16 : Assert(list_length(expr->args) == 2);
2148 16 : rightop = (Node *) lsecond(expr->args);
2149 16 : if (rightop && IsA(rightop, Const))
2150 0 : {
2151 16 : Datum arraydatum = ((Const *) rightop)->constvalue;
2152 16 : bool arrayisnull = ((Const *) rightop)->constisnull;
2153 : ArrayType *arrayval;
2154 : int nitems;
2155 :
2156 16 : if (arrayisnull)
2157 0 : return false;
2158 16 : arrayval = DatumGetArrayTypeP(arraydatum);
2159 16 : nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2160 16 : if (nitems > 0)
2161 16 : return true;
2162 : }
2163 0 : else if (rightop && IsA(rightop, ArrayExpr))
2164 : {
2165 0 : ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
2166 :
2167 0 : if (arrayexpr->elements != NIL && !arrayexpr->multidims)
2168 0 : return true;
2169 : }
2170 0 : return false;
2171 : }
2172 :
2173 :
2174 : /*****************************************************************************
2175 : * Check for "pseudo-constant" clauses
2176 : *****************************************************************************/
2177 :
2178 : /*
2179 : * is_pseudo_constant_clause
2180 : * Detect whether an expression is "pseudo constant", ie, it contains no
2181 : * variables of the current query level and no uses of volatile functions.
2182 : * Such an expr is not necessarily a true constant: it can still contain
2183 : * Params and outer-level Vars, not to mention functions whose results
2184 : * may vary from one statement to the next. However, the expr's value
2185 : * will be constant over any one scan of the current query, so it can be
2186 : * used as, eg, an indexscan key.
2187 : *
2188 : * CAUTION: this function omits to test for one very important class of
2189 : * not-constant expressions, namely aggregates (Aggrefs). In current usage
2190 : * this is only applied to WHERE clauses and so a check for Aggrefs would be
2191 : * a waste of cycles; but be sure to also check contain_agg_clause() if you
2192 : * want to know about pseudo-constness in other contexts. The same goes
2193 : * for window functions (WindowFuncs).
2194 : */
2195 : bool
2196 138 : is_pseudo_constant_clause(Node *clause)
2197 : {
2198 : /*
2199 : * We could implement this check in one recursive scan. But since the
2200 : * check for volatile functions is both moderately expensive and unlikely
2201 : * to fail, it seems better to look for Vars first and only check for
2202 : * volatile functions if we find no Vars.
2203 : */
2204 276 : if (!contain_var_clause(clause) &&
2205 138 : !contain_volatile_functions(clause))
2206 138 : return true;
2207 0 : return false;
2208 : }
2209 :
2210 : /*
2211 : * is_pseudo_constant_clause_relids
2212 : * Same as above, except caller already has available the var membership
2213 : * of the expression; this lets us avoid the contain_var_clause() scan.
2214 : */
2215 : bool
2216 12781 : is_pseudo_constant_clause_relids(Node *clause, Relids relids)
2217 : {
2218 24657 : if (bms_is_empty(relids) &&
2219 11876 : !contain_volatile_functions(clause))
2220 11876 : return true;
2221 905 : return false;
2222 : }
2223 :
2224 :
2225 : /*****************************************************************************
2226 : * *
2227 : * General clause-manipulating routines *
2228 : * *
2229 : *****************************************************************************/
2230 :
2231 : /*
2232 : * NumRelids
2233 : * (formerly clause_relids)
2234 : *
2235 : * Returns the number of different relations referenced in 'clause'.
2236 : */
2237 : int
2238 132 : NumRelids(Node *clause)
2239 : {
2240 132 : Relids varnos = pull_varnos(clause);
2241 132 : int result = bms_num_members(varnos);
2242 :
2243 132 : bms_free(varnos);
2244 132 : return result;
2245 : }
2246 :
2247 : /*
2248 : * CommuteOpExpr: commute a binary operator clause
2249 : *
2250 : * XXX the clause is destructively modified!
2251 : */
2252 : void
2253 1015 : CommuteOpExpr(OpExpr *clause)
2254 : {
2255 : Oid opoid;
2256 : Node *temp;
2257 :
2258 : /* Sanity checks: caller is at fault if these fail */
2259 2030 : if (!is_opclause(clause) ||
2260 1015 : list_length(clause->args) != 2)
2261 0 : elog(ERROR, "cannot commute non-binary-operator clause");
2262 :
2263 1015 : opoid = get_commutator(clause->opno);
2264 :
2265 1015 : if (!OidIsValid(opoid))
2266 0 : elog(ERROR, "could not find commutator for operator %u",
2267 : clause->opno);
2268 :
2269 : /*
2270 : * modify the clause in-place!
2271 : */
2272 1015 : clause->opno = opoid;
2273 1015 : clause->opfuncid = InvalidOid;
2274 : /* opresulttype, opretset, opcollid, inputcollid need not change */
2275 :
2276 1015 : temp = linitial(clause->args);
2277 1015 : linitial(clause->args) = lsecond(clause->args);
2278 1015 : lsecond(clause->args) = temp;
2279 1015 : }
2280 :
2281 : /*
2282 : * CommuteRowCompareExpr: commute a RowCompareExpr clause
2283 : *
2284 : * XXX the clause is destructively modified!
2285 : */
2286 : void
2287 0 : CommuteRowCompareExpr(RowCompareExpr *clause)
2288 : {
2289 : List *newops;
2290 : List *temp;
2291 : ListCell *l;
2292 :
2293 : /* Sanity checks: caller is at fault if these fail */
2294 0 : if (!IsA(clause, RowCompareExpr))
2295 0 : elog(ERROR, "expected a RowCompareExpr");
2296 :
2297 : /* Build list of commuted operators */
2298 0 : newops = NIL;
2299 0 : foreach(l, clause->opnos)
2300 : {
2301 0 : Oid opoid = lfirst_oid(l);
2302 :
2303 0 : opoid = get_commutator(opoid);
2304 0 : if (!OidIsValid(opoid))
2305 0 : elog(ERROR, "could not find commutator for operator %u",
2306 : lfirst_oid(l));
2307 0 : newops = lappend_oid(newops, opoid);
2308 : }
2309 :
2310 : /*
2311 : * modify the clause in-place!
2312 : */
2313 0 : switch (clause->rctype)
2314 : {
2315 : case ROWCOMPARE_LT:
2316 0 : clause->rctype = ROWCOMPARE_GT;
2317 0 : break;
2318 : case ROWCOMPARE_LE:
2319 0 : clause->rctype = ROWCOMPARE_GE;
2320 0 : break;
2321 : case ROWCOMPARE_GE:
2322 0 : clause->rctype = ROWCOMPARE_LE;
2323 0 : break;
2324 : case ROWCOMPARE_GT:
2325 0 : clause->rctype = ROWCOMPARE_LT;
2326 0 : break;
2327 : default:
2328 0 : elog(ERROR, "unexpected RowCompare type: %d",
2329 : (int) clause->rctype);
2330 : break;
2331 : }
2332 :
2333 0 : clause->opnos = newops;
2334 :
2335 : /*
2336 : * Note: we need not change the opfamilies list; we assume any btree
2337 : * opfamily containing an operator will also contain its commutator.
2338 : * Collations don't change either.
2339 : */
2340 :
2341 0 : temp = clause->largs;
2342 0 : clause->largs = clause->rargs;
2343 0 : clause->rargs = temp;
2344 0 : }
2345 :
2346 : /*
2347 : * Helper for eval_const_expressions: check that datatype of an attribute
2348 : * is still what it was when the expression was parsed. This is needed to
2349 : * guard against improper simplification after ALTER COLUMN TYPE. (XXX we
2350 : * may well need to make similar checks elsewhere?)
2351 : */
2352 : static bool
2353 12 : rowtype_field_matches(Oid rowtypeid, int fieldnum,
2354 : Oid expectedtype, int32 expectedtypmod,
2355 : Oid expectedcollation)
2356 : {
2357 : TupleDesc tupdesc;
2358 : Form_pg_attribute attr;
2359 :
2360 : /* No issue for RECORD, since there is no way to ALTER such a type */
2361 12 : if (rowtypeid == RECORDOID)
2362 0 : return true;
2363 12 : tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
2364 12 : if (fieldnum <= 0 || fieldnum > tupdesc->natts)
2365 : {
2366 0 : ReleaseTupleDesc(tupdesc);
2367 0 : return false;
2368 : }
2369 12 : attr = TupleDescAttr(tupdesc, fieldnum - 1);
2370 24 : if (attr->attisdropped ||
2371 24 : attr->atttypid != expectedtype ||
2372 24 : attr->atttypmod != expectedtypmod ||
2373 12 : attr->attcollation != expectedcollation)
2374 : {
2375 0 : ReleaseTupleDesc(tupdesc);
2376 0 : return false;
2377 : }
2378 12 : ReleaseTupleDesc(tupdesc);
2379 12 : return true;
2380 : }
2381 :
2382 :
2383 : /*--------------------
2384 : * eval_const_expressions
2385 : *
2386 : * Reduce any recognizably constant subexpressions of the given
2387 : * expression tree, for example "2 + 2" => "4". More interestingly,
2388 : * we can reduce certain boolean expressions even when they contain
2389 : * non-constant subexpressions: "x OR true" => "true" no matter what
2390 : * the subexpression x is. (XXX We assume that no such subexpression
2391 : * will have important side-effects, which is not necessarily a good
2392 : * assumption in the presence of user-defined functions; do we need a
2393 : * pg_proc flag that prevents discarding the execution of a function?)
2394 : *
2395 : * We do understand that certain functions may deliver non-constant
2396 : * results even with constant inputs, "nextval()" being the classic
2397 : * example. Functions that are not marked "immutable" in pg_proc
2398 : * will not be pre-evaluated here, although we will reduce their
2399 : * arguments as far as possible.
2400 : *
2401 : * Whenever a function is eliminated from the expression by means of
2402 : * constant-expression evaluation or inlining, we add the function to
2403 : * root->glob->invalItems. This ensures the plan is known to depend on
2404 : * such functions, even though they aren't referenced anymore.
2405 : *
2406 : * We assume that the tree has already been type-checked and contains
2407 : * only operators and functions that are reasonable to try to execute.
2408 : *
2409 : * NOTE: "root" can be passed as NULL if the caller never wants to do any
2410 : * Param substitutions nor receive info about inlined functions.
2411 : *
2412 : * NOTE: the planner assumes that this will always flatten nested AND and
2413 : * OR clauses into N-argument form. See comments in prepqual.c.
2414 : *
2415 : * NOTE: another critical effect is that any function calls that require
2416 : * default arguments will be expanded, and named-argument calls will be
2417 : * converted to positional notation. The executor won't handle either.
2418 : *--------------------
2419 : */
2420 : Node *
2421 44209 : eval_const_expressions(PlannerInfo *root, Node *node)
2422 : {
2423 : eval_const_expressions_context context;
2424 :
2425 44209 : if (root)
2426 41604 : context.boundParams = root->glob->boundParams; /* bound Params */
2427 : else
2428 2605 : context.boundParams = NULL;
2429 44209 : context.root = root; /* for inlined-function dependencies */
2430 44209 : context.active_fns = NIL; /* nothing being recursively simplified */
2431 44209 : context.case_val = NULL; /* no CASE being examined */
2432 44209 : context.estimate = false; /* safe transformations only */
2433 44209 : return eval_const_expressions_mutator(node, &context);
2434 : }
2435 :
2436 : /*--------------------
2437 : * estimate_expression_value
2438 : *
2439 : * This function attempts to estimate the value of an expression for
2440 : * planning purposes. It is in essence a more aggressive version of
2441 : * eval_const_expressions(): we will perform constant reductions that are
2442 : * not necessarily 100% safe, but are reasonable for estimation purposes.
2443 : *
2444 : * Currently the extra steps that are taken in this mode are:
2445 : * 1. Substitute values for Params, where a bound Param value has been made
2446 : * available by the caller of planner(), even if the Param isn't marked
2447 : * constant. This effectively means that we plan using the first supplied
2448 : * value of the Param.
2449 : * 2. Fold stable, as well as immutable, functions to constants.
2450 : * 3. Reduce PlaceHolderVar nodes to their contained expressions.
2451 : *--------------------
2452 : */
2453 : Node *
2454 22810 : estimate_expression_value(PlannerInfo *root, Node *node)
2455 : {
2456 : eval_const_expressions_context context;
2457 :
2458 22810 : context.boundParams = root->glob->boundParams; /* bound Params */
2459 : /* we do not need to mark the plan as depending on inlined functions */
2460 22810 : context.root = NULL;
2461 22810 : context.active_fns = NIL; /* nothing being recursively simplified */
2462 22810 : context.case_val = NULL; /* no CASE being examined */
2463 22810 : context.estimate = true; /* unsafe transformations OK */
2464 22810 : return eval_const_expressions_mutator(node, &context);
2465 : }
2466 :
2467 : static Node *
2468 326059 : eval_const_expressions_mutator(Node *node,
2469 : eval_const_expressions_context *context)
2470 : {
2471 326059 : if (node == NULL)
2472 16020 : return NULL;
2473 310039 : switch (nodeTag(node))
2474 : {
2475 : case T_Param:
2476 : {
2477 6461 : Param *param = (Param *) node;
2478 :
2479 : /* Look to see if we've been given a value for this Param */
2480 10942 : if (param->paramkind == PARAM_EXTERN &&
2481 5761 : context->boundParams != NULL &&
2482 2560 : param->paramid > 0 &&
2483 1280 : param->paramid <= context->boundParams->numParams)
2484 : {
2485 1280 : ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
2486 :
2487 1280 : if (OidIsValid(prm->ptype))
2488 : {
2489 : /* OK to substitute parameter value? */
2490 1989 : if (context->estimate ||
2491 994 : (prm->pflags & PARAM_FLAG_CONST))
2492 : {
2493 : /*
2494 : * Return a Const representing the param value.
2495 : * Must copy pass-by-ref datatypes, since the
2496 : * Param might be in a memory context
2497 : * shorter-lived than our output plan should be.
2498 : */
2499 : int16 typLen;
2500 : bool typByVal;
2501 : Datum pval;
2502 :
2503 994 : Assert(prm->ptype == param->paramtype);
2504 994 : get_typlenbyval(param->paramtype,
2505 : &typLen, &typByVal);
2506 994 : if (prm->isnull || typByVal)
2507 740 : pval = prm->value;
2508 : else
2509 254 : pval = datumCopy(prm->value, typByVal, typLen);
2510 1988 : return (Node *) makeConst(param->paramtype,
2511 : param->paramtypmod,
2512 : param->paramcollid,
2513 : (int) typLen,
2514 : pval,
2515 994 : prm->isnull,
2516 : typByVal);
2517 : }
2518 : }
2519 : }
2520 :
2521 : /*
2522 : * Not replaceable, so just copy the Param (no need to
2523 : * recurse)
2524 : */
2525 5467 : return (Node *) copyObject(param);
2526 : }
2527 : case T_WindowFunc:
2528 : {
2529 167 : WindowFunc *expr = (WindowFunc *) node;
2530 167 : Oid funcid = expr->winfnoid;
2531 : List *args;
2532 : Expr *aggfilter;
2533 : HeapTuple func_tuple;
2534 : WindowFunc *newexpr;
2535 :
2536 : /*
2537 : * We can't really simplify a WindowFunc node, but we mustn't
2538 : * just fall through to the default processing, because we
2539 : * have to apply expand_function_arguments to its argument
2540 : * list. That takes care of inserting default arguments and
2541 : * expanding named-argument notation.
2542 : */
2543 167 : func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2544 167 : if (!HeapTupleIsValid(func_tuple))
2545 0 : elog(ERROR, "cache lookup failed for function %u", funcid);
2546 :
2547 167 : args = expand_function_arguments(expr->args, expr->wintype,
2548 : func_tuple);
2549 :
2550 167 : ReleaseSysCache(func_tuple);
2551 :
2552 : /* Now, recursively simplify the args (which are a List) */
2553 167 : args = (List *)
2554 : expression_tree_mutator((Node *) args,
2555 : eval_const_expressions_mutator,
2556 : (void *) context);
2557 : /* ... and the filter expression, which isn't */
2558 167 : aggfilter = (Expr *)
2559 167 : eval_const_expressions_mutator((Node *) expr->aggfilter,
2560 : context);
2561 :
2562 : /* And build the replacement WindowFunc node */
2563 167 : newexpr = makeNode(WindowFunc);
2564 167 : newexpr->winfnoid = expr->winfnoid;
2565 167 : newexpr->wintype = expr->wintype;
2566 167 : newexpr->wincollid = expr->wincollid;
2567 167 : newexpr->inputcollid = expr->inputcollid;
2568 167 : newexpr->args = args;
2569 167 : newexpr->aggfilter = aggfilter;
2570 167 : newexpr->winref = expr->winref;
2571 167 : newexpr->winstar = expr->winstar;
2572 167 : newexpr->winagg = expr->winagg;
2573 167 : newexpr->location = expr->location;
2574 :
2575 167 : return (Node *) newexpr;
2576 : }
2577 : case T_FuncExpr:
2578 : {
2579 20869 : FuncExpr *expr = (FuncExpr *) node;
2580 20869 : List *args = expr->args;
2581 : Expr *simple;
2582 : FuncExpr *newexpr;
2583 :
2584 : /*
2585 : * Code for op/func reduction is pretty bulky, so split it out
2586 : * as a separate function. Note: exprTypmod normally returns
2587 : * -1 for a FuncExpr, but not when the node is recognizably a
2588 : * length coercion; we want to preserve the typmod in the
2589 : * eventual Const if so.
2590 : */
2591 20869 : simple = simplify_function(expr->funcid,
2592 : expr->funcresulttype,
2593 : exprTypmod(node),
2594 : expr->funccollid,
2595 : expr->inputcollid,
2596 : &args,
2597 20869 : expr->funcvariadic,
2598 : true,
2599 : true,
2600 : context);
2601 20717 : if (simple) /* successfully simplified it */
2602 6974 : return (Node *) simple;
2603 :
2604 : /*
2605 : * The expression cannot be simplified any further, so build
2606 : * and return a replacement FuncExpr node using the
2607 : * possibly-simplified arguments. Note that we have also
2608 : * converted the argument list to positional notation.
2609 : */
2610 13743 : newexpr = makeNode(FuncExpr);
2611 13743 : newexpr->funcid = expr->funcid;
2612 13743 : newexpr->funcresulttype = expr->funcresulttype;
2613 13743 : newexpr->funcretset = expr->funcretset;
2614 13743 : newexpr->funcvariadic = expr->funcvariadic;
2615 13743 : newexpr->funcformat = expr->funcformat;
2616 13743 : newexpr->funccollid = expr->funccollid;
2617 13743 : newexpr->inputcollid = expr->inputcollid;
2618 13743 : newexpr->args = args;
2619 13743 : newexpr->location = expr->location;
2620 13743 : return (Node *) newexpr;
2621 : }
2622 : case T_OpExpr:
2623 : {
2624 24016 : OpExpr *expr = (OpExpr *) node;
2625 24016 : List *args = expr->args;
2626 : Expr *simple;
2627 : OpExpr *newexpr;
2628 :
2629 : /*
2630 : * Need to get OID of underlying function. Okay to scribble
2631 : * on input to this extent.
2632 : */
2633 24016 : set_opfuncid(expr);
2634 :
2635 : /*
2636 : * Code for op/func reduction is pretty bulky, so split it out
2637 : * as a separate function.
2638 : */
2639 24016 : simple = simplify_function(expr->opfuncid,
2640 : expr->opresulttype, -1,
2641 : expr->opcollid,
2642 : expr->inputcollid,
2643 : &args,
2644 : false,
2645 : true,
2646 : true,
2647 : context);
2648 23926 : if (simple) /* successfully simplified it */
2649 1228 : return (Node *) simple;
2650 :
2651 : /*
2652 : * If the operator is boolean equality or inequality, we know
2653 : * how to simplify cases involving one constant and one
2654 : * non-constant argument.
2655 : */
2656 45379 : if (expr->opno == BooleanEqualOperator ||
2657 22681 : expr->opno == BooleanNotEqualOperator)
2658 : {
2659 32 : simple = (Expr *) simplify_boolean_equality(expr->opno,
2660 : args);
2661 32 : if (simple) /* successfully simplified it */
2662 15 : return (Node *) simple;
2663 : }
2664 :
2665 : /*
2666 : * The expression cannot be simplified any further, so build
2667 : * and return a replacement OpExpr node using the
2668 : * possibly-simplified arguments.
2669 : */
2670 22683 : newexpr = makeNode(OpExpr);
2671 22683 : newexpr->opno = expr->opno;
2672 22683 : newexpr->opfuncid = expr->opfuncid;
2673 22683 : newexpr->opresulttype = expr->opresulttype;
2674 22683 : newexpr->opretset = expr->opretset;
2675 22683 : newexpr->opcollid = expr->opcollid;
2676 22683 : newexpr->inputcollid = expr->inputcollid;
2677 22683 : newexpr->args = args;
2678 22683 : newexpr->location = expr->location;
2679 22683 : return (Node *) newexpr;
2680 : }
2681 : case T_DistinctExpr:
2682 : {
2683 28 : DistinctExpr *expr = (DistinctExpr *) node;
2684 : List *args;
2685 : ListCell *arg;
2686 28 : bool has_null_input = false;
2687 28 : bool all_null_input = true;
2688 28 : bool has_nonconst_input = false;
2689 : Expr *simple;
2690 : DistinctExpr *newexpr;
2691 :
2692 : /*
2693 : * Reduce constants in the DistinctExpr's arguments. We know
2694 : * args is either NIL or a List node, so we can call
2695 : * expression_tree_mutator directly rather than recursing to
2696 : * self.
2697 : */
2698 28 : args = (List *) expression_tree_mutator((Node *) expr->args,
2699 : eval_const_expressions_mutator,
2700 : (void *) context);
2701 :
2702 : /*
2703 : * We must do our own check for NULLs because DistinctExpr has
2704 : * different results for NULL input than the underlying
2705 : * operator does.
2706 : */
2707 84 : foreach(arg, args)
2708 : {
2709 56 : if (IsA(lfirst(arg), Const))
2710 : {
2711 13 : has_null_input |= ((Const *) lfirst(arg))->constisnull;
2712 13 : all_null_input &= ((Const *) lfirst(arg))->constisnull;
2713 : }
2714 : else
2715 43 : has_nonconst_input = true;
2716 : }
2717 :
2718 : /* all constants? then can optimize this out */
2719 28 : if (!has_nonconst_input)
2720 : {
2721 : /* all nulls? then not distinct */
2722 4 : if (all_null_input)
2723 0 : return makeBoolConst(false, false);
2724 :
2725 : /* one null? then distinct */
2726 4 : if (has_null_input)
2727 0 : return makeBoolConst(true, false);
2728 :
2729 : /* otherwise try to evaluate the '=' operator */
2730 : /* (NOT okay to try to inline it, though!) */
2731 :
2732 : /*
2733 : * Need to get OID of underlying function. Okay to
2734 : * scribble on input to this extent.
2735 : */
2736 4 : set_opfuncid((OpExpr *) expr); /* rely on struct
2737 : * equivalence */
2738 :
2739 : /*
2740 : * Code for op/func reduction is pretty bulky, so split it
2741 : * out as a separate function.
2742 : */
2743 4 : simple = simplify_function(expr->opfuncid,
2744 : expr->opresulttype, -1,
2745 : expr->opcollid,
2746 : expr->inputcollid,
2747 : &args,
2748 : false,
2749 : false,
2750 : false,
2751 : context);
2752 4 : if (simple) /* successfully simplified it */
2753 : {
2754 : /*
2755 : * Since the underlying operator is "=", must negate
2756 : * its result
2757 : */
2758 4 : Const *csimple = castNode(Const, simple);
2759 :
2760 4 : csimple->constvalue =
2761 4 : BoolGetDatum(!DatumGetBool(csimple->constvalue));
2762 4 : return (Node *) csimple;
2763 : }
2764 : }
2765 :
2766 : /*
2767 : * The expression cannot be simplified any further, so build
2768 : * and return a replacement DistinctExpr node using the
2769 : * possibly-simplified arguments.
2770 : */
2771 24 : newexpr = makeNode(DistinctExpr);
2772 24 : newexpr->opno = expr->opno;
2773 24 : newexpr->opfuncid = expr->opfuncid;
2774 24 : newexpr->opresulttype = expr->opresulttype;
2775 24 : newexpr->opretset = expr->opretset;
2776 24 : newexpr->opcollid = expr->opcollid;
2777 24 : newexpr->inputcollid = expr->inputcollid;
2778 24 : newexpr->args = args;
2779 24 : newexpr->location = expr->location;
2780 24 : return (Node *) newexpr;
2781 : }
2782 : case T_BoolExpr:
2783 : {
2784 5773 : BoolExpr *expr = (BoolExpr *) node;
2785 :
2786 5773 : switch (expr->boolop)
2787 : {
2788 : case OR_EXPR:
2789 : {
2790 : List *newargs;
2791 531 : bool haveNull = false;
2792 531 : bool forceTrue = false;
2793 :
2794 531 : newargs = simplify_or_arguments(expr->args,
2795 : context,
2796 : &haveNull,
2797 : &forceTrue);
2798 531 : if (forceTrue)
2799 6 : return makeBoolConst(true, false);
2800 525 : if (haveNull)
2801 1 : newargs = lappend(newargs,
2802 1 : makeBoolConst(false, true));
2803 : /* If all the inputs are FALSE, result is FALSE */
2804 525 : if (newargs == NIL)
2805 0 : return makeBoolConst(false, false);
2806 :
2807 : /*
2808 : * If only one nonconst-or-NULL input, it's the
2809 : * result
2810 : */
2811 525 : if (list_length(newargs) == 1)
2812 2 : return (Node *) linitial(newargs);
2813 : /* Else we still need an OR node */
2814 523 : return (Node *) make_orclause(newargs);
2815 : }
2816 : case AND_EXPR:
2817 : {
2818 : List *newargs;
2819 4440 : bool haveNull = false;
2820 4440 : bool forceFalse = false;
2821 :
2822 4440 : newargs = simplify_and_arguments(expr->args,
2823 : context,
2824 : &haveNull,
2825 : &forceFalse);
2826 4440 : if (forceFalse)
2827 65 : return makeBoolConst(false, false);
2828 4375 : if (haveNull)
2829 1 : newargs = lappend(newargs,
2830 1 : makeBoolConst(false, true));
2831 : /* If all the inputs are TRUE, result is TRUE */
2832 4375 : if (newargs == NIL)
2833 24 : return makeBoolConst(true, false);
2834 :
2835 : /*
2836 : * If only one nonconst-or-NULL input, it's the
2837 : * result
2838 : */
2839 4351 : if (list_length(newargs) == 1)
2840 2 : return (Node *) linitial(newargs);
2841 : /* Else we still need an AND node */
2842 4349 : return (Node *) make_andclause(newargs);
2843 : }
2844 : case NOT_EXPR:
2845 : {
2846 : Node *arg;
2847 :
2848 802 : Assert(list_length(expr->args) == 1);
2849 802 : arg = eval_const_expressions_mutator(linitial(expr->args),
2850 : context);
2851 :
2852 : /*
2853 : * Use negate_clause() to see if we can simplify
2854 : * away the NOT.
2855 : */
2856 802 : return negate_clause(arg);
2857 : }
2858 : default:
2859 0 : elog(ERROR, "unrecognized boolop: %d",
2860 : (int) expr->boolop);
2861 : break;
2862 : }
2863 : break;
2864 : }
2865 : case T_SubPlan:
2866 : case T_AlternativeSubPlan:
2867 :
2868 : /*
2869 : * Return a SubPlan unchanged --- too late to do anything with it.
2870 : *
2871 : * XXX should we ereport() here instead? Probably this routine
2872 : * should never be invoked after SubPlan creation.
2873 : */
2874 20 : return node;
2875 : case T_RelabelType:
2876 : {
2877 : /*
2878 : * If we can simplify the input to a constant, then we don't
2879 : * need the RelabelType node anymore: just change the type
2880 : * field of the Const node. Otherwise, must copy the
2881 : * RelabelType node.
2882 : */
2883 4192 : RelabelType *relabel = (RelabelType *) node;
2884 : Node *arg;
2885 :
2886 4192 : arg = eval_const_expressions_mutator((Node *) relabel->arg,
2887 : context);
2888 :
2889 : /*
2890 : * If we find stacked RelabelTypes (eg, from foo :: int ::
2891 : * oid) we can discard all but the top one.
2892 : */
2893 8386 : while (arg && IsA(arg, RelabelType))
2894 2 : arg = (Node *) ((RelabelType *) arg)->arg;
2895 :
2896 4192 : if (arg && IsA(arg, Const))
2897 : {
2898 1597 : Const *con = (Const *) arg;
2899 :
2900 1597 : con->consttype = relabel->resulttype;
2901 1597 : con->consttypmod = relabel->resulttypmod;
2902 1597 : con->constcollid = relabel->resultcollid;
2903 1597 : return (Node *) con;
2904 : }
2905 : else
2906 : {
2907 2595 : RelabelType *newrelabel = makeNode(RelabelType);
2908 :
2909 2595 : newrelabel->arg = (Expr *) arg;
2910 2595 : newrelabel->resulttype = relabel->resulttype;
2911 2595 : newrelabel->resulttypmod = relabel->resulttypmod;
2912 2595 : newrelabel->resultcollid = relabel->resultcollid;
2913 2595 : newrelabel->relabelformat = relabel->relabelformat;
2914 2595 : newrelabel->location = relabel->location;
2915 2595 : return (Node *) newrelabel;
2916 : }
2917 : }
2918 : case T_CoerceViaIO:
2919 : {
2920 775 : CoerceViaIO *expr = (CoerceViaIO *) node;
2921 : List *args;
2922 : Oid outfunc;
2923 : bool outtypisvarlena;
2924 : Oid infunc;
2925 : Oid intypioparam;
2926 : Expr *simple;
2927 : CoerceViaIO *newexpr;
2928 :
2929 : /* Make a List so we can use simplify_function */
2930 775 : args = list_make1(expr->arg);
2931 :
2932 : /*
2933 : * CoerceViaIO represents calling the source type's output
2934 : * function then the result type's input function. So, try to
2935 : * simplify it as though it were a stack of two such function
2936 : * calls. First we need to know what the functions are.
2937 : *
2938 : * Note that the coercion functions are assumed not to care
2939 : * about input collation, so we just pass InvalidOid for that.
2940 : */
2941 775 : getTypeOutputInfo(exprType((Node *) expr->arg),
2942 : &outfunc, &outtypisvarlena);
2943 775 : getTypeInputInfo(expr->resulttype,
2944 : &infunc, &intypioparam);
2945 :
2946 775 : simple = simplify_function(outfunc,
2947 : CSTRINGOID, -1,
2948 : InvalidOid,
2949 : InvalidOid,
2950 : &args,
2951 : false,
2952 : true,
2953 : true,
2954 : context);
2955 775 : if (simple) /* successfully simplified output fn */
2956 : {
2957 : /*
2958 : * Input functions may want 1 to 3 arguments. We always
2959 : * supply all three, trusting that nothing downstream will
2960 : * complain.
2961 : */
2962 72 : args = list_make3(simple,
2963 : makeConst(OIDOID,
2964 : -1,
2965 : InvalidOid,
2966 : sizeof(Oid),
2967 : ObjectIdGetDatum(intypioparam),
2968 : false,
2969 : true),
2970 : makeConst(INT4OID,
2971 : -1,
2972 : InvalidOid,
2973 : sizeof(int32),
2974 : Int32GetDatum(-1),
2975 : false,
2976 : true));
2977 :
2978 72 : simple = simplify_function(infunc,
2979 : expr->resulttype, -1,
2980 : expr->resultcollid,
2981 : InvalidOid,
2982 : &args,
2983 : false,
2984 : false,
2985 : true,
2986 : context);
2987 66 : if (simple) /* successfully simplified input fn */
2988 65 : return (Node *) simple;
2989 : }
2990 :
2991 : /*
2992 : * The expression cannot be simplified any further, so build
2993 : * and return a replacement CoerceViaIO node using the
2994 : * possibly-simplified argument.
2995 : */
2996 704 : newexpr = makeNode(CoerceViaIO);
2997 704 : newexpr->arg = (Expr *) linitial(args);
2998 704 : newexpr->resulttype = expr->resulttype;
2999 704 : newexpr->resultcollid = expr->resultcollid;
3000 704 : newexpr->coerceformat = expr->coerceformat;
3001 704 : newexpr->location = expr->location;
3002 704 : return (Node *) newexpr;
3003 : }
3004 : case T_ArrayCoerceExpr:
3005 : {
3006 111 : ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
3007 : Expr *arg;
3008 : ArrayCoerceExpr *newexpr;
3009 :
3010 : /*
3011 : * Reduce constants in the ArrayCoerceExpr's argument, then
3012 : * build a new ArrayCoerceExpr.
3013 : */
3014 111 : arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg,
3015 : context);
3016 :
3017 111 : newexpr = makeNode(ArrayCoerceExpr);
3018 111 : newexpr->arg = arg;
3019 111 : newexpr->elemfuncid = expr->elemfuncid;
3020 111 : newexpr->resulttype = expr->resulttype;
3021 111 : newexpr->resulttypmod = expr->resulttypmod;
3022 111 : newexpr->resultcollid = expr->resultcollid;
3023 111 : newexpr->isExplicit = expr->isExplicit;
3024 111 : newexpr->coerceformat = expr->coerceformat;
3025 111 : newexpr->location = expr->location;
3026 :
3027 : /*
3028 : * If constant argument and it's a binary-coercible or
3029 : * immutable conversion, we can simplify it to a constant.
3030 : */
3031 198 : if (arg && IsA(arg, Const) &&
3032 113 : (!OidIsValid(newexpr->elemfuncid) ||
3033 26 : func_volatile(newexpr->elemfuncid) == PROVOLATILE_IMMUTABLE))
3034 87 : return (Node *) evaluate_expr((Expr *) newexpr,
3035 : newexpr->resulttype,
3036 : newexpr->resulttypmod,
3037 : newexpr->resultcollid);
3038 :
3039 : /* Else we must return the partially-simplified node */
3040 24 : return (Node *) newexpr;
3041 : }
3042 : case T_CollateExpr:
3043 : {
3044 : /*
3045 : * If we can simplify the input to a constant, then we don't
3046 : * need the CollateExpr node at all: just change the
3047 : * constcollid field of the Const node. Otherwise, replace
3048 : * the CollateExpr with a RelabelType. (We do that so as to
3049 : * improve uniformity of expression representation and thus
3050 : * simplify comparison of expressions.)
3051 : */
3052 26 : CollateExpr *collate = (CollateExpr *) node;
3053 : Node *arg;
3054 :
3055 26 : arg = eval_const_expressions_mutator((Node *) collate->arg,
3056 : context);
3057 :
3058 26 : if (arg && IsA(arg, Const))
3059 : {
3060 11 : Const *con = (Const *) arg;
3061 :
3062 11 : con->constcollid = collate->collOid;
3063 11 : return (Node *) con;
3064 : }
3065 15 : else if (collate->collOid == exprCollation(arg))
3066 : {
3067 : /* Don't need a RelabelType either... */
3068 9 : return arg;
3069 : }
3070 : else
3071 : {
3072 6 : RelabelType *relabel = makeNode(RelabelType);
3073 :
3074 6 : relabel->resulttype = exprType(arg);
3075 6 : relabel->resulttypmod = exprTypmod(arg);
3076 6 : relabel->resultcollid = collate->collOid;
3077 6 : relabel->relabelformat = COERCE_IMPLICIT_CAST;
3078 6 : relabel->location = collate->location;
3079 :
3080 : /* Don't create stacked RelabelTypes */
3081 12 : while (arg && IsA(arg, RelabelType))
3082 0 : arg = (Node *) ((RelabelType *) arg)->arg;
3083 6 : relabel->arg = (Expr *) arg;
3084 :
3085 6 : return (Node *) relabel;
3086 : }
3087 : }
3088 : case T_CaseExpr:
3089 : {
3090 : /*----------
3091 : * CASE expressions can be simplified if there are constant
3092 : * condition clauses:
3093 : * FALSE (or NULL): drop the alternative
3094 : * TRUE: drop all remaining alternatives
3095 : * If the first non-FALSE alternative is a constant TRUE,
3096 : * we can simplify the entire CASE to that alternative's
3097 : * expression. If there are no non-FALSE alternatives,
3098 : * we simplify the entire CASE to the default result (ELSE).
3099 : *
3100 : * If we have a simple-form CASE with constant test
3101 : * expression, we substitute the constant value for contained
3102 : * CaseTestExpr placeholder nodes, so that we have the
3103 : * opportunity to reduce constant test conditions. For
3104 : * example this allows
3105 : * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
3106 : * to reduce to 1 rather than drawing a divide-by-0 error.
3107 : * Note that when the test expression is constant, we don't
3108 : * have to include it in the resulting CASE; for example
3109 : * CASE 0 WHEN x THEN y ELSE z END
3110 : * is transformed by the parser to
3111 : * CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
3112 : * which we can simplify to
3113 : * CASE WHEN 0 = x THEN y ELSE z END
3114 : * It is not necessary for the executor to evaluate the "arg"
3115 : * expression when executing the CASE, since any contained
3116 : * CaseTestExprs that might have referred to it will have been
3117 : * replaced by the constant.
3118 : *----------
3119 : */
3120 1146 : CaseExpr *caseexpr = (CaseExpr *) node;
3121 : CaseExpr *newcase;
3122 : Node *save_case_val;
3123 : Node *newarg;
3124 : List *newargs;
3125 : bool const_true_cond;
3126 1146 : Node *defresult = NULL;
3127 : ListCell *arg;
3128 :
3129 : /* Simplify the test expression, if any */
3130 1146 : newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
3131 : context);
3132 :
3133 : /* Set up for contained CaseTestExpr nodes */
3134 1146 : save_case_val = context->case_val;
3135 1146 : if (newarg && IsA(newarg, Const))
3136 : {
3137 2 : context->case_val = newarg;
3138 2 : newarg = NULL; /* not needed anymore, see above */
3139 : }
3140 : else
3141 1144 : context->case_val = NULL;
3142 :
3143 : /* Simplify the WHEN clauses */
3144 1146 : newargs = NIL;
3145 1146 : const_true_cond = false;
3146 2924 : foreach(arg, caseexpr->args)
3147 : {
3148 1801 : CaseWhen *oldcasewhen = lfirst_node(CaseWhen, arg);
3149 : Node *casecond;
3150 : Node *caseresult;
3151 :
3152 : /* Simplify this alternative's test condition */
3153 1801 : casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
3154 : context);
3155 :
3156 : /*
3157 : * If the test condition is constant FALSE (or NULL), then
3158 : * drop this WHEN clause completely, without processing
3159 : * the result.
3160 : */
3161 1801 : if (casecond && IsA(casecond, Const))
3162 : {
3163 35 : Const *const_input = (Const *) casecond;
3164 :
3165 70 : if (const_input->constisnull ||
3166 35 : !DatumGetBool(const_input->constvalue))
3167 13 : continue; /* drop alternative with FALSE cond */
3168 : /* Else it's constant TRUE */
3169 22 : const_true_cond = true;
3170 : }
3171 :
3172 : /* Simplify this alternative's result value */
3173 1788 : caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
3174 : context);
3175 :
3176 : /* If non-constant test condition, emit a new WHEN node */
3177 1787 : if (!const_true_cond)
3178 : {
3179 1765 : CaseWhen *newcasewhen = makeNode(CaseWhen);
3180 :
3181 1765 : newcasewhen->expr = (Expr *) casecond;
3182 1765 : newcasewhen->result = (Expr *) caseresult;
3183 1765 : newcasewhen->location = oldcasewhen->location;
3184 1765 : newargs = lappend(newargs, newcasewhen);
3185 1765 : continue;
3186 : }
3187 :
3188 : /*
3189 : * Found a TRUE condition, so none of the remaining
3190 : * alternatives can be reached. We treat the result as
3191 : * the default result.
3192 : */
3193 22 : defresult = caseresult;
3194 22 : break;
3195 : }
3196 :
3197 : /* Simplify the default result, unless we replaced it above */
3198 1145 : if (!const_true_cond)
3199 1123 : defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
3200 : context);
3201 :
3202 1145 : context->case_val = save_case_val;
3203 :
3204 : /*
3205 : * If no non-FALSE alternatives, CASE reduces to the default
3206 : * result
3207 : */
3208 1145 : if (newargs == NIL)
3209 32 : return defresult;
3210 : /* Otherwise we need a new CASE node */
3211 1113 : newcase = makeNode(CaseExpr);
3212 1113 : newcase->casetype = caseexpr->casetype;
3213 1113 : newcase->casecollid = caseexpr->casecollid;
3214 1113 : newcase->arg = (Expr *) newarg;
3215 1113 : newcase->args = newargs;
3216 1113 : newcase->defresult = (Expr *) defresult;
3217 1113 : newcase->location = caseexpr->location;
3218 1113 : return (Node *) newcase;
3219 : }
3220 : case T_CaseTestExpr:
3221 : {
3222 : /*
3223 : * If we know a constant test value for the current CASE
3224 : * construct, substitute it for the placeholder. Else just
3225 : * return the placeholder as-is.
3226 : */
3227 1029 : if (context->case_val)
3228 3 : return copyObject(context->case_val);
3229 : else
3230 1026 : return copyObject(node);
3231 : }
3232 : case T_ArrayExpr:
3233 : {
3234 1283 : ArrayExpr *arrayexpr = (ArrayExpr *) node;
3235 : ArrayExpr *newarray;
3236 1283 : bool all_const = true;
3237 : List *newelems;
3238 : ListCell *element;
3239 :
3240 1283 : newelems = NIL;
3241 4313 : foreach(element, arrayexpr->elements)
3242 : {
3243 : Node *e;
3244 :
3245 3030 : e = eval_const_expressions_mutator((Node *) lfirst(element),
3246 : context);
3247 3030 : if (!IsA(e, Const))
3248 137 : all_const = false;
3249 3030 : newelems = lappend(newelems, e);
3250 : }
3251 :
3252 1283 : newarray = makeNode(ArrayExpr);
3253 1283 : newarray->array_typeid = arrayexpr->array_typeid;
3254 1283 : newarray->array_collid = arrayexpr->array_collid;
3255 1283 : newarray->element_typeid = arrayexpr->element_typeid;
3256 1283 : newarray->elements = newelems;
3257 1283 : newarray->multidims = arrayexpr->multidims;
3258 1283 : newarray->location = arrayexpr->location;
3259 :
3260 1283 : if (all_const)
3261 1204 : return (Node *) evaluate_expr((Expr *) newarray,
3262 : newarray->array_typeid,
3263 : exprTypmod(node),
3264 : newarray->array_collid);
3265 :
3266 79 : return (Node *) newarray;
3267 : }
3268 : case T_CoalesceExpr:
3269 : {
3270 238 : CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3271 : CoalesceExpr *newcoalesce;
3272 : List *newargs;
3273 : ListCell *arg;
3274 :
3275 238 : newargs = NIL;
3276 589 : foreach(arg, coalesceexpr->args)
3277 : {
3278 : Node *e;
3279 :
3280 474 : e = eval_const_expressions_mutator((Node *) lfirst(arg),
3281 : context);
3282 :
3283 : /*
3284 : * We can remove null constants from the list. For a
3285 : * non-null constant, if it has not been preceded by any
3286 : * other non-null-constant expressions then it is the
3287 : * result. Otherwise, it's the next argument, but we can
3288 : * drop following arguments since they will never be
3289 : * reached.
3290 : */
3291 474 : if (IsA(e, Const))
3292 : {
3293 125 : if (((Const *) e)->constisnull)
3294 2 : continue; /* drop null constant */
3295 123 : if (newargs == NIL)
3296 2 : return e; /* first expr */
3297 121 : newargs = lappend(newargs, e);
3298 121 : break;
3299 : }
3300 349 : newargs = lappend(newargs, e);
3301 : }
3302 :
3303 : /*
3304 : * If all the arguments were constant null, the result is just
3305 : * null
3306 : */
3307 236 : if (newargs == NIL)
3308 0 : return (Node *) makeNullConst(coalesceexpr->coalescetype,
3309 : -1,
3310 : coalesceexpr->coalescecollid);
3311 :
3312 236 : newcoalesce = makeNode(CoalesceExpr);
3313 236 : newcoalesce->coalescetype = coalesceexpr->coalescetype;
3314 236 : newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
3315 236 : newcoalesce->args = newargs;
3316 236 : newcoalesce->location = coalesceexpr->location;
3317 236 : return (Node *) newcoalesce;
3318 : }
3319 : case T_SQLValueFunction:
3320 : {
3321 : /*
3322 : * All variants of SQLValueFunction are stable, so if we are
3323 : * estimating the expression's value, we should evaluate the
3324 : * current function value. Otherwise just copy.
3325 : */
3326 418 : SQLValueFunction *svf = (SQLValueFunction *) node;
3327 :
3328 418 : if (context->estimate)
3329 103 : return (Node *) evaluate_expr((Expr *) svf,
3330 : svf->type,
3331 : svf->typmod,
3332 : InvalidOid);
3333 : else
3334 315 : return copyObject((Node *) svf);
3335 : }
3336 : case T_FieldSelect:
3337 : {
3338 : /*
3339 : * We can optimize field selection from a whole-row Var into a
3340 : * simple Var. (This case won't be generated directly by the
3341 : * parser, because ParseComplexProjection short-circuits it.
3342 : * But it can arise while simplifying functions.) Also, we
3343 : * can optimize field selection from a RowExpr construct.
3344 : *
3345 : * However, replacing a whole-row Var in this way has a
3346 : * pitfall: if we've already built the rel targetlist for the
3347 : * source relation, then the whole-row Var is scheduled to be
3348 : * produced by the relation scan, but the simple Var probably
3349 : * isn't, which will lead to a failure in setrefs.c. This is
3350 : * not a problem when handling simple single-level queries, in
3351 : * which expression simplification always happens first. It
3352 : * is a risk for lateral references from subqueries, though.
3353 : * To avoid such failures, don't optimize uplevel references.
3354 : *
3355 : * We must also check that the declared type of the field is
3356 : * still the same as when the FieldSelect was created --- this
3357 : * can change if someone did ALTER COLUMN TYPE on the rowtype.
3358 : */
3359 295 : FieldSelect *fselect = (FieldSelect *) node;
3360 : FieldSelect *newfselect;
3361 : Node *arg;
3362 :
3363 295 : arg = eval_const_expressions_mutator((Node *) fselect->arg,
3364 : context);
3365 325 : if (arg && IsA(arg, Var) &&
3366 38 : ((Var *) arg)->varattno == InvalidAttrNumber &&
3367 8 : ((Var *) arg)->varlevelsup == 0)
3368 : {
3369 12 : if (rowtype_field_matches(((Var *) arg)->vartype,
3370 6 : fselect->fieldnum,
3371 : fselect->resulttype,
3372 : fselect->resulttypmod,
3373 : fselect->resultcollid))
3374 12 : return (Node *) makeVar(((Var *) arg)->varno,
3375 6 : fselect->fieldnum,
3376 : fselect->resulttype,
3377 : fselect->resulttypmod,
3378 : fselect->resultcollid,
3379 : ((Var *) arg)->varlevelsup);
3380 : }
3381 289 : if (arg && IsA(arg, RowExpr))
3382 : {
3383 6 : RowExpr *rowexpr = (RowExpr *) arg;
3384 :
3385 12 : if (fselect->fieldnum > 0 &&
3386 6 : fselect->fieldnum <= list_length(rowexpr->args))
3387 : {
3388 6 : Node *fld = (Node *) list_nth(rowexpr->args,
3389 6 : fselect->fieldnum - 1);
3390 :
3391 12 : if (rowtype_field_matches(rowexpr->row_typeid,
3392 6 : fselect->fieldnum,
3393 : fselect->resulttype,
3394 : fselect->resulttypmod,
3395 6 : fselect->resultcollid) &&
3396 12 : fselect->resulttype == exprType(fld) &&
3397 12 : fselect->resulttypmod == exprTypmod(fld) &&
3398 6 : fselect->resultcollid == exprCollation(fld))
3399 6 : return fld;
3400 : }
3401 : }
3402 283 : newfselect = makeNode(FieldSelect);
3403 283 : newfselect->arg = (Expr *) arg;
3404 283 : newfselect->fieldnum = fselect->fieldnum;
3405 283 : newfselect->resulttype = fselect->resulttype;
3406 283 : newfselect->resulttypmod = fselect->resulttypmod;
3407 283 : newfselect->resultcollid = fselect->resultcollid;
3408 283 : return (Node *) newfselect;
3409 : }
3410 : case T_NullTest:
3411 : {
3412 1280 : NullTest *ntest = (NullTest *) node;
3413 : NullTest *newntest;
3414 : Node *arg;
3415 :
3416 1280 : arg = eval_const_expressions_mutator((Node *) ntest->arg,
3417 : context);
3418 1280 : if (ntest->argisrow && arg && IsA(arg, RowExpr))
3419 : {
3420 : /*
3421 : * We break ROW(...) IS [NOT] NULL into separate tests on
3422 : * its component fields. This form is usually more
3423 : * efficient to evaluate, as well as being more amenable
3424 : * to optimization.
3425 : */
3426 6 : RowExpr *rarg = (RowExpr *) arg;
3427 6 : List *newargs = NIL;
3428 : ListCell *l;
3429 :
3430 21 : foreach(l, rarg->args)
3431 : {
3432 15 : Node *relem = (Node *) lfirst(l);
3433 :
3434 : /*
3435 : * A constant field refutes the whole NullTest if it's
3436 : * of the wrong nullness; else we can discard it.
3437 : */
3438 15 : if (relem && IsA(relem, Const))
3439 : {
3440 0 : Const *carg = (Const *) relem;
3441 :
3442 0 : if (carg->constisnull ?
3443 0 : (ntest->nulltesttype == IS_NOT_NULL) :
3444 0 : (ntest->nulltesttype == IS_NULL))
3445 0 : return makeBoolConst(false, false);
3446 0 : continue;
3447 : }
3448 :
3449 : /*
3450 : * Else, make a scalar (argisrow == false) NullTest
3451 : * for this field. Scalar semantics are required
3452 : * because IS [NOT] NULL doesn't recurse; see comments
3453 : * in ExecEvalRowNullInt().
3454 : */
3455 15 : newntest = makeNode(NullTest);
3456 15 : newntest->arg = (Expr *) relem;
3457 15 : newntest->nulltesttype = ntest->nulltesttype;
3458 15 : newntest->argisrow = false;
3459 15 : newntest->location = ntest->location;
3460 15 : newargs = lappend(newargs, newntest);
3461 : }
3462 : /* If all the inputs were constants, result is TRUE */
3463 6 : if (newargs == NIL)
3464 1 : return makeBoolConst(true, false);
3465 : /* If only one nonconst input, it's the result */
3466 5 : if (list_length(newargs) == 1)
3467 0 : return (Node *) linitial(newargs);
3468 : /* Else we need an AND node */
3469 5 : return (Node *) make_andclause(newargs);
3470 : }
3471 1274 : if (!ntest->argisrow && arg && IsA(arg, Const))
3472 : {
3473 32 : Const *carg = (Const *) arg;
3474 : bool result;
3475 :
3476 32 : switch (ntest->nulltesttype)
3477 : {
3478 : case IS_NULL:
3479 30 : result = carg->constisnull;
3480 30 : break;
3481 : case IS_NOT_NULL:
3482 2 : result = !carg->constisnull;
3483 2 : break;
3484 : default:
3485 0 : elog(ERROR, "unrecognized nulltesttype: %d",
3486 : (int) ntest->nulltesttype);
3487 : result = false; /* keep compiler quiet */
3488 : break;
3489 : }
3490 :
3491 32 : return makeBoolConst(result, false);
3492 : }
3493 :
3494 1242 : newntest = makeNode(NullTest);
3495 1242 : newntest->arg = (Expr *) arg;
3496 1242 : newntest->nulltesttype = ntest->nulltesttype;
3497 1242 : newntest->argisrow = ntest->argisrow;
3498 1242 : newntest->location = ntest->location;
3499 1242 : return (Node *) newntest;
3500 : }
3501 : case T_BooleanTest:
3502 : {
3503 71 : BooleanTest *btest = (BooleanTest *) node;
3504 : BooleanTest *newbtest;
3505 : Node *arg;
3506 :
3507 71 : arg = eval_const_expressions_mutator((Node *) btest->arg,
3508 : context);
3509 71 : if (arg && IsA(arg, Const))
3510 : {
3511 37 : Const *carg = (Const *) arg;
3512 : bool result;
3513 :
3514 37 : switch (btest->booltesttype)
3515 : {
3516 : case IS_TRUE:
3517 0 : result = (!carg->constisnull &&
3518 0 : DatumGetBool(carg->constvalue));
3519 0 : break;
3520 : case IS_NOT_TRUE:
3521 74 : result = (carg->constisnull ||
3522 37 : !DatumGetBool(carg->constvalue));
3523 37 : break;
3524 : case IS_FALSE:
3525 0 : result = (!carg->constisnull &&
3526 0 : !DatumGetBool(carg->constvalue));
3527 0 : break;
3528 : case IS_NOT_FALSE:
3529 0 : result = (carg->constisnull ||
3530 0 : DatumGetBool(carg->constvalue));
3531 0 : break;
3532 : case IS_UNKNOWN:
3533 0 : result = carg->constisnull;
3534 0 : break;
3535 : case IS_NOT_UNKNOWN:
3536 0 : result = !carg->constisnull;
3537 0 : break;
3538 : default:
3539 0 : elog(ERROR, "unrecognized booltesttype: %d",
3540 : (int) btest->booltesttype);
3541 : result = false; /* keep compiler quiet */
3542 : break;
3543 : }
3544 :
3545 37 : return makeBoolConst(result, false);
3546 : }
3547 :
3548 34 : newbtest = makeNode(BooleanTest);
3549 34 : newbtest->arg = (Expr *) arg;
3550 34 : newbtest->booltesttype = btest->booltesttype;
3551 34 : newbtest->location = btest->location;
3552 34 : return (Node *) newbtest;
3553 : }
3554 : case T_PlaceHolderVar:
3555 :
3556 : /*
3557 : * In estimation mode, just strip the PlaceHolderVar node
3558 : * altogether; this amounts to estimating that the contained value
3559 : * won't be forced to null by an outer join. In regular mode we
3560 : * just use the default behavior (ie, simplify the expression but
3561 : * leave the PlaceHolderVar node intact).
3562 : */
3563 100 : if (context->estimate)
3564 : {
3565 20 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
3566 :
3567 20 : return eval_const_expressions_mutator((Node *) phv->phexpr,
3568 : context);
3569 : }
3570 80 : break;
3571 : default:
3572 241741 : break;
3573 : }
3574 :
3575 : /*
3576 : * For any node type not handled above, we recurse using
3577 : * expression_tree_mutator, which will copy the node unchanged but try to
3578 : * simplify its arguments (if any) using this routine. For example: we
3579 : * cannot eliminate an ArrayRef node, but we might be able to simplify
3580 : * constant expressions in its subscripts.
3581 : */
3582 241821 : return expression_tree_mutator(node, eval_const_expressions_mutator,
3583 : (void *) context);
3584 : }
3585 :
3586 : /*
3587 : * Subroutine for eval_const_expressions: process arguments of an OR clause
3588 : *
3589 : * This includes flattening of nested ORs as well as recursion to
3590 : * eval_const_expressions to simplify the OR arguments.
3591 : *
3592 : * After simplification, OR arguments are handled as follows:
3593 : * non constant: keep
3594 : * FALSE: drop (does not affect result)
3595 : * TRUE: force result to TRUE
3596 : * NULL: keep only one
3597 : * We must keep one NULL input because OR expressions evaluate to NULL when no
3598 : * input is TRUE and at least one is NULL. We don't actually include the NULL
3599 : * here, that's supposed to be done by the caller.
3600 : *
3601 : * The output arguments *haveNull and *forceTrue must be initialized FALSE
3602 : * by the caller. They will be set TRUE if a null constant or true constant,
3603 : * respectively, is detected anywhere in the argument list.
3604 : */
3605 : static List *
3606 531 : simplify_or_arguments(List *args,
3607 : eval_const_expressions_context *context,
3608 : bool *haveNull, bool *forceTrue)
3609 : {
3610 531 : List *newargs = NIL;
3611 : List *unprocessed_args;
3612 :
3613 : /*
3614 : * We want to ensure that any OR immediately beneath another OR gets
3615 : * flattened into a single OR-list, so as to simplify later reasoning.
3616 : *
3617 : * To avoid stack overflow from recursion of eval_const_expressions, we
3618 : * resort to some tenseness here: we keep a list of not-yet-processed
3619 : * inputs, and handle flattening of nested ORs by prepending to the to-do
3620 : * list instead of recursing. Now that the parser generates N-argument
3621 : * ORs from simple lists, this complexity is probably less necessary than
3622 : * it once was, but we might as well keep the logic.
3623 : */
3624 531 : unprocessed_args = list_copy(args);
3625 2424 : while (unprocessed_args)
3626 : {
3627 1368 : Node *arg = (Node *) linitial(unprocessed_args);
3628 :
3629 1368 : unprocessed_args = list_delete_first(unprocessed_args);
3630 :
3631 : /* flatten nested ORs as per above comment */
3632 1368 : if (or_clause(arg))
3633 : {
3634 1 : List *subargs = list_copy(((BoolExpr *) arg)->args);
3635 :
3636 : /* overly tense code to avoid leaking unused list header */
3637 1 : if (!unprocessed_args)
3638 0 : unprocessed_args = subargs;
3639 : else
3640 : {
3641 1 : List *oldhdr = unprocessed_args;
3642 :
3643 1 : unprocessed_args = list_concat(subargs, unprocessed_args);
3644 1 : pfree(oldhdr);
3645 : }
3646 1 : continue;
3647 : }
3648 :
3649 : /* If it's not an OR, simplify it */
3650 1367 : arg = eval_const_expressions_mutator(arg, context);
3651 :
3652 : /*
3653 : * It is unlikely but not impossible for simplification of a non-OR
3654 : * clause to produce an OR. Recheck, but don't be too tense about it
3655 : * since it's not a mainstream case. In particular we don't worry
3656 : * about const-simplifying the input twice.
3657 : */
3658 1367 : if (or_clause(arg))
3659 : {
3660 0 : List *subargs = list_copy(((BoolExpr *) arg)->args);
3661 :
3662 0 : unprocessed_args = list_concat(subargs, unprocessed_args);
3663 0 : continue;
3664 : }
3665 :
3666 : /*
3667 : * OK, we have a const-simplified non-OR argument. Process it per
3668 : * comments above.
3669 : */
3670 1367 : if (IsA(arg, Const))
3671 : {
3672 14 : Const *const_input = (Const *) arg;
3673 :
3674 14 : if (const_input->constisnull)
3675 2 : *haveNull = true;
3676 12 : else if (DatumGetBool(const_input->constvalue))
3677 : {
3678 6 : *forceTrue = true;
3679 :
3680 : /*
3681 : * Once we detect a TRUE result we can just exit the loop
3682 : * immediately. However, if we ever add a notion of
3683 : * non-removable functions, we'd need to keep scanning.
3684 : */
3685 6 : return NIL;
3686 : }
3687 : /* otherwise, we can drop the constant-false input */
3688 8 : continue;
3689 : }
3690 :
3691 : /* else emit the simplified arg into the result list */
3692 1353 : newargs = lappend(newargs, arg);
3693 : }
3694 :
3695 525 : return newargs;
3696 : }
3697 :
3698 : /*
3699 : * Subroutine for eval_const_expressions: process arguments of an AND clause
3700 : *
3701 : * This includes flattening of nested ANDs as well as recursion to
3702 : * eval_const_expressions to simplify the AND arguments.
3703 : *
3704 : * After simplification, AND arguments are handled as follows:
3705 : * non constant: keep
3706 : * TRUE: drop (does not affect result)
3707 : * FALSE: force result to FALSE
3708 : * NULL: keep only one
3709 : * We must keep one NULL input because AND expressions evaluate to NULL when
3710 : * no input is FALSE and at least one is NULL. We don't actually include the
3711 : * NULL here, that's supposed to be done by the caller.
3712 : *
3713 : * The output arguments *haveNull and *forceFalse must be initialized FALSE
3714 : * by the caller. They will be set TRUE if a null constant or false constant,
3715 : * respectively, is detected anywhere in the argument list.
3716 : */
3717 : static List *
3718 4440 : simplify_and_arguments(List *args,
3719 : eval_const_expressions_context *context,
3720 : bool *haveNull, bool *forceFalse)
3721 : {
3722 4440 : List *newargs = NIL;
3723 : List *unprocessed_args;
3724 :
3725 : /* See comments in simplify_or_arguments */
3726 4440 : unprocessed_args = list_copy(args);
3727 20159 : while (unprocessed_args)
3728 : {
3729 11344 : Node *arg = (Node *) linitial(unprocessed_args);
3730 :
3731 11344 : unprocessed_args = list_delete_first(unprocessed_args);
3732 :
3733 : /* flatten nested ANDs as per above comment */
3734 11344 : if (and_clause(arg))
3735 : {
3736 120 : List *subargs = list_copy(((BoolExpr *) arg)->args);
3737 :
3738 : /* overly tense code to avoid leaking unused list header */
3739 120 : if (!unprocessed_args)
3740 30 : unprocessed_args = subargs;
3741 : else
3742 : {
3743 90 : List *oldhdr = unprocessed_args;
3744 :
3745 90 : unprocessed_args = list_concat(subargs, unprocessed_args);
3746 90 : pfree(oldhdr);
3747 : }
3748 120 : continue;
3749 : }
3750 :
3751 : /* If it's not an AND, simplify it */
3752 11224 : arg = eval_const_expressions_mutator(arg, context);
3753 :
3754 : /*
3755 : * It is unlikely but not impossible for simplification of a non-AND
3756 : * clause to produce an AND. Recheck, but don't be too tense about it
3757 : * since it's not a mainstream case. In particular we don't worry
3758 : * about const-simplifying the input twice.
3759 : */
3760 11224 : if (and_clause(arg))
3761 : {
3762 3 : List *subargs = list_copy(((BoolExpr *) arg)->args);
3763 :
3764 3 : unprocessed_args = list_concat(subargs, unprocessed_args);
3765 3 : continue;
3766 : }
3767 :
3768 : /*
3769 : * OK, we have a const-simplified non-AND argument. Process it per
3770 : * comments above.
3771 : */
3772 11221 : if (IsA(arg, Const))
3773 : {
3774 165 : Const *const_input = (Const *) arg;
3775 :
3776 165 : if (const_input->constisnull)
3777 3 : *haveNull = true;
3778 162 : else if (!DatumGetBool(const_input->constvalue))
3779 : {
3780 65 : *forceFalse = true;
3781 :
3782 : /*
3783 : * Once we detect a FALSE result we can just exit the loop
3784 : * immediately. However, if we ever add a notion of
3785 : * non-removable functions, we'd need to keep scanning.
3786 : */
3787 65 : return NIL;
3788 : }
3789 : /* otherwise, we can drop the constant-true input */
3790 100 : continue;
3791 : }
3792 :
3793 : /* else emit the simplified arg into the result list */
3794 11056 : newargs = lappend(newargs, arg);
3795 : }
3796 :
3797 4375 : return newargs;
3798 : }
3799 :
3800 : /*
3801 : * Subroutine for eval_const_expressions: try to simplify boolean equality
3802 : * or inequality condition
3803 : *
3804 : * Inputs are the operator OID and the simplified arguments to the operator.
3805 : * Returns a simplified expression if successful, or NULL if cannot
3806 : * simplify the expression.
3807 : *
3808 : * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x",
3809 : * or similarly "x <> true" to "NOT x" and "x <> false" to "x".
3810 : * This is only marginally useful in itself, but doing it in constant folding
3811 : * ensures that we will recognize these forms as being equivalent in, for
3812 : * example, partial index matching.
3813 : *
3814 : * We come here only if simplify_function has failed; therefore we cannot
3815 : * see two constant inputs, nor a constant-NULL input.
3816 : */
3817 : static Node *
3818 32 : simplify_boolean_equality(Oid opno, List *args)
3819 : {
3820 : Node *leftop;
3821 : Node *rightop;
3822 :
3823 32 : Assert(list_length(args) == 2);
3824 32 : leftop = linitial(args);
3825 32 : rightop = lsecond(args);
3826 32 : if (leftop && IsA(leftop, Const))
3827 : {
3828 0 : Assert(!((Const *) leftop)->constisnull);
3829 0 : if (opno == BooleanEqualOperator)
3830 : {
3831 0 : if (DatumGetBool(((Const *) leftop)->constvalue))
3832 0 : return rightop; /* true = foo */
3833 : else
3834 0 : return negate_clause(rightop); /* false = foo */
3835 : }
3836 : else
3837 : {
3838 0 : if (DatumGetBool(((Const *) leftop)->constvalue))
3839 0 : return negate_clause(rightop); /* true <> foo */
3840 : else
3841 0 : return rightop; /* false <> foo */
3842 : }
3843 : }
3844 32 : if (rightop && IsA(rightop, Const))
3845 : {
3846 15 : Assert(!((Const *) rightop)->constisnull);
3847 15 : if (opno == BooleanEqualOperator)
3848 : {
3849 14 : if (DatumGetBool(((Const *) rightop)->constvalue))
3850 4 : return leftop; /* foo = true */
3851 : else
3852 10 : return negate_clause(leftop); /* foo = false */
3853 : }
3854 : else
3855 : {
3856 1 : if (DatumGetBool(((Const *) rightop)->constvalue))
3857 0 : return negate_clause(leftop); /* foo <> true */
3858 : else
3859 1 : return leftop; /* foo <> false */
3860 : }
3861 : }
3862 17 : return NULL;
3863 : }
3864 :
3865 : /*
3866 : * Subroutine for eval_const_expressions: try to simplify a function call
3867 : * (which might originally have been an operator; we don't care)
3868 : *
3869 : * Inputs are the function OID, actual result type OID (which is needed for
3870 : * polymorphic functions), result typmod, result collation, the input
3871 : * collation to use for the function, the original argument list (not
3872 : * const-simplified yet, unless process_args is false), and some flags;
3873 : * also the context data for eval_const_expressions.
3874 : *
3875 : * Returns a simplified expression if successful, or NULL if cannot
3876 : * simplify the function call.
3877 : *
3878 : * This function is also responsible for converting named-notation argument
3879 : * lists into positional notation and/or adding any needed default argument
3880 : * expressions; which is a bit grotty, but it avoids extra fetches of the
3881 : * function's pg_proc tuple. For this reason, the args list is
3882 : * pass-by-reference. Conversion and const-simplification of the args list
3883 : * will be done even if simplification of the function call itself is not
3884 : * possible.
3885 : */
3886 : static Expr *
3887 45736 : simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
3888 : Oid result_collid, Oid input_collid, List **args_p,
3889 : bool funcvariadic, bool process_args, bool allow_non_const,
3890 : eval_const_expressions_context *context)
3891 : {
3892 45736 : List *args = *args_p;
3893 : HeapTuple func_tuple;
3894 : Form_pg_proc func_form;
3895 : Expr *newexpr;
3896 :
3897 : /*
3898 : * We have three strategies for simplification: execute the function to
3899 : * deliver a constant result, use a transform function to generate a
3900 : * substitute node tree, or expand in-line the body of the function
3901 : * definition (which only works for simple SQL-language functions, but
3902 : * that is a common case). Each case needs access to the function's
3903 : * pg_proc tuple, so fetch it just once.
3904 : *
3905 : * Note: the allow_non_const flag suppresses both the second and third
3906 : * strategies; so if !allow_non_const, simplify_function can only return a
3907 : * Const or NULL. Argument-list rewriting happens anyway, though.
3908 : */
3909 45736 : func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3910 45736 : if (!HeapTupleIsValid(func_tuple))
3911 0 : elog(ERROR, "cache lookup failed for function %u", funcid);
3912 45736 : func_form = (Form_pg_proc) GETSTRUCT(func_tuple);
3913 :
3914 : /*
3915 : * Process the function arguments, unless the caller did it already.
3916 : *
3917 : * Here we must deal with named or defaulted arguments, and then
3918 : * recursively apply eval_const_expressions to the whole argument list.
3919 : */
3920 45736 : if (process_args)
3921 : {
3922 45660 : args = expand_function_arguments(args, result_type, func_tuple);
3923 45660 : args = (List *) expression_tree_mutator((Node *) args,
3924 : eval_const_expressions_mutator,
3925 : (void *) context);
3926 : /* Argument processing done, give it back to the caller */
3927 45657 : *args_p = args;
3928 : }
3929 :
3930 : /* Now attempt simplification of the function call proper. */
3931 :
3932 45733 : newexpr = evaluate_function(funcid, result_type, result_typmod,
3933 : result_collid, input_collid,
3934 : args, funcvariadic,
3935 : func_tuple, context);
3936 :
3937 45489 : if (!newexpr && allow_non_const && OidIsValid(func_form->protransform))
3938 : {
3939 : /*
3940 : * Build a dummy FuncExpr node containing the simplified arg list. We
3941 : * use this approach to present a uniform interface to the transform
3942 : * function regardless of how the function is actually being invoked.
3943 : */
3944 : FuncExpr fexpr;
3945 :
3946 163 : fexpr.xpr.type = T_FuncExpr;
3947 163 : fexpr.funcid = funcid;
3948 163 : fexpr.funcresulttype = result_type;
3949 163 : fexpr.funcretset = func_form->proretset;
3950 163 : fexpr.funcvariadic = funcvariadic;
3951 163 : fexpr.funcformat = COERCE_EXPLICIT_CALL;
3952 163 : fexpr.funccollid = result_collid;
3953 163 : fexpr.inputcollid = input_collid;
3954 163 : fexpr.args = args;
3955 163 : fexpr.location = -1;
3956 :
3957 163 : newexpr = (Expr *)
3958 163 : DatumGetPointer(OidFunctionCall1(func_form->protransform,
3959 : PointerGetDatum(&fexpr)));
3960 : }
3961 :
3962 45489 : if (!newexpr && allow_non_const)
3963 39612 : newexpr = inline_function(funcid, result_type, result_collid,
3964 : input_collid, args, funcvariadic,
3965 : func_tuple, context);
3966 :
3967 45488 : ReleaseSysCache(func_tuple);
3968 :
3969 45488 : return newexpr;
3970 : }
3971 :
3972 : /*
3973 : * expand_function_arguments: convert named-notation args to positional args
3974 : * and/or insert default args, as needed
3975 : *
3976 : * If we need to change anything, the input argument list is copied, not
3977 : * modified.
3978 : *
3979 : * Note: this gets applied to operator argument lists too, even though the
3980 : * cases it handles should never occur there. This should be OK since it
3981 : * will fall through very quickly if there's nothing to do.
3982 : */
3983 : static List *
3984 45827 : expand_function_arguments(List *args, Oid result_type, HeapTuple func_tuple)
3985 : {
3986 45827 : Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3987 45827 : bool has_named_args = false;
3988 : ListCell *lc;
3989 :
3990 : /* Do we have any named arguments? */
3991 121212 : foreach(lc, args)
3992 : {
3993 75432 : Node *arg = (Node *) lfirst(lc);
3994 :
3995 75432 : if (IsA(arg, NamedArgExpr))
3996 : {
3997 47 : has_named_args = true;
3998 47 : break;
3999 : }
4000 : }
4001 :
4002 : /* If so, we must apply reorder_function_arguments */
4003 45827 : if (has_named_args)
4004 : {
4005 47 : args = reorder_function_arguments(args, func_tuple);
4006 : /* Recheck argument types and add casts if needed */
4007 47 : recheck_cast_function_args(args, result_type, func_tuple);
4008 : }
4009 45780 : else if (list_length(args) < funcform->pronargs)
4010 : {
4011 : /* No named args, but we seem to be short some defaults */
4012 184 : args = add_function_defaults(args, func_tuple);
4013 : /* Recheck argument types and add casts if needed */
4014 184 : recheck_cast_function_args(args, result_type, func_tuple);
4015 : }
4016 :
4017 45827 : return args;
4018 : }
4019 :
4020 : /*
4021 : * reorder_function_arguments: convert named-notation args to positional args
4022 : *
4023 : * This function also inserts default argument values as needed, since it's
4024 : * impossible to form a truly valid positional call without that.
4025 : */
4026 : static List *
4027 47 : reorder_function_arguments(List *args, HeapTuple func_tuple)
4028 : {
4029 47 : Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4030 47 : int pronargs = funcform->pronargs;
4031 47 : int nargsprovided = list_length(args);
4032 : Node *argarray[FUNC_MAX_ARGS];
4033 : ListCell *lc;
4034 : int i;
4035 :
4036 47 : Assert(nargsprovided <= pronargs);
4037 47 : if (pronargs > FUNC_MAX_ARGS)
4038 0 : elog(ERROR, "too many function arguments");
4039 47 : MemSet(argarray, 0, pronargs * sizeof(Node *));
4040 :
4041 : /* Deconstruct the argument list into an array indexed by argnumber */
4042 47 : i = 0;
4043 162 : foreach(lc, args)
4044 : {
4045 115 : Node *arg = (Node *) lfirst(lc);
4046 :
4047 115 : if (!IsA(arg, NamedArgExpr))
4048 : {
4049 : /* positional argument, assumed to precede all named args */
4050 19 : Assert(argarray[i] == NULL);
4051 19 : argarray[i++] = arg;
4052 : }
4053 : else
4054 : {
4055 96 : NamedArgExpr *na = (NamedArgExpr *) arg;
4056 :
4057 96 : Assert(argarray[na->argnumber] == NULL);
4058 96 : argarray[na->argnumber] = (Node *) na->arg;
4059 : }
4060 : }
4061 :
4062 : /*
4063 : * Fetch default expressions, if needed, and insert into array at proper
4064 : * locations (they aren't necessarily consecutive or all used)
4065 : */
4066 47 : if (nargsprovided < pronargs)
4067 : {
4068 29 : List *defaults = fetch_function_defaults(func_tuple);
4069 :
4070 29 : i = pronargs - funcform->pronargdefaults;
4071 137 : foreach(lc, defaults)
4072 : {
4073 108 : if (argarray[i] == NULL)
4074 68 : argarray[i] = (Node *) lfirst(lc);
4075 108 : i++;
4076 : }
4077 : }
4078 :
4079 : /* Now reconstruct the args list in proper order */
4080 47 : args = NIL;
4081 230 : for (i = 0; i < pronargs; i++)
4082 : {
4083 183 : Assert(argarray[i] != NULL);
4084 183 : args = lappend(args, argarray[i]);
4085 : }
4086 :
4087 47 : return args;
4088 : }
4089 :
4090 : /*
4091 : * add_function_defaults: add missing function arguments from its defaults
4092 : *
4093 : * This is used only when the argument list was positional to begin with,
4094 : * and so we know we just need to add defaults at the end.
4095 : */
4096 : static List *
4097 184 : add_function_defaults(List *args, HeapTuple func_tuple)
4098 : {
4099 184 : Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4100 184 : int nargsprovided = list_length(args);
4101 : List *defaults;
4102 : int ndelete;
4103 :
4104 : /* Get all the default expressions from the pg_proc tuple */
4105 184 : defaults = fetch_function_defaults(func_tuple);
4106 :
4107 : /* Delete any unused defaults from the list */
4108 184 : ndelete = nargsprovided + list_length(defaults) - funcform->pronargs;
4109 184 : if (ndelete < 0)
4110 0 : elog(ERROR, "not enough default arguments");
4111 381 : while (ndelete-- > 0)
4112 13 : defaults = list_delete_first(defaults);
4113 :
4114 : /* And form the combined argument list, not modifying the input list */
4115 184 : return list_concat(list_copy(args), defaults);
4116 : }
4117 :
4118 : /*
4119 : * fetch_function_defaults: get function's default arguments as expression list
4120 : */
4121 : static List *
4122 213 : fetch_function_defaults(HeapTuple func_tuple)
4123 : {
4124 : List *defaults;
4125 : Datum proargdefaults;
4126 : bool isnull;
4127 : char *str;
4128 :
4129 : /* The error cases here shouldn't happen, but check anyway */
4130 213 : proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
4131 : Anum_pg_proc_proargdefaults,
4132 : &isnull);
4133 213 : if (isnull)
4134 0 : elog(ERROR, "not enough default arguments");
4135 213 : str = TextDatumGetCString(proargdefaults);
4136 213 : defaults = castNode(List, stringToNode(str));
4137 213 : pfree(str);
4138 213 : return defaults;
4139 : }
4140 :
4141 : /*
4142 : * recheck_cast_function_args: recheck function args and typecast as needed
4143 : * after adding defaults.
4144 : *
4145 : * It is possible for some of the defaulted arguments to be polymorphic;
4146 : * therefore we can't assume that the default expressions have the correct
4147 : * data types already. We have to re-resolve polymorphics and do coercion
4148 : * just like the parser did.
4149 : *
4150 : * This should be a no-op if there are no polymorphic arguments,
4151 : * but we do it anyway to be sure.
4152 : *
4153 : * Note: if any casts are needed, the args list is modified in-place;
4154 : * caller should have already copied the list structure.
4155 : */
4156 : static void
4157 231 : recheck_cast_function_args(List *args, Oid result_type, HeapTuple func_tuple)
4158 : {
4159 231 : Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4160 : int nargs;
4161 : Oid actual_arg_types[FUNC_MAX_ARGS];
4162 : Oid declared_arg_types[FUNC_MAX_ARGS];
4163 : Oid rettype;
4164 : ListCell *lc;
4165 :
4166 231 : if (list_length(args) > FUNC_MAX_ARGS)
4167 0 : elog(ERROR, "too many function arguments");
4168 231 : nargs = 0;
4169 980 : foreach(lc, args)
4170 : {
4171 749 : actual_arg_types[nargs++] = exprType((Node *) lfirst(lc));
4172 : }
4173 231 : Assert(nargs == funcform->pronargs);
4174 231 : memcpy(declared_arg_types, funcform->proargtypes.values,
4175 231 : funcform->pronargs * sizeof(Oid));
4176 231 : rettype = enforce_generic_type_consistency(actual_arg_types,
4177 : declared_arg_types,
4178 : nargs,
4179 : funcform->prorettype,
4180 : false);
4181 : /* let's just check we got the same answer as the parser did ... */
4182 231 : if (rettype != result_type)
4183 0 : elog(ERROR, "function's resolved result type changed during planning");
4184 :
4185 : /* perform any necessary typecasting of arguments */
4186 231 : make_fn_arguments(NULL, args, actual_arg_types, declared_arg_types);
4187 231 : }
4188 :
4189 : /*
4190 : * evaluate_function: try to pre-evaluate a function call
4191 : *
4192 : * We can do this if the function is strict and has any constant-null inputs
4193 : * (just return a null constant), or if the function is immutable and has all
4194 : * constant inputs (call it and return the result as a Const node). In
4195 : * estimation mode we are willing to pre-evaluate stable functions too.
4196 : *
4197 : * Returns a simplified expression if successful, or NULL if cannot
4198 : * simplify the function.
4199 : */
4200 : static Expr *
4201 45733 : evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
4202 : Oid result_collid, Oid input_collid, List *args,
4203 : bool funcvariadic,
4204 : HeapTuple func_tuple,
4205 : eval_const_expressions_context *context)
4206 : {
4207 45733 : Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4208 45733 : bool has_nonconst_input = false;
4209 45733 : bool has_null_input = false;
4210 : ListCell *arg;
4211 : FuncExpr *newexpr;
4212 :
4213 : /*
4214 : * Can't simplify if it returns a set.
4215 : */
4216 45733 : if (funcform->proretset)
4217 1411 : return NULL;
4218 :
4219 : /*
4220 : * Can't simplify if it returns RECORD. The immediate problem is that it
4221 : * will be needing an expected tupdesc which we can't supply here.
4222 : *
4223 : * In the case where it has OUT parameters, it could get by without an
4224 : * expected tupdesc, but we still have issues: get_expr_result_type()
4225 : * doesn't know how to extract type info from a RECORD constant, and in
4226 : * the case of a NULL function result there doesn't seem to be any clean
4227 : * way to fix that. In view of the likelihood of there being still other
4228 : * gotchas, seems best to leave the function call unreduced.
4229 : */
4230 44322 : if (funcform->prorettype == RECORDOID)
4231 123 : return NULL;
4232 :
4233 : /*
4234 : * Check for constant inputs and especially constant-NULL inputs.
4235 : */
4236 117941 : foreach(arg, args)
4237 : {
4238 73742 : if (IsA(lfirst(arg), Const))
4239 32886 : has_null_input |= ((Const *) lfirst(arg))->constisnull;
4240 : else
4241 40856 : has_nonconst_input = true;
4242 : }
4243 :
4244 : /*
4245 : * If the function is strict and has a constant-NULL input, it will never
4246 : * be called at all, so we can replace the call by a NULL constant, even
4247 : * if there are other inputs that aren't constant, and even if the
4248 : * function is not otherwise immutable.
4249 : */
4250 44199 : if (funcform->proisstrict && has_null_input)
4251 52 : return (Expr *) makeNullConst(result_type, result_typmod,
4252 : result_collid);
4253 :
4254 : /*
4255 : * Otherwise, can simplify only if all inputs are constants. (For a
4256 : * non-strict function, constant NULL inputs are treated the same as
4257 : * constant non-NULL inputs.)
4258 : */
4259 44147 : if (has_nonconst_input)
4260 31471 : return NULL;
4261 :
4262 : /*
4263 : * Ordinarily we are only allowed to simplify immutable functions. But for
4264 : * purposes of estimation, we consider it okay to simplify functions that
4265 : * are merely stable; the risk that the result might change from planning
4266 : * time to execution time is worth taking in preference to not being able
4267 : * to estimate the value at all.
4268 : */
4269 12676 : if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
4270 : /* okay */ ;
4271 6645 : else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
4272 : /* okay */ ;
4273 : else
4274 6608 : return NULL;
4275 :
4276 : /*
4277 : * OK, looks like we can simplify this operator/function.
4278 : *
4279 : * Build a new FuncExpr node containing the already-simplified arguments.
4280 : */
4281 6068 : newexpr = makeNode(FuncExpr);
4282 6068 : newexpr->funcid = funcid;
4283 6068 : newexpr->funcresulttype = result_type;
4284 6068 : newexpr->funcretset = false;
4285 6068 : newexpr->funcvariadic = funcvariadic;
4286 6068 : newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4287 6068 : newexpr->funccollid = result_collid; /* doesn't matter */
4288 6068 : newexpr->inputcollid = input_collid;
4289 6068 : newexpr->args = args;
4290 6068 : newexpr->location = -1;
4291 :
4292 6068 : return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
4293 : result_collid);
4294 : }
4295 :
4296 : /*
4297 : * inline_function: try to expand a function call inline
4298 : *
4299 : * If the function is a sufficiently simple SQL-language function
4300 : * (just "SELECT expression"), then we can inline it and avoid the rather
4301 : * high per-call overhead of SQL functions. Furthermore, this can expose
4302 : * opportunities for constant-folding within the function expression.
4303 : *
4304 : * We have to beware of some special cases however. A directly or
4305 : * indirectly recursive function would cause us to recurse forever,
4306 : * so we keep track of which functions we are already expanding and
4307 : * do not re-expand them. Also, if a parameter is used more than once
4308 : * in the SQL-function body, we require it not to contain any volatile
4309 : * functions (volatiles might deliver inconsistent answers) nor to be
4310 : * unreasonably expensive to evaluate. The expensiveness check not only
4311 : * prevents us from doing multiple evaluations of an expensive parameter
4312 : * at runtime, but is a safety value to limit growth of an expression due
4313 : * to repeated inlining.
4314 : *
4315 : * We must also beware of changing the volatility or strictness status of
4316 : * functions by inlining them.
4317 : *
4318 : * Also, at the moment we can't inline functions returning RECORD. This
4319 : * doesn't work in the general case because it discards information such
4320 : * as OUT-parameter declarations.
4321 : *
4322 : * Also, context-dependent expression nodes in the argument list are trouble.
4323 : *
4324 : * Returns a simplified expression if successful, or NULL if cannot
4325 : * simplify the function.
4326 : */
4327 : static Expr *
4328 39612 : inline_function(Oid funcid, Oid result_type, Oid result_collid,
4329 : Oid input_collid, List *args,
4330 : bool funcvariadic,
4331 : HeapTuple func_tuple,
4332 : eval_const_expressions_context *context)
4333 : {
4334 39612 : Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4335 : char *src;
4336 : Datum tmp;
4337 : bool isNull;
4338 : bool modifyTargetList;
4339 : MemoryContext oldcxt;
4340 : MemoryContext mycxt;
4341 : inline_error_callback_arg callback_arg;
4342 : ErrorContextCallback sqlerrcontext;
4343 : FuncExpr *fexpr;
4344 : SQLFunctionParseInfoPtr pinfo;
4345 : ParseState *pstate;
4346 : List *raw_parsetree_list;
4347 : Query *querytree;
4348 : Node *newexpr;
4349 : int *usecounts;
4350 : ListCell *arg;
4351 : int i;
4352 :
4353 : /*
4354 : * Forget it if the function is not SQL-language or has other showstopper
4355 : * properties. (The nargs check is just paranoia.)
4356 : */
4357 44809 : if (funcform->prolang != SQLlanguageId ||
4358 10391 : funcform->prosecdef ||
4359 10204 : funcform->proretset ||
4360 9988 : funcform->prorettype == RECORDOID ||
4361 9953 : !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
4362 4975 : funcform->pronargs != list_length(args))
4363 34637 : return NULL;
4364 :
4365 : /* Check for recursive function, and give up trying to expand if so */
4366 4975 : if (list_member_oid(context->active_fns, funcid))
4367 2294 : return NULL;
4368 :
4369 : /* Check permission to call function (fail later, if not) */
4370 2681 : if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4371 1 : return NULL;
4372 :
4373 : /* Check whether a plugin wants to hook function entry/exit */
4374 2680 : if (FmgrHookIsNeeded(funcid))
4375 0 : return NULL;
4376 :
4377 : /*
4378 : * Make a temporary memory context, so that we don't leak all the stuff
4379 : * that parsing might create.
4380 : */
4381 2680 : mycxt = AllocSetContextCreate(CurrentMemoryContext,
4382 : "inline_function",
4383 : ALLOCSET_DEFAULT_SIZES);
4384 2680 : oldcxt = MemoryContextSwitchTo(mycxt);
4385 :
4386 : /* Fetch the function body */
4387 2680 : tmp = SysCacheGetAttr(PROCOID,
4388 : func_tuple,
4389 : Anum_pg_proc_prosrc,
4390 : &isNull);
4391 2680 : if (isNull)
4392 0 : elog(ERROR, "null prosrc for function %u", funcid);
4393 2680 : src = TextDatumGetCString(tmp);
4394 :
4395 : /*
4396 : * Setup error traceback support for ereport(). This is so that we can
4397 : * finger the function that bad information came from.
4398 : */
4399 2680 : callback_arg.proname = NameStr(funcform->proname);
4400 2680 : callback_arg.prosrc = src;
4401 :
4402 2680 : sqlerrcontext.callback = sql_inline_error_callback;
4403 2680 : sqlerrcontext.arg = (void *) &callback_arg;
4404 2680 : sqlerrcontext.previous = error_context_stack;
4405 2680 : error_context_stack = &sqlerrcontext;
4406 :
4407 : /*
4408 : * Set up to handle parameters while parsing the function body. We need a
4409 : * dummy FuncExpr node containing the already-simplified arguments to pass
4410 : * to prepare_sql_fn_parse_info. (It is really only needed if there are
4411 : * some polymorphic arguments, but for simplicity we always build it.)
4412 : */
4413 2680 : fexpr = makeNode(FuncExpr);
4414 2680 : fexpr->funcid = funcid;
4415 2680 : fexpr->funcresulttype = result_type;
4416 2680 : fexpr->funcretset = false;
4417 2680 : fexpr->funcvariadic = funcvariadic;
4418 2680 : fexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4419 2680 : fexpr->funccollid = result_collid; /* doesn't matter */
4420 2680 : fexpr->inputcollid = input_collid;
4421 2680 : fexpr->args = args;
4422 2680 : fexpr->location = -1;
4423 :
4424 2680 : pinfo = prepare_sql_fn_parse_info(func_tuple,
4425 : (Node *) fexpr,
4426 : input_collid);
4427 :
4428 : /*
4429 : * We just do parsing and parse analysis, not rewriting, because rewriting
4430 : * will not affect table-free-SELECT-only queries, which is all that we
4431 : * care about. Also, we can punt as soon as we detect more than one
4432 : * command in the function body.
4433 : */
4434 2680 : raw_parsetree_list = pg_parse_query(src);
4435 2680 : if (list_length(raw_parsetree_list) != 1)
4436 0 : goto fail;
4437 :
4438 2680 : pstate = make_parsestate(NULL);
4439 2680 : pstate->p_sourcetext = src;
4440 2680 : sql_fn_parser_setup(pstate, pinfo);
4441 :
4442 2680 : querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
4443 :
4444 2679 : free_parsestate(pstate);
4445 :
4446 : /*
4447 : * The single command must be a simple "SELECT expression".
4448 : *
4449 : * Note: if you change the tests involved in this, see also plpgsql's
4450 : * exec_simple_check_plan(). That generally needs to have the same idea
4451 : * of what's a "simple expression", so that inlining a function that
4452 : * previously wasn't inlined won't change plpgsql's conclusion.
4453 : */
4454 5358 : if (!IsA(querytree, Query) ||
4455 5347 : querytree->commandType != CMD_SELECT ||
4456 5328 : querytree->hasAggs ||
4457 5320 : querytree->hasWindowFuncs ||
4458 5320 : querytree->hasTargetSRFs ||
4459 5310 : querytree->hasSubLinks ||
4460 5300 : querytree->cteList ||
4461 5137 : querytree->rtable ||
4462 4974 : querytree->jointree->fromlist ||
4463 4974 : querytree->jointree->quals ||
4464 4974 : querytree->groupClause ||
4465 4974 : querytree->groupingSets ||
4466 4974 : querytree->havingQual ||
4467 4974 : querytree->windowClause ||
4468 4974 : querytree->distinctClause ||
4469 4974 : querytree->sortClause ||
4470 4974 : querytree->limitOffset ||
4471 4965 : querytree->limitCount ||
4472 4956 : querytree->setOperations ||
4473 2478 : list_length(querytree->targetList) != 1)
4474 : goto fail;
4475 :
4476 : /*
4477 : * Make sure the function (still) returns what it's declared to. This
4478 : * will raise an error if wrong, but that's okay since the function would
4479 : * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4480 : * a RelabelType if needed to make the tlist expression match the declared
4481 : * type of the function.
4482 : *
4483 : * Note: we do not try this until we have verified that no rewriting was
4484 : * needed; that's probably not important, but let's be careful.
4485 : */
4486 2472 : if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
4487 : &modifyTargetList, NULL))
4488 2 : goto fail; /* reject whole-tuple-result cases */
4489 :
4490 : /* Now we can grab the tlist expression */
4491 2470 : newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
4492 :
4493 : /* Assert that check_sql_fn_retval did the right thing */
4494 2470 : Assert(exprType(newexpr) == result_type);
4495 : /* It couldn't have made any dangerous tlist changes, either */
4496 2470 : Assert(!modifyTargetList);
4497 :
4498 : /*
4499 : * Additional validity checks on the expression. It mustn't be more
4500 : * volatile than the surrounding function (this is to avoid breaking hacks
4501 : * that involve pretending a function is immutable when it really ain't).
4502 : * If the surrounding function is declared strict, then the expression
4503 : * must contain only strict constructs and must use all of the function
4504 : * parameters (this is overkill, but an exact analysis is hard).
4505 : */
4506 2498 : if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
4507 28 : contain_mutable_functions(newexpr))
4508 : goto fail;
4509 2522 : else if (funcform->provolatile == PROVOLATILE_STABLE &&
4510 53 : contain_volatile_functions(newexpr))
4511 0 : goto fail;
4512 :
4513 2540 : if (funcform->proisstrict &&
4514 71 : contain_nonstrict_functions(newexpr))
4515 2 : goto fail;
4516 :
4517 : /*
4518 : * If any parameter expression contains a context-dependent node, we can't
4519 : * inline, for fear of putting such a node into the wrong context.
4520 : */
4521 2467 : if (contain_context_dependent_node((Node *) args))
4522 1 : goto fail;
4523 :
4524 : /*
4525 : * We may be able to do it; there are still checks on parameter usage to
4526 : * make, but those are most easily done in combination with the actual
4527 : * substitution of the inputs. So start building expression with inputs
4528 : * substituted.
4529 : */
4530 2466 : usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
4531 2466 : newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
4532 : args, usecounts);
4533 :
4534 : /* Now check for parameter usage */
4535 2466 : i = 0;
4536 2780 : foreach(arg, args)
4537 : {
4538 314 : Node *param = lfirst(arg);
4539 :
4540 314 : if (usecounts[i] == 0)
4541 : {
4542 : /* Param not used at all: uncool if func is strict */
4543 10 : if (funcform->proisstrict)
4544 0 : goto fail;
4545 : }
4546 304 : else if (usecounts[i] != 1)
4547 : {
4548 : /* Param used multiple times: uncool if expensive or volatile */
4549 : QualCost eval_cost;
4550 :
4551 : /*
4552 : * We define "expensive" as "contains any subplan or more than 10
4553 : * operators". Note that the subplan search has to be done
4554 : * explicitly, since cost_qual_eval() will barf on unplanned
4555 : * subselects.
4556 : */
4557 8 : if (contain_subplans(param))
4558 0 : goto fail;
4559 8 : cost_qual_eval(&eval_cost, list_make1(param), NULL);
4560 8 : if (eval_cost.startup + eval_cost.per_tuple >
4561 : 10 * cpu_operator_cost)
4562 0 : goto fail;
4563 :
4564 : /*
4565 : * Check volatility last since this is more expensive than the
4566 : * above tests
4567 : */
4568 8 : if (contain_volatile_functions(param))
4569 0 : goto fail;
4570 : }
4571 314 : i++;
4572 : }
4573 :
4574 : /*
4575 : * Whew --- we can make the substitution. Copy the modified expression
4576 : * out of the temporary memory context, and clean up.
4577 : */
4578 2466 : MemoryContextSwitchTo(oldcxt);
4579 :
4580 2466 : newexpr = copyObject(newexpr);
4581 :
4582 2466 : MemoryContextDelete(mycxt);
4583 :
4584 : /*
4585 : * If the result is of a collatable type, force the result to expose the
4586 : * correct collation. In most cases this does not matter, but it's
4587 : * possible that the function result is used directly as a sort key or in
4588 : * other places where we expect exprCollation() to tell the truth.
4589 : */
4590 2466 : if (OidIsValid(result_collid))
4591 : {
4592 79 : Oid exprcoll = exprCollation(newexpr);
4593 :
4594 79 : if (OidIsValid(exprcoll) && exprcoll != result_collid)
4595 : {
4596 0 : CollateExpr *newnode = makeNode(CollateExpr);
4597 :
4598 0 : newnode->arg = (Expr *) newexpr;
4599 0 : newnode->collOid = result_collid;
4600 0 : newnode->location = -1;
4601 :
4602 0 : newexpr = (Node *) newnode;
4603 : }
4604 : }
4605 :
4606 : /*
4607 : * Since there is now no trace of the function in the plan tree, we must
4608 : * explicitly record the plan's dependency on the function.
4609 : */
4610 2466 : if (context->root)
4611 2456 : record_plan_function_dependency(context->root, funcid);
4612 :
4613 : /*
4614 : * Recursively try to simplify the modified expression. Here we must add
4615 : * the current function to the context list of active functions.
4616 : */
4617 2466 : context->active_fns = lcons_oid(funcid, context->active_fns);
4618 2466 : newexpr = eval_const_expressions_mutator(newexpr, context);
4619 2466 : context->active_fns = list_delete_first(context->active_fns);
4620 :
4621 2466 : error_context_stack = sqlerrcontext.previous;
4622 :
4623 2466 : return (Expr *) newexpr;
4624 :
4625 : /* Here if func is not inlinable: release temp memory and return NULL */
4626 : fail:
4627 213 : MemoryContextSwitchTo(oldcxt);
4628 213 : MemoryContextDelete(mycxt);
4629 213 : error_context_stack = sqlerrcontext.previous;
4630 :
4631 213 : return NULL;
4632 : }
4633 :
4634 : /*
4635 : * Replace Param nodes by appropriate actual parameters
4636 : */
4637 : static Node *
4638 2466 : substitute_actual_parameters(Node *expr, int nargs, List *args,
4639 : int *usecounts)
4640 : {
4641 : substitute_actual_parameters_context context;
4642 :
4643 2466 : context.nargs = nargs;
4644 2466 : context.args = args;
4645 2466 : context.usecounts = usecounts;
4646 :
4647 2466 : return substitute_actual_parameters_mutator(expr, &context);
4648 : }
4649 :
4650 : static Node *
4651 5439 : substitute_actual_parameters_mutator(Node *node,
4652 : substitute_actual_parameters_context *context)
4653 : {
4654 5439 : if (node == NULL)
4655 2329 : return NULL;
4656 3110 : if (IsA(node, Param))
4657 : {
4658 312 : Param *param = (Param *) node;
4659 :
4660 312 : if (param->paramkind != PARAM_EXTERN)
4661 0 : elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
4662 312 : if (param->paramid <= 0 || param->paramid > context->nargs)
4663 0 : elog(ERROR, "invalid paramid: %d", param->paramid);
4664 :
4665 : /* Count usage of parameter */
4666 312 : context->usecounts[param->paramid - 1]++;
4667 :
4668 : /* Select the appropriate actual arg and replace the Param with it */
4669 : /* We don't need to copy at this time (it'll get done later) */
4670 312 : return list_nth(context->args, param->paramid - 1);
4671 : }
4672 2798 : return expression_tree_mutator(node, substitute_actual_parameters_mutator,
4673 : (void *) context);
4674 : }
4675 :
4676 : /*
4677 : * error context callback to let us supply a call-stack traceback
4678 : */
4679 : static void
4680 1 : sql_inline_error_callback(void *arg)
4681 : {
4682 1 : inline_error_callback_arg *callback_arg = (inline_error_callback_arg *) arg;
4683 : int syntaxerrposition;
4684 :
4685 : /* If it's a syntax error, convert to internal syntax error report */
4686 1 : syntaxerrposition = geterrposition();
4687 1 : if (syntaxerrposition > 0)
4688 : {
4689 0 : errposition(0);
4690 0 : internalerrposition(syntaxerrposition);
4691 0 : internalerrquery(callback_arg->prosrc);
4692 : }
4693 :
4694 1 : errcontext("SQL function \"%s\" during inlining", callback_arg->proname);
4695 1 : }
4696 :
4697 : /*
4698 : * evaluate_expr: pre-evaluate a constant expression
4699 : *
4700 : * We use the executor's routine ExecEvalExpr() to avoid duplication of
4701 : * code and ensure we get the same result as the executor would get.
4702 : */
4703 : static Expr *
4704 7462 : evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
4705 : Oid result_collation)
4706 : {
4707 : EState *estate;
4708 : ExprState *exprstate;
4709 : MemoryContext oldcontext;
4710 : Datum const_val;
4711 : bool const_is_null;
4712 : int16 resultTypLen;
4713 : bool resultTypByVal;
4714 :
4715 : /*
4716 : * To use the executor, we need an EState.
4717 : */
4718 7462 : estate = CreateExecutorState();
4719 :
4720 : /* We can use the estate's working context to avoid memory leaks. */
4721 7462 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4722 :
4723 : /* Make sure any opfuncids are filled in. */
4724 7462 : fix_opfuncids((Node *) expr);
4725 :
4726 : /*
4727 : * Prepare expr for execution. (Note: we can't use ExecPrepareExpr
4728 : * because it'd result in recursively invoking eval_const_expressions.)
4729 : */
4730 7462 : exprstate = ExecInitExpr(expr, NULL);
4731 :
4732 : /*
4733 : * And evaluate it.
4734 : *
4735 : * It is OK to use a default econtext because none of the ExecEvalExpr()
4736 : * code used in this situation will use econtext. That might seem
4737 : * fortuitous, but it's not so unreasonable --- a constant expression does
4738 : * not depend on context, by definition, n'est ce pas?
4739 : */
4740 7461 : const_val = ExecEvalExprSwitchContext(exprstate,
4741 7461 : GetPerTupleExprContext(estate),
4742 : &const_is_null);
4743 :
4744 : /* Get info needed about result datatype */
4745 7215 : get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
4746 :
4747 : /* Get back to outer memory context */
4748 7215 : MemoryContextSwitchTo(oldcontext);
4749 :
4750 : /*
4751 : * Must copy result out of sub-context used by expression eval.
4752 : *
4753 : * Also, if it's varlena, forcibly detoast it. This protects us against
4754 : * storing TOAST pointers into plans that might outlive the referenced
4755 : * data. (makeConst would handle detoasting anyway, but it's worth a few
4756 : * extra lines here so that we can do the copy and detoast in one step.)
4757 : */
4758 7215 : if (!const_is_null)
4759 : {
4760 7090 : if (resultTypLen == -1)
4761 4169 : const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
4762 : else
4763 2921 : const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
4764 : }
4765 :
4766 : /* Release all the junk we just created */
4767 7215 : FreeExecutorState(estate);
4768 :
4769 : /*
4770 : * Make the constant result node.
4771 : */
4772 7215 : return (Expr *) makeConst(result_type, result_typmod, result_collation,
4773 : resultTypLen,
4774 : const_val, const_is_null,
4775 : resultTypByVal);
4776 : }
4777 :
4778 :
4779 : /*
4780 : * inline_set_returning_function
4781 : * Attempt to "inline" a set-returning function in the FROM clause.
4782 : *
4783 : * "rte" is an RTE_FUNCTION rangetable entry. If it represents a call of a
4784 : * set-returning SQL function that can safely be inlined, expand the function
4785 : * and return the substitute Query structure. Otherwise, return NULL.
4786 : *
4787 : * This has a good deal of similarity to inline_function(), but that's
4788 : * for the non-set-returning case, and there are enough differences to
4789 : * justify separate functions.
4790 : */
4791 : Query *
4792 1336 : inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
4793 : {
4794 : RangeTblFunction *rtfunc;
4795 : FuncExpr *fexpr;
4796 : Oid func_oid;
4797 : HeapTuple func_tuple;
4798 : Form_pg_proc funcform;
4799 : char *src;
4800 : Datum tmp;
4801 : bool isNull;
4802 : bool modifyTargetList;
4803 : MemoryContext oldcxt;
4804 : MemoryContext mycxt;
4805 : List *saveInvalItems;
4806 : inline_error_callback_arg callback_arg;
4807 : ErrorContextCallback sqlerrcontext;
4808 : SQLFunctionParseInfoPtr pinfo;
4809 : List *raw_parsetree_list;
4810 : List *querytree_list;
4811 : Query *querytree;
4812 :
4813 1336 : Assert(rte->rtekind == RTE_FUNCTION);
4814 :
4815 : /*
4816 : * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
4817 : * own FROM clause, since that must cause infinite recursion at runtime.
4818 : * It will cause this code to recurse too, so check for stack overflow.
4819 : * (There's no need to do more.)
4820 : */
4821 1336 : check_stack_depth();
4822 :
4823 : /* Fail if the RTE has ORDINALITY - we don't implement that here. */
4824 1336 : if (rte->funcordinality)
4825 63 : return NULL;
4826 :
4827 : /* Fail if RTE isn't a single, simple FuncExpr */
4828 1273 : if (list_length(rte->functions) != 1)
4829 8 : return NULL;
4830 1265 : rtfunc = (RangeTblFunction *) linitial(rte->functions);
4831 :
4832 1265 : if (!IsA(rtfunc->funcexpr, FuncExpr))
4833 0 : return NULL;
4834 1265 : fexpr = (FuncExpr *) rtfunc->funcexpr;
4835 :
4836 1265 : func_oid = fexpr->funcid;
4837 :
4838 : /*
4839 : * The function must be declared to return a set, else inlining would
4840 : * change the results if the contained SELECT didn't return exactly one
4841 : * row.
4842 : */
4843 1265 : if (!fexpr->funcretset)
4844 261 : return NULL;
4845 :
4846 : /*
4847 : * Refuse to inline if the arguments contain any volatile functions or
4848 : * sub-selects. Volatile functions are rejected because inlining may
4849 : * result in the arguments being evaluated multiple times, risking a
4850 : * change in behavior. Sub-selects are rejected partly for implementation
4851 : * reasons (pushing them down another level might change their behavior)
4852 : * and partly because they're likely to be expensive and so multiple
4853 : * evaluation would be bad.
4854 : */
4855 2008 : if (contain_volatile_functions((Node *) fexpr->args) ||
4856 1004 : contain_subplans((Node *) fexpr->args))
4857 18 : return NULL;
4858 :
4859 : /* Check permission to call function (fail later, if not) */
4860 986 : if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4861 0 : return NULL;
4862 :
4863 : /* Check whether a plugin wants to hook function entry/exit */
4864 986 : if (FmgrHookIsNeeded(func_oid))
4865 0 : return NULL;
4866 :
4867 : /*
4868 : * OK, let's take a look at the function's pg_proc entry.
4869 : */
4870 986 : func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
4871 986 : if (!HeapTupleIsValid(func_tuple))
4872 0 : elog(ERROR, "cache lookup failed for function %u", func_oid);
4873 986 : funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4874 :
4875 : /*
4876 : * Forget it if the function is not SQL-language or has other showstopper
4877 : * properties. In particular it mustn't be declared STRICT, since we
4878 : * couldn't enforce that. It also mustn't be VOLATILE, because that is
4879 : * supposed to cause it to be executed with its own snapshot, rather than
4880 : * sharing the snapshot of the calling query. (Rechecking proretset is
4881 : * just paranoia.)
4882 : */
4883 1072 : if (funcform->prolang != SQLlanguageId ||
4884 166 : funcform->proisstrict ||
4885 89 : funcform->provolatile == PROVOLATILE_VOLATILE ||
4886 18 : funcform->prosecdef ||
4887 18 : !funcform->proretset ||
4888 9 : !heap_attisnull(func_tuple, Anum_pg_proc_proconfig))
4889 : {
4890 977 : ReleaseSysCache(func_tuple);
4891 977 : return NULL;
4892 : }
4893 :
4894 : /*
4895 : * Make a temporary memory context, so that we don't leak all the stuff
4896 : * that parsing might create.
4897 : */
4898 9 : mycxt = AllocSetContextCreate(CurrentMemoryContext,
4899 : "inline_set_returning_function",
4900 : ALLOCSET_DEFAULT_SIZES);
4901 9 : oldcxt = MemoryContextSwitchTo(mycxt);
4902 :
4903 : /*
4904 : * When we call eval_const_expressions below, it might try to add items to
4905 : * root->glob->invalItems. Since it is running in the temp context, those
4906 : * items will be in that context, and will need to be copied out if we're
4907 : * successful. Temporarily reset the list so that we can keep those items
4908 : * separate from the pre-existing list contents.
4909 : */
4910 9 : saveInvalItems = root->glob->invalItems;
4911 9 : root->glob->invalItems = NIL;
4912 :
4913 : /* Fetch the function body */
4914 9 : tmp = SysCacheGetAttr(PROCOID,
4915 : func_tuple,
4916 : Anum_pg_proc_prosrc,
4917 : &isNull);
4918 9 : if (isNull)
4919 0 : elog(ERROR, "null prosrc for function %u", func_oid);
4920 9 : src = TextDatumGetCString(tmp);
4921 :
4922 : /*
4923 : * Setup error traceback support for ereport(). This is so that we can
4924 : * finger the function that bad information came from.
4925 : */
4926 9 : callback_arg.proname = NameStr(funcform->proname);
4927 9 : callback_arg.prosrc = src;
4928 :
4929 9 : sqlerrcontext.callback = sql_inline_error_callback;
4930 9 : sqlerrcontext.arg = (void *) &callback_arg;
4931 9 : sqlerrcontext.previous = error_context_stack;
4932 9 : error_context_stack = &sqlerrcontext;
4933 :
4934 : /*
4935 : * Run eval_const_expressions on the function call. This is necessary to
4936 : * ensure that named-argument notation is converted to positional notation
4937 : * and any default arguments are inserted. It's a bit of overkill for the
4938 : * arguments, since they'll get processed again later, but no harm will be
4939 : * done.
4940 : */
4941 9 : fexpr = (FuncExpr *) eval_const_expressions(root, (Node *) fexpr);
4942 :
4943 : /* It should still be a call of the same function, but let's check */
4944 18 : if (!IsA(fexpr, FuncExpr) ||
4945 9 : fexpr->funcid != func_oid)
4946 : goto fail;
4947 :
4948 : /* Arg list length should now match the function */
4949 9 : if (list_length(fexpr->args) != funcform->pronargs)
4950 0 : goto fail;
4951 :
4952 : /*
4953 : * Set up to handle parameters while parsing the function body. We can
4954 : * use the FuncExpr just created as the input for
4955 : * prepare_sql_fn_parse_info.
4956 : */
4957 9 : pinfo = prepare_sql_fn_parse_info(func_tuple,
4958 : (Node *) fexpr,
4959 : fexpr->inputcollid);
4960 :
4961 : /*
4962 : * Parse, analyze, and rewrite (unlike inline_function(), we can't skip
4963 : * rewriting here). We can fail as soon as we find more than one query,
4964 : * though.
4965 : */
4966 9 : raw_parsetree_list = pg_parse_query(src);
4967 9 : if (list_length(raw_parsetree_list) != 1)
4968 0 : goto fail;
4969 :
4970 9 : querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
4971 : src,
4972 : (ParserSetupHook) sql_fn_parser_setup,
4973 : pinfo, NULL);
4974 9 : if (list_length(querytree_list) != 1)
4975 0 : goto fail;
4976 9 : querytree = linitial(querytree_list);
4977 :
4978 : /*
4979 : * The single command must be a plain SELECT.
4980 : */
4981 18 : if (!IsA(querytree, Query) ||
4982 9 : querytree->commandType != CMD_SELECT)
4983 : goto fail;
4984 :
4985 : /*
4986 : * Make sure the function (still) returns what it's declared to. This
4987 : * will raise an error if wrong, but that's okay since the function would
4988 : * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4989 : * RelabelType(s) and/or NULL columns if needed to make the tlist
4990 : * expression(s) match the declared type of the function.
4991 : *
4992 : * If the function returns a composite type, don't inline unless the check
4993 : * shows it's returning a whole tuple result; otherwise what it's
4994 : * returning is a single composite column which is not what we need.
4995 : */
4996 9 : if (!check_sql_fn_retval(func_oid, fexpr->funcresulttype,
4997 : querytree_list,
4998 8 : &modifyTargetList, NULL) &&
4999 16 : (get_typtype(fexpr->funcresulttype) == TYPTYPE_COMPOSITE ||
5000 8 : fexpr->funcresulttype == RECORDOID))
5001 : goto fail; /* reject not-whole-tuple-result cases */
5002 :
5003 : /*
5004 : * If we had to modify the tlist to make it match, and the statement is
5005 : * one in which changing the tlist contents could change semantics, we
5006 : * have to punt and not inline.
5007 : */
5008 9 : if (modifyTargetList)
5009 1 : goto fail;
5010 :
5011 : /*
5012 : * If it returns RECORD, we have to check against the column type list
5013 : * provided in the RTE; check_sql_fn_retval can't do that. (If no match,
5014 : * we just fail to inline, rather than complaining; see notes for
5015 : * tlist_matches_coltypelist.) We don't have to do this for functions
5016 : * with declared OUT parameters, even though their funcresulttype is
5017 : * RECORDOID, so check get_func_result_type too.
5018 : */
5019 8 : if (fexpr->funcresulttype == RECORDOID &&
5020 0 : get_func_result_type(func_oid, NULL, NULL) == TYPEFUNC_RECORD &&
5021 0 : !tlist_matches_coltypelist(querytree->targetList,
5022 : rtfunc->funccoltypes))
5023 0 : goto fail;
5024 :
5025 : /*
5026 : * Looks good --- substitute parameters into the query.
5027 : */
5028 16 : querytree = substitute_actual_srf_parameters(querytree,
5029 8 : funcform->pronargs,
5030 : fexpr->args);
5031 :
5032 : /*
5033 : * Copy the modified query out of the temporary memory context, and clean
5034 : * up.
5035 : */
5036 8 : MemoryContextSwitchTo(oldcxt);
5037 :
5038 8 : querytree = copyObject(querytree);
5039 :
5040 : /* copy up any new invalItems, too */
5041 16 : root->glob->invalItems = list_concat(saveInvalItems,
5042 8 : copyObject(root->glob->invalItems));
5043 :
5044 8 : MemoryContextDelete(mycxt);
5045 8 : error_context_stack = sqlerrcontext.previous;
5046 8 : ReleaseSysCache(func_tuple);
5047 :
5048 : /*
5049 : * We don't have to fix collations here because the upper query is already
5050 : * parsed, ie, the collations in the RTE are what count.
5051 : */
5052 :
5053 : /*
5054 : * Since there is now no trace of the function in the plan tree, we must
5055 : * explicitly record the plan's dependency on the function.
5056 : */
5057 8 : record_plan_function_dependency(root, func_oid);
5058 :
5059 8 : return querytree;
5060 :
5061 : /* Here if func is not inlinable: release temp memory and return NULL */
5062 : fail:
5063 1 : MemoryContextSwitchTo(oldcxt);
5064 1 : root->glob->invalItems = saveInvalItems;
5065 1 : MemoryContextDelete(mycxt);
5066 1 : error_context_stack = sqlerrcontext.previous;
5067 1 : ReleaseSysCache(func_tuple);
5068 :
5069 1 : return NULL;
5070 : }
5071 :
5072 : /*
5073 : * Replace Param nodes by appropriate actual parameters
5074 : *
5075 : * This is just enough different from substitute_actual_parameters()
5076 : * that it needs its own code.
5077 : */
5078 : static Query *
5079 8 : substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
5080 : {
5081 : substitute_actual_srf_parameters_context context;
5082 :
5083 8 : context.nargs = nargs;
5084 8 : context.args = args;
5085 8 : context.sublevels_up = 1;
5086 :
5087 8 : return query_tree_mutator(expr,
5088 : substitute_actual_srf_parameters_mutator,
5089 : &context,
5090 : 0);
5091 : }
5092 :
5093 : static Node *
5094 176 : substitute_actual_srf_parameters_mutator(Node *node,
5095 : substitute_actual_srf_parameters_context *context)
5096 : {
5097 : Node *result;
5098 :
5099 176 : if (node == NULL)
5100 84 : return NULL;
5101 92 : if (IsA(node, Query))
5102 : {
5103 0 : context->sublevels_up++;
5104 0 : result = (Node *) query_tree_mutator((Query *) node,
5105 : substitute_actual_srf_parameters_mutator,
5106 : (void *) context,
5107 : 0);
5108 0 : context->sublevels_up--;
5109 0 : return result;
5110 : }
5111 92 : if (IsA(node, Param))
5112 : {
5113 9 : Param *param = (Param *) node;
5114 :
5115 9 : if (param->paramkind == PARAM_EXTERN)
5116 : {
5117 9 : if (param->paramid <= 0 || param->paramid > context->nargs)
5118 0 : elog(ERROR, "invalid paramid: %d", param->paramid);
5119 :
5120 : /*
5121 : * Since the parameter is being inserted into a subquery, we must
5122 : * adjust levels.
5123 : */
5124 9 : result = copyObject(list_nth(context->args, param->paramid - 1));
5125 9 : IncrementVarSublevelsUp(result, context->sublevels_up, 0);
5126 9 : return result;
5127 : }
5128 : }
5129 83 : return expression_tree_mutator(node,
5130 : substitute_actual_srf_parameters_mutator,
5131 : (void *) context);
5132 : }
5133 :
5134 : /*
5135 : * Check whether a SELECT targetlist emits the specified column types,
5136 : * to see if it's safe to inline a function returning record.
5137 : *
5138 : * We insist on exact match here. The executor allows binary-coercible
5139 : * cases too, but we don't have a way to preserve the correct column types
5140 : * in the correct places if we inline the function in such a case.
5141 : *
5142 : * Note that we only check type OIDs not typmods; this agrees with what the
5143 : * executor would do at runtime, and attributing a specific typmod to a
5144 : * function result is largely wishful thinking anyway.
5145 : */
5146 : static bool
5147 0 : tlist_matches_coltypelist(List *tlist, List *coltypelist)
5148 : {
5149 : ListCell *tlistitem;
5150 : ListCell *clistitem;
5151 :
5152 0 : clistitem = list_head(coltypelist);
5153 0 : foreach(tlistitem, tlist)
5154 : {
5155 0 : TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
5156 : Oid coltype;
5157 :
5158 0 : if (tle->resjunk)
5159 0 : continue; /* ignore junk columns */
5160 :
5161 0 : if (clistitem == NULL)
5162 0 : return false; /* too many tlist items */
5163 :
5164 0 : coltype = lfirst_oid(clistitem);
5165 0 : clistitem = lnext(clistitem);
5166 :
5167 0 : if (exprType((Node *) tle->expr) != coltype)
5168 0 : return false; /* column type mismatch */
5169 : }
5170 :
5171 0 : if (clistitem != NULL)
5172 0 : return false; /* too few tlist items */
5173 :
5174 0 : return true;
5175 : }
|