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.222 2007/09/06 17:31:58 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 ExecEvalWholeRowSlow(ExprState *exprstate, ExprContext *econtext,
72 bool *isNull, ExprDoneCond *isDone);
73 static Datum ExecEvalConst(ExprState *exprstate, ExprContext *econtext,
74 bool *isNull, ExprDoneCond *isDone);
75 static Datum ExecEvalParam(ExprState *exprstate, ExprContext *econtext,
76 bool *isNull, ExprDoneCond *isDone);
77 static void ShutdownFuncExpr(Datum arg);
78 static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod,
79 TupleDesc *cache_field, ExprContext *econtext);
80 static void ShutdownTupleDescRef(Datum arg);
81 static ExprDoneCond ExecEvalFuncArgs(FunctionCallInfo fcinfo,
82 List *argList, ExprContext *econtext);
83 static Datum ExecMakeFunctionResultNoSets(FuncExprState *fcache,
84 ExprContext *econtext,
85 bool *isNull, ExprDoneCond *isDone);
86 static Datum ExecEvalFunc(FuncExprState *fcache, ExprContext *econtext,
87 bool *isNull, ExprDoneCond *isDone);
88 static Datum ExecEvalOper(FuncExprState *fcache, ExprContext *econtext,
89 bool *isNull, ExprDoneCond *isDone);
90 static Datum ExecEvalDistinct(FuncExprState *fcache, ExprContext *econtext,
91 bool *isNull, ExprDoneCond *isDone);
92 static Datum ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
93 ExprContext *econtext,
94 bool *isNull, ExprDoneCond *isDone);
95 static Datum ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
96 bool *isNull, ExprDoneCond *isDone);
97 static Datum ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
98 bool *isNull, ExprDoneCond *isDone);
99 static Datum ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
100 bool *isNull, ExprDoneCond *isDone);
101 static Datum ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
102 ExprContext *econtext,
103 bool *isNull, ExprDoneCond *isDone);
104 static Datum ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
105 bool *isNull, ExprDoneCond *isDone);
106 static Datum ExecEvalCaseTestExpr(ExprState *exprstate,
107 ExprContext *econtext,
108 bool *isNull, ExprDoneCond *isDone);
109 static Datum ExecEvalArray(ArrayExprState *astate,
110 ExprContext *econtext,
111 bool *isNull, ExprDoneCond *isDone);
112 static Datum ExecEvalRow(RowExprState *rstate,
113 ExprContext *econtext,
114 bool *isNull, ExprDoneCond *isDone);
115 static Datum ExecEvalRowCompare(RowCompareExprState *rstate,
116 ExprContext *econtext,
117 bool *isNull, ExprDoneCond *isDone);
118 static Datum ExecEvalCoalesce(CoalesceExprState *coalesceExpr,
119 ExprContext *econtext,
120 bool *isNull, ExprDoneCond *isDone);
121 static Datum ExecEvalMinMax(MinMaxExprState *minmaxExpr,
122 ExprContext *econtext,
123 bool *isNull, ExprDoneCond *isDone);
124 static Datum ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
125 bool *isNull, ExprDoneCond *isDone);
126 static Datum ExecEvalNullIf(FuncExprState *nullIfExpr,
127 ExprContext *econtext,
128 bool *isNull, ExprDoneCond *isDone);
129 static Datum ExecEvalNullTest(NullTestState *nstate,
130 ExprContext *econtext,
131 bool *isNull, ExprDoneCond *isDone);
132 static Datum ExecEvalBooleanTest(GenericExprState *bstate,
133 ExprContext *econtext,
134 bool *isNull, ExprDoneCond *isDone);
135 static Datum ExecEvalCoerceToDomain(CoerceToDomainState *cstate,
136 ExprContext *econtext,
137 bool *isNull, ExprDoneCond *isDone);
138 static Datum ExecEvalCoerceToDomainValue(ExprState *exprstate,
139 ExprContext *econtext,
140 bool *isNull, ExprDoneCond *isDone);
141 static Datum ExecEvalFieldSelect(FieldSelectState *fstate,
142 ExprContext *econtext,
143 bool *isNull, ExprDoneCond *isDone);
144 static Datum ExecEvalFieldStore(FieldStoreState *fstate,
145 ExprContext *econtext,
146 bool *isNull, ExprDoneCond *isDone);
147 static Datum ExecEvalRelabelType(GenericExprState *exprstate,
148 ExprContext *econtext,
149 bool *isNull, ExprDoneCond *isDone);
150 static Datum ExecEvalCoerceViaIO(CoerceViaIOState *iostate,
151 ExprContext *econtext,
152 bool *isNull, ExprDoneCond *isDone);
153 static Datum ExecEvalArrayCoerceExpr(ArrayCoerceExprState *astate,
154 ExprContext *econtext,
155 bool *isNull, ExprDoneCond *isDone);
156 static Datum ExecEvalCurrentOfExpr(ExprState *exprstate, ExprContext *econtext,
157 bool *isNull, ExprDoneCond *isDone);
160 /* ----------------------------------------------------------------
161 * ExecEvalExpr routines
163 * Recursively evaluate a targetlist or qualification expression.
165 * Each of the following routines having the signature
166 * Datum ExecEvalFoo(ExprState *expression,
167 * ExprContext *econtext,
169 * ExprDoneCond *isDone);
170 * is responsible for evaluating one type or subtype of ExprState node.
171 * They are normally called via the ExecEvalExpr macro, which makes use of
172 * the function pointer set up when the ExprState node was built by
173 * ExecInitExpr. (In some cases, we change this pointer later to avoid
174 * re-executing one-time overhead.)
176 * Note: for notational simplicity we declare these functions as taking the
177 * specific type of ExprState that they work on. This requires casting when
178 * assigning the function pointer in ExecInitExpr. Be careful that the
179 * function signature is declared correctly, because the cast suppresses
180 * automatic checking!
183 * All these functions share this calling convention:
186 * expression: the expression state tree to evaluate
187 * econtext: evaluation context information
190 * return value: Datum value of result
191 * *isNull: set to TRUE if result is NULL (actual return value is
192 * meaningless if so); set to FALSE if non-null result
193 * *isDone: set to indicator of set-result status
195 * A caller that can only accept a singleton (non-set) result should pass
196 * NULL for isDone; if the expression computes a set result then an error
197 * will be reported via ereport. If the caller does pass an isDone pointer
198 * then *isDone is set to one of these three states:
199 * ExprSingleResult singleton result (not a set)
200 * ExprMultipleResult return value is one element of a set
201 * ExprEndResult there are no more elements in the set
202 * When ExprMultipleResult is returned, the caller should invoke
203 * ExecEvalExpr() repeatedly until ExprEndResult is returned. ExprEndResult
204 * is returned after the last real set element. For convenience isNull will
205 * always be set TRUE when ExprEndResult is returned, but this should not be
206 * taken as indicating a NULL element of the set. Note that these return
207 * conventions allow us to distinguish among a singleton NULL, a NULL element
208 * of a set, and an empty set.
210 * The caller should already have switched into the temporary memory
211 * context econtext->ecxt_per_tuple_memory. The convenience entry point
212 * ExecEvalExprSwitchContext() is provided for callers who don't prefer to
213 * do the switch in an outer loop. We do not do the switch in these routines
214 * because it'd be a waste of cycles during nested expression evaluation.
215 * ----------------------------------------------------------------
222 * This function takes an ArrayRef and returns the extracted Datum
223 * if it's a simple reference, or the modified array value if it's
224 * an array assignment (i.e., array element or slice insertion).
226 * NOTE: if we get a NULL result from a subscript expression, we return NULL
227 * when it's an array reference, or raise an error when it's an assignment.
229 * NOTE: we deliberately refrain from applying DatumGetArrayTypeP() here,
230 * even though that might seem natural, because this code needs to support
231 * both varlena arrays and fixed-length array types. DatumGetArrayTypeP()
232 * only works for the varlena kind. The routines we call in arrayfuncs.c
233 * have to know the difference (that's what they need refattrlength for).
237 ExecEvalArrayRef(ArrayRefExprState *astate,
238 ExprContext *econtext,
240 ExprDoneCond *isDone)
242 ArrayRef *arrayRef = (ArrayRef *) astate->xprstate.expr;
243 ArrayType *array_source;
244 ArrayType *resultArray;
245 bool isAssignment = (arrayRef->refassgnexpr != NULL);
254 array_source = (ArrayType *)
255 DatumGetPointer(ExecEvalExpr(astate->refexpr,
261 * If refexpr yields NULL, and it's a fetch, then result is NULL. In the
262 * assignment case, we'll cons up something below.
266 if (isDone && *isDone == ExprEndResult)
267 return (Datum) NULL; /* end of set result */
272 foreach(l, astate->refupperindexpr)
274 ExprState *eltstate = (ExprState *) lfirst(l);
278 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
279 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
282 upper.indx[i++] = DatumGetInt32(ExecEvalExpr(eltstate,
286 /* If any index expr yields NULL, result is NULL or error */
291 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
292 errmsg("array subscript in assignment must not be null")));
298 if (astate->reflowerindexpr != NIL)
300 foreach(l, astate->reflowerindexpr)
302 ExprState *eltstate = (ExprState *) lfirst(l);
306 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
307 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
310 lower.indx[j++] = DatumGetInt32(ExecEvalExpr(eltstate,
314 /* If any index expr yields NULL, result is NULL or error */
319 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
320 errmsg("array subscript in assignment must not be null")));
325 /* this can't happen unless parser messed up */
327 elog(ERROR, "upper and lower index lists are not same length");
338 * Evaluate the value to be assigned into the array.
340 * XXX At some point we'll need to look into making the old value of
341 * the array element available via CaseTestExpr, as is done by
342 * ExecEvalFieldStore. This is not needed now but will be needed to
343 * support arrays of composite types; in an assignment to a field of
344 * an array member, the parser would generate a FieldStore that
345 * expects to fetch its input tuple via CaseTestExpr.
347 sourceData = ExecEvalExpr(astate->refassgnexpr,
353 * For an assignment to a fixed-length array type, both the original
354 * array and the value to be assigned into it must be non-NULL, else
355 * we punt and return the original array.
357 if (astate->refattrlength > 0) /* fixed-length array? */
358 if (eisnull || *isNull)
359 return PointerGetDatum(array_source);
362 * For assignment to varlena arrays, we handle a NULL original array
363 * by substituting an empty (zero-dimensional) array; insertion of the
364 * new element will result in a singleton array value. It does not
365 * matter whether the new element is NULL.
369 array_source = construct_empty_array(arrayRef->refelemtype);
374 resultArray = array_set(array_source, i,
378 astate->refattrlength,
379 astate->refelemlength,
380 astate->refelembyval,
381 astate->refelemalign);
383 resultArray = array_set_slice(array_source, i,
384 upper.indx, lower.indx,
385 (ArrayType *) DatumGetPointer(sourceData),
387 astate->refattrlength,
388 astate->refelemlength,
389 astate->refelembyval,
390 astate->refelemalign);
391 return PointerGetDatum(resultArray);
395 return array_ref(array_source, i, upper.indx,
396 astate->refattrlength,
397 astate->refelemlength,
398 astate->refelembyval,
399 astate->refelemalign,
403 resultArray = array_get_slice(array_source, i,
404 upper.indx, lower.indx,
405 astate->refattrlength,
406 astate->refelemlength,
407 astate->refelembyval,
408 astate->refelemalign);
409 return PointerGetDatum(resultArray);
414 /* ----------------------------------------------------------------
417 * Returns a Datum whose value is the value of the precomputed
418 * aggregate found in the given expression context.
419 * ----------------------------------------------------------------
422 ExecEvalAggref(AggrefExprState *aggref, ExprContext *econtext,
423 bool *isNull, ExprDoneCond *isDone)
426 *isDone = ExprSingleResult;
428 if (econtext->ecxt_aggvalues == NULL) /* safety check */
429 elog(ERROR, "no aggregates in this expression context");
431 *isNull = econtext->ecxt_aggnulls[aggref->aggno];
432 return econtext->ecxt_aggvalues[aggref->aggno];
435 /* ----------------------------------------------------------------
438 * Returns a Datum whose value is the value of a range
439 * variable with respect to given expression context.
441 * Note: ExecEvalVar is executed only the first time through in a given plan;
442 * it changes the ExprState's function pointer to pass control directly to
443 * ExecEvalScalarVar, ExecEvalWholeRowVar, or ExecEvalWholeRowSlow after
444 * making one-time checks.
445 * ----------------------------------------------------------------
448 ExecEvalVar(ExprState *exprstate, ExprContext *econtext,
449 bool *isNull, ExprDoneCond *isDone)
451 Var *variable = (Var *) exprstate->expr;
452 TupleTableSlot *slot;
456 *isDone = ExprSingleResult;
459 * Get the input slot and attribute number we want
461 * The asserts check that references to system attributes only appear at
462 * the level of a relation scan; at higher levels, system attributes must
463 * be treated as ordinary variables (since we no longer have access to the
466 attnum = variable->varattno;
468 switch (variable->varno)
470 case INNER: /* get the tuple from the inner node */
471 slot = econtext->ecxt_innertuple;
475 case OUTER: /* get the tuple from the outer node */
476 slot = econtext->ecxt_outertuple;
480 default: /* get the tuple from the relation being
482 slot = econtext->ecxt_scantuple;
486 if (attnum != InvalidAttrNumber)
489 * Scalar variable case.
491 * If it's a user attribute, check validity (bogus system attnums will
492 * be caught inside slot_getattr). What we have to check for here
493 * is the possibility of an attribute having been changed in type
494 * since the plan tree was created. Ideally the plan would get
495 * invalidated and not re-used, but until that day arrives, we need
496 * defenses. Fortunately it's sufficient to check once on the first
499 * Note: we allow a reference to a dropped attribute. slot_getattr
500 * will force a NULL result in such cases.
502 * Note: ideally we'd check typmod as well as typid, but that seems
503 * impractical at the moment: in many cases the tupdesc will have
504 * been generated by ExecTypeFromTL(), and that can't guarantee to
505 * generate an accurate typmod in all cases, because some expression
506 * node types don't carry typmod.
510 TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
511 Form_pg_attribute attr;
513 if (attnum > slot_tupdesc->natts) /* should never happen */
514 elog(ERROR, "attribute number %d exceeds number of columns %d",
515 attnum, slot_tupdesc->natts);
517 attr = slot_tupdesc->attrs[attnum - 1];
519 /* can't check type if dropped, since atttypid is probably 0 */
520 if (!attr->attisdropped)
522 if (variable->vartype != attr->atttypid)
524 (errmsg("attribute %d has wrong type", attnum),
525 errdetail("Table has type %s, but query expects %s.",
526 format_type_be(attr->atttypid),
527 format_type_be(variable->vartype))));
531 /* Skip the checking on future executions of node */
532 exprstate->evalfunc = ExecEvalScalarVar;
534 /* Fetch the value from the slot */
535 return slot_getattr(slot, attnum, isNull);
540 * Whole-row variable.
542 * If it's a RECORD Var, we'll use the slot's type ID info. It's
543 * likely that the slot's type is also RECORD; if so, make sure it's
544 * been "blessed", so that the Datum can be interpreted later.
546 * If the Var identifies a named composite type, we must check that
547 * the actual tuple type is compatible with it.
549 TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
550 bool needslow = false;
552 if (variable->vartype == RECORDOID)
554 if (slot_tupdesc->tdtypeid == RECORDOID &&
555 slot_tupdesc->tdtypmod < 0)
556 assign_record_type_typmod(slot_tupdesc);
560 TupleDesc var_tupdesc;
564 * We really only care about number of attributes and data type.
565 * Also, we can ignore type mismatch on columns that are dropped
566 * in the destination type, so long as the physical storage
567 * matches. This is helpful in some cases involving out-of-date
568 * cached plans. Also, we have to allow the case that the slot
569 * has more columns than the Var's type, because we might be
570 * looking at the output of a subplan that includes resjunk
571 * columns. (XXX it would be nice to verify that the extra
572 * columns are all marked resjunk, but we haven't got access to
573 * the subplan targetlist here...) Resjunk columns should always
574 * be at the end of a targetlist, so it's sufficient to ignore
575 * them here; but we need to use ExecEvalWholeRowSlow to get
576 * rid of them in the eventual output tuples.
578 var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1);
580 if (var_tupdesc->natts > slot_tupdesc->natts)
582 (errcode(ERRCODE_DATATYPE_MISMATCH),
583 errmsg("table row type and query-specified row type do not match"),
584 errdetail("Table row contains %d attributes, but query expects %d.",
585 slot_tupdesc->natts, var_tupdesc->natts)));
586 else if (var_tupdesc->natts < slot_tupdesc->natts)
589 for (i = 0; i < var_tupdesc->natts; i++)
591 Form_pg_attribute vattr = var_tupdesc->attrs[i];
592 Form_pg_attribute sattr = slot_tupdesc->attrs[i];
594 if (vattr->atttypid == sattr->atttypid)
595 continue; /* no worries */
596 if (!vattr->attisdropped)
598 (errcode(ERRCODE_DATATYPE_MISMATCH),
599 errmsg("table row type and query-specified row type do not match"),
600 errdetail("Table has type %s at ordinal position %d, but query expects %s.",
601 format_type_be(sattr->atttypid),
603 format_type_be(vattr->atttypid))));
605 if (vattr->attlen != sattr->attlen ||
606 vattr->attalign != sattr->attalign)
608 (errcode(ERRCODE_DATATYPE_MISMATCH),
609 errmsg("table row type and query-specified row type do not match"),
610 errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
614 ReleaseTupleDesc(var_tupdesc);
617 /* Skip the checking on future executions of node */
619 exprstate->evalfunc = ExecEvalWholeRowSlow;
621 exprstate->evalfunc = ExecEvalWholeRowVar;
623 /* Fetch the value */
624 return ExecEvalWholeRowVar(exprstate, econtext, isNull, isDone);
628 /* ----------------------------------------------------------------
631 * Returns a Datum for a scalar variable.
632 * ----------------------------------------------------------------
635 ExecEvalScalarVar(ExprState *exprstate, ExprContext *econtext,
636 bool *isNull, ExprDoneCond *isDone)
638 Var *variable = (Var *) exprstate->expr;
639 TupleTableSlot *slot;
643 *isDone = ExprSingleResult;
645 /* Get the input slot and attribute number we want */
646 switch (variable->varno)
648 case INNER: /* get the tuple from the inner node */
649 slot = econtext->ecxt_innertuple;
652 case OUTER: /* get the tuple from the outer node */
653 slot = econtext->ecxt_outertuple;
656 default: /* get the tuple from the relation being
658 slot = econtext->ecxt_scantuple;
662 attnum = variable->varattno;
664 /* Fetch the value from the slot */
665 return slot_getattr(slot, attnum, isNull);
668 /* ----------------------------------------------------------------
669 * ExecEvalWholeRowVar
671 * Returns a Datum for a whole-row variable.
672 * ----------------------------------------------------------------
675 ExecEvalWholeRowVar(ExprState *exprstate, ExprContext *econtext,
676 bool *isNull, ExprDoneCond *isDone)
678 Var *variable = (Var *) exprstate->expr;
679 TupleTableSlot *slot = econtext->ecxt_scantuple;
682 HeapTupleHeader dtuple;
685 *isDone = ExprSingleResult;
688 tuple = ExecFetchSlotTuple(slot);
689 tupleDesc = slot->tts_tupleDescriptor;
692 * We have to make a copy of the tuple so we can safely insert the Datum
693 * overhead fields, which are not set in on-disk tuples.
695 dtuple = (HeapTupleHeader) palloc(tuple->t_len);
696 memcpy((char *) dtuple, (char *) tuple->t_data, tuple->t_len);
698 HeapTupleHeaderSetDatumLength(dtuple, tuple->t_len);
701 * If the Var identifies a named composite type, label the tuple with that
702 * type; otherwise use what is in the tupleDesc.
704 if (variable->vartype != RECORDOID)
706 HeapTupleHeaderSetTypeId(dtuple, variable->vartype);
707 HeapTupleHeaderSetTypMod(dtuple, variable->vartypmod);
711 HeapTupleHeaderSetTypeId(dtuple, tupleDesc->tdtypeid);
712 HeapTupleHeaderSetTypMod(dtuple, tupleDesc->tdtypmod);
715 return PointerGetDatum(dtuple);
718 /* ----------------------------------------------------------------
719 * ExecEvalWholeRowSlow
721 * Returns a Datum for a whole-row variable, in the "slow" case where
722 * we can't just copy the subplan's output.
723 * ----------------------------------------------------------------
726 ExecEvalWholeRowSlow(ExprState *exprstate, ExprContext *econtext,
727 bool *isNull, ExprDoneCond *isDone)
729 Var *variable = (Var *) exprstate->expr;
730 TupleTableSlot *slot = econtext->ecxt_scantuple;
732 TupleDesc var_tupdesc;
733 HeapTupleHeader dtuple;
736 *isDone = ExprSingleResult;
740 * Currently, the only case handled here is stripping of trailing
741 * resjunk fields, which we do in a slightly chintzy way by just
742 * adjusting the tuple's natts header field. Possibly there will someday
743 * be a need for more-extensive rearrangements, in which case it'd
744 * be worth disassembling and reassembling the tuple (perhaps use a
745 * JunkFilter for that?)
747 Assert(variable->vartype != RECORDOID);
748 var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1);
750 tuple = ExecFetchSlotTuple(slot);
753 * We have to make a copy of the tuple so we can safely insert the Datum
754 * overhead fields, which are not set in on-disk tuples; not to mention
755 * fooling with its natts field.
757 dtuple = (HeapTupleHeader) palloc(tuple->t_len);
758 memcpy((char *) dtuple, (char *) tuple->t_data, tuple->t_len);
760 HeapTupleHeaderSetDatumLength(dtuple, tuple->t_len);
761 HeapTupleHeaderSetTypeId(dtuple, variable->vartype);
762 HeapTupleHeaderSetTypMod(dtuple, variable->vartypmod);
764 Assert(HeapTupleHeaderGetNatts(dtuple) >= var_tupdesc->natts);
765 HeapTupleHeaderSetNatts(dtuple, var_tupdesc->natts);
767 ReleaseTupleDesc(var_tupdesc);
769 return PointerGetDatum(dtuple);
772 /* ----------------------------------------------------------------
775 * Returns the value of a constant.
777 * Note that for pass-by-ref datatypes, we return a pointer to the
778 * actual constant node. This is one of the reasons why functions
779 * must treat their input arguments as read-only.
780 * ----------------------------------------------------------------
783 ExecEvalConst(ExprState *exprstate, ExprContext *econtext,
784 bool *isNull, ExprDoneCond *isDone)
786 Const *con = (Const *) exprstate->expr;
789 *isDone = ExprSingleResult;
791 *isNull = con->constisnull;
792 return con->constvalue;
795 /* ----------------------------------------------------------------
798 * Returns the value of a parameter. A param node contains
799 * something like ($.name) and the expression context contains
800 * the current parameter bindings (name = "sam") (age = 34)...
801 * so our job is to find and return the appropriate datum ("sam").
802 * ----------------------------------------------------------------
805 ExecEvalParam(ExprState *exprstate, ExprContext *econtext,
806 bool *isNull, ExprDoneCond *isDone)
808 Param *expression = (Param *) exprstate->expr;
809 int thisParamId = expression->paramid;
812 *isDone = ExprSingleResult;
814 if (expression->paramkind == PARAM_EXEC)
817 * PARAM_EXEC params (internal executor parameters) are stored in the
818 * ecxt_param_exec_vals array, and can be accessed by array index.
822 prm = &(econtext->ecxt_param_exec_vals[thisParamId]);
823 if (prm->execPlan != NULL)
825 /* Parameter not evaluated yet, so go do it */
826 ExecSetParamPlan(prm->execPlan, econtext);
827 /* ExecSetParamPlan should have processed this param... */
828 Assert(prm->execPlan == NULL);
830 *isNull = prm->isnull;
836 * PARAM_EXTERN parameters must be sought in ecxt_param_list_info.
838 ParamListInfo paramInfo = econtext->ecxt_param_list_info;
840 Assert(expression->paramkind == PARAM_EXTERN);
842 thisParamId > 0 && thisParamId <= paramInfo->numParams)
844 ParamExternData *prm = ¶mInfo->params[thisParamId - 1];
846 if (OidIsValid(prm->ptype))
848 Assert(prm->ptype == expression->paramtype);
849 *isNull = prm->isnull;
854 (errcode(ERRCODE_UNDEFINED_OBJECT),
855 errmsg("no value found for parameter %d", thisParamId)));
856 return (Datum) 0; /* keep compiler quiet */
861 /* ----------------------------------------------------------------
862 * ExecEvalOper / ExecEvalFunc support routines
863 * ----------------------------------------------------------------
870 * These functions return the value of the requested attribute
871 * out of the given tuple Datum.
872 * C functions which take a tuple as an argument are expected
873 * to use these. Ex: overpaid(EMP) might call GetAttributeByNum().
874 * Note: these are actually rather slow because they do a typcache
875 * lookup on each call.
878 GetAttributeByNum(HeapTupleHeader tuple,
886 HeapTupleData tmptup;
888 if (!AttributeNumberIsValid(attrno))
889 elog(ERROR, "invalid attribute number %d", attrno);
892 elog(ERROR, "a NULL isNull pointer was passed");
896 /* Kinda bogus but compatible with old behavior... */
901 tupType = HeapTupleHeaderGetTypeId(tuple);
902 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
903 tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
906 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
907 * the fields in the struct just in case user tries to inspect system
910 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
911 ItemPointerSetInvalid(&(tmptup.t_self));
912 tmptup.t_tableOid = InvalidOid;
913 tmptup.t_data = tuple;
915 result = heap_getattr(&tmptup,
920 ReleaseTupleDesc(tupDesc);
926 GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
933 HeapTupleData tmptup;
937 elog(ERROR, "invalid attribute name");
940 elog(ERROR, "a NULL isNull pointer was passed");
944 /* Kinda bogus but compatible with old behavior... */
949 tupType = HeapTupleHeaderGetTypeId(tuple);
950 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
951 tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
953 attrno = InvalidAttrNumber;
954 for (i = 0; i < tupDesc->natts; i++)
956 if (namestrcmp(&(tupDesc->attrs[i]->attname), attname) == 0)
958 attrno = tupDesc->attrs[i]->attnum;
963 if (attrno == InvalidAttrNumber)
964 elog(ERROR, "attribute \"%s\" does not exist", attname);
967 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
968 * the fields in the struct just in case user tries to inspect system
971 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
972 ItemPointerSetInvalid(&(tmptup.t_self));
973 tmptup.t_tableOid = InvalidOid;
974 tmptup.t_data = tuple;
976 result = heap_getattr(&tmptup,
981 ReleaseTupleDesc(tupDesc);
987 * init_fcache - initialize a FuncExprState node during first use
990 init_fcache(Oid foid, FuncExprState *fcache, MemoryContext fcacheCxt)
994 /* Check permission to call function */
995 aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE);
996 if (aclresult != ACLCHECK_OK)
997 aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(foid));
1000 * Safety check on nargs. Under normal circumstances this should never
1001 * fail, as parser should check sooner. But possibly it might fail if
1002 * server has been compiled with FUNC_MAX_ARGS smaller than some functions
1003 * declared in pg_proc?
1005 if (list_length(fcache->args) > FUNC_MAX_ARGS)
1007 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
1008 errmsg("cannot pass more than %d arguments to a function",
1011 /* Set up the primary fmgr lookup information */
1012 fmgr_info_cxt(foid, &(fcache->func), fcacheCxt);
1014 /* Initialize additional info */
1015 fcache->setArgsValid = false;
1016 fcache->shutdown_reg = false;
1017 fcache->func.fn_expr = (Node *) fcache->xprstate.expr;
1021 * callback function in case a FuncExpr returning a set needs to be shut down
1022 * before it has been run to completion
1025 ShutdownFuncExpr(Datum arg)
1027 FuncExprState *fcache = (FuncExprState *) DatumGetPointer(arg);
1029 /* Clear any active set-argument state */
1030 fcache->setArgsValid = false;
1032 /* execUtils will deregister the callback... */
1033 fcache->shutdown_reg = false;
1037 * get_cached_rowtype: utility function to lookup a rowtype tupdesc
1039 * type_id, typmod: identity of the rowtype
1040 * cache_field: where to cache the TupleDesc pointer in expression state node
1041 * (field must be initialized to NULL)
1042 * econtext: expression context we are executing in
1044 * NOTE: because the shutdown callback will be called during plan rescan,
1045 * must be prepared to re-do this during any node execution; cannot call
1046 * just once during expression initialization
1049 get_cached_rowtype(Oid type_id, int32 typmod,
1050 TupleDesc *cache_field, ExprContext *econtext)
1052 TupleDesc tupDesc = *cache_field;
1054 /* Do lookup if no cached value or if requested type changed */
1055 if (tupDesc == NULL ||
1056 type_id != tupDesc->tdtypeid ||
1057 typmod != tupDesc->tdtypmod)
1059 tupDesc = lookup_rowtype_tupdesc(type_id, typmod);
1063 /* Release old tupdesc; but callback is already registered */
1064 ReleaseTupleDesc(*cache_field);
1068 /* Need to register shutdown callback to release tupdesc */
1069 RegisterExprContextCallback(econtext,
1070 ShutdownTupleDescRef,
1071 PointerGetDatum(cache_field));
1073 *cache_field = tupDesc;
1079 * Callback function to release a tupdesc refcount at expression tree shutdown
1082 ShutdownTupleDescRef(Datum arg)
1084 TupleDesc *cache_field = (TupleDesc *) DatumGetPointer(arg);
1087 ReleaseTupleDesc(*cache_field);
1088 *cache_field = NULL;
1092 * Evaluate arguments for a function.
1095 ExecEvalFuncArgs(FunctionCallInfo fcinfo,
1097 ExprContext *econtext)
1099 ExprDoneCond argIsDone;
1103 argIsDone = ExprSingleResult; /* default assumption */
1106 foreach(arg, argList)
1108 ExprState *argstate = (ExprState *) lfirst(arg);
1109 ExprDoneCond thisArgIsDone;
1111 fcinfo->arg[i] = ExecEvalExpr(argstate,
1113 &fcinfo->argnull[i],
1116 if (thisArgIsDone != ExprSingleResult)
1119 * We allow only one argument to have a set value; we'd need much
1120 * more complexity to keep track of multiple set arguments (cf.
1121 * ExecTargetList) and it doesn't seem worth it.
1123 if (argIsDone != ExprSingleResult)
1125 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1126 errmsg("functions and operators can take at most one set argument")));
1127 argIsDone = thisArgIsDone;
1138 * ExecMakeFunctionResult
1140 * Evaluate the arguments to a function and then the function itself.
1143 ExecMakeFunctionResult(FuncExprState *fcache,
1144 ExprContext *econtext,
1146 ExprDoneCond *isDone)
1148 List *arguments = fcache->args;
1150 FunctionCallInfoData fcinfo;
1151 ReturnSetInfo rsinfo; /* for functions returning sets */
1152 ExprDoneCond argDone;
1156 /* Guard against stack overflow due to overly complex expressions */
1157 check_stack_depth();
1160 * arguments is a list of expressions to evaluate before passing to the
1161 * function manager. We skip the evaluation if it was already done in the
1162 * previous call (ie, we are continuing the evaluation of a set-valued
1163 * function). Otherwise, collect the current argument values into fcinfo.
1165 if (!fcache->setArgsValid)
1167 /* Need to prep callinfo structure */
1168 InitFunctionCallInfoData(fcinfo, &(fcache->func), 0, NULL, NULL);
1169 argDone = ExecEvalFuncArgs(&fcinfo, arguments, econtext);
1170 if (argDone == ExprEndResult)
1172 /* input is an empty set, so return an empty set. */
1175 *isDone = ExprEndResult;
1178 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1179 errmsg("set-valued function called in context that cannot accept a set")));
1182 hasSetArg = (argDone != ExprSingleResult);
1186 /* Copy callinfo from previous evaluation */
1187 memcpy(&fcinfo, &fcache->setArgs, sizeof(fcinfo));
1188 hasSetArg = fcache->setHasSetArg;
1189 /* Reset flag (we may set it again below) */
1190 fcache->setArgsValid = false;
1194 * If function returns set, prepare a resultinfo node for communication
1196 if (fcache->func.fn_retset)
1198 fcinfo.resultinfo = (Node *) &rsinfo;
1199 rsinfo.type = T_ReturnSetInfo;
1200 rsinfo.econtext = econtext;
1201 rsinfo.expectedDesc = NULL;
1202 rsinfo.allowedModes = (int) SFRM_ValuePerCall;
1203 rsinfo.returnMode = SFRM_ValuePerCall;
1204 /* isDone is filled below */
1205 rsinfo.setResult = NULL;
1206 rsinfo.setDesc = NULL;
1210 * now return the value gotten by calling the function manager, passing
1211 * the function the evaluated parameter values.
1213 if (fcache->func.fn_retset || hasSetArg)
1216 * We need to return a set result. Complain if caller not ready to
1221 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1222 errmsg("set-valued function called in context that cannot accept a set")));
1225 * This loop handles the situation where we have both a set argument
1226 * and a set-valued function. Once we have exhausted the function's
1227 * value(s) for a particular argument value, we have to get the next
1228 * argument value and start the function over again. We might have to
1229 * do it more than once, if the function produces an empty result set
1230 * for a particular input value.
1235 * If function is strict, and there are any NULL arguments, skip
1236 * calling the function (at least for this set of args).
1240 if (fcache->func.fn_strict)
1242 for (i = 0; i < fcinfo.nargs; i++)
1244 if (fcinfo.argnull[i])
1254 fcinfo.isnull = false;
1255 rsinfo.isDone = ExprSingleResult;
1256 result = FunctionCallInvoke(&fcinfo);
1257 *isNull = fcinfo.isnull;
1258 *isDone = rsinfo.isDone;
1264 *isDone = ExprEndResult;
1267 if (*isDone != ExprEndResult)
1270 * Got a result from current argument. If function itself
1271 * returns set, save the current argument values to re-use on
1274 if (fcache->func.fn_retset && *isDone == ExprMultipleResult)
1276 memcpy(&fcache->setArgs, &fcinfo, sizeof(fcinfo));
1277 fcache->setHasSetArg = hasSetArg;
1278 fcache->setArgsValid = true;
1279 /* Register cleanup callback if we didn't already */
1280 if (!fcache->shutdown_reg)
1282 RegisterExprContextCallback(econtext,
1284 PointerGetDatum(fcache));
1285 fcache->shutdown_reg = true;
1290 * Make sure we say we are returning a set, even if the
1291 * function itself doesn't return sets.
1294 *isDone = ExprMultipleResult;
1298 /* Else, done with this argument */
1300 break; /* input not a set, so done */
1302 /* Re-eval args to get the next element of the input set */
1303 argDone = ExecEvalFuncArgs(&fcinfo, arguments, econtext);
1305 if (argDone != ExprMultipleResult)
1307 /* End of argument set, so we're done. */
1309 *isDone = ExprEndResult;
1315 * If we reach here, loop around to run the function on the new
1323 * Non-set case: much easier.
1325 * We change the ExprState function pointer to use the simpler
1326 * ExecMakeFunctionResultNoSets on subsequent calls. This amounts to
1327 * assuming that no argument can return a set if it didn't do so the
1330 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResultNoSets;
1333 *isDone = ExprSingleResult;
1336 * If function is strict, and there are any NULL arguments, skip
1337 * calling the function and return NULL.
1339 if (fcache->func.fn_strict)
1341 for (i = 0; i < fcinfo.nargs; i++)
1343 if (fcinfo.argnull[i])
1350 fcinfo.isnull = false;
1351 result = FunctionCallInvoke(&fcinfo);
1352 *isNull = fcinfo.isnull;
1359 * ExecMakeFunctionResultNoSets
1361 * Simplified version of ExecMakeFunctionResult that can only handle
1362 * non-set cases. Hand-tuned for speed.
1365 ExecMakeFunctionResultNoSets(FuncExprState *fcache,
1366 ExprContext *econtext,
1368 ExprDoneCond *isDone)
1372 FunctionCallInfoData fcinfo;
1375 /* Guard against stack overflow due to overly complex expressions */
1376 check_stack_depth();
1379 *isDone = ExprSingleResult;
1381 /* inlined, simplified version of ExecEvalFuncArgs */
1383 foreach(arg, fcache->args)
1385 ExprState *argstate = (ExprState *) lfirst(arg);
1387 fcinfo.arg[i] = ExecEvalExpr(argstate,
1394 InitFunctionCallInfoData(fcinfo, &(fcache->func), i, NULL, NULL);
1397 * If function is strict, and there are any NULL arguments, skip calling
1398 * the function and return NULL.
1400 if (fcache->func.fn_strict)
1404 if (fcinfo.argnull[i])
1411 /* fcinfo.isnull = false; */ /* handled by InitFunctionCallInfoData */
1412 result = FunctionCallInvoke(&fcinfo);
1413 *isNull = fcinfo.isnull;
1420 * ExecMakeTableFunctionResult
1422 * Evaluate a table function, producing a materialized result in a Tuplestore
1423 * object. *returnDesc is set to the tupledesc actually returned by the
1424 * function, or NULL if it didn't provide one.
1427 ExecMakeTableFunctionResult(ExprState *funcexpr,
1428 ExprContext *econtext,
1429 TupleDesc expectedDesc,
1430 TupleDesc *returnDesc)
1432 Tuplestorestate *tupstore = NULL;
1433 TupleDesc tupdesc = NULL;
1436 bool returnsSet = false;
1437 FunctionCallInfoData fcinfo;
1438 ReturnSetInfo rsinfo;
1439 HeapTupleData tmptup;
1440 MemoryContext callerContext;
1441 MemoryContext oldcontext;
1442 bool direct_function_call;
1443 bool first_time = true;
1445 callerContext = CurrentMemoryContext;
1447 funcrettype = exprType((Node *) funcexpr->expr);
1449 returnsTuple = type_is_rowtype(funcrettype);
1452 * Prepare a resultinfo node for communication. We always do this even if
1453 * not expecting a set result, so that we can pass expectedDesc. In the
1454 * generic-expression case, the expression doesn't actually get to see the
1455 * resultinfo, but set it up anyway because we use some of the fields as
1456 * our own state variables.
1458 InitFunctionCallInfoData(fcinfo, NULL, 0, NULL, (Node *) &rsinfo);
1459 rsinfo.type = T_ReturnSetInfo;
1460 rsinfo.econtext = econtext;
1461 rsinfo.expectedDesc = expectedDesc;
1462 rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize);
1463 rsinfo.returnMode = SFRM_ValuePerCall;
1464 /* isDone is filled below */
1465 rsinfo.setResult = NULL;
1466 rsinfo.setDesc = NULL;
1469 * Normally the passed expression tree will be a FuncExprState, since the
1470 * grammar only allows a function call at the top level of a table
1471 * function reference. However, if the function doesn't return set then
1472 * the planner might have replaced the function call via constant-folding
1473 * or inlining. So if we see any other kind of expression node, execute
1474 * it via the general ExecEvalExpr() code; the only difference is that we
1475 * don't get a chance to pass a special ReturnSetInfo to any functions
1476 * buried in the expression.
1478 if (funcexpr && IsA(funcexpr, FuncExprState) &&
1479 IsA(funcexpr->expr, FuncExpr))
1481 FuncExprState *fcache = (FuncExprState *) funcexpr;
1482 ExprDoneCond argDone;
1485 * This path is similar to ExecMakeFunctionResult.
1487 direct_function_call = true;
1490 * Initialize function cache if first time through
1492 if (fcache->func.fn_oid == InvalidOid)
1494 FuncExpr *func = (FuncExpr *) fcache->xprstate.expr;
1496 init_fcache(func->funcid, fcache, econtext->ecxt_per_query_memory);
1498 returnsSet = fcache->func.fn_retset;
1501 * Evaluate the function's argument list.
1503 * Note: ideally, we'd do this in the per-tuple context, but then the
1504 * argument values would disappear when we reset the context in the
1505 * inner loop. So do it in caller context. Perhaps we should make a
1506 * separate context just to hold the evaluated arguments?
1508 fcinfo.flinfo = &(fcache->func);
1509 argDone = ExecEvalFuncArgs(&fcinfo, fcache->args, econtext);
1510 /* We don't allow sets in the arguments of the table function */
1511 if (argDone != ExprSingleResult)
1513 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1514 errmsg("set-valued function called in context that cannot accept a set")));
1517 * If function is strict, and there are any NULL arguments, skip
1518 * calling the function and act like it returned NULL (or an empty
1519 * set, in the returns-set case).
1521 if (fcache->func.fn_strict)
1525 for (i = 0; i < fcinfo.nargs; i++)
1527 if (fcinfo.argnull[i])
1528 goto no_function_result;
1534 /* Treat funcexpr as a generic expression */
1535 direct_function_call = false;
1539 * Switch to short-lived context for calling the function or expression.
1541 MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1544 * Loop to handle the ValuePerCall protocol (which is also the same
1545 * behavior needed in the generic ExecEvalExpr path).
1552 CHECK_FOR_INTERRUPTS();
1555 * reset per-tuple memory context before each call of the function or
1556 * expression. This cleans up any local memory the function may leak
1559 ResetExprContext(econtext);
1561 /* Call the function or expression one time */
1562 if (direct_function_call)
1564 fcinfo.isnull = false;
1565 rsinfo.isDone = ExprSingleResult;
1566 result = FunctionCallInvoke(&fcinfo);
1570 result = ExecEvalExpr(funcexpr, econtext,
1571 &fcinfo.isnull, &rsinfo.isDone);
1574 /* Which protocol does function want to use? */
1575 if (rsinfo.returnMode == SFRM_ValuePerCall)
1578 * Check for end of result set.
1580 if (rsinfo.isDone == ExprEndResult)
1584 * Can't do anything very useful with NULL rowtype values. For a
1585 * function returning set, we consider this a protocol violation
1586 * (but another alternative would be to just ignore the result and
1587 * "continue" to get another row). For a function not returning
1588 * set, we fall out of the loop; we'll cons up an all-nulls result
1591 if (returnsTuple && fcinfo.isnull)
1596 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1597 errmsg("function returning set of rows cannot return null value")));
1601 * If first time through, build tupdesc and tuplestore for result
1605 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1609 * Use the type info embedded in the rowtype Datum to look
1610 * up the needed tupdesc. Make a copy for the query.
1614 td = DatumGetHeapTupleHeader(result);
1615 tupdesc = lookup_rowtype_tupdesc_copy(HeapTupleHeaderGetTypeId(td),
1616 HeapTupleHeaderGetTypMod(td));
1621 * Scalar type, so make a single-column descriptor
1623 tupdesc = CreateTemplateTupleDesc(1, false);
1624 TupleDescInitEntry(tupdesc,
1631 tupstore = tuplestore_begin_heap(true, false, work_mem);
1632 MemoryContextSwitchTo(oldcontext);
1633 rsinfo.setResult = tupstore;
1634 rsinfo.setDesc = tupdesc;
1638 * Store current resultset item.
1644 td = DatumGetHeapTupleHeader(result);
1647 * tuplestore_puttuple needs a HeapTuple not a bare
1648 * HeapTupleHeader, but it doesn't need all the fields.
1650 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1656 tuple = heap_form_tuple(tupdesc, &result, &fcinfo.isnull);
1659 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1660 tuplestore_puttuple(tupstore, tuple);
1661 MemoryContextSwitchTo(oldcontext);
1666 if (rsinfo.isDone != ExprMultipleResult)
1669 else if (rsinfo.returnMode == SFRM_Materialize)
1671 /* check we're on the same page as the function author */
1672 if (!first_time || rsinfo.isDone != ExprSingleResult)
1674 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1675 errmsg("table-function protocol for materialize mode was not followed")));
1676 /* Done evaluating the set result */
1681 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1682 errmsg("unrecognized table-function returnMode: %d",
1683 (int) rsinfo.returnMode)));
1691 * If we got nothing from the function (ie, an empty-set or NULL result),
1692 * we have to create the tuplestore to return, and if it's a
1693 * non-set-returning function then insert a single all-nulls row.
1695 if (rsinfo.setResult == NULL)
1697 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1698 tupstore = tuplestore_begin_heap(true, false, work_mem);
1699 rsinfo.setResult = tupstore;
1702 int natts = expectedDesc->natts;
1707 MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1708 nulldatums = (Datum *) palloc0(natts * sizeof(Datum));
1709 nullflags = (bool *) palloc(natts * sizeof(bool));
1710 memset(nullflags, true, natts * sizeof(bool));
1711 tuple = heap_form_tuple(expectedDesc, nulldatums, nullflags);
1712 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1713 tuplestore_puttuple(tupstore, tuple);
1717 MemoryContextSwitchTo(callerContext);
1719 /* The returned pointers are those in rsinfo */
1720 *returnDesc = rsinfo.setDesc;
1721 return rsinfo.setResult;
1725 /* ----------------------------------------------------------------
1729 * Evaluate the functional result of a list of arguments by calling the
1731 * ----------------------------------------------------------------
1734 /* ----------------------------------------------------------------
1736 * ----------------------------------------------------------------
1739 ExecEvalFunc(FuncExprState *fcache,
1740 ExprContext *econtext,
1742 ExprDoneCond *isDone)
1744 /* This is called only the first time through */
1745 FuncExpr *func = (FuncExpr *) fcache->xprstate.expr;
1747 /* Initialize function lookup info */
1748 init_fcache(func->funcid, fcache, econtext->ecxt_per_query_memory);
1750 /* Go directly to ExecMakeFunctionResult on subsequent uses */
1751 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
1753 return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
1756 /* ----------------------------------------------------------------
1758 * ----------------------------------------------------------------
1761 ExecEvalOper(FuncExprState *fcache,
1762 ExprContext *econtext,
1764 ExprDoneCond *isDone)
1766 /* This is called only the first time through */
1767 OpExpr *op = (OpExpr *) fcache->xprstate.expr;
1769 /* Initialize function lookup info */
1770 init_fcache(op->opfuncid, fcache, econtext->ecxt_per_query_memory);
1772 /* Go directly to ExecMakeFunctionResult on subsequent uses */
1773 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
1775 return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
1778 /* ----------------------------------------------------------------
1781 * IS DISTINCT FROM must evaluate arguments to determine whether
1782 * they are NULL; if either is NULL then the result is already
1783 * known. If neither is NULL, then proceed to evaluate the
1784 * function. Note that this is *always* derived from the equals
1785 * operator, but since we need special processing of the arguments
1786 * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
1787 * ----------------------------------------------------------------
1790 ExecEvalDistinct(FuncExprState *fcache,
1791 ExprContext *econtext,
1793 ExprDoneCond *isDone)
1796 FunctionCallInfoData fcinfo;
1797 ExprDoneCond argDone;
1800 /* Set default values for result flags: non-null, not a set result */
1803 *isDone = ExprSingleResult;
1806 * Initialize function cache if first time through
1808 if (fcache->func.fn_oid == InvalidOid)
1810 DistinctExpr *op = (DistinctExpr *) fcache->xprstate.expr;
1812 init_fcache(op->opfuncid, fcache, econtext->ecxt_per_query_memory);
1813 Assert(!fcache->func.fn_retset);
1817 * extract info from fcache
1819 argList = fcache->args;
1821 /* Need to prep callinfo structure */
1822 InitFunctionCallInfoData(fcinfo, &(fcache->func), 0, NULL, NULL);
1823 argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
1824 if (argDone != ExprSingleResult)
1826 (errcode(ERRCODE_DATATYPE_MISMATCH),
1827 errmsg("IS DISTINCT FROM does not support set arguments")));
1828 Assert(fcinfo.nargs == 2);
1830 if (fcinfo.argnull[0] && fcinfo.argnull[1])
1832 /* Both NULL? Then is not distinct... */
1833 result = BoolGetDatum(FALSE);
1835 else if (fcinfo.argnull[0] || fcinfo.argnull[1])
1837 /* Only one is NULL? Then is distinct... */
1838 result = BoolGetDatum(TRUE);
1842 fcinfo.isnull = false;
1843 result = FunctionCallInvoke(&fcinfo);
1844 *isNull = fcinfo.isnull;
1845 /* Must invert result of "=" */
1846 result = BoolGetDatum(!DatumGetBool(result));
1853 * ExecEvalScalarArrayOp
1855 * Evaluate "scalar op ANY/ALL (array)". The operator always yields boolean,
1856 * and we combine the results across all array elements using OR and AND
1857 * (for ANY and ALL respectively). Of course we short-circuit as soon as
1858 * the result is known.
1861 ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
1862 ExprContext *econtext,
1863 bool *isNull, ExprDoneCond *isDone)
1865 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) sstate->fxprstate.xprstate.expr;
1866 bool useOr = opexpr->useOr;
1871 FunctionCallInfoData fcinfo;
1872 ExprDoneCond argDone;
1881 /* Set default values for result flags: non-null, not a set result */
1884 *isDone = ExprSingleResult;
1887 * Initialize function cache if first time through
1889 if (sstate->fxprstate.func.fn_oid == InvalidOid)
1891 init_fcache(opexpr->opfuncid, &sstate->fxprstate,
1892 econtext->ecxt_per_query_memory);
1893 Assert(!sstate->fxprstate.func.fn_retset);
1896 /* Need to prep callinfo structure */
1897 InitFunctionCallInfoData(fcinfo, &(sstate->fxprstate.func), 0, NULL, NULL);
1898 argDone = ExecEvalFuncArgs(&fcinfo, sstate->fxprstate.args, econtext);
1899 if (argDone != ExprSingleResult)
1901 (errcode(ERRCODE_DATATYPE_MISMATCH),
1902 errmsg("op ANY/ALL (array) does not support set arguments")));
1903 Assert(fcinfo.nargs == 2);
1906 * If the array is NULL then we return NULL --- it's not very meaningful
1907 * to do anything else, even if the operator isn't strict.
1909 if (fcinfo.argnull[1])
1914 /* Else okay to fetch and detoast the array */
1915 arr = DatumGetArrayTypeP(fcinfo.arg[1]);
1918 * If the array is empty, we return either FALSE or TRUE per the useOr
1919 * flag. This is correct even if the scalar is NULL; since we would
1920 * evaluate the operator zero times, it matters not whether it would want
1923 nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
1925 return BoolGetDatum(!useOr);
1928 * If the scalar is NULL, and the function is strict, return NULL; no
1929 * point in iterating the loop.
1931 if (fcinfo.argnull[0] && sstate->fxprstate.func.fn_strict)
1938 * We arrange to look up info about the element type only once per series
1939 * of calls, assuming the element type doesn't change underneath us.
1941 if (sstate->element_type != ARR_ELEMTYPE(arr))
1943 get_typlenbyvalalign(ARR_ELEMTYPE(arr),
1947 sstate->element_type = ARR_ELEMTYPE(arr);
1949 typlen = sstate->typlen;
1950 typbyval = sstate->typbyval;
1951 typalign = sstate->typalign;
1953 result = BoolGetDatum(!useOr);
1956 /* Loop over the array elements */
1957 s = (char *) ARR_DATA_PTR(arr);
1958 bitmap = ARR_NULLBITMAP(arr);
1961 for (i = 0; i < nitems; i++)
1966 /* Get array element, checking for NULL */
1967 if (bitmap && (*bitmap & bitmask) == 0)
1969 fcinfo.arg[1] = (Datum) 0;
1970 fcinfo.argnull[1] = true;
1974 elt = fetch_att(s, typbyval, typlen);
1975 s = att_addlength_pointer(s, typlen, s);
1976 s = (char *) att_align_nominal(s, typalign);
1977 fcinfo.arg[1] = elt;
1978 fcinfo.argnull[1] = false;
1981 /* Call comparison function */
1982 if (fcinfo.argnull[1] && sstate->fxprstate.func.fn_strict)
1984 fcinfo.isnull = true;
1985 thisresult = (Datum) 0;
1989 fcinfo.isnull = false;
1990 thisresult = FunctionCallInvoke(&fcinfo);
1993 /* Combine results per OR or AND semantics */
1998 if (DatumGetBool(thisresult))
2000 result = BoolGetDatum(true);
2002 break; /* needn't look at any more elements */
2007 if (!DatumGetBool(thisresult))
2009 result = BoolGetDatum(false);
2011 break; /* needn't look at any more elements */
2015 /* advance bitmap pointer if any */
2019 if (bitmask == 0x100)
2027 *isNull = resultnull;
2031 /* ----------------------------------------------------------------
2036 * Evaluate boolean expressions, with appropriate short-circuiting.
2038 * The query planner reformulates clause expressions in the
2039 * qualification to conjunctive normal form. If we ever get
2040 * an AND to evaluate, we can be sure that it's not a top-level
2041 * clause in the qualification, but appears lower (as a function
2042 * argument, for example), or in the target list. Not that you
2043 * need to know this, mind you...
2044 * ----------------------------------------------------------------
2047 ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
2048 bool *isNull, ExprDoneCond *isDone)
2050 ExprState *clause = linitial(notclause->args);
2054 *isDone = ExprSingleResult;
2056 expr_value = ExecEvalExpr(clause, econtext, isNull, NULL);
2059 * if the expression evaluates to null, then we just cascade the null back
2060 * to whoever called us.
2066 * evaluation of 'not' is simple.. expr is false, then return 'true' and
2069 return BoolGetDatum(!DatumGetBool(expr_value));
2072 /* ----------------------------------------------------------------
2074 * ----------------------------------------------------------------
2077 ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
2078 bool *isNull, ExprDoneCond *isDone)
2080 List *clauses = orExpr->args;
2085 *isDone = ExprSingleResult;
2090 * If any of the clauses is TRUE, the OR result is TRUE regardless of the
2091 * states of the rest of the clauses, so we can stop evaluating and return
2092 * TRUE immediately. If none are TRUE and one or more is NULL, we return
2093 * NULL; otherwise we return FALSE. This makes sense when you interpret
2094 * NULL as "don't know": if we have a TRUE then the OR is TRUE even if we
2095 * aren't sure about some of the other inputs. If all the known inputs are
2096 * FALSE, but we have one or more "don't knows", then we have to report
2097 * that we "don't know" what the OR's result should be --- perhaps one of
2098 * the "don't knows" would have been TRUE if we'd known its value. Only
2099 * when all the inputs are known to be FALSE can we state confidently that
2100 * the OR's result is FALSE.
2102 foreach(clause, clauses)
2104 ExprState *clausestate = (ExprState *) lfirst(clause);
2107 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
2110 * if we have a non-null true result, then return it.
2113 AnyNull = true; /* remember we got a null */
2114 else if (DatumGetBool(clause_value))
2115 return clause_value;
2118 /* AnyNull is true if at least one clause evaluated to NULL */
2120 return BoolGetDatum(false);
2123 /* ----------------------------------------------------------------
2125 * ----------------------------------------------------------------
2128 ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
2129 bool *isNull, ExprDoneCond *isDone)
2131 List *clauses = andExpr->args;
2136 *isDone = ExprSingleResult;
2141 * If any of the clauses is FALSE, the AND result is FALSE regardless of
2142 * the states of the rest of the clauses, so we can stop evaluating and
2143 * return FALSE immediately. If none are FALSE and one or more is NULL,
2144 * we return NULL; otherwise we return TRUE. This makes sense when you
2145 * interpret NULL as "don't know", using the same sort of reasoning as for
2149 foreach(clause, clauses)
2151 ExprState *clausestate = (ExprState *) lfirst(clause);
2154 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
2157 * if we have a non-null false result, then return it.
2160 AnyNull = true; /* remember we got a null */
2161 else if (!DatumGetBool(clause_value))
2162 return clause_value;
2165 /* AnyNull is true if at least one clause evaluated to NULL */
2167 return BoolGetDatum(!AnyNull);
2170 /* ----------------------------------------------------------------
2171 * ExecEvalConvertRowtype
2173 * Evaluate a rowtype coercion operation. This may require
2174 * rearranging field positions.
2175 * ----------------------------------------------------------------
2178 ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
2179 ExprContext *econtext,
2180 bool *isNull, ExprDoneCond *isDone)
2182 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) cstate->xprstate.expr;
2185 HeapTupleHeader tuple;
2186 HeapTupleData tmptup;
2187 AttrNumber *attrMap;
2195 tupDatum = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
2197 /* this test covers the isDone exception too: */
2201 tuple = DatumGetHeapTupleHeader(tupDatum);
2203 /* Lookup tupdescs if first time through or after rescan */
2204 if (cstate->indesc == NULL)
2205 get_cached_rowtype(exprType((Node *) convert->arg), -1,
2206 &cstate->indesc, econtext);
2207 if (cstate->outdesc == NULL)
2208 get_cached_rowtype(convert->resulttype, -1,
2209 &cstate->outdesc, econtext);
2211 Assert(HeapTupleHeaderGetTypeId(tuple) == cstate->indesc->tdtypeid);
2212 Assert(HeapTupleHeaderGetTypMod(tuple) == cstate->indesc->tdtypmod);
2214 /* if first time through, initialize */
2215 if (cstate->attrMap == NULL)
2217 MemoryContext old_cxt;
2220 /* allocate state in long-lived memory context */
2221 old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
2223 /* prepare map from old to new attribute numbers */
2224 n = cstate->outdesc->natts;
2225 cstate->attrMap = (AttrNumber *) palloc0(n * sizeof(AttrNumber));
2226 for (i = 0; i < n; i++)
2228 Form_pg_attribute att = cstate->outdesc->attrs[i];
2234 if (att->attisdropped)
2235 continue; /* attrMap[i] is already 0 */
2236 attname = NameStr(att->attname);
2237 atttypid = att->atttypid;
2238 atttypmod = att->atttypmod;
2239 for (j = 0; j < cstate->indesc->natts; j++)
2241 att = cstate->indesc->attrs[j];
2242 if (att->attisdropped)
2244 if (strcmp(attname, NameStr(att->attname)) == 0)
2246 /* Found it, check type */
2247 if (atttypid != att->atttypid || atttypmod != att->atttypmod)
2248 elog(ERROR, "attribute \"%s\" of type %s does not match corresponding attribute of type %s",
2250 format_type_be(cstate->indesc->tdtypeid),
2251 format_type_be(cstate->outdesc->tdtypeid));
2252 cstate->attrMap[i] = (AttrNumber) (j + 1);
2256 if (cstate->attrMap[i] == 0)
2257 elog(ERROR, "attribute \"%s\" of type %s does not exist",
2259 format_type_be(cstate->indesc->tdtypeid));
2261 /* preallocate workspace for Datum arrays */
2262 n = cstate->indesc->natts + 1; /* +1 for NULL */
2263 cstate->invalues = (Datum *) palloc(n * sizeof(Datum));
2264 cstate->inisnull = (bool *) palloc(n * sizeof(bool));
2265 n = cstate->outdesc->natts;
2266 cstate->outvalues = (Datum *) palloc(n * sizeof(Datum));
2267 cstate->outisnull = (bool *) palloc(n * sizeof(bool));
2269 MemoryContextSwitchTo(old_cxt);
2272 attrMap = cstate->attrMap;
2273 invalues = cstate->invalues;
2274 inisnull = cstate->inisnull;
2275 outvalues = cstate->outvalues;
2276 outisnull = cstate->outisnull;
2277 outnatts = cstate->outdesc->natts;
2280 * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader.
2282 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
2283 tmptup.t_data = tuple;
2286 * Extract all the values of the old tuple, offsetting the arrays so that
2287 * invalues[0] is NULL and invalues[1] is the first source attribute; this
2288 * exactly matches the numbering convention in attrMap.
2290 heap_deform_tuple(&tmptup, cstate->indesc, invalues + 1, inisnull + 1);
2291 invalues[0] = (Datum) 0;
2295 * Transpose into proper fields of the new tuple.
2297 for (i = 0; i < outnatts; i++)
2301 outvalues[i] = invalues[j];
2302 outisnull[i] = inisnull[j];
2306 * Now form the new tuple.
2308 result = heap_form_tuple(cstate->outdesc, outvalues, outisnull);
2310 return HeapTupleGetDatum(result);
2313 /* ----------------------------------------------------------------
2316 * Evaluate a CASE clause. Will have boolean expressions
2317 * inside the WHEN clauses, and will have expressions
2319 * - thomas 1998-11-09
2320 * ----------------------------------------------------------------
2323 ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
2324 bool *isNull, ExprDoneCond *isDone)
2326 List *clauses = caseExpr->args;
2332 *isDone = ExprSingleResult;
2335 * If there's a test expression, we have to evaluate it and save the value
2336 * where the CaseTestExpr placeholders can find it. We must save and
2337 * restore prior setting of econtext's caseValue fields, in case this node
2338 * is itself within a larger CASE.
2340 save_datum = econtext->caseValue_datum;
2341 save_isNull = econtext->caseValue_isNull;
2345 econtext->caseValue_datum = ExecEvalExpr(caseExpr->arg,
2347 &econtext->caseValue_isNull,
2352 * we evaluate each of the WHEN clauses in turn, as soon as one is true we
2353 * return the corresponding result. If none are true then we return the
2354 * value of the default clause, or NULL if there is none.
2356 foreach(clause, clauses)
2358 CaseWhenState *wclause = lfirst(clause);
2361 clause_value = ExecEvalExpr(wclause->expr,
2367 * if we have a true test, then we return the result, since the case
2368 * statement is satisfied. A NULL result from the test is not
2371 if (DatumGetBool(clause_value) && !*isNull)
2373 econtext->caseValue_datum = save_datum;
2374 econtext->caseValue_isNull = save_isNull;
2375 return ExecEvalExpr(wclause->result,
2382 econtext->caseValue_datum = save_datum;
2383 econtext->caseValue_isNull = save_isNull;
2385 if (caseExpr->defresult)
2387 return ExecEvalExpr(caseExpr->defresult,
2398 * ExecEvalCaseTestExpr
2400 * Return the value stored by CASE.
2403 ExecEvalCaseTestExpr(ExprState *exprstate,
2404 ExprContext *econtext,
2405 bool *isNull, ExprDoneCond *isDone)
2408 *isDone = ExprSingleResult;
2409 *isNull = econtext->caseValue_isNull;
2410 return econtext->caseValue_datum;
2413 /* ----------------------------------------------------------------
2414 * ExecEvalArray - ARRAY[] expressions
2415 * ----------------------------------------------------------------
2418 ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
2419 bool *isNull, ExprDoneCond *isDone)
2421 ArrayExpr *arrayExpr = (ArrayExpr *) astate->xprstate.expr;
2424 Oid element_type = arrayExpr->element_typeid;
2429 /* Set default values for result flags: non-null, not a set result */
2432 *isDone = ExprSingleResult;
2434 if (!arrayExpr->multidims)
2436 /* Elements are presumably of scalar type */
2443 nelems = list_length(astate->elements);
2445 /* Shouldn't happen here, but if length is 0, return empty array */
2447 return PointerGetDatum(construct_empty_array(element_type));
2449 dvalues = (Datum *) palloc(nelems * sizeof(Datum));
2450 dnulls = (bool *) palloc(nelems * sizeof(bool));
2452 /* loop through and build array of datums */
2453 foreach(element, astate->elements)
2455 ExprState *e = (ExprState *) lfirst(element);
2457 dvalues[i] = ExecEvalExpr(e, econtext, &dnulls[i], NULL);
2461 /* setup for 1-D array of the given length */
2465 result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
2473 /* Must be nested array expressions */
2476 int outer_nelems = 0;
2478 int *elem_dims = NULL;
2479 int *elem_lbs = NULL;
2480 bool firstone = true;
2481 bool havenulls = false;
2482 bool haveempty = false;
2492 i = list_length(astate->elements);
2493 subdata = (char **) palloc(i * sizeof(char *));
2494 subbitmaps = (bits8 **) palloc(i * sizeof(bits8 *));
2495 subbytes = (int *) palloc(i * sizeof(int));
2496 subnitems = (int *) palloc(i * sizeof(int));
2498 /* loop through and get data area from each element */
2499 foreach(element, astate->elements)
2501 ExprState *e = (ExprState *) lfirst(element);
2507 arraydatum = ExecEvalExpr(e, econtext, &eisnull, NULL);
2508 /* temporarily ignore null subarrays */
2515 array = DatumGetArrayTypeP(arraydatum);
2517 /* run-time double-check on element type */
2518 if (element_type != ARR_ELEMTYPE(array))
2520 (errcode(ERRCODE_DATATYPE_MISMATCH),
2521 errmsg("cannot merge incompatible arrays"),
2522 errdetail("Array with element type %s cannot be "
2523 "included in ARRAY construct with element type %s.",
2524 format_type_be(ARR_ELEMTYPE(array)),
2525 format_type_be(element_type))));
2527 this_ndims = ARR_NDIM(array);
2528 /* temporarily ignore zero-dimensional subarrays */
2529 if (this_ndims <= 0)
2537 /* Get sub-array details from first member */
2538 elem_ndims = this_ndims;
2539 ndims = elem_ndims + 1;
2540 if (ndims <= 0 || ndims > MAXDIM)
2542 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2543 errmsg("number of array dimensions (%d) exceeds " \
2544 "the maximum allowed (%d)", ndims, MAXDIM)));
2546 elem_dims = (int *) palloc(elem_ndims * sizeof(int));
2547 memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
2548 elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
2549 memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
2555 /* Check other sub-arrays are compatible */
2556 if (elem_ndims != this_ndims ||
2557 memcmp(elem_dims, ARR_DIMS(array),
2558 elem_ndims * sizeof(int)) != 0 ||
2559 memcmp(elem_lbs, ARR_LBOUND(array),
2560 elem_ndims * sizeof(int)) != 0)
2562 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2563 errmsg("multidimensional arrays must have array "
2564 "expressions with matching dimensions")));
2567 subdata[outer_nelems] = ARR_DATA_PTR(array);
2568 subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
2569 subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
2570 nbytes += subbytes[outer_nelems];
2571 subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
2573 nitems += subnitems[outer_nelems];
2574 havenulls |= ARR_HASNULL(array);
2579 * If all items were null or empty arrays, return an empty array;
2580 * otherwise, if some were and some weren't, raise error. (Note:
2581 * we must special-case this somehow to avoid trying to generate
2582 * a 1-D array formed from empty arrays. It's not ideal...)
2586 if (ndims == 0) /* didn't find any nonempty array */
2587 return PointerGetDatum(construct_empty_array(element_type));
2589 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2590 errmsg("multidimensional arrays must have array "
2591 "expressions with matching dimensions")));
2594 /* setup for multi-D array */
2595 dims[0] = outer_nelems;
2597 for (i = 1; i < ndims; i++)
2599 dims[i] = elem_dims[i - 1];
2600 lbs[i] = elem_lbs[i - 1];
2605 dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
2606 nbytes += dataoffset;
2610 dataoffset = 0; /* marker for no null bitmap */
2611 nbytes += ARR_OVERHEAD_NONULLS(ndims);
2614 result = (ArrayType *) palloc(nbytes);
2615 SET_VARSIZE(result, nbytes);
2616 result->ndim = ndims;
2617 result->dataoffset = dataoffset;
2618 result->elemtype = element_type;
2619 memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
2620 memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
2622 dat = ARR_DATA_PTR(result);
2624 for (i = 0; i < outer_nelems; i++)
2626 memcpy(dat, subdata[i], subbytes[i]);
2629 array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
2632 iitem += subnitems[i];
2636 return PointerGetDatum(result);
2639 /* ----------------------------------------------------------------
2640 * ExecEvalRow - ROW() expressions
2641 * ----------------------------------------------------------------
2644 ExecEvalRow(RowExprState *rstate,
2645 ExprContext *econtext,
2646 bool *isNull, ExprDoneCond *isDone)
2655 /* Set default values for result flags: non-null, not a set result */
2658 *isDone = ExprSingleResult;
2660 /* Allocate workspace */
2661 natts = rstate->tupdesc->natts;
2662 values = (Datum *) palloc0(natts * sizeof(Datum));
2663 isnull = (bool *) palloc(natts * sizeof(bool));
2665 /* preset to nulls in case rowtype has some later-added columns */
2666 memset(isnull, true, natts * sizeof(bool));
2668 /* Evaluate field values */
2670 foreach(arg, rstate->args)
2672 ExprState *e = (ExprState *) lfirst(arg);
2674 values[i] = ExecEvalExpr(e, econtext, &isnull[i], NULL);
2678 tuple = heap_form_tuple(rstate->tupdesc, values, isnull);
2683 return HeapTupleGetDatum(tuple);
2686 /* ----------------------------------------------------------------
2687 * ExecEvalRowCompare - ROW() comparison-op ROW()
2688 * ----------------------------------------------------------------
2691 ExecEvalRowCompare(RowCompareExprState *rstate,
2692 ExprContext *econtext,
2693 bool *isNull, ExprDoneCond *isDone)
2696 RowCompareType rctype = ((RowCompareExpr *) rstate->xprstate.expr)->rctype;
2697 int32 cmpresult = 0;
2703 *isDone = ExprSingleResult;
2704 *isNull = true; /* until we get a result */
2707 forboth(l, rstate->largs, r, rstate->rargs)
2709 ExprState *le = (ExprState *) lfirst(l);
2710 ExprState *re = (ExprState *) lfirst(r);
2711 FunctionCallInfoData locfcinfo;
2713 InitFunctionCallInfoData(locfcinfo, &(rstate->funcs[i]), 2,
2715 locfcinfo.arg[0] = ExecEvalExpr(le, econtext,
2716 &locfcinfo.argnull[0], NULL);
2717 locfcinfo.arg[1] = ExecEvalExpr(re, econtext,
2718 &locfcinfo.argnull[1], NULL);
2719 if (rstate->funcs[i].fn_strict &&
2720 (locfcinfo.argnull[0] || locfcinfo.argnull[1]))
2721 return (Datum) 0; /* force NULL result */
2722 locfcinfo.isnull = false;
2723 cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo));
2724 if (locfcinfo.isnull)
2725 return (Datum) 0; /* force NULL result */
2727 break; /* no need to compare remaining columns */
2733 /* EQ and NE cases aren't allowed here */
2735 result = (cmpresult < 0);
2738 result = (cmpresult <= 0);
2741 result = (cmpresult >= 0);
2744 result = (cmpresult > 0);
2747 elog(ERROR, "unrecognized RowCompareType: %d", (int) rctype);
2748 result = 0; /* keep compiler quiet */
2753 return BoolGetDatum(result);
2756 /* ----------------------------------------------------------------
2758 * ----------------------------------------------------------------
2761 ExecEvalCoalesce(CoalesceExprState *coalesceExpr, ExprContext *econtext,
2762 bool *isNull, ExprDoneCond *isDone)
2767 *isDone = ExprSingleResult;
2769 /* Simply loop through until something NOT NULL is found */
2770 foreach(arg, coalesceExpr->args)
2772 ExprState *e = (ExprState *) lfirst(arg);
2775 value = ExecEvalExpr(e, econtext, isNull, NULL);
2780 /* Else return NULL */
2785 /* ----------------------------------------------------------------
2787 * ----------------------------------------------------------------
2790 ExecEvalMinMax(MinMaxExprState *minmaxExpr, ExprContext *econtext,
2791 bool *isNull, ExprDoneCond *isDone)
2793 Datum result = (Datum) 0;
2794 MinMaxOp op = ((MinMaxExpr *) minmaxExpr->xprstate.expr)->op;
2795 FunctionCallInfoData locfcinfo;
2799 *isDone = ExprSingleResult;
2800 *isNull = true; /* until we get a result */
2802 InitFunctionCallInfoData(locfcinfo, &minmaxExpr->cfunc, 2, NULL, NULL);
2803 locfcinfo.argnull[0] = false;
2804 locfcinfo.argnull[1] = false;
2806 foreach(arg, minmaxExpr->args)
2808 ExprState *e = (ExprState *) lfirst(arg);
2813 value = ExecEvalExpr(e, econtext, &valueIsNull, NULL);
2815 continue; /* ignore NULL inputs */
2819 /* first nonnull input, adopt value */
2825 /* apply comparison function */
2826 locfcinfo.arg[0] = result;
2827 locfcinfo.arg[1] = value;
2828 locfcinfo.isnull = false;
2829 cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo));
2830 if (locfcinfo.isnull) /* probably should not happen */
2832 if (cmpresult > 0 && op == IS_LEAST)
2834 else if (cmpresult < 0 && op == IS_GREATEST)
2842 /* ----------------------------------------------------------------
2844 * ----------------------------------------------------------------
2847 ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
2848 bool *isNull, ExprDoneCond *isDone)
2850 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
2860 *isDone = ExprSingleResult;
2861 *isNull = true; /* until we get a result */
2869 foreach(arg, xmlExpr->args)
2871 ExprState *e = (ExprState *) lfirst(arg);
2873 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2875 values = lappend(values, DatumGetPointer(value));
2878 if (list_length(values) > 0)
2881 return PointerGetDatum(xmlconcat(values));
2887 initStringInfo(&buf);
2889 forboth(arg, xmlExpr->named_args, narg, xexpr->arg_names)
2891 ExprState *e = (ExprState *) lfirst(arg);
2892 char *argname = strVal(lfirst(narg));
2894 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2897 appendStringInfo(&buf, "<%s>%s</%s>",
2899 map_sql_value_to_xml_value(value, exprType((Node *) e->expr)),
2907 /* The remaining cases don't need to set up buf */
2910 return PointerGetDatum(xmlelement(xmlExpr, econtext));
2917 bool preserve_whitespace;
2919 /* arguments are known to be text, bool */
2920 Assert(list_length(xmlExpr->args) == 2);
2922 e = (ExprState *) linitial(xmlExpr->args);
2923 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2926 data = DatumGetTextP(value);
2928 e = (ExprState *) lsecond(xmlExpr->args);
2929 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2930 if (isnull) /* probably can't happen */
2932 preserve_whitespace = DatumGetBool(value);
2936 return PointerGetDatum(xmlparse(data,
2938 preserve_whitespace));
2947 /* optional argument is known to be text */
2948 Assert(list_length(xmlExpr->args) <= 1);
2952 e = (ExprState *) linitial(xmlExpr->args);
2953 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2957 arg = DatumGetTextP(value);
2965 return PointerGetDatum(xmlpi(xexpr->name, arg, isnull, isNull));
2976 /* arguments are known to be xml, text, int */
2977 Assert(list_length(xmlExpr->args) == 3);
2979 e = (ExprState *) linitial(xmlExpr->args);
2980 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2983 data = DatumGetXmlP(value);
2985 e = (ExprState *) lsecond(xmlExpr->args);
2986 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2990 version = DatumGetTextP(value);
2992 e = (ExprState *) lthird(xmlExpr->args);
2993 value = ExecEvalExpr(e, econtext, &isnull, NULL);
2994 standalone = DatumGetInt32(value);
2998 return PointerGetDatum(xmlroot(data,
3004 case IS_XMLSERIALIZE:
3008 /* argument type is known to be xml */
3009 Assert(list_length(xmlExpr->args) == 1);
3011 e = (ExprState *) linitial(xmlExpr->args);
3012 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3018 return PointerGetDatum(xmltotext_with_xmloption(DatumGetXmlP(value), xexpr->xmloption));
3026 /* optional argument is known to be xml */
3027 Assert(list_length(xmlExpr->args) == 1);
3029 e = (ExprState *) linitial(xmlExpr->args);
3030 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3036 return BoolGetDatum(xml_is_document(DatumGetXmlP(value)));
3046 int len = buf.len + VARHDRSZ;
3048 result = palloc(len);
3049 SET_VARSIZE(result, len);
3050 memcpy(VARDATA(result), buf.data, buf.len);
3054 return PointerGetDatum(result);
3057 /* ----------------------------------------------------------------
3060 * Note that this is *always* derived from the equals operator,
3061 * but since we need special processing of the arguments
3062 * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
3063 * ----------------------------------------------------------------
3066 ExecEvalNullIf(FuncExprState *nullIfExpr,
3067 ExprContext *econtext,
3068 bool *isNull, ExprDoneCond *isDone)
3071 FunctionCallInfoData fcinfo;
3072 ExprDoneCond argDone;
3076 *isDone = ExprSingleResult;
3079 * Initialize function cache if first time through
3081 if (nullIfExpr->func.fn_oid == InvalidOid)
3083 NullIfExpr *op = (NullIfExpr *) nullIfExpr->xprstate.expr;
3085 init_fcache(op->opfuncid, nullIfExpr, econtext->ecxt_per_query_memory);
3086 Assert(!nullIfExpr->func.fn_retset);
3090 * extract info from nullIfExpr
3092 argList = nullIfExpr->args;
3094 /* Need to prep callinfo structure */
3095 InitFunctionCallInfoData(fcinfo, &(nullIfExpr->func), 0, NULL, NULL);
3096 argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
3097 if (argDone != ExprSingleResult)
3099 (errcode(ERRCODE_DATATYPE_MISMATCH),
3100 errmsg("NULLIF does not support set arguments")));
3101 Assert(fcinfo.nargs == 2);
3103 /* if either argument is NULL they can't be equal */
3104 if (!fcinfo.argnull[0] && !fcinfo.argnull[1])
3106 fcinfo.isnull = false;
3107 result = FunctionCallInvoke(&fcinfo);
3108 /* if the arguments are equal return null */
3109 if (!fcinfo.isnull && DatumGetBool(result))
3116 /* else return first argument */
3117 *isNull = fcinfo.argnull[0];
3118 return fcinfo.arg[0];
3121 /* ----------------------------------------------------------------
3124 * Evaluate a NullTest node.
3125 * ----------------------------------------------------------------
3128 ExecEvalNullTest(NullTestState *nstate,
3129 ExprContext *econtext,
3131 ExprDoneCond *isDone)
3133 NullTest *ntest = (NullTest *) nstate->xprstate.expr;
3136 result = ExecEvalExpr(nstate->arg, econtext, isNull, isDone);
3138 if (isDone && *isDone == ExprEndResult)
3139 return result; /* nothing to check */
3141 if (nstate->argisrow && !(*isNull))
3143 HeapTupleHeader tuple;
3147 HeapTupleData tmptup;
3150 tuple = DatumGetHeapTupleHeader(result);
3152 tupType = HeapTupleHeaderGetTypeId(tuple);
3153 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3155 /* Lookup tupdesc if first time through or if type changes */
3156 tupDesc = get_cached_rowtype(tupType, tupTypmod,
3157 &nstate->argdesc, econtext);
3160 * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
3162 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3163 tmptup.t_data = tuple;
3165 for (att = 1; att <= tupDesc->natts; att++)
3167 /* ignore dropped columns */
3168 if (tupDesc->attrs[att - 1]->attisdropped)
3170 if (heap_attisnull(&tmptup, att))
3172 /* null field disproves IS NOT NULL */
3173 if (ntest->nulltesttype == IS_NOT_NULL)
3174 return BoolGetDatum(false);
3178 /* non-null field disproves IS NULL */
3179 if (ntest->nulltesttype == IS_NULL)
3180 return BoolGetDatum(false);
3184 return BoolGetDatum(true);
3188 /* Simple scalar-argument case, or a null rowtype datum */
3189 switch (ntest->nulltesttype)
3195 return BoolGetDatum(true);
3198 return BoolGetDatum(false);
3203 return BoolGetDatum(false);
3206 return BoolGetDatum(true);
3208 elog(ERROR, "unrecognized nulltesttype: %d",
3209 (int) ntest->nulltesttype);
3210 return (Datum) 0; /* keep compiler quiet */
3215 /* ----------------------------------------------------------------
3216 * ExecEvalBooleanTest
3218 * Evaluate a BooleanTest node.
3219 * ----------------------------------------------------------------
3222 ExecEvalBooleanTest(GenericExprState *bstate,
3223 ExprContext *econtext,
3225 ExprDoneCond *isDone)
3227 BooleanTest *btest = (BooleanTest *) bstate->xprstate.expr;
3230 result = ExecEvalExpr(bstate->arg, econtext, isNull, isDone);
3232 if (isDone && *isDone == ExprEndResult)
3233 return result; /* nothing to check */
3235 switch (btest->booltesttype)
3241 return BoolGetDatum(false);
3243 else if (DatumGetBool(result))
3244 return BoolGetDatum(true);
3246 return BoolGetDatum(false);
3251 return BoolGetDatum(true);
3253 else if (DatumGetBool(result))
3254 return BoolGetDatum(false);
3256 return BoolGetDatum(true);
3261 return BoolGetDatum(false);
3263 else if (DatumGetBool(result))
3264 return BoolGetDatum(false);
3266 return BoolGetDatum(true);
3271 return BoolGetDatum(true);
3273 else if (DatumGetBool(result))
3274 return BoolGetDatum(true);
3276 return BoolGetDatum(false);
3281 return BoolGetDatum(true);
3284 return BoolGetDatum(false);
3285 case IS_NOT_UNKNOWN:
3289 return BoolGetDatum(false);
3292 return BoolGetDatum(true);
3294 elog(ERROR, "unrecognized booltesttype: %d",
3295 (int) btest->booltesttype);
3296 return (Datum) 0; /* keep compiler quiet */
3301 * ExecEvalCoerceToDomain
3303 * Test the provided data against the domain constraint(s). If the data
3304 * passes the constraint specifications, pass it through (return the
3305 * datum) otherwise throw an error.
3308 ExecEvalCoerceToDomain(CoerceToDomainState *cstate, ExprContext *econtext,
3309 bool *isNull, ExprDoneCond *isDone)
3311 CoerceToDomain *ctest = (CoerceToDomain *) cstate->xprstate.expr;
3315 result = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
3317 if (isDone && *isDone == ExprEndResult)
3318 return result; /* nothing to check */
3320 foreach(l, cstate->constraints)
3322 DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
3324 switch (con->constrainttype)
3326 case DOM_CONSTRAINT_NOTNULL:
3329 (errcode(ERRCODE_NOT_NULL_VIOLATION),
3330 errmsg("domain %s does not allow null values",
3331 format_type_be(ctest->resulttype))));
3333 case DOM_CONSTRAINT_CHECK:
3341 * Set up value to be returned by CoerceToDomainValue
3342 * nodes. We must save and restore prior setting of
3343 * econtext's domainValue fields, in case this node is
3344 * itself within a check expression for another domain.
3346 save_datum = econtext->domainValue_datum;
3347 save_isNull = econtext->domainValue_isNull;
3349 econtext->domainValue_datum = result;
3350 econtext->domainValue_isNull = *isNull;
3352 conResult = ExecEvalExpr(con->check_expr,
3353 econtext, &conIsNull, NULL);
3356 !DatumGetBool(conResult))
3358 (errcode(ERRCODE_CHECK_VIOLATION),
3359 errmsg("value for domain %s violates check constraint \"%s\"",
3360 format_type_be(ctest->resulttype),
3362 econtext->domainValue_datum = save_datum;
3363 econtext->domainValue_isNull = save_isNull;
3368 elog(ERROR, "unrecognized constraint type: %d",
3369 (int) con->constrainttype);
3374 /* If all has gone well (constraints did not fail) return the datum */
3379 * ExecEvalCoerceToDomainValue
3381 * Return the value stored by CoerceToDomain.
3384 ExecEvalCoerceToDomainValue(ExprState *exprstate,
3385 ExprContext *econtext,
3386 bool *isNull, ExprDoneCond *isDone)
3389 *isDone = ExprSingleResult;
3390 *isNull = econtext->domainValue_isNull;
3391 return econtext->domainValue_datum;
3394 /* ----------------------------------------------------------------
3395 * ExecEvalFieldSelect
3397 * Evaluate a FieldSelect node.
3398 * ----------------------------------------------------------------
3401 ExecEvalFieldSelect(FieldSelectState *fstate,
3402 ExprContext *econtext,
3404 ExprDoneCond *isDone)
3406 FieldSelect *fselect = (FieldSelect *) fstate->xprstate.expr;
3407 AttrNumber fieldnum = fselect->fieldnum;
3410 HeapTupleHeader tuple;
3414 Form_pg_attribute attr;
3415 HeapTupleData tmptup;
3417 tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
3419 /* this test covers the isDone exception too: */
3423 tuple = DatumGetHeapTupleHeader(tupDatum);
3425 tupType = HeapTupleHeaderGetTypeId(tuple);
3426 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3428 /* Lookup tupdesc if first time through or if type changes */
3429 tupDesc = get_cached_rowtype(tupType, tupTypmod,
3430 &fstate->argdesc, econtext);
3432 /* Check for dropped column, and force a NULL result if so */
3433 if (fieldnum <= 0 ||
3434 fieldnum > tupDesc->natts) /* should never happen */
3435 elog(ERROR, "attribute number %d exceeds number of columns %d",
3436 fieldnum, tupDesc->natts);
3437 attr = tupDesc->attrs[fieldnum - 1];
3438 if (attr->attisdropped)
3444 /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3445 /* As in ExecEvalVar, we should but can't check typmod */
3446 if (fselect->resulttype != attr->atttypid)
3448 (errmsg("attribute %d has wrong type", fieldnum),
3449 errdetail("Table has type %s, but query expects %s.",
3450 format_type_be(attr->atttypid),
3451 format_type_be(fselect->resulttype))));
3454 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
3455 * the fields in the struct just in case user tries to inspect system
3458 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3459 ItemPointerSetInvalid(&(tmptup.t_self));
3460 tmptup.t_tableOid = InvalidOid;
3461 tmptup.t_data = tuple;
3463 result = heap_getattr(&tmptup,
3470 /* ----------------------------------------------------------------
3471 * ExecEvalFieldStore
3473 * Evaluate a FieldStore node.
3474 * ----------------------------------------------------------------
3477 ExecEvalFieldStore(FieldStoreState *fstate,
3478 ExprContext *econtext,
3480 ExprDoneCond *isDone)
3482 FieldStore *fstore = (FieldStore *) fstate->xprstate.expr;
3493 tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
3495 if (isDone && *isDone == ExprEndResult)
3498 /* Lookup tupdesc if first time through or after rescan */
3499 tupDesc = get_cached_rowtype(fstore->resulttype, -1,
3500 &fstate->argdesc, econtext);
3502 /* Allocate workspace */
3503 values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
3504 isnull = (bool *) palloc(tupDesc->natts * sizeof(bool));
3509 * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We
3510 * set all the fields in the struct just in case.
3512 HeapTupleHeader tuphdr;
3513 HeapTupleData tmptup;
3515 tuphdr = DatumGetHeapTupleHeader(tupDatum);
3516 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
3517 ItemPointerSetInvalid(&(tmptup.t_self));
3518 tmptup.t_tableOid = InvalidOid;
3519 tmptup.t_data = tuphdr;
3521 heap_deform_tuple(&tmptup, tupDesc, values, isnull);
3525 /* Convert null input tuple into an all-nulls row */
3526 memset(isnull, true, tupDesc->natts * sizeof(bool));
3529 /* Result is never null */
3532 save_datum = econtext->caseValue_datum;
3533 save_isNull = econtext->caseValue_isNull;
3535 forboth(l1, fstate->newvals, l2, fstore->fieldnums)
3537 ExprState *newval = (ExprState *) lfirst(l1);
3538 AttrNumber fieldnum = lfirst_int(l2);
3540 Assert(fieldnum > 0 && fieldnum <= tupDesc->natts);
3543 * Use the CaseTestExpr mechanism to pass down the old value of the
3544 * field being replaced; this is useful in case we have a nested field
3545 * update situation. It's safe to reuse the CASE mechanism because
3546 * there cannot be a CASE between here and where the value would be
3549 econtext->caseValue_datum = values[fieldnum - 1];
3550 econtext->caseValue_isNull = isnull[fieldnum - 1];
3552 values[fieldnum - 1] = ExecEvalExpr(newval,
3554 &isnull[fieldnum - 1],
3558 econtext->caseValue_datum = save_datum;
3559 econtext->caseValue_isNull = save_isNull;
3561 tuple = heap_form_tuple(tupDesc, values, isnull);
3566 return HeapTupleGetDatum(tuple);
3569 /* ----------------------------------------------------------------
3570 * ExecEvalRelabelType
3572 * Evaluate a RelabelType node.
3573 * ----------------------------------------------------------------
3576 ExecEvalRelabelType(GenericExprState *exprstate,
3577 ExprContext *econtext,
3578 bool *isNull, ExprDoneCond *isDone)
3580 return ExecEvalExpr(exprstate->arg, econtext, isNull, isDone);
3583 /* ----------------------------------------------------------------
3584 * ExecEvalCoerceViaIO
3586 * Evaluate a CoerceViaIO node.
3587 * ----------------------------------------------------------------
3590 ExecEvalCoerceViaIO(CoerceViaIOState *iostate,
3591 ExprContext *econtext,
3592 bool *isNull, ExprDoneCond *isDone)
3598 inputval = ExecEvalExpr(iostate->arg, econtext, isNull, isDone);
3600 if (isDone && *isDone == ExprEndResult)
3601 return inputval; /* nothing to do */
3604 string = NULL; /* output functions are not called on nulls */
3606 string = OutputFunctionCall(&iostate->outfunc, inputval);
3608 result = InputFunctionCall(&iostate->infunc,
3610 iostate->intypioparam,
3613 /* The input function cannot change the null/not-null status */
3617 /* ----------------------------------------------------------------
3618 * ExecEvalArrayCoerceExpr
3620 * Evaluate an ArrayCoerceExpr node.
3621 * ----------------------------------------------------------------
3624 ExecEvalArrayCoerceExpr(ArrayCoerceExprState *astate,
3625 ExprContext *econtext,
3626 bool *isNull, ExprDoneCond *isDone)
3628 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) astate->xprstate.expr;
3631 FunctionCallInfoData locfcinfo;
3633 result = ExecEvalExpr(astate->arg, econtext, isNull, isDone);
3635 if (isDone && *isDone == ExprEndResult)
3636 return result; /* nothing to do */
3638 return result; /* nothing to do */
3641 * If it's binary-compatible, modify the element type in the array header,
3642 * but otherwise leave the array as we received it.
3644 if (!OidIsValid(acoerce->elemfuncid))
3646 /* Detoast input array if necessary, and copy in any case */
3647 array = DatumGetArrayTypePCopy(result);
3648 ARR_ELEMTYPE(array) = astate->resultelemtype;
3649 PG_RETURN_ARRAYTYPE_P(array);
3652 /* Detoast input array if necessary, but don't make a useless copy */
3653 array = DatumGetArrayTypeP(result);
3655 /* Initialize function cache if first time through */
3656 if (astate->elemfunc.fn_oid == InvalidOid)
3658 AclResult aclresult;
3660 /* Check permission to call function */
3661 aclresult = pg_proc_aclcheck(acoerce->elemfuncid, GetUserId(),
3663 if (aclresult != ACLCHECK_OK)
3664 aclcheck_error(aclresult, ACL_KIND_PROC,
3665 get_func_name(acoerce->elemfuncid));
3667 /* Set up the primary fmgr lookup information */
3668 fmgr_info_cxt(acoerce->elemfuncid, &(astate->elemfunc),
3669 econtext->ecxt_per_query_memory);
3671 /* Initialize additional info */
3672 astate->elemfunc.fn_expr = (Node *) acoerce;
3676 * Use array_map to apply the function to each array element.
3678 * We pass on the desttypmod and isExplicit flags whether or not the
3679 * function wants them.
3681 InitFunctionCallInfoData(locfcinfo, &(astate->elemfunc), 3,
3683 locfcinfo.arg[0] = PointerGetDatum(array);
3684 locfcinfo.arg[1] = Int32GetDatum(acoerce->resulttypmod);
3685 locfcinfo.arg[2] = BoolGetDatum(acoerce->isExplicit);
3686 locfcinfo.argnull[0] = false;
3687 locfcinfo.argnull[1] = false;
3688 locfcinfo.argnull[2] = false;
3690 return array_map(&locfcinfo, ARR_ELEMTYPE(array), astate->resultelemtype,
3694 /* ----------------------------------------------------------------
3695 * ExecEvalCurrentOfExpr
3697 * Normally, the planner will convert CURRENT OF into a TidScan qualification,
3698 * but we have plain execQual support in case it doesn't.
3699 * ----------------------------------------------------------------
3702 ExecEvalCurrentOfExpr(ExprState *exprstate, ExprContext *econtext,
3703 bool *isNull, ExprDoneCond *isDone)
3705 CurrentOfExpr *cexpr = (CurrentOfExpr *) exprstate->expr;
3709 ItemPointer tuple_tid;
3710 ItemPointerData cursor_tid;
3713 *isDone = ExprSingleResult;
3716 Assert(cexpr->cvarno != INNER);
3717 Assert(cexpr->cvarno != OUTER);
3718 Assert(!TupIsNull(econtext->ecxt_scantuple));
3719 /* Use slot_getattr to catch any possible mistakes */
3720 tableoid = DatumGetObjectId(slot_getattr(econtext->ecxt_scantuple,
3721 TableOidAttributeNumber,
3724 tuple_tid = (ItemPointer)
3725 DatumGetPointer(slot_getattr(econtext->ecxt_scantuple,
3726 SelfItemPointerAttributeNumber,
3730 if (execCurrentOf(cexpr, econtext, tableoid, &cursor_tid))
3731 result = ItemPointerEquals(&cursor_tid, tuple_tid);
3735 return BoolGetDatum(result);
3740 * ExecEvalExprSwitchContext
3742 * Same as ExecEvalExpr, but get into the right allocation context explicitly.
3745 ExecEvalExprSwitchContext(ExprState *expression,
3746 ExprContext *econtext,
3748 ExprDoneCond *isDone)
3751 MemoryContext oldContext;
3753 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
3754 retDatum = ExecEvalExpr(expression, econtext, isNull, isDone);
3755 MemoryContextSwitchTo(oldContext);
3761 * ExecInitExpr: prepare an expression tree for execution
3763 * This function builds and returns an ExprState tree paralleling the given
3764 * Expr node tree. The ExprState tree can then be handed to ExecEvalExpr
3765 * for execution. Because the Expr tree itself is read-only as far as
3766 * ExecInitExpr and ExecEvalExpr are concerned, several different executions
3767 * of the same plan tree can occur concurrently.
3769 * This must be called in a memory context that will last as long as repeated
3770 * executions of the expression are needed. Typically the context will be
3771 * the same as the per-query context of the associated ExprContext.
3773 * Any Aggref and SubPlan nodes found in the tree are added to the lists
3774 * of such nodes held by the parent PlanState. Otherwise, we do very little
3775 * initialization here other than building the state-node tree. Any nontrivial
3776 * work associated with initializing runtime info for a node should happen
3777 * during the first actual evaluation of that node. (This policy lets us
3778 * avoid work if the node is never actually evaluated.)
3780 * Note: there is no ExecEndExpr function; we assume that any resource
3781 * cleanup needed will be handled by just releasing the memory context
3782 * in which the state tree is built. Functions that require additional
3783 * cleanup work can register a shutdown callback in the ExprContext.
3785 * 'node' is the root of the expression tree to examine
3786 * 'parent' is the PlanState node that owns the expression.
3788 * 'parent' may be NULL if we are preparing an expression that is not
3789 * associated with a plan tree. (If so, it can't have aggs or subplans.)
3790 * This case should usually come through ExecPrepareExpr, not directly here.
3793 ExecInitExpr(Expr *node, PlanState *parent)
3800 /* Guard against stack overflow due to overly complex expressions */
3801 check_stack_depth();
3803 switch (nodeTag(node))
3806 state = (ExprState *) makeNode(ExprState);
3807 state->evalfunc = ExecEvalVar;
3810 state = (ExprState *) makeNode(ExprState);
3811 state->evalfunc = ExecEvalConst;
3814 state = (ExprState *) makeNode(ExprState);
3815 state->evalfunc = ExecEvalParam;
3817 case T_CoerceToDomainValue:
3818 state = (ExprState *) makeNode(ExprState);
3819 state->evalfunc = ExecEvalCoerceToDomainValue;
3821 case T_CaseTestExpr:
3822 state = (ExprState *) makeNode(ExprState);
3823 state->evalfunc = ExecEvalCaseTestExpr;
3827 Aggref *aggref = (Aggref *) node;
3828 AggrefExprState *astate = makeNode(AggrefExprState);
3830 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAggref;
3831 if (parent && IsA(parent, AggState))
3833 AggState *aggstate = (AggState *) parent;
3836 aggstate->aggs = lcons(astate, aggstate->aggs);
3837 naggs = ++aggstate->numaggs;
3839 astate->args = (List *) ExecInitExpr((Expr *) aggref->args,
3843 * Complain if the aggregate's arguments contain any
3844 * aggregates; nested agg functions are semantically
3845 * nonsensical. (This should have been caught earlier,
3846 * but we defend against it here anyway.)
3848 if (naggs != aggstate->numaggs)
3850 (errcode(ERRCODE_GROUPING_ERROR),
3851 errmsg("aggregate function calls cannot be nested")));
3855 /* planner messed up */
3856 elog(ERROR, "aggref found in non-Agg plan node");
3858 state = (ExprState *) astate;
3863 ArrayRef *aref = (ArrayRef *) node;
3864 ArrayRefExprState *astate = makeNode(ArrayRefExprState);
3866 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArrayRef;
3867 astate->refupperindexpr = (List *)
3868 ExecInitExpr((Expr *) aref->refupperindexpr, parent);
3869 astate->reflowerindexpr = (List *)
3870 ExecInitExpr((Expr *) aref->reflowerindexpr, parent);
3871 astate->refexpr = ExecInitExpr(aref->refexpr, parent);
3872 astate->refassgnexpr = ExecInitExpr(aref->refassgnexpr,
3874 /* do one-time catalog lookups for type info */
3875 astate->refattrlength = get_typlen(aref->refarraytype);
3876 get_typlenbyvalalign(aref->refelemtype,
3877 &astate->refelemlength,
3878 &astate->refelembyval,
3879 &astate->refelemalign);
3880 state = (ExprState *) astate;
3885 FuncExpr *funcexpr = (FuncExpr *) node;
3886 FuncExprState *fstate = makeNode(FuncExprState);
3888 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFunc;
3889 fstate->args = (List *)
3890 ExecInitExpr((Expr *) funcexpr->args, parent);
3891 fstate->func.fn_oid = InvalidOid; /* not initialized */
3892 state = (ExprState *) fstate;
3897 OpExpr *opexpr = (OpExpr *) node;
3898 FuncExprState *fstate = makeNode(FuncExprState);
3900 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOper;
3901 fstate->args = (List *)
3902 ExecInitExpr((Expr *) opexpr->args, parent);
3903 fstate->func.fn_oid = InvalidOid; /* not initialized */
3904 state = (ExprState *) fstate;
3907 case T_DistinctExpr:
3909 DistinctExpr *distinctexpr = (DistinctExpr *) node;
3910 FuncExprState *fstate = makeNode(FuncExprState);
3912 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalDistinct;
3913 fstate->args = (List *)
3914 ExecInitExpr((Expr *) distinctexpr->args, parent);
3915 fstate->func.fn_oid = InvalidOid; /* not initialized */
3916 state = (ExprState *) fstate;
3919 case T_ScalarArrayOpExpr:
3921 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
3922 ScalarArrayOpExprState *sstate = makeNode(ScalarArrayOpExprState);
3924 sstate->fxprstate.xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalScalarArrayOp;
3925 sstate->fxprstate.args = (List *)
3926 ExecInitExpr((Expr *) opexpr->args, parent);
3927 sstate->fxprstate.func.fn_oid = InvalidOid; /* not initialized */
3928 sstate->element_type = InvalidOid; /* ditto */
3929 state = (ExprState *) sstate;
3934 BoolExpr *boolexpr = (BoolExpr *) node;
3935 BoolExprState *bstate = makeNode(BoolExprState);
3937 switch (boolexpr->boolop)
3940 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAnd;
3943 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOr;
3946 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNot;
3949 elog(ERROR, "unrecognized boolop: %d",
3950 (int) boolexpr->boolop);
3953 bstate->args = (List *)
3954 ExecInitExpr((Expr *) boolexpr->args, parent);
3955 state = (ExprState *) bstate;
3960 SubPlan *subplan = (SubPlan *) node;
3961 SubPlanState *sstate;
3964 elog(ERROR, "SubPlan found with no parent plan");
3966 sstate = ExecInitSubPlan(subplan, parent);
3968 /* Add SubPlanState nodes to parent->subPlan */
3969 parent->subPlan = lcons(sstate, parent->subPlan);
3971 state = (ExprState *) sstate;
3976 FieldSelect *fselect = (FieldSelect *) node;
3977 FieldSelectState *fstate = makeNode(FieldSelectState);
3979 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldSelect;
3980 fstate->arg = ExecInitExpr(fselect->arg, parent);
3981 fstate->argdesc = NULL;
3982 state = (ExprState *) fstate;
3987 FieldStore *fstore = (FieldStore *) node;
3988 FieldStoreState *fstate = makeNode(FieldStoreState);
3990 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldStore;
3991 fstate->arg = ExecInitExpr(fstore->arg, parent);
3992 fstate->newvals = (List *) ExecInitExpr((Expr *) fstore->newvals, parent);
3993 fstate->argdesc = NULL;
3994 state = (ExprState *) fstate;
3999 RelabelType *relabel = (RelabelType *) node;
4000 GenericExprState *gstate = makeNode(GenericExprState);
4002 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRelabelType;
4003 gstate->arg = ExecInitExpr(relabel->arg, parent);
4004 state = (ExprState *) gstate;
4009 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
4010 CoerceViaIOState *iostate = makeNode(CoerceViaIOState);
4014 iostate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoerceViaIO;
4015 iostate->arg = ExecInitExpr(iocoerce->arg, parent);
4016 /* lookup the result type's input function */
4017 getTypeInputInfo(iocoerce->resulttype, &iofunc,
4018 &iostate->intypioparam);
4019 fmgr_info(iofunc, &iostate->infunc);
4020 /* lookup the input type's output function */
4021 getTypeOutputInfo(exprType((Node *) iocoerce->arg),
4022 &iofunc, &typisvarlena);
4023 fmgr_info(iofunc, &iostate->outfunc);
4024 state = (ExprState *) iostate;
4027 case T_ArrayCoerceExpr:
4029 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
4030 ArrayCoerceExprState *astate = makeNode(ArrayCoerceExprState);
4032 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArrayCoerceExpr;
4033 astate->arg = ExecInitExpr(acoerce->arg, parent);
4034 astate->resultelemtype = get_element_type(acoerce->resulttype);
4035 if (astate->resultelemtype == InvalidOid)
4037 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4038 errmsg("target type is not an array")));
4039 /* Arrays over domains aren't supported yet */
4040 Assert(getBaseType(astate->resultelemtype) ==
4041 astate->resultelemtype);
4042 astate->elemfunc.fn_oid = InvalidOid; /* not initialized */
4043 astate->amstate = (ArrayMapState *) palloc0(sizeof(ArrayMapState));
4044 state = (ExprState *) astate;
4047 case T_ConvertRowtypeExpr:
4049 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
4050 ConvertRowtypeExprState *cstate = makeNode(ConvertRowtypeExprState);
4052 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalConvertRowtype;
4053 cstate->arg = ExecInitExpr(convert->arg, parent);
4054 state = (ExprState *) cstate;
4059 CaseExpr *caseexpr = (CaseExpr *) node;
4060 CaseExprState *cstate = makeNode(CaseExprState);
4061 List *outlist = NIL;
4064 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCase;
4065 cstate->arg = ExecInitExpr(caseexpr->arg, parent);
4066 foreach(l, caseexpr->args)
4068 CaseWhen *when = (CaseWhen *) lfirst(l);
4069 CaseWhenState *wstate = makeNode(CaseWhenState);
4071 Assert(IsA(when, CaseWhen));
4072 wstate->xprstate.evalfunc = NULL; /* not used */
4073 wstate->xprstate.expr = (Expr *) when;
4074 wstate->expr = ExecInitExpr(when->expr, parent);
4075 wstate->result = ExecInitExpr(when->result, parent);
4076 outlist = lappend(outlist, wstate);
4078 cstate->args = outlist;
4079 cstate->defresult = ExecInitExpr(caseexpr->defresult, parent);
4080 state = (ExprState *) cstate;
4085 ArrayExpr *arrayexpr = (ArrayExpr *) node;
4086 ArrayExprState *astate = makeNode(ArrayExprState);
4087 List *outlist = NIL;
4090 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArray;
4091 foreach(l, arrayexpr->elements)
4093 Expr *e = (Expr *) lfirst(l);
4096 estate = ExecInitExpr(e, parent);
4097 outlist = lappend(outlist, estate);
4099 astate->elements = outlist;
4100 /* do one-time catalog lookup for type info */
4101 get_typlenbyvalalign(arrayexpr->element_typeid,
4102 &astate->elemlength,
4104 &astate->elemalign);
4105 state = (ExprState *) astate;
4110 RowExpr *rowexpr = (RowExpr *) node;
4111 RowExprState *rstate = makeNode(RowExprState);
4112 Form_pg_attribute *attrs;
4113 List *outlist = NIL;
4117 rstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRow;
4118 /* Build tupdesc to describe result tuples */
4119 if (rowexpr->row_typeid == RECORDOID)
4121 /* generic record, use runtime type assignment */
4122 rstate->tupdesc = ExecTypeFromExprList(rowexpr->args);
4123 BlessTupleDesc(rstate->tupdesc);
4124 /* we won't need to redo this at runtime */
4128 /* it's been cast to a named type, use that */
4129 rstate->tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
4131 /* Set up evaluation, skipping any deleted columns */
4132 Assert(list_length(rowexpr->args) <= rstate->tupdesc->natts);
4133 attrs = rstate->tupdesc->attrs;
4135 foreach(l, rowexpr->args)
4137 Expr *e = (Expr *) lfirst(l);
4140 if (!attrs[i]->attisdropped)
4143 * Guard against ALTER COLUMN TYPE on rowtype since
4144 * the RowExpr was created. XXX should we check
4145 * typmod too? Not sure we can be sure it'll be the
4148 if (exprType((Node *) e) != attrs[i]->atttypid)
4150 (errcode(ERRCODE_DATATYPE_MISMATCH),
4151 errmsg("ROW() column has type %s instead of type %s",
4152 format_type_be(exprType((Node *) e)),
4153 format_type_be(attrs[i]->atttypid))));
4158 * Ignore original expression and insert a NULL. We
4159 * don't really care what type of NULL it is, so
4160 * always make an int4 NULL.
4162 e = (Expr *) makeNullConst(INT4OID, -1);
4164 estate = ExecInitExpr(e, parent);
4165 outlist = lappend(outlist, estate);
4168 rstate->args = outlist;
4169 state = (ExprState *) rstate;
4172 case T_RowCompareExpr:
4174 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
4175 RowCompareExprState *rstate = makeNode(RowCompareExprState);
4176 int nopers = list_length(rcexpr->opnos);
4182 rstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRowCompare;
4183 Assert(list_length(rcexpr->largs) == nopers);
4185 foreach(l, rcexpr->largs)
4187 Expr *e = (Expr *) lfirst(l);
4190 estate = ExecInitExpr(e, parent);
4191 outlist = lappend(outlist, estate);
4193 rstate->largs = outlist;
4194 Assert(list_length(rcexpr->rargs) == nopers);
4196 foreach(l, rcexpr->rargs)
4198 Expr *e = (Expr *) lfirst(l);
4201 estate = ExecInitExpr(e, parent);
4202 outlist = lappend(outlist, estate);
4204 rstate->rargs = outlist;
4205 Assert(list_length(rcexpr->opfamilies) == nopers);
4206 rstate->funcs = (FmgrInfo *) palloc(nopers * sizeof(FmgrInfo));
4208 forboth(l, rcexpr->opnos, l2, rcexpr->opfamilies)
4210 Oid opno = lfirst_oid(l);
4211 Oid opfamily = lfirst_oid(l2);
4218 get_op_opfamily_properties(opno, opfamily,
4223 proc = get_opfamily_proc(opfamily,
4229 * If we enforced permissions checks on index support
4230 * functions, we'd need to make a check here. But the
4231 * index support machinery doesn't do that, and neither
4234 fmgr_info(proc, &(rstate->funcs[i]));
4237 state = (ExprState *) rstate;
4240 case T_CoalesceExpr:
4242 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
4243 CoalesceExprState *cstate = makeNode(CoalesceExprState);
4244 List *outlist = NIL;
4247 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoalesce;
4248 foreach(l, coalesceexpr->args)
4250 Expr *e = (Expr *) lfirst(l);
4253 estate = ExecInitExpr(e, parent);
4254 outlist = lappend(outlist, estate);
4256 cstate->args = outlist;
4257 state = (ExprState *) cstate;
4262 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
4263 MinMaxExprState *mstate = makeNode(MinMaxExprState);
4264 List *outlist = NIL;
4266 TypeCacheEntry *typentry;
4268 mstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalMinMax;
4269 foreach(l, minmaxexpr->args)
4271 Expr *e = (Expr *) lfirst(l);
4274 estate = ExecInitExpr(e, parent);
4275 outlist = lappend(outlist, estate);
4277 mstate->args = outlist;
4278 /* Look up the btree comparison function for the datatype */
4279 typentry = lookup_type_cache(minmaxexpr->minmaxtype,
4280 TYPECACHE_CMP_PROC);
4281 if (!OidIsValid(typentry->cmp_proc))
4283 (errcode(ERRCODE_UNDEFINED_FUNCTION),
4284 errmsg("could not identify a comparison function for type %s",
4285 format_type_be(minmaxexpr->minmaxtype))));
4288 * If we enforced permissions checks on index support
4289 * functions, we'd need to make a check here. But the index
4290 * support machinery doesn't do that, and neither does this
4293 fmgr_info(typentry->cmp_proc, &(mstate->cfunc));
4294 state = (ExprState *) mstate;
4299 XmlExpr *xexpr = (XmlExpr *) node;
4300 XmlExprState *xstate = makeNode(XmlExprState);
4305 xstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalXml;
4306 xstate->named_outfuncs = (FmgrInfo *)
4307 palloc0(list_length(xexpr->named_args) * sizeof(FmgrInfo));
4310 foreach(arg, xexpr->named_args)
4312 Expr *e = (Expr *) lfirst(arg);
4317 estate = ExecInitExpr(e, parent);
4318 outlist = lappend(outlist, estate);
4320 getTypeOutputInfo(exprType((Node *) e),
4321 &typOutFunc, &typIsVarlena);
4322 fmgr_info(typOutFunc, &xstate->named_outfuncs[i]);
4325 xstate->named_args = outlist;
4328 foreach(arg, xexpr->args)
4330 Expr *e = (Expr *) lfirst(arg);
4333 estate = ExecInitExpr(e, parent);
4334 outlist = lappend(outlist, estate);
4336 xstate->args = outlist;
4338 state = (ExprState *) xstate;
4343 NullIfExpr *nullifexpr = (NullIfExpr *) node;
4344 FuncExprState *fstate = makeNode(FuncExprState);
4346 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullIf;
4347 fstate->args = (List *)
4348 ExecInitExpr((Expr *) nullifexpr->args, parent);
4349 fstate->func.fn_oid = InvalidOid; /* not initialized */
4350 state = (ExprState *) fstate;
4355 NullTest *ntest = (NullTest *) node;
4356 NullTestState *nstate = makeNode(NullTestState);
4358 nstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullTest;
4359 nstate->arg = ExecInitExpr(ntest->arg, parent);
4360 nstate->argisrow = type_is_rowtype(exprType((Node *) ntest->arg));
4361 nstate->argdesc = NULL;
4362 state = (ExprState *) nstate;
4367 BooleanTest *btest = (BooleanTest *) node;
4368 GenericExprState *gstate = makeNode(GenericExprState);
4370 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalBooleanTest;
4371 gstate->arg = ExecInitExpr(btest->arg, parent);
4372 state = (ExprState *) gstate;
4375 case T_CoerceToDomain:
4377 CoerceToDomain *ctest = (CoerceToDomain *) node;
4378 CoerceToDomainState *cstate = makeNode(CoerceToDomainState);
4380 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoerceToDomain;
4381 cstate->arg = ExecInitExpr(ctest->arg, parent);
4382 cstate->constraints = GetDomainConstraints(ctest->resulttype);
4383 state = (ExprState *) cstate;
4386 case T_CurrentOfExpr:
4387 state = (ExprState *) makeNode(ExprState);
4388 state->evalfunc = ExecEvalCurrentOfExpr;
4392 TargetEntry *tle = (TargetEntry *) node;
4393 GenericExprState *gstate = makeNode(GenericExprState);
4395 gstate->xprstate.evalfunc = NULL; /* not used */
4396 gstate->arg = ExecInitExpr(tle->expr, parent);
4397 state = (ExprState *) gstate;
4402 List *outlist = NIL;
4405 foreach(l, (List *) node)
4407 outlist = lappend(outlist,
4408 ExecInitExpr((Expr *) lfirst(l),
4411 /* Don't fall through to the "common" code below */
4412 return (ExprState *) outlist;
4415 elog(ERROR, "unrecognized node type: %d",
4416 (int) nodeTag(node));
4417 state = NULL; /* keep compiler quiet */
4421 /* Common code for all state-node types */
4428 * ExecPrepareExpr --- initialize for expression execution outside a normal
4429 * Plan tree context.
4431 * This differs from ExecInitExpr in that we don't assume the caller is
4432 * already running in the EState's per-query context. Also, we apply
4433 * fix_opfuncids() to the passed expression tree to be sure it is ready
4434 * to run. (In ordinary Plan trees the planner will have fixed opfuncids,
4435 * but callers outside the executor will not have done this.)
4438 ExecPrepareExpr(Expr *node, EState *estate)
4441 MemoryContext oldcontext;
4443 fix_opfuncids((Node *) node);
4445 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4447 result = ExecInitExpr(node, NULL);
4449 MemoryContextSwitchTo(oldcontext);
4455 /* ----------------------------------------------------------------
4456 * ExecQual / ExecTargetList / ExecProject
4457 * ----------------------------------------------------------------
4460 /* ----------------------------------------------------------------
4463 * Evaluates a conjunctive boolean expression (qual list) and
4464 * returns true iff none of the subexpressions are false.
4465 * (We also return true if the list is empty.)
4467 * If some of the subexpressions yield NULL but none yield FALSE,
4468 * then the result of the conjunction is NULL (ie, unknown)
4469 * according to three-valued boolean logic. In this case,
4470 * we return the value specified by the "resultForNull" parameter.
4472 * Callers evaluating WHERE clauses should pass resultForNull=FALSE,
4473 * since SQL specifies that tuples with null WHERE results do not
4474 * get selected. On the other hand, callers evaluating constraint
4475 * conditions should pass resultForNull=TRUE, since SQL also specifies
4476 * that NULL constraint conditions are not failures.
4478 * NOTE: it would not be correct to use this routine to evaluate an
4479 * AND subclause of a boolean expression; for that purpose, a NULL
4480 * result must be returned as NULL so that it can be properly treated
4481 * in the next higher operator (cf. ExecEvalAnd and ExecEvalOr).
4482 * This routine is only used in contexts where a complete expression
4483 * is being evaluated and we know that NULL can be treated the same
4484 * as one boolean result or the other.
4486 * ----------------------------------------------------------------
4489 ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
4492 MemoryContext oldContext;
4498 EV_printf("ExecQual: qual is ");
4499 EV_nodeDisplay(qual);
4505 * Run in short-lived per-tuple context while computing expressions.
4507 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4510 * Evaluate the qual conditions one at a time. If we find a FALSE result,
4511 * we can stop evaluating and return FALSE --- the AND result must be
4512 * FALSE. Also, if we find a NULL result when resultForNull is FALSE, we
4513 * can stop and return FALSE --- the AND result must be FALSE or NULL in
4514 * that case, and the caller doesn't care which.
4516 * If we get to the end of the list, we can return TRUE. This will happen
4517 * when the AND result is indeed TRUE, or when the AND result is NULL (one
4518 * or more NULL subresult, with all the rest TRUE) and the caller has
4519 * specified resultForNull = TRUE.
4525 ExprState *clause = (ExprState *) lfirst(l);
4529 expr_value = ExecEvalExpr(clause, econtext, &isNull, NULL);
4533 if (resultForNull == false)
4535 result = false; /* treat NULL as FALSE */
4541 if (!DatumGetBool(expr_value))
4543 result = false; /* definitely FALSE */
4549 MemoryContextSwitchTo(oldContext);
4555 * Number of items in a tlist (including any resjunk items!)
4558 ExecTargetListLength(List *targetlist)
4560 /* This used to be more complex, but fjoins are dead */
4561 return list_length(targetlist);
4565 * Number of items in a tlist, not including any resjunk items
4568 ExecCleanTargetListLength(List *targetlist)
4573 foreach(tl, targetlist)
4575 TargetEntry *curTle = (TargetEntry *) lfirst(tl);
4577 Assert(IsA(curTle, TargetEntry));
4578 if (!curTle->resjunk)
4586 * Evaluates a targetlist with respect to the given
4587 * expression context. Returns TRUE if we were able to create
4588 * a result, FALSE if we have exhausted a set-valued expression.
4590 * Results are stored into the passed values and isnull arrays.
4591 * The caller must provide an itemIsDone array that persists across calls.
4593 * As with ExecEvalExpr, the caller should pass isDone = NULL if not
4594 * prepared to deal with sets of result tuples. Otherwise, a return
4595 * of *isDone = ExprMultipleResult signifies a set element, and a return
4596 * of *isDone = ExprEndResult signifies end of the set of tuple.
4599 ExecTargetList(List *targetlist,
4600 ExprContext *econtext,
4603 ExprDoneCond *itemIsDone,
4604 ExprDoneCond *isDone)
4606 MemoryContext oldContext;
4611 * Run in short-lived per-tuple context while computing expressions.
4613 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4616 * evaluate all the expressions in the target list
4619 *isDone = ExprSingleResult; /* until proven otherwise */
4621 haveDoneSets = false; /* any exhausted set exprs in tlist? */
4623 foreach(tl, targetlist)
4625 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4626 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4627 AttrNumber resind = tle->resno - 1;
4629 values[resind] = ExecEvalExpr(gstate->arg,
4632 &itemIsDone[resind]);
4634 if (itemIsDone[resind] != ExprSingleResult)
4636 /* We have a set-valued expression in the tlist */
4639 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4640 errmsg("set-valued function called in context that cannot accept a set")));
4641 if (itemIsDone[resind] == ExprMultipleResult)
4643 /* we have undone sets in the tlist, set flag */
4644 *isDone = ExprMultipleResult;
4648 /* we have done sets in the tlist, set flag for that */
4649 haveDoneSets = true;
4657 * note: can't get here unless we verified isDone != NULL
4659 if (*isDone == ExprSingleResult)
4662 * all sets are done, so report that tlist expansion is complete.
4664 *isDone = ExprEndResult;
4665 MemoryContextSwitchTo(oldContext);
4671 * We have some done and some undone sets. Restart the done ones
4672 * so that we can deliver a tuple (if possible).
4674 foreach(tl, targetlist)
4676 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4677 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4678 AttrNumber resind = tle->resno - 1;
4680 if (itemIsDone[resind] == ExprEndResult)
4682 values[resind] = ExecEvalExpr(gstate->arg,
4685 &itemIsDone[resind]);
4687 if (itemIsDone[resind] == ExprEndResult)
4690 * Oh dear, this item is returning an empty set. Guess
4691 * we can't make a tuple after all.
4693 *isDone = ExprEndResult;
4700 * If we cannot make a tuple because some sets are empty, we still
4701 * have to cycle the nonempty sets to completion, else resources
4702 * will not be released from subplans etc.
4704 * XXX is that still necessary?
4706 if (*isDone == ExprEndResult)
4708 foreach(tl, targetlist)
4710 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4711 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4712 AttrNumber resind = tle->resno - 1;
4714 while (itemIsDone[resind] == ExprMultipleResult)
4716 values[resind] = ExecEvalExpr(gstate->arg,
4719 &itemIsDone[resind]);
4723 MemoryContextSwitchTo(oldContext);
4729 /* Report success */
4730 MemoryContextSwitchTo(oldContext);
4737 * Evaluates a simple-Variable-list projection.
4739 * Results are stored into the passed values and isnull arrays.
4742 ExecVariableList(ProjectionInfo *projInfo,
4746 ExprContext *econtext = projInfo->pi_exprContext;
4747 int *varSlotOffsets = projInfo->pi_varSlotOffsets;
4748 int *varNumbers = projInfo->pi_varNumbers;
4752 * Force extraction of all input values that we need.
4754 if (projInfo->pi_lastInnerVar > 0)
4755 slot_getsomeattrs(econtext->ecxt_innertuple,
4756 projInfo->pi_lastInnerVar);
4757 if (projInfo->pi_lastOuterVar > 0)
4758 slot_getsomeattrs(econtext->ecxt_outertuple,
4759 projInfo->pi_lastOuterVar);
4760 if (projInfo->pi_lastScanVar > 0)
4761 slot_getsomeattrs(econtext->ecxt_scantuple,
4762 projInfo->pi_lastScanVar);
4765 * Assign to result by direct extraction of fields from source slots ... a
4766 * mite ugly, but fast ...
4768 for (i = list_length(projInfo->pi_targetlist) - 1; i >= 0; i--)
4770 char *slotptr = ((char *) econtext) + varSlotOffsets[i];
4771 TupleTableSlot *varSlot = *((TupleTableSlot **) slotptr);
4772 int varNumber = varNumbers[i] - 1;
4774 values[i] = varSlot->tts_values[varNumber];
4775 isnull[i] = varSlot->tts_isnull[varNumber];
4782 * projects a tuple based on projection info and stores
4783 * it in the previously specified tuple table slot.
4785 * Note: the result is always a virtual tuple; therefore it
4786 * may reference the contents of the exprContext's scan tuples
4787 * and/or temporary results constructed in the exprContext.
4788 * If the caller wishes the result to be valid longer than that
4789 * data will be valid, he must call ExecMaterializeSlot on the
4793 ExecProject(ProjectionInfo *projInfo, ExprDoneCond *isDone)
4795 TupleTableSlot *slot;
4800 Assert(projInfo != NULL);
4803 * get the projection info we want
4805 slot = projInfo->pi_slot;
4808 * Clear any former contents of the result slot. This makes it safe for
4809 * us to use the slot's Datum/isnull arrays as workspace. (Also, we can
4810 * return the slot as-is if we decide no rows can be projected.)
4812 ExecClearTuple(slot);
4815 * form a new result tuple (if possible); if successful, mark the result
4816 * slot as containing a valid virtual tuple
4818 if (projInfo->pi_isVarList)
4820 /* simple Var list: this always succeeds with one result row */
4822 *isDone = ExprSingleResult;
4823 ExecVariableList(projInfo,
4826 ExecStoreVirtualTuple(slot);
4830 if (ExecTargetList(projInfo->pi_targetlist,
4831 projInfo->pi_exprContext,
4834 projInfo->pi_itemIsDone,
4836 ExecStoreVirtualTuple(slot);