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