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