]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/util/clauses.c
Get rid of some old and crufty global variables in the planner. When
[postgresql] / src / backend / optimizer / util / clauses.c
1 /*-------------------------------------------------------------------------
2  *
3  * clauses.c
4  *        routines to manipulate qualification clauses
5  *
6  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.235 2007/02/19 07:03:30 tgl Exp $
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 "catalog/pg_aggregate.h"
23 #include "catalog/pg_language.h"
24 #include "catalog/pg_operator.h"
25 #include "catalog/pg_proc.h"
26 #include "catalog/pg_type.h"
27 #include "executor/executor.h"
28 #include "executor/functions.h"
29 #include "miscadmin.h"
30 #include "nodes/makefuncs.h"
31 #include "optimizer/clauses.h"
32 #include "optimizer/cost.h"
33 #include "optimizer/planmain.h"
34 #include "optimizer/planner.h"
35 #include "optimizer/var.h"
36 #include "parser/analyze.h"
37 #include "parser/parse_clause.h"
38 #include "parser/parse_coerce.h"
39 #include "parser/parse_expr.h"
40 #include "tcop/tcopprot.h"
41 #include "utils/acl.h"
42 #include "utils/builtins.h"
43 #include "utils/datum.h"
44 #include "utils/lsyscache.h"
45 #include "utils/memutils.h"
46 #include "utils/syscache.h"
47 #include "utils/typcache.h"
48
49
50 typedef struct
51 {
52         ParamListInfo boundParams;
53         List       *active_fns;
54         Node       *case_val;
55         bool            estimate;
56 } eval_const_expressions_context;
57
58 typedef struct
59 {
60         int                     nargs;
61         List       *args;
62         int                *usecounts;
63 } substitute_actual_parameters_context;
64
65 static bool contain_agg_clause_walker(Node *node, void *context);
66 static bool count_agg_clauses_walker(Node *node, AggClauseCounts *counts);
67 static bool expression_returns_set_walker(Node *node, void *context);
68 static bool expression_returns_set_rows_walker(Node *node, double *count);
69 static bool contain_subplans_walker(Node *node, void *context);
70 static bool contain_mutable_functions_walker(Node *node, void *context);
71 static bool contain_volatile_functions_walker(Node *node, void *context);
72 static bool contain_nonstrict_functions_walker(Node *node, void *context);
73 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
74 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
75 static bool set_coercionform_dontcare_walker(Node *node, void *context);
76 static Node *eval_const_expressions_mutator(Node *node,
77                                                            eval_const_expressions_context *context);
78 static List *simplify_or_arguments(List *args,
79                                           eval_const_expressions_context *context,
80                                           bool *haveNull, bool *forceTrue);
81 static List *simplify_and_arguments(List *args,
82                                            eval_const_expressions_context *context,
83                                            bool *haveNull, bool *forceFalse);
84 static Expr *simplify_boolean_equality(List *args);
85 static Expr *simplify_function(Oid funcid, Oid result_type, List *args,
86                                   bool allow_inline,
87                                   eval_const_expressions_context *context);
88 static Expr *evaluate_function(Oid funcid, Oid result_type, List *args,
89                                   HeapTuple func_tuple,
90                                   eval_const_expressions_context *context);
91 static Expr *inline_function(Oid funcid, Oid result_type, List *args,
92                                 HeapTuple func_tuple,
93                                 eval_const_expressions_context *context);
94 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
95                                                          int *usecounts);
96 static Node *substitute_actual_parameters_mutator(Node *node,
97                                                           substitute_actual_parameters_context *context);
98 static void sql_inline_error_callback(void *arg);
99 static Expr *evaluate_expr(Expr *expr, Oid result_type);
100
101
102 /*****************************************************************************
103  *              OPERATOR clause functions
104  *****************************************************************************/
105
106 /*
107  * make_opclause
108  *        Creates an operator clause given its operator info, left operand,
109  *        and right operand (pass NULL to create single-operand clause).
110  */
111 Expr *
112 make_opclause(Oid opno, Oid opresulttype, bool opretset,
113                           Expr *leftop, Expr *rightop)
114 {
115         OpExpr     *expr = makeNode(OpExpr);
116
117         expr->opno = opno;
118         expr->opfuncid = InvalidOid;
119         expr->opresulttype = opresulttype;
120         expr->opretset = opretset;
121         if (rightop)
122                 expr->args = list_make2(leftop, rightop);
123         else
124                 expr->args = list_make1(leftop);
125         return (Expr *) expr;
126 }
127
128 /*
129  * get_leftop
130  *
131  * Returns the left operand of a clause of the form (op expr expr)
132  *              or (op expr)
133  */
134 Node *
135 get_leftop(Expr *clause)
136 {
137         OpExpr     *expr = (OpExpr *) clause;
138
139         if (expr->args != NIL)
140                 return linitial(expr->args);
141         else
142                 return NULL;
143 }
144
145 /*
146  * get_rightop
147  *
148  * Returns the right operand in a clause of the form (op expr expr).
149  * NB: result will be NULL if applied to a unary op clause.
150  */
151 Node *
152 get_rightop(Expr *clause)
153 {
154         OpExpr     *expr = (OpExpr *) clause;
155
156         if (list_length(expr->args) >= 2)
157                 return lsecond(expr->args);
158         else
159                 return NULL;
160 }
161
162 /*****************************************************************************
163  *              NOT clause functions
164  *****************************************************************************/
165
166 /*
167  * not_clause
168  *
169  * Returns t iff this is a 'not' clause: (NOT expr).
170  */
171 bool
172 not_clause(Node *clause)
173 {
174         return (clause != NULL &&
175                         IsA(clause, BoolExpr) &&
176                         ((BoolExpr *) clause)->boolop == NOT_EXPR);
177 }
178
179 /*
180  * make_notclause
181  *
182  * Create a 'not' clause given the expression to be negated.
183  */
184 Expr *
185 make_notclause(Expr *notclause)
186 {
187         BoolExpr   *expr = makeNode(BoolExpr);
188
189         expr->boolop = NOT_EXPR;
190         expr->args = list_make1(notclause);
191         return (Expr *) expr;
192 }
193
194 /*
195  * get_notclausearg
196  *
197  * Retrieve the clause within a 'not' clause
198  */
199 Expr *
200 get_notclausearg(Expr *notclause)
201 {
202         return linitial(((BoolExpr *) notclause)->args);
203 }
204
205 /*****************************************************************************
206  *              OR clause functions
207  *****************************************************************************/
208
209 /*
210  * or_clause
211  *
212  * Returns t iff the clause is an 'or' clause: (OR { expr }).
213  */
214 bool
215 or_clause(Node *clause)
216 {
217         return (clause != NULL &&
218                         IsA(clause, BoolExpr) &&
219                         ((BoolExpr *) clause)->boolop == OR_EXPR);
220 }
221
222 /*
223  * make_orclause
224  *
225  * Creates an 'or' clause given a list of its subclauses.
226  */
227 Expr *
228 make_orclause(List *orclauses)
229 {
230         BoolExpr   *expr = makeNode(BoolExpr);
231
232         expr->boolop = OR_EXPR;
233         expr->args = orclauses;
234         return (Expr *) expr;
235 }
236
237 /*****************************************************************************
238  *              AND clause functions
239  *****************************************************************************/
240
241
242 /*
243  * and_clause
244  *
245  * Returns t iff its argument is an 'and' clause: (AND { expr }).
246  */
247 bool
248 and_clause(Node *clause)
249 {
250         return (clause != NULL &&
251                         IsA(clause, BoolExpr) &&
252                         ((BoolExpr *) clause)->boolop == AND_EXPR);
253 }
254
255 /*
256  * make_andclause
257  *
258  * Creates an 'and' clause given a list of its subclauses.
259  */
260 Expr *
261 make_andclause(List *andclauses)
262 {
263         BoolExpr   *expr = makeNode(BoolExpr);
264
265         expr->boolop = AND_EXPR;
266         expr->args = andclauses;
267         return (Expr *) expr;
268 }
269
270 /*
271  * make_and_qual
272  *
273  * Variant of make_andclause for ANDing two qual conditions together.
274  * Qual conditions have the property that a NULL nodetree is interpreted
275  * as 'true'.
276  *
277  * NB: this makes no attempt to preserve AND/OR flatness; so it should not
278  * be used on a qual that has already been run through prepqual.c.
279  */
280 Node *
281 make_and_qual(Node *qual1, Node *qual2)
282 {
283         if (qual1 == NULL)
284                 return qual2;
285         if (qual2 == NULL)
286                 return qual1;
287         return (Node *) make_andclause(list_make2(qual1, qual2));
288 }
289
290 /*
291  * Sometimes (such as in the input of ExecQual), we use lists of expression
292  * nodes with implicit AND semantics.
293  *
294  * These functions convert between an AND-semantics expression list and the
295  * ordinary representation of a boolean expression.
296  *
297  * Note that an empty list is considered equivalent to TRUE.
298  */
299 Expr *
300 make_ands_explicit(List *andclauses)
301 {
302         if (andclauses == NIL)
303                 return (Expr *) makeBoolConst(true, false);
304         else if (list_length(andclauses) == 1)
305                 return (Expr *) linitial(andclauses);
306         else
307                 return make_andclause(andclauses);
308 }
309
310 List *
311 make_ands_implicit(Expr *clause)
312 {
313         /*
314          * NB: because the parser sets the qual field to NULL in a query that has
315          * no WHERE clause, we must consider a NULL input clause as TRUE, even
316          * though one might more reasonably think it FALSE.  Grumble. If this
317          * causes trouble, consider changing the parser's behavior.
318          */
319         if (clause == NULL)
320                 return NIL;                             /* NULL -> NIL list == TRUE */
321         else if (and_clause((Node *) clause))
322                 return ((BoolExpr *) clause)->args;
323         else if (IsA(clause, Const) &&
324                          !((Const *) clause)->constisnull &&
325                          DatumGetBool(((Const *) clause)->constvalue))
326                 return NIL;                             /* constant TRUE input -> NIL list */
327         else
328                 return list_make1(clause);
329 }
330
331
332 /*****************************************************************************
333  *              Aggregate-function clause manipulation
334  *****************************************************************************/
335
336 /*
337  * contain_agg_clause
338  *        Recursively search for Aggref nodes within a clause.
339  *
340  *        Returns true if any aggregate found.
341  *
342  * This does not descend into subqueries, and so should be used only after
343  * reduction of sublinks to subplans, or in contexts where it's known there
344  * are no subqueries.  There mustn't be outer-aggregate references either.
345  *
346  * (If you want something like this but able to deal with subqueries,
347  * see rewriteManip.c's checkExprHasAggs().)
348  */
349 bool
350 contain_agg_clause(Node *clause)
351 {
352         return contain_agg_clause_walker(clause, NULL);
353 }
354
355 static bool
356 contain_agg_clause_walker(Node *node, void *context)
357 {
358         if (node == NULL)
359                 return false;
360         if (IsA(node, Aggref))
361         {
362                 Assert(((Aggref *) node)->agglevelsup == 0);
363                 return true;                    /* abort the tree traversal and return true */
364         }
365         Assert(!IsA(node, SubLink));
366         return expression_tree_walker(node, contain_agg_clause_walker, context);
367 }
368
369 /*
370  * count_agg_clauses
371  *        Recursively count the Aggref nodes in an expression tree.
372  *
373  *        Note: this also checks for nested aggregates, which are an error.
374  *
375  * We not only count the nodes, but attempt to estimate the total space
376  * needed for their transition state values if all are evaluated in parallel
377  * (as would be done in a HashAgg plan).  See AggClauseCounts for the exact
378  * set of statistics returned.
379  *
380  * NOTE that the counts are ADDED to those already in *counts ... so the
381  * caller is responsible for zeroing the struct initially.
382  *
383  * This does not descend into subqueries, and so should be used only after
384  * reduction of sublinks to subplans, or in contexts where it's known there
385  * are no subqueries.  There mustn't be outer-aggregate references either.
386  */
387 void
388 count_agg_clauses(Node *clause, AggClauseCounts *counts)
389 {
390         /* no setup needed */
391         count_agg_clauses_walker(clause, counts);
392 }
393
394 static bool
395 count_agg_clauses_walker(Node *node, AggClauseCounts *counts)
396 {
397         if (node == NULL)
398                 return false;
399         if (IsA(node, Aggref))
400         {
401                 Aggref     *aggref = (Aggref *) node;
402                 Oid                *inputTypes;
403                 int                     numArguments;
404                 HeapTuple       aggTuple;
405                 Form_pg_aggregate aggform;
406                 Oid                     aggtranstype;
407                 int                     i;
408                 ListCell   *l;
409
410                 Assert(aggref->agglevelsup == 0);
411                 counts->numAggs++;
412                 if (aggref->aggdistinct)
413                         counts->numDistinctAggs++;
414
415                 /* extract argument types */
416                 numArguments = list_length(aggref->args);
417                 inputTypes = (Oid *) palloc(sizeof(Oid) * numArguments);
418                 i = 0;
419                 foreach(l, aggref->args)
420                 {
421                         inputTypes[i++] = exprType((Node *) lfirst(l));
422                 }
423
424                 /* fetch aggregate transition datatype from pg_aggregate */
425                 aggTuple = SearchSysCache(AGGFNOID,
426                                                                   ObjectIdGetDatum(aggref->aggfnoid),
427                                                                   0, 0, 0);
428                 if (!HeapTupleIsValid(aggTuple))
429                         elog(ERROR, "cache lookup failed for aggregate %u",
430                                  aggref->aggfnoid);
431                 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
432                 aggtranstype = aggform->aggtranstype;
433                 ReleaseSysCache(aggTuple);
434
435                 /* resolve actual type of transition state, if polymorphic */
436                 if (aggtranstype == ANYARRAYOID || aggtranstype == ANYELEMENTOID)
437                 {
438                         /* have to fetch the agg's declared input types... */
439                         Oid                *declaredArgTypes;
440                         int                     agg_nargs;
441
442                         (void) get_func_signature(aggref->aggfnoid,
443                                                                           &declaredArgTypes, &agg_nargs);
444                         Assert(agg_nargs == numArguments);
445                         aggtranstype = enforce_generic_type_consistency(inputTypes,
446                                                                                                                         declaredArgTypes,
447                                                                                                                         agg_nargs,
448                                                                                                                         aggtranstype);
449                         pfree(declaredArgTypes);
450                 }
451
452                 /*
453                  * If the transition type is pass-by-value then it doesn't add
454                  * anything to the required size of the hashtable.      If it is
455                  * pass-by-reference then we have to add the estimated size of the
456                  * value itself, plus palloc overhead.
457                  */
458                 if (!get_typbyval(aggtranstype))
459                 {
460                         int32           aggtranstypmod;
461                         int32           avgwidth;
462
463                         /*
464                          * If transition state is of same type as first input, assume it's
465                          * the same typmod (same width) as well.  This works for cases
466                          * like MAX/MIN and is probably somewhat reasonable otherwise.
467                          */
468                         if (numArguments > 0 && aggtranstype == inputTypes[0])
469                                 aggtranstypmod = exprTypmod((Node *) linitial(aggref->args));
470                         else
471                                 aggtranstypmod = -1;
472
473                         avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
474                         avgwidth = MAXALIGN(avgwidth);
475
476                         counts->transitionSpace += avgwidth + 2 * sizeof(void *);
477                 }
478
479                 /*
480                  * Complain if the aggregate's arguments contain any aggregates;
481                  * nested agg functions are semantically nonsensical.
482                  */
483                 if (contain_agg_clause((Node *) aggref->args))
484                         ereport(ERROR,
485                                         (errcode(ERRCODE_GROUPING_ERROR),
486                                          errmsg("aggregate function calls cannot be nested")));
487
488                 /*
489                  * Having checked that, we need not recurse into the argument.
490                  */
491                 return false;
492         }
493         Assert(!IsA(node, SubLink));
494         return expression_tree_walker(node, count_agg_clauses_walker,
495                                                                   (void *) counts);
496 }
497
498
499 /*****************************************************************************
500  *              Support for expressions returning sets
501  *****************************************************************************/
502
503 /*
504  * expression_returns_set
505  *        Test whether an expression returns a set result.
506  *
507  * Because we use expression_tree_walker(), this can also be applied to
508  * whole targetlists; it'll produce TRUE if any one of the tlist items
509  * returns a set.
510  */
511 bool
512 expression_returns_set(Node *clause)
513 {
514         return expression_returns_set_walker(clause, NULL);
515 }
516
517 static bool
518 expression_returns_set_walker(Node *node, void *context)
519 {
520         if (node == NULL)
521                 return false;
522         if (IsA(node, FuncExpr))
523         {
524                 FuncExpr   *expr = (FuncExpr *) node;
525
526                 if (expr->funcretset)
527                         return true;
528                 /* else fall through to check args */
529         }
530         if (IsA(node, OpExpr))
531         {
532                 OpExpr     *expr = (OpExpr *) node;
533
534                 if (expr->opretset)
535                         return true;
536                 /* else fall through to check args */
537         }
538
539         /* Avoid recursion for some cases that can't return a set */
540         if (IsA(node, Aggref))
541                 return false;
542         if (IsA(node, DistinctExpr))
543                 return false;
544         if (IsA(node, ScalarArrayOpExpr))
545                 return false;
546         if (IsA(node, BoolExpr))
547                 return false;
548         if (IsA(node, SubLink))
549                 return false;
550         if (IsA(node, SubPlan))
551                 return false;
552         if (IsA(node, ArrayExpr))
553                 return false;
554         if (IsA(node, RowExpr))
555                 return false;
556         if (IsA(node, RowCompareExpr))
557                 return false;
558         if (IsA(node, CoalesceExpr))
559                 return false;
560         if (IsA(node, MinMaxExpr))
561                 return false;
562         if (IsA(node, XmlExpr))
563                 return false;
564         if (IsA(node, NullIfExpr))
565                 return false;
566
567         return expression_tree_walker(node, expression_returns_set_walker,
568                                                                   context);
569 }
570
571 /*
572  * expression_returns_set_rows
573  *        Estimate the number of rows in a set result.
574  *
575  * We use the product of the rowcount estimates of all the functions in
576  * the given tree.  The result is 1 if there are no set-returning functions.
577  */
578 double
579 expression_returns_set_rows(Node *clause)
580 {
581         double          result = 1;
582
583         (void) expression_returns_set_rows_walker(clause, &result);
584         return result;
585 }
586
587 static bool
588 expression_returns_set_rows_walker(Node *node, double *count)
589 {
590         if (node == NULL)
591                 return false;
592         if (IsA(node, FuncExpr))
593         {
594                 FuncExpr   *expr = (FuncExpr *) node;
595
596                 if (expr->funcretset)
597                         *count *= get_func_rows(expr->funcid);
598         }
599         if (IsA(node, OpExpr))
600         {
601                 OpExpr     *expr = (OpExpr *) node;
602
603                 if (expr->opretset)
604                 {
605                         set_opfuncid(expr);
606                         *count *= get_func_rows(expr->opfuncid);
607                 }
608         }
609
610         /* Avoid recursion for some cases that can't return a set */
611         if (IsA(node, Aggref))
612                 return false;
613         if (IsA(node, DistinctExpr))
614                 return false;
615         if (IsA(node, ScalarArrayOpExpr))
616                 return false;
617         if (IsA(node, BoolExpr))
618                 return false;
619         if (IsA(node, SubLink))
620                 return false;
621         if (IsA(node, SubPlan))
622                 return false;
623         if (IsA(node, ArrayExpr))
624                 return false;
625         if (IsA(node, RowExpr))
626                 return false;
627         if (IsA(node, RowCompareExpr))
628                 return false;
629         if (IsA(node, CoalesceExpr))
630                 return false;
631         if (IsA(node, MinMaxExpr))
632                 return false;
633         if (IsA(node, XmlExpr))
634                 return false;
635         if (IsA(node, NullIfExpr))
636                 return false;
637
638         return expression_tree_walker(node, expression_returns_set_rows_walker,
639                                                                   (void *) count);
640 }
641
642
643 /*****************************************************************************
644  *              Subplan clause manipulation
645  *****************************************************************************/
646
647 /*
648  * contain_subplans
649  *        Recursively search for subplan nodes within a clause.
650  *
651  * If we see a SubLink node, we will return TRUE.  This is only possible if
652  * the expression tree hasn't yet been transformed by subselect.c.  We do not
653  * know whether the node will produce a true subplan or just an initplan,
654  * but we make the conservative assumption that it will be a subplan.
655  *
656  * Returns true if any subplan found.
657  */
658 bool
659 contain_subplans(Node *clause)
660 {
661         return contain_subplans_walker(clause, NULL);
662 }
663
664 static bool
665 contain_subplans_walker(Node *node, void *context)
666 {
667         if (node == NULL)
668                 return false;
669         if (IsA(node, SubPlan) ||
670                 IsA(node, SubLink))
671                 return true;                    /* abort the tree traversal and return true */
672         return expression_tree_walker(node, contain_subplans_walker, context);
673 }
674
675
676 /*****************************************************************************
677  *              Check clauses for mutable functions
678  *****************************************************************************/
679
680 /*
681  * contain_mutable_functions
682  *        Recursively search for mutable functions within a clause.
683  *
684  * Returns true if any mutable function (or operator implemented by a
685  * mutable function) is found.  This test is needed so that we don't
686  * mistakenly think that something like "WHERE random() < 0.5" can be treated
687  * as a constant qualification.
688  *
689  * XXX we do not examine sub-selects to see if they contain uses of
690  * mutable functions.  It's not real clear if that is correct or not...
691  */
692 bool
693 contain_mutable_functions(Node *clause)
694 {
695         return contain_mutable_functions_walker(clause, NULL);
696 }
697
698 static bool
699 contain_mutable_functions_walker(Node *node, void *context)
700 {
701         if (node == NULL)
702                 return false;
703         if (IsA(node, FuncExpr))
704         {
705                 FuncExpr   *expr = (FuncExpr *) node;
706
707                 if (func_volatile(expr->funcid) != PROVOLATILE_IMMUTABLE)
708                         return true;
709                 /* else fall through to check args */
710         }
711         if (IsA(node, OpExpr))
712         {
713                 OpExpr     *expr = (OpExpr *) node;
714
715                 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
716                         return true;
717                 /* else fall through to check args */
718         }
719         if (IsA(node, DistinctExpr))
720         {
721                 DistinctExpr *expr = (DistinctExpr *) node;
722
723                 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
724                         return true;
725                 /* else fall through to check args */
726         }
727         if (IsA(node, ScalarArrayOpExpr))
728         {
729                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
730
731                 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
732                         return true;
733                 /* else fall through to check args */
734         }
735         if (IsA(node, NullIfExpr))
736         {
737                 NullIfExpr *expr = (NullIfExpr *) node;
738
739                 if (op_volatile(expr->opno) != PROVOLATILE_IMMUTABLE)
740                         return true;
741                 /* else fall through to check args */
742         }
743         if (IsA(node, RowCompareExpr))
744         {
745                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
746                 ListCell   *opid;
747
748                 foreach(opid, rcexpr->opnos)
749                 {
750                         if (op_volatile(lfirst_oid(opid)) != PROVOLATILE_IMMUTABLE)
751                                 return true;
752                 }
753                 /* else fall through to check args */
754         }
755         return expression_tree_walker(node, contain_mutable_functions_walker,
756                                                                   context);
757 }
758
759
760 /*****************************************************************************
761  *              Check clauses for volatile functions
762  *****************************************************************************/
763
764 /*
765  * contain_volatile_functions
766  *        Recursively search for volatile functions within a clause.
767  *
768  * Returns true if any volatile function (or operator implemented by a
769  * volatile function) is found. This test prevents invalid conversions
770  * of volatile expressions into indexscan quals.
771  *
772  * XXX we do not examine sub-selects to see if they contain uses of
773  * volatile functions.  It's not real clear if that is correct or not...
774  */
775 bool
776 contain_volatile_functions(Node *clause)
777 {
778         return contain_volatile_functions_walker(clause, NULL);
779 }
780
781 static bool
782 contain_volatile_functions_walker(Node *node, void *context)
783 {
784         if (node == NULL)
785                 return false;
786         if (IsA(node, FuncExpr))
787         {
788                 FuncExpr   *expr = (FuncExpr *) node;
789
790                 if (func_volatile(expr->funcid) == PROVOLATILE_VOLATILE)
791                         return true;
792                 /* else fall through to check args */
793         }
794         if (IsA(node, OpExpr))
795         {
796                 OpExpr     *expr = (OpExpr *) node;
797
798                 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
799                         return true;
800                 /* else fall through to check args */
801         }
802         if (IsA(node, DistinctExpr))
803         {
804                 DistinctExpr *expr = (DistinctExpr *) node;
805
806                 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
807                         return true;
808                 /* else fall through to check args */
809         }
810         if (IsA(node, ScalarArrayOpExpr))
811         {
812                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
813
814                 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
815                         return true;
816                 /* else fall through to check args */
817         }
818         if (IsA(node, NullIfExpr))
819         {
820                 NullIfExpr *expr = (NullIfExpr *) node;
821
822                 if (op_volatile(expr->opno) == PROVOLATILE_VOLATILE)
823                         return true;
824                 /* else fall through to check args */
825         }
826         if (IsA(node, RowCompareExpr))
827         {
828                 /* RowCompare probably can't have volatile ops, but check anyway */
829                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
830                 ListCell   *opid;
831
832                 foreach(opid, rcexpr->opnos)
833                 {
834                         if (op_volatile(lfirst_oid(opid)) == PROVOLATILE_VOLATILE)
835                                 return true;
836                 }
837                 /* else fall through to check args */
838         }
839         return expression_tree_walker(node, contain_volatile_functions_walker,
840                                                                   context);
841 }
842
843
844 /*****************************************************************************
845  *              Check clauses for nonstrict functions
846  *****************************************************************************/
847
848 /*
849  * contain_nonstrict_functions
850  *        Recursively search for nonstrict functions within a clause.
851  *
852  * Returns true if any nonstrict construct is found --- ie, anything that
853  * could produce non-NULL output with a NULL input.
854  *
855  * The idea here is that the caller has verified that the expression contains
856  * one or more Var or Param nodes (as appropriate for the caller's need), and
857  * now wishes to prove that the expression result will be NULL if any of these
858  * inputs is NULL.      If we return false, then the proof succeeded.
859  */
860 bool
861 contain_nonstrict_functions(Node *clause)
862 {
863         return contain_nonstrict_functions_walker(clause, NULL);
864 }
865
866 static bool
867 contain_nonstrict_functions_walker(Node *node, void *context)
868 {
869         if (node == NULL)
870                 return false;
871         if (IsA(node, Aggref))
872         {
873                 /* an aggregate could return non-null with null input */
874                 return true;
875         }
876         if (IsA(node, ArrayRef))
877         {
878                 /* array assignment is nonstrict, but subscripting is strict */
879                 if (((ArrayRef *) node)->refassgnexpr != NULL)
880                         return true;
881                 /* else fall through to check args */
882         }
883         if (IsA(node, FuncExpr))
884         {
885                 FuncExpr   *expr = (FuncExpr *) node;
886
887                 if (!func_strict(expr->funcid))
888                         return true;
889                 /* else fall through to check args */
890         }
891         if (IsA(node, OpExpr))
892         {
893                 OpExpr     *expr = (OpExpr *) node;
894
895                 if (!op_strict(expr->opno))
896                         return true;
897                 /* else fall through to check args */
898         }
899         if (IsA(node, DistinctExpr))
900         {
901                 /* IS DISTINCT FROM is inherently non-strict */
902                 return true;
903         }
904         if (IsA(node, ScalarArrayOpExpr))
905         {
906                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
907
908                 if (!is_strict_saop(expr, false))
909                         return true;
910                 /* else fall through to check args */
911         }
912         if (IsA(node, BoolExpr))
913         {
914                 BoolExpr   *expr = (BoolExpr *) node;
915
916                 switch (expr->boolop)
917                 {
918                         case AND_EXPR:
919                         case OR_EXPR:
920                                 /* AND, OR are inherently non-strict */
921                                 return true;
922                         default:
923                                 break;
924                 }
925         }
926         if (IsA(node, SubLink))
927         {
928                 /* In some cases a sublink might be strict, but in general not */
929                 return true;
930         }
931         if (IsA(node, SubPlan))
932                 return true;
933         if (IsA(node, FieldStore))
934                 return true;
935         if (IsA(node, CaseExpr))
936                 return true;
937         if (IsA(node, CaseWhen))
938                 return true;
939         if (IsA(node, ArrayExpr))
940                 return true;
941         if (IsA(node, RowExpr))
942                 return true;
943         if (IsA(node, RowCompareExpr))
944                 return true;
945         if (IsA(node, CoalesceExpr))
946                 return true;
947         if (IsA(node, MinMaxExpr))
948                 return true;
949         if (IsA(node, XmlExpr))
950                 return true;
951         if (IsA(node, NullIfExpr))
952                 return true;
953         if (IsA(node, NullTest))
954                 return true;
955         if (IsA(node, BooleanTest))
956                 return true;
957         return expression_tree_walker(node, contain_nonstrict_functions_walker,
958                                                                   context);
959 }
960
961
962 /*
963  * find_nonnullable_rels
964  *              Determine which base rels are forced nonnullable by given clause.
965  *
966  * Returns the set of all Relids that are referenced in the clause in such
967  * a way that the clause cannot possibly return TRUE if any of these Relids
968  * is an all-NULL row.  (It is OK to err on the side of conservatism; hence
969  * the analysis here is simplistic.)
970  *
971  * The semantics here are subtly different from contain_nonstrict_functions:
972  * that function is concerned with NULL results from arbitrary expressions,
973  * but here we assume that the input is a Boolean expression, and wish to
974  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
975  * the expression to have been AND/OR flattened and converted to implicit-AND
976  * format.
977  *
978  * top_level is TRUE while scanning top-level AND/OR structure; here, showing
979  * the result is either FALSE or NULL is good enough.  top_level is FALSE when
980  * we have descended below a NOT or a strict function: now we must be able to
981  * prove that the subexpression goes to NULL.
982  *
983  * We don't use expression_tree_walker here because we don't want to descend
984  * through very many kinds of nodes; only the ones we can be sure are strict.
985  */
986 Relids
987 find_nonnullable_rels(Node *clause)
988 {
989         return find_nonnullable_rels_walker(clause, true);
990 }
991
992 static Relids
993 find_nonnullable_rels_walker(Node *node, bool top_level)
994 {
995         Relids          result = NULL;
996         ListCell   *l;
997
998         if (node == NULL)
999                 return NULL;
1000         if (IsA(node, Var))
1001         {
1002                 Var                *var = (Var *) node;
1003
1004                 if (var->varlevelsup == 0)
1005                         result = bms_make_singleton(var->varno);
1006         }
1007         else if (IsA(node, List))
1008         {
1009                 /*
1010                  * At top level, we are examining an implicit-AND list: if any of
1011                  * the arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1012                  * If not at top level, we are examining the arguments of a strict
1013                  * function: if any of them produce NULL then the result of the
1014                  * function must be NULL.  So in both cases, the set of nonnullable
1015                  * rels is the union of those found in the arms, and we pass down
1016                  * the top_level flag unmodified.
1017                  */
1018                 foreach(l, (List *) node)
1019                 {
1020                         result = bms_join(result,
1021                                                           find_nonnullable_rels_walker(lfirst(l),
1022                                                                                                                    top_level));
1023                 }
1024         }
1025         else if (IsA(node, FuncExpr))
1026         {
1027                 FuncExpr   *expr = (FuncExpr *) node;
1028
1029                 if (func_strict(expr->funcid))
1030                         result = find_nonnullable_rels_walker((Node *) expr->args, false);
1031         }
1032         else if (IsA(node, OpExpr))
1033         {
1034                 OpExpr     *expr = (OpExpr *) node;
1035
1036                 if (op_strict(expr->opno))
1037                         result = find_nonnullable_rels_walker((Node *) expr->args, false);
1038         }
1039         else if (IsA(node, ScalarArrayOpExpr))
1040         {
1041                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1042
1043                 if (is_strict_saop(expr, true))
1044                         result = find_nonnullable_rels_walker((Node *) expr->args, false);
1045         }
1046         else if (IsA(node, BoolExpr))
1047         {
1048                 BoolExpr   *expr = (BoolExpr *) node;
1049
1050                 switch (expr->boolop)
1051                 {
1052                         case AND_EXPR:
1053                                 /* At top level we can just recurse (to the List case) */
1054                                 if (top_level)
1055                                 {
1056                                         result = find_nonnullable_rels_walker((Node *) expr->args,
1057                                                                                                                   top_level);
1058                                         break;
1059                                 }
1060                                 /*
1061                                  * Below top level, even if one arm produces NULL, the result
1062                                  * could be FALSE (hence not NULL).  However, if *all* the
1063                                  * arms produce NULL then the result is NULL, so we can
1064                                  * take the intersection of the sets of nonnullable rels,
1065                                  * just as for OR.  Fall through to share code.
1066                                  */
1067                                 /* FALL THRU */
1068                         case OR_EXPR:
1069                                 /*
1070                                  * OR is strict if all of its arms are, so we can take the
1071                                  * intersection of the sets of nonnullable rels for each arm.
1072                                  * This works for both values of top_level.
1073                                  */
1074                                 foreach(l, expr->args)
1075                                 {
1076                                         Relids          subresult;
1077
1078                                         subresult = find_nonnullable_rels_walker(lfirst(l),
1079                                                                                                                          top_level);
1080                                         if (result == NULL)                             /* first subresult? */
1081                                                 result = subresult;
1082                                         else
1083                                                 result = bms_int_members(result, subresult);
1084                                         /*
1085                                          * If the intersection is empty, we can stop looking.
1086                                          * This also justifies the test for first-subresult above.
1087                                          */
1088                                         if (bms_is_empty(result))
1089                                                 break;
1090                                 }
1091                                 break;
1092                         case NOT_EXPR:
1093                                 /* NOT will return null if its arg is null */
1094                                 result = find_nonnullable_rels_walker((Node *) expr->args,
1095                                                                                                           false);
1096                                 break;
1097                         default:
1098                                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1099                                 break;
1100                 }
1101         }
1102         else if (IsA(node, RelabelType))
1103         {
1104                 RelabelType *expr = (RelabelType *) node;
1105
1106                 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1107         }
1108         else if (IsA(node, ConvertRowtypeExpr))
1109         {
1110                 /* not clear this is useful, but it can't hurt */
1111                 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1112
1113                 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1114         }
1115         else if (IsA(node, NullTest))
1116         {
1117                 /* IS NOT NULL can be considered strict, but only at top level */
1118                 NullTest   *expr = (NullTest *) node;
1119
1120                 if (top_level && expr->nulltesttype == IS_NOT_NULL)
1121                         result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1122         }
1123         else if (IsA(node, BooleanTest))
1124         {
1125                 /* Boolean tests that reject NULL are strict at top level */
1126                 BooleanTest *expr = (BooleanTest *) node;
1127
1128                 if (top_level &&
1129                         (expr->booltesttype == IS_TRUE ||
1130                          expr->booltesttype == IS_FALSE ||
1131                          expr->booltesttype == IS_NOT_UNKNOWN))
1132                         result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1133         }
1134         return result;
1135 }
1136
1137 /*
1138  * Can we treat a ScalarArrayOpExpr as strict?
1139  *
1140  * If "falseOK" is true, then a "false" result can be considered strict,
1141  * else we need to guarantee an actual NULL result for NULL input.
1142  *
1143  * "foo op ALL array" is strict if the op is strict *and* we can prove
1144  * that the array input isn't an empty array.  We can check that
1145  * for the cases of an array constant and an ARRAY[] construct.
1146  *
1147  * "foo op ANY array" is strict in the falseOK sense if the op is strict.
1148  * If not falseOK, the test is the same as for "foo op ALL array".
1149  */
1150 static bool
1151 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
1152 {
1153         Node       *rightop;
1154
1155         /* The contained operator must be strict. */
1156         if (!op_strict(expr->opno))
1157                 return false;
1158         /* If ANY and falseOK, that's all we need to check. */
1159         if (expr->useOr && falseOK)
1160                 return true;
1161         /* Else, we have to see if the array is provably non-empty. */
1162         Assert(list_length(expr->args) == 2);
1163         rightop = (Node *) lsecond(expr->args);
1164         if (rightop && IsA(rightop, Const))
1165         {
1166                 Datum           arraydatum = ((Const *) rightop)->constvalue;
1167                 bool            arrayisnull = ((Const *) rightop)->constisnull;
1168                 ArrayType  *arrayval;
1169                 int                     nitems;
1170
1171                 if (arrayisnull)
1172                         return false;
1173                 arrayval = DatumGetArrayTypeP(arraydatum);
1174                 nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1175                 if (nitems > 0)
1176                         return true;
1177         }
1178         else if (rightop && IsA(rightop, ArrayExpr))
1179         {
1180                 ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
1181
1182                 if (arrayexpr->elements != NIL && !arrayexpr->multidims)
1183                         return true;
1184         }
1185         return false;
1186 }
1187
1188
1189 /*****************************************************************************
1190  *              Check for "pseudo-constant" clauses
1191  *****************************************************************************/
1192
1193 /*
1194  * is_pseudo_constant_clause
1195  *        Detect whether an expression is "pseudo constant", ie, it contains no
1196  *        variables of the current query level and no uses of volatile functions.
1197  *        Such an expr is not necessarily a true constant: it can still contain
1198  *        Params and outer-level Vars, not to mention functions whose results
1199  *        may vary from one statement to the next.      However, the expr's value
1200  *        will be constant over any one scan of the current query, so it can be
1201  *        used as, eg, an indexscan key.
1202  *
1203  * CAUTION: this function omits to test for one very important class of
1204  * not-constant expressions, namely aggregates (Aggrefs).  In current usage
1205  * this is only applied to WHERE clauses and so a check for Aggrefs would be
1206  * a waste of cycles; but be sure to also check contain_agg_clause() if you
1207  * want to know about pseudo-constness in other contexts.
1208  */
1209 bool
1210 is_pseudo_constant_clause(Node *clause)
1211 {
1212         /*
1213          * We could implement this check in one recursive scan.  But since the
1214          * check for volatile functions is both moderately expensive and unlikely
1215          * to fail, it seems better to look for Vars first and only check for
1216          * volatile functions if we find no Vars.
1217          */
1218         if (!contain_var_clause(clause) &&
1219                 !contain_volatile_functions(clause))
1220                 return true;
1221         return false;
1222 }
1223
1224 /*
1225  * is_pseudo_constant_clause_relids
1226  *        Same as above, except caller already has available the var membership
1227  *        of the expression; this lets us avoid the contain_var_clause() scan.
1228  */
1229 bool
1230 is_pseudo_constant_clause_relids(Node *clause, Relids relids)
1231 {
1232         if (bms_is_empty(relids) &&
1233                 !contain_volatile_functions(clause))
1234                 return true;
1235         return false;
1236 }
1237
1238
1239 /*****************************************************************************
1240  *              Tests on clauses of queries
1241  *
1242  * Possibly this code should go someplace else, since this isn't quite the
1243  * same meaning of "clause" as is used elsewhere in this module.  But I can't
1244  * think of a better place for it...
1245  *****************************************************************************/
1246
1247 /*
1248  * Test whether a query uses DISTINCT ON, ie, has a distinct-list that is
1249  * not the same as the set of output columns.
1250  */
1251 bool
1252 has_distinct_on_clause(Query *query)
1253 {
1254         ListCell   *l;
1255
1256         /* Is there a DISTINCT clause at all? */
1257         if (query->distinctClause == NIL)
1258                 return false;
1259
1260         /*
1261          * If the DISTINCT list contains all the nonjunk targetlist items, and
1262          * nothing else (ie, no junk tlist items), then it's a simple DISTINCT,
1263          * else it's DISTINCT ON.  We do not require the lists to be in the same
1264          * order (since the parser may have adjusted the DISTINCT clause ordering
1265          * to agree with ORDER BY).  Furthermore, a non-DISTINCT junk tlist item
1266          * that is in the sortClause is also evidence of DISTINCT ON, since we
1267          * don't allow ORDER BY on junk tlist items when plain DISTINCT is used.
1268          *
1269          * This code assumes that the DISTINCT list is valid, ie, all its entries
1270          * match some entry of the tlist.
1271          */
1272         foreach(l, query->targetList)
1273         {
1274                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1275
1276                 if (tle->ressortgroupref == 0)
1277                 {
1278                         if (tle->resjunk)
1279                                 continue;               /* we can ignore unsorted junk cols */
1280                         return true;            /* definitely not in DISTINCT list */
1281                 }
1282                 if (targetIsInSortList(tle, InvalidOid, query->distinctClause))
1283                 {
1284                         if (tle->resjunk)
1285                                 return true;    /* junk TLE in DISTINCT means DISTINCT ON */
1286                         /* else this TLE is okay, keep looking */
1287                 }
1288                 else
1289                 {
1290                         /* This TLE is not in DISTINCT list */
1291                         if (!tle->resjunk)
1292                                 return true;    /* non-junk, non-DISTINCT, so DISTINCT ON */
1293                         if (targetIsInSortList(tle, InvalidOid, query->sortClause))
1294                                 return true;    /* sorted, non-distinct junk */
1295                         /* unsorted junk is okay, keep looking */
1296                 }
1297         }
1298         /* It's a simple DISTINCT */
1299         return false;
1300 }
1301
1302 /*
1303  * Test whether a query uses simple DISTINCT, ie, has a distinct-list that
1304  * is the same as the set of output columns.
1305  */
1306 bool
1307 has_distinct_clause(Query *query)
1308 {
1309         /* Is there a DISTINCT clause at all? */
1310         if (query->distinctClause == NIL)
1311                 return false;
1312
1313         /* It's DISTINCT if it's not DISTINCT ON */
1314         return !has_distinct_on_clause(query);
1315 }
1316
1317
1318 /*****************************************************************************
1319  *                                                                                                                                                       *
1320  *              General clause-manipulating routines                                                             *
1321  *                                                                                                                                                       *
1322  *****************************************************************************/
1323
1324 /*
1325  * NumRelids
1326  *              (formerly clause_relids)
1327  *
1328  * Returns the number of different relations referenced in 'clause'.
1329  */
1330 int
1331 NumRelids(Node *clause)
1332 {
1333         Relids          varnos = pull_varnos(clause);
1334         int                     result = bms_num_members(varnos);
1335
1336         bms_free(varnos);
1337         return result;
1338 }
1339
1340 /*
1341  * CommuteOpExpr: commute a binary operator clause
1342  *
1343  * XXX the clause is destructively modified!
1344  */
1345 void
1346 CommuteOpExpr(OpExpr *clause)
1347 {
1348         Oid                     opoid;
1349         Node       *temp;
1350
1351         /* Sanity checks: caller is at fault if these fail */
1352         if (!is_opclause(clause) ||
1353                 list_length(clause->args) != 2)
1354                 elog(ERROR, "cannot commute non-binary-operator clause");
1355
1356         opoid = get_commutator(clause->opno);
1357
1358         if (!OidIsValid(opoid))
1359                 elog(ERROR, "could not find commutator for operator %u",
1360                          clause->opno);
1361
1362         /*
1363          * modify the clause in-place!
1364          */
1365         clause->opno = opoid;
1366         clause->opfuncid = InvalidOid;
1367         /* opresulttype and opretset are assumed not to change */
1368
1369         temp = linitial(clause->args);
1370         linitial(clause->args) = lsecond(clause->args);
1371         lsecond(clause->args) = temp;
1372 }
1373
1374 /*
1375  * CommuteRowCompareExpr: commute a RowCompareExpr clause
1376  *
1377  * XXX the clause is destructively modified!
1378  */
1379 void
1380 CommuteRowCompareExpr(RowCompareExpr *clause)
1381 {
1382         List       *newops;
1383         List       *temp;
1384         ListCell   *l;
1385
1386         /* Sanity checks: caller is at fault if these fail */
1387         if (!IsA(clause, RowCompareExpr))
1388                 elog(ERROR, "expected a RowCompareExpr");
1389
1390         /* Build list of commuted operators */
1391         newops = NIL;
1392         foreach(l, clause->opnos)
1393         {
1394                 Oid                     opoid = lfirst_oid(l);
1395
1396                 opoid = get_commutator(opoid);
1397                 if (!OidIsValid(opoid))
1398                         elog(ERROR, "could not find commutator for operator %u",
1399                                  lfirst_oid(l));
1400                 newops = lappend_oid(newops, opoid);
1401         }
1402
1403         /*
1404          * modify the clause in-place!
1405          */
1406         switch (clause->rctype)
1407         {
1408                 case ROWCOMPARE_LT:
1409                         clause->rctype = ROWCOMPARE_GT;
1410                         break;
1411                 case ROWCOMPARE_LE:
1412                         clause->rctype = ROWCOMPARE_GE;
1413                         break;
1414                 case ROWCOMPARE_GE:
1415                         clause->rctype = ROWCOMPARE_LE;
1416                         break;
1417                 case ROWCOMPARE_GT:
1418                         clause->rctype = ROWCOMPARE_LT;
1419                         break;
1420                 default:
1421                         elog(ERROR, "unexpected RowCompare type: %d",
1422                                  (int) clause->rctype);
1423                         break;
1424         }
1425
1426         clause->opnos = newops;
1427
1428         /*
1429          * Note: we need not change the opfamilies list; we assume any btree
1430          * opfamily containing an operator will also contain its commutator.
1431          */
1432
1433         temp = clause->largs;
1434         clause->largs = clause->rargs;
1435         clause->rargs = temp;
1436 }
1437
1438 /*
1439  * strip_implicit_coercions: remove implicit coercions at top level of tree
1440  *
1441  * Note: there isn't any useful thing we can do with a RowExpr here, so
1442  * just return it unchanged, even if it's marked as an implicit coercion.
1443  */
1444 Node *
1445 strip_implicit_coercions(Node *node)
1446 {
1447         if (node == NULL)
1448                 return NULL;
1449         if (IsA(node, FuncExpr))
1450         {
1451                 FuncExpr   *f = (FuncExpr *) node;
1452
1453                 if (f->funcformat == COERCE_IMPLICIT_CAST)
1454                         return strip_implicit_coercions(linitial(f->args));
1455         }
1456         else if (IsA(node, RelabelType))
1457         {
1458                 RelabelType *r = (RelabelType *) node;
1459
1460                 if (r->relabelformat == COERCE_IMPLICIT_CAST)
1461                         return strip_implicit_coercions((Node *) r->arg);
1462         }
1463         else if (IsA(node, ConvertRowtypeExpr))
1464         {
1465                 ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
1466
1467                 if (c->convertformat == COERCE_IMPLICIT_CAST)
1468                         return strip_implicit_coercions((Node *) c->arg);
1469         }
1470         else if (IsA(node, CoerceToDomain))
1471         {
1472                 CoerceToDomain *c = (CoerceToDomain *) node;
1473
1474                 if (c->coercionformat == COERCE_IMPLICIT_CAST)
1475                         return strip_implicit_coercions((Node *) c->arg);
1476         }
1477         return node;
1478 }
1479
1480 /*
1481  * set_coercionform_dontcare: set all CoercionForm fields to COERCE_DONTCARE
1482  *
1483  * This is used to make index expressions and index predicates more easily
1484  * comparable to clauses of queries.  CoercionForm is not semantically
1485  * significant (for cases where it does matter, the significant info is
1486  * coded into the coercion function arguments) so we can ignore it during
1487  * comparisons.  Thus, for example, an index on "foo::int4" can match an
1488  * implicit coercion to int4.
1489  *
1490  * Caution: the passed expression tree is modified in-place.
1491  */
1492 void
1493 set_coercionform_dontcare(Node *node)
1494 {
1495         (void) set_coercionform_dontcare_walker(node, NULL);
1496 }
1497
1498 static bool
1499 set_coercionform_dontcare_walker(Node *node, void *context)
1500 {
1501         if (node == NULL)
1502                 return false;
1503         if (IsA(node, FuncExpr))
1504                 ((FuncExpr *) node)->funcformat = COERCE_DONTCARE;
1505         else if (IsA(node, RelabelType))
1506                 ((RelabelType *) node)->relabelformat = COERCE_DONTCARE;
1507         else if (IsA(node, ConvertRowtypeExpr))
1508                 ((ConvertRowtypeExpr *) node)->convertformat = COERCE_DONTCARE;
1509         else if (IsA(node, RowExpr))
1510                 ((RowExpr *) node)->row_format = COERCE_DONTCARE;
1511         else if (IsA(node, CoerceToDomain))
1512                 ((CoerceToDomain *) node)->coercionformat = COERCE_DONTCARE;
1513         return expression_tree_walker(node, set_coercionform_dontcare_walker,
1514                                                                   context);
1515 }
1516
1517 /*
1518  * Helper for eval_const_expressions: check that datatype of an attribute
1519  * is still what it was when the expression was parsed.  This is needed to
1520  * guard against improper simplification after ALTER COLUMN TYPE.  (XXX we
1521  * may well need to make similar checks elsewhere?)
1522  */
1523 static bool
1524 rowtype_field_matches(Oid rowtypeid, int fieldnum,
1525                                           Oid expectedtype, int32 expectedtypmod)
1526 {
1527         TupleDesc       tupdesc;
1528         Form_pg_attribute attr;
1529
1530         /* No issue for RECORD, since there is no way to ALTER such a type */
1531         if (rowtypeid == RECORDOID)
1532                 return true;
1533         tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
1534         if (fieldnum <= 0 || fieldnum > tupdesc->natts)
1535         {
1536                 ReleaseTupleDesc(tupdesc);
1537                 return false;
1538         }
1539         attr = tupdesc->attrs[fieldnum - 1];
1540         if (attr->attisdropped ||
1541                 attr->atttypid != expectedtype ||
1542                 attr->atttypmod != expectedtypmod)
1543         {
1544                 ReleaseTupleDesc(tupdesc);
1545                 return false;
1546         }
1547         ReleaseTupleDesc(tupdesc);
1548         return true;
1549 }
1550
1551
1552 /*--------------------
1553  * eval_const_expressions
1554  *
1555  * Reduce any recognizably constant subexpressions of the given
1556  * expression tree, for example "2 + 2" => "4".  More interestingly,
1557  * we can reduce certain boolean expressions even when they contain
1558  * non-constant subexpressions: "x OR true" => "true" no matter what
1559  * the subexpression x is.      (XXX We assume that no such subexpression
1560  * will have important side-effects, which is not necessarily a good
1561  * assumption in the presence of user-defined functions; do we need a
1562  * pg_proc flag that prevents discarding the execution of a function?)
1563  *
1564  * We do understand that certain functions may deliver non-constant
1565  * results even with constant inputs, "nextval()" being the classic
1566  * example.  Functions that are not marked "immutable" in pg_proc
1567  * will not be pre-evaluated here, although we will reduce their
1568  * arguments as far as possible.
1569  *
1570  * We assume that the tree has already been type-checked and contains
1571  * only operators and functions that are reasonable to try to execute.
1572  *
1573  * NOTE: the planner assumes that this will always flatten nested AND and
1574  * OR clauses into N-argument form.  See comments in prepqual.c.
1575  *--------------------
1576  */
1577 Node *
1578 eval_const_expressions(Node *node)
1579 {
1580         eval_const_expressions_context context;
1581
1582         context.boundParams = NULL;     /* don't use any bound params */
1583         context.active_fns = NIL;       /* nothing being recursively simplified */
1584         context.case_val = NULL;        /* no CASE being examined */
1585         context.estimate = false;       /* safe transformations only */
1586         return eval_const_expressions_mutator(node, &context);
1587 }
1588
1589 /*--------------------
1590  * estimate_expression_value
1591  *
1592  * This function attempts to estimate the value of an expression for
1593  * planning purposes.  It is in essence a more aggressive version of
1594  * eval_const_expressions(): we will perform constant reductions that are
1595  * not necessarily 100% safe, but are reasonable for estimation purposes.
1596  *
1597  * Currently the extra steps that are taken in this mode are:
1598  * 1. Substitute values for Params, where a bound Param value has been made
1599  *        available by the caller of planner(), even if the Param isn't marked
1600  *        constant.  This effectively means that we plan using the first supplied
1601  *        value of the Param.
1602  * 2. Fold stable, as well as immutable, functions to constants.
1603  *--------------------
1604  */
1605 Node *
1606 estimate_expression_value(PlannerInfo *root, Node *node)
1607 {
1608         eval_const_expressions_context context;
1609
1610         context.boundParams = root->glob->boundParams;  /* bound Params */
1611         context.active_fns = NIL;       /* nothing being recursively simplified */
1612         context.case_val = NULL;        /* no CASE being examined */
1613         context.estimate = true;        /* unsafe transformations OK */
1614         return eval_const_expressions_mutator(node, &context);
1615 }
1616
1617 static Node *
1618 eval_const_expressions_mutator(Node *node,
1619                                                            eval_const_expressions_context *context)
1620 {
1621         if (node == NULL)
1622                 return NULL;
1623         if (IsA(node, Param))
1624         {
1625                 Param      *param = (Param *) node;
1626
1627                 /* Look to see if we've been given a value for this Param */
1628                 if (param->paramkind == PARAM_EXTERN &&
1629                         context->boundParams != NULL &&
1630                         param->paramid > 0 &&
1631                         param->paramid <= context->boundParams->numParams)
1632                 {
1633                         ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
1634
1635                         if (OidIsValid(prm->ptype))
1636                         {
1637                                 /* OK to substitute parameter value? */
1638                                 if (context->estimate || (prm->pflags & PARAM_FLAG_CONST))
1639                                 {
1640                                         /*
1641                                          * Return a Const representing the param value.  Must copy
1642                                          * pass-by-ref datatypes, since the Param might be in a
1643                                          * memory context shorter-lived than our output plan
1644                                          * should be.
1645                                          */
1646                                         int16           typLen;
1647                                         bool            typByVal;
1648                                         Datum           pval;
1649
1650                                         Assert(prm->ptype == param->paramtype);
1651                                         get_typlenbyval(param->paramtype, &typLen, &typByVal);
1652                                         if (prm->isnull || typByVal)
1653                                                 pval = prm->value;
1654                                         else
1655                                                 pval = datumCopy(prm->value, typByVal, typLen);
1656                                         return (Node *) makeConst(param->paramtype,
1657                                                                                           (int) typLen,
1658                                                                                           pval,
1659                                                                                           prm->isnull,
1660                                                                                           typByVal);
1661                                 }
1662                         }
1663                 }
1664                 /* Not replaceable, so just copy the Param (no need to recurse) */
1665                 return (Node *) copyObject(param);
1666         }
1667         if (IsA(node, FuncExpr))
1668         {
1669                 FuncExpr   *expr = (FuncExpr *) node;
1670                 List       *args;
1671                 Expr       *simple;
1672                 FuncExpr   *newexpr;
1673
1674                 /*
1675                  * Reduce constants in the FuncExpr's arguments.  We know args is
1676                  * either NIL or a List node, so we can call expression_tree_mutator
1677                  * directly rather than recursing to self.
1678                  */
1679                 args = (List *) expression_tree_mutator((Node *) expr->args,
1680                                                                                           eval_const_expressions_mutator,
1681                                                                                                 (void *) context);
1682
1683                 /*
1684                  * Code for op/func reduction is pretty bulky, so split it out as a
1685                  * separate function.
1686                  */
1687                 simple = simplify_function(expr->funcid, expr->funcresulttype, args,
1688                                                                    true, context);
1689                 if (simple)                             /* successfully simplified it */
1690                         return (Node *) simple;
1691
1692                 /*
1693                  * The expression cannot be simplified any further, so build and
1694                  * return a replacement FuncExpr node using the possibly-simplified
1695                  * arguments.
1696                  */
1697                 newexpr = makeNode(FuncExpr);
1698                 newexpr->funcid = expr->funcid;
1699                 newexpr->funcresulttype = expr->funcresulttype;
1700                 newexpr->funcretset = expr->funcretset;
1701                 newexpr->funcformat = expr->funcformat;
1702                 newexpr->args = args;
1703                 return (Node *) newexpr;
1704         }
1705         if (IsA(node, OpExpr))
1706         {
1707                 OpExpr     *expr = (OpExpr *) node;
1708                 List       *args;
1709                 Expr       *simple;
1710                 OpExpr     *newexpr;
1711
1712                 /*
1713                  * Reduce constants in the OpExpr's arguments.  We know args is either
1714                  * NIL or a List node, so we can call expression_tree_mutator directly
1715                  * rather than recursing to self.
1716                  */
1717                 args = (List *) expression_tree_mutator((Node *) expr->args,
1718                                                                                           eval_const_expressions_mutator,
1719                                                                                                 (void *) context);
1720
1721                 /*
1722                  * Need to get OID of underlying function.      Okay to scribble on input
1723                  * to this extent.
1724                  */
1725                 set_opfuncid(expr);
1726
1727                 /*
1728                  * Code for op/func reduction is pretty bulky, so split it out as a
1729                  * separate function.
1730                  */
1731                 simple = simplify_function(expr->opfuncid, expr->opresulttype, args,
1732                                                                    true, context);
1733                 if (simple)                             /* successfully simplified it */
1734                         return (Node *) simple;
1735
1736                 /*
1737                  * If the operator is boolean equality, we know how to simplify cases
1738                  * involving one constant and one non-constant argument.
1739                  */
1740                 if (expr->opno == BooleanEqualOperator)
1741                 {
1742                         simple = simplify_boolean_equality(args);
1743                         if (simple)                     /* successfully simplified it */
1744                                 return (Node *) simple;
1745                 }
1746
1747                 /*
1748                  * The expression cannot be simplified any further, so build and
1749                  * return a replacement OpExpr node using the possibly-simplified
1750                  * arguments.
1751                  */
1752                 newexpr = makeNode(OpExpr);
1753                 newexpr->opno = expr->opno;
1754                 newexpr->opfuncid = expr->opfuncid;
1755                 newexpr->opresulttype = expr->opresulttype;
1756                 newexpr->opretset = expr->opretset;
1757                 newexpr->args = args;
1758                 return (Node *) newexpr;
1759         }
1760         if (IsA(node, DistinctExpr))
1761         {
1762                 DistinctExpr *expr = (DistinctExpr *) node;
1763                 List       *args;
1764                 ListCell   *arg;
1765                 bool            has_null_input = false;
1766                 bool            all_null_input = true;
1767                 bool            has_nonconst_input = false;
1768                 Expr       *simple;
1769                 DistinctExpr *newexpr;
1770
1771                 /*
1772                  * Reduce constants in the DistinctExpr's arguments.  We know args is
1773                  * either NIL or a List node, so we can call expression_tree_mutator
1774                  * directly rather than recursing to self.
1775                  */
1776                 args = (List *) expression_tree_mutator((Node *) expr->args,
1777                                                                                           eval_const_expressions_mutator,
1778                                                                                                 (void *) context);
1779
1780                 /*
1781                  * We must do our own check for NULLs because DistinctExpr has
1782                  * different results for NULL input than the underlying operator does.
1783                  */
1784                 foreach(arg, args)
1785                 {
1786                         if (IsA(lfirst(arg), Const))
1787                         {
1788                                 has_null_input |= ((Const *) lfirst(arg))->constisnull;
1789                                 all_null_input &= ((Const *) lfirst(arg))->constisnull;
1790                         }
1791                         else
1792                                 has_nonconst_input = true;
1793                 }
1794
1795                 /* all constants? then can optimize this out */
1796                 if (!has_nonconst_input)
1797                 {
1798                         /* all nulls? then not distinct */
1799                         if (all_null_input)
1800                                 return makeBoolConst(false, false);
1801
1802                         /* one null? then distinct */
1803                         if (has_null_input)
1804                                 return makeBoolConst(true, false);
1805
1806                         /* otherwise try to evaluate the '=' operator */
1807                         /* (NOT okay to try to inline it, though!) */
1808
1809                         /*
1810                          * Need to get OID of underlying function.      Okay to scribble on
1811                          * input to this extent.
1812                          */
1813                         set_opfuncid((OpExpr *) expr);          /* rely on struct equivalence */
1814
1815                         /*
1816                          * Code for op/func reduction is pretty bulky, so split it out as
1817                          * a separate function.
1818                          */
1819                         simple = simplify_function(expr->opfuncid, expr->opresulttype,
1820                                                                            args, false, context);
1821                         if (simple)                     /* successfully simplified it */
1822                         {
1823                                 /*
1824                                  * Since the underlying operator is "=", must negate its
1825                                  * result
1826                                  */
1827                                 Const      *csimple = (Const *) simple;
1828
1829                                 Assert(IsA(csimple, Const));
1830                                 csimple->constvalue =
1831                                         BoolGetDatum(!DatumGetBool(csimple->constvalue));
1832                                 return (Node *) csimple;
1833                         }
1834                 }
1835
1836                 /*
1837                  * The expression cannot be simplified any further, so build and
1838                  * return a replacement DistinctExpr node using the
1839                  * possibly-simplified arguments.
1840                  */
1841                 newexpr = makeNode(DistinctExpr);
1842                 newexpr->opno = expr->opno;
1843                 newexpr->opfuncid = expr->opfuncid;
1844                 newexpr->opresulttype = expr->opresulttype;
1845                 newexpr->opretset = expr->opretset;
1846                 newexpr->args = args;
1847                 return (Node *) newexpr;
1848         }
1849         if (IsA(node, BoolExpr))
1850         {
1851                 BoolExpr   *expr = (BoolExpr *) node;
1852
1853                 switch (expr->boolop)
1854                 {
1855                         case OR_EXPR:
1856                                 {
1857                                         List       *newargs;
1858                                         bool            haveNull = false;
1859                                         bool            forceTrue = false;
1860
1861                                         newargs = simplify_or_arguments(expr->args, context,
1862                                                                                                         &haveNull, &forceTrue);
1863                                         if (forceTrue)
1864                                                 return makeBoolConst(true, false);
1865                                         if (haveNull)
1866                                                 newargs = lappend(newargs, makeBoolConst(false, true));
1867                                         /* If all the inputs are FALSE, result is FALSE */
1868                                         if (newargs == NIL)
1869                                                 return makeBoolConst(false, false);
1870                                         /* If only one nonconst-or-NULL input, it's the result */
1871                                         if (list_length(newargs) == 1)
1872                                                 return (Node *) linitial(newargs);
1873                                         /* Else we still need an OR node */
1874                                         return (Node *) make_orclause(newargs);
1875                                 }
1876                         case AND_EXPR:
1877                                 {
1878                                         List       *newargs;
1879                                         bool            haveNull = false;
1880                                         bool            forceFalse = false;
1881
1882                                         newargs = simplify_and_arguments(expr->args, context,
1883                                                                                                          &haveNull, &forceFalse);
1884                                         if (forceFalse)
1885                                                 return makeBoolConst(false, false);
1886                                         if (haveNull)
1887                                                 newargs = lappend(newargs, makeBoolConst(false, true));
1888                                         /* If all the inputs are TRUE, result is TRUE */
1889                                         if (newargs == NIL)
1890                                                 return makeBoolConst(true, false);
1891                                         /* If only one nonconst-or-NULL input, it's the result */
1892                                         if (list_length(newargs) == 1)
1893                                                 return (Node *) linitial(newargs);
1894                                         /* Else we still need an AND node */
1895                                         return (Node *) make_andclause(newargs);
1896                                 }
1897                         case NOT_EXPR:
1898                                 {
1899                                         Node       *arg;
1900
1901                                         Assert(list_length(expr->args) == 1);
1902                                         arg = eval_const_expressions_mutator(linitial(expr->args),
1903                                                                                                                  context);
1904                                         if (IsA(arg, Const))
1905                                         {
1906                                                 Const      *const_input = (Const *) arg;
1907
1908                                                 /* NOT NULL => NULL */
1909                                                 if (const_input->constisnull)
1910                                                         return makeBoolConst(false, true);
1911                                                 /* otherwise pretty easy */
1912                                                 return makeBoolConst(!DatumGetBool(const_input->constvalue),
1913                                                                                          false);
1914                                         }
1915                                         else if (not_clause(arg))
1916                                         {
1917                                                 /* Cancel NOT/NOT */
1918                                                 return (Node *) get_notclausearg((Expr *) arg);
1919                                         }
1920                                         /* Else we still need a NOT node */
1921                                         return (Node *) make_notclause((Expr *) arg);
1922                                 }
1923                         default:
1924                                 elog(ERROR, "unrecognized boolop: %d",
1925                                          (int) expr->boolop);
1926                                 break;
1927                 }
1928         }
1929         if (IsA(node, SubPlan))
1930         {
1931                 /*
1932                  * Return a SubPlan unchanged --- too late to do anything with it.
1933                  *
1934                  * XXX should we ereport() here instead?  Probably this routine should
1935                  * never be invoked after SubPlan creation.
1936                  */
1937                 return node;
1938         }
1939         if (IsA(node, RelabelType))
1940         {
1941                 /*
1942                  * If we can simplify the input to a constant, then we don't need the
1943                  * RelabelType node anymore: just change the type field of the Const
1944                  * node.  Otherwise, must copy the RelabelType node.
1945                  */
1946                 RelabelType *relabel = (RelabelType *) node;
1947                 Node       *arg;
1948
1949                 arg = eval_const_expressions_mutator((Node *) relabel->arg,
1950                                                                                          context);
1951
1952                 /*
1953                  * If we find stacked RelabelTypes (eg, from foo :: int :: oid) we can
1954                  * discard all but the top one.
1955                  */
1956                 while (arg && IsA(arg, RelabelType))
1957                         arg = (Node *) ((RelabelType *) arg)->arg;
1958
1959                 if (arg && IsA(arg, Const))
1960                 {
1961                         Const      *con = (Const *) arg;
1962
1963                         con->consttype = relabel->resulttype;
1964
1965                         /*
1966                          * relabel's resulttypmod is discarded, which is OK for now; if
1967                          * the type actually needs a runtime length coercion then there
1968                          * should be a function call to do it just above this node.
1969                          */
1970                         return (Node *) con;
1971                 }
1972                 else
1973                 {
1974                         RelabelType *newrelabel = makeNode(RelabelType);
1975
1976                         newrelabel->arg = (Expr *) arg;
1977                         newrelabel->resulttype = relabel->resulttype;
1978                         newrelabel->resulttypmod = relabel->resulttypmod;
1979                         return (Node *) newrelabel;
1980                 }
1981         }
1982         if (IsA(node, CaseExpr))
1983         {
1984                 /*----------
1985                  * CASE expressions can be simplified if there are constant
1986                  * condition clauses:
1987                  *              FALSE (or NULL): drop the alternative
1988                  *              TRUE: drop all remaining alternatives
1989                  * If the first non-FALSE alternative is a constant TRUE, we can
1990                  * simplify the entire CASE to that alternative's expression.
1991                  * If there are no non-FALSE alternatives, we simplify the entire
1992                  * CASE to the default result (ELSE result).
1993                  *
1994                  * If we have a simple-form CASE with constant test expression,
1995                  * we substitute the constant value for contained CaseTestExpr
1996                  * placeholder nodes, so that we have the opportunity to reduce
1997                  * constant test conditions.  For example this allows
1998                  *              CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
1999                  * to reduce to 1 rather than drawing a divide-by-0 error.
2000                  *----------
2001                  */
2002                 CaseExpr   *caseexpr = (CaseExpr *) node;
2003                 CaseExpr   *newcase;
2004                 Node       *save_case_val;
2005                 Node       *newarg;
2006                 List       *newargs;
2007                 bool            const_true_cond;
2008                 Node       *defresult = NULL;
2009                 ListCell   *arg;
2010
2011                 /* Simplify the test expression, if any */
2012                 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
2013                                                                                                 context);
2014
2015                 /* Set up for contained CaseTestExpr nodes */
2016                 save_case_val = context->case_val;
2017                 if (newarg && IsA(newarg, Const))
2018                         context->case_val = newarg;
2019                 else
2020                         context->case_val = NULL;
2021
2022                 /* Simplify the WHEN clauses */
2023                 newargs = NIL;
2024                 const_true_cond = false;
2025                 foreach(arg, caseexpr->args)
2026                 {
2027                         CaseWhen   *oldcasewhen = (CaseWhen *) lfirst(arg);
2028                         Node       *casecond;
2029                         Node       *caseresult;
2030
2031                         Assert(IsA(oldcasewhen, CaseWhen));
2032
2033                         /* Simplify this alternative's test condition */
2034                         casecond =
2035                                 eval_const_expressions_mutator((Node *) oldcasewhen->expr,
2036                                                                                            context);
2037
2038                         /*
2039                          * If the test condition is constant FALSE (or NULL), then drop
2040                          * this WHEN clause completely, without processing the result.
2041                          */
2042                         if (casecond && IsA(casecond, Const))
2043                         {
2044                                 Const      *const_input = (Const *) casecond;
2045
2046                                 if (const_input->constisnull ||
2047                                         !DatumGetBool(const_input->constvalue))
2048                                         continue;       /* drop alternative with FALSE condition */
2049                                 /* Else it's constant TRUE */
2050                                 const_true_cond = true;
2051                         }
2052
2053                         /* Simplify this alternative's result value */
2054                         caseresult =
2055                                 eval_const_expressions_mutator((Node *) oldcasewhen->result,
2056                                                                                            context);
2057
2058                         /* If non-constant test condition, emit a new WHEN node */
2059                         if (!const_true_cond)
2060                         {
2061                                 CaseWhen   *newcasewhen = makeNode(CaseWhen);
2062
2063                                 newcasewhen->expr = (Expr *) casecond;
2064                                 newcasewhen->result = (Expr *) caseresult;
2065                                 newargs = lappend(newargs, newcasewhen);
2066                                 continue;
2067                         }
2068
2069                         /*
2070                          * Found a TRUE condition, so none of the remaining alternatives
2071                          * can be reached.      We treat the result as the default result.
2072                          */
2073                         defresult = caseresult;
2074                         break;
2075                 }
2076
2077                 /* Simplify the default result, unless we replaced it above */
2078                 if (!const_true_cond)
2079                         defresult =
2080                                 eval_const_expressions_mutator((Node *) caseexpr->defresult,
2081                                                                                            context);
2082
2083                 context->case_val = save_case_val;
2084
2085                 /* If no non-FALSE alternatives, CASE reduces to the default result */
2086                 if (newargs == NIL)
2087                         return defresult;
2088                 /* Otherwise we need a new CASE node */
2089                 newcase = makeNode(CaseExpr);
2090                 newcase->casetype = caseexpr->casetype;
2091                 newcase->arg = (Expr *) newarg;
2092                 newcase->args = newargs;
2093                 newcase->defresult = (Expr *) defresult;
2094                 return (Node *) newcase;
2095         }
2096         if (IsA(node, CaseTestExpr))
2097         {
2098                 /*
2099                  * If we know a constant test value for the current CASE construct,
2100                  * substitute it for the placeholder.  Else just return the
2101                  * placeholder as-is.
2102                  */
2103                 if (context->case_val)
2104                         return copyObject(context->case_val);
2105                 else
2106                         return copyObject(node);
2107         }
2108         if (IsA(node, ArrayExpr))
2109         {
2110                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
2111                 ArrayExpr  *newarray;
2112                 bool            all_const = true;
2113                 List       *newelems;
2114                 ListCell   *element;
2115
2116                 newelems = NIL;
2117                 foreach(element, arrayexpr->elements)
2118                 {
2119                         Node       *e;
2120
2121                         e = eval_const_expressions_mutator((Node *) lfirst(element),
2122                                                                                            context);
2123                         if (!IsA(e, Const))
2124                                 all_const = false;
2125                         newelems = lappend(newelems, e);
2126                 }
2127
2128                 newarray = makeNode(ArrayExpr);
2129                 newarray->array_typeid = arrayexpr->array_typeid;
2130                 newarray->element_typeid = arrayexpr->element_typeid;
2131                 newarray->elements = newelems;
2132                 newarray->multidims = arrayexpr->multidims;
2133
2134                 if (all_const)
2135                         return (Node *) evaluate_expr((Expr *) newarray,
2136                                                                                   newarray->array_typeid);
2137
2138                 return (Node *) newarray;
2139         }
2140         if (IsA(node, CoalesceExpr))
2141         {
2142                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
2143                 CoalesceExpr *newcoalesce;
2144                 List       *newargs;
2145                 ListCell   *arg;
2146
2147                 newargs = NIL;
2148                 foreach(arg, coalesceexpr->args)
2149                 {
2150                         Node       *e;
2151
2152                         e = eval_const_expressions_mutator((Node *) lfirst(arg),
2153                                                                                            context);
2154
2155                         /*
2156                          * We can remove null constants from the list. For a non-null
2157                          * constant, if it has not been preceded by any other
2158                          * non-null-constant expressions then that is the result.
2159                          */
2160                         if (IsA(e, Const))
2161                         {
2162                                 if (((Const *) e)->constisnull)
2163                                         continue;       /* drop null constant */
2164                                 if (newargs == NIL)
2165                                         return e;       /* first expr */
2166                         }
2167                         newargs = lappend(newargs, e);
2168                 }
2169
2170                 /* If all the arguments were constant null, the result is just null */
2171                 if (newargs == NIL)
2172                         return (Node *) makeNullConst(coalesceexpr->coalescetype);
2173
2174                 newcoalesce = makeNode(CoalesceExpr);
2175                 newcoalesce->coalescetype = coalesceexpr->coalescetype;
2176                 newcoalesce->args = newargs;
2177                 return (Node *) newcoalesce;
2178         }
2179         if (IsA(node, FieldSelect))
2180         {
2181                 /*
2182                  * We can optimize field selection from a whole-row Var into a simple
2183                  * Var.  (This case won't be generated directly by the parser, because
2184                  * ParseComplexProjection short-circuits it. But it can arise while
2185                  * simplifying functions.)      Also, we can optimize field selection from
2186                  * a RowExpr construct.
2187                  *
2188                  * We must however check that the declared type of the field is still
2189                  * the same as when the FieldSelect was created --- this can change if
2190                  * someone did ALTER COLUMN TYPE on the rowtype.
2191                  */
2192                 FieldSelect *fselect = (FieldSelect *) node;
2193                 FieldSelect *newfselect;
2194                 Node       *arg;
2195
2196                 arg = eval_const_expressions_mutator((Node *) fselect->arg,
2197                                                                                          context);
2198                 if (arg && IsA(arg, Var) &&
2199                         ((Var *) arg)->varattno == InvalidAttrNumber)
2200                 {
2201                         if (rowtype_field_matches(((Var *) arg)->vartype,
2202                                                                           fselect->fieldnum,
2203                                                                           fselect->resulttype,
2204                                                                           fselect->resulttypmod))
2205                                 return (Node *) makeVar(((Var *) arg)->varno,
2206                                                                                 fselect->fieldnum,
2207                                                                                 fselect->resulttype,
2208                                                                                 fselect->resulttypmod,
2209                                                                                 ((Var *) arg)->varlevelsup);
2210                 }
2211                 if (arg && IsA(arg, RowExpr))
2212                 {
2213                         RowExpr    *rowexpr = (RowExpr *) arg;
2214
2215                         if (fselect->fieldnum > 0 &&
2216                                 fselect->fieldnum <= list_length(rowexpr->args))
2217                         {
2218                                 Node       *fld = (Node *) list_nth(rowexpr->args,
2219                                                                                                         fselect->fieldnum - 1);
2220
2221                                 if (rowtype_field_matches(rowexpr->row_typeid,
2222                                                                                   fselect->fieldnum,
2223                                                                                   fselect->resulttype,
2224                                                                                   fselect->resulttypmod) &&
2225                                         fselect->resulttype == exprType(fld) &&
2226                                         fselect->resulttypmod == exprTypmod(fld))
2227                                         return fld;
2228                         }
2229                 }
2230                 newfselect = makeNode(FieldSelect);
2231                 newfselect->arg = (Expr *) arg;
2232                 newfselect->fieldnum = fselect->fieldnum;
2233                 newfselect->resulttype = fselect->resulttype;
2234                 newfselect->resulttypmod = fselect->resulttypmod;
2235                 return (Node *) newfselect;
2236         }
2237         if (IsA(node, NullTest))
2238         {
2239                 NullTest   *ntest = (NullTest *) node;
2240                 NullTest   *newntest;
2241                 Node       *arg;
2242
2243                 arg = eval_const_expressions_mutator((Node *) ntest->arg,
2244                                                                                          context);
2245                 if (arg && IsA(arg, RowExpr))
2246                 {
2247                         RowExpr    *rarg = (RowExpr *) arg;
2248                         List       *newargs = NIL;
2249                         ListCell   *l;
2250
2251                         /*
2252                          * We break ROW(...) IS [NOT] NULL into separate tests on its
2253                          * component fields.  This form is usually more efficient to
2254                          * evaluate, as well as being more amenable to optimization.
2255                          */
2256                         foreach(l, rarg->args)
2257                         {
2258                                 Node       *relem = (Node *) lfirst(l);
2259
2260                                 /*
2261                                  * A constant field refutes the whole NullTest if it's of the
2262                                  * wrong nullness; else we can discard it.
2263                                  */
2264                                 if (relem && IsA(relem, Const))
2265                                 {
2266                                         Const      *carg = (Const *) relem;
2267
2268                                         if (carg->constisnull ?
2269                                                 (ntest->nulltesttype == IS_NOT_NULL) :
2270                                                 (ntest->nulltesttype == IS_NULL))
2271                                                 return makeBoolConst(false, false);
2272                                         continue;
2273                                 }
2274                                 newntest = makeNode(NullTest);
2275                                 newntest->arg = (Expr *) relem;
2276                                 newntest->nulltesttype = ntest->nulltesttype;
2277                                 newargs = lappend(newargs, newntest);
2278                         }
2279                         /* If all the inputs were constants, result is TRUE */
2280                         if (newargs == NIL)
2281                                 return makeBoolConst(true, false);
2282                         /* If only one nonconst input, it's the result */
2283                         if (list_length(newargs) == 1)
2284                                 return (Node *) linitial(newargs);
2285                         /* Else we need an AND node */
2286                         return (Node *) make_andclause(newargs);
2287                 }
2288                 if (arg && IsA(arg, Const))
2289                 {
2290                         Const      *carg = (Const *) arg;
2291                         bool            result;
2292
2293                         switch (ntest->nulltesttype)
2294                         {
2295                                 case IS_NULL:
2296                                         result = carg->constisnull;
2297                                         break;
2298                                 case IS_NOT_NULL:
2299                                         result = !carg->constisnull;
2300                                         break;
2301                                 default:
2302                                         elog(ERROR, "unrecognized nulltesttype: %d",
2303                                                  (int) ntest->nulltesttype);
2304                                         result = false;         /* keep compiler quiet */
2305                                         break;
2306                         }
2307
2308                         return makeBoolConst(result, false);
2309                 }
2310
2311                 newntest = makeNode(NullTest);
2312                 newntest->arg = (Expr *) arg;
2313                 newntest->nulltesttype = ntest->nulltesttype;
2314                 return (Node *) newntest;
2315         }
2316         if (IsA(node, BooleanTest))
2317         {
2318                 BooleanTest *btest = (BooleanTest *) node;
2319                 BooleanTest *newbtest;
2320                 Node       *arg;
2321
2322                 arg = eval_const_expressions_mutator((Node *) btest->arg,
2323                                                                                          context);
2324                 if (arg && IsA(arg, Const))
2325                 {
2326                         Const      *carg = (Const *) arg;
2327                         bool            result;
2328
2329                         switch (btest->booltesttype)
2330                         {
2331                                 case IS_TRUE:
2332                                         result = (!carg->constisnull &&
2333                                                           DatumGetBool(carg->constvalue));
2334                                         break;
2335                                 case IS_NOT_TRUE:
2336                                         result = (carg->constisnull ||
2337                                                           !DatumGetBool(carg->constvalue));
2338                                         break;
2339                                 case IS_FALSE:
2340                                         result = (!carg->constisnull &&
2341                                                           !DatumGetBool(carg->constvalue));
2342                                         break;
2343                                 case IS_NOT_FALSE:
2344                                         result = (carg->constisnull ||
2345                                                           DatumGetBool(carg->constvalue));
2346                                         break;
2347                                 case IS_UNKNOWN:
2348                                         result = carg->constisnull;
2349                                         break;
2350                                 case IS_NOT_UNKNOWN:
2351                                         result = !carg->constisnull;
2352                                         break;
2353                                 default:
2354                                         elog(ERROR, "unrecognized booltesttype: %d",
2355                                                  (int) btest->booltesttype);
2356                                         result = false;         /* keep compiler quiet */
2357                                         break;
2358                         }
2359
2360                         return makeBoolConst(result, false);
2361                 }
2362
2363                 newbtest = makeNode(BooleanTest);
2364                 newbtest->arg = (Expr *) arg;
2365                 newbtest->booltesttype = btest->booltesttype;
2366                 return (Node *) newbtest;
2367         }
2368
2369         /*
2370          * For any node type not handled above, we recurse using
2371          * expression_tree_mutator, which will copy the node unchanged but try to
2372          * simplify its arguments (if any) using this routine. For example: we
2373          * cannot eliminate an ArrayRef node, but we might be able to simplify
2374          * constant expressions in its subscripts.
2375          */
2376         return expression_tree_mutator(node, eval_const_expressions_mutator,
2377                                                                    (void *) context);
2378 }
2379
2380 /*
2381  * Subroutine for eval_const_expressions: process arguments of an OR clause
2382  *
2383  * This includes flattening of nested ORs as well as recursion to
2384  * eval_const_expressions to simplify the OR arguments.
2385  *
2386  * After simplification, OR arguments are handled as follows:
2387  *              non constant: keep
2388  *              FALSE: drop (does not affect result)
2389  *              TRUE: force result to TRUE
2390  *              NULL: keep only one
2391  * We must keep one NULL input because ExecEvalOr returns NULL when no input
2392  * is TRUE and at least one is NULL.  We don't actually include the NULL
2393  * here, that's supposed to be done by the caller.
2394  *
2395  * The output arguments *haveNull and *forceTrue must be initialized FALSE
2396  * by the caller.  They will be set TRUE if a null constant or true constant,
2397  * respectively, is detected anywhere in the argument list.
2398  */
2399 static List *
2400 simplify_or_arguments(List *args,
2401                                           eval_const_expressions_context *context,
2402                                           bool *haveNull, bool *forceTrue)
2403 {
2404         List       *newargs = NIL;
2405         List       *unprocessed_args;
2406
2407         /*
2408          * Since the parser considers OR to be a binary operator, long OR lists
2409          * become deeply nested expressions.  We must flatten these into long
2410          * argument lists of a single OR operator.      To avoid blowing out the stack
2411          * with recursion of eval_const_expressions, we resort to some tenseness
2412          * here: we keep a list of not-yet-processed inputs, and handle flattening
2413          * of nested ORs by prepending to the to-do list instead of recursing.
2414          */
2415         unprocessed_args = list_copy(args);
2416         while (unprocessed_args)
2417         {
2418                 Node       *arg = (Node *) linitial(unprocessed_args);
2419
2420                 unprocessed_args = list_delete_first(unprocessed_args);
2421
2422                 /* flatten nested ORs as per above comment */
2423                 if (or_clause(arg))
2424                 {
2425                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
2426
2427                         /* overly tense code to avoid leaking unused list header */
2428                         if (!unprocessed_args)
2429                                 unprocessed_args = subargs;
2430                         else
2431                         {
2432                                 List       *oldhdr = unprocessed_args;
2433
2434                                 unprocessed_args = list_concat(subargs, unprocessed_args);
2435                                 pfree(oldhdr);
2436                         }
2437                         continue;
2438                 }
2439
2440                 /* If it's not an OR, simplify it */
2441                 arg = eval_const_expressions_mutator(arg, context);
2442
2443                 /*
2444                  * It is unlikely but not impossible for simplification of a non-OR
2445                  * clause to produce an OR.  Recheck, but don't be too tense about it
2446                  * since it's not a mainstream case. In particular we don't worry
2447                  * about const-simplifying the input twice.
2448                  */
2449                 if (or_clause(arg))
2450                 {
2451                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
2452
2453                         unprocessed_args = list_concat(subargs, unprocessed_args);
2454                         continue;
2455                 }
2456
2457                 /*
2458                  * OK, we have a const-simplified non-OR argument.      Process it per
2459                  * comments above.
2460                  */
2461                 if (IsA(arg, Const))
2462                 {
2463                         Const      *const_input = (Const *) arg;
2464
2465                         if (const_input->constisnull)
2466                                 *haveNull = true;
2467                         else if (DatumGetBool(const_input->constvalue))
2468                         {
2469                                 *forceTrue = true;
2470
2471                                 /*
2472                                  * Once we detect a TRUE result we can just exit the loop
2473                                  * immediately.  However, if we ever add a notion of
2474                                  * non-removable functions, we'd need to keep scanning.
2475                                  */
2476                                 return NIL;
2477                         }
2478                         /* otherwise, we can drop the constant-false input */
2479                         continue;
2480                 }
2481
2482                 /* else emit the simplified arg into the result list */
2483                 newargs = lappend(newargs, arg);
2484         }
2485
2486         return newargs;
2487 }
2488
2489 /*
2490  * Subroutine for eval_const_expressions: process arguments of an AND clause
2491  *
2492  * This includes flattening of nested ANDs as well as recursion to
2493  * eval_const_expressions to simplify the AND arguments.
2494  *
2495  * After simplification, AND arguments are handled as follows:
2496  *              non constant: keep
2497  *              TRUE: drop (does not affect result)
2498  *              FALSE: force result to FALSE
2499  *              NULL: keep only one
2500  * We must keep one NULL input because ExecEvalAnd returns NULL when no input
2501  * is FALSE and at least one is NULL.  We don't actually include the NULL
2502  * here, that's supposed to be done by the caller.
2503  *
2504  * The output arguments *haveNull and *forceFalse must be initialized FALSE
2505  * by the caller.  They will be set TRUE if a null constant or false constant,
2506  * respectively, is detected anywhere in the argument list.
2507  */
2508 static List *
2509 simplify_and_arguments(List *args,
2510                                            eval_const_expressions_context *context,
2511                                            bool *haveNull, bool *forceFalse)
2512 {
2513         List       *newargs = NIL;
2514         List       *unprocessed_args;
2515
2516         /* See comments in simplify_or_arguments */
2517         unprocessed_args = list_copy(args);
2518         while (unprocessed_args)
2519         {
2520                 Node       *arg = (Node *) linitial(unprocessed_args);
2521
2522                 unprocessed_args = list_delete_first(unprocessed_args);
2523
2524                 /* flatten nested ANDs as per above comment */
2525                 if (and_clause(arg))
2526                 {
2527                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
2528
2529                         /* overly tense code to avoid leaking unused list header */
2530                         if (!unprocessed_args)
2531                                 unprocessed_args = subargs;
2532                         else
2533                         {
2534                                 List       *oldhdr = unprocessed_args;
2535
2536                                 unprocessed_args = list_concat(subargs, unprocessed_args);
2537                                 pfree(oldhdr);
2538                         }
2539                         continue;
2540                 }
2541
2542                 /* If it's not an AND, simplify it */
2543                 arg = eval_const_expressions_mutator(arg, context);
2544
2545                 /*
2546                  * It is unlikely but not impossible for simplification of a non-AND
2547                  * clause to produce an AND.  Recheck, but don't be too tense about it
2548                  * since it's not a mainstream case. In particular we don't worry
2549                  * about const-simplifying the input twice.
2550                  */
2551                 if (and_clause(arg))
2552                 {
2553                         List       *subargs = list_copy(((BoolExpr *) arg)->args);
2554
2555                         unprocessed_args = list_concat(subargs, unprocessed_args);
2556                         continue;
2557                 }
2558
2559                 /*
2560                  * OK, we have a const-simplified non-AND argument.  Process it per
2561                  * comments above.
2562                  */
2563                 if (IsA(arg, Const))
2564                 {
2565                         Const      *const_input = (Const *) arg;
2566
2567                         if (const_input->constisnull)
2568                                 *haveNull = true;
2569                         else if (!DatumGetBool(const_input->constvalue))
2570                         {
2571                                 *forceFalse = true;
2572
2573                                 /*
2574                                  * Once we detect a FALSE result we can just exit the loop
2575                                  * immediately.  However, if we ever add a notion of
2576                                  * non-removable functions, we'd need to keep scanning.
2577                                  */
2578                                 return NIL;
2579                         }
2580                         /* otherwise, we can drop the constant-true input */
2581                         continue;
2582                 }
2583
2584                 /* else emit the simplified arg into the result list */
2585                 newargs = lappend(newargs, arg);
2586         }
2587
2588         return newargs;
2589 }
2590
2591 /*
2592  * Subroutine for eval_const_expressions: try to simplify boolean equality
2593  *
2594  * Input is the list of simplified arguments to the operator.
2595  * Returns a simplified expression if successful, or NULL if cannot
2596  * simplify the expression.
2597  *
2598  * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x".
2599  * This is only marginally useful in itself, but doing it in constant folding
2600  * ensures that we will recognize the two forms as being equivalent in, for
2601  * example, partial index matching.
2602  *
2603  * We come here only if simplify_function has failed; therefore we cannot
2604  * see two constant inputs, nor a constant-NULL input.
2605  */
2606 static Expr *
2607 simplify_boolean_equality(List *args)
2608 {
2609         Expr       *leftop;
2610         Expr       *rightop;
2611
2612         Assert(list_length(args) == 2);
2613         leftop = linitial(args);
2614         rightop = lsecond(args);
2615         if (leftop && IsA(leftop, Const))
2616         {
2617                 Assert(!((Const *) leftop)->constisnull);
2618                 if (DatumGetBool(((Const *) leftop)->constvalue))
2619                         return rightop;         /* true = foo */
2620                 else
2621                         return make_notclause(rightop);         /* false = foo */
2622         }
2623         if (rightop && IsA(rightop, Const))
2624         {
2625                 Assert(!((Const *) rightop)->constisnull);
2626                 if (DatumGetBool(((Const *) rightop)->constvalue))
2627                         return leftop;          /* foo = true */
2628                 else
2629                         return make_notclause(leftop);          /* foo = false */
2630         }
2631         return NULL;
2632 }
2633
2634 /*
2635  * Subroutine for eval_const_expressions: try to simplify a function call
2636  * (which might originally have been an operator; we don't care)
2637  *
2638  * Inputs are the function OID, actual result type OID (which is needed for
2639  * polymorphic functions), and the pre-simplified argument list;
2640  * also the context data for eval_const_expressions.
2641  *
2642  * Returns a simplified expression if successful, or NULL if cannot
2643  * simplify the function call.
2644  */
2645 static Expr *
2646 simplify_function(Oid funcid, Oid result_type, List *args,
2647                                   bool allow_inline,
2648                                   eval_const_expressions_context *context)
2649 {
2650         HeapTuple       func_tuple;
2651         Expr       *newexpr;
2652
2653         /*
2654          * We have two strategies for simplification: either execute the function
2655          * to deliver a constant result, or expand in-line the body of the
2656          * function definition (which only works for simple SQL-language
2657          * functions, but that is a common case).  In either case we need access
2658          * to the function's pg_proc tuple, so fetch it just once to use in both
2659          * attempts.
2660          */
2661         func_tuple = SearchSysCache(PROCOID,
2662                                                                 ObjectIdGetDatum(funcid),
2663                                                                 0, 0, 0);
2664         if (!HeapTupleIsValid(func_tuple))
2665                 elog(ERROR, "cache lookup failed for function %u", funcid);
2666
2667         newexpr = evaluate_function(funcid, result_type, args,
2668                                                                 func_tuple, context);
2669
2670         if (!newexpr && allow_inline)
2671                 newexpr = inline_function(funcid, result_type, args,
2672                                                                   func_tuple, context);
2673
2674         ReleaseSysCache(func_tuple);
2675
2676         return newexpr;
2677 }
2678
2679 /*
2680  * evaluate_function: try to pre-evaluate a function call
2681  *
2682  * We can do this if the function is strict and has any constant-null inputs
2683  * (just return a null constant), or if the function is immutable and has all
2684  * constant inputs (call it and return the result as a Const node).  In
2685  * estimation mode we are willing to pre-evaluate stable functions too.
2686  *
2687  * Returns a simplified expression if successful, or NULL if cannot
2688  * simplify the function.
2689  */
2690 static Expr *
2691 evaluate_function(Oid funcid, Oid result_type, List *args,
2692                                   HeapTuple func_tuple,
2693                                   eval_const_expressions_context *context)
2694 {
2695         Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
2696         bool            has_nonconst_input = false;
2697         bool            has_null_input = false;
2698         ListCell   *arg;
2699         FuncExpr   *newexpr;
2700
2701         /*
2702          * Can't simplify if it returns a set.
2703          */
2704         if (funcform->proretset)
2705                 return NULL;
2706
2707         /*
2708          * Can't simplify if it returns RECORD.  The immediate problem is that it
2709          * will be needing an expected tupdesc which we can't supply here.
2710          *
2711          * In the case where it has OUT parameters, it could get by without an
2712          * expected tupdesc, but we still have issues: get_expr_result_type()
2713          * doesn't know how to extract type info from a RECORD constant, and in
2714          * the case of a NULL function result there doesn't seem to be any clean
2715          * way to fix that.  In view of the likelihood of there being still other
2716          * gotchas, seems best to leave the function call unreduced.
2717          */
2718         if (funcform->prorettype == RECORDOID)
2719                 return NULL;
2720
2721         /*
2722          * Check for constant inputs and especially constant-NULL inputs.
2723          */
2724         foreach(arg, args)
2725         {
2726                 if (IsA(lfirst(arg), Const))
2727                         has_null_input |= ((Const *) lfirst(arg))->constisnull;
2728                 else
2729                         has_nonconst_input = true;
2730         }
2731
2732         /*
2733          * If the function is strict and has a constant-NULL input, it will never
2734          * be called at all, so we can replace the call by a NULL constant, even
2735          * if there are other inputs that aren't constant, and even if the
2736          * function is not otherwise immutable.
2737          */
2738         if (funcform->proisstrict && has_null_input)
2739                 return (Expr *) makeNullConst(result_type);
2740
2741         /*
2742          * Otherwise, can simplify only if all inputs are constants. (For a
2743          * non-strict function, constant NULL inputs are treated the same as
2744          * constant non-NULL inputs.)
2745          */
2746         if (has_nonconst_input)
2747                 return NULL;
2748
2749         /*
2750          * Ordinarily we are only allowed to simplify immutable functions. But for
2751          * purposes of estimation, we consider it okay to simplify functions that
2752          * are merely stable; the risk that the result might change from planning
2753          * time to execution time is worth taking in preference to not being able
2754          * to estimate the value at all.
2755          */
2756         if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
2757                  /* okay */ ;
2758         else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
2759                  /* okay */ ;
2760         else
2761                 return NULL;
2762
2763         /*
2764          * OK, looks like we can simplify this operator/function.
2765          *
2766          * Build a new FuncExpr node containing the already-simplified arguments.
2767          */
2768         newexpr = makeNode(FuncExpr);
2769         newexpr->funcid = funcid;
2770         newexpr->funcresulttype = result_type;
2771         newexpr->funcretset = false;
2772         newexpr->funcformat = COERCE_DONTCARE;          /* doesn't matter */
2773         newexpr->args = args;
2774
2775         return evaluate_expr((Expr *) newexpr, result_type);
2776 }
2777
2778 /*
2779  * inline_function: try to expand a function call inline
2780  *
2781  * If the function is a sufficiently simple SQL-language function
2782  * (just "SELECT expression"), then we can inline it and avoid the rather
2783  * high per-call overhead of SQL functions.  Furthermore, this can expose
2784  * opportunities for constant-folding within the function expression.
2785  *
2786  * We have to beware of some special cases however.  A directly or
2787  * indirectly recursive function would cause us to recurse forever,
2788  * so we keep track of which functions we are already expanding and
2789  * do not re-expand them.  Also, if a parameter is used more than once
2790  * in the SQL-function body, we require it not to contain any volatile
2791  * functions (volatiles might deliver inconsistent answers) nor to be
2792  * unreasonably expensive to evaluate.  The expensiveness check not only
2793  * prevents us from doing multiple evaluations of an expensive parameter
2794  * at runtime, but is a safety value to limit growth of an expression due
2795  * to repeated inlining.
2796  *
2797  * We must also beware of changing the volatility or strictness status of
2798  * functions by inlining them.
2799  *
2800  * Returns a simplified expression if successful, or NULL if cannot
2801  * simplify the function.
2802  */
2803 static Expr *
2804 inline_function(Oid funcid, Oid result_type, List *args,
2805                                 HeapTuple func_tuple,
2806                                 eval_const_expressions_context *context)
2807 {
2808         Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
2809         Oid                *argtypes;
2810         char       *src;
2811         Datum           tmp;
2812         bool            isNull;
2813         MemoryContext oldcxt;
2814         MemoryContext mycxt;
2815         ErrorContextCallback sqlerrcontext;
2816         List       *raw_parsetree_list;
2817         List       *querytree_list;
2818         Query      *querytree;
2819         Node       *newexpr;
2820         int                *usecounts;
2821         ListCell   *arg;
2822         int                     i;
2823
2824         /*
2825          * Forget it if the function is not SQL-language or has other showstopper
2826          * properties.  (The nargs check is just paranoia.)
2827          */
2828         if (funcform->prolang != SQLlanguageId ||
2829                 funcform->prosecdef ||
2830                 funcform->proretset ||
2831                 funcform->pronargs != list_length(args))
2832                 return NULL;
2833
2834         /* Check for recursive function, and give up trying to expand if so */
2835         if (list_member_oid(context->active_fns, funcid))
2836                 return NULL;
2837
2838         /* Check permission to call function (fail later, if not) */
2839         if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
2840                 return NULL;
2841
2842         /*
2843          * Setup error traceback support for ereport().  This is so that we can
2844          * finger the function that bad information came from.
2845          */
2846         sqlerrcontext.callback = sql_inline_error_callback;
2847         sqlerrcontext.arg = func_tuple;
2848         sqlerrcontext.previous = error_context_stack;
2849         error_context_stack = &sqlerrcontext;
2850
2851         /*
2852          * Make a temporary memory context, so that we don't leak all the stuff
2853          * that parsing might create.
2854          */
2855         mycxt = AllocSetContextCreate(CurrentMemoryContext,
2856                                                                   "inline_function",
2857                                                                   ALLOCSET_DEFAULT_MINSIZE,
2858                                                                   ALLOCSET_DEFAULT_INITSIZE,
2859                                                                   ALLOCSET_DEFAULT_MAXSIZE);
2860         oldcxt = MemoryContextSwitchTo(mycxt);
2861
2862         /* Check for polymorphic arguments, and substitute actual arg types */
2863         argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
2864         memcpy(argtypes, funcform->proargtypes.values,
2865                    funcform->pronargs * sizeof(Oid));
2866         for (i = 0; i < funcform->pronargs; i++)
2867         {
2868                 if (argtypes[i] == ANYARRAYOID ||
2869                         argtypes[i] == ANYELEMENTOID)
2870                 {
2871                         argtypes[i] = exprType((Node *) list_nth(args, i));
2872                 }
2873         }
2874
2875         /* Fetch and parse the function body */
2876         tmp = SysCacheGetAttr(PROCOID,
2877                                                   func_tuple,
2878                                                   Anum_pg_proc_prosrc,
2879                                                   &isNull);
2880         if (isNull)
2881                 elog(ERROR, "null prosrc for function %u", funcid);
2882         src = DatumGetCString(DirectFunctionCall1(textout, tmp));
2883
2884         /*
2885          * We just do parsing and parse analysis, not rewriting, because rewriting
2886          * will not affect table-free-SELECT-only queries, which is all that we
2887          * care about.  Also, we can punt as soon as we detect more than one
2888          * command in the function body.
2889          */
2890         raw_parsetree_list = pg_parse_query(src);
2891         if (list_length(raw_parsetree_list) != 1)
2892                 goto fail;
2893
2894         querytree_list = parse_analyze(linitial(raw_parsetree_list), src,
2895                                                                    argtypes, funcform->pronargs);
2896
2897         if (list_length(querytree_list) != 1)
2898                 goto fail;
2899
2900         querytree = (Query *) linitial(querytree_list);
2901
2902         /*
2903          * The single command must be a simple "SELECT expression".
2904          */
2905         if (!IsA(querytree, Query) ||
2906                 querytree->commandType != CMD_SELECT ||
2907                 querytree->into ||
2908                 querytree->hasAggs ||
2909                 querytree->hasSubLinks ||
2910                 querytree->rtable ||
2911                 querytree->jointree->fromlist ||
2912                 querytree->jointree->quals ||
2913                 querytree->groupClause ||
2914                 querytree->havingQual ||
2915                 querytree->distinctClause ||
2916                 querytree->sortClause ||
2917                 querytree->limitOffset ||
2918                 querytree->limitCount ||
2919                 querytree->setOperations ||
2920                 list_length(querytree->targetList) != 1)
2921                 goto fail;
2922
2923         newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
2924
2925         /*
2926          * Make sure the function (still) returns what it's declared to.  This will
2927          * raise an error if wrong, but that's okay since the function would fail
2928          * at runtime anyway.  Note we do not try this until we have verified that
2929          * no rewriting was needed; that's probably not important, but let's be
2930          * careful.
2931          */
2932         (void) check_sql_fn_retval(funcid, result_type, querytree_list, NULL);
2933
2934         /*
2935          * Additional validity checks on the expression.  It mustn't return a set,
2936          * and it mustn't be more volatile than the surrounding function (this is
2937          * to avoid breaking hacks that involve pretending a function is immutable
2938          * when it really ain't).  If the surrounding function is declared strict,
2939          * then the expression must contain only strict constructs and must use
2940          * all of the function parameters (this is overkill, but an exact analysis
2941          * is hard).
2942          */
2943         if (expression_returns_set(newexpr))
2944                 goto fail;
2945
2946         if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
2947                 contain_mutable_functions(newexpr))
2948                 goto fail;
2949         else if (funcform->provolatile == PROVOLATILE_STABLE &&
2950                          contain_volatile_functions(newexpr))
2951                 goto fail;
2952
2953         if (funcform->proisstrict &&
2954                 contain_nonstrict_functions(newexpr))
2955                 goto fail;
2956
2957         /*
2958          * We may be able to do it; there are still checks on parameter usage to
2959          * make, but those are most easily done in combination with the actual
2960          * substitution of the inputs.  So start building expression with inputs
2961          * substituted.
2962          */
2963         usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
2964         newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
2965                                                                                    args, usecounts);
2966
2967         /* Now check for parameter usage */
2968         i = 0;
2969         foreach(arg, args)
2970         {
2971                 Node       *param = lfirst(arg);
2972
2973                 if (usecounts[i] == 0)
2974                 {
2975                         /* Param not used at all: uncool if func is strict */
2976                         if (funcform->proisstrict)
2977                                 goto fail;
2978                 }
2979                 else if (usecounts[i] != 1)
2980                 {
2981                         /* Param used multiple times: uncool if expensive or volatile */
2982                         QualCost        eval_cost;
2983
2984                         /*
2985                          * We define "expensive" as "contains any subplan or more than 10
2986                          * operators".  Note that the subplan search has to be done
2987                          * explicitly, since cost_qual_eval() will barf on unplanned
2988                          * subselects.
2989                          */
2990                         if (contain_subplans(param))
2991                                 goto fail;
2992                         cost_qual_eval(&eval_cost, list_make1(param));
2993                         if (eval_cost.startup + eval_cost.per_tuple >
2994                                 10 * cpu_operator_cost)
2995                                 goto fail;
2996
2997                         /*
2998                          * Check volatility last since this is more expensive than the
2999                          * above tests
3000                          */
3001                         if (contain_volatile_functions(param))
3002                                 goto fail;
3003                 }
3004                 i++;
3005         }
3006
3007         /*
3008          * Whew --- we can make the substitution.  Copy the modified expression
3009          * out of the temporary memory context, and clean up.
3010          */
3011         MemoryContextSwitchTo(oldcxt);
3012
3013         newexpr = copyObject(newexpr);
3014
3015         MemoryContextDelete(mycxt);
3016
3017         /*
3018          * Recursively try to simplify the modified expression.  Here we must add
3019          * the current function to the context list of active functions.
3020          */
3021         context->active_fns = lcons_oid(funcid, context->active_fns);
3022         newexpr = eval_const_expressions_mutator(newexpr, context);
3023         context->active_fns = list_delete_first(context->active_fns);
3024
3025         error_context_stack = sqlerrcontext.previous;
3026
3027         return (Expr *) newexpr;
3028
3029         /* Here if func is not inlinable: release temp memory and return NULL */
3030 fail:
3031         MemoryContextSwitchTo(oldcxt);
3032         MemoryContextDelete(mycxt);
3033         error_context_stack = sqlerrcontext.previous;
3034
3035         return NULL;
3036 }
3037
3038 /*
3039  * Replace Param nodes by appropriate actual parameters
3040  */
3041 static Node *
3042 substitute_actual_parameters(Node *expr, int nargs, List *args,
3043                                                          int *usecounts)
3044 {
3045         substitute_actual_parameters_context context;
3046
3047         context.nargs = nargs;
3048         context.args = args;
3049         context.usecounts = usecounts;
3050
3051         return substitute_actual_parameters_mutator(expr, &context);
3052 }
3053
3054 static Node *
3055 substitute_actual_parameters_mutator(Node *node,
3056                                                            substitute_actual_parameters_context *context)
3057 {
3058         if (node == NULL)
3059                 return NULL;
3060         if (IsA(node, Param))
3061         {
3062                 Param      *param = (Param *) node;
3063
3064                 if (param->paramkind != PARAM_EXTERN)
3065                         elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
3066                 if (param->paramid <= 0 || param->paramid > context->nargs)
3067                         elog(ERROR, "invalid paramid: %d", param->paramid);
3068
3069                 /* Count usage of parameter */
3070                 context->usecounts[param->paramid - 1]++;
3071
3072                 /* Select the appropriate actual arg and replace the Param with it */
3073                 /* We don't need to copy at this time (it'll get done later) */
3074                 return list_nth(context->args, param->paramid - 1);
3075         }
3076         return expression_tree_mutator(node, substitute_actual_parameters_mutator,
3077                                                                    (void *) context);
3078 }
3079
3080 /*
3081  * error context callback to let us supply a call-stack traceback
3082  */
3083 static void
3084 sql_inline_error_callback(void *arg)
3085 {
3086         HeapTuple       func_tuple = (HeapTuple) arg;
3087         Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3088         int                     syntaxerrposition;
3089
3090         /* If it's a syntax error, convert to internal syntax error report */
3091         syntaxerrposition = geterrposition();
3092         if (syntaxerrposition > 0)
3093         {
3094                 bool            isnull;
3095                 Datum           tmp;
3096                 char       *prosrc;
3097
3098                 tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
3099                                                           &isnull);
3100                 if (isnull)
3101                         elog(ERROR, "null prosrc");
3102                 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
3103                 errposition(0);
3104                 internalerrposition(syntaxerrposition);
3105                 internalerrquery(prosrc);
3106         }
3107
3108         errcontext("SQL function \"%s\" during inlining",
3109                            NameStr(funcform->proname));
3110 }
3111
3112 /*
3113  * evaluate_expr: pre-evaluate a constant expression
3114  *
3115  * We use the executor's routine ExecEvalExpr() to avoid duplication of
3116  * code and ensure we get the same result as the executor would get.
3117  */
3118 static Expr *
3119 evaluate_expr(Expr *expr, Oid result_type)
3120 {
3121         EState     *estate;
3122         ExprState  *exprstate;
3123         MemoryContext oldcontext;
3124         Datum           const_val;
3125         bool            const_is_null;
3126         int16           resultTypLen;
3127         bool            resultTypByVal;
3128
3129         /*
3130          * To use the executor, we need an EState.
3131          */
3132         estate = CreateExecutorState();
3133
3134         /* We can use the estate's working context to avoid memory leaks. */
3135         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3136
3137         /*
3138          * Prepare expr for execution.
3139          */
3140         exprstate = ExecPrepareExpr(expr, estate);
3141
3142         /*
3143          * And evaluate it.
3144          *
3145          * It is OK to use a default econtext because none of the ExecEvalExpr()
3146          * code used in this situation will use econtext.  That might seem
3147          * fortuitous, but it's not so unreasonable --- a constant expression does
3148          * not depend on context, by definition, n'est ce pas?
3149          */
3150         const_val = ExecEvalExprSwitchContext(exprstate,
3151                                                                                   GetPerTupleExprContext(estate),
3152                                                                                   &const_is_null, NULL);
3153
3154         /* Get info needed about result datatype */
3155         get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
3156
3157         /* Get back to outer memory context */
3158         MemoryContextSwitchTo(oldcontext);
3159
3160         /* Must copy result out of sub-context used by expression eval */
3161         if (!const_is_null)
3162                 const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
3163
3164         /* Release all the junk we just created */
3165         FreeExecutorState(estate);
3166
3167         /*
3168          * Make the constant result node.
3169          */
3170         return (Expr *) makeConst(result_type, resultTypLen,
3171                                                           const_val, const_is_null,
3172                                                           resultTypByVal);
3173 }
3174
3175
3176 /*
3177  * Standard expression-tree walking support
3178  *
3179  * We used to have near-duplicate code in many different routines that
3180  * understood how to recurse through an expression node tree.  That was
3181  * a pain to maintain, and we frequently had bugs due to some particular
3182  * routine neglecting to support a particular node type.  In most cases,
3183  * these routines only actually care about certain node types, and don't
3184  * care about other types except insofar as they have to recurse through
3185  * non-primitive node types.  Therefore, we now provide generic tree-walking
3186  * logic to consolidate the redundant "boilerplate" code.  There are
3187  * two versions: expression_tree_walker() and expression_tree_mutator().
3188  */
3189
3190 /*--------------------
3191  * expression_tree_walker() is designed to support routines that traverse
3192  * a tree in a read-only fashion (although it will also work for routines
3193  * that modify nodes in-place but never add/delete/replace nodes).
3194  * A walker routine should look like this:
3195  *
3196  * bool my_walker (Node *node, my_struct *context)
3197  * {
3198  *              if (node == NULL)
3199  *                      return false;
3200  *              // check for nodes that special work is required for, eg:
3201  *              if (IsA(node, Var))
3202  *              {
3203  *                      ... do special actions for Var nodes
3204  *              }
3205  *              else if (IsA(node, ...))
3206  *              {
3207  *                      ... do special actions for other node types
3208  *              }
3209  *              // for any node type not specially processed, do:
3210  *              return expression_tree_walker(node, my_walker, (void *) context);
3211  * }
3212  *
3213  * The "context" argument points to a struct that holds whatever context
3214  * information the walker routine needs --- it can be used to return data
3215  * gathered by the walker, too.  This argument is not touched by
3216  * expression_tree_walker, but it is passed down to recursive sub-invocations
3217  * of my_walker.  The tree walk is started from a setup routine that
3218  * fills in the appropriate context struct, calls my_walker with the top-level
3219  * node of the tree, and then examines the results.
3220  *
3221  * The walker routine should return "false" to continue the tree walk, or
3222  * "true" to abort the walk and immediately return "true" to the top-level
3223  * caller.      This can be used to short-circuit the traversal if the walker
3224  * has found what it came for.  "false" is returned to the top-level caller
3225  * iff no invocation of the walker returned "true".
3226  *
3227  * The node types handled by expression_tree_walker include all those
3228  * normally found in target lists and qualifier clauses during the planning
3229  * stage.  In particular, it handles List nodes since a cnf-ified qual clause
3230  * will have List structure at the top level, and it handles TargetEntry nodes
3231  * so that a scan of a target list can be handled without additional code.
3232  * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
3233  * handled, so that query jointrees and setOperation trees can be processed
3234  * without additional code.
3235  *
3236  * expression_tree_walker will handle SubLink nodes by recursing normally
3237  * into the "testexpr" subtree (which is an expression belonging to the outer
3238  * plan).  It will also call the walker on the sub-Query node; however, when
3239  * expression_tree_walker itself is called on a Query node, it does nothing
3240  * and returns "false".  The net effect is that unless the walker does
3241  * something special at a Query node, sub-selects will not be visited during
3242  * an expression tree walk. This is exactly the behavior wanted in many cases
3243  * --- and for those walkers that do want to recurse into sub-selects, special
3244  * behavior is typically needed anyway at the entry to a sub-select (such as
3245  * incrementing a depth counter). A walker that wants to examine sub-selects
3246  * should include code along the lines of:
3247  *
3248  *              if (IsA(node, Query))
3249  *              {
3250  *                      adjust context for subquery;
3251  *                      result = query_tree_walker((Query *) node, my_walker, context,
3252  *                                                                         0); // adjust flags as needed
3253  *                      restore context if needed;
3254  *                      return result;
3255  *              }
3256  *
3257  * query_tree_walker is a convenience routine (see below) that calls the
3258  * walker on all the expression subtrees of the given Query node.
3259  *
3260  * expression_tree_walker will handle SubPlan nodes by recursing normally
3261  * into the "testexpr" and the "args" list (which are expressions belonging to
3262  * the outer plan).  It will not touch the completed subplan, however.  Since
3263  * there is no link to the original Query, it is not possible to recurse into
3264  * subselects of an already-planned expression tree.  This is OK for current
3265  * uses, but may need to be revisited in future.
3266  *--------------------
3267  */
3268
3269 bool
3270 expression_tree_walker(Node *node,
3271                                            bool (*walker) (),
3272                                            void *context)
3273 {
3274         ListCell   *temp;
3275
3276         /*
3277          * The walker has already visited the current node, and so we need only
3278          * recurse into any sub-nodes it has.
3279          *
3280          * We assume that the walker is not interested in List nodes per se, so
3281          * when we expect a List we just recurse directly to self without
3282          * bothering to call the walker.
3283          */
3284         if (node == NULL)
3285                 return false;
3286
3287         /* Guard against stack overflow due to overly complex expressions */
3288         check_stack_depth();
3289
3290         switch (nodeTag(node))
3291         {
3292                 case T_Var:
3293                 case T_Const:
3294                 case T_Param:
3295                 case T_CoerceToDomainValue:
3296                 case T_CaseTestExpr:
3297                 case T_SetToDefault:
3298                 case T_RangeTblRef:
3299                 case T_OuterJoinInfo:
3300                         /* primitive node types with no expression subnodes */
3301                         break;
3302                 case T_Aggref:
3303                         {
3304                                 Aggref     *expr = (Aggref *) node;
3305
3306                                 /* recurse directly on List */
3307                                 if (expression_tree_walker((Node *) expr->args,
3308                                                                                    walker, context))
3309                                         return true;
3310                         }
3311                         break;
3312                 case T_ArrayRef:
3313                         {
3314                                 ArrayRef   *aref = (ArrayRef *) node;
3315
3316                                 /* recurse directly for upper/lower array index lists */
3317                                 if (expression_tree_walker((Node *) aref->refupperindexpr,
3318                                                                                    walker, context))
3319                                         return true;
3320                                 if (expression_tree_walker((Node *) aref->reflowerindexpr,
3321                                                                                    walker, context))
3322                                         return true;
3323                                 /* walker must see the refexpr and refassgnexpr, however */
3324                                 if (walker(aref->refexpr, context))
3325                                         return true;
3326                                 if (walker(aref->refassgnexpr, context))
3327                                         return true;
3328                         }
3329                         break;
3330                 case T_FuncExpr:
3331                         {
3332                                 FuncExpr   *expr = (FuncExpr *) node;
3333
3334                                 if (expression_tree_walker((Node *) expr->args,
3335                                                                                    walker, context))
3336                                         return true;
3337                         }
3338                         break;
3339                 case T_OpExpr:
3340                         {
3341                                 OpExpr     *expr = (OpExpr *) node;
3342
3343                                 if (expression_tree_walker((Node *) expr->args,
3344                                                                                    walker, context))
3345                                         return true;
3346                         }
3347                         break;
3348                 case T_DistinctExpr:
3349                         {
3350                                 DistinctExpr *expr = (DistinctExpr *) node;
3351
3352                                 if (expression_tree_walker((Node *) expr->args,
3353                                                                                    walker, context))
3354                                         return true;
3355                         }
3356                         break;
3357                 case T_ScalarArrayOpExpr:
3358                         {
3359                                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3360
3361                                 if (expression_tree_walker((Node *) expr->args,
3362                                                                                    walker, context))
3363                                         return true;
3364                         }
3365                         break;
3366                 case T_BoolExpr:
3367                         {
3368                                 BoolExpr   *expr = (BoolExpr *) node;
3369
3370                                 if (expression_tree_walker((Node *) expr->args,
3371                                                                                    walker, context))
3372                                         return true;
3373                         }
3374                         break;
3375                 case T_SubLink:
3376                         {
3377                                 SubLink    *sublink = (SubLink *) node;
3378
3379                                 if (walker(sublink->testexpr, context))
3380                                         return true;
3381
3382                                 /*
3383                                  * Also invoke the walker on the sublink's Query node, so it
3384                                  * can recurse into the sub-query if it wants to.
3385                                  */
3386                                 return walker(sublink->subselect, context);
3387                         }
3388                         break;
3389                 case T_SubPlan:
3390                         {
3391                                 SubPlan    *subplan = (SubPlan *) node;
3392
3393                                 /* recurse into the testexpr, but not into the Plan */
3394                                 if (walker(subplan->testexpr, context))
3395                                         return true;
3396                                 /* also examine args list */
3397                                 if (expression_tree_walker((Node *) subplan->args,
3398                                                                                    walker, context))
3399                                         return true;
3400                         }
3401                         break;
3402                 case T_FieldSelect:
3403                         return walker(((FieldSelect *) node)->arg, context);
3404                 case T_FieldStore:
3405                         {
3406                                 FieldStore *fstore = (FieldStore *) node;
3407
3408                                 if (walker(fstore->arg, context))
3409                                         return true;
3410                                 if (walker(fstore->newvals, context))
3411                                         return true;
3412                         }
3413                         break;
3414                 case T_RelabelType:
3415                         return walker(((RelabelType *) node)->arg, context);
3416                 case T_ConvertRowtypeExpr:
3417                         return walker(((ConvertRowtypeExpr *) node)->arg, context);
3418                 case T_CaseExpr:
3419                         {
3420                                 CaseExpr   *caseexpr = (CaseExpr *) node;
3421
3422                                 if (walker(caseexpr->arg, context))
3423                                         return true;
3424                                 /* we assume walker doesn't care about CaseWhens, either */
3425                                 foreach(temp, caseexpr->args)
3426                                 {
3427                                         CaseWhen   *when = (CaseWhen *) lfirst(temp);
3428
3429                                         Assert(IsA(when, CaseWhen));
3430                                         if (walker(when->expr, context))
3431                                                 return true;
3432                                         if (walker(when->result, context))
3433                                                 return true;
3434                                 }
3435                                 if (walker(caseexpr->defresult, context))
3436                                         return true;
3437                         }
3438                         break;
3439                 case T_ArrayExpr:
3440                         return walker(((ArrayExpr *) node)->elements, context);
3441                 case T_RowExpr:
3442                         return walker(((RowExpr *) node)->args, context);
3443                 case T_RowCompareExpr:
3444                         {
3445                                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3446
3447                                 if (walker(rcexpr->largs, context))
3448                                         return true;
3449                                 if (walker(rcexpr->rargs, context))
3450                                         return true;
3451                         }
3452                         break;
3453                 case T_CoalesceExpr:
3454                         return walker(((CoalesceExpr *) node)->args, context);
3455                 case T_MinMaxExpr:
3456                         return walker(((MinMaxExpr *) node)->args, context);
3457                 case T_XmlExpr:
3458                         {
3459                                 XmlExpr *xexpr = (XmlExpr *) node;
3460                                 
3461                                 if (walker(xexpr->named_args, context))
3462                                         return true;
3463                                 /* we assume walker doesn't care about arg_names */
3464                                 if (walker(xexpr->args, context))
3465                                         return true;
3466                         }
3467                         break;
3468                 case T_NullIfExpr:
3469                         return walker(((NullIfExpr *) node)->args, context);
3470                 case T_NullTest:
3471                         return walker(((NullTest *) node)->arg, context);
3472                 case T_BooleanTest:
3473                         return walker(((BooleanTest *) node)->arg, context);
3474                 case T_CoerceToDomain:
3475                         return walker(((CoerceToDomain *) node)->arg, context);
3476                 case T_TargetEntry:
3477                         return walker(((TargetEntry *) node)->expr, context);
3478                 case T_Query:
3479                         /* Do nothing with a sub-Query, per discussion above */
3480                         break;
3481                 case T_List:
3482                         foreach(temp, (List *) node)
3483                         {
3484                                 if (walker((Node *) lfirst(temp), context))
3485                                         return true;
3486                         }
3487                         break;
3488                 case T_FromExpr:
3489                         {
3490                                 FromExpr   *from = (FromExpr *) node;
3491
3492                                 if (walker(from->fromlist, context))
3493                                         return true;
3494                                 if (walker(from->quals, context))
3495                                         return true;
3496                         }
3497                         break;
3498                 case T_JoinExpr:
3499                         {
3500                                 JoinExpr   *join = (JoinExpr *) node;
3501
3502                                 if (walker(join->larg, context))
3503                                         return true;
3504                                 if (walker(join->rarg, context))
3505                                         return true;
3506                                 if (walker(join->quals, context))
3507                                         return true;
3508
3509                                 /*
3510                                  * alias clause, using list are deemed uninteresting.
3511                                  */
3512                         }
3513                         break;
3514                 case T_SetOperationStmt:
3515                         {
3516                                 SetOperationStmt *setop = (SetOperationStmt *) node;
3517
3518                                 if (walker(setop->larg, context))
3519                                         return true;
3520                                 if (walker(setop->rarg, context))
3521                                         return true;
3522                         }
3523                         break;
3524                 case T_InClauseInfo:
3525                         {
3526                                 InClauseInfo *ininfo = (InClauseInfo *) node;
3527
3528                                 if (expression_tree_walker((Node *) ininfo->sub_targetlist,
3529                                                                                    walker, context))
3530                                         return true;
3531                         }
3532                         break;
3533                 case T_AppendRelInfo:
3534                         {
3535                                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
3536
3537                                 if (expression_tree_walker((Node *) appinfo->translated_vars,
3538                                                                                    walker, context))
3539                                         return true;
3540                         }
3541                         break;
3542                 default:
3543                         elog(ERROR, "unrecognized node type: %d",
3544                                  (int) nodeTag(node));
3545                         break;
3546         }
3547         return false;
3548 }
3549
3550 /*
3551  * query_tree_walker --- initiate a walk of a Query's expressions
3552  *
3553  * This routine exists just to reduce the number of places that need to know
3554  * where all the expression subtrees of a Query are.  Note it can be used
3555  * for starting a walk at top level of a Query regardless of whether the
3556  * walker intends to descend into subqueries.  It is also useful for
3557  * descending into subqueries within a walker.
3558  *
3559  * Some callers want to suppress visitation of certain items in the sub-Query,
3560  * typically because they need to process them specially, or don't actually
3561  * want to recurse into subqueries.  This is supported by the flags argument,
3562  * which is the bitwise OR of flag values to suppress visitation of
3563  * indicated items.  (More flag bits may be added as needed.)
3564  */
3565 bool
3566 query_tree_walker(Query *query,
3567                                   bool (*walker) (),
3568                                   void *context,
3569                                   int flags)
3570 {
3571         Assert(query != NULL && IsA(query, Query));
3572
3573         if (walker((Node *) query->targetList, context))
3574                 return true;
3575         if (walker((Node *) query->returningList, context))
3576                 return true;
3577         if (walker((Node *) query->jointree, context))
3578                 return true;
3579         if (walker(query->setOperations, context))
3580                 return true;
3581         if (walker(query->havingQual, context))
3582                 return true;
3583         if (walker(query->limitOffset, context))
3584                 return true;
3585         if (walker(query->limitCount, context))
3586                 return true;
3587         if (range_table_walker(query->rtable, walker, context, flags))
3588                 return true;
3589         if (query->utilityStmt)
3590         {
3591                 /*
3592                  * Certain utility commands contain general-purpose Querys embedded in
3593                  * them --- if this is one, invoke the walker on the sub-Query.
3594                  */
3595                 if (IsA(query->utilityStmt, CopyStmt))
3596                 {
3597                         if (walker(((CopyStmt *) query->utilityStmt)->query, context))
3598                                 return true;
3599                 }
3600                 if (IsA(query->utilityStmt, DeclareCursorStmt))
3601                 {
3602                         if (walker(((DeclareCursorStmt *) query->utilityStmt)->query, context))
3603                                 return true;
3604                 }
3605                 if (IsA(query->utilityStmt, ExplainStmt))
3606                 {
3607                         if (walker(((ExplainStmt *) query->utilityStmt)->query, context))
3608                                 return true;
3609                 }
3610                 if (IsA(query->utilityStmt, PrepareStmt))
3611                 {
3612                         if (walker(((PrepareStmt *) query->utilityStmt)->query, context))
3613                                 return true;
3614                 }
3615                 if (IsA(query->utilityStmt, ViewStmt))
3616                 {
3617                         if (walker(((ViewStmt *) query->utilityStmt)->query, context))
3618                                 return true;
3619                 }
3620         }
3621         return false;
3622 }
3623
3624 /*
3625  * range_table_walker is just the part of query_tree_walker that scans
3626  * a query's rangetable.  This is split out since it can be useful on
3627  * its own.
3628  */
3629 bool
3630 range_table_walker(List *rtable,
3631                                    bool (*walker) (),
3632                                    void *context,
3633                                    int flags)
3634 {
3635         ListCell   *rt;
3636
3637         foreach(rt, rtable)
3638         {
3639                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3640
3641                 switch (rte->rtekind)
3642                 {
3643                         case RTE_RELATION:
3644                         case RTE_SPECIAL:
3645                                 /* nothing to do */
3646                                 break;
3647                         case RTE_SUBQUERY:
3648                                 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
3649                                         if (walker(rte->subquery, context))
3650                                                 return true;
3651                                 break;
3652                         case RTE_JOIN:
3653                                 if (!(flags & QTW_IGNORE_JOINALIASES))
3654                                         if (walker(rte->joinaliasvars, context))
3655                                                 return true;
3656                                 break;
3657                         case RTE_FUNCTION:
3658                                 if (walker(rte->funcexpr, context))
3659                                         return true;
3660                                 break;
3661                         case RTE_VALUES:
3662                                 if (walker(rte->values_lists, context))
3663                                         return true;
3664                                 break;
3665                 }
3666         }
3667         return false;
3668 }
3669
3670
3671 /*--------------------
3672  * expression_tree_mutator() is designed to support routines that make a
3673  * modified copy of an expression tree, with some nodes being added,
3674  * removed, or replaced by new subtrees.  The original tree is (normally)
3675  * not changed.  Each recursion level is responsible for returning a copy of
3676  * (or appropriately modified substitute for) the subtree it is handed.
3677  * A mutator routine should look like this:
3678  *
3679  * Node * my_mutator (Node *node, my_struct *context)
3680  * {
3681  *              if (node == NULL)
3682  *                      return NULL;
3683  *              // check for nodes that special work is required for, eg:
3684  *              if (IsA(node, Var))
3685  *              {
3686  *                      ... create and return modified copy of Var node
3687  *              }
3688  *              else if (IsA(node, ...))
3689  *              {
3690  *                      ... do special transformations of other node types
3691  *              }
3692  *              // for any node type not specially processed, do:
3693  *              return expression_tree_mutator(node, my_mutator, (void *) context);
3694  * }
3695  *
3696  * The "context" argument points to a struct that holds whatever context
3697  * information the mutator routine needs --- it can be used to return extra
3698  * data gathered by the mutator, too.  This argument is not touched by
3699  * expression_tree_mutator, but it is passed down to recursive sub-invocations
3700  * of my_mutator.  The tree walk is started from a setup routine that
3701  * fills in the appropriate context struct, calls my_mutator with the
3702  * top-level node of the tree, and does any required post-processing.
3703  *
3704  * Each level of recursion must return an appropriately modified Node.
3705  * If expression_tree_mutator() is called, it will make an exact copy
3706  * of the given Node, but invoke my_mutator() to copy the sub-node(s)
3707  * of that Node.  In this way, my_mutator() has full control over the
3708  * copying process but need not directly deal with expression trees
3709  * that it has no interest in.
3710  *
3711  * Just as for expression_tree_walker, the node types handled by
3712  * expression_tree_mutator include all those normally found in target lists
3713  * and qualifier clauses during the planning stage.
3714  *
3715  * expression_tree_mutator will handle SubLink nodes by recursing normally
3716  * into the "testexpr" subtree (which is an expression belonging to the outer
3717  * plan).  It will also call the mutator on the sub-Query node; however, when
3718  * expression_tree_mutator itself is called on a Query node, it does nothing
3719  * and returns the unmodified Query node.  The net effect is that unless the
3720  * mutator does something special at a Query node, sub-selects will not be
3721  * visited or modified; the original sub-select will be linked to by the new
3722  * SubLink node.  Mutators that want to descend into sub-selects will usually
3723  * do so by recognizing Query nodes and calling query_tree_mutator (below).
3724  *
3725  * expression_tree_mutator will handle a SubPlan node by recursing into the
3726  * "testexpr" and the "args" list (which belong to the outer plan), but it
3727  * will simply copy the link to the inner plan, since that's typically what
3728  * expression tree mutators want.  A mutator that wants to modify the subplan
3729  * can force appropriate behavior by recognizing SubPlan expression nodes
3730  * and doing the right thing.
3731  *--------------------
3732  */
3733
3734 Node *
3735 expression_tree_mutator(Node *node,
3736                                                 Node *(*mutator) (),
3737                                                 void *context)
3738 {
3739         /*
3740          * The mutator has already decided not to modify the current node, but we
3741          * must call the mutator for any sub-nodes.
3742          */
3743
3744 #define FLATCOPY(newnode, node, nodetype)  \
3745         ( (newnode) = makeNode(nodetype), \
3746           memcpy((newnode), (node), sizeof(nodetype)) )
3747
3748 #define CHECKFLATCOPY(newnode, node, nodetype)  \
3749         ( AssertMacro(IsA((node), nodetype)), \
3750           (newnode) = makeNode(nodetype), \
3751           memcpy((newnode), (node), sizeof(nodetype)) )
3752
3753 #define MUTATE(newfield, oldfield, fieldtype)  \
3754                 ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
3755
3756         if (node == NULL)
3757                 return NULL;
3758
3759         /* Guard against stack overflow due to overly complex expressions */
3760         check_stack_depth();
3761
3762         switch (nodeTag(node))
3763         {
3764                 case T_Var:
3765                 case T_Const:
3766                 case T_Param:
3767                 case T_CoerceToDomainValue:
3768                 case T_CaseTestExpr:
3769                 case T_SetToDefault:
3770                 case T_RangeTblRef:
3771                 case T_OuterJoinInfo:
3772                         /* primitive node types with no expression subnodes */
3773                         return (Node *) copyObject(node);
3774                 case T_Aggref:
3775                         {
3776                                 Aggref     *aggref = (Aggref *) node;
3777                                 Aggref     *newnode;
3778
3779                                 FLATCOPY(newnode, aggref, Aggref);
3780                                 MUTATE(newnode->args, aggref->args, List *);
3781                                 return (Node *) newnode;
3782                         }
3783                         break;
3784                 case T_ArrayRef:
3785                         {
3786                                 ArrayRef   *arrayref = (ArrayRef *) node;
3787                                 ArrayRef   *newnode;
3788
3789                                 FLATCOPY(newnode, arrayref, ArrayRef);
3790                                 MUTATE(newnode->refupperindexpr, arrayref->refupperindexpr,
3791                                            List *);
3792                                 MUTATE(newnode->reflowerindexpr, arrayref->reflowerindexpr,
3793                                            List *);
3794                                 MUTATE(newnode->refexpr, arrayref->refexpr,
3795                                            Expr *);
3796                                 MUTATE(newnode->refassgnexpr, arrayref->refassgnexpr,
3797                                            Expr *);
3798                                 return (Node *) newnode;
3799                         }
3800                         break;
3801                 case T_FuncExpr:
3802                         {
3803                                 FuncExpr   *expr = (FuncExpr *) node;
3804                                 FuncExpr   *newnode;
3805
3806                                 FLATCOPY(newnode, expr, FuncExpr);
3807                                 MUTATE(newnode->args, expr->args, List *);
3808                                 return (Node *) newnode;
3809                         }
3810                         break;
3811                 case T_OpExpr:
3812                         {
3813                                 OpExpr     *expr = (OpExpr *) node;
3814                                 OpExpr     *newnode;
3815
3816                                 FLATCOPY(newnode, expr, OpExpr);
3817                                 MUTATE(newnode->args, expr->args, List *);
3818                                 return (Node *) newnode;
3819                         }
3820                         break;
3821                 case T_DistinctExpr:
3822                         {
3823                                 DistinctExpr *expr = (DistinctExpr *) node;
3824                                 DistinctExpr *newnode;
3825
3826                                 FLATCOPY(newnode, expr, DistinctExpr);
3827                                 MUTATE(newnode->args, expr->args, List *);
3828                                 return (Node *) newnode;
3829                         }
3830                         break;
3831                 case T_ScalarArrayOpExpr:
3832                         {
3833                                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
3834                                 ScalarArrayOpExpr *newnode;
3835
3836                                 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
3837                                 MUTATE(newnode->args, expr->args, List *);
3838                                 return (Node *) newnode;
3839                         }
3840                         break;
3841                 case T_BoolExpr:
3842                         {
3843                                 BoolExpr   *expr = (BoolExpr *) node;
3844                                 BoolExpr   *newnode;
3845
3846                                 FLATCOPY(newnode, expr, BoolExpr);
3847                                 MUTATE(newnode->args, expr->args, List *);
3848                                 return (Node *) newnode;
3849                         }
3850                         break;
3851                 case T_SubLink:
3852                         {
3853                                 SubLink    *sublink = (SubLink *) node;
3854                                 SubLink    *newnode;
3855
3856                                 FLATCOPY(newnode, sublink, SubLink);
3857                                 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
3858
3859                                 /*
3860                                  * Also invoke the mutator on the sublink's Query node, so it
3861                                  * can recurse into the sub-query if it wants to.
3862                                  */
3863                                 MUTATE(newnode->subselect, sublink->subselect, Node *);
3864                                 return (Node *) newnode;
3865                         }
3866                         break;
3867                 case T_SubPlan:
3868                         {
3869                                 SubPlan    *subplan = (SubPlan *) node;
3870                                 SubPlan    *newnode;
3871
3872                                 FLATCOPY(newnode, subplan, SubPlan);
3873                                 /* transform testexpr */
3874                                 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
3875                                 /* transform args list (params to be passed to subplan) */
3876                                 MUTATE(newnode->args, subplan->args, List *);
3877                                 /* but not the sub-Plan itself, which is referenced as-is */
3878                                 return (Node *) newnode;
3879                         }
3880                         break;
3881                 case T_FieldSelect:
3882                         {
3883                                 FieldSelect *fselect = (FieldSelect *) node;
3884                                 FieldSelect *newnode;
3885
3886                                 FLATCOPY(newnode, fselect, FieldSelect);
3887                                 MUTATE(newnode->arg, fselect->arg, Expr *);
3888                                 return (Node *) newnode;
3889                         }
3890                         break;
3891                 case T_FieldStore:
3892                         {
3893                                 FieldStore *fstore = (FieldStore *) node;
3894                                 FieldStore *newnode;
3895
3896                                 FLATCOPY(newnode, fstore, FieldStore);
3897                                 MUTATE(newnode->arg, fstore->arg, Expr *);
3898                                 MUTATE(newnode->newvals, fstore->newvals, List *);
3899                                 newnode->fieldnums = list_copy(fstore->fieldnums);
3900                                 return (Node *) newnode;
3901                         }
3902                         break;
3903                 case T_RelabelType:
3904                         {
3905                                 RelabelType *relabel = (RelabelType *) node;
3906                                 RelabelType *newnode;
3907
3908                                 FLATCOPY(newnode, relabel, RelabelType);
3909                                 MUTATE(newnode->arg, relabel->arg, Expr *);
3910                                 return (Node *) newnode;
3911                         }
3912                         break;
3913                 case T_ConvertRowtypeExpr:
3914                         {
3915                                 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
3916                                 ConvertRowtypeExpr *newnode;
3917
3918                                 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
3919                                 MUTATE(newnode->arg, convexpr->arg, Expr *);
3920                                 return (Node *) newnode;
3921                         }
3922                         break;
3923                 case T_CaseExpr:
3924                         {
3925                                 CaseExpr   *caseexpr = (CaseExpr *) node;
3926                                 CaseExpr   *newnode;
3927
3928                                 FLATCOPY(newnode, caseexpr, CaseExpr);
3929                                 MUTATE(newnode->arg, caseexpr->arg, Expr *);
3930                                 MUTATE(newnode->args, caseexpr->args, List *);
3931                                 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
3932                                 return (Node *) newnode;
3933                         }
3934                         break;
3935                 case T_CaseWhen:
3936                         {
3937                                 CaseWhen   *casewhen = (CaseWhen *) node;
3938                                 CaseWhen   *newnode;
3939
3940                                 FLATCOPY(newnode, casewhen, CaseWhen);
3941                                 MUTATE(newnode->expr, casewhen->expr, Expr *);
3942                                 MUTATE(newnode->result, casewhen->result, Expr *);
3943                                 return (Node *) newnode;
3944                         }
3945                         break;
3946                 case T_ArrayExpr:
3947                         {
3948                                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
3949                                 ArrayExpr  *newnode;
3950
3951                                 FLATCOPY(newnode, arrayexpr, ArrayExpr);
3952                                 MUTATE(newnode->elements, arrayexpr->elements, List *);
3953                                 return (Node *) newnode;
3954                         }
3955                         break;
3956                 case T_RowExpr:
3957                         {
3958                                 RowExpr    *rowexpr = (RowExpr *) node;
3959                                 RowExpr    *newnode;
3960
3961                                 FLATCOPY(newnode, rowexpr, RowExpr);
3962                                 MUTATE(newnode->args, rowexpr->args, List *);
3963                                 return (Node *) newnode;
3964                         }
3965                         break;
3966                 case T_RowCompareExpr:
3967                         {
3968                                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3969                                 RowCompareExpr *newnode;
3970
3971                                 FLATCOPY(newnode, rcexpr, RowCompareExpr);
3972                                 MUTATE(newnode->largs, rcexpr->largs, List *);
3973                                 MUTATE(newnode->rargs, rcexpr->rargs, List *);
3974                                 return (Node *) newnode;
3975                         }
3976                         break;
3977                 case T_CoalesceExpr:
3978                         {
3979                                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3980                                 CoalesceExpr *newnode;
3981
3982                                 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
3983                                 MUTATE(newnode->args, coalesceexpr->args, List *);
3984                                 return (Node *) newnode;
3985                         }
3986                         break;
3987                 case T_MinMaxExpr:
3988                         {
3989                                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3990                                 MinMaxExpr *newnode;
3991
3992                                 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
3993                                 MUTATE(newnode->args, minmaxexpr->args, List *);
3994                                 return (Node *) newnode;
3995                         }
3996                         break;
3997                 case T_XmlExpr:
3998                         {
3999                                 XmlExpr *xexpr = (XmlExpr *) node;
4000                                 XmlExpr *newnode;
4001
4002                                 FLATCOPY(newnode, xexpr, XmlExpr);
4003                                 MUTATE(newnode->named_args, xexpr->named_args, List *);
4004                                 /* assume mutator does not care about arg_names */
4005                                 MUTATE(newnode->args, xexpr->args, List *);
4006                                 return (Node *) newnode;
4007                         }
4008                         break;
4009                 case T_NullIfExpr:
4010                         {
4011                                 NullIfExpr *expr = (NullIfExpr *) node;
4012                                 NullIfExpr *newnode;
4013
4014                                 FLATCOPY(newnode, expr, NullIfExpr);
4015                                 MUTATE(newnode->args, expr->args, List *);
4016                                 return (Node *) newnode;
4017                         }
4018                         break;
4019                 case T_NullTest:
4020                         {
4021                                 NullTest   *ntest = (NullTest *) node;
4022                                 NullTest   *newnode;
4023
4024                                 FLATCOPY(newnode, ntest, NullTest);
4025                                 MUTATE(newnode->arg, ntest->arg, Expr *);
4026                                 return (Node *) newnode;
4027                         }
4028                         break;
4029                 case T_BooleanTest:
4030                         {
4031                                 BooleanTest *btest = (BooleanTest *) node;
4032                                 BooleanTest *newnode;
4033
4034                                 FLATCOPY(newnode, btest, BooleanTest);
4035                                 MUTATE(newnode->arg, btest->arg, Expr *);
4036                                 return (Node *) newnode;
4037                         }
4038                         break;
4039                 case T_CoerceToDomain:
4040                         {
4041                                 CoerceToDomain *ctest = (CoerceToDomain *) node;
4042                                 CoerceToDomain *newnode;
4043
4044                                 FLATCOPY(newnode, ctest, CoerceToDomain);
4045                                 MUTATE(newnode->arg, ctest->arg, Expr *);
4046                                 return (Node *) newnode;
4047                         }
4048                         break;
4049                 case T_TargetEntry:
4050                         {
4051                                 TargetEntry *targetentry = (TargetEntry *) node;
4052                                 TargetEntry *newnode;
4053
4054                                 FLATCOPY(newnode, targetentry, TargetEntry);
4055                                 MUTATE(newnode->expr, targetentry->expr, Expr *);
4056                                 return (Node *) newnode;
4057                         }
4058                         break;
4059                 case T_Query:
4060                         /* Do nothing with a sub-Query, per discussion above */
4061                         return node;
4062                 case T_List:
4063                         {
4064                                 /*
4065                                  * We assume the mutator isn't interested in the list nodes
4066                                  * per se, so just invoke it on each list element. NOTE: this
4067                                  * would fail badly on a list with integer elements!
4068                                  */
4069                                 List       *resultlist;
4070                                 ListCell   *temp;
4071
4072                                 resultlist = NIL;
4073                                 foreach(temp, (List *) node)
4074                                 {
4075                                         resultlist = lappend(resultlist,
4076                                                                                  mutator((Node *) lfirst(temp),
4077                                                                                                  context));
4078                                 }
4079                                 return (Node *) resultlist;
4080                         }
4081                         break;
4082                 case T_FromExpr:
4083                         {
4084                                 FromExpr   *from = (FromExpr *) node;
4085                                 FromExpr   *newnode;
4086
4087                                 FLATCOPY(newnode, from, FromExpr);
4088                                 MUTATE(newnode->fromlist, from->fromlist, List *);
4089                                 MUTATE(newnode->quals, from->quals, Node *);
4090                                 return (Node *) newnode;
4091                         }
4092                         break;
4093                 case T_JoinExpr:
4094                         {
4095                                 JoinExpr   *join = (JoinExpr *) node;
4096                                 JoinExpr   *newnode;
4097
4098                                 FLATCOPY(newnode, join, JoinExpr);
4099                                 MUTATE(newnode->larg, join->larg, Node *);
4100                                 MUTATE(newnode->rarg, join->rarg, Node *);
4101                                 MUTATE(newnode->quals, join->quals, Node *);
4102                                 /* We do not mutate alias or using by default */
4103                                 return (Node *) newnode;
4104                         }
4105                         break;
4106                 case T_SetOperationStmt:
4107                         {
4108                                 SetOperationStmt *setop = (SetOperationStmt *) node;
4109                                 SetOperationStmt *newnode;
4110
4111                                 FLATCOPY(newnode, setop, SetOperationStmt);
4112                                 MUTATE(newnode->larg, setop->larg, Node *);
4113                                 MUTATE(newnode->rarg, setop->rarg, Node *);
4114                                 return (Node *) newnode;
4115                         }
4116                         break;
4117                 case T_InClauseInfo:
4118                         {
4119                                 InClauseInfo *ininfo = (InClauseInfo *) node;
4120                                 InClauseInfo *newnode;
4121
4122                                 FLATCOPY(newnode, ininfo, InClauseInfo);
4123                                 MUTATE(newnode->sub_targetlist, ininfo->sub_targetlist, List *);
4124                                 /* Assume we need not make a copy of in_operators list */
4125                                 return (Node *) newnode;
4126                         }
4127                         break;
4128                 case T_AppendRelInfo:
4129                         {
4130                                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
4131                                 AppendRelInfo *newnode;
4132
4133                                 FLATCOPY(newnode, appinfo, AppendRelInfo);
4134                                 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
4135                                 return (Node *) newnode;
4136                         }
4137                         break;
4138                 default:
4139                         elog(ERROR, "unrecognized node type: %d",
4140                                  (int) nodeTag(node));
4141                         break;
4142         }
4143         /* can't get here, but keep compiler happy */
4144         return NULL;
4145 }
4146
4147
4148 /*
4149  * query_tree_mutator --- initiate modification of a Query's expressions
4150  *
4151  * This routine exists just to reduce the number of places that need to know
4152  * where all the expression subtrees of a Query are.  Note it can be used
4153  * for starting a walk at top level of a Query regardless of whether the
4154  * mutator intends to descend into subqueries.  It is also useful for
4155  * descending into subqueries within a mutator.
4156  *
4157  * Some callers want to suppress mutating of certain items in the Query,
4158  * typically because they need to process them specially, or don't actually
4159  * want to recurse into subqueries.  This is supported by the flags argument,
4160  * which is the bitwise OR of flag values to suppress mutating of
4161  * indicated items.  (More flag bits may be added as needed.)
4162  *
4163  * Normally the Query node itself is copied, but some callers want it to be
4164  * modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.      All
4165  * modified substructure is safely copied in any case.
4166  */
4167 Query *
4168 query_tree_mutator(Query *query,
4169                                    Node *(*mutator) (),
4170                                    void *context,
4171                                    int flags)
4172 {
4173         Assert(query != NULL && IsA(query, Query));
4174
4175         if (!(flags & QTW_DONT_COPY_QUERY))
4176         {
4177                 Query      *newquery;
4178
4179                 FLATCOPY(newquery, query, Query);
4180                 query = newquery;
4181         }
4182
4183         MUTATE(query->targetList, query->targetList, List *);
4184         MUTATE(query->returningList, query->returningList, List *);
4185         MUTATE(query->jointree, query->jointree, FromExpr *);
4186         MUTATE(query->setOperations, query->setOperations, Node *);
4187         MUTATE(query->havingQual, query->havingQual, Node *);
4188         MUTATE(query->limitOffset, query->limitOffset, Node *);
4189         MUTATE(query->limitCount, query->limitCount, Node *);
4190         query->rtable = range_table_mutator(query->rtable,
4191                                                                                 mutator, context, flags);
4192         return query;
4193 }
4194
4195 /*
4196  * range_table_mutator is just the part of query_tree_mutator that processes
4197  * a query's rangetable.  This is split out since it can be useful on
4198  * its own.
4199  */
4200 List *
4201 range_table_mutator(List *rtable,
4202                                         Node *(*mutator) (),
4203                                         void *context,
4204                                         int flags)
4205 {
4206         List       *newrt = NIL;
4207         ListCell   *rt;
4208
4209         foreach(rt, rtable)
4210         {
4211                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
4212                 RangeTblEntry *newrte;
4213
4214                 FLATCOPY(newrte, rte, RangeTblEntry);
4215                 switch (rte->rtekind)
4216                 {
4217                         case RTE_RELATION:
4218                         case RTE_SPECIAL:
4219                                 /* we don't bother to copy eref, aliases, etc; OK? */
4220                                 break;
4221                         case RTE_SUBQUERY:
4222                                 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
4223                                 {
4224                                         CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
4225                                         MUTATE(newrte->subquery, newrte->subquery, Query *);
4226                                 }
4227                                 else
4228                                 {
4229                                         /* else, copy RT subqueries as-is */
4230                                         newrte->subquery = copyObject(rte->subquery);
4231                                 }
4232                                 break;
4233                         case RTE_JOIN:
4234                                 if (!(flags & QTW_IGNORE_JOINALIASES))
4235                                         MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
4236                                 else
4237                                 {
4238                                         /* else, copy join aliases as-is */
4239                                         newrte->joinaliasvars = copyObject(rte->joinaliasvars);
4240                                 }
4241                                 break;
4242                         case RTE_FUNCTION:
4243                                 MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
4244                                 break;
4245                         case RTE_VALUES:
4246                                 MUTATE(newrte->values_lists, rte->values_lists, List *);
4247                                 break;
4248                 }
4249                 newrt = lappend(newrt, newrte);
4250         }
4251         return newrt;
4252 }
4253
4254 /*
4255  * query_or_expression_tree_walker --- hybrid form
4256  *
4257  * This routine will invoke query_tree_walker if called on a Query node,
4258  * else will invoke the walker directly.  This is a useful way of starting
4259  * the recursion when the walker's normal change of state is not appropriate
4260  * for the outermost Query node.
4261  */
4262 bool
4263 query_or_expression_tree_walker(Node *node,
4264                                                                 bool (*walker) (),
4265                                                                 void *context,
4266                                                                 int flags)
4267 {
4268         if (node && IsA(node, Query))
4269                 return query_tree_walker((Query *) node,
4270                                                                  walker,
4271                                                                  context,
4272                                                                  flags);
4273         else
4274                 return walker(node, context);
4275 }
4276
4277 /*
4278  * query_or_expression_tree_mutator --- hybrid form
4279  *
4280  * This routine will invoke query_tree_mutator if called on a Query node,
4281  * else will invoke the mutator directly.  This is a useful way of starting
4282  * the recursion when the mutator's normal change of state is not appropriate
4283  * for the outermost Query node.
4284  */
4285 Node *
4286 query_or_expression_tree_mutator(Node *node,
4287                                                                  Node *(*mutator) (),
4288                                                                  void *context,
4289                                                                  int flags)
4290 {
4291         if (node && IsA(node, Query))
4292                 return (Node *) query_tree_mutator((Query *) node,
4293                                                                                    mutator,
4294                                                                                    context,
4295                                                                                    flags);
4296         else
4297                 return mutator(node, context);
4298 }