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