]> granicus.if.org Git - postgresql/blob - src/backend/executor/execQual.c
87cc201a3e3cf5412c2ebd4d3a523415b880bbdf
[postgresql] / src / backend / executor / execQual.c
1 /*-------------------------------------------------------------------------
2  *
3  * execQual.c
4  *        Routines to evaluate qualification and targetlist expressions
5  *
6  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.172 2005/03/14 04:41:12 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  *       INTERFACE ROUTINES
17  *              ExecEvalExpr    - (now a macro) evaluate an expression, return a datum
18  *              ExecEvalExprSwitchContext - same, but switch into eval memory context
19  *              ExecQual                - return true/false if qualification is satisfied
20  *              ExecProject             - form a new tuple by projecting the given tuple
21  *
22  *       NOTES
23  *              The more heavily used ExecEvalExpr routines, such as ExecEvalVar(),
24  *              are hotspots. Making these faster will speed up the entire system.
25  *
26  *              ExecProject() is used to make tuple projections.  Rather then
27  *              trying to speed it up, the execution plan should be pre-processed
28  *              to facilitate attribute sharing between nodes wherever possible,
29  *              instead of doing needless copying.      -cim 5/31/91
30  *
31  *              During expression evaluation, we check_stack_depth only in
32  *              ExecMakeFunctionResult rather than at every single node.  This
33  *              is a compromise that trades off precision of the stack limit setting
34  *              to gain speed.
35  */
36
37 #include "postgres.h"
38
39 #include "access/heapam.h"
40 #include "catalog/pg_type.h"
41 #include "commands/typecmds.h"
42 #include "executor/execdebug.h"
43 #include "executor/functions.h"
44 #include "executor/nodeSubplan.h"
45 #include "funcapi.h"
46 #include "miscadmin.h"
47 #include "nodes/makefuncs.h"
48 #include "optimizer/planmain.h"
49 #include "parser/parse_expr.h"
50 #include "utils/acl.h"
51 #include "utils/array.h"
52 #include "utils/builtins.h"
53 #include "utils/lsyscache.h"
54 #include "utils/typcache.h"
55
56
57 /* static function decls */
58 static Datum ExecEvalArrayRef(ArrayRefExprState *astate,
59                                  ExprContext *econtext,
60                                  bool *isNull, ExprDoneCond *isDone);
61 static Datum ExecEvalAggref(AggrefExprState *aggref,
62                            ExprContext *econtext,
63                            bool *isNull, ExprDoneCond *isDone);
64 static Datum ExecEvalVar(ExprState *exprstate, ExprContext *econtext,
65                         bool *isNull, ExprDoneCond *isDone);
66 static Datum ExecEvalConst(ExprState *exprstate, ExprContext *econtext,
67                           bool *isNull, ExprDoneCond *isDone);
68 static Datum ExecEvalParam(ExprState *exprstate, ExprContext *econtext,
69                           bool *isNull, ExprDoneCond *isDone);
70 static ExprDoneCond ExecEvalFuncArgs(FunctionCallInfo fcinfo,
71                                  List *argList, ExprContext *econtext);
72 static Datum ExecMakeFunctionResultNoSets(FuncExprState *fcache,
73                                                          ExprContext *econtext,
74                                                          bool *isNull, ExprDoneCond *isDone);
75 static Datum ExecEvalFunc(FuncExprState *fcache, ExprContext *econtext,
76                          bool *isNull, ExprDoneCond *isDone);
77 static Datum ExecEvalOper(FuncExprState *fcache, ExprContext *econtext,
78                          bool *isNull, ExprDoneCond *isDone);
79 static Datum ExecEvalDistinct(FuncExprState *fcache, ExprContext *econtext,
80                                  bool *isNull, ExprDoneCond *isDone);
81 static Datum ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
82                                           ExprContext *econtext,
83                                           bool *isNull, ExprDoneCond *isDone);
84 static Datum ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
85                         bool *isNull, ExprDoneCond *isDone);
86 static Datum ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
87                    bool *isNull, ExprDoneCond *isDone);
88 static Datum ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
89                         bool *isNull, ExprDoneCond *isDone);
90 static Datum ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
91                                                                         ExprContext *econtext,
92                                                                         bool *isNull, ExprDoneCond *isDone);
93 static Datum ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
94                          bool *isNull, ExprDoneCond *isDone);
95 static Datum ExecEvalCaseTestExpr(ExprState *exprstate,
96                                          ExprContext *econtext,
97                                          bool *isNull, ExprDoneCond *isDone);
98 static Datum ExecEvalArray(ArrayExprState *astate,
99                           ExprContext *econtext,
100                           bool *isNull, ExprDoneCond *isDone);
101 static Datum ExecEvalRow(RowExprState *rstate,
102                         ExprContext *econtext,
103                         bool *isNull, ExprDoneCond *isDone);
104 static Datum ExecEvalCoalesce(CoalesceExprState *coalesceExpr,
105                                  ExprContext *econtext,
106                                  bool *isNull, ExprDoneCond *isDone);
107 static Datum ExecEvalNullIf(FuncExprState *nullIfExpr,
108                            ExprContext *econtext,
109                            bool *isNull, ExprDoneCond *isDone);
110 static Datum ExecEvalNullTest(GenericExprState *nstate,
111                                  ExprContext *econtext,
112                                  bool *isNull, ExprDoneCond *isDone);
113 static Datum ExecEvalBooleanTest(GenericExprState *bstate,
114                                         ExprContext *econtext,
115                                         bool *isNull, ExprDoneCond *isDone);
116 static Datum ExecEvalCoerceToDomain(CoerceToDomainState *cstate,
117                                            ExprContext *econtext,
118                                            bool *isNull, ExprDoneCond *isDone);
119 static Datum ExecEvalCoerceToDomainValue(ExprState *exprstate,
120                                                         ExprContext *econtext,
121                                                         bool *isNull, ExprDoneCond *isDone);
122 static Datum ExecEvalFieldSelect(FieldSelectState *fstate,
123                                         ExprContext *econtext,
124                                         bool *isNull, ExprDoneCond *isDone);
125 static Datum ExecEvalFieldStore(FieldStoreState *fstate,
126                                    ExprContext *econtext,
127                                    bool *isNull, ExprDoneCond *isDone);
128 static Datum ExecEvalRelabelType(GenericExprState *exprstate,
129                                         ExprContext *econtext,
130                                         bool *isNull, ExprDoneCond *isDone);
131
132
133 /* ----------------------------------------------------------------
134  *              ExecEvalExpr routines
135  *
136  *              Recursively evaluate a targetlist or qualification expression.
137  *
138  * Each of the following routines having the signature
139  *              Datum ExecEvalFoo(ExprState *expression,
140  *                                                ExprContext *econtext,
141  *                                                bool *isNull,
142  *                                                ExprDoneCond *isDone);
143  * is responsible for evaluating one type or subtype of ExprState node.
144  * They are normally called via the ExecEvalExpr macro, which makes use of
145  * the function pointer set up when the ExprState node was built by
146  * ExecInitExpr.  (In some cases, we change this pointer later to avoid
147  * re-executing one-time overhead.)
148  *
149  * Note: for notational simplicity we declare these functions as taking the
150  * specific type of ExprState that they work on.  This requires casting when
151  * assigning the function pointer in ExecInitExpr.      Be careful that the
152  * function signature is declared correctly, because the cast suppresses
153  * automatic checking!
154  *
155  *
156  * All these functions share this calling convention:
157  *
158  * Inputs:
159  *              expression: the expression state tree to evaluate
160  *              econtext: evaluation context information
161  *
162  * Outputs:
163  *              return value: Datum value of result
164  *              *isNull: set to TRUE if result is NULL (actual return value is
165  *                               meaningless if so); set to FALSE if non-null result
166  *              *isDone: set to indicator of set-result status
167  *
168  * A caller that can only accept a singleton (non-set) result should pass
169  * NULL for isDone; if the expression computes a set result then an error
170  * will be reported via ereport.  If the caller does pass an isDone pointer
171  * then *isDone is set to one of these three states:
172  *              ExprSingleResult                singleton result (not a set)
173  *              ExprMultipleResult              return value is one element of a set
174  *              ExprEndResult                   there are no more elements in the set
175  * When ExprMultipleResult is returned, the caller should invoke
176  * ExecEvalExpr() repeatedly until ExprEndResult is returned.  ExprEndResult
177  * is returned after the last real set element.  For convenience isNull will
178  * always be set TRUE when ExprEndResult is returned, but this should not be
179  * taken as indicating a NULL element of the set.  Note that these return
180  * conventions allow us to distinguish among a singleton NULL, a NULL element
181  * of a set, and an empty set.
182  *
183  * The caller should already have switched into the temporary memory
184  * context econtext->ecxt_per_tuple_memory.  The convenience entry point
185  * ExecEvalExprSwitchContext() is provided for callers who don't prefer to
186  * do the switch in an outer loop.      We do not do the switch in these routines
187  * because it'd be a waste of cycles during nested expression evaluation.
188  * ----------------------------------------------------------------
189  */
190
191
192 /*----------
193  *        ExecEvalArrayRef
194  *
195  *         This function takes an ArrayRef and returns the extracted Datum
196  *         if it's a simple reference, or the modified array value if it's
197  *         an array assignment (i.e., array element or slice insertion).
198  *
199  * NOTE: if we get a NULL result from a subexpression, we return NULL when
200  * it's an array reference, or the unmodified source array when it's an
201  * array assignment.  This may seem peculiar, but if we return NULL (as was
202  * done in versions up through 7.0) then an assignment like
203  *                      UPDATE table SET arrayfield[4] = NULL
204  * will result in setting the whole array to NULL, which is certainly not
205  * very desirable.      By returning the source array we make the assignment
206  * into a no-op, instead.  (Eventually we need to redesign arrays so that
207  * individual elements can be NULL, but for now, let's try to protect users
208  * from shooting themselves in the foot.)
209  *
210  * NOTE: we deliberately refrain from applying DatumGetArrayTypeP() here,
211  * even though that might seem natural, because this code needs to support
212  * both varlena arrays and fixed-length array types.  DatumGetArrayTypeP()
213  * only works for the varlena kind.  The routines we call in arrayfuncs.c
214  * have to know the difference (that's what they need refattrlength for).
215  *----------
216  */
217 static Datum
218 ExecEvalArrayRef(ArrayRefExprState *astate,
219                                  ExprContext *econtext,
220                                  bool *isNull,
221                                  ExprDoneCond *isDone)
222 {
223         ArrayRef   *arrayRef = (ArrayRef *) astate->xprstate.expr;
224         ArrayType  *array_source;
225         ArrayType  *resultArray;
226         bool            isAssignment = (arrayRef->refassgnexpr != NULL);
227         bool            eisnull;
228         ListCell   *l;
229         int                     i = 0,
230                                 j = 0;
231         IntArray        upper,
232                                 lower;
233         int                *lIndex;
234
235         array_source = (ArrayType *)
236                 DatumGetPointer(ExecEvalExpr(astate->refexpr,
237                                                                          econtext,
238                                                                          isNull,
239                                                                          isDone));
240
241         /*
242          * If refexpr yields NULL, and it's a fetch, then result is NULL. In
243          * the assignment case, we'll cons up something below.
244          */
245         if (*isNull)
246         {
247                 if (isDone && *isDone == ExprEndResult)
248                         return (Datum) NULL;    /* end of set result */
249                 if (!isAssignment)
250                         return (Datum) NULL;
251         }
252
253         foreach(l, astate->refupperindexpr)
254         {
255                 ExprState  *eltstate = (ExprState *) lfirst(l);
256
257                 if (i >= MAXDIM)
258                         ereport(ERROR,
259                                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
260                                          errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
261                                                         i, MAXDIM)));
262
263                 upper.indx[i++] = DatumGetInt32(ExecEvalExpr(eltstate,
264                                                                                                          econtext,
265                                                                                                          &eisnull,
266                                                                                                          NULL));
267                 /* If any index expr yields NULL, result is NULL or source array */
268                 if (eisnull)
269                 {
270                         if (!isAssignment)
271                         {
272                                 *isNull = true;
273                                 return (Datum) NULL;
274                         }
275                         return PointerGetDatum(array_source);
276                 }
277         }
278
279         if (astate->reflowerindexpr != NIL)
280         {
281                 foreach(l, astate->reflowerindexpr)
282                 {
283                         ExprState  *eltstate = (ExprState *) lfirst(l);
284
285                         if (j >= MAXDIM)
286                                 ereport(ERROR,
287                                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
288                                                  errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
289                                                                 i, MAXDIM)));
290
291                         lower.indx[j++] = DatumGetInt32(ExecEvalExpr(eltstate,
292                                                                                                                  econtext,
293                                                                                                                  &eisnull,
294                                                                                                                  NULL));
295
296                         /*
297                          * If any index expr yields NULL, result is NULL or source
298                          * array
299                          */
300                         if (eisnull)
301                         {
302                                 if (!isAssignment)
303                                 {
304                                         *isNull = true;
305                                         return (Datum) NULL;
306                                 }
307                                 return PointerGetDatum(array_source);
308                         }
309                 }
310                 /* this can't happen unless parser messed up */
311                 if (i != j)
312                         elog(ERROR, "upper and lower index lists are not same length");
313                 lIndex = lower.indx;
314         }
315         else
316                 lIndex = NULL;
317
318         if (isAssignment)
319         {
320                 Datum           sourceData;
321
322                 /*
323                  * Evaluate the value to be assigned into the array.
324                  *
325                  * XXX At some point we'll need to look into making the old value of
326                  * the array element available via CaseTestExpr, as is done by
327                  * ExecEvalFieldStore.  This is not needed now but will be needed
328                  * to support arrays of composite types; in an assignment to a
329                  * field of an array member, the parser would generate a
330                  * FieldStore that expects to fetch its input tuple via
331                  * CaseTestExpr.
332                  */
333                 sourceData = ExecEvalExpr(astate->refassgnexpr,
334                                                                   econtext,
335                                                                   &eisnull,
336                                                                   NULL);
337
338                 /*
339                  * For now, can't cope with inserting NULL into an array, so make
340                  * it a no-op per discussion above...
341                  */
342                 if (eisnull)
343                         return PointerGetDatum(array_source);
344
345                 /*
346                  * For an assignment, if all the subscripts and the input
347                  * expression are non-null but the original array is null, then
348                  * substitute an empty (zero-dimensional) array and proceed with
349                  * the assignment. This only works for varlena arrays, though; for
350                  * fixed-length array types we punt and return the null input
351                  * array.
352                  */
353                 if (*isNull)
354                 {
355                         if (astate->refattrlength > 0)          /* fixed-length array? */
356                                 return PointerGetDatum(array_source);
357
358                         array_source = construct_md_array(NULL, 0, NULL, NULL,
359                                                                                           arrayRef->refelemtype,
360                                                                                           astate->refelemlength,
361                                                                                           astate->refelembyval,
362                                                                                           astate->refelemalign);
363                         *isNull = false;
364                 }
365
366                 if (lIndex == NULL)
367                         resultArray = array_set(array_source, i,
368                                                                         upper.indx,
369                                                                         sourceData,
370                                                                         astate->refattrlength,
371                                                                         astate->refelemlength,
372                                                                         astate->refelembyval,
373                                                                         astate->refelemalign,
374                                                                         isNull);
375                 else
376                         resultArray = array_set_slice(array_source, i,
377                                                                                   upper.indx, lower.indx,
378                                                            (ArrayType *) DatumGetPointer(sourceData),
379                                                                                   astate->refattrlength,
380                                                                                   astate->refelemlength,
381                                                                                   astate->refelembyval,
382                                                                                   astate->refelemalign,
383                                                                                   isNull);
384                 return PointerGetDatum(resultArray);
385         }
386
387         if (lIndex == NULL)
388                 return array_ref(array_source, i, upper.indx,
389                                                  astate->refattrlength,
390                                                  astate->refelemlength,
391                                                  astate->refelembyval,
392                                                  astate->refelemalign,
393                                                  isNull);
394         else
395         {
396                 resultArray = array_get_slice(array_source, i,
397                                                                           upper.indx, lower.indx,
398                                                                           astate->refattrlength,
399                                                                           astate->refelemlength,
400                                                                           astate->refelembyval,
401                                                                           astate->refelemalign,
402                                                                           isNull);
403                 return PointerGetDatum(resultArray);
404         }
405 }
406
407
408 /* ----------------------------------------------------------------
409  *              ExecEvalAggref
410  *
411  *              Returns a Datum whose value is the value of the precomputed
412  *              aggregate found in the given expression context.
413  * ----------------------------------------------------------------
414  */
415 static Datum
416 ExecEvalAggref(AggrefExprState *aggref, ExprContext *econtext,
417                            bool *isNull, ExprDoneCond *isDone)
418 {
419         if (isDone)
420                 *isDone = ExprSingleResult;
421
422         if (econtext->ecxt_aggvalues == NULL)           /* safety check */
423                 elog(ERROR, "no aggregates in this expression context");
424
425         *isNull = econtext->ecxt_aggnulls[aggref->aggno];
426         return econtext->ecxt_aggvalues[aggref->aggno];
427 }
428
429 /* ----------------------------------------------------------------
430  *              ExecEvalVar
431  *
432  *              Returns a Datum whose value is the value of a range
433  *              variable with respect to given expression context.
434  * ----------------------------------------------------------------
435  */
436 static Datum
437 ExecEvalVar(ExprState *exprstate, ExprContext *econtext,
438                         bool *isNull, ExprDoneCond *isDone)
439 {
440         Var                *variable = (Var *) exprstate->expr;
441         TupleTableSlot *slot;
442         AttrNumber      attnum;
443
444         if (isDone)
445                 *isDone = ExprSingleResult;
446
447         /*
448          * Get the slot and attribute number we want
449          *
450          * The asserts check that references to system attributes only appear at
451          * the level of a relation scan; at higher levels, system attributes
452          * must be treated as ordinary variables (since we no longer have
453          * access to the original tuple).
454          */
455         attnum = variable->varattno;
456
457         switch (variable->varno)
458         {
459                 case INNER:                             /* get the tuple from the inner node */
460                         slot = econtext->ecxt_innertuple;
461                         Assert(attnum > 0);
462                         break;
463
464                 case OUTER:                             /* get the tuple from the outer node */
465                         slot = econtext->ecxt_outertuple;
466                         Assert(attnum > 0);
467                         break;
468
469                 default:                                /* get the tuple from the relation being
470                                                                  * scanned */
471                         slot = econtext->ecxt_scantuple;
472                         break;
473         }
474
475 #ifdef USE_ASSERT_CHECKING
476         /*
477          * Some checks that are only applied for user attribute numbers (bogus
478          * system attnums will be caught inside slot_getattr).
479          */
480         if (attnum > 0)
481         {
482                 TupleDesc       tuple_type = slot->ttc_tupleDescriptor;
483
484                 /*
485                  * This assert checks that the attnum is valid.
486                  */
487                 Assert(attnum <= tuple_type->natts);
488
489                 /*
490                  * This assert checks that the datatype the plan expects to get
491                  * (as told by our "variable" argument) is in fact the datatype of
492                  * the attribute being fetched (as seen in the current context,
493                  * identified by our "econtext" argument).      Otherwise crashes are
494                  * likely.
495                  *
496                  * Note that we can't check dropped columns, since their atttypid has
497                  * been zeroed.
498                  */
499                 Assert(variable->vartype == tuple_type->attrs[attnum - 1]->atttypid ||
500                            tuple_type->attrs[attnum - 1]->attisdropped);
501         }
502 #endif /* USE_ASSERT_CHECKING */
503
504         return slot_getattr(slot, attnum, isNull);
505 }
506
507 /* ----------------------------------------------------------------
508  *              ExecEvalConst
509  *
510  *              Returns the value of a constant.
511  *
512  *              Note that for pass-by-ref datatypes, we return a pointer to the
513  *              actual constant node.  This is one of the reasons why functions
514  *              must treat their input arguments as read-only.
515  * ----------------------------------------------------------------
516  */
517 static Datum
518 ExecEvalConst(ExprState *exprstate, ExprContext *econtext,
519                           bool *isNull, ExprDoneCond *isDone)
520 {
521         Const      *con = (Const *) exprstate->expr;
522
523         if (isDone)
524                 *isDone = ExprSingleResult;
525
526         *isNull = con->constisnull;
527         return con->constvalue;
528 }
529
530 /* ----------------------------------------------------------------
531  *              ExecEvalParam
532  *
533  *              Returns the value of a parameter.  A param node contains
534  *              something like ($.name) and the expression context contains
535  *              the current parameter bindings (name = "sam") (age = 34)...
536  *              so our job is to find and return the appropriate datum ("sam").
537  *
538  *              Q: if we have a parameter ($.foo) without a binding, i.e.
539  *                 there is no (foo = xxx) in the parameter list info,
540  *                 is this a fatal error or should this be a "not available"
541  *                 (in which case we could return NULL)?        -cim 10/13/89
542  * ----------------------------------------------------------------
543  */
544 static Datum
545 ExecEvalParam(ExprState *exprstate, ExprContext *econtext,
546                           bool *isNull, ExprDoneCond *isDone)
547 {
548         Param      *expression = (Param *) exprstate->expr;
549         int                     thisParamKind = expression->paramkind;
550         AttrNumber      thisParamId = expression->paramid;
551
552         if (isDone)
553                 *isDone = ExprSingleResult;
554
555         if (thisParamKind == PARAM_EXEC)
556         {
557                 /*
558                  * PARAM_EXEC params (internal executor parameters) are stored in
559                  * the ecxt_param_exec_vals array, and can be accessed by array
560                  * index.
561                  */
562                 ParamExecData *prm;
563
564                 prm = &(econtext->ecxt_param_exec_vals[thisParamId]);
565                 if (prm->execPlan != NULL)
566                 {
567                         /* Parameter not evaluated yet, so go do it */
568                         ExecSetParamPlan(prm->execPlan, econtext);
569                         /* ExecSetParamPlan should have processed this param... */
570                         Assert(prm->execPlan == NULL);
571                 }
572                 *isNull = prm->isnull;
573                 return prm->value;
574         }
575         else
576         {
577                 /*
578                  * All other parameter types must be sought in
579                  * ecxt_param_list_info.
580                  */
581                 ParamListInfo paramInfo;
582
583                 paramInfo = lookupParam(econtext->ecxt_param_list_info,
584                                                                 thisParamKind,
585                                                                 expression->paramname,
586                                                                 thisParamId,
587                                                                 false);
588                 Assert(paramInfo->ptype == expression->paramtype);
589                 *isNull = paramInfo->isnull;
590                 return paramInfo->value;
591         }
592 }
593
594
595 /* ----------------------------------------------------------------
596  *              ExecEvalOper / ExecEvalFunc support routines
597  * ----------------------------------------------------------------
598  */
599
600 /*
601  *              GetAttributeByName
602  *              GetAttributeByNum
603  *
604  *              These functions return the value of the requested attribute
605  *              out of the given tuple Datum.
606  *              C functions which take a tuple as an argument are expected
607  *              to use these.  Ex: overpaid(EMP) might call GetAttributeByNum().
608  *              Note: these are actually rather slow because they do a typcache
609  *              lookup on each call.
610  */
611 Datum
612 GetAttributeByNum(HeapTupleHeader tuple,
613                                   AttrNumber attrno,
614                                   bool *isNull)
615 {
616         Datum           result;
617         Oid                     tupType;
618         int32           tupTypmod;
619         TupleDesc       tupDesc;
620         HeapTupleData tmptup;
621
622         if (!AttributeNumberIsValid(attrno))
623                 elog(ERROR, "invalid attribute number %d", attrno);
624
625         if (isNull == NULL)
626                 elog(ERROR, "a NULL isNull pointer was passed");
627
628         if (tuple == NULL)
629         {
630                 /* Kinda bogus but compatible with old behavior... */
631                 *isNull = true;
632                 return (Datum) 0;
633         }
634
635         tupType = HeapTupleHeaderGetTypeId(tuple);
636         tupTypmod = HeapTupleHeaderGetTypMod(tuple);
637         tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
638
639         /*
640          * heap_getattr needs a HeapTuple not a bare HeapTupleHeader.  We set
641          * all the fields in the struct just in case user tries to inspect
642          * system columns.
643          */
644         tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
645         ItemPointerSetInvalid(&(tmptup.t_self));
646         tmptup.t_tableOid = InvalidOid;
647         tmptup.t_data = tuple;
648
649         result = heap_getattr(&tmptup,
650                                                   attrno,
651                                                   tupDesc,
652                                                   isNull);
653         return result;
654 }
655
656 Datum
657 GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
658 {
659         AttrNumber      attrno;
660         Datum           result;
661         Oid                     tupType;
662         int32           tupTypmod;
663         TupleDesc       tupDesc;
664         HeapTupleData tmptup;
665         int                     i;
666
667         if (attname == NULL)
668                 elog(ERROR, "invalid attribute name");
669
670         if (isNull == NULL)
671                 elog(ERROR, "a NULL isNull pointer was passed");
672
673         if (tuple == NULL)
674         {
675                 /* Kinda bogus but compatible with old behavior... */
676                 *isNull = true;
677                 return (Datum) 0;
678         }
679
680         tupType = HeapTupleHeaderGetTypeId(tuple);
681         tupTypmod = HeapTupleHeaderGetTypMod(tuple);
682         tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
683
684         attrno = InvalidAttrNumber;
685         for (i = 0; i < tupDesc->natts; i++)
686         {
687                 if (namestrcmp(&(tupDesc->attrs[i]->attname), attname) == 0)
688                 {
689                         attrno = tupDesc->attrs[i]->attnum;
690                         break;
691                 }
692         }
693
694         if (attrno == InvalidAttrNumber)
695                 elog(ERROR, "attribute \"%s\" does not exist", attname);
696
697         /*
698          * heap_getattr needs a HeapTuple not a bare HeapTupleHeader.  We set
699          * all the fields in the struct just in case user tries to inspect
700          * system columns.
701          */
702         tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
703         ItemPointerSetInvalid(&(tmptup.t_self));
704         tmptup.t_tableOid = InvalidOid;
705         tmptup.t_data = tuple;
706
707         result = heap_getattr(&tmptup,
708                                                   attrno,
709                                                   tupDesc,
710                                                   isNull);
711         return result;
712 }
713
714 /*
715  * init_fcache - initialize a FuncExprState node during first use
716  */
717 void
718 init_fcache(Oid foid, FuncExprState *fcache, MemoryContext fcacheCxt)
719 {
720         AclResult       aclresult;
721
722         /* Check permission to call function */
723         aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE);
724         if (aclresult != ACLCHECK_OK)
725                 aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(foid));
726
727         /* Safety check (should never fail, as parser should check sooner) */
728         if (list_length(fcache->args) > FUNC_MAX_ARGS)
729                 elog(ERROR, "too many arguments");
730
731         /* Set up the primary fmgr lookup information */
732         fmgr_info_cxt(foid, &(fcache->func), fcacheCxt);
733
734         /* Initialize additional info */
735         fcache->setArgsValid = false;
736         fcache->shutdown_reg = false;
737         fcache->func.fn_expr = (Node *) fcache->xprstate.expr;
738 }
739
740 /*
741  * callback function in case a FuncExpr returning a set needs to be shut down
742  * before it has been run to completion
743  */
744 static void
745 ShutdownFuncExpr(Datum arg)
746 {
747         FuncExprState *fcache = (FuncExprState *) DatumGetPointer(arg);
748
749         /* Clear any active set-argument state */
750         fcache->setArgsValid = false;
751
752         /* execUtils will deregister the callback... */
753         fcache->shutdown_reg = false;
754 }
755
756 /*
757  * Evaluate arguments for a function.
758  */
759 static ExprDoneCond
760 ExecEvalFuncArgs(FunctionCallInfo fcinfo,
761                                  List *argList,
762                                  ExprContext *econtext)
763 {
764         ExprDoneCond argIsDone;
765         int                     i;
766         ListCell   *arg;
767
768         argIsDone = ExprSingleResult;           /* default assumption */
769
770         i = 0;
771         foreach(arg, argList)
772         {
773                 ExprState  *argstate = (ExprState *) lfirst(arg);
774                 ExprDoneCond thisArgIsDone;
775
776                 fcinfo->arg[i] = ExecEvalExpr(argstate,
777                                                                           econtext,
778                                                                           &fcinfo->argnull[i],
779                                                                           &thisArgIsDone);
780
781                 if (thisArgIsDone != ExprSingleResult)
782                 {
783                         /*
784                          * We allow only one argument to have a set value; we'd need
785                          * much more complexity to keep track of multiple set
786                          * arguments (cf. ExecTargetList) and it doesn't seem worth
787                          * it.
788                          */
789                         if (argIsDone != ExprSingleResult)
790                                 ereport(ERROR,
791                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
792                                                  errmsg("functions and operators can take at most one set argument")));
793                         argIsDone = thisArgIsDone;
794                 }
795                 i++;
796         }
797
798         fcinfo->nargs = i;
799
800         return argIsDone;
801 }
802
803 /*
804  *              ExecMakeFunctionResult
805  *
806  * Evaluate the arguments to a function and then the function itself.
807  */
808 Datum
809 ExecMakeFunctionResult(FuncExprState *fcache,
810                                            ExprContext *econtext,
811                                            bool *isNull,
812                                            ExprDoneCond *isDone)
813 {
814         List       *arguments = fcache->args;
815         Datum           result;
816         FunctionCallInfoData fcinfo;
817         ReturnSetInfo rsinfo;           /* for functions returning sets */
818         ExprDoneCond argDone;
819         bool            hasSetArg;
820         int                     i;
821
822         /* Guard against stack overflow due to overly complex expressions */
823         check_stack_depth();
824
825         /*
826          * arguments is a list of expressions to evaluate before passing to
827          * the function manager.  We skip the evaluation if it was already
828          * done in the previous call (ie, we are continuing the evaluation of
829          * a set-valued function).      Otherwise, collect the current argument
830          * values into fcinfo.
831          */
832         if (!fcache->setArgsValid)
833         {
834                 /* Need to prep callinfo structure */
835                 MemSet(&fcinfo, 0, sizeof(fcinfo));
836                 fcinfo.flinfo = &(fcache->func);
837                 argDone = ExecEvalFuncArgs(&fcinfo, arguments, econtext);
838                 if (argDone == ExprEndResult)
839                 {
840                         /* input is an empty set, so return an empty set. */
841                         *isNull = true;
842                         if (isDone)
843                                 *isDone = ExprEndResult;
844                         else
845                                 ereport(ERROR,
846                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
847                                                  errmsg("set-valued function called in context that cannot accept a set")));
848                         return (Datum) 0;
849                 }
850                 hasSetArg = (argDone != ExprSingleResult);
851         }
852         else
853         {
854                 /* Copy callinfo from previous evaluation */
855                 memcpy(&fcinfo, &fcache->setArgs, sizeof(fcinfo));
856                 hasSetArg = fcache->setHasSetArg;
857                 /* Reset flag (we may set it again below) */
858                 fcache->setArgsValid = false;
859         }
860
861         /*
862          * If function returns set, prepare a resultinfo node for
863          * communication
864          */
865         if (fcache->func.fn_retset)
866         {
867                 fcinfo.resultinfo = (Node *) &rsinfo;
868                 rsinfo.type = T_ReturnSetInfo;
869                 rsinfo.econtext = econtext;
870                 rsinfo.expectedDesc = NULL;
871                 rsinfo.allowedModes = (int) SFRM_ValuePerCall;
872                 rsinfo.returnMode = SFRM_ValuePerCall;
873                 /* isDone is filled below */
874                 rsinfo.setResult = NULL;
875                 rsinfo.setDesc = NULL;
876         }
877
878         /*
879          * now return the value gotten by calling the function manager,
880          * passing the function the evaluated parameter values.
881          */
882         if (fcache->func.fn_retset || hasSetArg)
883         {
884                 /*
885                  * We need to return a set result.      Complain if caller not ready
886                  * to accept one.
887                  */
888                 if (isDone == NULL)
889                         ereport(ERROR,
890                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
891                                          errmsg("set-valued function called in context that cannot accept a set")));
892
893                 /*
894                  * This loop handles the situation where we have both a set
895                  * argument and a set-valued function.  Once we have exhausted the
896                  * function's value(s) for a particular argument value, we have to
897                  * get the next argument value and start the function over again.
898                  * We might have to do it more than once, if the function produces
899                  * an empty result set for a particular input value.
900                  */
901                 for (;;)
902                 {
903                         /*
904                          * If function is strict, and there are any NULL arguments,
905                          * skip calling the function (at least for this set of args).
906                          */
907                         bool            callit = true;
908
909                         if (fcache->func.fn_strict)
910                         {
911                                 for (i = 0; i < fcinfo.nargs; i++)
912                                 {
913                                         if (fcinfo.argnull[i])
914                                         {
915                                                 callit = false;
916                                                 break;
917                                         }
918                                 }
919                         }
920
921                         if (callit)
922                         {
923                                 fcinfo.isnull = false;
924                                 rsinfo.isDone = ExprSingleResult;
925                                 result = FunctionCallInvoke(&fcinfo);
926                                 *isNull = fcinfo.isnull;
927                                 *isDone = rsinfo.isDone;
928                         }
929                         else
930                         {
931                                 result = (Datum) 0;
932                                 *isNull = true;
933                                 *isDone = ExprEndResult;
934                         }
935
936                         if (*isDone != ExprEndResult)
937                         {
938                                 /*
939                                  * Got a result from current argument.  If function itself
940                                  * returns set, save the current argument values to re-use
941                                  * on the next call.
942                                  */
943                                 if (fcache->func.fn_retset)
944                                 {
945                                         memcpy(&fcache->setArgs, &fcinfo, sizeof(fcinfo));
946                                         fcache->setHasSetArg = hasSetArg;
947                                         fcache->setArgsValid = true;
948                                         /* Register cleanup callback if we didn't already */
949                                         if (!fcache->shutdown_reg)
950                                         {
951                                                 RegisterExprContextCallback(econtext,
952                                                                                                         ShutdownFuncExpr,
953                                                                                                 PointerGetDatum(fcache));
954                                                 fcache->shutdown_reg = true;
955                                         }
956                                 }
957
958                                 /*
959                                  * Make sure we say we are returning a set, even if the
960                                  * function itself doesn't return sets.
961                                  */
962                                 *isDone = ExprMultipleResult;
963                                 break;
964                         }
965
966                         /* Else, done with this argument */
967                         if (!hasSetArg)
968                                 break;                  /* input not a set, so done */
969
970                         /* Re-eval args to get the next element of the input set */
971                         argDone = ExecEvalFuncArgs(&fcinfo, arguments, econtext);
972
973                         if (argDone != ExprMultipleResult)
974                         {
975                                 /* End of argument set, so we're done. */
976                                 *isNull = true;
977                                 *isDone = ExprEndResult;
978                                 result = (Datum) 0;
979                                 break;
980                         }
981
982                         /*
983                          * If we reach here, loop around to run the function on the
984                          * new argument.
985                          */
986                 }
987         }
988         else
989         {
990                 /*
991                  * Non-set case: much easier.
992                  *
993                  * We change the ExprState function pointer to use the simpler
994                  * ExecMakeFunctionResultNoSets on subsequent calls.  This amounts
995                  * to assuming that no argument can return a set if it didn't do
996                  * so the first time.
997                  */
998                 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResultNoSets;
999
1000                 if (isDone)
1001                         *isDone = ExprSingleResult;
1002
1003                 /*
1004                  * If function is strict, and there are any NULL arguments, skip
1005                  * calling the function and return NULL.
1006                  */
1007                 if (fcache->func.fn_strict)
1008                 {
1009                         for (i = 0; i < fcinfo.nargs; i++)
1010                         {
1011                                 if (fcinfo.argnull[i])
1012                                 {
1013                                         *isNull = true;
1014                                         return (Datum) 0;
1015                                 }
1016                         }
1017                 }
1018                 fcinfo.isnull = false;
1019                 result = FunctionCallInvoke(&fcinfo);
1020                 *isNull = fcinfo.isnull;
1021         }
1022
1023         return result;
1024 }
1025
1026 /*
1027  *              ExecMakeFunctionResultNoSets
1028  *
1029  * Simplified version of ExecMakeFunctionResult that can only handle
1030  * non-set cases.  Hand-tuned for speed.
1031  */
1032 static Datum
1033 ExecMakeFunctionResultNoSets(FuncExprState *fcache,
1034                                                          ExprContext *econtext,
1035                                                          bool *isNull,
1036                                                          ExprDoneCond *isDone)
1037 {
1038         ListCell   *arg;
1039         Datum           result;
1040         FunctionCallInfoData fcinfo;
1041         int                     i;
1042
1043         /* Guard against stack overflow due to overly complex expressions */
1044         check_stack_depth();
1045
1046         if (isDone)
1047                 *isDone = ExprSingleResult;
1048
1049         MemSet(&fcinfo, 0, sizeof(fcinfo));
1050         fcinfo.flinfo = &(fcache->func);
1051
1052         /* inlined, simplified version of ExecEvalFuncArgs */
1053         i = 0;
1054         foreach(arg, fcache->args)
1055         {
1056                 ExprState  *argstate = (ExprState *) lfirst(arg);
1057                 ExprDoneCond thisArgIsDone;
1058
1059                 fcinfo.arg[i] = ExecEvalExpr(argstate,
1060                                                                          econtext,
1061                                                                          &fcinfo.argnull[i],
1062                                                                          &thisArgIsDone);
1063
1064                 if (thisArgIsDone != ExprSingleResult)
1065                         ereport(ERROR,
1066                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1067                                          errmsg("set-valued function called in context that cannot accept a set")));
1068                 i++;
1069         }
1070         fcinfo.nargs = i;
1071
1072         /*
1073          * If function is strict, and there are any NULL arguments, skip
1074          * calling the function and return NULL.
1075          */
1076         if (fcache->func.fn_strict)
1077         {
1078                 while (--i >= 0)
1079                 {
1080                         if (fcinfo.argnull[i])
1081                         {
1082                                 *isNull = true;
1083                                 return (Datum) 0;
1084                         }
1085                 }
1086         }
1087         /* fcinfo.isnull = false; */    /* handled by MemSet */
1088         result = FunctionCallInvoke(&fcinfo);
1089         *isNull = fcinfo.isnull;
1090
1091         return result;
1092 }
1093
1094
1095 /*
1096  *              ExecMakeTableFunctionResult
1097  *
1098  * Evaluate a table function, producing a materialized result in a Tuplestore
1099  * object.  *returnDesc is set to the tupledesc actually returned by the
1100  * function, or NULL if it didn't provide one.
1101  */
1102 Tuplestorestate *
1103 ExecMakeTableFunctionResult(ExprState *funcexpr,
1104                                                         ExprContext *econtext,
1105                                                         TupleDesc expectedDesc,
1106                                                         TupleDesc *returnDesc)
1107 {
1108         Tuplestorestate *tupstore = NULL;
1109         TupleDesc       tupdesc = NULL;
1110         Oid                     funcrettype;
1111         bool            returnsTuple;
1112         bool            returnsSet = false;
1113         FunctionCallInfoData fcinfo;
1114         ReturnSetInfo rsinfo;
1115         HeapTupleData tmptup;
1116         MemoryContext callerContext;
1117         MemoryContext oldcontext;
1118         bool            direct_function_call;
1119         bool            first_time = true;
1120
1121         callerContext = CurrentMemoryContext;
1122
1123         funcrettype = exprType((Node *) funcexpr->expr);
1124
1125         returnsTuple = (funcrettype == RECORDOID ||
1126                                         get_typtype(funcrettype) == 'c');
1127
1128         /*
1129          * Prepare a resultinfo node for communication.  We always do this
1130          * even if not expecting a set result, so that we can pass
1131          * expectedDesc.  In the generic-expression case, the expression
1132          * doesn't actually get to see the resultinfo, but set it up anyway
1133          * because we use some of the fields as our own state variables.
1134          */
1135         MemSet(&fcinfo, 0, sizeof(fcinfo));
1136         fcinfo.resultinfo = (Node *) &rsinfo;
1137         rsinfo.type = T_ReturnSetInfo;
1138         rsinfo.econtext = econtext;
1139         rsinfo.expectedDesc = expectedDesc;
1140         rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize);
1141         rsinfo.returnMode = SFRM_ValuePerCall;
1142         /* isDone is filled below */
1143         rsinfo.setResult = NULL;
1144         rsinfo.setDesc = NULL;
1145
1146         /*
1147          * Normally the passed expression tree will be a FuncExprState, since
1148          * the grammar only allows a function call at the top level of a table
1149          * function reference.  However, if the function doesn't return set
1150          * then the planner might have replaced the function call via
1151          * constant-folding or inlining.  So if we see any other kind of
1152          * expression node, execute it via the general ExecEvalExpr() code;
1153          * the only difference is that we don't get a chance to pass a special
1154          * ReturnSetInfo to any functions buried in the expression.
1155          */
1156         if (funcexpr && IsA(funcexpr, FuncExprState) &&
1157                 IsA(funcexpr->expr, FuncExpr))
1158         {
1159                 FuncExprState *fcache = (FuncExprState *) funcexpr;
1160                 ExprDoneCond argDone;
1161
1162                 /*
1163                  * This path is similar to ExecMakeFunctionResult.
1164                  */
1165                 direct_function_call = true;
1166
1167                 /*
1168                  * Initialize function cache if first time through
1169                  */
1170                 if (fcache->func.fn_oid == InvalidOid)
1171                 {
1172                         FuncExpr   *func = (FuncExpr *) fcache->xprstate.expr;
1173
1174                         init_fcache(func->funcid, fcache, econtext->ecxt_per_query_memory);
1175                 }
1176                 returnsSet = fcache->func.fn_retset;
1177
1178                 /*
1179                  * Evaluate the function's argument list.
1180                  *
1181                  * Note: ideally, we'd do this in the per-tuple context, but then the
1182                  * argument values would disappear when we reset the context in
1183                  * the inner loop.      So do it in caller context.  Perhaps we should
1184                  * make a separate context just to hold the evaluated arguments?
1185                  */
1186                 fcinfo.flinfo = &(fcache->func);
1187                 argDone = ExecEvalFuncArgs(&fcinfo, fcache->args, econtext);
1188                 /* We don't allow sets in the arguments of the table function */
1189                 if (argDone != ExprSingleResult)
1190                         ereport(ERROR,
1191                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1192                                          errmsg("set-valued function called in context that cannot accept a set")));
1193
1194                 /*
1195                  * If function is strict, and there are any NULL arguments, skip
1196                  * calling the function and act like it returned NULL (or an empty
1197                  * set, in the returns-set case).
1198                  */
1199                 if (fcache->func.fn_strict)
1200                 {
1201                         int                     i;
1202
1203                         for (i = 0; i < fcinfo.nargs; i++)
1204                         {
1205                                 if (fcinfo.argnull[i])
1206                                         goto no_function_result;
1207                         }
1208                 }
1209         }
1210         else
1211         {
1212                 /* Treat funcexpr as a generic expression */
1213                 direct_function_call = false;
1214         }
1215
1216         /*
1217          * Switch to short-lived context for calling the function or
1218          * expression.
1219          */
1220         MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1221
1222         /*
1223          * Loop to handle the ValuePerCall protocol (which is also the same
1224          * behavior needed in the generic ExecEvalExpr path).
1225          */
1226         for (;;)
1227         {
1228                 Datum           result;
1229                 HeapTuple       tuple;
1230
1231                 /*
1232                  * reset per-tuple memory context before each call of the function
1233                  * or expression. This cleans up any local memory the function may
1234                  * leak when called.
1235                  */
1236                 ResetExprContext(econtext);
1237
1238                 /* Call the function or expression one time */
1239                 if (direct_function_call)
1240                 {
1241                         fcinfo.isnull = false;
1242                         rsinfo.isDone = ExprSingleResult;
1243                         result = FunctionCallInvoke(&fcinfo);
1244                 }
1245                 else
1246                 {
1247                         result = ExecEvalExpr(funcexpr, econtext,
1248                                                                   &fcinfo.isnull, &rsinfo.isDone);
1249                 }
1250
1251                 /* Which protocol does function want to use? */
1252                 if (rsinfo.returnMode == SFRM_ValuePerCall)
1253                 {
1254                         /*
1255                          * Check for end of result set.
1256                          */
1257                         if (rsinfo.isDone == ExprEndResult)
1258                                 break;
1259
1260                         /*
1261                          * Can't do anything very useful with NULL rowtype values.
1262                          * For a function returning set, we consider this a protocol
1263                          * violation (but another alternative would be to just ignore
1264                          * the result and "continue" to get another row).  For a function
1265                          * not returning set, we fall out of the loop; we'll cons up
1266                          * an all-nulls result row below.
1267                          */
1268                         if (returnsTuple && fcinfo.isnull)
1269                         {
1270                                 if (!returnsSet)
1271                                         break;
1272                                 ereport(ERROR,
1273                                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1274                                                  errmsg("function returning set of rows cannot return null value")));
1275                         }
1276
1277                         /*
1278                          * If first time through, build tupdesc and tuplestore for
1279                          * result
1280                          */
1281                         if (first_time)
1282                         {
1283                                 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1284                                 if (returnsTuple)
1285                                 {
1286                                         /*
1287                                          * Use the type info embedded in the rowtype Datum to
1288                                          * look up the needed tupdesc.  Make a copy for the
1289                                          * query.
1290                                          */
1291                                         HeapTupleHeader td;
1292
1293                                         td = DatumGetHeapTupleHeader(result);
1294                                         tupdesc = lookup_rowtype_tupdesc(HeapTupleHeaderGetTypeId(td),
1295                                                                                    HeapTupleHeaderGetTypMod(td));
1296                                         tupdesc = CreateTupleDescCopy(tupdesc);
1297                                 }
1298                                 else
1299                                 {
1300                                         /*
1301                                          * Scalar type, so make a single-column descriptor
1302                                          */
1303                                         tupdesc = CreateTemplateTupleDesc(1, false);
1304                                         TupleDescInitEntry(tupdesc,
1305                                                                            (AttrNumber) 1,
1306                                                                            "column",
1307                                                                            funcrettype,
1308                                                                            -1,
1309                                                                            0);
1310                                 }
1311                                 tupstore = tuplestore_begin_heap(true, false, work_mem);
1312                                 MemoryContextSwitchTo(oldcontext);
1313                                 rsinfo.setResult = tupstore;
1314                                 rsinfo.setDesc = tupdesc;
1315                         }
1316
1317                         /*
1318                          * Store current resultset item.
1319                          */
1320                         if (returnsTuple)
1321                         {
1322                                 HeapTupleHeader td;
1323
1324                                 td = DatumGetHeapTupleHeader(result);
1325
1326                                 /*
1327                                  * tuplestore_puttuple needs a HeapTuple not a bare
1328                                  * HeapTupleHeader, but it doesn't need all the fields.
1329                                  */
1330                                 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1331                                 tmptup.t_data = td;
1332                                 tuple = &tmptup;
1333                         }
1334                         else
1335                         {
1336                                 char            nullflag;
1337
1338                                 nullflag = fcinfo.isnull ? 'n' : ' ';
1339                                 tuple = heap_formtuple(tupdesc, &result, &nullflag);
1340                         }
1341
1342                         oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1343                         tuplestore_puttuple(tupstore, tuple);
1344                         MemoryContextSwitchTo(oldcontext);
1345
1346                         /*
1347                          * Are we done?
1348                          */
1349                         if (rsinfo.isDone != ExprMultipleResult)
1350                                 break;
1351                 }
1352                 else if (rsinfo.returnMode == SFRM_Materialize)
1353                 {
1354                         /* check we're on the same page as the function author */
1355                         if (!first_time || rsinfo.isDone != ExprSingleResult)
1356                                 ereport(ERROR,
1357                                                 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1358                                                  errmsg("table-function protocol for materialize mode was not followed")));
1359                         /* Done evaluating the set result */
1360                         break;
1361                 }
1362                 else
1363                         ereport(ERROR,
1364                                         (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1365                                          errmsg("unrecognized table-function returnMode: %d",
1366                                                         (int) rsinfo.returnMode)));
1367
1368                 first_time = false;
1369         }
1370
1371 no_function_result:
1372
1373         /*
1374          * If we got nothing from the function (ie, an empty-set or NULL result),
1375          * we have to create the tuplestore to return, and if it's a
1376          * non-set-returning function then insert a single all-nulls row.
1377          */
1378         if (rsinfo.setResult == NULL)
1379         {
1380                 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1381                 tupstore = tuplestore_begin_heap(true, false, work_mem);
1382                 rsinfo.setResult = tupstore;
1383                 if (!returnsSet)
1384                 {
1385                         int                     natts = expectedDesc->natts;
1386                         Datum      *nulldatums;
1387                         char       *nullflags;
1388                         HeapTuple       tuple;
1389
1390                         MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1391                         nulldatums = (Datum *) palloc0(natts * sizeof(Datum));
1392                         nullflags = (char *) palloc(natts * sizeof(char));
1393                         memset(nullflags, 'n', natts * sizeof(char));
1394                         tuple = heap_formtuple(expectedDesc, nulldatums, nullflags);
1395                         MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1396                         tuplestore_puttuple(tupstore, tuple);
1397                 }
1398         }
1399
1400         MemoryContextSwitchTo(callerContext);
1401
1402         /* The returned pointers are those in rsinfo */
1403         *returnDesc = rsinfo.setDesc;
1404         return rsinfo.setResult;
1405 }
1406
1407
1408 /* ----------------------------------------------------------------
1409  *              ExecEvalFunc
1410  *              ExecEvalOper
1411  *
1412  *              Evaluate the functional result of a list of arguments by calling the
1413  *              function manager.
1414  * ----------------------------------------------------------------
1415  */
1416
1417 /* ----------------------------------------------------------------
1418  *              ExecEvalFunc
1419  * ----------------------------------------------------------------
1420  */
1421 static Datum
1422 ExecEvalFunc(FuncExprState *fcache,
1423                          ExprContext *econtext,
1424                          bool *isNull,
1425                          ExprDoneCond *isDone)
1426 {
1427         /* This is called only the first time through */
1428         FuncExpr   *func = (FuncExpr *) fcache->xprstate.expr;
1429
1430         /* Initialize function lookup info */
1431         init_fcache(func->funcid, fcache, econtext->ecxt_per_query_memory);
1432
1433         /* Go directly to ExecMakeFunctionResult on subsequent uses */
1434         fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
1435
1436         return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
1437 }
1438
1439 /* ----------------------------------------------------------------
1440  *              ExecEvalOper
1441  * ----------------------------------------------------------------
1442  */
1443 static Datum
1444 ExecEvalOper(FuncExprState *fcache,
1445                          ExprContext *econtext,
1446                          bool *isNull,
1447                          ExprDoneCond *isDone)
1448 {
1449         /* This is called only the first time through */
1450         OpExpr     *op = (OpExpr *) fcache->xprstate.expr;
1451
1452         /* Initialize function lookup info */
1453         init_fcache(op->opfuncid, fcache, econtext->ecxt_per_query_memory);
1454
1455         /* Go directly to ExecMakeFunctionResult on subsequent uses */
1456         fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
1457
1458         return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
1459 }
1460
1461 /* ----------------------------------------------------------------
1462  *              ExecEvalDistinct
1463  *
1464  * IS DISTINCT FROM must evaluate arguments to determine whether
1465  * they are NULL; if either is NULL then the result is already
1466  * known. If neither is NULL, then proceed to evaluate the
1467  * function. Note that this is *always* derived from the equals
1468  * operator, but since we need special processing of the arguments
1469  * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
1470  * ----------------------------------------------------------------
1471  */
1472 static Datum
1473 ExecEvalDistinct(FuncExprState *fcache,
1474                                  ExprContext *econtext,
1475                                  bool *isNull,
1476                                  ExprDoneCond *isDone)
1477 {
1478         Datum           result;
1479         FunctionCallInfoData fcinfo;
1480         ExprDoneCond argDone;
1481         List       *argList;
1482
1483         /* Set default values for result flags: non-null, not a set result */
1484         *isNull = false;
1485         if (isDone)
1486                 *isDone = ExprSingleResult;
1487
1488         /*
1489          * Initialize function cache if first time through
1490          */
1491         if (fcache->func.fn_oid == InvalidOid)
1492         {
1493                 DistinctExpr *op = (DistinctExpr *) fcache->xprstate.expr;
1494
1495                 init_fcache(op->opfuncid, fcache, econtext->ecxt_per_query_memory);
1496                 Assert(!fcache->func.fn_retset);
1497         }
1498
1499         /*
1500          * extract info from fcache
1501          */
1502         argList = fcache->args;
1503
1504         /* Need to prep callinfo structure */
1505         MemSet(&fcinfo, 0, sizeof(fcinfo));
1506         fcinfo.flinfo = &(fcache->func);
1507         argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
1508         if (argDone != ExprSingleResult)
1509                 ereport(ERROR,
1510                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1511                          errmsg("IS DISTINCT FROM does not support set arguments")));
1512         Assert(fcinfo.nargs == 2);
1513
1514         if (fcinfo.argnull[0] && fcinfo.argnull[1])
1515         {
1516                 /* Both NULL? Then is not distinct... */
1517                 result = BoolGetDatum(FALSE);
1518         }
1519         else if (fcinfo.argnull[0] || fcinfo.argnull[1])
1520         {
1521                 /* Only one is NULL? Then is distinct... */
1522                 result = BoolGetDatum(TRUE);
1523         }
1524         else
1525         {
1526                 fcinfo.isnull = false;
1527                 result = FunctionCallInvoke(&fcinfo);
1528                 *isNull = fcinfo.isnull;
1529                 /* Must invert result of "=" */
1530                 result = BoolGetDatum(!DatumGetBool(result));
1531         }
1532
1533         return result;
1534 }
1535
1536 /*
1537  * ExecEvalScalarArrayOp
1538  *
1539  * Evaluate "scalar op ANY/ALL (array)".  The operator always yields boolean,
1540  * and we combine the results across all array elements using OR and AND
1541  * (for ANY and ALL respectively).      Of course we short-circuit as soon as
1542  * the result is known.
1543  */
1544 static Datum
1545 ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
1546                                           ExprContext *econtext,
1547                                           bool *isNull, ExprDoneCond *isDone)
1548 {
1549         ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) sstate->fxprstate.xprstate.expr;
1550         bool            useOr = opexpr->useOr;
1551         ArrayType  *arr;
1552         int                     nitems;
1553         Datum           result;
1554         bool            resultnull;
1555         FunctionCallInfoData fcinfo;
1556         ExprDoneCond argDone;
1557         int                     i;
1558         int16           typlen;
1559         bool            typbyval;
1560         char            typalign;
1561         char       *s;
1562
1563         /* Set default values for result flags: non-null, not a set result */
1564         *isNull = false;
1565         if (isDone)
1566                 *isDone = ExprSingleResult;
1567
1568         /*
1569          * Initialize function cache if first time through
1570          */
1571         if (sstate->fxprstate.func.fn_oid == InvalidOid)
1572         {
1573                 init_fcache(opexpr->opfuncid, &sstate->fxprstate,
1574                                         econtext->ecxt_per_query_memory);
1575                 Assert(!sstate->fxprstate.func.fn_retset);
1576         }
1577
1578         /* Need to prep callinfo structure */
1579         MemSet(&fcinfo, 0, sizeof(fcinfo));
1580         fcinfo.flinfo = &(sstate->fxprstate.func);
1581         argDone = ExecEvalFuncArgs(&fcinfo, sstate->fxprstate.args, econtext);
1582         if (argDone != ExprSingleResult)
1583                 ereport(ERROR,
1584                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1585                    errmsg("op ANY/ALL (array) does not support set arguments")));
1586         Assert(fcinfo.nargs == 2);
1587
1588         /*
1589          * If the array is NULL then we return NULL --- it's not very
1590          * meaningful to do anything else, even if the operator isn't strict.
1591          */
1592         if (fcinfo.argnull[1])
1593         {
1594                 *isNull = true;
1595                 return (Datum) 0;
1596         }
1597         /* Else okay to fetch and detoast the array */
1598         arr = DatumGetArrayTypeP(fcinfo.arg[1]);
1599
1600         /*
1601          * If the array is empty, we return either FALSE or TRUE per the useOr
1602          * flag.  This is correct even if the scalar is NULL; since we would
1603          * evaluate the operator zero times, it matters not whether it would
1604          * want to return NULL.
1605          */
1606         nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
1607         if (nitems <= 0)
1608                 return BoolGetDatum(!useOr);
1609
1610         /*
1611          * If the scalar is NULL, and the function is strict, return NULL.
1612          * This is just to avoid having to test for strictness inside the
1613          * loop.  (XXX but if arrays could have null elements, we'd need a
1614          * test anyway.)
1615          */
1616         if (fcinfo.argnull[0] && sstate->fxprstate.func.fn_strict)
1617         {
1618                 *isNull = true;
1619                 return (Datum) 0;
1620         }
1621
1622         /*
1623          * We arrange to look up info about the element type only once per
1624          * series of calls, assuming the element type doesn't change
1625          * underneath us.
1626          */
1627         if (sstate->element_type != ARR_ELEMTYPE(arr))
1628         {
1629                 get_typlenbyvalalign(ARR_ELEMTYPE(arr),
1630                                                          &sstate->typlen,
1631                                                          &sstate->typbyval,
1632                                                          &sstate->typalign);
1633                 sstate->element_type = ARR_ELEMTYPE(arr);
1634         }
1635         typlen = sstate->typlen;
1636         typbyval = sstate->typbyval;
1637         typalign = sstate->typalign;
1638
1639         result = BoolGetDatum(!useOr);
1640         resultnull = false;
1641
1642         /* Loop over the array elements */
1643         s = (char *) ARR_DATA_PTR(arr);
1644         for (i = 0; i < nitems; i++)
1645         {
1646                 Datum           elt;
1647                 Datum           thisresult;
1648
1649                 /* Get array element */
1650                 elt = fetch_att(s, typbyval, typlen);
1651
1652                 s = att_addlength(s, typlen, PointerGetDatum(s));
1653                 s = (char *) att_align(s, typalign);
1654
1655                 /* Call comparison function */
1656                 fcinfo.arg[1] = elt;
1657                 fcinfo.argnull[1] = false;
1658                 fcinfo.isnull = false;
1659                 thisresult = FunctionCallInvoke(&fcinfo);
1660
1661                 /* Combine results per OR or AND semantics */
1662                 if (fcinfo.isnull)
1663                         resultnull = true;
1664                 else if (useOr)
1665                 {
1666                         if (DatumGetBool(thisresult))
1667                         {
1668                                 result = BoolGetDatum(true);
1669                                 resultnull = false;
1670                                 break;                  /* needn't look at any more elements */
1671                         }
1672                 }
1673                 else
1674                 {
1675                         if (!DatumGetBool(thisresult))
1676                         {
1677                                 result = BoolGetDatum(false);
1678                                 resultnull = false;
1679                                 break;                  /* needn't look at any more elements */
1680                         }
1681                 }
1682         }
1683
1684         *isNull = resultnull;
1685         return result;
1686 }
1687
1688 /* ----------------------------------------------------------------
1689  *              ExecEvalNot
1690  *              ExecEvalOr
1691  *              ExecEvalAnd
1692  *
1693  *              Evaluate boolean expressions, with appropriate short-circuiting.
1694  *
1695  *              The query planner reformulates clause expressions in the
1696  *              qualification to conjunctive normal form.  If we ever get
1697  *              an AND to evaluate, we can be sure that it's not a top-level
1698  *              clause in the qualification, but appears lower (as a function
1699  *              argument, for example), or in the target list.  Not that you
1700  *              need to know this, mind you...
1701  * ----------------------------------------------------------------
1702  */
1703 static Datum
1704 ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
1705                         bool *isNull, ExprDoneCond *isDone)
1706 {
1707         ExprState  *clause = linitial(notclause->args);
1708         Datum           expr_value;
1709
1710         if (isDone)
1711                 *isDone = ExprSingleResult;
1712
1713         expr_value = ExecEvalExpr(clause, econtext, isNull, NULL);
1714
1715         /*
1716          * if the expression evaluates to null, then we just cascade the null
1717          * back to whoever called us.
1718          */
1719         if (*isNull)
1720                 return expr_value;
1721
1722         /*
1723          * evaluation of 'not' is simple.. expr is false, then return 'true'
1724          * and vice versa.
1725          */
1726         return BoolGetDatum(!DatumGetBool(expr_value));
1727 }
1728
1729 /* ----------------------------------------------------------------
1730  *              ExecEvalOr
1731  * ----------------------------------------------------------------
1732  */
1733 static Datum
1734 ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
1735                    bool *isNull, ExprDoneCond *isDone)
1736 {
1737         List       *clauses = orExpr->args;
1738         ListCell   *clause;
1739         bool            AnyNull;
1740
1741         if (isDone)
1742                 *isDone = ExprSingleResult;
1743
1744         AnyNull = false;
1745
1746         /*
1747          * If any of the clauses is TRUE, the OR result is TRUE regardless of
1748          * the states of the rest of the clauses, so we can stop evaluating
1749          * and return TRUE immediately.  If none are TRUE and one or more is
1750          * NULL, we return NULL; otherwise we return FALSE.  This makes sense
1751          * when you interpret NULL as "don't know": if we have a TRUE then the
1752          * OR is TRUE even if we aren't sure about some of the other inputs.
1753          * If all the known inputs are FALSE, but we have one or more "don't
1754          * knows", then we have to report that we "don't know" what the OR's
1755          * result should be --- perhaps one of the "don't knows" would have
1756          * been TRUE if we'd known its value.  Only when all the inputs are
1757          * known to be FALSE can we state confidently that the OR's result is
1758          * FALSE.
1759          */
1760         foreach(clause, clauses)
1761         {
1762                 ExprState  *clausestate = (ExprState *) lfirst(clause);
1763                 Datum           clause_value;
1764
1765                 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
1766
1767                 /*
1768                  * if we have a non-null true result, then return it.
1769                  */
1770                 if (*isNull)
1771                         AnyNull = true;         /* remember we got a null */
1772                 else if (DatumGetBool(clause_value))
1773                         return clause_value;
1774         }
1775
1776         /* AnyNull is true if at least one clause evaluated to NULL */
1777         *isNull = AnyNull;
1778         return BoolGetDatum(false);
1779 }
1780
1781 /* ----------------------------------------------------------------
1782  *              ExecEvalAnd
1783  * ----------------------------------------------------------------
1784  */
1785 static Datum
1786 ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
1787                         bool *isNull, ExprDoneCond *isDone)
1788 {
1789         List       *clauses = andExpr->args;
1790         ListCell   *clause;
1791         bool            AnyNull;
1792
1793         if (isDone)
1794                 *isDone = ExprSingleResult;
1795
1796         AnyNull = false;
1797
1798         /*
1799          * If any of the clauses is FALSE, the AND result is FALSE regardless
1800          * of the states of the rest of the clauses, so we can stop evaluating
1801          * and return FALSE immediately.  If none are FALSE and one or more is
1802          * NULL, we return NULL; otherwise we return TRUE.      This makes sense
1803          * when you interpret NULL as "don't know", using the same sort of
1804          * reasoning as for OR, above.
1805          */
1806
1807         foreach(clause, clauses)
1808         {
1809                 ExprState  *clausestate = (ExprState *) lfirst(clause);
1810                 Datum           clause_value;
1811
1812                 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
1813
1814                 /*
1815                  * if we have a non-null false result, then return it.
1816                  */
1817                 if (*isNull)
1818                         AnyNull = true;         /* remember we got a null */
1819                 else if (!DatumGetBool(clause_value))
1820                         return clause_value;
1821         }
1822
1823         /* AnyNull is true if at least one clause evaluated to NULL */
1824         *isNull = AnyNull;
1825         return BoolGetDatum(!AnyNull);
1826 }
1827
1828 /* ----------------------------------------------------------------
1829  *              ExecEvalConvertRowtype
1830  *
1831  *              Evaluate a rowtype coercion operation.  This may require
1832  *              rearranging field positions.
1833  * ----------------------------------------------------------------
1834  */
1835 static Datum
1836 ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
1837                                            ExprContext *econtext,
1838                                            bool *isNull, ExprDoneCond *isDone)
1839 {
1840         HeapTuple       result;
1841         Datum           tupDatum;
1842         HeapTupleHeader tuple;
1843         HeapTupleData tmptup;
1844         AttrNumber *attrMap = cstate->attrMap;
1845         Datum      *invalues = cstate->invalues;
1846         char       *innulls = cstate->innulls;
1847         Datum      *outvalues = cstate->outvalues;
1848         char       *outnulls = cstate->outnulls;
1849         int                     i;
1850         int                     outnatts = cstate->outdesc->natts;
1851
1852         tupDatum = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
1853
1854         /* this test covers the isDone exception too: */
1855         if (*isNull)
1856                 return tupDatum;
1857
1858         tuple = DatumGetHeapTupleHeader(tupDatum);
1859
1860         Assert(HeapTupleHeaderGetTypeId(tuple) == cstate->indesc->tdtypeid);
1861         Assert(HeapTupleHeaderGetTypMod(tuple) == cstate->indesc->tdtypmod);
1862
1863         /*
1864          * heap_deformtuple needs a HeapTuple not a bare HeapTupleHeader.
1865          */
1866         tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
1867         tmptup.t_data = tuple;
1868
1869         /*
1870          * Extract all the values of the old tuple, offsetting the arrays
1871          * so that invalues[0] is NULL and invalues[1] is the first
1872          * source attribute; this exactly matches the numbering convention
1873          * in attrMap.
1874          */
1875         heap_deformtuple(&tmptup, cstate->indesc, invalues + 1, innulls + 1);
1876         invalues[0] = (Datum) 0;
1877         innulls[0] = 'n';
1878
1879         /*
1880          * Transpose into proper fields of the new tuple.
1881          */
1882         for (i = 0; i < outnatts; i++)
1883         {
1884                 int                     j = attrMap[i];
1885
1886                 outvalues[i] = invalues[j];
1887                 outnulls[i] = innulls[j];
1888         }
1889
1890         /*
1891          * Now form the new tuple.
1892          */
1893         result = heap_formtuple(cstate->outdesc, outvalues, outnulls);
1894
1895         return HeapTupleGetDatum(result);
1896 }
1897
1898 /* ----------------------------------------------------------------
1899  *              ExecEvalCase
1900  *
1901  *              Evaluate a CASE clause. Will have boolean expressions
1902  *              inside the WHEN clauses, and will have expressions
1903  *              for results.
1904  *              - thomas 1998-11-09
1905  * ----------------------------------------------------------------
1906  */
1907 static Datum
1908 ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
1909                          bool *isNull, ExprDoneCond *isDone)
1910 {
1911         List       *clauses = caseExpr->args;
1912         ListCell   *clause;
1913         Datum           save_datum;
1914         bool            save_isNull;
1915
1916         if (isDone)
1917                 *isDone = ExprSingleResult;
1918
1919         /*
1920          * If there's a test expression, we have to evaluate it and save the
1921          * value where the CaseTestExpr placeholders can find it. We must save
1922          * and restore prior setting of econtext's caseValue fields, in case
1923          * this node is itself within a larger CASE.
1924          */
1925         save_datum = econtext->caseValue_datum;
1926         save_isNull = econtext->caseValue_isNull;
1927
1928         if (caseExpr->arg)
1929         {
1930                 econtext->caseValue_datum = ExecEvalExpr(caseExpr->arg,
1931                                                                                                  econtext,
1932                                                                                          &econtext->caseValue_isNull,
1933                                                                                                  NULL);
1934         }
1935
1936         /*
1937          * we evaluate each of the WHEN clauses in turn, as soon as one is
1938          * true we return the corresponding result. If none are true then we
1939          * return the value of the default clause, or NULL if there is none.
1940          */
1941         foreach(clause, clauses)
1942         {
1943                 CaseWhenState *wclause = lfirst(clause);
1944                 Datum           clause_value;
1945
1946                 clause_value = ExecEvalExpr(wclause->expr,
1947                                                                         econtext,
1948                                                                         isNull,
1949                                                                         NULL);
1950
1951                 /*
1952                  * if we have a true test, then we return the result, since the
1953                  * case statement is satisfied.  A NULL result from the test is
1954                  * not considered true.
1955                  */
1956                 if (DatumGetBool(clause_value) && !*isNull)
1957                 {
1958                         econtext->caseValue_datum = save_datum;
1959                         econtext->caseValue_isNull = save_isNull;
1960                         return ExecEvalExpr(wclause->result,
1961                                                                 econtext,
1962                                                                 isNull,
1963                                                                 isDone);
1964                 }
1965         }
1966
1967         econtext->caseValue_datum = save_datum;
1968         econtext->caseValue_isNull = save_isNull;
1969
1970         if (caseExpr->defresult)
1971         {
1972                 return ExecEvalExpr(caseExpr->defresult,
1973                                                         econtext,
1974                                                         isNull,
1975                                                         isDone);
1976         }
1977
1978         *isNull = true;
1979         return (Datum) 0;
1980 }
1981
1982 /*
1983  * ExecEvalCaseTestExpr
1984  *
1985  * Return the value stored by CASE.
1986  */
1987 static Datum
1988 ExecEvalCaseTestExpr(ExprState *exprstate,
1989                                          ExprContext *econtext,
1990                                          bool *isNull, ExprDoneCond *isDone)
1991 {
1992         if (isDone)
1993                 *isDone = ExprSingleResult;
1994         *isNull = econtext->caseValue_isNull;
1995         return econtext->caseValue_datum;
1996 }
1997
1998 /* ----------------------------------------------------------------
1999  *              ExecEvalArray - ARRAY[] expressions
2000  *
2001  * NOTE: currently, if any input value is NULL then we return a NULL array,
2002  * so the ARRAY[] construct can be considered strict.  Eventually this will
2003  * change; when it does, be sure to fix contain_nonstrict_functions().
2004  * ----------------------------------------------------------------
2005  */
2006 static Datum
2007 ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
2008                           bool *isNull, ExprDoneCond *isDone)
2009 {
2010         ArrayExpr  *arrayExpr = (ArrayExpr *) astate->xprstate.expr;
2011         ArrayType  *result;
2012         ListCell   *element;
2013         Oid                     element_type = arrayExpr->element_typeid;
2014         int                     ndims = 0;
2015         int                     dims[MAXDIM];
2016         int                     lbs[MAXDIM];
2017
2018         /* Set default values for result flags: non-null, not a set result */
2019         *isNull = false;
2020         if (isDone)
2021                 *isDone = ExprSingleResult;
2022
2023         if (!arrayExpr->multidims)
2024         {
2025                 /* Elements are presumably of scalar type */
2026                 int                     nelems;
2027                 Datum      *dvalues;
2028                 int                     i = 0;
2029
2030                 ndims = 1;
2031                 nelems = list_length(astate->elements);
2032
2033                 /* Shouldn't happen here, but if length is 0, return NULL */
2034                 if (nelems == 0)
2035                 {
2036                         *isNull = true;
2037                         return (Datum) 0;
2038                 }
2039
2040                 dvalues = (Datum *) palloc(nelems * sizeof(Datum));
2041
2042                 /* loop through and build array of datums */
2043                 foreach(element, astate->elements)
2044                 {
2045                         ExprState  *e = (ExprState *) lfirst(element);
2046                         bool            eisnull;
2047
2048                         dvalues[i++] = ExecEvalExpr(e, econtext, &eisnull, NULL);
2049                         if (eisnull)
2050                         {
2051                                 *isNull = true;
2052                                 return (Datum) 0;
2053                         }
2054                 }
2055
2056                 /* setup for 1-D array of the given length */
2057                 dims[0] = nelems;
2058                 lbs[0] = 1;
2059
2060                 result = construct_md_array(dvalues, ndims, dims, lbs,
2061                                                                         element_type,
2062                                                                         astate->elemlength,
2063                                                                         astate->elembyval,
2064                                                                         astate->elemalign);
2065         }
2066         else
2067         {
2068                 /* Must be nested array expressions */
2069                 char       *dat = NULL;
2070                 Size            ndatabytes = 0;
2071                 int                     nbytes;
2072                 int                     outer_nelems = list_length(astate->elements);
2073                 int                     elem_ndims = 0;
2074                 int                *elem_dims = NULL;
2075                 int                *elem_lbs = NULL;
2076                 bool            firstone = true;
2077                 int                     i;
2078
2079                 /* loop through and get data area from each element */
2080                 foreach(element, astate->elements)
2081                 {
2082                         ExprState  *e = (ExprState *) lfirst(element);
2083                         bool            eisnull;
2084                         Datum           arraydatum;
2085                         ArrayType  *array;
2086                         int                     elem_ndatabytes;
2087
2088                         arraydatum = ExecEvalExpr(e, econtext, &eisnull, NULL);
2089                         if (eisnull)
2090                         {
2091                                 *isNull = true;
2092                                 return (Datum) 0;
2093                         }
2094
2095                         array = DatumGetArrayTypeP(arraydatum);
2096
2097                         /* run-time double-check on element type */
2098                         if (element_type != ARR_ELEMTYPE(array))
2099                                 ereport(ERROR,
2100                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
2101                                                  errmsg("cannot merge incompatible arrays"),
2102                                                  errdetail("Array with element type %s cannot be "
2103                                          "included in ARRAY construct with element type %s.",
2104                                                                    format_type_be(ARR_ELEMTYPE(array)),
2105                                                                    format_type_be(element_type))));
2106
2107                         if (firstone)
2108                         {
2109                                 /* Get sub-array details from first member */
2110                                 elem_ndims = ARR_NDIM(array);
2111                                 ndims = elem_ndims + 1;
2112                                 if (ndims <= 0 || ndims > MAXDIM)
2113                                         ereport(ERROR,
2114                                                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2115                                           errmsg("number of array dimensions (%d) exceeds " \
2116                                                          "the maximum allowed (%d)", ndims, MAXDIM)));
2117
2118                                 elem_dims = (int *) palloc(elem_ndims * sizeof(int));
2119                                 memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
2120                                 elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
2121                                 memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
2122
2123                                 firstone = false;
2124                         }
2125                         else
2126                         {
2127                                 /* Check other sub-arrays are compatible */
2128                                 if (elem_ndims != ARR_NDIM(array) ||
2129                                         memcmp(elem_dims, ARR_DIMS(array),
2130                                                    elem_ndims * sizeof(int)) != 0 ||
2131                                         memcmp(elem_lbs, ARR_LBOUND(array),
2132                                                    elem_ndims * sizeof(int)) != 0)
2133                                         ereport(ERROR,
2134                                                         (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2135                                                 errmsg("multidimensional arrays must have array "
2136                                                            "expressions with matching dimensions")));
2137                         }
2138
2139                         elem_ndatabytes = ARR_SIZE(array) - ARR_OVERHEAD(elem_ndims);
2140                         ndatabytes += elem_ndatabytes;
2141                         if (dat == NULL)
2142                                 dat = (char *) palloc(ndatabytes);
2143                         else
2144                                 dat = (char *) repalloc(dat, ndatabytes);
2145
2146                         memcpy(dat + (ndatabytes - elem_ndatabytes),
2147                                    ARR_DATA_PTR(array),
2148                                    elem_ndatabytes);
2149                 }
2150
2151                 /* setup for multi-D array */
2152                 dims[0] = outer_nelems;
2153                 lbs[0] = 1;
2154                 for (i = 1; i < ndims; i++)
2155                 {
2156                         dims[i] = elem_dims[i - 1];
2157                         lbs[i] = elem_lbs[i - 1];
2158                 }
2159
2160                 nbytes = ndatabytes + ARR_OVERHEAD(ndims);
2161                 result = (ArrayType *) palloc(nbytes);
2162
2163                 result->size = nbytes;
2164                 result->ndim = ndims;
2165                 result->flags = 0;
2166                 result->elemtype = element_type;
2167                 memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
2168                 memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
2169                 if (ndatabytes > 0)
2170                         memcpy(ARR_DATA_PTR(result), dat, ndatabytes);
2171
2172                 if (dat != NULL)
2173                         pfree(dat);
2174         }
2175
2176         return PointerGetDatum(result);
2177 }
2178
2179 /* ----------------------------------------------------------------
2180  *              ExecEvalRow - ROW() expressions
2181  * ----------------------------------------------------------------
2182  */
2183 static Datum
2184 ExecEvalRow(RowExprState *rstate,
2185                         ExprContext *econtext,
2186                         bool *isNull, ExprDoneCond *isDone)
2187 {
2188         HeapTuple       tuple;
2189         Datum      *values;
2190         char       *nulls;
2191         int                     natts;
2192         ListCell   *arg;
2193         int                     i;
2194
2195         /* Set default values for result flags: non-null, not a set result */
2196         *isNull = false;
2197         if (isDone)
2198                 *isDone = ExprSingleResult;
2199
2200         /* Allocate workspace */
2201         natts = rstate->tupdesc->natts;
2202         values = (Datum *) palloc0(natts * sizeof(Datum));
2203         nulls = (char *) palloc(natts * sizeof(char));
2204
2205         /* preset to nulls in case rowtype has some later-added columns */
2206         memset(nulls, 'n', natts * sizeof(char));
2207
2208         /* Evaluate field values */
2209         i = 0;
2210         foreach(arg, rstate->args)
2211         {
2212                 ExprState  *e = (ExprState *) lfirst(arg);
2213                 bool            eisnull;
2214
2215                 values[i] = ExecEvalExpr(e, econtext, &eisnull, NULL);
2216                 nulls[i] = eisnull ? 'n' : ' ';
2217                 i++;
2218         }
2219
2220         tuple = heap_formtuple(rstate->tupdesc, values, nulls);
2221
2222         pfree(values);
2223         pfree(nulls);
2224
2225         return HeapTupleGetDatum(tuple);
2226 }
2227
2228 /* ----------------------------------------------------------------
2229  *              ExecEvalCoalesce
2230  * ----------------------------------------------------------------
2231  */
2232 static Datum
2233 ExecEvalCoalesce(CoalesceExprState *coalesceExpr, ExprContext *econtext,
2234                                  bool *isNull, ExprDoneCond *isDone)
2235 {
2236         ListCell   *arg;
2237
2238         if (isDone)
2239                 *isDone = ExprSingleResult;
2240
2241         /* Simply loop through until something NOT NULL is found */
2242         foreach(arg, coalesceExpr->args)
2243         {
2244                 ExprState  *e = (ExprState *) lfirst(arg);
2245                 Datum           value;
2246
2247                 value = ExecEvalExpr(e, econtext, isNull, NULL);
2248                 if (!*isNull)
2249                         return value;
2250         }
2251
2252         /* Else return NULL */
2253         *isNull = true;
2254         return (Datum) 0;
2255 }
2256
2257 /* ----------------------------------------------------------------
2258  *              ExecEvalNullIf
2259  *
2260  * Note that this is *always* derived from the equals operator,
2261  * but since we need special processing of the arguments
2262  * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
2263  * ----------------------------------------------------------------
2264  */
2265 static Datum
2266 ExecEvalNullIf(FuncExprState *nullIfExpr,
2267                            ExprContext *econtext,
2268                            bool *isNull, ExprDoneCond *isDone)
2269 {
2270         Datum           result;
2271         FunctionCallInfoData fcinfo;
2272         ExprDoneCond argDone;
2273         List       *argList;
2274
2275         if (isDone)
2276                 *isDone = ExprSingleResult;
2277
2278         /*
2279          * Initialize function cache if first time through
2280          */
2281         if (nullIfExpr->func.fn_oid == InvalidOid)
2282         {
2283                 NullIfExpr *op = (NullIfExpr *) nullIfExpr->xprstate.expr;
2284
2285                 init_fcache(op->opfuncid, nullIfExpr, econtext->ecxt_per_query_memory);
2286                 Assert(!nullIfExpr->func.fn_retset);
2287         }
2288
2289         /*
2290          * extract info from nullIfExpr
2291          */
2292         argList = nullIfExpr->args;
2293
2294         /* Need to prep callinfo structure */
2295         MemSet(&fcinfo, 0, sizeof(fcinfo));
2296         fcinfo.flinfo = &(nullIfExpr->func);
2297         argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
2298         if (argDone != ExprSingleResult)
2299                 ereport(ERROR,
2300                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
2301                                  errmsg("NULLIF does not support set arguments")));
2302         Assert(fcinfo.nargs == 2);
2303
2304         /* if either argument is NULL they can't be equal */
2305         if (!fcinfo.argnull[0] && !fcinfo.argnull[1])
2306         {
2307                 fcinfo.isnull = false;
2308                 result = FunctionCallInvoke(&fcinfo);
2309                 /* if the arguments are equal return null */
2310                 if (!fcinfo.isnull && DatumGetBool(result))
2311                 {
2312                         *isNull = true;
2313                         return (Datum) 0;
2314                 }
2315         }
2316
2317         /* else return first argument */
2318         *isNull = fcinfo.argnull[0];
2319         return fcinfo.arg[0];
2320 }
2321
2322 /* ----------------------------------------------------------------
2323  *              ExecEvalNullTest
2324  *
2325  *              Evaluate a NullTest node.
2326  * ----------------------------------------------------------------
2327  */
2328 static Datum
2329 ExecEvalNullTest(GenericExprState *nstate,
2330                                  ExprContext *econtext,
2331                                  bool *isNull,
2332                                  ExprDoneCond *isDone)
2333 {
2334         NullTest   *ntest = (NullTest *) nstate->xprstate.expr;
2335         Datum           result;
2336
2337         result = ExecEvalExpr(nstate->arg, econtext, isNull, isDone);
2338
2339         if (isDone && *isDone == ExprEndResult)
2340                 return result;                  /* nothing to check */
2341
2342         switch (ntest->nulltesttype)
2343         {
2344                 case IS_NULL:
2345                         if (*isNull)
2346                         {
2347                                 *isNull = false;
2348                                 return BoolGetDatum(true);
2349                         }
2350                         else
2351                                 return BoolGetDatum(false);
2352                 case IS_NOT_NULL:
2353                         if (*isNull)
2354                         {
2355                                 *isNull = false;
2356                                 return BoolGetDatum(false);
2357                         }
2358                         else
2359                                 return BoolGetDatum(true);
2360                 default:
2361                         elog(ERROR, "unrecognized nulltesttype: %d",
2362                                  (int) ntest->nulltesttype);
2363                         return (Datum) 0;       /* keep compiler quiet */
2364         }
2365 }
2366
2367 /* ----------------------------------------------------------------
2368  *              ExecEvalBooleanTest
2369  *
2370  *              Evaluate a BooleanTest node.
2371  * ----------------------------------------------------------------
2372  */
2373 static Datum
2374 ExecEvalBooleanTest(GenericExprState *bstate,
2375                                         ExprContext *econtext,
2376                                         bool *isNull,
2377                                         ExprDoneCond *isDone)
2378 {
2379         BooleanTest *btest = (BooleanTest *) bstate->xprstate.expr;
2380         Datum           result;
2381
2382         result = ExecEvalExpr(bstate->arg, econtext, isNull, isDone);
2383
2384         if (isDone && *isDone == ExprEndResult)
2385                 return result;                  /* nothing to check */
2386
2387         switch (btest->booltesttype)
2388         {
2389                 case IS_TRUE:
2390                         if (*isNull)
2391                         {
2392                                 *isNull = false;
2393                                 return BoolGetDatum(false);
2394                         }
2395                         else if (DatumGetBool(result))
2396                                 return BoolGetDatum(true);
2397                         else
2398                                 return BoolGetDatum(false);
2399                 case IS_NOT_TRUE:
2400                         if (*isNull)
2401                         {
2402                                 *isNull = false;
2403                                 return BoolGetDatum(true);
2404                         }
2405                         else if (DatumGetBool(result))
2406                                 return BoolGetDatum(false);
2407                         else
2408                                 return BoolGetDatum(true);
2409                 case IS_FALSE:
2410                         if (*isNull)
2411                         {
2412                                 *isNull = false;
2413                                 return BoolGetDatum(false);
2414                         }
2415                         else if (DatumGetBool(result))
2416                                 return BoolGetDatum(false);
2417                         else
2418                                 return BoolGetDatum(true);
2419                 case IS_NOT_FALSE:
2420                         if (*isNull)
2421                         {
2422                                 *isNull = false;
2423                                 return BoolGetDatum(true);
2424                         }
2425                         else if (DatumGetBool(result))
2426                                 return BoolGetDatum(true);
2427                         else
2428                                 return BoolGetDatum(false);
2429                 case IS_UNKNOWN:
2430                         if (*isNull)
2431                         {
2432                                 *isNull = false;
2433                                 return BoolGetDatum(true);
2434                         }
2435                         else
2436                                 return BoolGetDatum(false);
2437                 case IS_NOT_UNKNOWN:
2438                         if (*isNull)
2439                         {
2440                                 *isNull = false;
2441                                 return BoolGetDatum(false);
2442                         }
2443                         else
2444                                 return BoolGetDatum(true);
2445                 default:
2446                         elog(ERROR, "unrecognized booltesttype: %d",
2447                                  (int) btest->booltesttype);
2448                         return (Datum) 0;       /* keep compiler quiet */
2449         }
2450 }
2451
2452 /*
2453  * ExecEvalCoerceToDomain
2454  *
2455  * Test the provided data against the domain constraint(s).  If the data
2456  * passes the constraint specifications, pass it through (return the
2457  * datum) otherwise throw an error.
2458  */
2459 static Datum
2460 ExecEvalCoerceToDomain(CoerceToDomainState *cstate, ExprContext *econtext,
2461                                            bool *isNull, ExprDoneCond *isDone)
2462 {
2463         CoerceToDomain *ctest = (CoerceToDomain *) cstate->xprstate.expr;
2464         Datum           result;
2465         ListCell   *l;
2466
2467         result = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
2468
2469         if (isDone && *isDone == ExprEndResult)
2470                 return result;                  /* nothing to check */
2471
2472         foreach(l, cstate->constraints)
2473         {
2474                 DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
2475
2476                 switch (con->constrainttype)
2477                 {
2478                         case DOM_CONSTRAINT_NOTNULL:
2479                                 if (*isNull)
2480                                         ereport(ERROR,
2481                                                         (errcode(ERRCODE_NOT_NULL_VIOLATION),
2482                                                    errmsg("domain %s does not allow null values",
2483                                                                   format_type_be(ctest->resulttype))));
2484                                 break;
2485                         case DOM_CONSTRAINT_CHECK:
2486                                 {
2487                                         Datum           conResult;
2488                                         bool            conIsNull;
2489                                         Datum           save_datum;
2490                                         bool            save_isNull;
2491
2492                                         /*
2493                                          * Set up value to be returned by CoerceToDomainValue
2494                                          * nodes. We must save and restore prior setting of
2495                                          * econtext's domainValue fields, in case this node is
2496                                          * itself within a check expression for another
2497                                          * domain.
2498                                          */
2499                                         save_datum = econtext->domainValue_datum;
2500                                         save_isNull = econtext->domainValue_isNull;
2501
2502                                         econtext->domainValue_datum = result;
2503                                         econtext->domainValue_isNull = *isNull;
2504
2505                                         conResult = ExecEvalExpr(con->check_expr,
2506                                                                                          econtext, &conIsNull, NULL);
2507
2508                                         if (!conIsNull &&
2509                                                 !DatumGetBool(conResult))
2510                                                 ereport(ERROR,
2511                                                                 (errcode(ERRCODE_CHECK_VIOLATION),
2512                                                                  errmsg("value for domain %s violates check constraint \"%s\"",
2513                                                                                 format_type_be(ctest->resulttype),
2514                                                                                 con->name)));
2515                                         econtext->domainValue_datum = save_datum;
2516                                         econtext->domainValue_isNull = save_isNull;
2517
2518                                         break;
2519                                 }
2520                         default:
2521                                 elog(ERROR, "unrecognized constraint type: %d",
2522                                          (int) con->constrainttype);
2523                                 break;
2524                 }
2525         }
2526
2527         /* If all has gone well (constraints did not fail) return the datum */
2528         return result;
2529 }
2530
2531 /*
2532  * ExecEvalCoerceToDomainValue
2533  *
2534  * Return the value stored by CoerceToDomain.
2535  */
2536 static Datum
2537 ExecEvalCoerceToDomainValue(ExprState *exprstate,
2538                                                         ExprContext *econtext,
2539                                                         bool *isNull, ExprDoneCond *isDone)
2540 {
2541         if (isDone)
2542                 *isDone = ExprSingleResult;
2543         *isNull = econtext->domainValue_isNull;
2544         return econtext->domainValue_datum;
2545 }
2546
2547 /* ----------------------------------------------------------------
2548  *              ExecEvalFieldSelect
2549  *
2550  *              Evaluate a FieldSelect node.
2551  * ----------------------------------------------------------------
2552  */
2553 static Datum
2554 ExecEvalFieldSelect(FieldSelectState *fstate,
2555                                         ExprContext *econtext,
2556                                         bool *isNull,
2557                                         ExprDoneCond *isDone)
2558 {
2559         FieldSelect *fselect = (FieldSelect *) fstate->xprstate.expr;
2560         Datum           result;
2561         Datum           tupDatum;
2562         HeapTupleHeader tuple;
2563         Oid                     tupType;
2564         int32           tupTypmod;
2565         TupleDesc       tupDesc;
2566         HeapTupleData tmptup;
2567
2568         tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
2569
2570         /* this test covers the isDone exception too: */
2571         if (*isNull)
2572                 return tupDatum;
2573
2574         tuple = DatumGetHeapTupleHeader(tupDatum);
2575
2576         tupType = HeapTupleHeaderGetTypeId(tuple);
2577         tupTypmod = HeapTupleHeaderGetTypMod(tuple);
2578
2579         /* Lookup tupdesc if first time through or if type changes */
2580         tupDesc = fstate->argdesc;
2581         if (tupDesc == NULL ||
2582                 tupType != tupDesc->tdtypeid ||
2583                 tupTypmod != tupDesc->tdtypmod)
2584         {
2585                 MemoryContext oldcontext;
2586
2587                 tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
2588                 /* Copy the tupdesc into query storage for safety */
2589                 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
2590                 tupDesc = CreateTupleDescCopy(tupDesc);
2591                 if (fstate->argdesc)
2592                         FreeTupleDesc(fstate->argdesc);
2593                 fstate->argdesc = tupDesc;
2594                 MemoryContextSwitchTo(oldcontext);
2595         }
2596
2597         /*
2598          * heap_getattr needs a HeapTuple not a bare HeapTupleHeader.  We set
2599          * all the fields in the struct just in case user tries to inspect
2600          * system columns.
2601          */
2602         tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
2603         ItemPointerSetInvalid(&(tmptup.t_self));
2604         tmptup.t_tableOid = InvalidOid;
2605         tmptup.t_data = tuple;
2606
2607         result = heap_getattr(&tmptup,
2608                                                   fselect->fieldnum,
2609                                                   tupDesc,
2610                                                   isNull);
2611         return result;
2612 }
2613
2614 /* ----------------------------------------------------------------
2615  *              ExecEvalFieldStore
2616  *
2617  *              Evaluate a FieldStore node.
2618  * ----------------------------------------------------------------
2619  */
2620 static Datum
2621 ExecEvalFieldStore(FieldStoreState *fstate,
2622                                    ExprContext *econtext,
2623                                    bool *isNull,
2624                                    ExprDoneCond *isDone)
2625 {
2626         FieldStore *fstore = (FieldStore *) fstate->xprstate.expr;
2627         HeapTuple       tuple;
2628         Datum           tupDatum;
2629         TupleDesc       tupDesc;
2630         Datum      *values;
2631         char       *nulls;
2632         Datum           save_datum;
2633         bool            save_isNull;
2634         ListCell   *l1,
2635                            *l2;
2636
2637         tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
2638
2639         if (isDone && *isDone == ExprEndResult)
2640                 return tupDatum;
2641
2642         /* Lookup tupdesc if first time through or if type changes */
2643         tupDesc = fstate->argdesc;
2644         if (tupDesc == NULL ||
2645                 fstore->resulttype != tupDesc->tdtypeid)
2646         {
2647                 MemoryContext oldcontext;
2648
2649                 tupDesc = lookup_rowtype_tupdesc(fstore->resulttype, -1);
2650                 /* Copy the tupdesc into query storage for safety */
2651                 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
2652                 tupDesc = CreateTupleDescCopy(tupDesc);
2653                 if (fstate->argdesc)
2654                         FreeTupleDesc(fstate->argdesc);
2655                 fstate->argdesc = tupDesc;
2656                 MemoryContextSwitchTo(oldcontext);
2657         }
2658
2659         /* Allocate workspace */
2660         values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
2661         nulls = (char *) palloc(tupDesc->natts * sizeof(char));
2662
2663         if (!*isNull)
2664         {
2665                 /*
2666                  * heap_deformtuple needs a HeapTuple not a bare HeapTupleHeader.
2667                  * We set all the fields in the struct just in case.
2668                  */
2669                 HeapTupleHeader tuphdr;
2670                 HeapTupleData tmptup;
2671
2672                 tuphdr = DatumGetHeapTupleHeader(tupDatum);
2673                 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
2674                 ItemPointerSetInvalid(&(tmptup.t_self));
2675                 tmptup.t_tableOid = InvalidOid;
2676                 tmptup.t_data = tuphdr;
2677
2678                 heap_deformtuple(&tmptup, tupDesc, values, nulls);
2679         }
2680         else
2681         {
2682                 /* Convert null input tuple into an all-nulls row */
2683                 memset(nulls, 'n', tupDesc->natts * sizeof(char));
2684         }
2685
2686         /* Result is never null */
2687         *isNull = false;
2688
2689         save_datum = econtext->caseValue_datum;
2690         save_isNull = econtext->caseValue_isNull;
2691
2692         forboth(l1, fstate->newvals, l2, fstore->fieldnums)
2693         {
2694                 ExprState  *newval = (ExprState *) lfirst(l1);
2695                 AttrNumber      fieldnum = lfirst_int(l2);
2696                 bool            eisnull;
2697
2698                 Assert(fieldnum > 0 && fieldnum <= tupDesc->natts);
2699
2700                 /*
2701                  * Use the CaseTestExpr mechanism to pass down the old value of
2702                  * the field being replaced; this is useful in case we have a
2703                  * nested field update situation.  It's safe to reuse the CASE
2704                  * mechanism because there cannot be a CASE between here and where
2705                  * the value would be needed.
2706                  */
2707                 econtext->caseValue_datum = values[fieldnum - 1];
2708                 econtext->caseValue_isNull = (nulls[fieldnum - 1] == 'n');
2709
2710                 values[fieldnum - 1] = ExecEvalExpr(newval,
2711                                                                                         econtext,
2712                                                                                         &eisnull,
2713                                                                                         NULL);
2714                 nulls[fieldnum - 1] = eisnull ? 'n' : ' ';
2715         }
2716
2717         econtext->caseValue_datum = save_datum;
2718         econtext->caseValue_isNull = save_isNull;
2719
2720         tuple = heap_formtuple(tupDesc, values, nulls);
2721
2722         pfree(values);
2723         pfree(nulls);
2724
2725         return HeapTupleGetDatum(tuple);
2726 }
2727
2728 /* ----------------------------------------------------------------
2729  *              ExecEvalRelabelType
2730  *
2731  *              Evaluate a RelabelType node.
2732  * ----------------------------------------------------------------
2733  */
2734 static Datum
2735 ExecEvalRelabelType(GenericExprState *exprstate,
2736                                         ExprContext *econtext,
2737                                         bool *isNull, ExprDoneCond *isDone)
2738 {
2739         return ExecEvalExpr(exprstate->arg, econtext, isNull, isDone);
2740 }
2741
2742
2743 /*
2744  * ExecEvalExprSwitchContext
2745  *
2746  * Same as ExecEvalExpr, but get into the right allocation context explicitly.
2747  */
2748 Datum
2749 ExecEvalExprSwitchContext(ExprState *expression,
2750                                                   ExprContext *econtext,
2751                                                   bool *isNull,
2752                                                   ExprDoneCond *isDone)
2753 {
2754         Datum           retDatum;
2755         MemoryContext oldContext;
2756
2757         oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
2758         retDatum = ExecEvalExpr(expression, econtext, isNull, isDone);
2759         MemoryContextSwitchTo(oldContext);
2760         return retDatum;
2761 }
2762
2763
2764 /*
2765  * ExecInitExpr: prepare an expression tree for execution
2766  *
2767  * This function builds and returns an ExprState tree paralleling the given
2768  * Expr node tree.      The ExprState tree can then be handed to ExecEvalExpr
2769  * for execution.  Because the Expr tree itself is read-only as far as
2770  * ExecInitExpr and ExecEvalExpr are concerned, several different executions
2771  * of the same plan tree can occur concurrently.
2772  *
2773  * This must be called in a memory context that will last as long as repeated
2774  * executions of the expression are needed.  Typically the context will be
2775  * the same as the per-query context of the associated ExprContext.
2776  *
2777  * Any Aggref and SubPlan nodes found in the tree are added to the lists
2778  * of such nodes held by the parent PlanState.  Otherwise, we do very little
2779  * initialization here other than building the state-node tree.  Any nontrivial
2780  * work associated with initializing runtime info for a node should happen
2781  * during the first actual evaluation of that node.  (This policy lets us
2782  * avoid work if the node is never actually evaluated.)
2783  *
2784  * Note: there is no ExecEndExpr function; we assume that any resource
2785  * cleanup needed will be handled by just releasing the memory context
2786  * in which the state tree is built.  Functions that require additional
2787  * cleanup work can register a shutdown callback in the ExprContext.
2788  *
2789  *      'node' is the root of the expression tree to examine
2790  *      'parent' is the PlanState node that owns the expression.
2791  *
2792  * 'parent' may be NULL if we are preparing an expression that is not
2793  * associated with a plan tree.  (If so, it can't have aggs or subplans.)
2794  * This case should usually come through ExecPrepareExpr, not directly here.
2795  */
2796 ExprState *
2797 ExecInitExpr(Expr *node, PlanState *parent)
2798 {
2799         ExprState  *state;
2800
2801         if (node == NULL)
2802                 return NULL;
2803
2804         /* Guard against stack overflow due to overly complex expressions */
2805         check_stack_depth();
2806
2807         switch (nodeTag(node))
2808         {
2809                 case T_Var:
2810                         state = (ExprState *) makeNode(ExprState);
2811                         state->evalfunc = ExecEvalVar;
2812                         break;
2813                 case T_Const:
2814                         state = (ExprState *) makeNode(ExprState);
2815                         state->evalfunc = ExecEvalConst;
2816                         break;
2817                 case T_Param:
2818                         state = (ExprState *) makeNode(ExprState);
2819                         state->evalfunc = ExecEvalParam;
2820                         break;
2821                 case T_CoerceToDomainValue:
2822                         state = (ExprState *) makeNode(ExprState);
2823                         state->evalfunc = ExecEvalCoerceToDomainValue;
2824                         break;
2825                 case T_CaseTestExpr:
2826                         state = (ExprState *) makeNode(ExprState);
2827                         state->evalfunc = ExecEvalCaseTestExpr;
2828                         break;
2829                 case T_Aggref:
2830                         {
2831                                 Aggref     *aggref = (Aggref *) node;
2832                                 AggrefExprState *astate = makeNode(AggrefExprState);
2833
2834                                 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAggref;
2835                                 if (parent && IsA(parent, AggState))
2836                                 {
2837                                         AggState   *aggstate = (AggState *) parent;
2838                                         int                     naggs;
2839
2840                                         aggstate->aggs = lcons(astate, aggstate->aggs);
2841                                         naggs = ++aggstate->numaggs;
2842
2843                                         astate->target = ExecInitExpr(aggref->target, parent);
2844
2845                                         /*
2846                                          * Complain if the aggregate's argument contains any
2847                                          * aggregates; nested agg functions are semantically
2848                                          * nonsensical.  (This should have been caught
2849                                          * earlier, but we defend against it here anyway.)
2850                                          */
2851                                         if (naggs != aggstate->numaggs)
2852                                                 ereport(ERROR,
2853                                                                 (errcode(ERRCODE_GROUPING_ERROR),
2854                                                                  errmsg("aggregate function calls may not be nested")));
2855                                 }
2856                                 else
2857                                 {
2858                                         /* planner messed up */
2859                                         elog(ERROR, "aggref found in non-Agg plan node");
2860                                 }
2861                                 state = (ExprState *) astate;
2862                         }
2863                         break;
2864                 case T_ArrayRef:
2865                         {
2866                                 ArrayRef   *aref = (ArrayRef *) node;
2867                                 ArrayRefExprState *astate = makeNode(ArrayRefExprState);
2868
2869                                 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArrayRef;
2870                                 astate->refupperindexpr = (List *)
2871                                         ExecInitExpr((Expr *) aref->refupperindexpr, parent);
2872                                 astate->reflowerindexpr = (List *)
2873                                         ExecInitExpr((Expr *) aref->reflowerindexpr, parent);
2874                                 astate->refexpr = ExecInitExpr(aref->refexpr, parent);
2875                                 astate->refassgnexpr = ExecInitExpr(aref->refassgnexpr,
2876                                                                                                         parent);
2877                                 /* do one-time catalog lookups for type info */
2878                                 astate->refattrlength = get_typlen(aref->refarraytype);
2879                                 get_typlenbyvalalign(aref->refelemtype,
2880                                                                          &astate->refelemlength,
2881                                                                          &astate->refelembyval,
2882                                                                          &astate->refelemalign);
2883                                 state = (ExprState *) astate;
2884                         }
2885                         break;
2886                 case T_FuncExpr:
2887                         {
2888                                 FuncExpr   *funcexpr = (FuncExpr *) node;
2889                                 FuncExprState *fstate = makeNode(FuncExprState);
2890
2891                                 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFunc;
2892                                 fstate->args = (List *)
2893                                         ExecInitExpr((Expr *) funcexpr->args, parent);
2894                                 fstate->func.fn_oid = InvalidOid;               /* not initialized */
2895                                 state = (ExprState *) fstate;
2896                         }
2897                         break;
2898                 case T_OpExpr:
2899                         {
2900                                 OpExpr     *opexpr = (OpExpr *) node;
2901                                 FuncExprState *fstate = makeNode(FuncExprState);
2902
2903                                 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOper;
2904                                 fstate->args = (List *)
2905                                         ExecInitExpr((Expr *) opexpr->args, parent);
2906                                 fstate->func.fn_oid = InvalidOid;               /* not initialized */
2907                                 state = (ExprState *) fstate;
2908                         }
2909                         break;
2910                 case T_DistinctExpr:
2911                         {
2912                                 DistinctExpr *distinctexpr = (DistinctExpr *) node;
2913                                 FuncExprState *fstate = makeNode(FuncExprState);
2914
2915                                 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalDistinct;
2916                                 fstate->args = (List *)
2917                                         ExecInitExpr((Expr *) distinctexpr->args, parent);
2918                                 fstate->func.fn_oid = InvalidOid;               /* not initialized */
2919                                 state = (ExprState *) fstate;
2920                         }
2921                         break;
2922                 case T_ScalarArrayOpExpr:
2923                         {
2924                                 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
2925                                 ScalarArrayOpExprState *sstate = makeNode(ScalarArrayOpExprState);
2926
2927                                 sstate->fxprstate.xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalScalarArrayOp;
2928                                 sstate->fxprstate.args = (List *)
2929                                         ExecInitExpr((Expr *) opexpr->args, parent);
2930                                 sstate->fxprstate.func.fn_oid = InvalidOid;             /* not initialized */
2931                                 sstate->element_type = InvalidOid;              /* ditto */
2932                                 state = (ExprState *) sstate;
2933                         }
2934                         break;
2935                 case T_BoolExpr:
2936                         {
2937                                 BoolExpr   *boolexpr = (BoolExpr *) node;
2938                                 BoolExprState *bstate = makeNode(BoolExprState);
2939
2940                                 switch (boolexpr->boolop)
2941                                 {
2942                                         case AND_EXPR:
2943                                                 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAnd;
2944                                                 break;
2945                                         case OR_EXPR:
2946                                                 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOr;
2947                                                 break;
2948                                         case NOT_EXPR:
2949                                                 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNot;
2950                                                 break;
2951                                         default:
2952                                                 elog(ERROR, "unrecognized boolop: %d",
2953                                                          (int) boolexpr->boolop);
2954                                                 break;
2955                                 }
2956                                 bstate->args = (List *)
2957                                         ExecInitExpr((Expr *) boolexpr->args, parent);
2958                                 state = (ExprState *) bstate;
2959                         }
2960                         break;
2961                 case T_SubPlan:
2962                         {
2963                                 /* Keep this in sync with ExecInitExprInitPlan, below */
2964                                 SubPlan    *subplan = (SubPlan *) node;
2965                                 SubPlanState *sstate = makeNode(SubPlanState);
2966
2967                                 sstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecSubPlan;
2968
2969                                 if (!parent)
2970                                         elog(ERROR, "SubPlan found with no parent plan");
2971
2972                                 /*
2973                                  * Here we just add the SubPlanState nodes to
2974                                  * parent->subPlan.  The subplans will be initialized
2975                                  * later.
2976                                  */
2977                                 parent->subPlan = lcons(sstate, parent->subPlan);
2978                                 sstate->sub_estate = NULL;
2979                                 sstate->planstate = NULL;
2980
2981                                 sstate->exprs = (List *)
2982                                         ExecInitExpr((Expr *) subplan->exprs, parent);
2983                                 sstate->args = (List *)
2984                                         ExecInitExpr((Expr *) subplan->args, parent);
2985
2986                                 state = (ExprState *) sstate;
2987                         }
2988                         break;
2989                 case T_FieldSelect:
2990                         {
2991                                 FieldSelect *fselect = (FieldSelect *) node;
2992                                 FieldSelectState *fstate = makeNode(FieldSelectState);
2993
2994                                 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldSelect;
2995                                 fstate->arg = ExecInitExpr(fselect->arg, parent);
2996                                 fstate->argdesc = NULL;
2997                                 state = (ExprState *) fstate;
2998                         }
2999                         break;
3000                 case T_FieldStore:
3001                         {
3002                                 FieldStore *fstore = (FieldStore *) node;
3003                                 FieldStoreState *fstate = makeNode(FieldStoreState);
3004
3005                                 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldStore;
3006                                 fstate->arg = ExecInitExpr(fstore->arg, parent);
3007                                 fstate->newvals = (List *) ExecInitExpr((Expr *) fstore->newvals, parent);
3008                                 fstate->argdesc = NULL;
3009                                 state = (ExprState *) fstate;
3010                         }
3011                         break;
3012                 case T_RelabelType:
3013                         {
3014                                 RelabelType *relabel = (RelabelType *) node;
3015                                 GenericExprState *gstate = makeNode(GenericExprState);
3016
3017                                 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRelabelType;
3018                                 gstate->arg = ExecInitExpr(relabel->arg, parent);
3019                                 state = (ExprState *) gstate;
3020                         }
3021                         break;
3022                 case T_ConvertRowtypeExpr:
3023                         {
3024                                 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
3025                                 ConvertRowtypeExprState *cstate = makeNode(ConvertRowtypeExprState);
3026                                 int             i;
3027                                 int             n;
3028
3029                                 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalConvertRowtype;
3030                                 cstate->arg = ExecInitExpr(convert->arg, parent);
3031                                 /* save copies of needed tuple descriptors */
3032                                 cstate->indesc = lookup_rowtype_tupdesc(exprType((Node *) convert->arg), -1);
3033                                 cstate->indesc = CreateTupleDescCopy(cstate->indesc);
3034                                 cstate->outdesc = lookup_rowtype_tupdesc(convert->resulttype, -1);
3035                                 cstate->outdesc = CreateTupleDescCopy(cstate->outdesc);
3036                                 /* prepare map from old to new attribute numbers */
3037                                 n = cstate->outdesc->natts;
3038                                 cstate->attrMap = (AttrNumber *) palloc0(n * sizeof(AttrNumber));
3039                                 for (i = 0; i < n; i++)
3040                                 {
3041                                         Form_pg_attribute att = cstate->outdesc->attrs[i];
3042                                         char       *attname;
3043                                         Oid                     atttypid;
3044                                         int32           atttypmod;
3045                                         int                     j;
3046
3047                                         if (att->attisdropped)
3048                                                 continue;       /* attrMap[i] is already 0 */
3049                                         attname = NameStr(att->attname);
3050                                         atttypid = att->atttypid;
3051                                         atttypmod = att->atttypmod;
3052                                         for (j = 0; j < cstate->indesc->natts; j++)
3053                                         {
3054                                                 att = cstate->indesc->attrs[j];
3055                                                 if (att->attisdropped)
3056                                                         continue;
3057                                                 if (strcmp(attname, NameStr(att->attname)) == 0)
3058                                                 {
3059                                                         /* Found it, check type */
3060                                                         if (atttypid != att->atttypid || atttypmod != att->atttypmod)
3061                                                                 elog(ERROR, "attribute \"%s\" of type %s does not match corresponding attribute of type %s",
3062                                                                          attname,
3063                                                                          format_type_be(cstate->indesc->tdtypeid),
3064                                                                          format_type_be(cstate->outdesc->tdtypeid));
3065                                                         cstate->attrMap[i] = (AttrNumber) (j + 1);
3066                                                         break;
3067                                                 }
3068                                         }
3069                                         if (cstate->attrMap[i] == 0)
3070                                                 elog(ERROR, "attribute \"%s\" of type %s does not exist",
3071                                                          attname,
3072                                                          format_type_be(cstate->indesc->tdtypeid));
3073                                 }
3074                                 /* preallocate workspace for Datum arrays */
3075                                 n = cstate->indesc->natts + 1;  /* +1 for NULL */
3076                                 cstate->invalues = (Datum *) palloc(n * sizeof(Datum));
3077                                 cstate->innulls = (char *) palloc(n * sizeof(char));
3078                                 n = cstate->outdesc->natts;
3079                                 cstate->outvalues = (Datum *) palloc(n * sizeof(Datum));
3080                                 cstate->outnulls = (char *) palloc(n * sizeof(char));
3081                                 state = (ExprState *) cstate;
3082                         }
3083                         break;
3084                 case T_CaseExpr:
3085                         {
3086                                 CaseExpr   *caseexpr = (CaseExpr *) node;
3087                                 CaseExprState *cstate = makeNode(CaseExprState);
3088                                 List       *outlist = NIL;
3089                                 ListCell   *l;
3090
3091                                 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCase;
3092                                 cstate->arg = ExecInitExpr(caseexpr->arg, parent);
3093                                 foreach(l, caseexpr->args)
3094                                 {
3095                                         CaseWhen   *when = (CaseWhen *) lfirst(l);
3096                                         CaseWhenState *wstate = makeNode(CaseWhenState);
3097
3098                                         Assert(IsA(when, CaseWhen));
3099                                         wstate->xprstate.evalfunc = NULL;       /* not used */
3100                                         wstate->xprstate.expr = (Expr *) when;
3101                                         wstate->expr = ExecInitExpr(when->expr, parent);
3102                                         wstate->result = ExecInitExpr(when->result, parent);
3103                                         outlist = lappend(outlist, wstate);
3104                                 }
3105                                 cstate->args = outlist;
3106                                 cstate->defresult = ExecInitExpr(caseexpr->defresult, parent);
3107                                 state = (ExprState *) cstate;
3108                         }
3109                         break;
3110                 case T_ArrayExpr:
3111                         {
3112                                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
3113                                 ArrayExprState *astate = makeNode(ArrayExprState);
3114                                 List       *outlist = NIL;
3115                                 ListCell   *l;
3116
3117                                 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArray;
3118                                 foreach(l, arrayexpr->elements)
3119                                 {
3120                                         Expr       *e = (Expr *) lfirst(l);
3121                                         ExprState  *estate;
3122
3123                                         estate = ExecInitExpr(e, parent);
3124                                         outlist = lappend(outlist, estate);
3125                                 }
3126                                 astate->elements = outlist;
3127                                 /* do one-time catalog lookup for type info */
3128                                 get_typlenbyvalalign(arrayexpr->element_typeid,
3129                                                                          &astate->elemlength,
3130                                                                          &astate->elembyval,
3131                                                                          &astate->elemalign);
3132                                 state = (ExprState *) astate;
3133                         }
3134                         break;
3135                 case T_RowExpr:
3136                         {
3137                                 RowExpr    *rowexpr = (RowExpr *) node;
3138                                 RowExprState *rstate = makeNode(RowExprState);
3139                                 Form_pg_attribute *attrs;
3140                                 List       *outlist = NIL;
3141                                 ListCell   *l;
3142                                 int                     i;
3143
3144                                 rstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRow;
3145                                 /* Build tupdesc to describe result tuples */
3146                                 if (rowexpr->row_typeid == RECORDOID)
3147                                 {
3148                                         /* generic record, use runtime type assignment */
3149                                         rstate->tupdesc = ExecTypeFromExprList(rowexpr->args);
3150                                         rstate->tupdesc = BlessTupleDesc(rstate->tupdesc);
3151                                 }
3152                                 else
3153                                 {
3154                                         /* it's been cast to a named type, use that */
3155                                         rstate->tupdesc = lookup_rowtype_tupdesc(rowexpr->row_typeid, -1);
3156                                         rstate->tupdesc = CreateTupleDescCopy(rstate->tupdesc);
3157                                 }
3158                                 /* Set up evaluation, skipping any deleted columns */
3159                                 Assert(list_length(rowexpr->args) <= rstate->tupdesc->natts);
3160                                 attrs = rstate->tupdesc->attrs;
3161                                 i = 0;
3162                                 foreach(l, rowexpr->args)
3163                                 {
3164                                         Expr       *e = (Expr *) lfirst(l);
3165                                         ExprState  *estate;
3166
3167                                         if (!attrs[i]->attisdropped)
3168                                         {
3169                                                 /*
3170                                                  * Guard against ALTER COLUMN TYPE on rowtype
3171                                                  * since the RowExpr was created.  XXX should we
3172                                                  * check typmod too?  Not sure we can be sure
3173                                                  * it'll be the same.
3174                                                  */
3175                                                 if (exprType((Node *) e) != attrs[i]->atttypid)
3176                                                         ereport(ERROR,
3177                                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3178                                                                          errmsg("ROW() column has type %s instead of type %s",
3179                                                                         format_type_be(exprType((Node *) e)),
3180                                                                    format_type_be(attrs[i]->atttypid))));
3181                                         }
3182                                         else
3183                                         {
3184                                                 /*
3185                                                  * Ignore original expression and insert a NULL.
3186                                                  * We don't really care what type of NULL it is,
3187                                                  * so always make an int4 NULL.
3188                                                  */
3189                                                 e = (Expr *) makeNullConst(INT4OID);
3190                                         }
3191                                         estate = ExecInitExpr(e, parent);
3192                                         outlist = lappend(outlist, estate);
3193                                         i++;
3194                                 }
3195                                 rstate->args = outlist;
3196                                 state = (ExprState *) rstate;
3197                         }
3198                         break;
3199                 case T_CoalesceExpr:
3200                         {
3201                                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3202                                 CoalesceExprState *cstate = makeNode(CoalesceExprState);
3203                                 List       *outlist = NIL;
3204                                 ListCell   *l;
3205
3206                                 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoalesce;
3207                                 foreach(l, coalesceexpr->args)
3208                                 {
3209                                         Expr       *e = (Expr *) lfirst(l);
3210                                         ExprState  *estate;
3211
3212                                         estate = ExecInitExpr(e, parent);
3213                                         outlist = lappend(outlist, estate);
3214                                 }
3215                                 cstate->args = outlist;
3216                                 state = (ExprState *) cstate;
3217                         }
3218                         break;
3219                 case T_NullIfExpr:
3220                         {
3221                                 NullIfExpr *nullifexpr = (NullIfExpr *) node;
3222                                 FuncExprState *fstate = makeNode(FuncExprState);
3223
3224                                 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullIf;
3225                                 fstate->args = (List *)
3226                                         ExecInitExpr((Expr *) nullifexpr->args, parent);
3227                                 fstate->func.fn_oid = InvalidOid;               /* not initialized */
3228                                 state = (ExprState *) fstate;
3229                         }
3230                         break;
3231                 case T_NullTest:
3232                         {
3233                                 NullTest   *ntest = (NullTest *) node;
3234                                 GenericExprState *gstate = makeNode(GenericExprState);
3235
3236                                 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullTest;
3237                                 gstate->arg = ExecInitExpr(ntest->arg, parent);
3238                                 state = (ExprState *) gstate;
3239                         }
3240                         break;
3241                 case T_BooleanTest:
3242                         {
3243                                 BooleanTest *btest = (BooleanTest *) node;
3244                                 GenericExprState *gstate = makeNode(GenericExprState);
3245
3246                                 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalBooleanTest;
3247                                 gstate->arg = ExecInitExpr(btest->arg, parent);
3248                                 state = (ExprState *) gstate;
3249                         }
3250                         break;
3251                 case T_CoerceToDomain:
3252                         {
3253                                 CoerceToDomain *ctest = (CoerceToDomain *) node;
3254                                 CoerceToDomainState *cstate = makeNode(CoerceToDomainState);
3255
3256                                 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoerceToDomain;
3257                                 cstate->arg = ExecInitExpr(ctest->arg, parent);
3258                                 cstate->constraints = GetDomainConstraints(ctest->resulttype);
3259                                 state = (ExprState *) cstate;
3260                         }
3261                         break;
3262                 case T_TargetEntry:
3263                         {
3264                                 TargetEntry *tle = (TargetEntry *) node;
3265                                 GenericExprState *gstate = makeNode(GenericExprState);
3266
3267                                 gstate->xprstate.evalfunc = NULL;               /* not used */
3268                                 gstate->arg = ExecInitExpr(tle->expr, parent);
3269                                 state = (ExprState *) gstate;
3270                         }
3271                         break;
3272                 case T_List:
3273                         {
3274                                 List       *outlist = NIL;
3275                                 ListCell   *l;
3276
3277                                 foreach(l, (List *) node)
3278                                 {
3279                                         outlist = lappend(outlist,
3280                                                                           ExecInitExpr((Expr *) lfirst(l),
3281                                                                                                    parent));
3282                                 }
3283                                 /* Don't fall through to the "common" code below */
3284                                 return (ExprState *) outlist;
3285                         }
3286                 default:
3287                         elog(ERROR, "unrecognized node type: %d",
3288                                  (int) nodeTag(node));
3289                         state = NULL;           /* keep compiler quiet */
3290                         break;
3291         }
3292
3293         /* Common code for all state-node types */
3294         state->expr = node;
3295
3296         return state;
3297 }
3298
3299 /*
3300  * ExecInitExprInitPlan --- initialize a subplan expr that's being handled
3301  * as an InitPlan.      This is identical to ExecInitExpr's handling of a regular
3302  * subplan expr, except we do NOT want to add the node to the parent's
3303  * subplan list.
3304  */
3305 SubPlanState *
3306 ExecInitExprInitPlan(SubPlan *node, PlanState *parent)
3307 {
3308         SubPlanState *sstate = makeNode(SubPlanState);
3309
3310         if (!parent)
3311                 elog(ERROR, "SubPlan found with no parent plan");
3312
3313         /* The subplan's state will be initialized later */
3314         sstate->sub_estate = NULL;
3315         sstate->planstate = NULL;
3316
3317         sstate->exprs = (List *) ExecInitExpr((Expr *) node->exprs, parent);
3318         sstate->args = (List *) ExecInitExpr((Expr *) node->args, parent);
3319
3320         sstate->xprstate.expr = (Expr *) node;
3321
3322         return sstate;
3323 }
3324
3325 /*
3326  * ExecPrepareExpr --- initialize for expression execution outside a normal
3327  * Plan tree context.
3328  *
3329  * This differs from ExecInitExpr in that we don't assume the caller is
3330  * already running in the EState's per-query context.  Also, we apply
3331  * fix_opfuncids() to the passed expression tree to be sure it is ready
3332  * to run.      (In ordinary Plan trees the planner will have fixed opfuncids,
3333  * but callers outside the executor will not have done this.)
3334  */
3335 ExprState *
3336 ExecPrepareExpr(Expr *node, EState *estate)
3337 {
3338         ExprState  *result;
3339         MemoryContext oldcontext;
3340
3341         fix_opfuncids((Node *) node);
3342
3343         oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3344
3345         result = ExecInitExpr(node, NULL);
3346
3347         MemoryContextSwitchTo(oldcontext);
3348
3349         return result;
3350 }
3351
3352
3353 /* ----------------------------------------------------------------
3354  *                                       ExecQual / ExecTargetList / ExecProject
3355  * ----------------------------------------------------------------
3356  */
3357
3358 /* ----------------------------------------------------------------
3359  *              ExecQual
3360  *
3361  *              Evaluates a conjunctive boolean expression (qual list) and
3362  *              returns true iff none of the subexpressions are false.
3363  *              (We also return true if the list is empty.)
3364  *
3365  *      If some of the subexpressions yield NULL but none yield FALSE,
3366  *      then the result of the conjunction is NULL (ie, unknown)
3367  *      according to three-valued boolean logic.  In this case,
3368  *      we return the value specified by the "resultForNull" parameter.
3369  *
3370  *      Callers evaluating WHERE clauses should pass resultForNull=FALSE,
3371  *      since SQL specifies that tuples with null WHERE results do not
3372  *      get selected.  On the other hand, callers evaluating constraint
3373  *      conditions should pass resultForNull=TRUE, since SQL also specifies
3374  *      that NULL constraint conditions are not failures.
3375  *
3376  *      NOTE: it would not be correct to use this routine to evaluate an
3377  *      AND subclause of a boolean expression; for that purpose, a NULL
3378  *      result must be returned as NULL so that it can be properly treated
3379  *      in the next higher operator (cf. ExecEvalAnd and ExecEvalOr).
3380  *      This routine is only used in contexts where a complete expression
3381  *      is being evaluated and we know that NULL can be treated the same
3382  *      as one boolean result or the other.
3383  *
3384  * ----------------------------------------------------------------
3385  */
3386 bool
3387 ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
3388 {
3389         bool            result;
3390         MemoryContext oldContext;
3391         ListCell   *l;
3392
3393         /*
3394          * debugging stuff
3395          */
3396         EV_printf("ExecQual: qual is ");
3397         EV_nodeDisplay(qual);
3398         EV_printf("\n");
3399
3400         IncrProcessed();
3401
3402         /*
3403          * Run in short-lived per-tuple context while computing expressions.
3404          */
3405         oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
3406
3407         /*
3408          * Evaluate the qual conditions one at a time.  If we find a FALSE
3409          * result, we can stop evaluating and return FALSE --- the AND result
3410          * must be FALSE.  Also, if we find a NULL result when resultForNull
3411          * is FALSE, we can stop and return FALSE --- the AND result must be
3412          * FALSE or NULL in that case, and the caller doesn't care which.
3413          *
3414          * If we get to the end of the list, we can return TRUE.  This will
3415          * happen when the AND result is indeed TRUE, or when the AND result
3416          * is NULL (one or more NULL subresult, with all the rest TRUE) and
3417          * the caller has specified resultForNull = TRUE.
3418          */
3419         result = true;
3420
3421         foreach(l, qual)
3422         {
3423                 ExprState  *clause = (ExprState *) lfirst(l);
3424                 Datum           expr_value;
3425                 bool            isNull;
3426
3427                 expr_value = ExecEvalExpr(clause, econtext, &isNull, NULL);
3428
3429                 if (isNull)
3430                 {
3431                         if (resultForNull == false)
3432                         {
3433                                 result = false; /* treat NULL as FALSE */
3434                                 break;
3435                         }
3436                 }
3437                 else
3438                 {
3439                         if (!DatumGetBool(expr_value))
3440                         {
3441                                 result = false; /* definitely FALSE */
3442                                 break;
3443                         }
3444                 }
3445         }
3446
3447         MemoryContextSwitchTo(oldContext);
3448
3449         return result;
3450 }
3451
3452 /*
3453  * Number of items in a tlist (including any resjunk items!)
3454  */
3455 int
3456 ExecTargetListLength(List *targetlist)
3457 {
3458         /* This used to be more complex, but fjoins are dead */
3459         return list_length(targetlist);
3460 }
3461
3462 /*
3463  * Number of items in a tlist, not including any resjunk items
3464  */
3465 int
3466 ExecCleanTargetListLength(List *targetlist)
3467 {
3468         int                     len = 0;
3469         ListCell   *tl;
3470
3471         foreach(tl, targetlist)
3472         {
3473                 TargetEntry *curTle = (TargetEntry *) lfirst(tl);
3474
3475                 Assert(IsA(curTle, TargetEntry));
3476                 if (!curTle->resdom->resjunk)
3477                         len++;
3478         }
3479         return len;
3480 }
3481
3482 /* ----------------------------------------------------------------
3483  *              ExecTargetList
3484  *
3485  *              Evaluates a targetlist with respect to the given
3486  *              expression context and returns a tuple.
3487  *
3488  * The caller must pass workspace for the values and nulls arrays
3489  * as well as the itemIsDone array.  This convention saves palloc'ing
3490  * workspace on each call, and some callers may find it useful to examine
3491  * the values array directly.
3492  *
3493  * As with ExecEvalExpr, the caller should pass isDone = NULL if not
3494  * prepared to deal with sets of result tuples.  Otherwise, a return
3495  * of *isDone = ExprMultipleResult signifies a set element, and a return
3496  * of *isDone = ExprEndResult signifies end of the set of tuple.
3497  * ----------------------------------------------------------------
3498  */
3499 static HeapTuple
3500 ExecTargetList(List *targetlist,
3501                            TupleDesc targettype,
3502                            ExprContext *econtext,
3503                            Datum *values,
3504                            char *nulls,
3505                            ExprDoneCond *itemIsDone,
3506                            ExprDoneCond *isDone)
3507 {
3508         MemoryContext oldContext;
3509         ListCell   *tl;
3510         bool            isNull;
3511         bool            haveDoneSets;
3512
3513         /*
3514          * debugging stuff
3515          */
3516         EV_printf("ExecTargetList: tl is ");
3517         EV_nodeDisplay(targetlist);
3518         EV_printf("\n");
3519
3520         /*
3521          * Run in short-lived per-tuple context while computing expressions.
3522          */
3523         oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
3524
3525         /*
3526          * There used to be some klugy and demonstrably broken code here that
3527          * special-cased the situation where targetlist == NIL.  Now we just
3528          * fall through and return an empty-but-valid tuple.
3529          */
3530
3531         /*
3532          * evaluate all the expressions in the target list
3533          */
3534         if (isDone)
3535                 *isDone = ExprSingleResult;             /* until proven otherwise */
3536
3537         haveDoneSets = false;           /* any exhausted set exprs in tlist? */
3538
3539         foreach(tl, targetlist)
3540         {
3541                 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
3542                 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
3543                 AttrNumber      resind = tle->resdom->resno - 1;
3544
3545                 values[resind] = ExecEvalExpr(gstate->arg,
3546                                                                           econtext,
3547                                                                           &isNull,
3548                                                                           &itemIsDone[resind]);
3549                 nulls[resind] = isNull ? 'n' : ' ';
3550
3551                 if (itemIsDone[resind] != ExprSingleResult)
3552                 {
3553                         /* We have a set-valued expression in the tlist */
3554                         if (isDone == NULL)
3555                                 ereport(ERROR,
3556                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3557                                                  errmsg("set-valued function called in context that cannot accept a set")));
3558                         if (itemIsDone[resind] == ExprMultipleResult)
3559                         {
3560                                 /* we have undone sets in the tlist, set flag */
3561                                 *isDone = ExprMultipleResult;
3562                         }
3563                         else
3564                         {
3565                                 /* we have done sets in the tlist, set flag for that */
3566                                 haveDoneSets = true;
3567                         }
3568                 }
3569         }
3570
3571         if (haveDoneSets)
3572         {
3573                 /*
3574                  * note: can't get here unless we verified isDone != NULL
3575                  */
3576                 if (*isDone == ExprSingleResult)
3577                 {
3578                         /*
3579                          * all sets are done, so report that tlist expansion is
3580                          * complete.
3581                          */
3582                         *isDone = ExprEndResult;
3583                         MemoryContextSwitchTo(oldContext);
3584                         return NULL;
3585                 }
3586                 else
3587                 {
3588                         /*
3589                          * We have some done and some undone sets.      Restart the done
3590                          * ones so that we can deliver a tuple (if possible).
3591                          */
3592                         foreach(tl, targetlist)
3593                         {
3594                                 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
3595                                 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
3596                                 AttrNumber      resind = tle->resdom->resno - 1;
3597
3598                                 if (itemIsDone[resind] == ExprEndResult)
3599                                 {
3600                                         values[resind] = ExecEvalExpr(gstate->arg,
3601                                                                                                   econtext,
3602                                                                                                   &isNull,
3603                                                                                                   &itemIsDone[resind]);
3604                                         nulls[resind] = isNull ? 'n' : ' ';
3605
3606                                         if (itemIsDone[resind] == ExprEndResult)
3607                                         {
3608                                                 /*
3609                                                  * Oh dear, this item is returning an empty set.
3610                                                  * Guess we can't make a tuple after all.
3611                                                  */
3612                                                 *isDone = ExprEndResult;
3613                                                 break;
3614                                         }
3615                                 }
3616                         }
3617
3618                         /*
3619                          * If we cannot make a tuple because some sets are empty, we
3620                          * still have to cycle the nonempty sets to completion, else
3621                          * resources will not be released from subplans etc.
3622                          *
3623                          * XXX is that still necessary?
3624                          */
3625                         if (*isDone == ExprEndResult)
3626                         {
3627                                 foreach(tl, targetlist)
3628                                 {
3629                                         GenericExprState *gstate = (GenericExprState *) lfirst(tl);
3630                                         TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
3631                                         AttrNumber      resind = tle->resdom->resno - 1;
3632
3633                                         while (itemIsDone[resind] == ExprMultipleResult)
3634                                         {
3635                                                 (void) ExecEvalExpr(gstate->arg,
3636                                                                                         econtext,
3637                                                                                         &isNull,
3638                                                                                         &itemIsDone[resind]);
3639                                         }
3640                                 }
3641
3642                                 MemoryContextSwitchTo(oldContext);
3643                                 return NULL;
3644                         }
3645                 }
3646         }
3647
3648         /*
3649          * form the new result tuple (in the caller's memory context!)
3650          */
3651         MemoryContextSwitchTo(oldContext);
3652
3653         return heap_formtuple(targettype, values, nulls);
3654 }
3655
3656 /* ----------------------------------------------------------------
3657  *              ExecProject
3658  *
3659  *              projects a tuple based on projection info and stores
3660  *              it in the specified tuple table slot.
3661  *
3662  *              Note: someday soon the executor can be extended to eliminate
3663  *                        redundant projections by storing pointers to datums
3664  *                        in the tuple table and then passing these around when
3665  *                        possible.  this should make things much quicker.
3666  *                        -cim 6/3/91
3667  * ----------------------------------------------------------------
3668  */
3669 TupleTableSlot *
3670 ExecProject(ProjectionInfo *projInfo, ExprDoneCond *isDone)
3671 {
3672         TupleTableSlot *slot;
3673         TupleDesc       tupType;
3674         HeapTuple       newTuple;
3675
3676         /*
3677          * sanity checks
3678          */
3679         if (projInfo == NULL)
3680                 return NULL;
3681
3682         /*
3683          * get the projection info we want
3684          */
3685         slot = projInfo->pi_slot;
3686         tupType = slot->ttc_tupleDescriptor;
3687
3688         /*
3689          * form a new result tuple (if possible --- result can be NULL)
3690          */
3691         newTuple = ExecTargetList(projInfo->pi_targetlist,
3692                                                           tupType,
3693                                                           projInfo->pi_exprContext,
3694                                                           projInfo->pi_tupValues,
3695                                                           projInfo->pi_tupNulls,
3696                                                           projInfo->pi_itemIsDone,
3697                                                           isDone);
3698
3699         /*
3700          * store the tuple in the projection slot and return the slot.
3701          */
3702         return ExecStoreTuple(newTuple,         /* tuple to store */
3703                                                   slot, /* slot to store in */
3704                                                   InvalidBuffer,                /* tuple has no buffer */
3705                                                   true);
3706 }