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