1 /*-------------------------------------------------------------------------
4 * Routines to evaluate qualification and targetlist expressions
6 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.214 2007/02/27 01:11:25 tgl Exp $
13 *-------------------------------------------------------------------------
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
23 * The more heavily used ExecEvalExpr routines, such as ExecEvalVar(),
24 * are hotspots. Making these faster will speed up the entire system.
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
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
39 #include "access/heapam.h"
40 #include "access/nbtree.h"
41 #include "catalog/pg_type.h"
42 #include "commands/typecmds.h"
43 #include "executor/execdebug.h"
44 #include "executor/nodeSubplan.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/builtins.h"
52 #include "utils/lsyscache.h"
53 #include "utils/memutils.h"
54 #include "utils/typcache.h"
55 #include "utils/xml.h"
58 /* static function decls */
59 static Datum ExecEvalArrayRef(ArrayRefExprState *astate,
60 ExprContext *econtext,
61 bool *isNull, ExprDoneCond *isDone);
62 static Datum ExecEvalAggref(AggrefExprState *aggref,
63 ExprContext *econtext,
64 bool *isNull, ExprDoneCond *isDone);
65 static Datum ExecEvalVar(ExprState *exprstate, ExprContext *econtext,
66 bool *isNull, ExprDoneCond *isDone);
67 static Datum ExecEvalScalarVar(ExprState *exprstate, ExprContext *econtext,
68 bool *isNull, ExprDoneCond *isDone);
69 static Datum ExecEvalWholeRowVar(ExprState *exprstate, ExprContext *econtext,
70 bool *isNull, ExprDoneCond *isDone);
71 static Datum ExecEvalConst(ExprState *exprstate, ExprContext *econtext,
72 bool *isNull, ExprDoneCond *isDone);
73 static Datum ExecEvalParam(ExprState *exprstate, ExprContext *econtext,
74 bool *isNull, ExprDoneCond *isDone);
75 static void ShutdownFuncExpr(Datum arg);
76 static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod,
77 TupleDesc *cache_field, ExprContext *econtext);
78 static void ShutdownTupleDescRef(Datum arg);
79 static ExprDoneCond ExecEvalFuncArgs(FunctionCallInfo fcinfo,
80 List *argList, ExprContext *econtext);
81 static Datum ExecMakeFunctionResultNoSets(FuncExprState *fcache,
82 ExprContext *econtext,
83 bool *isNull, ExprDoneCond *isDone);
84 static Datum ExecEvalFunc(FuncExprState *fcache, ExprContext *econtext,
85 bool *isNull, ExprDoneCond *isDone);
86 static Datum ExecEvalOper(FuncExprState *fcache, ExprContext *econtext,
87 bool *isNull, ExprDoneCond *isDone);
88 static Datum ExecEvalDistinct(FuncExprState *fcache, ExprContext *econtext,
89 bool *isNull, ExprDoneCond *isDone);
90 static Datum ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
91 ExprContext *econtext,
92 bool *isNull, ExprDoneCond *isDone);
93 static Datum ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
94 bool *isNull, ExprDoneCond *isDone);
95 static Datum ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
96 bool *isNull, ExprDoneCond *isDone);
97 static Datum ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
98 bool *isNull, ExprDoneCond *isDone);
99 static Datum ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
100 ExprContext *econtext,
101 bool *isNull, ExprDoneCond *isDone);
102 static Datum ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
103 bool *isNull, ExprDoneCond *isDone);
104 static Datum ExecEvalCaseTestExpr(ExprState *exprstate,
105 ExprContext *econtext,
106 bool *isNull, ExprDoneCond *isDone);
107 static Datum ExecEvalArray(ArrayExprState *astate,
108 ExprContext *econtext,
109 bool *isNull, ExprDoneCond *isDone);
110 static Datum ExecEvalRow(RowExprState *rstate,
111 ExprContext *econtext,
112 bool *isNull, ExprDoneCond *isDone);
113 static Datum ExecEvalRowCompare(RowCompareExprState *rstate,
114 ExprContext *econtext,
115 bool *isNull, ExprDoneCond *isDone);
116 static Datum ExecEvalCoalesce(CoalesceExprState *coalesceExpr,
117 ExprContext *econtext,
118 bool *isNull, ExprDoneCond *isDone);
119 static Datum ExecEvalMinMax(MinMaxExprState *minmaxExpr,
120 ExprContext *econtext,
121 bool *isNull, ExprDoneCond *isDone);
122 static Datum ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
123 bool *isNull, ExprDoneCond *isDone);
124 static Datum ExecEvalNullIf(FuncExprState *nullIfExpr,
125 ExprContext *econtext,
126 bool *isNull, ExprDoneCond *isDone);
127 static Datum ExecEvalNullTest(NullTestState *nstate,
128 ExprContext *econtext,
129 bool *isNull, ExprDoneCond *isDone);
130 static Datum ExecEvalBooleanTest(GenericExprState *bstate,
131 ExprContext *econtext,
132 bool *isNull, ExprDoneCond *isDone);
133 static Datum ExecEvalCoerceToDomain(CoerceToDomainState *cstate,
134 ExprContext *econtext,
135 bool *isNull, ExprDoneCond *isDone);
136 static Datum ExecEvalCoerceToDomainValue(ExprState *exprstate,
137 ExprContext *econtext,
138 bool *isNull, ExprDoneCond *isDone);
139 static Datum ExecEvalFieldSelect(FieldSelectState *fstate,
140 ExprContext *econtext,
141 bool *isNull, ExprDoneCond *isDone);
142 static Datum ExecEvalFieldStore(FieldStoreState *fstate,
143 ExprContext *econtext,
144 bool *isNull, ExprDoneCond *isDone);
145 static Datum ExecEvalRelabelType(GenericExprState *exprstate,
146 ExprContext *econtext,
147 bool *isNull, ExprDoneCond *isDone);
150 /* ----------------------------------------------------------------
151 * ExecEvalExpr routines
153 * Recursively evaluate a targetlist or qualification expression.
155 * Each of the following routines having the signature
156 * Datum ExecEvalFoo(ExprState *expression,
157 * ExprContext *econtext,
159 * ExprDoneCond *isDone);
160 * is responsible for evaluating one type or subtype of ExprState node.
161 * They are normally called via the ExecEvalExpr macro, which makes use of
162 * the function pointer set up when the ExprState node was built by
163 * ExecInitExpr. (In some cases, we change this pointer later to avoid
164 * re-executing one-time overhead.)
166 * Note: for notational simplicity we declare these functions as taking the
167 * specific type of ExprState that they work on. This requires casting when
168 * assigning the function pointer in ExecInitExpr. Be careful that the
169 * function signature is declared correctly, because the cast suppresses
170 * automatic checking!
173 * All these functions share this calling convention:
176 * expression: the expression state tree to evaluate
177 * econtext: evaluation context information
180 * return value: Datum value of result
181 * *isNull: set to TRUE if result is NULL (actual return value is
182 * meaningless if so); set to FALSE if non-null result
183 * *isDone: set to indicator of set-result status
185 * A caller that can only accept a singleton (non-set) result should pass
186 * NULL for isDone; if the expression computes a set result then an error
187 * will be reported via ereport. If the caller does pass an isDone pointer
188 * then *isDone is set to one of these three states:
189 * ExprSingleResult singleton result (not a set)
190 * ExprMultipleResult return value is one element of a set
191 * ExprEndResult there are no more elements in the set
192 * When ExprMultipleResult is returned, the caller should invoke
193 * ExecEvalExpr() repeatedly until ExprEndResult is returned. ExprEndResult
194 * is returned after the last real set element. For convenience isNull will
195 * always be set TRUE when ExprEndResult is returned, but this should not be
196 * taken as indicating a NULL element of the set. Note that these return
197 * conventions allow us to distinguish among a singleton NULL, a NULL element
198 * of a set, and an empty set.
200 * The caller should already have switched into the temporary memory
201 * context econtext->ecxt_per_tuple_memory. The convenience entry point
202 * ExecEvalExprSwitchContext() is provided for callers who don't prefer to
203 * do the switch in an outer loop. We do not do the switch in these routines
204 * because it'd be a waste of cycles during nested expression evaluation.
205 * ----------------------------------------------------------------
212 * This function takes an ArrayRef and returns the extracted Datum
213 * if it's a simple reference, or the modified array value if it's
214 * an array assignment (i.e., array element or slice insertion).
216 * NOTE: if we get a NULL result from a subscript expression, we return NULL
217 * when it's an array reference, or raise an error when it's an assignment.
219 * NOTE: we deliberately refrain from applying DatumGetArrayTypeP() here,
220 * even though that might seem natural, because this code needs to support
221 * both varlena arrays and fixed-length array types. DatumGetArrayTypeP()
222 * only works for the varlena kind. The routines we call in arrayfuncs.c
223 * have to know the difference (that's what they need refattrlength for).
227 ExecEvalArrayRef(ArrayRefExprState *astate,
228 ExprContext *econtext,
230 ExprDoneCond *isDone)
232 ArrayRef *arrayRef = (ArrayRef *) astate->xprstate.expr;
233 ArrayType *array_source;
234 ArrayType *resultArray;
235 bool isAssignment = (arrayRef->refassgnexpr != NULL);
244 array_source = (ArrayType *)
245 DatumGetPointer(ExecEvalExpr(astate->refexpr,
251 * If refexpr yields NULL, and it's a fetch, then result is NULL. In the
252 * assignment case, we'll cons up something below.
256 if (isDone && *isDone == ExprEndResult)
257 return (Datum) NULL; /* end of set result */
262 foreach(l, astate->refupperindexpr)
264 ExprState *eltstate = (ExprState *) lfirst(l);
268 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
269 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
272 upper.indx[i++] = DatumGetInt32(ExecEvalExpr(eltstate,
276 /* If any index expr yields NULL, result is NULL or error */
281 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
282 errmsg("array subscript in assignment must not be null")));
288 if (astate->reflowerindexpr != NIL)
290 foreach(l, astate->reflowerindexpr)
292 ExprState *eltstate = (ExprState *) lfirst(l);
296 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
297 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
300 lower.indx[j++] = DatumGetInt32(ExecEvalExpr(eltstate,
304 /* If any index expr yields NULL, result is NULL or error */
309 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
310 errmsg("array subscript in assignment must not be null")));
315 /* this can't happen unless parser messed up */
317 elog(ERROR, "upper and lower index lists are not same length");
328 * Evaluate the value to be assigned into the array.
330 * XXX At some point we'll need to look into making the old value of
331 * the array element available via CaseTestExpr, as is done by
332 * ExecEvalFieldStore. This is not needed now but will be needed to
333 * support arrays of composite types; in an assignment to a field of
334 * an array member, the parser would generate a FieldStore that
335 * expects to fetch its input tuple via CaseTestExpr.
337 sourceData = ExecEvalExpr(astate->refassgnexpr,
343 * For an assignment to a fixed-length array type, both the original
344 * array and the value to be assigned into it must be non-NULL, else
345 * we punt and return the original array.
347 if (astate->refattrlength > 0) /* fixed-length array? */
348 if (eisnull || *isNull)
349 return PointerGetDatum(array_source);
352 * For assignment to varlena arrays, we handle a NULL original array
353 * by substituting an empty (zero-dimensional) array; insertion of the
354 * new element will result in a singleton array value. It does not
355 * matter whether the new element is NULL.
359 array_source = construct_empty_array(arrayRef->refelemtype);
364 resultArray = array_set(array_source, i,
368 astate->refattrlength,
369 astate->refelemlength,
370 astate->refelembyval,
371 astate->refelemalign);
373 resultArray = array_set_slice(array_source, i,
374 upper.indx, lower.indx,
375 (ArrayType *) DatumGetPointer(sourceData),
377 astate->refattrlength,
378 astate->refelemlength,
379 astate->refelembyval,
380 astate->refelemalign);
381 return PointerGetDatum(resultArray);
385 return array_ref(array_source, i, upper.indx,
386 astate->refattrlength,
387 astate->refelemlength,
388 astate->refelembyval,
389 astate->refelemalign,
393 resultArray = array_get_slice(array_source, i,
394 upper.indx, lower.indx,
395 astate->refattrlength,
396 astate->refelemlength,
397 astate->refelembyval,
398 astate->refelemalign);
399 return PointerGetDatum(resultArray);
404 /* ----------------------------------------------------------------
407 * Returns a Datum whose value is the value of the precomputed
408 * aggregate found in the given expression context.
409 * ----------------------------------------------------------------
412 ExecEvalAggref(AggrefExprState *aggref, ExprContext *econtext,
413 bool *isNull, ExprDoneCond *isDone)
416 *isDone = ExprSingleResult;
418 if (econtext->ecxt_aggvalues == NULL) /* safety check */
419 elog(ERROR, "no aggregates in this expression context");
421 *isNull = econtext->ecxt_aggnulls[aggref->aggno];
422 return econtext->ecxt_aggvalues[aggref->aggno];
425 /* ----------------------------------------------------------------
428 * Returns a Datum whose value is the value of a range
429 * variable with respect to given expression context.
431 * Note: ExecEvalVar is executed only the first time through in a given plan;
432 * it changes the ExprState's function pointer to pass control directly to
433 * ExecEvalScalarVar or ExecEvalWholeRowVar after making one-time checks.
434 * ----------------------------------------------------------------
437 ExecEvalVar(ExprState *exprstate, ExprContext *econtext,
438 bool *isNull, ExprDoneCond *isDone)
440 Var *variable = (Var *) exprstate->expr;
441 TupleTableSlot *slot;
445 *isDone = ExprSingleResult;
448 * Get the input slot and attribute number we want
450 * The asserts check that references to system attributes only appear at
451 * the level of a relation scan; at higher levels, system attributes must
452 * be treated as ordinary variables (since we no longer have access to the
455 attnum = variable->varattno;
457 switch (variable->varno)
459 case INNER: /* get the tuple from the inner node */
460 slot = econtext->ecxt_innertuple;
464 case OUTER: /* get the tuple from the outer node */
465 slot = econtext->ecxt_outertuple;
469 default: /* get the tuple from the relation being
471 slot = econtext->ecxt_scantuple;
475 if (attnum != InvalidAttrNumber)
478 * Scalar variable case.
480 * If it's a user attribute, check validity (bogus system attnums will
481 * be caught inside slot_getattr). What we have to check for here
482 * is the possibility of an attribute having been changed in type
483 * since the plan tree was created. Ideally the plan would get
484 * invalidated and not re-used, but until that day arrives, we need
485 * defenses. Fortunately it's sufficient to check once on the first
488 * Note: we allow a reference to a dropped attribute. slot_getattr
489 * will force a NULL result in such cases.
491 * Note: ideally we'd check typmod as well as typid, but that seems
492 * impractical at the moment: in many cases the tupdesc will have
493 * been generated by ExecTypeFromTL(), and that can't guarantee to
494 * generate an accurate typmod in all cases, because some expression
495 * node types don't carry typmod.
499 TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
500 Form_pg_attribute attr;
502 if (attnum > slot_tupdesc->natts) /* should never happen */
503 elog(ERROR, "attribute number %d exceeds number of columns %d",
504 attnum, slot_tupdesc->natts);
506 attr = slot_tupdesc->attrs[attnum - 1];
508 /* can't check type if dropped, since atttypid is probably 0 */
509 if (!attr->attisdropped)
511 if (variable->vartype != attr->atttypid)
513 (errmsg("attribute %d has wrong type", attnum),
514 errdetail("Table has type %s, but query expects %s.",
515 format_type_be(attr->atttypid),
516 format_type_be(variable->vartype))));
520 /* Skip the checking on future executions of node */
521 exprstate->evalfunc = ExecEvalScalarVar;
523 /* Fetch the value from the slot */
524 return slot_getattr(slot, attnum, isNull);
529 * Whole-row variable.
531 * If it's a RECORD Var, we'll use the slot's type ID info. It's
532 * likely that the slot's type is also RECORD; if so, make sure it's
533 * been "blessed", so that the Datum can be interpreted later.
535 * If the Var identifies a named composite type, we must check that
536 * the actual tuple type is compatible with it.
538 TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
540 if (variable->vartype == RECORDOID)
542 if (slot_tupdesc->tdtypeid == RECORDOID &&
543 slot_tupdesc->tdtypmod < 0)
544 assign_record_type_typmod(slot_tupdesc);
548 TupleDesc var_tupdesc;
552 * We really only care about number of attributes and data type.
553 * Also, we can ignore type mismatch on columns that are dropped
554 * in the destination type, so long as the physical storage
555 * matches. This is helpful in some cases involving out-of-date
558 var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1);
560 if (var_tupdesc->natts != slot_tupdesc->natts)
562 (errcode(ERRCODE_DATATYPE_MISMATCH),
563 errmsg("table row type and query-specified row type do not match"),
564 errdetail("Table row contains %d attributes, but query expects %d.",
565 slot_tupdesc->natts, var_tupdesc->natts)));
567 for (i = 0; i < var_tupdesc->natts; i++)
569 Form_pg_attribute vattr = var_tupdesc->attrs[i];
570 Form_pg_attribute sattr = slot_tupdesc->attrs[i];
572 if (vattr->atttypid == sattr->atttypid)
573 continue; /* no worries */
574 if (!vattr->attisdropped)
576 (errcode(ERRCODE_DATATYPE_MISMATCH),
577 errmsg("table row type and query-specified row type do not match"),
578 errdetail("Table has type %s at ordinal position %d, but query expects %s.",
579 format_type_be(sattr->atttypid),
581 format_type_be(vattr->atttypid))));
583 if (vattr->attlen != sattr->attlen ||
584 vattr->attalign != sattr->attalign)
586 (errcode(ERRCODE_DATATYPE_MISMATCH),
587 errmsg("table row type and query-specified row type do not match"),
588 errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
592 ReleaseTupleDesc(var_tupdesc);
595 /* Skip the checking on future executions of node */
596 exprstate->evalfunc = ExecEvalWholeRowVar;
598 /* Fetch the value */
599 return ExecEvalWholeRowVar(exprstate, econtext, isNull, isDone);
603 /* ----------------------------------------------------------------
606 * Returns a Datum for a scalar variable.
607 * ----------------------------------------------------------------
610 ExecEvalScalarVar(ExprState *exprstate, ExprContext *econtext,
611 bool *isNull, ExprDoneCond *isDone)
613 Var *variable = (Var *) exprstate->expr;
614 TupleTableSlot *slot;
618 *isDone = ExprSingleResult;
620 /* Get the input slot and attribute number we want */
621 switch (variable->varno)
623 case INNER: /* get the tuple from the inner node */
624 slot = econtext->ecxt_innertuple;
627 case OUTER: /* get the tuple from the outer node */
628 slot = econtext->ecxt_outertuple;
631 default: /* get the tuple from the relation being
633 slot = econtext->ecxt_scantuple;
637 attnum = variable->varattno;
639 /* Fetch the value from the slot */
640 return slot_getattr(slot, attnum, isNull);
643 /* ----------------------------------------------------------------
644 * ExecEvalWholeRowVar
646 * Returns a Datum for a whole-row variable.
647 * ----------------------------------------------------------------
650 ExecEvalWholeRowVar(ExprState *exprstate, ExprContext *econtext,
651 bool *isNull, ExprDoneCond *isDone)
653 Var *variable = (Var *) exprstate->expr;
654 TupleTableSlot *slot = econtext->ecxt_scantuple;
657 HeapTupleHeader dtuple;
660 *isDone = ExprSingleResult;
663 tuple = ExecFetchSlotTuple(slot);
664 tupleDesc = slot->tts_tupleDescriptor;
667 * We have to make a copy of the tuple so we can safely insert the Datum
668 * overhead fields, which are not set in on-disk tuples.
670 dtuple = (HeapTupleHeader) palloc(tuple->t_len);
671 memcpy((char *) dtuple, (char *) tuple->t_data, tuple->t_len);
673 HeapTupleHeaderSetDatumLength(dtuple, tuple->t_len);
676 * If the Var identifies a named composite type, label the tuple with that
677 * type; otherwise use what is in the tupleDesc.
679 if (variable->vartype != RECORDOID)
681 HeapTupleHeaderSetTypeId(dtuple, variable->vartype);
682 HeapTupleHeaderSetTypMod(dtuple, variable->vartypmod);
686 HeapTupleHeaderSetTypeId(dtuple, tupleDesc->tdtypeid);
687 HeapTupleHeaderSetTypMod(dtuple, tupleDesc->tdtypmod);
690 return PointerGetDatum(dtuple);
693 /* ----------------------------------------------------------------
696 * Returns the value of a constant.
698 * Note that for pass-by-ref datatypes, we return a pointer to the
699 * actual constant node. This is one of the reasons why functions
700 * must treat their input arguments as read-only.
701 * ----------------------------------------------------------------
704 ExecEvalConst(ExprState *exprstate, ExprContext *econtext,
705 bool *isNull, ExprDoneCond *isDone)
707 Const *con = (Const *) exprstate->expr;
710 *isDone = ExprSingleResult;
712 *isNull = con->constisnull;
713 return con->constvalue;
716 /* ----------------------------------------------------------------
719 * Returns the value of a parameter. A param node contains
720 * something like ($.name) and the expression context contains
721 * the current parameter bindings (name = "sam") (age = 34)...
722 * so our job is to find and return the appropriate datum ("sam").
723 * ----------------------------------------------------------------
726 ExecEvalParam(ExprState *exprstate, ExprContext *econtext,
727 bool *isNull, ExprDoneCond *isDone)
729 Param *expression = (Param *) exprstate->expr;
730 int thisParamId = expression->paramid;
733 *isDone = ExprSingleResult;
735 if (expression->paramkind == PARAM_EXEC)
738 * PARAM_EXEC params (internal executor parameters) are stored in the
739 * ecxt_param_exec_vals array, and can be accessed by array index.
743 prm = &(econtext->ecxt_param_exec_vals[thisParamId]);
744 if (prm->execPlan != NULL)
746 /* Parameter not evaluated yet, so go do it */
747 ExecSetParamPlan(prm->execPlan, econtext);
748 /* ExecSetParamPlan should have processed this param... */
749 Assert(prm->execPlan == NULL);
751 *isNull = prm->isnull;
757 * PARAM_EXTERN parameters must be sought in ecxt_param_list_info.
759 ParamListInfo paramInfo = econtext->ecxt_param_list_info;
761 Assert(expression->paramkind == PARAM_EXTERN);
763 thisParamId > 0 && thisParamId <= paramInfo->numParams)
765 ParamExternData *prm = ¶mInfo->params[thisParamId - 1];
767 if (OidIsValid(prm->ptype))
769 Assert(prm->ptype == expression->paramtype);
770 *isNull = prm->isnull;
775 (errcode(ERRCODE_UNDEFINED_OBJECT),
776 errmsg("no value found for parameter %d", thisParamId)));
777 return (Datum) 0; /* keep compiler quiet */
782 /* ----------------------------------------------------------------
783 * ExecEvalOper / ExecEvalFunc support routines
784 * ----------------------------------------------------------------
791 * These functions return the value of the requested attribute
792 * out of the given tuple Datum.
793 * C functions which take a tuple as an argument are expected
794 * to use these. Ex: overpaid(EMP) might call GetAttributeByNum().
795 * Note: these are actually rather slow because they do a typcache
796 * lookup on each call.
799 GetAttributeByNum(HeapTupleHeader tuple,
807 HeapTupleData tmptup;
809 if (!AttributeNumberIsValid(attrno))
810 elog(ERROR, "invalid attribute number %d", attrno);
813 elog(ERROR, "a NULL isNull pointer was passed");
817 /* Kinda bogus but compatible with old behavior... */
822 tupType = HeapTupleHeaderGetTypeId(tuple);
823 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
824 tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
827 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
828 * the fields in the struct just in case user tries to inspect system
831 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
832 ItemPointerSetInvalid(&(tmptup.t_self));
833 tmptup.t_tableOid = InvalidOid;
834 tmptup.t_data = tuple;
836 result = heap_getattr(&tmptup,
841 ReleaseTupleDesc(tupDesc);
847 GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
854 HeapTupleData tmptup;
858 elog(ERROR, "invalid attribute name");
861 elog(ERROR, "a NULL isNull pointer was passed");
865 /* Kinda bogus but compatible with old behavior... */
870 tupType = HeapTupleHeaderGetTypeId(tuple);
871 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
872 tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
874 attrno = InvalidAttrNumber;
875 for (i = 0; i < tupDesc->natts; i++)
877 if (namestrcmp(&(tupDesc->attrs[i]->attname), attname) == 0)
879 attrno = tupDesc->attrs[i]->attnum;
884 if (attrno == InvalidAttrNumber)
885 elog(ERROR, "attribute \"%s\" does not exist", attname);
888 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
889 * the fields in the struct just in case user tries to inspect system
892 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
893 ItemPointerSetInvalid(&(tmptup.t_self));
894 tmptup.t_tableOid = InvalidOid;
895 tmptup.t_data = tuple;
897 result = heap_getattr(&tmptup,
902 ReleaseTupleDesc(tupDesc);
908 * init_fcache - initialize a FuncExprState node during first use
911 init_fcache(Oid foid, FuncExprState *fcache, MemoryContext fcacheCxt)
915 /* Check permission to call function */
916 aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE);
917 if (aclresult != ACLCHECK_OK)
918 aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(foid));
921 * Safety check on nargs. Under normal circumstances this should never
922 * fail, as parser should check sooner. But possibly it might fail if
923 * server has been compiled with FUNC_MAX_ARGS smaller than some functions
924 * declared in pg_proc?
926 if (list_length(fcache->args) > FUNC_MAX_ARGS)
928 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
929 errmsg("cannot pass more than %d arguments to a function",
932 /* Set up the primary fmgr lookup information */
933 fmgr_info_cxt(foid, &(fcache->func), fcacheCxt);
935 /* Initialize additional info */
936 fcache->setArgsValid = false;
937 fcache->shutdown_reg = false;
938 fcache->func.fn_expr = (Node *) fcache->xprstate.expr;
942 * callback function in case a FuncExpr returning a set needs to be shut down
943 * before it has been run to completion
946 ShutdownFuncExpr(Datum arg)
948 FuncExprState *fcache = (FuncExprState *) DatumGetPointer(arg);
950 /* Clear any active set-argument state */
951 fcache->setArgsValid = false;
953 /* execUtils will deregister the callback... */
954 fcache->shutdown_reg = false;
958 * get_cached_rowtype: utility function to lookup a rowtype tupdesc
960 * type_id, typmod: identity of the rowtype
961 * cache_field: where to cache the TupleDesc pointer in expression state node
962 * (field must be initialized to NULL)
963 * econtext: expression context we are executing in
965 * NOTE: because the shutdown callback will be called during plan rescan,
966 * must be prepared to re-do this during any node execution; cannot call
967 * just once during expression initialization
970 get_cached_rowtype(Oid type_id, int32 typmod,
971 TupleDesc *cache_field, ExprContext *econtext)
973 TupleDesc tupDesc = *cache_field;
975 /* Do lookup if no cached value or if requested type changed */
976 if (tupDesc == NULL ||
977 type_id != tupDesc->tdtypeid ||
978 typmod != tupDesc->tdtypmod)
980 tupDesc = lookup_rowtype_tupdesc(type_id, typmod);
984 /* Release old tupdesc; but callback is already registered */
985 ReleaseTupleDesc(*cache_field);
989 /* Need to register shutdown callback to release tupdesc */
990 RegisterExprContextCallback(econtext,
991 ShutdownTupleDescRef,
992 PointerGetDatum(cache_field));
994 *cache_field = tupDesc;
1000 * Callback function to release a tupdesc refcount at expression tree shutdown
1003 ShutdownTupleDescRef(Datum arg)
1005 TupleDesc *cache_field = (TupleDesc *) DatumGetPointer(arg);
1008 ReleaseTupleDesc(*cache_field);
1009 *cache_field = NULL;
1013 * Evaluate arguments for a function.
1016 ExecEvalFuncArgs(FunctionCallInfo fcinfo,
1018 ExprContext *econtext)
1020 ExprDoneCond argIsDone;
1024 argIsDone = ExprSingleResult; /* default assumption */
1027 foreach(arg, argList)
1029 ExprState *argstate = (ExprState *) lfirst(arg);
1030 ExprDoneCond thisArgIsDone;
1032 fcinfo->arg[i] = ExecEvalExpr(argstate,
1034 &fcinfo->argnull[i],
1037 if (thisArgIsDone != ExprSingleResult)
1040 * We allow only one argument to have a set value; we'd need much
1041 * more complexity to keep track of multiple set arguments (cf.
1042 * ExecTargetList) and it doesn't seem worth it.
1044 if (argIsDone != ExprSingleResult)
1046 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1047 errmsg("functions and operators can take at most one set argument")));
1048 argIsDone = thisArgIsDone;
1059 * ExecMakeFunctionResult
1061 * Evaluate the arguments to a function and then the function itself.
1064 ExecMakeFunctionResult(FuncExprState *fcache,
1065 ExprContext *econtext,
1067 ExprDoneCond *isDone)
1069 List *arguments = fcache->args;
1071 FunctionCallInfoData fcinfo;
1072 ReturnSetInfo rsinfo; /* for functions returning sets */
1073 ExprDoneCond argDone;
1077 /* Guard against stack overflow due to overly complex expressions */
1078 check_stack_depth();
1081 * arguments is a list of expressions to evaluate before passing to the
1082 * function manager. We skip the evaluation if it was already done in the
1083 * previous call (ie, we are continuing the evaluation of a set-valued
1084 * function). Otherwise, collect the current argument values into fcinfo.
1086 if (!fcache->setArgsValid)
1088 /* Need to prep callinfo structure */
1089 InitFunctionCallInfoData(fcinfo, &(fcache->func), 0, NULL, NULL);
1090 argDone = ExecEvalFuncArgs(&fcinfo, arguments, econtext);
1091 if (argDone == ExprEndResult)
1093 /* input is an empty set, so return an empty set. */
1096 *isDone = ExprEndResult;
1099 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1100 errmsg("set-valued function called in context that cannot accept a set")));
1103 hasSetArg = (argDone != ExprSingleResult);
1107 /* Copy callinfo from previous evaluation */
1108 memcpy(&fcinfo, &fcache->setArgs, sizeof(fcinfo));
1109 hasSetArg = fcache->setHasSetArg;
1110 /* Reset flag (we may set it again below) */
1111 fcache->setArgsValid = false;
1115 * If function returns set, prepare a resultinfo node for communication
1117 if (fcache->func.fn_retset)
1119 fcinfo.resultinfo = (Node *) &rsinfo;
1120 rsinfo.type = T_ReturnSetInfo;
1121 rsinfo.econtext = econtext;
1122 rsinfo.expectedDesc = NULL;
1123 rsinfo.allowedModes = (int) SFRM_ValuePerCall;
1124 rsinfo.returnMode = SFRM_ValuePerCall;
1125 /* isDone is filled below */
1126 rsinfo.setResult = NULL;
1127 rsinfo.setDesc = NULL;
1131 * now return the value gotten by calling the function manager, passing
1132 * the function the evaluated parameter values.
1134 if (fcache->func.fn_retset || hasSetArg)
1137 * We need to return a set result. Complain if caller not ready to
1142 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1143 errmsg("set-valued function called in context that cannot accept a set")));
1146 * This loop handles the situation where we have both a set argument
1147 * and a set-valued function. Once we have exhausted the function's
1148 * value(s) for a particular argument value, we have to get the next
1149 * argument value and start the function over again. We might have to
1150 * do it more than once, if the function produces an empty result set
1151 * for a particular input value.
1156 * If function is strict, and there are any NULL arguments, skip
1157 * calling the function (at least for this set of args).
1161 if (fcache->func.fn_strict)
1163 for (i = 0; i < fcinfo.nargs; i++)
1165 if (fcinfo.argnull[i])
1175 fcinfo.isnull = false;
1176 rsinfo.isDone = ExprSingleResult;
1177 result = FunctionCallInvoke(&fcinfo);
1178 *isNull = fcinfo.isnull;
1179 *isDone = rsinfo.isDone;
1185 *isDone = ExprEndResult;
1188 if (*isDone != ExprEndResult)
1191 * Got a result from current argument. If function itself
1192 * returns set, save the current argument values to re-use on
1195 if (fcache->func.fn_retset && *isDone == ExprMultipleResult)
1197 memcpy(&fcache->setArgs, &fcinfo, sizeof(fcinfo));
1198 fcache->setHasSetArg = hasSetArg;
1199 fcache->setArgsValid = true;
1200 /* Register cleanup callback if we didn't already */
1201 if (!fcache->shutdown_reg)
1203 RegisterExprContextCallback(econtext,
1205 PointerGetDatum(fcache));
1206 fcache->shutdown_reg = true;
1211 * Make sure we say we are returning a set, even if the
1212 * function itself doesn't return sets.
1215 *isDone = ExprMultipleResult;
1219 /* Else, done with this argument */
1221 break; /* input not a set, so done */
1223 /* Re-eval args to get the next element of the input set */
1224 argDone = ExecEvalFuncArgs(&fcinfo, arguments, econtext);
1226 if (argDone != ExprMultipleResult)
1228 /* End of argument set, so we're done. */
1230 *isDone = ExprEndResult;
1236 * If we reach here, loop around to run the function on the new
1244 * Non-set case: much easier.
1246 * We change the ExprState function pointer to use the simpler
1247 * ExecMakeFunctionResultNoSets on subsequent calls. This amounts to
1248 * assuming that no argument can return a set if it didn't do so the
1251 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResultNoSets;
1254 *isDone = ExprSingleResult;
1257 * If function is strict, and there are any NULL arguments, skip
1258 * calling the function and return NULL.
1260 if (fcache->func.fn_strict)
1262 for (i = 0; i < fcinfo.nargs; i++)
1264 if (fcinfo.argnull[i])
1271 fcinfo.isnull = false;
1272 result = FunctionCallInvoke(&fcinfo);
1273 *isNull = fcinfo.isnull;
1280 * ExecMakeFunctionResultNoSets
1282 * Simplified version of ExecMakeFunctionResult that can only handle
1283 * non-set cases. Hand-tuned for speed.
1286 ExecMakeFunctionResultNoSets(FuncExprState *fcache,
1287 ExprContext *econtext,
1289 ExprDoneCond *isDone)
1293 FunctionCallInfoData fcinfo;
1296 /* Guard against stack overflow due to overly complex expressions */
1297 check_stack_depth();
1300 *isDone = ExprSingleResult;
1302 /* inlined, simplified version of ExecEvalFuncArgs */
1304 foreach(arg, fcache->args)
1306 ExprState *argstate = (ExprState *) lfirst(arg);
1308 fcinfo.arg[i] = ExecEvalExpr(argstate,
1315 InitFunctionCallInfoData(fcinfo, &(fcache->func), i, NULL, NULL);
1318 * If function is strict, and there are any NULL arguments, skip calling
1319 * the function and return NULL.
1321 if (fcache->func.fn_strict)
1325 if (fcinfo.argnull[i])
1332 /* fcinfo.isnull = false; */ /* handled by InitFunctionCallInfoData */
1333 result = FunctionCallInvoke(&fcinfo);
1334 *isNull = fcinfo.isnull;
1341 * ExecMakeTableFunctionResult
1343 * Evaluate a table function, producing a materialized result in a Tuplestore
1344 * object. *returnDesc is set to the tupledesc actually returned by the
1345 * function, or NULL if it didn't provide one.
1348 ExecMakeTableFunctionResult(ExprState *funcexpr,
1349 ExprContext *econtext,
1350 TupleDesc expectedDesc,
1351 TupleDesc *returnDesc)
1353 Tuplestorestate *tupstore = NULL;
1354 TupleDesc tupdesc = NULL;
1357 bool returnsSet = false;
1358 FunctionCallInfoData fcinfo;
1359 ReturnSetInfo rsinfo;
1360 HeapTupleData tmptup;
1361 MemoryContext callerContext;
1362 MemoryContext oldcontext;
1363 bool direct_function_call;
1364 bool first_time = true;
1366 callerContext = CurrentMemoryContext;
1368 funcrettype = exprType((Node *) funcexpr->expr);
1370 returnsTuple = type_is_rowtype(funcrettype);
1373 * Prepare a resultinfo node for communication. We always do this even if
1374 * not expecting a set result, so that we can pass expectedDesc. In the
1375 * generic-expression case, the expression doesn't actually get to see the
1376 * resultinfo, but set it up anyway because we use some of the fields as
1377 * our own state variables.
1379 InitFunctionCallInfoData(fcinfo, NULL, 0, NULL, (Node *) &rsinfo);
1380 rsinfo.type = T_ReturnSetInfo;
1381 rsinfo.econtext = econtext;
1382 rsinfo.expectedDesc = expectedDesc;
1383 rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize);
1384 rsinfo.returnMode = SFRM_ValuePerCall;
1385 /* isDone is filled below */
1386 rsinfo.setResult = NULL;
1387 rsinfo.setDesc = NULL;
1390 * Normally the passed expression tree will be a FuncExprState, since the
1391 * grammar only allows a function call at the top level of a table
1392 * function reference. However, if the function doesn't return set then
1393 * the planner might have replaced the function call via constant-folding
1394 * or inlining. So if we see any other kind of expression node, execute
1395 * it via the general ExecEvalExpr() code; the only difference is that we
1396 * don't get a chance to pass a special ReturnSetInfo to any functions
1397 * buried in the expression.
1399 if (funcexpr && IsA(funcexpr, FuncExprState) &&
1400 IsA(funcexpr->expr, FuncExpr))
1402 FuncExprState *fcache = (FuncExprState *) funcexpr;
1403 ExprDoneCond argDone;
1406 * This path is similar to ExecMakeFunctionResult.
1408 direct_function_call = true;
1411 * Initialize function cache if first time through
1413 if (fcache->func.fn_oid == InvalidOid)
1415 FuncExpr *func = (FuncExpr *) fcache->xprstate.expr;
1417 init_fcache(func->funcid, fcache, econtext->ecxt_per_query_memory);
1419 returnsSet = fcache->func.fn_retset;
1422 * Evaluate the function's argument list.
1424 * Note: ideally, we'd do this in the per-tuple context, but then the
1425 * argument values would disappear when we reset the context in the
1426 * inner loop. So do it in caller context. Perhaps we should make a
1427 * separate context just to hold the evaluated arguments?
1429 fcinfo.flinfo = &(fcache->func);
1430 argDone = ExecEvalFuncArgs(&fcinfo, fcache->args, econtext);
1431 /* We don't allow sets in the arguments of the table function */
1432 if (argDone != ExprSingleResult)
1434 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1435 errmsg("set-valued function called in context that cannot accept a set")));
1438 * If function is strict, and there are any NULL arguments, skip
1439 * calling the function and act like it returned NULL (or an empty
1440 * set, in the returns-set case).
1442 if (fcache->func.fn_strict)
1446 for (i = 0; i < fcinfo.nargs; i++)
1448 if (fcinfo.argnull[i])
1449 goto no_function_result;
1455 /* Treat funcexpr as a generic expression */
1456 direct_function_call = false;
1460 * Switch to short-lived context for calling the function or expression.
1462 MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1465 * Loop to handle the ValuePerCall protocol (which is also the same
1466 * behavior needed in the generic ExecEvalExpr path).
1473 CHECK_FOR_INTERRUPTS();
1476 * reset per-tuple memory context before each call of the function or
1477 * expression. This cleans up any local memory the function may leak
1480 ResetExprContext(econtext);
1482 /* Call the function or expression one time */
1483 if (direct_function_call)
1485 fcinfo.isnull = false;
1486 rsinfo.isDone = ExprSingleResult;
1487 result = FunctionCallInvoke(&fcinfo);
1491 result = ExecEvalExpr(funcexpr, econtext,
1492 &fcinfo.isnull, &rsinfo.isDone);
1495 /* Which protocol does function want to use? */
1496 if (rsinfo.returnMode == SFRM_ValuePerCall)
1499 * Check for end of result set.
1501 if (rsinfo.isDone == ExprEndResult)
1505 * Can't do anything very useful with NULL rowtype values. For a
1506 * function returning set, we consider this a protocol violation
1507 * (but another alternative would be to just ignore the result and
1508 * "continue" to get another row). For a function not returning
1509 * set, we fall out of the loop; we'll cons up an all-nulls result
1512 if (returnsTuple && fcinfo.isnull)
1517 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1518 errmsg("function returning set of rows cannot return null value")));
1522 * If first time through, build tupdesc and tuplestore for result
1526 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1530 * Use the type info embedded in the rowtype Datum to look
1531 * up the needed tupdesc. Make a copy for the query.
1535 td = DatumGetHeapTupleHeader(result);
1536 tupdesc = lookup_rowtype_tupdesc_copy(HeapTupleHeaderGetTypeId(td),
1537 HeapTupleHeaderGetTypMod(td));
1542 * Scalar type, so make a single-column descriptor
1544 tupdesc = CreateTemplateTupleDesc(1, false);
1545 TupleDescInitEntry(tupdesc,
1552 tupstore = tuplestore_begin_heap(true, false, work_mem);
1553 MemoryContextSwitchTo(oldcontext);
1554 rsinfo.setResult = tupstore;
1555 rsinfo.setDesc = tupdesc;
1559 * Store current resultset item.
1565 td = DatumGetHeapTupleHeader(result);
1568 * tuplestore_puttuple needs a HeapTuple not a bare
1569 * HeapTupleHeader, but it doesn't need all the fields.
1571 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1577 tuple = heap_form_tuple(tupdesc, &result, &fcinfo.isnull);
1580 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1581 tuplestore_puttuple(tupstore, tuple);
1582 MemoryContextSwitchTo(oldcontext);
1587 if (rsinfo.isDone != ExprMultipleResult)
1590 else if (rsinfo.returnMode == SFRM_Materialize)
1592 /* check we're on the same page as the function author */
1593 if (!first_time || rsinfo.isDone != ExprSingleResult)
1595 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1596 errmsg("table-function protocol for materialize mode was not followed")));
1597 /* Done evaluating the set result */
1602 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1603 errmsg("unrecognized table-function returnMode: %d",
1604 (int) rsinfo.returnMode)));
1612 * If we got nothing from the function (ie, an empty-set or NULL result),
1613 * we have to create the tuplestore to return, and if it's a
1614 * non-set-returning function then insert a single all-nulls row.
1616 if (rsinfo.setResult == NULL)
1618 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1619 tupstore = tuplestore_begin_heap(true, false, work_mem);
1620 rsinfo.setResult = tupstore;
1623 int natts = expectedDesc->natts;
1628 MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1629 nulldatums = (Datum *) palloc0(natts * sizeof(Datum));
1630 nullflags = (bool *) palloc(natts * sizeof(bool));
1631 memset(nullflags, true, natts * sizeof(bool));
1632 tuple = heap_form_tuple(expectedDesc, nulldatums, nullflags);
1633 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1634 tuplestore_puttuple(tupstore, tuple);
1638 MemoryContextSwitchTo(callerContext);
1640 /* The returned pointers are those in rsinfo */
1641 *returnDesc = rsinfo.setDesc;
1642 return rsinfo.setResult;
1646 /* ----------------------------------------------------------------
1650 * Evaluate the functional result of a list of arguments by calling the
1652 * ----------------------------------------------------------------
1655 /* ----------------------------------------------------------------
1657 * ----------------------------------------------------------------
1660 ExecEvalFunc(FuncExprState *fcache,
1661 ExprContext *econtext,
1663 ExprDoneCond *isDone)
1665 /* This is called only the first time through */
1666 FuncExpr *func = (FuncExpr *) fcache->xprstate.expr;
1668 /* Initialize function lookup info */
1669 init_fcache(func->funcid, fcache, econtext->ecxt_per_query_memory);
1671 /* Go directly to ExecMakeFunctionResult on subsequent uses */
1672 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
1674 return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
1677 /* ----------------------------------------------------------------
1679 * ----------------------------------------------------------------
1682 ExecEvalOper(FuncExprState *fcache,
1683 ExprContext *econtext,
1685 ExprDoneCond *isDone)
1687 /* This is called only the first time through */
1688 OpExpr *op = (OpExpr *) fcache->xprstate.expr;
1690 /* Initialize function lookup info */
1691 init_fcache(op->opfuncid, fcache, econtext->ecxt_per_query_memory);
1693 /* Go directly to ExecMakeFunctionResult on subsequent uses */
1694 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
1696 return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
1699 /* ----------------------------------------------------------------
1702 * IS DISTINCT FROM must evaluate arguments to determine whether
1703 * they are NULL; if either is NULL then the result is already
1704 * known. If neither is NULL, then proceed to evaluate the
1705 * function. Note that this is *always* derived from the equals
1706 * operator, but since we need special processing of the arguments
1707 * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
1708 * ----------------------------------------------------------------
1711 ExecEvalDistinct(FuncExprState *fcache,
1712 ExprContext *econtext,
1714 ExprDoneCond *isDone)
1717 FunctionCallInfoData fcinfo;
1718 ExprDoneCond argDone;
1721 /* Set default values for result flags: non-null, not a set result */
1724 *isDone = ExprSingleResult;
1727 * Initialize function cache if first time through
1729 if (fcache->func.fn_oid == InvalidOid)
1731 DistinctExpr *op = (DistinctExpr *) fcache->xprstate.expr;
1733 init_fcache(op->opfuncid, fcache, econtext->ecxt_per_query_memory);
1734 Assert(!fcache->func.fn_retset);
1738 * extract info from fcache
1740 argList = fcache->args;
1742 /* Need to prep callinfo structure */
1743 InitFunctionCallInfoData(fcinfo, &(fcache->func), 0, NULL, NULL);
1744 argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
1745 if (argDone != ExprSingleResult)
1747 (errcode(ERRCODE_DATATYPE_MISMATCH),
1748 errmsg("IS DISTINCT FROM does not support set arguments")));
1749 Assert(fcinfo.nargs == 2);
1751 if (fcinfo.argnull[0] && fcinfo.argnull[1])
1753 /* Both NULL? Then is not distinct... */
1754 result = BoolGetDatum(FALSE);
1756 else if (fcinfo.argnull[0] || fcinfo.argnull[1])
1758 /* Only one is NULL? Then is distinct... */
1759 result = BoolGetDatum(TRUE);
1763 fcinfo.isnull = false;
1764 result = FunctionCallInvoke(&fcinfo);
1765 *isNull = fcinfo.isnull;
1766 /* Must invert result of "=" */
1767 result = BoolGetDatum(!DatumGetBool(result));
1774 * ExecEvalScalarArrayOp
1776 * Evaluate "scalar op ANY/ALL (array)". The operator always yields boolean,
1777 * and we combine the results across all array elements using OR and AND
1778 * (for ANY and ALL respectively). Of course we short-circuit as soon as
1779 * the result is known.
1782 ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
1783 ExprContext *econtext,
1784 bool *isNull, ExprDoneCond *isDone)
1786 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) sstate->fxprstate.xprstate.expr;
1787 bool useOr = opexpr->useOr;
1792 FunctionCallInfoData fcinfo;
1793 ExprDoneCond argDone;
1802 /* Set default values for result flags: non-null, not a set result */
1805 *isDone = ExprSingleResult;
1808 * Initialize function cache if first time through
1810 if (sstate->fxprstate.func.fn_oid == InvalidOid)
1812 init_fcache(opexpr->opfuncid, &sstate->fxprstate,
1813 econtext->ecxt_per_query_memory);
1814 Assert(!sstate->fxprstate.func.fn_retset);
1817 /* Need to prep callinfo structure */
1818 InitFunctionCallInfoData(fcinfo, &(sstate->fxprstate.func), 0, NULL, NULL);
1819 argDone = ExecEvalFuncArgs(&fcinfo, sstate->fxprstate.args, econtext);
1820 if (argDone != ExprSingleResult)
1822 (errcode(ERRCODE_DATATYPE_MISMATCH),
1823 errmsg("op ANY/ALL (array) does not support set arguments")));
1824 Assert(fcinfo.nargs == 2);
1827 * If the array is NULL then we return NULL --- it's not very meaningful
1828 * to do anything else, even if the operator isn't strict.
1830 if (fcinfo.argnull[1])
1835 /* Else okay to fetch and detoast the array */
1836 arr = DatumGetArrayTypeP(fcinfo.arg[1]);
1839 * If the array is empty, we return either FALSE or TRUE per the useOr
1840 * flag. This is correct even if the scalar is NULL; since we would
1841 * evaluate the operator zero times, it matters not whether it would want
1844 nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
1846 return BoolGetDatum(!useOr);
1849 * If the scalar is NULL, and the function is strict, return NULL; no
1850 * point in iterating the loop.
1852 if (fcinfo.argnull[0] && sstate->fxprstate.func.fn_strict)
1859 * We arrange to look up info about the element type only once per series
1860 * of calls, assuming the element type doesn't change underneath us.
1862 if (sstate->element_type != ARR_ELEMTYPE(arr))
1864 get_typlenbyvalalign(ARR_ELEMTYPE(arr),
1868 sstate->element_type = ARR_ELEMTYPE(arr);
1870 typlen = sstate->typlen;
1871 typbyval = sstate->typbyval;
1872 typalign = sstate->typalign;
1874 result = BoolGetDatum(!useOr);
1877 /* Loop over the array elements */
1878 s = (char *) ARR_DATA_PTR(arr);
1879 bitmap = ARR_NULLBITMAP(arr);
1882 for (i = 0; i < nitems; i++)
1887 /* Get array element, checking for NULL */
1888 if (bitmap && (*bitmap & bitmask) == 0)
1890 fcinfo.arg[1] = (Datum) 0;
1891 fcinfo.argnull[1] = true;
1895 elt = fetch_att(s, typbyval, typlen);
1896 s = att_addlength(s, typlen, PointerGetDatum(s));
1897 s = (char *) att_align(s, typalign);
1898 fcinfo.arg[1] = elt;
1899 fcinfo.argnull[1] = false;
1902 /* Call comparison function */
1903 if (fcinfo.argnull[1] && sstate->fxprstate.func.fn_strict)
1905 fcinfo.isnull = true;
1906 thisresult = (Datum) 0;
1910 fcinfo.isnull = false;
1911 thisresult = FunctionCallInvoke(&fcinfo);
1914 /* Combine results per OR or AND semantics */
1919 if (DatumGetBool(thisresult))
1921 result = BoolGetDatum(true);
1923 break; /* needn't look at any more elements */
1928 if (!DatumGetBool(thisresult))
1930 result = BoolGetDatum(false);
1932 break; /* needn't look at any more elements */
1936 /* advance bitmap pointer if any */
1940 if (bitmask == 0x100)
1948 *isNull = resultnull;
1952 /* ----------------------------------------------------------------
1957 * Evaluate boolean expressions, with appropriate short-circuiting.
1959 * The query planner reformulates clause expressions in the
1960 * qualification to conjunctive normal form. If we ever get
1961 * an AND to evaluate, we can be sure that it's not a top-level
1962 * clause in the qualification, but appears lower (as a function
1963 * argument, for example), or in the target list. Not that you
1964 * need to know this, mind you...
1965 * ----------------------------------------------------------------
1968 ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
1969 bool *isNull, ExprDoneCond *isDone)
1971 ExprState *clause = linitial(notclause->args);
1975 *isDone = ExprSingleResult;
1977 expr_value = ExecEvalExpr(clause, econtext, isNull, NULL);
1980 * if the expression evaluates to null, then we just cascade the null back
1981 * to whoever called us.
1987 * evaluation of 'not' is simple.. expr is false, then return 'true' and
1990 return BoolGetDatum(!DatumGetBool(expr_value));
1993 /* ----------------------------------------------------------------
1995 * ----------------------------------------------------------------
1998 ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
1999 bool *isNull, ExprDoneCond *isDone)
2001 List *clauses = orExpr->args;
2006 *isDone = ExprSingleResult;
2011 * If any of the clauses is TRUE, the OR result is TRUE regardless of the
2012 * states of the rest of the clauses, so we can stop evaluating and return
2013 * TRUE immediately. If none are TRUE and one or more is NULL, we return
2014 * NULL; otherwise we return FALSE. This makes sense when you interpret
2015 * NULL as "don't know": if we have a TRUE then the OR is TRUE even if we
2016 * aren't sure about some of the other inputs. If all the known inputs are
2017 * FALSE, but we have one or more "don't knows", then we have to report
2018 * that we "don't know" what the OR's result should be --- perhaps one of
2019 * the "don't knows" would have been TRUE if we'd known its value. Only
2020 * when all the inputs are known to be FALSE can we state confidently that
2021 * the OR's result is FALSE.
2023 foreach(clause, clauses)
2025 ExprState *clausestate = (ExprState *) lfirst(clause);
2028 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
2031 * if we have a non-null true result, then return it.
2034 AnyNull = true; /* remember we got a null */
2035 else if (DatumGetBool(clause_value))
2036 return clause_value;
2039 /* AnyNull is true if at least one clause evaluated to NULL */
2041 return BoolGetDatum(false);
2044 /* ----------------------------------------------------------------
2046 * ----------------------------------------------------------------
2049 ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
2050 bool *isNull, ExprDoneCond *isDone)
2052 List *clauses = andExpr->args;
2057 *isDone = ExprSingleResult;
2062 * If any of the clauses is FALSE, the AND result is FALSE regardless of
2063 * the states of the rest of the clauses, so we can stop evaluating and
2064 * return FALSE immediately. If none are FALSE and one or more is NULL,
2065 * we return NULL; otherwise we return TRUE. This makes sense when you
2066 * interpret NULL as "don't know", using the same sort of reasoning as for
2070 foreach(clause, clauses)
2072 ExprState *clausestate = (ExprState *) lfirst(clause);
2075 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
2078 * if we have a non-null false result, then return it.
2081 AnyNull = true; /* remember we got a null */
2082 else if (!DatumGetBool(clause_value))
2083 return clause_value;
2086 /* AnyNull is true if at least one clause evaluated to NULL */
2088 return BoolGetDatum(!AnyNull);
2091 /* ----------------------------------------------------------------
2092 * ExecEvalConvertRowtype
2094 * Evaluate a rowtype coercion operation. This may require
2095 * rearranging field positions.
2096 * ----------------------------------------------------------------
2099 ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
2100 ExprContext *econtext,
2101 bool *isNull, ExprDoneCond *isDone)
2103 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) cstate->xprstate.expr;
2106 HeapTupleHeader tuple;
2107 HeapTupleData tmptup;
2108 AttrNumber *attrMap;
2116 tupDatum = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
2118 /* this test covers the isDone exception too: */
2122 tuple = DatumGetHeapTupleHeader(tupDatum);
2124 /* Lookup tupdescs if first time through or after rescan */
2125 if (cstate->indesc == NULL)
2126 get_cached_rowtype(exprType((Node *) convert->arg), -1,
2127 &cstate->indesc, econtext);
2128 if (cstate->outdesc == NULL)
2129 get_cached_rowtype(convert->resulttype, -1,
2130 &cstate->outdesc, econtext);
2132 Assert(HeapTupleHeaderGetTypeId(tuple) == cstate->indesc->tdtypeid);
2133 Assert(HeapTupleHeaderGetTypMod(tuple) == cstate->indesc->tdtypmod);
2135 /* if first time through, initialize */
2136 if (cstate->attrMap == NULL)
2138 MemoryContext old_cxt;
2141 /* allocate state in long-lived memory context */
2142 old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
2144 /* prepare map from old to new attribute numbers */
2145 n = cstate->outdesc->natts;
2146 cstate->attrMap = (AttrNumber *) palloc0(n * sizeof(AttrNumber));
2147 for (i = 0; i < n; i++)
2149 Form_pg_attribute att = cstate->outdesc->attrs[i];
2155 if (att->attisdropped)
2156 continue; /* attrMap[i] is already 0 */
2157 attname = NameStr(att->attname);
2158 atttypid = att->atttypid;
2159 atttypmod = att->atttypmod;
2160 for (j = 0; j < cstate->indesc->natts; j++)
2162 att = cstate->indesc->attrs[j];
2163 if (att->attisdropped)
2165 if (strcmp(attname, NameStr(att->attname)) == 0)
2167 /* Found it, check type */
2168 if (atttypid != att->atttypid || atttypmod != att->atttypmod)
2169 elog(ERROR, "attribute \"%s\" of type %s does not match corresponding attribute of type %s",
2171 format_type_be(cstate->indesc->tdtypeid),
2172 format_type_be(cstate->outdesc->tdtypeid));
2173 cstate->attrMap[i] = (AttrNumber) (j + 1);
2177 if (cstate->attrMap[i] == 0)
2178 elog(ERROR, "attribute \"%s\" of type %s does not exist",
2180 format_type_be(cstate->indesc->tdtypeid));
2182 /* preallocate workspace for Datum arrays */
2183 n = cstate->indesc->natts + 1; /* +1 for NULL */
2184 cstate->invalues = (Datum *) palloc(n * sizeof(Datum));
2185 cstate->inisnull = (bool *) palloc(n * sizeof(bool));
2186 n = cstate->outdesc->natts;
2187 cstate->outvalues = (Datum *) palloc(n * sizeof(Datum));
2188 cstate->outisnull = (bool *) palloc(n * sizeof(bool));
2190 MemoryContextSwitchTo(old_cxt);
2193 attrMap = cstate->attrMap;
2194 invalues = cstate->invalues;
2195 inisnull = cstate->inisnull;
2196 outvalues = cstate->outvalues;
2197 outisnull = cstate->outisnull;
2198 outnatts = cstate->outdesc->natts;
2201 * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader.
2203 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
2204 tmptup.t_data = tuple;
2207 * Extract all the values of the old tuple, offsetting the arrays so that
2208 * invalues[0] is NULL and invalues[1] is the first source attribute; this
2209 * exactly matches the numbering convention in attrMap.
2211 heap_deform_tuple(&tmptup, cstate->indesc, invalues + 1, inisnull + 1);
2212 invalues[0] = (Datum) 0;
2216 * Transpose into proper fields of the new tuple.
2218 for (i = 0; i < outnatts; i++)
2222 outvalues[i] = invalues[j];
2223 outisnull[i] = inisnull[j];
2227 * Now form the new tuple.
2229 result = heap_form_tuple(cstate->outdesc, outvalues, outisnull);
2231 return HeapTupleGetDatum(result);
2234 /* ----------------------------------------------------------------
2237 * Evaluate a CASE clause. Will have boolean expressions
2238 * inside the WHEN clauses, and will have expressions
2240 * - thomas 1998-11-09
2241 * ----------------------------------------------------------------
2244 ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
2245 bool *isNull, ExprDoneCond *isDone)
2247 List *clauses = caseExpr->args;
2253 *isDone = ExprSingleResult;
2256 * If there's a test expression, we have to evaluate it and save the value
2257 * where the CaseTestExpr placeholders can find it. We must save and
2258 * restore prior setting of econtext's caseValue fields, in case this node
2259 * is itself within a larger CASE.
2261 save_datum = econtext->caseValue_datum;
2262 save_isNull = econtext->caseValue_isNull;
2266 econtext->caseValue_datum = ExecEvalExpr(caseExpr->arg,
2268 &econtext->caseValue_isNull,
2273 * we evaluate each of the WHEN clauses in turn, as soon as one is true we
2274 * return the corresponding result. If none are true then we return the
2275 * value of the default clause, or NULL if there is none.
2277 foreach(clause, clauses)
2279 CaseWhenState *wclause = lfirst(clause);
2282 clause_value = ExecEvalExpr(wclause->expr,
2288 * if we have a true test, then we return the result, since the case
2289 * statement is satisfied. A NULL result from the test is not
2292 if (DatumGetBool(clause_value) && !*isNull)
2294 econtext->caseValue_datum = save_datum;
2295 econtext->caseValue_isNull = save_isNull;
2296 return ExecEvalExpr(wclause->result,
2303 econtext->caseValue_datum = save_datum;
2304 econtext->caseValue_isNull = save_isNull;
2306 if (caseExpr->defresult)
2308 return ExecEvalExpr(caseExpr->defresult,
2319 * ExecEvalCaseTestExpr
2321 * Return the value stored by CASE.
2324 ExecEvalCaseTestExpr(ExprState *exprstate,
2325 ExprContext *econtext,
2326 bool *isNull, ExprDoneCond *isDone)
2329 *isDone = ExprSingleResult;
2330 *isNull = econtext->caseValue_isNull;
2331 return econtext->caseValue_datum;
2334 /* ----------------------------------------------------------------
2335 * ExecEvalArray - ARRAY[] expressions
2336 * ----------------------------------------------------------------
2339 ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
2340 bool *isNull, ExprDoneCond *isDone)
2342 ArrayExpr *arrayExpr = (ArrayExpr *) astate->xprstate.expr;
2345 Oid element_type = arrayExpr->element_typeid;
2350 /* Set default values for result flags: non-null, not a set result */
2353 *isDone = ExprSingleResult;
2355 if (!arrayExpr->multidims)
2357 /* Elements are presumably of scalar type */
2364 nelems = list_length(astate->elements);
2366 /* Shouldn't happen here, but if length is 0, return empty array */
2368 return PointerGetDatum(construct_empty_array(element_type));
2370 dvalues = (Datum *) palloc(nelems * sizeof(Datum));
2371 dnulls = (bool *) palloc(nelems * sizeof(bool));
2373 /* loop through and build array of datums */
2374 foreach(element, astate->elements)
2376 ExprState *e = (ExprState *) lfirst(element);
2378 dvalues[i] = ExecEvalExpr(e, econtext, &dnulls[i], NULL);
2382 /* setup for 1-D array of the given length */
2386 result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
2394 /* Must be nested array expressions */
2397 int outer_nelems = 0;
2399 int *elem_dims = NULL;
2400 int *elem_lbs = NULL;
2401 bool firstone = true;
2402 bool havenulls = false;
2403 bool haveempty = false;
2413 i = list_length(astate->elements);
2414 subdata = (char **) palloc(i * sizeof(char *));
2415 subbitmaps = (bits8 **) palloc(i * sizeof(bits8 *));
2416 subbytes = (int *) palloc(i * sizeof(int));
2417 subnitems = (int *) palloc(i * sizeof(int));
2419 /* loop through and get data area from each element */
2420 foreach(element, astate->elements)
2422 ExprState *e = (ExprState *) lfirst(element);
2428 arraydatum = ExecEvalExpr(e, econtext, &eisnull, NULL);
2429 /* temporarily ignore null subarrays */
2436 array = DatumGetArrayTypeP(arraydatum);
2438 /* run-time double-check on element type */
2439 if (element_type != ARR_ELEMTYPE(array))
2441 (errcode(ERRCODE_DATATYPE_MISMATCH),
2442 errmsg("cannot merge incompatible arrays"),
2443 errdetail("Array with element type %s cannot be "
2444 "included in ARRAY construct with element type %s.",
2445 format_type_be(ARR_ELEMTYPE(array)),
2446 format_type_be(element_type))));
2448 this_ndims = ARR_NDIM(array);
2449 /* temporarily ignore zero-dimensional subarrays */
2450 if (this_ndims <= 0)
2458 /* Get sub-array details from first member */
2459 elem_ndims = this_ndims;
2460 ndims = elem_ndims + 1;
2461 if (ndims <= 0 || ndims > MAXDIM)
2463 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2464 errmsg("number of array dimensions (%d) exceeds " \
2465 "the maximum allowed (%d)", ndims, MAXDIM)));
2467 elem_dims = (int *) palloc(elem_ndims * sizeof(int));
2468 memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
2469 elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
2470 memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
2476 /* Check other sub-arrays are compatible */
2477 if (elem_ndims != this_ndims ||
2478 memcmp(elem_dims, ARR_DIMS(array),
2479 elem_ndims * sizeof(int)) != 0 ||
2480 memcmp(elem_lbs, ARR_LBOUND(array),
2481 elem_ndims * sizeof(int)) != 0)
2483 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2484 errmsg("multidimensional arrays must have array "
2485 "expressions with matching dimensions")));
2488 subdata[outer_nelems] = ARR_DATA_PTR(array);
2489 subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
2490 subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
2491 nbytes += subbytes[outer_nelems];
2492 subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
2494 nitems += subnitems[outer_nelems];
2495 havenulls |= ARR_HASNULL(array);
2500 * If all items were null or empty arrays, return an empty array;
2501 * otherwise, if some were and some weren't, raise error. (Note:
2502 * we must special-case this somehow to avoid trying to generate
2503 * a 1-D array formed from empty arrays. It's not ideal...)
2507 if (ndims == 0) /* didn't find any nonempty array */
2508 return PointerGetDatum(construct_empty_array(element_type));
2510 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2511 errmsg("multidimensional arrays must have array "
2512 "expressions with matching dimensions")));
2515 /* setup for multi-D array */
2516 dims[0] = outer_nelems;
2518 for (i = 1; i < ndims; i++)
2520 dims[i] = elem_dims[i - 1];
2521 lbs[i] = elem_lbs[i - 1];
2526 dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
2527 nbytes += dataoffset;
2531 dataoffset = 0; /* marker for no null bitmap */
2532 nbytes += ARR_OVERHEAD_NONULLS(ndims);
2535 result = (ArrayType *) palloc(nbytes);
2536 result->size = nbytes;
2537 result->ndim = ndims;
2538 result->dataoffset = dataoffset;
2539 result->elemtype = element_type;
2540 memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
2541 memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
2543 dat = ARR_DATA_PTR(result);
2545 for (i = 0; i < outer_nelems; i++)
2547 memcpy(dat, subdata[i], subbytes[i]);
2550 array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
2553 iitem += subnitems[i];
2557 return PointerGetDatum(result);
2560 /* ----------------------------------------------------------------
2561 * ExecEvalRow - ROW() expressions
2562 * ----------------------------------------------------------------
2565 ExecEvalRow(RowExprState *rstate,
2566 ExprContext *econtext,
2567 bool *isNull, ExprDoneCond *isDone)
2576 /* Set default values for result flags: non-null, not a set result */
2579 *isDone = ExprSingleResult;
2581 /* Allocate workspace */
2582 natts = rstate->tupdesc->natts;
2583 values = (Datum *) palloc0(natts * sizeof(Datum));
2584 isnull = (bool *) palloc(natts * sizeof(bool));
2586 /* preset to nulls in case rowtype has some later-added columns */
2587 memset(isnull, true, natts * sizeof(bool));
2589 /* Evaluate field values */
2591 foreach(arg, rstate->args)
2593 ExprState *e = (ExprState *) lfirst(arg);
2595 values[i] = ExecEvalExpr(e, econtext, &isnull[i], NULL);
2599 tuple = heap_form_tuple(rstate->tupdesc, values, isnull);
2604 return HeapTupleGetDatum(tuple);
2607 /* ----------------------------------------------------------------
2608 * ExecEvalRowCompare - ROW() comparison-op ROW()
2609 * ----------------------------------------------------------------
2612 ExecEvalRowCompare(RowCompareExprState *rstate,
2613 ExprContext *econtext,
2614 bool *isNull, ExprDoneCond *isDone)
2617 RowCompareType rctype = ((RowCompareExpr *) rstate->xprstate.expr)->rctype;
2618 int32 cmpresult = 0;
2624 *isDone = ExprSingleResult;
2625 *isNull = true; /* until we get a result */
2628 forboth(l, rstate->largs, r, rstate->rargs)
2630 ExprState *le = (ExprState *) lfirst(l);
2631 ExprState *re = (ExprState *) lfirst(r);
2632 FunctionCallInfoData locfcinfo;
2634 InitFunctionCallInfoData(locfcinfo, &(rstate->funcs[i]), 2,
2636 locfcinfo.arg[0] = ExecEvalExpr(le, econtext,
2637 &locfcinfo.argnull[0], NULL);
2638 locfcinfo.arg[1] = ExecEvalExpr(re, econtext,
2639 &locfcinfo.argnull[1], NULL);
2640 if (rstate->funcs[i].fn_strict &&
2641 (locfcinfo.argnull[0] || locfcinfo.argnull[1]))
2642 return (Datum) 0; /* force NULL result */
2643 locfcinfo.isnull = false;
2644 cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo));
2645 if (locfcinfo.isnull)
2646 return (Datum) 0; /* force NULL result */
2648 break; /* no need to compare remaining columns */
2654 /* EQ and NE cases aren't allowed here */
2656 result = (cmpresult < 0);
2659 result = (cmpresult <= 0);
2662 result = (cmpresult >= 0);
2665 result = (cmpresult > 0);
2668 elog(ERROR, "unrecognized RowCompareType: %d", (int) rctype);
2669 result = 0; /* keep compiler quiet */
2674 return BoolGetDatum(result);
2677 /* ----------------------------------------------------------------
2679 * ----------------------------------------------------------------
2682 ExecEvalCoalesce(CoalesceExprState *coalesceExpr, ExprContext *econtext,
2683 bool *isNull, ExprDoneCond *isDone)
2688 *isDone = ExprSingleResult;
2690 /* Simply loop through until something NOT NULL is found */
2691 foreach(arg, coalesceExpr->args)
2693 ExprState *e = (ExprState *) lfirst(arg);
2696 value = ExecEvalExpr(e, econtext, isNull, NULL);
2701 /* Else return NULL */
2706 /* ----------------------------------------------------------------
2708 * ----------------------------------------------------------------
2711 ExecEvalMinMax(MinMaxExprState *minmaxExpr, ExprContext *econtext,
2712 bool *isNull, ExprDoneCond *isDone)
2714 Datum result = (Datum) 0;
2715 MinMaxOp op = ((MinMaxExpr *) minmaxExpr->xprstate.expr)->op;
2716 FunctionCallInfoData locfcinfo;
2720 *isDone = ExprSingleResult;
2721 *isNull = true; /* until we get a result */
2723 InitFunctionCallInfoData(locfcinfo, &minmaxExpr->cfunc, 2, NULL, NULL);
2724 locfcinfo.argnull[0] = false;
2725 locfcinfo.argnull[1] = false;
2727 foreach(arg, minmaxExpr->args)
2729 ExprState *e = (ExprState *) lfirst(arg);
2734 value = ExecEvalExpr(e, econtext, &valueIsNull, NULL);
2736 continue; /* ignore NULL inputs */
2740 /* first nonnull input, adopt value */
2746 /* apply comparison function */
2747 locfcinfo.arg[0] = result;
2748 locfcinfo.arg[1] = value;
2749 locfcinfo.isnull = false;
2750 cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo));
2751 if (locfcinfo.isnull) /* probably should not happen */
2753 if (cmpresult > 0 && op == IS_LEAST)
2755 else if (cmpresult < 0 && op == IS_GREATEST)
2763 /* ----------------------------------------------------------------
2765 * ----------------------------------------------------------------
2768 ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
2769 bool *isNull, ExprDoneCond *isDone)
2771 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
2781 *isDone = ExprSingleResult;
2782 *isNull = true; /* until we get a result */
2790 foreach(arg, xmlExpr->args)
2792 ExprState *e = (ExprState *) lfirst(arg);
2794 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2796 values = lappend(values, DatumGetPointer(value));
2799 if (list_length(values) > 0)
2802 return PointerGetDatum(xmlconcat(values));
2808 initStringInfo(&buf);
2810 forboth(arg, xmlExpr->named_args, narg, xexpr->arg_names)
2812 ExprState *e = (ExprState *) lfirst(arg);
2813 char *argname = strVal(lfirst(narg));
2815 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2818 appendStringInfo(&buf, "<%s>%s</%s>",
2820 map_sql_value_to_xml_value(value, exprType((Node *) e->expr)),
2828 /* The remaining cases don't need to set up buf */
2831 return PointerGetDatum(xmlelement(xmlExpr, econtext));
2838 bool preserve_whitespace;
2840 /* arguments are known to be text, bool */
2841 Assert(list_length(xmlExpr->args) == 2);
2843 e = (ExprState *) linitial(xmlExpr->args);
2844 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2847 data = DatumGetTextP(value);
2849 e = (ExprState *) lsecond(xmlExpr->args);
2850 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2851 if (isnull) /* probably can't happen */
2853 preserve_whitespace = DatumGetBool(value);
2857 return PointerGetDatum(xmlparse(data,
2859 preserve_whitespace));
2868 /* optional argument is known to be text */
2869 Assert(list_length(xmlExpr->args) <= 1);
2873 e = (ExprState *) linitial(xmlExpr->args);
2874 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2878 arg = DatumGetTextP(value);
2886 return PointerGetDatum(xmlpi(xexpr->name, arg, isnull, isNull));
2897 /* arguments are known to be xml, text, int */
2898 Assert(list_length(xmlExpr->args) == 3);
2900 e = (ExprState *) linitial(xmlExpr->args);
2901 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2904 data = DatumGetXmlP(value);
2906 e = (ExprState *) lsecond(xmlExpr->args);
2907 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2911 version = DatumGetTextP(value);
2913 e = (ExprState *) lthird(xmlExpr->args);
2914 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2915 standalone = DatumGetInt32(value);
2919 return PointerGetDatum(xmlroot(data,
2925 case IS_XMLSERIALIZE:
2929 /* argument type is known to be xml */
2930 Assert(list_length(xmlExpr->args) == 1);
2932 e = (ExprState *) linitial(xmlExpr->args);
2933 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2939 return PointerGetDatum(xmltotext_with_xmloption(DatumGetXmlP(value), xexpr->xmloption));
2947 /* optional argument is known to be xml */
2948 Assert(list_length(xmlExpr->args) == 1);
2950 e = (ExprState *) linitial(xmlExpr->args);
2951 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2957 return BoolGetDatum(xml_is_document(DatumGetXmlP(value)));
2967 int len = buf.len + VARHDRSZ;
2969 result = palloc(len);
2970 VARATT_SIZEP(result) = len;
2971 memcpy(VARDATA(result), buf.data, buf.len);
2975 return PointerGetDatum(result);
2978 /* ----------------------------------------------------------------
2981 * Note that this is *always* derived from the equals operator,
2982 * but since we need special processing of the arguments
2983 * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
2984 * ----------------------------------------------------------------
2987 ExecEvalNullIf(FuncExprState *nullIfExpr,
2988 ExprContext *econtext,
2989 bool *isNull, ExprDoneCond *isDone)
2992 FunctionCallInfoData fcinfo;
2993 ExprDoneCond argDone;
2997 *isDone = ExprSingleResult;
3000 * Initialize function cache if first time through
3002 if (nullIfExpr->func.fn_oid == InvalidOid)
3004 NullIfExpr *op = (NullIfExpr *) nullIfExpr->xprstate.expr;
3006 init_fcache(op->opfuncid, nullIfExpr, econtext->ecxt_per_query_memory);
3007 Assert(!nullIfExpr->func.fn_retset);
3011 * extract info from nullIfExpr
3013 argList = nullIfExpr->args;
3015 /* Need to prep callinfo structure */
3016 InitFunctionCallInfoData(fcinfo, &(nullIfExpr->func), 0, NULL, NULL);
3017 argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
3018 if (argDone != ExprSingleResult)
3020 (errcode(ERRCODE_DATATYPE_MISMATCH),
3021 errmsg("NULLIF does not support set arguments")));
3022 Assert(fcinfo.nargs == 2);
3024 /* if either argument is NULL they can't be equal */
3025 if (!fcinfo.argnull[0] && !fcinfo.argnull[1])
3027 fcinfo.isnull = false;
3028 result = FunctionCallInvoke(&fcinfo);
3029 /* if the arguments are equal return null */
3030 if (!fcinfo.isnull && DatumGetBool(result))
3037 /* else return first argument */
3038 *isNull = fcinfo.argnull[0];
3039 return fcinfo.arg[0];
3042 /* ----------------------------------------------------------------
3045 * Evaluate a NullTest node.
3046 * ----------------------------------------------------------------
3049 ExecEvalNullTest(NullTestState *nstate,
3050 ExprContext *econtext,
3052 ExprDoneCond *isDone)
3054 NullTest *ntest = (NullTest *) nstate->xprstate.expr;
3057 result = ExecEvalExpr(nstate->arg, econtext, isNull, isDone);
3059 if (isDone && *isDone == ExprEndResult)
3060 return result; /* nothing to check */
3062 if (nstate->argisrow && !(*isNull))
3064 HeapTupleHeader tuple;
3068 HeapTupleData tmptup;
3071 tuple = DatumGetHeapTupleHeader(result);
3073 tupType = HeapTupleHeaderGetTypeId(tuple);
3074 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3076 /* Lookup tupdesc if first time through or if type changes */
3077 tupDesc = get_cached_rowtype(tupType, tupTypmod,
3078 &nstate->argdesc, econtext);
3081 * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
3083 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3084 tmptup.t_data = tuple;
3086 for (att = 1; att <= tupDesc->natts; att++)
3088 /* ignore dropped columns */
3089 if (tupDesc->attrs[att - 1]->attisdropped)
3091 if (heap_attisnull(&tmptup, att))
3093 /* null field disproves IS NOT NULL */
3094 if (ntest->nulltesttype == IS_NOT_NULL)
3095 return BoolGetDatum(false);
3099 /* non-null field disproves IS NULL */
3100 if (ntest->nulltesttype == IS_NULL)
3101 return BoolGetDatum(false);
3105 return BoolGetDatum(true);
3109 /* Simple scalar-argument case, or a null rowtype datum */
3110 switch (ntest->nulltesttype)
3116 return BoolGetDatum(true);
3119 return BoolGetDatum(false);
3124 return BoolGetDatum(false);
3127 return BoolGetDatum(true);
3129 elog(ERROR, "unrecognized nulltesttype: %d",
3130 (int) ntest->nulltesttype);
3131 return (Datum) 0; /* keep compiler quiet */
3136 /* ----------------------------------------------------------------
3137 * ExecEvalBooleanTest
3139 * Evaluate a BooleanTest node.
3140 * ----------------------------------------------------------------
3143 ExecEvalBooleanTest(GenericExprState *bstate,
3144 ExprContext *econtext,
3146 ExprDoneCond *isDone)
3148 BooleanTest *btest = (BooleanTest *) bstate->xprstate.expr;
3151 result = ExecEvalExpr(bstate->arg, econtext, isNull, isDone);
3153 if (isDone && *isDone == ExprEndResult)
3154 return result; /* nothing to check */
3156 switch (btest->booltesttype)
3162 return BoolGetDatum(false);
3164 else if (DatumGetBool(result))
3165 return BoolGetDatum(true);
3167 return BoolGetDatum(false);
3172 return BoolGetDatum(true);
3174 else if (DatumGetBool(result))
3175 return BoolGetDatum(false);
3177 return BoolGetDatum(true);
3182 return BoolGetDatum(false);
3184 else if (DatumGetBool(result))
3185 return BoolGetDatum(false);
3187 return BoolGetDatum(true);
3192 return BoolGetDatum(true);
3194 else if (DatumGetBool(result))
3195 return BoolGetDatum(true);
3197 return BoolGetDatum(false);
3202 return BoolGetDatum(true);
3205 return BoolGetDatum(false);
3206 case IS_NOT_UNKNOWN:
3210 return BoolGetDatum(false);
3213 return BoolGetDatum(true);
3215 elog(ERROR, "unrecognized booltesttype: %d",
3216 (int) btest->booltesttype);
3217 return (Datum) 0; /* keep compiler quiet */
3222 * ExecEvalCoerceToDomain
3224 * Test the provided data against the domain constraint(s). If the data
3225 * passes the constraint specifications, pass it through (return the
3226 * datum) otherwise throw an error.
3229 ExecEvalCoerceToDomain(CoerceToDomainState *cstate, ExprContext *econtext,
3230 bool *isNull, ExprDoneCond *isDone)
3232 CoerceToDomain *ctest = (CoerceToDomain *) cstate->xprstate.expr;
3236 result = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
3238 if (isDone && *isDone == ExprEndResult)
3239 return result; /* nothing to check */
3241 foreach(l, cstate->constraints)
3243 DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
3245 switch (con->constrainttype)
3247 case DOM_CONSTRAINT_NOTNULL:
3250 (errcode(ERRCODE_NOT_NULL_VIOLATION),
3251 errmsg("domain %s does not allow null values",
3252 format_type_be(ctest->resulttype))));
3254 case DOM_CONSTRAINT_CHECK:
3262 * Set up value to be returned by CoerceToDomainValue
3263 * nodes. We must save and restore prior setting of
3264 * econtext's domainValue fields, in case this node is
3265 * itself within a check expression for another domain.
3267 save_datum = econtext->domainValue_datum;
3268 save_isNull = econtext->domainValue_isNull;
3270 econtext->domainValue_datum = result;
3271 econtext->domainValue_isNull = *isNull;
3273 conResult = ExecEvalExpr(con->check_expr,
3274 econtext, &conIsNull, NULL);
3277 !DatumGetBool(conResult))
3279 (errcode(ERRCODE_CHECK_VIOLATION),
3280 errmsg("value for domain %s violates check constraint \"%s\"",
3281 format_type_be(ctest->resulttype),
3283 econtext->domainValue_datum = save_datum;
3284 econtext->domainValue_isNull = save_isNull;
3289 elog(ERROR, "unrecognized constraint type: %d",
3290 (int) con->constrainttype);
3295 /* If all has gone well (constraints did not fail) return the datum */
3300 * ExecEvalCoerceToDomainValue
3302 * Return the value stored by CoerceToDomain.
3305 ExecEvalCoerceToDomainValue(ExprState *exprstate,
3306 ExprContext *econtext,
3307 bool *isNull, ExprDoneCond *isDone)
3310 *isDone = ExprSingleResult;
3311 *isNull = econtext->domainValue_isNull;
3312 return econtext->domainValue_datum;
3315 /* ----------------------------------------------------------------
3316 * ExecEvalFieldSelect
3318 * Evaluate a FieldSelect node.
3319 * ----------------------------------------------------------------
3322 ExecEvalFieldSelect(FieldSelectState *fstate,
3323 ExprContext *econtext,
3325 ExprDoneCond *isDone)
3327 FieldSelect *fselect = (FieldSelect *) fstate->xprstate.expr;
3328 AttrNumber fieldnum = fselect->fieldnum;
3331 HeapTupleHeader tuple;
3335 Form_pg_attribute attr;
3336 HeapTupleData tmptup;
3338 tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
3340 /* this test covers the isDone exception too: */
3344 tuple = DatumGetHeapTupleHeader(tupDatum);
3346 tupType = HeapTupleHeaderGetTypeId(tuple);
3347 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3349 /* Lookup tupdesc if first time through or if type changes */
3350 tupDesc = get_cached_rowtype(tupType, tupTypmod,
3351 &fstate->argdesc, econtext);
3353 /* Check for dropped column, and force a NULL result if so */
3354 if (fieldnum <= 0 ||
3355 fieldnum > tupDesc->natts) /* should never happen */
3356 elog(ERROR, "attribute number %d exceeds number of columns %d",
3357 fieldnum, tupDesc->natts);
3358 attr = tupDesc->attrs[fieldnum - 1];
3359 if (attr->attisdropped)
3365 /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3366 /* As in ExecEvalVar, we should but can't check typmod */
3367 if (fselect->resulttype != attr->atttypid)
3369 (errmsg("attribute %d has wrong type", fieldnum),
3370 errdetail("Table has type %s, but query expects %s.",
3371 format_type_be(attr->atttypid),
3372 format_type_be(fselect->resulttype))));
3375 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
3376 * the fields in the struct just in case user tries to inspect system
3379 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3380 ItemPointerSetInvalid(&(tmptup.t_self));
3381 tmptup.t_tableOid = InvalidOid;
3382 tmptup.t_data = tuple;
3384 result = heap_getattr(&tmptup,
3391 /* ----------------------------------------------------------------
3392 * ExecEvalFieldStore
3394 * Evaluate a FieldStore node.
3395 * ----------------------------------------------------------------
3398 ExecEvalFieldStore(FieldStoreState *fstate,
3399 ExprContext *econtext,
3401 ExprDoneCond *isDone)
3403 FieldStore *fstore = (FieldStore *) fstate->xprstate.expr;
3414 tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
3416 if (isDone && *isDone == ExprEndResult)
3419 /* Lookup tupdesc if first time through or after rescan */
3420 tupDesc = get_cached_rowtype(fstore->resulttype, -1,
3421 &fstate->argdesc, econtext);
3423 /* Allocate workspace */
3424 values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
3425 isnull = (bool *) palloc(tupDesc->natts * sizeof(bool));
3430 * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We
3431 * set all the fields in the struct just in case.
3433 HeapTupleHeader tuphdr;
3434 HeapTupleData tmptup;
3436 tuphdr = DatumGetHeapTupleHeader(tupDatum);
3437 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
3438 ItemPointerSetInvalid(&(tmptup.t_self));
3439 tmptup.t_tableOid = InvalidOid;
3440 tmptup.t_data = tuphdr;
3442 heap_deform_tuple(&tmptup, tupDesc, values, isnull);
3446 /* Convert null input tuple into an all-nulls row */
3447 memset(isnull, true, tupDesc->natts * sizeof(bool));
3450 /* Result is never null */
3453 save_datum = econtext->caseValue_datum;
3454 save_isNull = econtext->caseValue_isNull;
3456 forboth(l1, fstate->newvals, l2, fstore->fieldnums)
3458 ExprState *newval = (ExprState *) lfirst(l1);
3459 AttrNumber fieldnum = lfirst_int(l2);
3461 Assert(fieldnum > 0 && fieldnum <= tupDesc->natts);
3464 * Use the CaseTestExpr mechanism to pass down the old value of the
3465 * field being replaced; this is useful in case we have a nested field
3466 * update situation. It's safe to reuse the CASE mechanism because
3467 * there cannot be a CASE between here and where the value would be
3470 econtext->caseValue_datum = values[fieldnum - 1];
3471 econtext->caseValue_isNull = isnull[fieldnum - 1];
3473 values[fieldnum - 1] = ExecEvalExpr(newval,
3475 &isnull[fieldnum - 1],
3479 econtext->caseValue_datum = save_datum;
3480 econtext->caseValue_isNull = save_isNull;
3482 tuple = heap_form_tuple(tupDesc, values, isnull);
3487 return HeapTupleGetDatum(tuple);
3490 /* ----------------------------------------------------------------
3491 * ExecEvalRelabelType
3493 * Evaluate a RelabelType node.
3494 * ----------------------------------------------------------------
3497 ExecEvalRelabelType(GenericExprState *exprstate,
3498 ExprContext *econtext,
3499 bool *isNull, ExprDoneCond *isDone)
3501 return ExecEvalExpr(exprstate->arg, econtext, isNull, isDone);
3506 * ExecEvalExprSwitchContext
3508 * Same as ExecEvalExpr, but get into the right allocation context explicitly.
3511 ExecEvalExprSwitchContext(ExprState *expression,
3512 ExprContext *econtext,
3514 ExprDoneCond *isDone)
3517 MemoryContext oldContext;
3519 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
3520 retDatum = ExecEvalExpr(expression, econtext, isNull, isDone);
3521 MemoryContextSwitchTo(oldContext);
3527 * ExecInitExpr: prepare an expression tree for execution
3529 * This function builds and returns an ExprState tree paralleling the given
3530 * Expr node tree. The ExprState tree can then be handed to ExecEvalExpr
3531 * for execution. Because the Expr tree itself is read-only as far as
3532 * ExecInitExpr and ExecEvalExpr are concerned, several different executions
3533 * of the same plan tree can occur concurrently.
3535 * This must be called in a memory context that will last as long as repeated
3536 * executions of the expression are needed. Typically the context will be
3537 * the same as the per-query context of the associated ExprContext.
3539 * Any Aggref and SubPlan nodes found in the tree are added to the lists
3540 * of such nodes held by the parent PlanState. Otherwise, we do very little
3541 * initialization here other than building the state-node tree. Any nontrivial
3542 * work associated with initializing runtime info for a node should happen
3543 * during the first actual evaluation of that node. (This policy lets us
3544 * avoid work if the node is never actually evaluated.)
3546 * Note: there is no ExecEndExpr function; we assume that any resource
3547 * cleanup needed will be handled by just releasing the memory context
3548 * in which the state tree is built. Functions that require additional
3549 * cleanup work can register a shutdown callback in the ExprContext.
3551 * 'node' is the root of the expression tree to examine
3552 * 'parent' is the PlanState node that owns the expression.
3554 * 'parent' may be NULL if we are preparing an expression that is not
3555 * associated with a plan tree. (If so, it can't have aggs or subplans.)
3556 * This case should usually come through ExecPrepareExpr, not directly here.
3559 ExecInitExpr(Expr *node, PlanState *parent)
3566 /* Guard against stack overflow due to overly complex expressions */
3567 check_stack_depth();
3569 switch (nodeTag(node))
3572 state = (ExprState *) makeNode(ExprState);
3573 state->evalfunc = ExecEvalVar;
3576 state = (ExprState *) makeNode(ExprState);
3577 state->evalfunc = ExecEvalConst;
3580 state = (ExprState *) makeNode(ExprState);
3581 state->evalfunc = ExecEvalParam;
3583 case T_CoerceToDomainValue:
3584 state = (ExprState *) makeNode(ExprState);
3585 state->evalfunc = ExecEvalCoerceToDomainValue;
3587 case T_CaseTestExpr:
3588 state = (ExprState *) makeNode(ExprState);
3589 state->evalfunc = ExecEvalCaseTestExpr;
3593 Aggref *aggref = (Aggref *) node;
3594 AggrefExprState *astate = makeNode(AggrefExprState);
3596 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAggref;
3597 if (parent && IsA(parent, AggState))
3599 AggState *aggstate = (AggState *) parent;
3602 aggstate->aggs = lcons(astate, aggstate->aggs);
3603 naggs = ++aggstate->numaggs;
3605 astate->args = (List *) ExecInitExpr((Expr *) aggref->args,
3609 * Complain if the aggregate's arguments contain any
3610 * aggregates; nested agg functions are semantically
3611 * nonsensical. (This should have been caught earlier,
3612 * but we defend against it here anyway.)
3614 if (naggs != aggstate->numaggs)
3616 (errcode(ERRCODE_GROUPING_ERROR),
3617 errmsg("aggregate function calls cannot be nested")));
3621 /* planner messed up */
3622 elog(ERROR, "aggref found in non-Agg plan node");
3624 state = (ExprState *) astate;
3629 ArrayRef *aref = (ArrayRef *) node;
3630 ArrayRefExprState *astate = makeNode(ArrayRefExprState);
3632 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArrayRef;
3633 astate->refupperindexpr = (List *)
3634 ExecInitExpr((Expr *) aref->refupperindexpr, parent);
3635 astate->reflowerindexpr = (List *)
3636 ExecInitExpr((Expr *) aref->reflowerindexpr, parent);
3637 astate->refexpr = ExecInitExpr(aref->refexpr, parent);
3638 astate->refassgnexpr = ExecInitExpr(aref->refassgnexpr,
3640 /* do one-time catalog lookups for type info */
3641 astate->refattrlength = get_typlen(aref->refarraytype);
3642 get_typlenbyvalalign(aref->refelemtype,
3643 &astate->refelemlength,
3644 &astate->refelembyval,
3645 &astate->refelemalign);
3646 state = (ExprState *) astate;
3651 FuncExpr *funcexpr = (FuncExpr *) node;
3652 FuncExprState *fstate = makeNode(FuncExprState);
3654 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFunc;
3655 fstate->args = (List *)
3656 ExecInitExpr((Expr *) funcexpr->args, parent);
3657 fstate->func.fn_oid = InvalidOid; /* not initialized */
3658 state = (ExprState *) fstate;
3663 OpExpr *opexpr = (OpExpr *) node;
3664 FuncExprState *fstate = makeNode(FuncExprState);
3666 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOper;
3667 fstate->args = (List *)
3668 ExecInitExpr((Expr *) opexpr->args, parent);
3669 fstate->func.fn_oid = InvalidOid; /* not initialized */
3670 state = (ExprState *) fstate;
3673 case T_DistinctExpr:
3675 DistinctExpr *distinctexpr = (DistinctExpr *) node;
3676 FuncExprState *fstate = makeNode(FuncExprState);
3678 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalDistinct;
3679 fstate->args = (List *)
3680 ExecInitExpr((Expr *) distinctexpr->args, parent);
3681 fstate->func.fn_oid = InvalidOid; /* not initialized */
3682 state = (ExprState *) fstate;
3685 case T_ScalarArrayOpExpr:
3687 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
3688 ScalarArrayOpExprState *sstate = makeNode(ScalarArrayOpExprState);
3690 sstate->fxprstate.xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalScalarArrayOp;
3691 sstate->fxprstate.args = (List *)
3692 ExecInitExpr((Expr *) opexpr->args, parent);
3693 sstate->fxprstate.func.fn_oid = InvalidOid; /* not initialized */
3694 sstate->element_type = InvalidOid; /* ditto */
3695 state = (ExprState *) sstate;
3700 BoolExpr *boolexpr = (BoolExpr *) node;
3701 BoolExprState *bstate = makeNode(BoolExprState);
3703 switch (boolexpr->boolop)
3706 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAnd;
3709 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOr;
3712 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNot;
3715 elog(ERROR, "unrecognized boolop: %d",
3716 (int) boolexpr->boolop);
3719 bstate->args = (List *)
3720 ExecInitExpr((Expr *) boolexpr->args, parent);
3721 state = (ExprState *) bstate;
3726 SubPlan *subplan = (SubPlan *) node;
3727 SubPlanState *sstate;
3730 elog(ERROR, "SubPlan found with no parent plan");
3732 sstate = ExecInitSubPlan(subplan, parent);
3734 /* Add SubPlanState nodes to parent->subPlan */
3735 parent->subPlan = lcons(sstate, parent->subPlan);
3737 state = (ExprState *) sstate;
3742 FieldSelect *fselect = (FieldSelect *) node;
3743 FieldSelectState *fstate = makeNode(FieldSelectState);
3745 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldSelect;
3746 fstate->arg = ExecInitExpr(fselect->arg, parent);
3747 fstate->argdesc = NULL;
3748 state = (ExprState *) fstate;
3753 FieldStore *fstore = (FieldStore *) node;
3754 FieldStoreState *fstate = makeNode(FieldStoreState);
3756 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldStore;
3757 fstate->arg = ExecInitExpr(fstore->arg, parent);
3758 fstate->newvals = (List *) ExecInitExpr((Expr *) fstore->newvals, parent);
3759 fstate->argdesc = NULL;
3760 state = (ExprState *) fstate;
3765 RelabelType *relabel = (RelabelType *) node;
3766 GenericExprState *gstate = makeNode(GenericExprState);
3768 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRelabelType;
3769 gstate->arg = ExecInitExpr(relabel->arg, parent);
3770 state = (ExprState *) gstate;
3773 case T_ConvertRowtypeExpr:
3775 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
3776 ConvertRowtypeExprState *cstate = makeNode(ConvertRowtypeExprState);
3778 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalConvertRowtype;
3779 cstate->arg = ExecInitExpr(convert->arg, parent);
3780 state = (ExprState *) cstate;
3785 CaseExpr *caseexpr = (CaseExpr *) node;
3786 CaseExprState *cstate = makeNode(CaseExprState);
3787 List *outlist = NIL;
3790 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCase;
3791 cstate->arg = ExecInitExpr(caseexpr->arg, parent);
3792 foreach(l, caseexpr->args)
3794 CaseWhen *when = (CaseWhen *) lfirst(l);
3795 CaseWhenState *wstate = makeNode(CaseWhenState);
3797 Assert(IsA(when, CaseWhen));
3798 wstate->xprstate.evalfunc = NULL; /* not used */
3799 wstate->xprstate.expr = (Expr *) when;
3800 wstate->expr = ExecInitExpr(when->expr, parent);
3801 wstate->result = ExecInitExpr(when->result, parent);
3802 outlist = lappend(outlist, wstate);
3804 cstate->args = outlist;
3805 cstate->defresult = ExecInitExpr(caseexpr->defresult, parent);
3806 state = (ExprState *) cstate;
3811 ArrayExpr *arrayexpr = (ArrayExpr *) node;
3812 ArrayExprState *astate = makeNode(ArrayExprState);
3813 List *outlist = NIL;
3816 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArray;
3817 foreach(l, arrayexpr->elements)
3819 Expr *e = (Expr *) lfirst(l);
3822 estate = ExecInitExpr(e, parent);
3823 outlist = lappend(outlist, estate);
3825 astate->elements = outlist;
3826 /* do one-time catalog lookup for type info */
3827 get_typlenbyvalalign(arrayexpr->element_typeid,
3828 &astate->elemlength,
3830 &astate->elemalign);
3831 state = (ExprState *) astate;
3836 RowExpr *rowexpr = (RowExpr *) node;
3837 RowExprState *rstate = makeNode(RowExprState);
3838 Form_pg_attribute *attrs;
3839 List *outlist = NIL;
3843 rstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRow;
3844 /* Build tupdesc to describe result tuples */
3845 if (rowexpr->row_typeid == RECORDOID)
3847 /* generic record, use runtime type assignment */
3848 rstate->tupdesc = ExecTypeFromExprList(rowexpr->args);
3849 BlessTupleDesc(rstate->tupdesc);
3850 /* we won't need to redo this at runtime */
3854 /* it's been cast to a named type, use that */
3855 rstate->tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
3857 /* Set up evaluation, skipping any deleted columns */
3858 Assert(list_length(rowexpr->args) <= rstate->tupdesc->natts);
3859 attrs = rstate->tupdesc->attrs;
3861 foreach(l, rowexpr->args)
3863 Expr *e = (Expr *) lfirst(l);
3866 if (!attrs[i]->attisdropped)
3869 * Guard against ALTER COLUMN TYPE on rowtype since
3870 * the RowExpr was created. XXX should we check
3871 * typmod too? Not sure we can be sure it'll be the
3874 if (exprType((Node *) e) != attrs[i]->atttypid)
3876 (errcode(ERRCODE_DATATYPE_MISMATCH),
3877 errmsg("ROW() column has type %s instead of type %s",
3878 format_type_be(exprType((Node *) e)),
3879 format_type_be(attrs[i]->atttypid))));
3884 * Ignore original expression and insert a NULL. We
3885 * don't really care what type of NULL it is, so
3886 * always make an int4 NULL.
3888 e = (Expr *) makeNullConst(INT4OID);
3890 estate = ExecInitExpr(e, parent);
3891 outlist = lappend(outlist, estate);
3894 rstate->args = outlist;
3895 state = (ExprState *) rstate;
3898 case T_RowCompareExpr:
3900 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
3901 RowCompareExprState *rstate = makeNode(RowCompareExprState);
3902 int nopers = list_length(rcexpr->opnos);
3908 rstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRowCompare;
3909 Assert(list_length(rcexpr->largs) == nopers);
3911 foreach(l, rcexpr->largs)
3913 Expr *e = (Expr *) lfirst(l);
3916 estate = ExecInitExpr(e, parent);
3917 outlist = lappend(outlist, estate);
3919 rstate->largs = outlist;
3920 Assert(list_length(rcexpr->rargs) == nopers);
3922 foreach(l, rcexpr->rargs)
3924 Expr *e = (Expr *) lfirst(l);
3927 estate = ExecInitExpr(e, parent);
3928 outlist = lappend(outlist, estate);
3930 rstate->rargs = outlist;
3931 Assert(list_length(rcexpr->opfamilies) == nopers);
3932 rstate->funcs = (FmgrInfo *) palloc(nopers * sizeof(FmgrInfo));
3934 forboth(l, rcexpr->opnos, l2, rcexpr->opfamilies)
3936 Oid opno = lfirst_oid(l);
3937 Oid opfamily = lfirst_oid(l2);
3944 get_op_opfamily_properties(opno, opfamily,
3949 proc = get_opfamily_proc(opfamily,
3955 * If we enforced permissions checks on index support
3956 * functions, we'd need to make a check here. But the
3957 * index support machinery doesn't do that, and neither
3960 fmgr_info(proc, &(rstate->funcs[i]));
3963 state = (ExprState *) rstate;
3966 case T_CoalesceExpr:
3968 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3969 CoalesceExprState *cstate = makeNode(CoalesceExprState);
3970 List *outlist = NIL;
3973 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoalesce;
3974 foreach(l, coalesceexpr->args)
3976 Expr *e = (Expr *) lfirst(l);
3979 estate = ExecInitExpr(e, parent);
3980 outlist = lappend(outlist, estate);
3982 cstate->args = outlist;
3983 state = (ExprState *) cstate;
3988 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
3989 MinMaxExprState *mstate = makeNode(MinMaxExprState);
3990 List *outlist = NIL;
3992 TypeCacheEntry *typentry;
3994 mstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalMinMax;
3995 foreach(l, minmaxexpr->args)
3997 Expr *e = (Expr *) lfirst(l);
4000 estate = ExecInitExpr(e, parent);
4001 outlist = lappend(outlist, estate);
4003 mstate->args = outlist;
4004 /* Look up the btree comparison function for the datatype */
4005 typentry = lookup_type_cache(minmaxexpr->minmaxtype,
4006 TYPECACHE_CMP_PROC);
4007 if (!OidIsValid(typentry->cmp_proc))
4009 (errcode(ERRCODE_UNDEFINED_FUNCTION),
4010 errmsg("could not identify a comparison function for type %s",
4011 format_type_be(minmaxexpr->minmaxtype))));
4014 * If we enforced permissions checks on index support
4015 * functions, we'd need to make a check here. But the index
4016 * support machinery doesn't do that, and neither does this
4019 fmgr_info(typentry->cmp_proc, &(mstate->cfunc));
4020 state = (ExprState *) mstate;
4025 XmlExpr *xexpr = (XmlExpr *) node;
4026 XmlExprState *xstate = makeNode(XmlExprState);
4031 xstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalXml;
4032 xstate->named_outfuncs = (FmgrInfo *)
4033 palloc0(list_length(xexpr->named_args) * sizeof(FmgrInfo));
4036 foreach(arg, xexpr->named_args)
4038 Expr *e = (Expr *) lfirst(arg);
4043 estate = ExecInitExpr(e, parent);
4044 outlist = lappend(outlist, estate);
4046 getTypeOutputInfo(exprType((Node *) e),
4047 &typOutFunc, &typIsVarlena);
4048 fmgr_info(typOutFunc, &xstate->named_outfuncs[i]);
4051 xstate->named_args = outlist;
4054 foreach(arg, xexpr->args)
4056 Expr *e = (Expr *) lfirst(arg);
4059 estate = ExecInitExpr(e, parent);
4060 outlist = lappend(outlist, estate);
4062 xstate->args = outlist;
4064 state = (ExprState *) xstate;
4069 NullIfExpr *nullifexpr = (NullIfExpr *) node;
4070 FuncExprState *fstate = makeNode(FuncExprState);
4072 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullIf;
4073 fstate->args = (List *)
4074 ExecInitExpr((Expr *) nullifexpr->args, parent);
4075 fstate->func.fn_oid = InvalidOid; /* not initialized */
4076 state = (ExprState *) fstate;
4081 NullTest *ntest = (NullTest *) node;
4082 NullTestState *nstate = makeNode(NullTestState);
4084 nstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullTest;
4085 nstate->arg = ExecInitExpr(ntest->arg, parent);
4086 nstate->argisrow = type_is_rowtype(exprType((Node *) ntest->arg));
4087 nstate->argdesc = NULL;
4088 state = (ExprState *) nstate;
4093 BooleanTest *btest = (BooleanTest *) node;
4094 GenericExprState *gstate = makeNode(GenericExprState);
4096 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalBooleanTest;
4097 gstate->arg = ExecInitExpr(btest->arg, parent);
4098 state = (ExprState *) gstate;
4101 case T_CoerceToDomain:
4103 CoerceToDomain *ctest = (CoerceToDomain *) node;
4104 CoerceToDomainState *cstate = makeNode(CoerceToDomainState);
4106 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoerceToDomain;
4107 cstate->arg = ExecInitExpr(ctest->arg, parent);
4108 cstate->constraints = GetDomainConstraints(ctest->resulttype);
4109 state = (ExprState *) cstate;
4114 TargetEntry *tle = (TargetEntry *) node;
4115 GenericExprState *gstate = makeNode(GenericExprState);
4117 gstate->xprstate.evalfunc = NULL; /* not used */
4118 gstate->arg = ExecInitExpr(tle->expr, parent);
4119 state = (ExprState *) gstate;
4124 List *outlist = NIL;
4127 foreach(l, (List *) node)
4129 outlist = lappend(outlist,
4130 ExecInitExpr((Expr *) lfirst(l),
4133 /* Don't fall through to the "common" code below */
4134 return (ExprState *) outlist;
4137 elog(ERROR, "unrecognized node type: %d",
4138 (int) nodeTag(node));
4139 state = NULL; /* keep compiler quiet */
4143 /* Common code for all state-node types */
4150 * ExecPrepareExpr --- initialize for expression execution outside a normal
4151 * Plan tree context.
4153 * This differs from ExecInitExpr in that we don't assume the caller is
4154 * already running in the EState's per-query context. Also, we apply
4155 * fix_opfuncids() to the passed expression tree to be sure it is ready
4156 * to run. (In ordinary Plan trees the planner will have fixed opfuncids,
4157 * but callers outside the executor will not have done this.)
4160 ExecPrepareExpr(Expr *node, EState *estate)
4163 MemoryContext oldcontext;
4165 fix_opfuncids((Node *) node);
4167 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4169 result = ExecInitExpr(node, NULL);
4171 MemoryContextSwitchTo(oldcontext);
4177 /* ----------------------------------------------------------------
4178 * ExecQual / ExecTargetList / ExecProject
4179 * ----------------------------------------------------------------
4182 /* ----------------------------------------------------------------
4185 * Evaluates a conjunctive boolean expression (qual list) and
4186 * returns true iff none of the subexpressions are false.
4187 * (We also return true if the list is empty.)
4189 * If some of the subexpressions yield NULL but none yield FALSE,
4190 * then the result of the conjunction is NULL (ie, unknown)
4191 * according to three-valued boolean logic. In this case,
4192 * we return the value specified by the "resultForNull" parameter.
4194 * Callers evaluating WHERE clauses should pass resultForNull=FALSE,
4195 * since SQL specifies that tuples with null WHERE results do not
4196 * get selected. On the other hand, callers evaluating constraint
4197 * conditions should pass resultForNull=TRUE, since SQL also specifies
4198 * that NULL constraint conditions are not failures.
4200 * NOTE: it would not be correct to use this routine to evaluate an
4201 * AND subclause of a boolean expression; for that purpose, a NULL
4202 * result must be returned as NULL so that it can be properly treated
4203 * in the next higher operator (cf. ExecEvalAnd and ExecEvalOr).
4204 * This routine is only used in contexts where a complete expression
4205 * is being evaluated and we know that NULL can be treated the same
4206 * as one boolean result or the other.
4208 * ----------------------------------------------------------------
4211 ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
4214 MemoryContext oldContext;
4220 EV_printf("ExecQual: qual is ");
4221 EV_nodeDisplay(qual);
4227 * Run in short-lived per-tuple context while computing expressions.
4229 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4232 * Evaluate the qual conditions one at a time. If we find a FALSE result,
4233 * we can stop evaluating and return FALSE --- the AND result must be
4234 * FALSE. Also, if we find a NULL result when resultForNull is FALSE, we
4235 * can stop and return FALSE --- the AND result must be FALSE or NULL in
4236 * that case, and the caller doesn't care which.
4238 * If we get to the end of the list, we can return TRUE. This will happen
4239 * when the AND result is indeed TRUE, or when the AND result is NULL (one
4240 * or more NULL subresult, with all the rest TRUE) and the caller has
4241 * specified resultForNull = TRUE.
4247 ExprState *clause = (ExprState *) lfirst(l);
4251 expr_value = ExecEvalExpr(clause, econtext, &isNull, NULL);
4255 if (resultForNull == false)
4257 result = false; /* treat NULL as FALSE */
4263 if (!DatumGetBool(expr_value))
4265 result = false; /* definitely FALSE */
4271 MemoryContextSwitchTo(oldContext);
4277 * Number of items in a tlist (including any resjunk items!)
4280 ExecTargetListLength(List *targetlist)
4282 /* This used to be more complex, but fjoins are dead */
4283 return list_length(targetlist);
4287 * Number of items in a tlist, not including any resjunk items
4290 ExecCleanTargetListLength(List *targetlist)
4295 foreach(tl, targetlist)
4297 TargetEntry *curTle = (TargetEntry *) lfirst(tl);
4299 Assert(IsA(curTle, TargetEntry));
4300 if (!curTle->resjunk)
4308 * Evaluates a targetlist with respect to the given
4309 * expression context. Returns TRUE if we were able to create
4310 * a result, FALSE if we have exhausted a set-valued expression.
4312 * Results are stored into the passed values and isnull arrays.
4313 * The caller must provide an itemIsDone array that persists across calls.
4315 * As with ExecEvalExpr, the caller should pass isDone = NULL if not
4316 * prepared to deal with sets of result tuples. Otherwise, a return
4317 * of *isDone = ExprMultipleResult signifies a set element, and a return
4318 * of *isDone = ExprEndResult signifies end of the set of tuple.
4321 ExecTargetList(List *targetlist,
4322 ExprContext *econtext,
4325 ExprDoneCond *itemIsDone,
4326 ExprDoneCond *isDone)
4328 MemoryContext oldContext;
4333 * Run in short-lived per-tuple context while computing expressions.
4335 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4338 * evaluate all the expressions in the target list
4341 *isDone = ExprSingleResult; /* until proven otherwise */
4343 haveDoneSets = false; /* any exhausted set exprs in tlist? */
4345 foreach(tl, targetlist)
4347 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4348 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4349 AttrNumber resind = tle->resno - 1;
4351 values[resind] = ExecEvalExpr(gstate->arg,
4354 &itemIsDone[resind]);
4356 if (itemIsDone[resind] != ExprSingleResult)
4358 /* We have a set-valued expression in the tlist */
4361 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4362 errmsg("set-valued function called in context that cannot accept a set")));
4363 if (itemIsDone[resind] == ExprMultipleResult)
4365 /* we have undone sets in the tlist, set flag */
4366 *isDone = ExprMultipleResult;
4370 /* we have done sets in the tlist, set flag for that */
4371 haveDoneSets = true;
4379 * note: can't get here unless we verified isDone != NULL
4381 if (*isDone == ExprSingleResult)
4384 * all sets are done, so report that tlist expansion is complete.
4386 *isDone = ExprEndResult;
4387 MemoryContextSwitchTo(oldContext);
4393 * We have some done and some undone sets. Restart the done ones
4394 * so that we can deliver a tuple (if possible).
4396 foreach(tl, targetlist)
4398 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4399 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4400 AttrNumber resind = tle->resno - 1;
4402 if (itemIsDone[resind] == ExprEndResult)
4404 values[resind] = ExecEvalExpr(gstate->arg,
4407 &itemIsDone[resind]);
4409 if (itemIsDone[resind] == ExprEndResult)
4412 * Oh dear, this item is returning an empty set. Guess
4413 * we can't make a tuple after all.
4415 *isDone = ExprEndResult;
4422 * If we cannot make a tuple because some sets are empty, we still
4423 * have to cycle the nonempty sets to completion, else resources
4424 * will not be released from subplans etc.
4426 * XXX is that still necessary?
4428 if (*isDone == ExprEndResult)
4430 foreach(tl, targetlist)
4432 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4433 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4434 AttrNumber resind = tle->resno - 1;
4436 while (itemIsDone[resind] == ExprMultipleResult)
4438 values[resind] = ExecEvalExpr(gstate->arg,
4441 &itemIsDone[resind]);
4445 MemoryContextSwitchTo(oldContext);
4451 /* Report success */
4452 MemoryContextSwitchTo(oldContext);
4459 * Evaluates a simple-Variable-list projection.
4461 * Results are stored into the passed values and isnull arrays.
4464 ExecVariableList(ProjectionInfo *projInfo,
4468 ExprContext *econtext = projInfo->pi_exprContext;
4469 int *varSlotOffsets = projInfo->pi_varSlotOffsets;
4470 int *varNumbers = projInfo->pi_varNumbers;
4474 * Force extraction of all input values that we need.
4476 if (projInfo->pi_lastInnerVar > 0)
4477 slot_getsomeattrs(econtext->ecxt_innertuple,
4478 projInfo->pi_lastInnerVar);
4479 if (projInfo->pi_lastOuterVar > 0)
4480 slot_getsomeattrs(econtext->ecxt_outertuple,
4481 projInfo->pi_lastOuterVar);
4482 if (projInfo->pi_lastScanVar > 0)
4483 slot_getsomeattrs(econtext->ecxt_scantuple,
4484 projInfo->pi_lastScanVar);
4487 * Assign to result by direct extraction of fields from source slots ... a
4488 * mite ugly, but fast ...
4490 for (i = list_length(projInfo->pi_targetlist) - 1; i >= 0; i--)
4492 char *slotptr = ((char *) econtext) + varSlotOffsets[i];
4493 TupleTableSlot *varSlot = *((TupleTableSlot **) slotptr);
4494 int varNumber = varNumbers[i] - 1;
4496 values[i] = varSlot->tts_values[varNumber];
4497 isnull[i] = varSlot->tts_isnull[varNumber];
4504 * projects a tuple based on projection info and stores
4505 * it in the previously specified tuple table slot.
4507 * Note: the result is always a virtual tuple; therefore it
4508 * may reference the contents of the exprContext's scan tuples
4509 * and/or temporary results constructed in the exprContext.
4510 * If the caller wishes the result to be valid longer than that
4511 * data will be valid, he must call ExecMaterializeSlot on the
4515 ExecProject(ProjectionInfo *projInfo, ExprDoneCond *isDone)
4517 TupleTableSlot *slot;
4522 Assert(projInfo != NULL);
4525 * get the projection info we want
4527 slot = projInfo->pi_slot;
4530 * Clear any former contents of the result slot. This makes it safe for
4531 * us to use the slot's Datum/isnull arrays as workspace. (Also, we can
4532 * return the slot as-is if we decide no rows can be projected.)
4534 ExecClearTuple(slot);
4537 * form a new result tuple (if possible); if successful, mark the result
4538 * slot as containing a valid virtual tuple
4540 if (projInfo->pi_isVarList)
4542 /* simple Var list: this always succeeds with one result row */
4544 *isDone = ExprSingleResult;
4545 ExecVariableList(projInfo,
4548 ExecStoreVirtualTuple(slot);
4552 if (ExecTargetList(projInfo->pi_targetlist,
4553 projInfo->pi_exprContext,
4556 projInfo->pi_itemIsDone,
4558 ExecStoreVirtualTuple(slot);