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