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