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