1 /*-------------------------------------------------------------------------
4 * Routines to evaluate qualification and targetlist expressions
6 * Portions Copyright (c) 1996-2008, 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.238 2008/12/18 19:38:22 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 (and substitute routines) rather than at every
33 * single node. This is a compromise that trades off precision of the
34 * stack limit setting to gain speed.
39 #include "access/nbtree.h"
40 #include "catalog/pg_type.h"
41 #include "commands/typecmds.h"
42 #include "executor/execdebug.h"
43 #include "executor/nodeSubplan.h"
45 #include "miscadmin.h"
46 #include "nodes/makefuncs.h"
47 #include "nodes/nodeFuncs.h"
48 #include "optimizer/planmain.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 init_fcache(Oid foid, FuncExprState *fcache,
78 MemoryContext fcacheCxt, bool needDescForSets);
79 static void ShutdownFuncExpr(Datum arg);
80 static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod,
81 TupleDesc *cache_field, ExprContext *econtext);
82 static void ShutdownTupleDescRef(Datum arg);
83 static ExprDoneCond ExecEvalFuncArgs(FunctionCallInfo fcinfo,
84 List *argList, ExprContext *econtext);
85 static void ExecPrepareTuplestoreResult(FuncExprState *fcache,
86 ExprContext *econtext,
87 Tuplestorestate *resultStore,
88 TupleDesc resultDesc);
89 static void tupledesc_match(TupleDesc dst_tupdesc, TupleDesc src_tupdesc);
90 static Datum ExecMakeFunctionResult(FuncExprState *fcache,
91 ExprContext *econtext,
93 ExprDoneCond *isDone);
94 static Datum ExecMakeFunctionResultNoSets(FuncExprState *fcache,
95 ExprContext *econtext,
96 bool *isNull, ExprDoneCond *isDone);
97 static Datum ExecEvalFunc(FuncExprState *fcache, ExprContext *econtext,
98 bool *isNull, ExprDoneCond *isDone);
99 static Datum ExecEvalOper(FuncExprState *fcache, ExprContext *econtext,
100 bool *isNull, ExprDoneCond *isDone);
101 static Datum ExecEvalDistinct(FuncExprState *fcache, ExprContext *econtext,
102 bool *isNull, ExprDoneCond *isDone);
103 static Datum ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
104 ExprContext *econtext,
105 bool *isNull, ExprDoneCond *isDone);
106 static Datum ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
107 bool *isNull, ExprDoneCond *isDone);
108 static Datum ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
109 bool *isNull, ExprDoneCond *isDone);
110 static Datum ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
111 bool *isNull, ExprDoneCond *isDone);
112 static Datum ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
113 ExprContext *econtext,
114 bool *isNull, ExprDoneCond *isDone);
115 static Datum ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
116 bool *isNull, ExprDoneCond *isDone);
117 static Datum ExecEvalCaseTestExpr(ExprState *exprstate,
118 ExprContext *econtext,
119 bool *isNull, ExprDoneCond *isDone);
120 static Datum ExecEvalArray(ArrayExprState *astate,
121 ExprContext *econtext,
122 bool *isNull, ExprDoneCond *isDone);
123 static Datum ExecEvalRow(RowExprState *rstate,
124 ExprContext *econtext,
125 bool *isNull, ExprDoneCond *isDone);
126 static Datum ExecEvalRowCompare(RowCompareExprState *rstate,
127 ExprContext *econtext,
128 bool *isNull, ExprDoneCond *isDone);
129 static Datum ExecEvalCoalesce(CoalesceExprState *coalesceExpr,
130 ExprContext *econtext,
131 bool *isNull, ExprDoneCond *isDone);
132 static Datum ExecEvalMinMax(MinMaxExprState *minmaxExpr,
133 ExprContext *econtext,
134 bool *isNull, ExprDoneCond *isDone);
135 static Datum ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
136 bool *isNull, ExprDoneCond *isDone);
137 static Datum ExecEvalNullIf(FuncExprState *nullIfExpr,
138 ExprContext *econtext,
139 bool *isNull, ExprDoneCond *isDone);
140 static Datum ExecEvalNullTest(NullTestState *nstate,
141 ExprContext *econtext,
142 bool *isNull, ExprDoneCond *isDone);
143 static Datum ExecEvalBooleanTest(GenericExprState *bstate,
144 ExprContext *econtext,
145 bool *isNull, ExprDoneCond *isDone);
146 static Datum ExecEvalCoerceToDomain(CoerceToDomainState *cstate,
147 ExprContext *econtext,
148 bool *isNull, ExprDoneCond *isDone);
149 static Datum ExecEvalCoerceToDomainValue(ExprState *exprstate,
150 ExprContext *econtext,
151 bool *isNull, ExprDoneCond *isDone);
152 static Datum ExecEvalFieldSelect(FieldSelectState *fstate,
153 ExprContext *econtext,
154 bool *isNull, ExprDoneCond *isDone);
155 static Datum ExecEvalFieldStore(FieldStoreState *fstate,
156 ExprContext *econtext,
157 bool *isNull, ExprDoneCond *isDone);
158 static Datum ExecEvalRelabelType(GenericExprState *exprstate,
159 ExprContext *econtext,
160 bool *isNull, ExprDoneCond *isDone);
161 static Datum ExecEvalCoerceViaIO(CoerceViaIOState *iostate,
162 ExprContext *econtext,
163 bool *isNull, ExprDoneCond *isDone);
164 static Datum ExecEvalArrayCoerceExpr(ArrayCoerceExprState *astate,
165 ExprContext *econtext,
166 bool *isNull, ExprDoneCond *isDone);
167 static Datum ExecEvalCurrentOfExpr(ExprState *exprstate, ExprContext *econtext,
168 bool *isNull, ExprDoneCond *isDone);
171 /* ----------------------------------------------------------------
172 * ExecEvalExpr routines
174 * Recursively evaluate a targetlist or qualification expression.
176 * Each of the following routines having the signature
177 * Datum ExecEvalFoo(ExprState *expression,
178 * ExprContext *econtext,
180 * ExprDoneCond *isDone);
181 * is responsible for evaluating one type or subtype of ExprState node.
182 * They are normally called via the ExecEvalExpr macro, which makes use of
183 * the function pointer set up when the ExprState node was built by
184 * ExecInitExpr. (In some cases, we change this pointer later to avoid
185 * re-executing one-time overhead.)
187 * Note: for notational simplicity we declare these functions as taking the
188 * specific type of ExprState that they work on. This requires casting when
189 * assigning the function pointer in ExecInitExpr. Be careful that the
190 * function signature is declared correctly, because the cast suppresses
191 * automatic checking!
194 * All these functions share this calling convention:
197 * expression: the expression state tree to evaluate
198 * econtext: evaluation context information
201 * return value: Datum value of result
202 * *isNull: set to TRUE if result is NULL (actual return value is
203 * meaningless if so); set to FALSE if non-null result
204 * *isDone: set to indicator of set-result status
206 * A caller that can only accept a singleton (non-set) result should pass
207 * NULL for isDone; if the expression computes a set result then an error
208 * will be reported via ereport. If the caller does pass an isDone pointer
209 * then *isDone is set to one of these three states:
210 * ExprSingleResult singleton result (not a set)
211 * ExprMultipleResult return value is one element of a set
212 * ExprEndResult there are no more elements in the set
213 * When ExprMultipleResult is returned, the caller should invoke
214 * ExecEvalExpr() repeatedly until ExprEndResult is returned. ExprEndResult
215 * is returned after the last real set element. For convenience isNull will
216 * always be set TRUE when ExprEndResult is returned, but this should not be
217 * taken as indicating a NULL element of the set. Note that these return
218 * conventions allow us to distinguish among a singleton NULL, a NULL element
219 * of a set, and an empty set.
221 * The caller should already have switched into the temporary memory
222 * context econtext->ecxt_per_tuple_memory. The convenience entry point
223 * ExecEvalExprSwitchContext() is provided for callers who don't prefer to
224 * do the switch in an outer loop. We do not do the switch in these routines
225 * because it'd be a waste of cycles during nested expression evaluation.
226 * ----------------------------------------------------------------
233 * This function takes an ArrayRef and returns the extracted Datum
234 * if it's a simple reference, or the modified array value if it's
235 * an array assignment (i.e., array element or slice insertion).
237 * NOTE: if we get a NULL result from a subscript expression, we return NULL
238 * when it's an array reference, or raise an error when it's an assignment.
240 * NOTE: we deliberately refrain from applying DatumGetArrayTypeP() here,
241 * even though that might seem natural, because this code needs to support
242 * both varlena arrays and fixed-length array types. DatumGetArrayTypeP()
243 * only works for the varlena kind. The routines we call in arrayfuncs.c
244 * have to know the difference (that's what they need refattrlength for).
248 ExecEvalArrayRef(ArrayRefExprState *astate,
249 ExprContext *econtext,
251 ExprDoneCond *isDone)
253 ArrayRef *arrayRef = (ArrayRef *) astate->xprstate.expr;
254 ArrayType *array_source;
255 ArrayType *resultArray;
256 bool isAssignment = (arrayRef->refassgnexpr != NULL);
265 array_source = (ArrayType *)
266 DatumGetPointer(ExecEvalExpr(astate->refexpr,
272 * If refexpr yields NULL, and it's a fetch, then result is NULL. In the
273 * assignment case, we'll cons up something below.
277 if (isDone && *isDone == ExprEndResult)
278 return (Datum) NULL; /* end of set result */
283 foreach(l, astate->refupperindexpr)
285 ExprState *eltstate = (ExprState *) lfirst(l);
289 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
290 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
293 upper.indx[i++] = DatumGetInt32(ExecEvalExpr(eltstate,
297 /* If any index expr yields NULL, result is NULL or error */
302 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
303 errmsg("array subscript in assignment must not be null")));
309 if (astate->reflowerindexpr != NIL)
311 foreach(l, astate->reflowerindexpr)
313 ExprState *eltstate = (ExprState *) lfirst(l);
317 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
318 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
321 lower.indx[j++] = DatumGetInt32(ExecEvalExpr(eltstate,
325 /* If any index expr yields NULL, result is NULL or error */
330 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
331 errmsg("array subscript in assignment must not be null")));
336 /* this can't happen unless parser messed up */
338 elog(ERROR, "upper and lower index lists are not same length");
349 * Evaluate the value to be assigned into the array.
351 * XXX At some point we'll need to look into making the old value of
352 * the array element available via CaseTestExpr, as is done by
353 * ExecEvalFieldStore. This is not needed now but will be needed to
354 * support arrays of composite types; in an assignment to a field of
355 * an array member, the parser would generate a FieldStore that
356 * expects to fetch its input tuple via CaseTestExpr.
358 sourceData = ExecEvalExpr(astate->refassgnexpr,
364 * For an assignment to a fixed-length array type, both the original
365 * array and the value to be assigned into it must be non-NULL, else
366 * we punt and return the original array.
368 if (astate->refattrlength > 0) /* fixed-length array? */
369 if (eisnull || *isNull)
370 return PointerGetDatum(array_source);
373 * For assignment to varlena arrays, we handle a NULL original array
374 * by substituting an empty (zero-dimensional) array; insertion of the
375 * new element will result in a singleton array value. It does not
376 * matter whether the new element is NULL.
380 array_source = construct_empty_array(arrayRef->refelemtype);
385 resultArray = array_set(array_source, i,
389 astate->refattrlength,
390 astate->refelemlength,
391 astate->refelembyval,
392 astate->refelemalign);
394 resultArray = array_set_slice(array_source, i,
395 upper.indx, lower.indx,
396 (ArrayType *) DatumGetPointer(sourceData),
398 astate->refattrlength,
399 astate->refelemlength,
400 astate->refelembyval,
401 astate->refelemalign);
402 return PointerGetDatum(resultArray);
406 return array_ref(array_source, i, upper.indx,
407 astate->refattrlength,
408 astate->refelemlength,
409 astate->refelembyval,
410 astate->refelemalign,
414 resultArray = array_get_slice(array_source, i,
415 upper.indx, lower.indx,
416 astate->refattrlength,
417 astate->refelemlength,
418 astate->refelembyval,
419 astate->refelemalign);
420 return PointerGetDatum(resultArray);
425 /* ----------------------------------------------------------------
428 * Returns a Datum whose value is the value of the precomputed
429 * aggregate found in the given expression context.
430 * ----------------------------------------------------------------
433 ExecEvalAggref(AggrefExprState *aggref, ExprContext *econtext,
434 bool *isNull, ExprDoneCond *isDone)
437 *isDone = ExprSingleResult;
439 if (econtext->ecxt_aggvalues == NULL) /* safety check */
440 elog(ERROR, "no aggregates in this expression context");
442 *isNull = econtext->ecxt_aggnulls[aggref->aggno];
443 return econtext->ecxt_aggvalues[aggref->aggno];
446 /* ----------------------------------------------------------------
449 * Returns a Datum whose value is the value of a range
450 * variable with respect to given expression context.
452 * Note: ExecEvalVar is executed only the first time through in a given plan;
453 * it changes the ExprState's function pointer to pass control directly to
454 * ExecEvalScalarVar, ExecEvalWholeRowVar, or ExecEvalWholeRowSlow after
455 * making one-time checks.
456 * ----------------------------------------------------------------
459 ExecEvalVar(ExprState *exprstate, ExprContext *econtext,
460 bool *isNull, ExprDoneCond *isDone)
462 Var *variable = (Var *) exprstate->expr;
463 TupleTableSlot *slot;
467 *isDone = ExprSingleResult;
470 * Get the input slot and attribute number we want
472 * The asserts check that references to system attributes only appear at
473 * the level of a relation scan; at higher levels, system attributes must
474 * be treated as ordinary variables (since we no longer have access to the
477 attnum = variable->varattno;
479 switch (variable->varno)
481 case INNER: /* get the tuple from the inner node */
482 slot = econtext->ecxt_innertuple;
486 case OUTER: /* get the tuple from the outer node */
487 slot = econtext->ecxt_outertuple;
491 default: /* get the tuple from the relation being
493 slot = econtext->ecxt_scantuple;
497 if (attnum != InvalidAttrNumber)
500 * Scalar variable case.
502 * If it's a user attribute, check validity (bogus system attnums will
503 * be caught inside slot_getattr). What we have to check for here is
504 * the possibility of an attribute having been changed in type since
505 * the plan tree was created. Ideally the plan would get invalidated
506 * and not re-used, but until that day arrives, we need defenses.
507 * Fortunately it's sufficient to check once on the first time
510 * Note: we allow a reference to a dropped attribute. slot_getattr
511 * will force a NULL result in such cases.
513 * Note: ideally we'd check typmod as well as typid, but that seems
514 * impractical at the moment: in many cases the tupdesc will have been
515 * generated by ExecTypeFromTL(), and that can't guarantee to generate
516 * an accurate typmod in all cases, because some expression node types
517 * don't carry typmod.
521 TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
522 Form_pg_attribute attr;
524 if (attnum > slot_tupdesc->natts) /* should never happen */
525 elog(ERROR, "attribute number %d exceeds number of columns %d",
526 attnum, slot_tupdesc->natts);
528 attr = slot_tupdesc->attrs[attnum - 1];
530 /* can't check type if dropped, since atttypid is probably 0 */
531 if (!attr->attisdropped)
533 if (variable->vartype != attr->atttypid)
535 (errmsg("attribute %d has wrong type", attnum),
536 errdetail("Table has type %s, but query expects %s.",
537 format_type_be(attr->atttypid),
538 format_type_be(variable->vartype))));
542 /* Skip the checking on future executions of node */
543 exprstate->evalfunc = ExecEvalScalarVar;
545 /* Fetch the value from the slot */
546 return slot_getattr(slot, attnum, isNull);
551 * Whole-row variable.
553 * If it's a RECORD Var, we'll use the slot's type ID info. It's
554 * likely that the slot's type is also RECORD; if so, make sure it's
555 * been "blessed", so that the Datum can be interpreted later.
557 * If the Var identifies a named composite type, we must check that
558 * the actual tuple type is compatible with it.
560 TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
561 bool needslow = false;
563 if (variable->vartype == RECORDOID)
565 if (slot_tupdesc->tdtypeid == RECORDOID &&
566 slot_tupdesc->tdtypmod < 0)
567 assign_record_type_typmod(slot_tupdesc);
571 TupleDesc var_tupdesc;
575 * We really only care about number of attributes and data type.
576 * Also, we can ignore type mismatch on columns that are dropped
577 * in the destination type, so long as the physical storage
578 * matches. This is helpful in some cases involving out-of-date
579 * cached plans. Also, we have to allow the case that the slot
580 * has more columns than the Var's type, because we might be
581 * looking at the output of a subplan that includes resjunk
582 * columns. (XXX it would be nice to verify that the extra
583 * columns are all marked resjunk, but we haven't got access to
584 * the subplan targetlist here...) Resjunk columns should always
585 * be at the end of a targetlist, so it's sufficient to ignore
586 * them here; but we need to use ExecEvalWholeRowSlow to get rid
587 * of them in the eventual output tuples.
589 var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1);
591 if (var_tupdesc->natts > slot_tupdesc->natts)
593 (errcode(ERRCODE_DATATYPE_MISMATCH),
594 errmsg("table row type and query-specified row type do not match"),
595 errdetail("Table row contains %d attributes, but query expects %d.",
596 slot_tupdesc->natts, var_tupdesc->natts)));
597 else if (var_tupdesc->natts < slot_tupdesc->natts)
600 for (i = 0; i < var_tupdesc->natts; i++)
602 Form_pg_attribute vattr = var_tupdesc->attrs[i];
603 Form_pg_attribute sattr = slot_tupdesc->attrs[i];
605 if (vattr->atttypid == sattr->atttypid)
606 continue; /* no worries */
607 if (!vattr->attisdropped)
609 (errcode(ERRCODE_DATATYPE_MISMATCH),
610 errmsg("table row type and query-specified row type do not match"),
611 errdetail("Table has type %s at ordinal position %d, but query expects %s.",
612 format_type_be(sattr->atttypid),
614 format_type_be(vattr->atttypid))));
616 if (vattr->attlen != sattr->attlen ||
617 vattr->attalign != sattr->attalign)
619 (errcode(ERRCODE_DATATYPE_MISMATCH),
620 errmsg("table row type and query-specified row type do not match"),
621 errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
625 ReleaseTupleDesc(var_tupdesc);
628 /* Skip the checking on future executions of node */
630 exprstate->evalfunc = ExecEvalWholeRowSlow;
632 exprstate->evalfunc = ExecEvalWholeRowVar;
634 /* Fetch the value */
635 return ExecEvalWholeRowVar(exprstate, econtext, isNull, isDone);
639 /* ----------------------------------------------------------------
642 * Returns a Datum for a scalar variable.
643 * ----------------------------------------------------------------
646 ExecEvalScalarVar(ExprState *exprstate, ExprContext *econtext,
647 bool *isNull, ExprDoneCond *isDone)
649 Var *variable = (Var *) exprstate->expr;
650 TupleTableSlot *slot;
654 *isDone = ExprSingleResult;
656 /* Get the input slot and attribute number we want */
657 switch (variable->varno)
659 case INNER: /* get the tuple from the inner node */
660 slot = econtext->ecxt_innertuple;
663 case OUTER: /* get the tuple from the outer node */
664 slot = econtext->ecxt_outertuple;
667 default: /* get the tuple from the relation being
669 slot = econtext->ecxt_scantuple;
673 attnum = variable->varattno;
675 /* Fetch the value from the slot */
676 return slot_getattr(slot, attnum, isNull);
679 /* ----------------------------------------------------------------
680 * ExecEvalWholeRowVar
682 * Returns a Datum for a whole-row variable.
683 * ----------------------------------------------------------------
686 ExecEvalWholeRowVar(ExprState *exprstate, ExprContext *econtext,
687 bool *isNull, ExprDoneCond *isDone)
689 Var *variable = (Var *) exprstate->expr;
690 TupleTableSlot *slot = econtext->ecxt_scantuple;
693 HeapTupleHeader dtuple;
696 *isDone = ExprSingleResult;
699 tuple = ExecFetchSlotTuple(slot);
700 tupleDesc = slot->tts_tupleDescriptor;
703 * We have to make a copy of the tuple so we can safely insert the Datum
704 * overhead fields, which are not set in on-disk tuples.
706 dtuple = (HeapTupleHeader) palloc(tuple->t_len);
707 memcpy((char *) dtuple, (char *) tuple->t_data, tuple->t_len);
709 HeapTupleHeaderSetDatumLength(dtuple, tuple->t_len);
712 * If the Var identifies a named composite type, label the tuple with that
713 * type; otherwise use what is in the tupleDesc.
715 if (variable->vartype != RECORDOID)
717 HeapTupleHeaderSetTypeId(dtuple, variable->vartype);
718 HeapTupleHeaderSetTypMod(dtuple, variable->vartypmod);
722 HeapTupleHeaderSetTypeId(dtuple, tupleDesc->tdtypeid);
723 HeapTupleHeaderSetTypMod(dtuple, tupleDesc->tdtypmod);
726 return PointerGetDatum(dtuple);
729 /* ----------------------------------------------------------------
730 * ExecEvalWholeRowSlow
732 * Returns a Datum for a whole-row variable, in the "slow" case where
733 * we can't just copy the subplan's output.
734 * ----------------------------------------------------------------
737 ExecEvalWholeRowSlow(ExprState *exprstate, ExprContext *econtext,
738 bool *isNull, ExprDoneCond *isDone)
740 Var *variable = (Var *) exprstate->expr;
741 TupleTableSlot *slot = econtext->ecxt_scantuple;
743 TupleDesc var_tupdesc;
744 HeapTupleHeader dtuple;
747 *isDone = ExprSingleResult;
751 * Currently, the only case handled here is stripping of trailing resjunk
752 * fields, which we do in a slightly chintzy way by just adjusting the
753 * tuple's natts header field. Possibly there will someday be a need for
754 * more-extensive rearrangements, in which case it'd be worth
755 * disassembling and reassembling the tuple (perhaps use a JunkFilter for
758 Assert(variable->vartype != RECORDOID);
759 var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1);
761 tuple = ExecFetchSlotTuple(slot);
764 * We have to make a copy of the tuple so we can safely insert the Datum
765 * overhead fields, which are not set in on-disk tuples; not to mention
766 * fooling with its natts field.
768 dtuple = (HeapTupleHeader) palloc(tuple->t_len);
769 memcpy((char *) dtuple, (char *) tuple->t_data, tuple->t_len);
771 HeapTupleHeaderSetDatumLength(dtuple, tuple->t_len);
772 HeapTupleHeaderSetTypeId(dtuple, variable->vartype);
773 HeapTupleHeaderSetTypMod(dtuple, variable->vartypmod);
775 Assert(HeapTupleHeaderGetNatts(dtuple) >= var_tupdesc->natts);
776 HeapTupleHeaderSetNatts(dtuple, var_tupdesc->natts);
778 ReleaseTupleDesc(var_tupdesc);
780 return PointerGetDatum(dtuple);
783 /* ----------------------------------------------------------------
786 * Returns the value of a constant.
788 * Note that for pass-by-ref datatypes, we return a pointer to the
789 * actual constant node. This is one of the reasons why functions
790 * must treat their input arguments as read-only.
791 * ----------------------------------------------------------------
794 ExecEvalConst(ExprState *exprstate, ExprContext *econtext,
795 bool *isNull, ExprDoneCond *isDone)
797 Const *con = (Const *) exprstate->expr;
800 *isDone = ExprSingleResult;
802 *isNull = con->constisnull;
803 return con->constvalue;
806 /* ----------------------------------------------------------------
809 * Returns the value of a parameter. A param node contains
810 * something like ($.name) and the expression context contains
811 * the current parameter bindings (name = "sam") (age = 34)...
812 * so our job is to find and return the appropriate datum ("sam").
813 * ----------------------------------------------------------------
816 ExecEvalParam(ExprState *exprstate, ExprContext *econtext,
817 bool *isNull, ExprDoneCond *isDone)
819 Param *expression = (Param *) exprstate->expr;
820 int thisParamId = expression->paramid;
823 *isDone = ExprSingleResult;
825 if (expression->paramkind == PARAM_EXEC)
828 * PARAM_EXEC params (internal executor parameters) are stored in the
829 * ecxt_param_exec_vals array, and can be accessed by array index.
833 prm = &(econtext->ecxt_param_exec_vals[thisParamId]);
834 if (prm->execPlan != NULL)
836 /* Parameter not evaluated yet, so go do it */
837 ExecSetParamPlan(prm->execPlan, econtext);
838 /* ExecSetParamPlan should have processed this param... */
839 Assert(prm->execPlan == NULL);
841 *isNull = prm->isnull;
847 * PARAM_EXTERN parameters must be sought in ecxt_param_list_info.
849 ParamListInfo paramInfo = econtext->ecxt_param_list_info;
851 Assert(expression->paramkind == PARAM_EXTERN);
853 thisParamId > 0 && thisParamId <= paramInfo->numParams)
855 ParamExternData *prm = ¶mInfo->params[thisParamId - 1];
857 if (OidIsValid(prm->ptype))
859 Assert(prm->ptype == expression->paramtype);
860 *isNull = prm->isnull;
865 (errcode(ERRCODE_UNDEFINED_OBJECT),
866 errmsg("no value found for parameter %d", thisParamId)));
867 return (Datum) 0; /* keep compiler quiet */
872 /* ----------------------------------------------------------------
873 * ExecEvalOper / ExecEvalFunc support routines
874 * ----------------------------------------------------------------
881 * These functions return the value of the requested attribute
882 * out of the given tuple Datum.
883 * C functions which take a tuple as an argument are expected
884 * to use these. Ex: overpaid(EMP) might call GetAttributeByNum().
885 * Note: these are actually rather slow because they do a typcache
886 * lookup on each call.
889 GetAttributeByNum(HeapTupleHeader tuple,
897 HeapTupleData tmptup;
899 if (!AttributeNumberIsValid(attrno))
900 elog(ERROR, "invalid attribute number %d", attrno);
903 elog(ERROR, "a NULL isNull pointer was passed");
907 /* Kinda bogus but compatible with old behavior... */
912 tupType = HeapTupleHeaderGetTypeId(tuple);
913 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
914 tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
917 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
918 * the fields in the struct just in case user tries to inspect system
921 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
922 ItemPointerSetInvalid(&(tmptup.t_self));
923 tmptup.t_tableOid = InvalidOid;
924 tmptup.t_data = tuple;
926 result = heap_getattr(&tmptup,
931 ReleaseTupleDesc(tupDesc);
937 GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
944 HeapTupleData tmptup;
948 elog(ERROR, "invalid attribute name");
951 elog(ERROR, "a NULL isNull pointer was passed");
955 /* Kinda bogus but compatible with old behavior... */
960 tupType = HeapTupleHeaderGetTypeId(tuple);
961 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
962 tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
964 attrno = InvalidAttrNumber;
965 for (i = 0; i < tupDesc->natts; i++)
967 if (namestrcmp(&(tupDesc->attrs[i]->attname), attname) == 0)
969 attrno = tupDesc->attrs[i]->attnum;
974 if (attrno == InvalidAttrNumber)
975 elog(ERROR, "attribute \"%s\" does not exist", attname);
978 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
979 * the fields in the struct just in case user tries to inspect system
982 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
983 ItemPointerSetInvalid(&(tmptup.t_self));
984 tmptup.t_tableOid = InvalidOid;
985 tmptup.t_data = tuple;
987 result = heap_getattr(&tmptup,
992 ReleaseTupleDesc(tupDesc);
998 * init_fcache - initialize a FuncExprState node during first use
1001 init_fcache(Oid foid, FuncExprState *fcache,
1002 MemoryContext fcacheCxt, bool needDescForSets)
1004 AclResult aclresult;
1006 /* Check permission to call function */
1007 aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE);
1008 if (aclresult != ACLCHECK_OK)
1009 aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(foid));
1012 * Safety check on nargs. Under normal circumstances this should never
1013 * fail, as parser should check sooner. But possibly it might fail if
1014 * server has been compiled with FUNC_MAX_ARGS smaller than some functions
1015 * declared in pg_proc?
1017 if (list_length(fcache->args) > FUNC_MAX_ARGS)
1019 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
1020 errmsg("cannot pass more than %d arguments to a function",
1023 /* Set up the primary fmgr lookup information */
1024 fmgr_info_cxt(foid, &(fcache->func), fcacheCxt);
1025 fcache->func.fn_expr = (Node *) fcache->xprstate.expr;
1027 /* If function returns set, prepare expected tuple descriptor */
1028 if (fcache->func.fn_retset && needDescForSets)
1030 TypeFuncClass functypclass;
1033 MemoryContext oldcontext;
1035 functypclass = get_expr_result_type(fcache->func.fn_expr,
1039 /* Must save tupdesc in fcache's context */
1040 oldcontext = MemoryContextSwitchTo(fcacheCxt);
1042 if (functypclass == TYPEFUNC_COMPOSITE)
1044 /* Composite data type, e.g. a table's row type */
1046 /* Must copy it out of typcache for safety */
1047 fcache->funcResultDesc = CreateTupleDescCopy(tupdesc);
1048 fcache->funcReturnsTuple = true;
1050 else if (functypclass == TYPEFUNC_SCALAR)
1052 /* Base data type, i.e. scalar */
1053 tupdesc = CreateTemplateTupleDesc(1, false);
1054 TupleDescInitEntry(tupdesc,
1060 fcache->funcResultDesc = tupdesc;
1061 fcache->funcReturnsTuple = false;
1065 /* Else, we will complain if function wants materialize mode */
1066 fcache->funcResultDesc = NULL;
1069 MemoryContextSwitchTo(oldcontext);
1072 fcache->funcResultDesc = NULL;
1074 /* Initialize additional state */
1075 fcache->funcResultStore = NULL;
1076 fcache->funcResultSlot = NULL;
1077 fcache->setArgsValid = false;
1078 fcache->shutdown_reg = false;
1082 * callback function in case a FuncExpr returning a set needs to be shut down
1083 * before it has been run to completion
1086 ShutdownFuncExpr(Datum arg)
1088 FuncExprState *fcache = (FuncExprState *) DatumGetPointer(arg);
1090 /* If we have a slot, make sure it's let go of any tuplestore pointer */
1091 if (fcache->funcResultSlot)
1092 ExecClearTuple(fcache->funcResultSlot);
1094 /* Release any open tuplestore */
1095 if (fcache->funcResultStore)
1096 tuplestore_end(fcache->funcResultStore);
1097 fcache->funcResultStore = NULL;
1099 /* Clear any active set-argument state */
1100 fcache->setArgsValid = false;
1102 /* execUtils will deregister the callback... */
1103 fcache->shutdown_reg = false;
1107 * get_cached_rowtype: utility function to lookup a rowtype tupdesc
1109 * type_id, typmod: identity of the rowtype
1110 * cache_field: where to cache the TupleDesc pointer in expression state node
1111 * (field must be initialized to NULL)
1112 * econtext: expression context we are executing in
1114 * NOTE: because the shutdown callback will be called during plan rescan,
1115 * must be prepared to re-do this during any node execution; cannot call
1116 * just once during expression initialization
1119 get_cached_rowtype(Oid type_id, int32 typmod,
1120 TupleDesc *cache_field, ExprContext *econtext)
1122 TupleDesc tupDesc = *cache_field;
1124 /* Do lookup if no cached value or if requested type changed */
1125 if (tupDesc == NULL ||
1126 type_id != tupDesc->tdtypeid ||
1127 typmod != tupDesc->tdtypmod)
1129 tupDesc = lookup_rowtype_tupdesc(type_id, typmod);
1133 /* Release old tupdesc; but callback is already registered */
1134 ReleaseTupleDesc(*cache_field);
1138 /* Need to register shutdown callback to release tupdesc */
1139 RegisterExprContextCallback(econtext,
1140 ShutdownTupleDescRef,
1141 PointerGetDatum(cache_field));
1143 *cache_field = tupDesc;
1149 * Callback function to release a tupdesc refcount at expression tree shutdown
1152 ShutdownTupleDescRef(Datum arg)
1154 TupleDesc *cache_field = (TupleDesc *) DatumGetPointer(arg);
1157 ReleaseTupleDesc(*cache_field);
1158 *cache_field = NULL;
1162 * Evaluate arguments for a function.
1165 ExecEvalFuncArgs(FunctionCallInfo fcinfo,
1167 ExprContext *econtext)
1169 ExprDoneCond argIsDone;
1173 argIsDone = ExprSingleResult; /* default assumption */
1176 foreach(arg, argList)
1178 ExprState *argstate = (ExprState *) lfirst(arg);
1179 ExprDoneCond thisArgIsDone;
1181 fcinfo->arg[i] = ExecEvalExpr(argstate,
1183 &fcinfo->argnull[i],
1186 if (thisArgIsDone != ExprSingleResult)
1189 * We allow only one argument to have a set value; we'd need much
1190 * more complexity to keep track of multiple set arguments (cf.
1191 * ExecTargetList) and it doesn't seem worth it.
1193 if (argIsDone != ExprSingleResult)
1195 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1196 errmsg("functions and operators can take at most one set argument")));
1197 argIsDone = thisArgIsDone;
1208 * ExecPrepareTuplestoreResult
1210 * Subroutine for ExecMakeFunctionResult: prepare to extract rows from a
1211 * tuplestore function result. We must set up a funcResultSlot (unless
1212 * already done in a previous call cycle) and verify that the function
1213 * returned the expected tuple descriptor.
1216 ExecPrepareTuplestoreResult(FuncExprState *fcache,
1217 ExprContext *econtext,
1218 Tuplestorestate *resultStore,
1219 TupleDesc resultDesc)
1221 fcache->funcResultStore = resultStore;
1223 if (fcache->funcResultSlot == NULL)
1225 /* Create a slot so we can read data out of the tuplestore */
1226 MemoryContext oldcontext;
1228 /* We must have been able to determine the result rowtype */
1229 if (fcache->funcResultDesc == NULL)
1231 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1232 errmsg("function returning setof record called in "
1233 "context that cannot accept type record")));
1235 oldcontext = MemoryContextSwitchTo(fcache->func.fn_mcxt);
1236 fcache->funcResultSlot =
1237 MakeSingleTupleTableSlot(fcache->funcResultDesc);
1238 MemoryContextSwitchTo(oldcontext);
1242 * If function provided a tupdesc, cross-check it. We only really
1243 * need to do this for functions returning RECORD, but might as well
1248 if (fcache->funcResultDesc)
1249 tupledesc_match(fcache->funcResultDesc, resultDesc);
1252 * If it is a dynamically-allocated TupleDesc, free it: it is
1253 * typically allocated in a per-query context, so we must avoid
1254 * leaking it across multiple usages.
1256 if (resultDesc->tdrefcount == -1)
1257 FreeTupleDesc(resultDesc);
1260 /* Register cleanup callback if we didn't already */
1261 if (!fcache->shutdown_reg)
1263 RegisterExprContextCallback(econtext,
1265 PointerGetDatum(fcache));
1266 fcache->shutdown_reg = true;
1271 * Check that function result tuple type (src_tupdesc) matches or can
1272 * be considered to match what the query expects (dst_tupdesc). If
1273 * they don't match, ereport.
1275 * We really only care about number of attributes and data type.
1276 * Also, we can ignore type mismatch on columns that are dropped in the
1277 * destination type, so long as the physical storage matches. This is
1278 * helpful in some cases involving out-of-date cached plans.
1281 tupledesc_match(TupleDesc dst_tupdesc, TupleDesc src_tupdesc)
1285 if (dst_tupdesc->natts != src_tupdesc->natts)
1287 (errcode(ERRCODE_DATATYPE_MISMATCH),
1288 errmsg("function return row and query-specified return row do not match"),
1289 errdetail("Returned row contains %d attributes, but query expects %d.",
1290 src_tupdesc->natts, dst_tupdesc->natts)));
1292 for (i = 0; i < dst_tupdesc->natts; i++)
1294 Form_pg_attribute dattr = dst_tupdesc->attrs[i];
1295 Form_pg_attribute sattr = src_tupdesc->attrs[i];
1297 if (dattr->atttypid == sattr->atttypid)
1298 continue; /* no worries */
1299 if (!dattr->attisdropped)
1301 (errcode(ERRCODE_DATATYPE_MISMATCH),
1302 errmsg("function return row and query-specified return row do not match"),
1303 errdetail("Returned type %s at ordinal position %d, but query expects %s.",
1304 format_type_be(sattr->atttypid),
1306 format_type_be(dattr->atttypid))));
1308 if (dattr->attlen != sattr->attlen ||
1309 dattr->attalign != sattr->attalign)
1311 (errcode(ERRCODE_DATATYPE_MISMATCH),
1312 errmsg("function return row and query-specified return row do not match"),
1313 errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
1319 * ExecMakeFunctionResult
1321 * Evaluate the arguments to a function and then the function itself.
1322 * init_fcache is presumed already run on the FuncExprState.
1324 * This function handles the most general case, wherein the function or
1325 * one of its arguments might (or might not) return a set. If we find
1326 * no sets involved, we will change the FuncExprState's function pointer
1327 * to use a simpler method on subsequent calls.
1330 ExecMakeFunctionResult(FuncExprState *fcache,
1331 ExprContext *econtext,
1333 ExprDoneCond *isDone)
1337 FunctionCallInfoData fcinfo_data;
1338 FunctionCallInfo fcinfo;
1339 PgStat_FunctionCallUsage fcusage;
1340 ReturnSetInfo rsinfo; /* for functions returning sets */
1341 ExprDoneCond argDone;
1347 /* Guard against stack overflow due to overly complex expressions */
1348 check_stack_depth();
1351 * If a previous call of the function returned a set result in the form
1352 * of a tuplestore, continue reading rows from the tuplestore until it's
1355 if (fcache->funcResultStore)
1357 Assert(isDone); /* it was provided before ... */
1358 if (tuplestore_gettupleslot(fcache->funcResultStore, true,
1359 fcache->funcResultSlot))
1361 *isDone = ExprMultipleResult;
1362 if (fcache->funcReturnsTuple)
1364 /* We must return the whole tuple as a Datum. */
1366 return ExecFetchSlotTupleDatum(fcache->funcResultSlot);
1370 /* Extract the first column and return it as a scalar. */
1371 return slot_getattr(fcache->funcResultSlot, 1, isNull);
1374 /* Exhausted the tuplestore, so clean up */
1375 tuplestore_end(fcache->funcResultStore);
1376 fcache->funcResultStore = NULL;
1377 /* We are done unless there was a set-valued argument */
1378 if (!fcache->setHasSetArg)
1380 *isDone = ExprEndResult;
1384 /* If there was, continue evaluating the argument values */
1385 Assert(!fcache->setArgsValid);
1389 * For non-set-returning functions, we just use a local-variable
1390 * FunctionCallInfoData. For set-returning functions we keep the callinfo
1391 * record in fcache->setArgs so that it can survive across multiple
1392 * value-per-call invocations. (The reason we don't just do the latter
1393 * all the time is that plpgsql expects to be able to use simple expression
1394 * trees re-entrantly. Which might not be a good idea, but the penalty
1395 * for not doing so is high.)
1397 if (fcache->func.fn_retset)
1398 fcinfo = &fcache->setArgs;
1400 fcinfo = &fcinfo_data;
1403 * arguments is a list of expressions to evaluate before passing to the
1404 * function manager. We skip the evaluation if it was already done in the
1405 * previous call (ie, we are continuing the evaluation of a set-valued
1406 * function). Otherwise, collect the current argument values into fcinfo.
1408 arguments = fcache->args;
1409 if (!fcache->setArgsValid)
1411 /* Need to prep callinfo structure */
1412 InitFunctionCallInfoData(*fcinfo, &(fcache->func), 0, NULL, NULL);
1413 argDone = ExecEvalFuncArgs(fcinfo, arguments, econtext);
1414 if (argDone == ExprEndResult)
1416 /* input is an empty set, so return an empty set. */
1419 *isDone = ExprEndResult;
1422 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1423 errmsg("set-valued function called in context that cannot accept a set")));
1426 hasSetArg = (argDone != ExprSingleResult);
1430 /* Re-use callinfo from previous evaluation */
1431 hasSetArg = fcache->setHasSetArg;
1432 /* Reset flag (we may set it again below) */
1433 fcache->setArgsValid = false;
1437 * Now call the function, passing the evaluated parameter values.
1439 if (fcache->func.fn_retset || hasSetArg)
1442 * We need to return a set result. Complain if caller not ready to
1447 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1448 errmsg("set-valued function called in context that cannot accept a set")));
1451 * Prepare a resultinfo node for communication. If the function
1452 * doesn't itself return set, we don't pass the resultinfo to the
1453 * function, but we need to fill it in anyway for internal use.
1455 if (fcache->func.fn_retset)
1456 fcinfo->resultinfo = (Node *) &rsinfo;
1457 rsinfo.type = T_ReturnSetInfo;
1458 rsinfo.econtext = econtext;
1459 rsinfo.expectedDesc = fcache->funcResultDesc;
1460 rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize);
1461 /* note we do not set SFRM_Materialize_Random or _Preferred */
1462 rsinfo.returnMode = SFRM_ValuePerCall;
1463 /* isDone is filled below */
1464 rsinfo.setResult = NULL;
1465 rsinfo.setDesc = NULL;
1468 * This loop handles the situation where we have both a set argument
1469 * and a set-valued function. Once we have exhausted the function's
1470 * value(s) for a particular argument value, we have to get the next
1471 * argument value and start the function over again. We might have to
1472 * do it more than once, if the function produces an empty result set
1473 * for a particular input value.
1478 * If function is strict, and there are any NULL arguments, skip
1479 * calling the function (at least for this set of args).
1483 if (fcache->func.fn_strict)
1485 for (i = 0; i < fcinfo->nargs; i++)
1487 if (fcinfo->argnull[i])
1497 pgstat_init_function_usage(fcinfo, &fcusage);
1499 fcinfo->isnull = false;
1500 rsinfo.isDone = ExprSingleResult;
1501 result = FunctionCallInvoke(fcinfo);
1502 *isNull = fcinfo->isnull;
1503 *isDone = rsinfo.isDone;
1505 pgstat_end_function_usage(&fcusage,
1506 rsinfo.isDone != ExprMultipleResult);
1512 *isDone = ExprEndResult;
1515 /* Which protocol does function want to use? */
1516 if (rsinfo.returnMode == SFRM_ValuePerCall)
1518 if (*isDone != ExprEndResult)
1521 * Got a result from current argument. If function itself
1522 * returns set, save the current argument values to re-use
1525 if (fcache->func.fn_retset &&
1526 *isDone == ExprMultipleResult)
1528 Assert(fcinfo == &fcache->setArgs);
1529 fcache->setHasSetArg = hasSetArg;
1530 fcache->setArgsValid = true;
1531 /* Register cleanup callback if we didn't already */
1532 if (!fcache->shutdown_reg)
1534 RegisterExprContextCallback(econtext,
1536 PointerGetDatum(fcache));
1537 fcache->shutdown_reg = true;
1542 * Make sure we say we are returning a set, even if the
1543 * function itself doesn't return sets.
1546 *isDone = ExprMultipleResult;
1550 else if (rsinfo.returnMode == SFRM_Materialize)
1552 /* check we're on the same page as the function author */
1553 if (rsinfo.isDone != ExprSingleResult)
1555 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1556 errmsg("table-function protocol for materialize mode was not followed")));
1557 if (rsinfo.setResult != NULL)
1559 /* prepare to return values from the tuplestore */
1560 ExecPrepareTuplestoreResult(fcache, econtext,
1563 /* remember whether we had set arguments */
1564 fcache->setHasSetArg = hasSetArg;
1565 /* loop back to top to start returning from tuplestore */
1568 /* if setResult was left null, treat it as empty set */
1569 *isDone = ExprEndResult;
1575 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1576 errmsg("unrecognized table-function returnMode: %d",
1577 (int) rsinfo.returnMode)));
1579 /* Else, done with this argument */
1581 break; /* input not a set, so done */
1583 /* Re-eval args to get the next element of the input set */
1584 argDone = ExecEvalFuncArgs(fcinfo, arguments, econtext);
1586 if (argDone != ExprMultipleResult)
1588 /* End of argument set, so we're done. */
1590 *isDone = ExprEndResult;
1596 * If we reach here, loop around to run the function on the new
1604 * Non-set case: much easier.
1606 * We change the ExprState function pointer to use the simpler
1607 * ExecMakeFunctionResultNoSets on subsequent calls. This amounts to
1608 * assuming that no argument can return a set if it didn't do so the
1611 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResultNoSets;
1614 *isDone = ExprSingleResult;
1617 * If function is strict, and there are any NULL arguments, skip
1618 * calling the function and return NULL.
1620 if (fcache->func.fn_strict)
1622 for (i = 0; i < fcinfo->nargs; i++)
1624 if (fcinfo->argnull[i])
1632 pgstat_init_function_usage(fcinfo, &fcusage);
1634 fcinfo->isnull = false;
1635 result = FunctionCallInvoke(fcinfo);
1636 *isNull = fcinfo->isnull;
1638 pgstat_end_function_usage(&fcusage, true);
1645 * ExecMakeFunctionResultNoSets
1647 * Simplified version of ExecMakeFunctionResult that can only handle
1648 * non-set cases. Hand-tuned for speed.
1651 ExecMakeFunctionResultNoSets(FuncExprState *fcache,
1652 ExprContext *econtext,
1654 ExprDoneCond *isDone)
1658 FunctionCallInfoData fcinfo;
1659 PgStat_FunctionCallUsage fcusage;
1662 /* Guard against stack overflow due to overly complex expressions */
1663 check_stack_depth();
1666 *isDone = ExprSingleResult;
1668 /* inlined, simplified version of ExecEvalFuncArgs */
1670 foreach(arg, fcache->args)
1672 ExprState *argstate = (ExprState *) lfirst(arg);
1674 fcinfo.arg[i] = ExecEvalExpr(argstate,
1681 InitFunctionCallInfoData(fcinfo, &(fcache->func), i, NULL, NULL);
1684 * If function is strict, and there are any NULL arguments, skip calling
1685 * the function and return NULL.
1687 if (fcache->func.fn_strict)
1691 if (fcinfo.argnull[i])
1699 pgstat_init_function_usage(&fcinfo, &fcusage);
1701 /* fcinfo.isnull = false; */ /* handled by InitFunctionCallInfoData */
1702 result = FunctionCallInvoke(&fcinfo);
1703 *isNull = fcinfo.isnull;
1705 pgstat_end_function_usage(&fcusage, true);
1712 * ExecMakeTableFunctionResult
1714 * Evaluate a table function, producing a materialized result in a Tuplestore
1718 ExecMakeTableFunctionResult(ExprState *funcexpr,
1719 ExprContext *econtext,
1720 TupleDesc expectedDesc,
1723 Tuplestorestate *tupstore = NULL;
1724 TupleDesc tupdesc = NULL;
1727 bool returnsSet = false;
1728 FunctionCallInfoData fcinfo;
1729 PgStat_FunctionCallUsage fcusage;
1730 ReturnSetInfo rsinfo;
1731 HeapTupleData tmptup;
1732 MemoryContext callerContext;
1733 MemoryContext oldcontext;
1734 bool direct_function_call;
1735 bool first_time = true;
1737 callerContext = CurrentMemoryContext;
1739 funcrettype = exprType((Node *) funcexpr->expr);
1741 returnsTuple = type_is_rowtype(funcrettype);
1744 * Prepare a resultinfo node for communication. We always do this even if
1745 * not expecting a set result, so that we can pass expectedDesc. In the
1746 * generic-expression case, the expression doesn't actually get to see the
1747 * resultinfo, but set it up anyway because we use some of the fields as
1748 * our own state variables.
1750 InitFunctionCallInfoData(fcinfo, NULL, 0, NULL, (Node *) &rsinfo);
1751 rsinfo.type = T_ReturnSetInfo;
1752 rsinfo.econtext = econtext;
1753 rsinfo.expectedDesc = expectedDesc;
1754 rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize | SFRM_Materialize_Preferred);
1756 rsinfo.allowedModes |= (int) SFRM_Materialize_Random;
1757 rsinfo.returnMode = SFRM_ValuePerCall;
1758 /* isDone is filled below */
1759 rsinfo.setResult = NULL;
1760 rsinfo.setDesc = NULL;
1763 * Normally the passed expression tree will be a FuncExprState, since the
1764 * grammar only allows a function call at the top level of a table
1765 * function reference. However, if the function doesn't return set then
1766 * the planner might have replaced the function call via constant-folding
1767 * or inlining. So if we see any other kind of expression node, execute
1768 * it via the general ExecEvalExpr() code; the only difference is that we
1769 * don't get a chance to pass a special ReturnSetInfo to any functions
1770 * buried in the expression.
1772 if (funcexpr && IsA(funcexpr, FuncExprState) &&
1773 IsA(funcexpr->expr, FuncExpr))
1775 FuncExprState *fcache = (FuncExprState *) funcexpr;
1776 ExprDoneCond argDone;
1779 * This path is similar to ExecMakeFunctionResult.
1781 direct_function_call = true;
1784 * Initialize function cache if first time through
1786 if (fcache->func.fn_oid == InvalidOid)
1788 FuncExpr *func = (FuncExpr *) fcache->xprstate.expr;
1790 init_fcache(func->funcid, fcache,
1791 econtext->ecxt_per_query_memory, false);
1793 returnsSet = fcache->func.fn_retset;
1796 * Evaluate the function's argument list.
1798 * Note: ideally, we'd do this in the per-tuple context, but then the
1799 * argument values would disappear when we reset the context in the
1800 * inner loop. So do it in caller context. Perhaps we should make a
1801 * separate context just to hold the evaluated arguments?
1803 fcinfo.flinfo = &(fcache->func);
1804 argDone = ExecEvalFuncArgs(&fcinfo, fcache->args, econtext);
1805 /* We don't allow sets in the arguments of the table function */
1806 if (argDone != ExprSingleResult)
1808 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1809 errmsg("set-valued function called in context that cannot accept a set")));
1812 * If function is strict, and there are any NULL arguments, skip
1813 * calling the function and act like it returned NULL (or an empty
1814 * set, in the returns-set case).
1816 if (fcache->func.fn_strict)
1820 for (i = 0; i < fcinfo.nargs; i++)
1822 if (fcinfo.argnull[i])
1823 goto no_function_result;
1829 /* Treat funcexpr as a generic expression */
1830 direct_function_call = false;
1834 * Switch to short-lived context for calling the function or expression.
1836 MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1839 * Loop to handle the ValuePerCall protocol (which is also the same
1840 * behavior needed in the generic ExecEvalExpr path).
1846 CHECK_FOR_INTERRUPTS();
1849 * reset per-tuple memory context before each call of the function or
1850 * expression. This cleans up any local memory the function may leak
1853 ResetExprContext(econtext);
1855 /* Call the function or expression one time */
1856 if (direct_function_call)
1858 pgstat_init_function_usage(&fcinfo, &fcusage);
1860 fcinfo.isnull = false;
1861 rsinfo.isDone = ExprSingleResult;
1862 result = FunctionCallInvoke(&fcinfo);
1864 pgstat_end_function_usage(&fcusage,
1865 rsinfo.isDone != ExprMultipleResult);
1869 result = ExecEvalExpr(funcexpr, econtext,
1870 &fcinfo.isnull, &rsinfo.isDone);
1873 /* Which protocol does function want to use? */
1874 if (rsinfo.returnMode == SFRM_ValuePerCall)
1877 * Check for end of result set.
1879 if (rsinfo.isDone == ExprEndResult)
1883 * Can't do anything very useful with NULL rowtype values. For a
1884 * function returning set, we consider this a protocol violation
1885 * (but another alternative would be to just ignore the result and
1886 * "continue" to get another row). For a function not returning
1887 * set, we fall out of the loop; we'll cons up an all-nulls result
1890 if (returnsTuple && fcinfo.isnull)
1895 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1896 errmsg("function returning set of rows cannot return null value")));
1900 * If first time through, build tupdesc and tuplestore for result
1904 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1908 * Use the type info embedded in the rowtype Datum to look
1909 * up the needed tupdesc. Make a copy for the query.
1913 td = DatumGetHeapTupleHeader(result);
1914 tupdesc = lookup_rowtype_tupdesc_copy(HeapTupleHeaderGetTypeId(td),
1915 HeapTupleHeaderGetTypMod(td));
1920 * Scalar type, so make a single-column descriptor
1922 tupdesc = CreateTemplateTupleDesc(1, false);
1923 TupleDescInitEntry(tupdesc,
1930 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1931 MemoryContextSwitchTo(oldcontext);
1932 rsinfo.setResult = tupstore;
1933 rsinfo.setDesc = tupdesc;
1937 * Store current resultset item.
1943 td = DatumGetHeapTupleHeader(result);
1946 * tuplestore_puttuple needs a HeapTuple not a bare
1947 * HeapTupleHeader, but it doesn't need all the fields.
1949 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1952 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1953 tuplestore_puttuple(tupstore, &tmptup);
1957 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1958 tuplestore_putvalues(tupstore, tupdesc, &result, &fcinfo.isnull);
1960 MemoryContextSwitchTo(oldcontext);
1965 if (rsinfo.isDone != ExprMultipleResult)
1968 else if (rsinfo.returnMode == SFRM_Materialize)
1970 /* check we're on the same page as the function author */
1971 if (!first_time || rsinfo.isDone != ExprSingleResult)
1973 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1974 errmsg("table-function protocol for materialize mode was not followed")));
1975 /* Done evaluating the set result */
1980 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
1981 errmsg("unrecognized table-function returnMode: %d",
1982 (int) rsinfo.returnMode)));
1990 * If we got nothing from the function (ie, an empty-set or NULL result),
1991 * we have to create the tuplestore to return, and if it's a
1992 * non-set-returning function then insert a single all-nulls row.
1994 if (rsinfo.setResult == NULL)
1996 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1997 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1998 rsinfo.setResult = tupstore;
2001 int natts = expectedDesc->natts;
2005 MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
2006 nulldatums = (Datum *) palloc0(natts * sizeof(Datum));
2007 nullflags = (bool *) palloc(natts * sizeof(bool));
2008 memset(nullflags, true, natts * sizeof(bool));
2009 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
2010 tuplestore_putvalues(tupstore, expectedDesc, nulldatums, nullflags);
2015 * If function provided a tupdesc, cross-check it. We only really
2016 * need to do this for functions returning RECORD, but might as well
2021 tupledesc_match(expectedDesc, rsinfo.setDesc);
2024 * If it is a dynamically-allocated TupleDesc, free it: it is
2025 * typically allocated in a per-query context, so we must avoid
2026 * leaking it across multiple usages.
2028 if (rsinfo.setDesc->tdrefcount == -1)
2029 FreeTupleDesc(rsinfo.setDesc);
2032 MemoryContextSwitchTo(callerContext);
2034 /* All done, pass back the tuplestore */
2035 return rsinfo.setResult;
2039 /* ----------------------------------------------------------------
2043 * Evaluate the functional result of a list of arguments by calling the
2045 * ----------------------------------------------------------------
2048 /* ----------------------------------------------------------------
2050 * ----------------------------------------------------------------
2053 ExecEvalFunc(FuncExprState *fcache,
2054 ExprContext *econtext,
2056 ExprDoneCond *isDone)
2058 /* This is called only the first time through */
2059 FuncExpr *func = (FuncExpr *) fcache->xprstate.expr;
2061 /* Initialize function lookup info */
2062 init_fcache(func->funcid, fcache, econtext->ecxt_per_query_memory, true);
2064 /* Go directly to ExecMakeFunctionResult on subsequent uses */
2065 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
2067 return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
2070 /* ----------------------------------------------------------------
2072 * ----------------------------------------------------------------
2075 ExecEvalOper(FuncExprState *fcache,
2076 ExprContext *econtext,
2078 ExprDoneCond *isDone)
2080 /* This is called only the first time through */
2081 OpExpr *op = (OpExpr *) fcache->xprstate.expr;
2083 /* Initialize function lookup info */
2084 init_fcache(op->opfuncid, fcache, econtext->ecxt_per_query_memory, true);
2086 /* Go directly to ExecMakeFunctionResult on subsequent uses */
2087 fcache->xprstate.evalfunc = (ExprStateEvalFunc) ExecMakeFunctionResult;
2089 return ExecMakeFunctionResult(fcache, econtext, isNull, isDone);
2092 /* ----------------------------------------------------------------
2095 * IS DISTINCT FROM must evaluate arguments to determine whether
2096 * they are NULL; if either is NULL then the result is already
2097 * known. If neither is NULL, then proceed to evaluate the
2098 * function. Note that this is *always* derived from the equals
2099 * operator, but since we need special processing of the arguments
2100 * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
2101 * ----------------------------------------------------------------
2104 ExecEvalDistinct(FuncExprState *fcache,
2105 ExprContext *econtext,
2107 ExprDoneCond *isDone)
2110 FunctionCallInfoData fcinfo;
2111 ExprDoneCond argDone;
2114 /* Set default values for result flags: non-null, not a set result */
2117 *isDone = ExprSingleResult;
2120 * Initialize function cache if first time through
2122 if (fcache->func.fn_oid == InvalidOid)
2124 DistinctExpr *op = (DistinctExpr *) fcache->xprstate.expr;
2126 init_fcache(op->opfuncid, fcache,
2127 econtext->ecxt_per_query_memory, true);
2128 Assert(!fcache->func.fn_retset);
2132 * extract info from fcache
2134 argList = fcache->args;
2136 /* Need to prep callinfo structure */
2137 InitFunctionCallInfoData(fcinfo, &(fcache->func), 0, NULL, NULL);
2138 argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
2139 if (argDone != ExprSingleResult)
2141 (errcode(ERRCODE_DATATYPE_MISMATCH),
2142 errmsg("IS DISTINCT FROM does not support set arguments")));
2143 Assert(fcinfo.nargs == 2);
2145 if (fcinfo.argnull[0] && fcinfo.argnull[1])
2147 /* Both NULL? Then is not distinct... */
2148 result = BoolGetDatum(FALSE);
2150 else if (fcinfo.argnull[0] || fcinfo.argnull[1])
2152 /* Only one is NULL? Then is distinct... */
2153 result = BoolGetDatum(TRUE);
2157 fcinfo.isnull = false;
2158 result = FunctionCallInvoke(&fcinfo);
2159 *isNull = fcinfo.isnull;
2160 /* Must invert result of "=" */
2161 result = BoolGetDatum(!DatumGetBool(result));
2168 * ExecEvalScalarArrayOp
2170 * Evaluate "scalar op ANY/ALL (array)". The operator always yields boolean,
2171 * and we combine the results across all array elements using OR and AND
2172 * (for ANY and ALL respectively). Of course we short-circuit as soon as
2173 * the result is known.
2176 ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
2177 ExprContext *econtext,
2178 bool *isNull, ExprDoneCond *isDone)
2180 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) sstate->fxprstate.xprstate.expr;
2181 bool useOr = opexpr->useOr;
2186 FunctionCallInfoData fcinfo;
2187 ExprDoneCond argDone;
2196 /* Set default values for result flags: non-null, not a set result */
2199 *isDone = ExprSingleResult;
2202 * Initialize function cache if first time through
2204 if (sstate->fxprstate.func.fn_oid == InvalidOid)
2206 init_fcache(opexpr->opfuncid, &sstate->fxprstate,
2207 econtext->ecxt_per_query_memory, true);
2208 Assert(!sstate->fxprstate.func.fn_retset);
2211 /* Need to prep callinfo structure */
2212 InitFunctionCallInfoData(fcinfo, &(sstate->fxprstate.func), 0, NULL, NULL);
2213 argDone = ExecEvalFuncArgs(&fcinfo, sstate->fxprstate.args, econtext);
2214 if (argDone != ExprSingleResult)
2216 (errcode(ERRCODE_DATATYPE_MISMATCH),
2217 errmsg("op ANY/ALL (array) does not support set arguments")));
2218 Assert(fcinfo.nargs == 2);
2221 * If the array is NULL then we return NULL --- it's not very meaningful
2222 * to do anything else, even if the operator isn't strict.
2224 if (fcinfo.argnull[1])
2229 /* Else okay to fetch and detoast the array */
2230 arr = DatumGetArrayTypeP(fcinfo.arg[1]);
2233 * If the array is empty, we return either FALSE or TRUE per the useOr
2234 * flag. This is correct even if the scalar is NULL; since we would
2235 * evaluate the operator zero times, it matters not whether it would want
2238 nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
2240 return BoolGetDatum(!useOr);
2243 * If the scalar is NULL, and the function is strict, return NULL; no
2244 * point in iterating the loop.
2246 if (fcinfo.argnull[0] && sstate->fxprstate.func.fn_strict)
2253 * We arrange to look up info about the element type only once per series
2254 * of calls, assuming the element type doesn't change underneath us.
2256 if (sstate->element_type != ARR_ELEMTYPE(arr))
2258 get_typlenbyvalalign(ARR_ELEMTYPE(arr),
2262 sstate->element_type = ARR_ELEMTYPE(arr);
2264 typlen = sstate->typlen;
2265 typbyval = sstate->typbyval;
2266 typalign = sstate->typalign;
2268 result = BoolGetDatum(!useOr);
2271 /* Loop over the array elements */
2272 s = (char *) ARR_DATA_PTR(arr);
2273 bitmap = ARR_NULLBITMAP(arr);
2276 for (i = 0; i < nitems; i++)
2281 /* Get array element, checking for NULL */
2282 if (bitmap && (*bitmap & bitmask) == 0)
2284 fcinfo.arg[1] = (Datum) 0;
2285 fcinfo.argnull[1] = true;
2289 elt = fetch_att(s, typbyval, typlen);
2290 s = att_addlength_pointer(s, typlen, s);
2291 s = (char *) att_align_nominal(s, typalign);
2292 fcinfo.arg[1] = elt;
2293 fcinfo.argnull[1] = false;
2296 /* Call comparison function */
2297 if (fcinfo.argnull[1] && sstate->fxprstate.func.fn_strict)
2299 fcinfo.isnull = true;
2300 thisresult = (Datum) 0;
2304 fcinfo.isnull = false;
2305 thisresult = FunctionCallInvoke(&fcinfo);
2308 /* Combine results per OR or AND semantics */
2313 if (DatumGetBool(thisresult))
2315 result = BoolGetDatum(true);
2317 break; /* needn't look at any more elements */
2322 if (!DatumGetBool(thisresult))
2324 result = BoolGetDatum(false);
2326 break; /* needn't look at any more elements */
2330 /* advance bitmap pointer if any */
2334 if (bitmask == 0x100)
2342 *isNull = resultnull;
2346 /* ----------------------------------------------------------------
2351 * Evaluate boolean expressions, with appropriate short-circuiting.
2353 * The query planner reformulates clause expressions in the
2354 * qualification to conjunctive normal form. If we ever get
2355 * an AND to evaluate, we can be sure that it's not a top-level
2356 * clause in the qualification, but appears lower (as a function
2357 * argument, for example), or in the target list. Not that you
2358 * need to know this, mind you...
2359 * ----------------------------------------------------------------
2362 ExecEvalNot(BoolExprState *notclause, ExprContext *econtext,
2363 bool *isNull, ExprDoneCond *isDone)
2365 ExprState *clause = linitial(notclause->args);
2369 *isDone = ExprSingleResult;
2371 expr_value = ExecEvalExpr(clause, econtext, isNull, NULL);
2374 * if the expression evaluates to null, then we just cascade the null back
2375 * to whoever called us.
2381 * evaluation of 'not' is simple.. expr is false, then return 'true' and
2384 return BoolGetDatum(!DatumGetBool(expr_value));
2387 /* ----------------------------------------------------------------
2389 * ----------------------------------------------------------------
2392 ExecEvalOr(BoolExprState *orExpr, ExprContext *econtext,
2393 bool *isNull, ExprDoneCond *isDone)
2395 List *clauses = orExpr->args;
2400 *isDone = ExprSingleResult;
2405 * If any of the clauses is TRUE, the OR result is TRUE regardless of the
2406 * states of the rest of the clauses, so we can stop evaluating and return
2407 * TRUE immediately. If none are TRUE and one or more is NULL, we return
2408 * NULL; otherwise we return FALSE. This makes sense when you interpret
2409 * NULL as "don't know": if we have a TRUE then the OR is TRUE even if we
2410 * aren't sure about some of the other inputs. If all the known inputs are
2411 * FALSE, but we have one or more "don't knows", then we have to report
2412 * that we "don't know" what the OR's result should be --- perhaps one of
2413 * the "don't knows" would have been TRUE if we'd known its value. Only
2414 * when all the inputs are known to be FALSE can we state confidently that
2415 * the OR's result is FALSE.
2417 foreach(clause, clauses)
2419 ExprState *clausestate = (ExprState *) lfirst(clause);
2422 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
2425 * if we have a non-null true result, then return it.
2428 AnyNull = true; /* remember we got a null */
2429 else if (DatumGetBool(clause_value))
2430 return clause_value;
2433 /* AnyNull is true if at least one clause evaluated to NULL */
2435 return BoolGetDatum(false);
2438 /* ----------------------------------------------------------------
2440 * ----------------------------------------------------------------
2443 ExecEvalAnd(BoolExprState *andExpr, ExprContext *econtext,
2444 bool *isNull, ExprDoneCond *isDone)
2446 List *clauses = andExpr->args;
2451 *isDone = ExprSingleResult;
2456 * If any of the clauses is FALSE, the AND result is FALSE regardless of
2457 * the states of the rest of the clauses, so we can stop evaluating and
2458 * return FALSE immediately. If none are FALSE and one or more is NULL,
2459 * we return NULL; otherwise we return TRUE. This makes sense when you
2460 * interpret NULL as "don't know", using the same sort of reasoning as for
2464 foreach(clause, clauses)
2466 ExprState *clausestate = (ExprState *) lfirst(clause);
2469 clause_value = ExecEvalExpr(clausestate, econtext, isNull, NULL);
2472 * if we have a non-null false result, then return it.
2475 AnyNull = true; /* remember we got a null */
2476 else if (!DatumGetBool(clause_value))
2477 return clause_value;
2480 /* AnyNull is true if at least one clause evaluated to NULL */
2482 return BoolGetDatum(!AnyNull);
2485 /* ----------------------------------------------------------------
2486 * ExecEvalConvertRowtype
2488 * Evaluate a rowtype coercion operation. This may require
2489 * rearranging field positions.
2490 * ----------------------------------------------------------------
2493 ExecEvalConvertRowtype(ConvertRowtypeExprState *cstate,
2494 ExprContext *econtext,
2495 bool *isNull, ExprDoneCond *isDone)
2497 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) cstate->xprstate.expr;
2500 HeapTupleHeader tuple;
2501 HeapTupleData tmptup;
2502 AttrNumber *attrMap;
2510 tupDatum = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
2512 /* this test covers the isDone exception too: */
2516 tuple = DatumGetHeapTupleHeader(tupDatum);
2518 /* Lookup tupdescs if first time through or after rescan */
2519 if (cstate->indesc == NULL)
2520 get_cached_rowtype(exprType((Node *) convert->arg), -1,
2521 &cstate->indesc, econtext);
2522 if (cstate->outdesc == NULL)
2523 get_cached_rowtype(convert->resulttype, -1,
2524 &cstate->outdesc, econtext);
2526 Assert(HeapTupleHeaderGetTypeId(tuple) == cstate->indesc->tdtypeid);
2527 Assert(HeapTupleHeaderGetTypMod(tuple) == cstate->indesc->tdtypmod);
2529 /* if first time through, initialize */
2530 if (cstate->attrMap == NULL)
2532 MemoryContext old_cxt;
2535 /* allocate state in long-lived memory context */
2536 old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
2538 /* prepare map from old to new attribute numbers */
2539 n = cstate->outdesc->natts;
2540 cstate->attrMap = (AttrNumber *) palloc0(n * sizeof(AttrNumber));
2541 for (i = 0; i < n; i++)
2543 Form_pg_attribute att = cstate->outdesc->attrs[i];
2549 if (att->attisdropped)
2550 continue; /* attrMap[i] is already 0 */
2551 attname = NameStr(att->attname);
2552 atttypid = att->atttypid;
2553 atttypmod = att->atttypmod;
2554 for (j = 0; j < cstate->indesc->natts; j++)
2556 att = cstate->indesc->attrs[j];
2557 if (att->attisdropped)
2559 if (strcmp(attname, NameStr(att->attname)) == 0)
2561 /* Found it, check type */
2562 if (atttypid != att->atttypid || atttypmod != att->atttypmod)
2563 elog(ERROR, "attribute \"%s\" of type %s does not match corresponding attribute of type %s",
2565 format_type_be(cstate->indesc->tdtypeid),
2566 format_type_be(cstate->outdesc->tdtypeid));
2567 cstate->attrMap[i] = (AttrNumber) (j + 1);
2571 if (cstate->attrMap[i] == 0)
2572 elog(ERROR, "attribute \"%s\" of type %s does not exist",
2574 format_type_be(cstate->indesc->tdtypeid));
2576 /* preallocate workspace for Datum arrays */
2577 n = cstate->indesc->natts + 1; /* +1 for NULL */
2578 cstate->invalues = (Datum *) palloc(n * sizeof(Datum));
2579 cstate->inisnull = (bool *) palloc(n * sizeof(bool));
2580 n = cstate->outdesc->natts;
2581 cstate->outvalues = (Datum *) palloc(n * sizeof(Datum));
2582 cstate->outisnull = (bool *) palloc(n * sizeof(bool));
2584 MemoryContextSwitchTo(old_cxt);
2587 attrMap = cstate->attrMap;
2588 invalues = cstate->invalues;
2589 inisnull = cstate->inisnull;
2590 outvalues = cstate->outvalues;
2591 outisnull = cstate->outisnull;
2592 outnatts = cstate->outdesc->natts;
2595 * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader.
2597 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
2598 tmptup.t_data = tuple;
2601 * Extract all the values of the old tuple, offsetting the arrays so that
2602 * invalues[0] is NULL and invalues[1] is the first source attribute; this
2603 * exactly matches the numbering convention in attrMap.
2605 heap_deform_tuple(&tmptup, cstate->indesc, invalues + 1, inisnull + 1);
2606 invalues[0] = (Datum) 0;
2610 * Transpose into proper fields of the new tuple.
2612 for (i = 0; i < outnatts; i++)
2616 outvalues[i] = invalues[j];
2617 outisnull[i] = inisnull[j];
2621 * Now form the new tuple.
2623 result = heap_form_tuple(cstate->outdesc, outvalues, outisnull);
2625 return HeapTupleGetDatum(result);
2628 /* ----------------------------------------------------------------
2631 * Evaluate a CASE clause. Will have boolean expressions
2632 * inside the WHEN clauses, and will have expressions
2634 * - thomas 1998-11-09
2635 * ----------------------------------------------------------------
2638 ExecEvalCase(CaseExprState *caseExpr, ExprContext *econtext,
2639 bool *isNull, ExprDoneCond *isDone)
2641 List *clauses = caseExpr->args;
2647 *isDone = ExprSingleResult;
2650 * If there's a test expression, we have to evaluate it and save the value
2651 * where the CaseTestExpr placeholders can find it. We must save and
2652 * restore prior setting of econtext's caseValue fields, in case this node
2653 * is itself within a larger CASE.
2655 save_datum = econtext->caseValue_datum;
2656 save_isNull = econtext->caseValue_isNull;
2660 econtext->caseValue_datum = ExecEvalExpr(caseExpr->arg,
2662 &econtext->caseValue_isNull,
2667 * we evaluate each of the WHEN clauses in turn, as soon as one is true we
2668 * return the corresponding result. If none are true then we return the
2669 * value of the default clause, or NULL if there is none.
2671 foreach(clause, clauses)
2673 CaseWhenState *wclause = lfirst(clause);
2676 clause_value = ExecEvalExpr(wclause->expr,
2682 * if we have a true test, then we return the result, since the case
2683 * statement is satisfied. A NULL result from the test is not
2686 if (DatumGetBool(clause_value) && !*isNull)
2688 econtext->caseValue_datum = save_datum;
2689 econtext->caseValue_isNull = save_isNull;
2690 return ExecEvalExpr(wclause->result,
2697 econtext->caseValue_datum = save_datum;
2698 econtext->caseValue_isNull = save_isNull;
2700 if (caseExpr->defresult)
2702 return ExecEvalExpr(caseExpr->defresult,
2713 * ExecEvalCaseTestExpr
2715 * Return the value stored by CASE.
2718 ExecEvalCaseTestExpr(ExprState *exprstate,
2719 ExprContext *econtext,
2720 bool *isNull, ExprDoneCond *isDone)
2723 *isDone = ExprSingleResult;
2724 *isNull = econtext->caseValue_isNull;
2725 return econtext->caseValue_datum;
2728 /* ----------------------------------------------------------------
2729 * ExecEvalArray - ARRAY[] expressions
2730 * ----------------------------------------------------------------
2733 ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
2734 bool *isNull, ExprDoneCond *isDone)
2736 ArrayExpr *arrayExpr = (ArrayExpr *) astate->xprstate.expr;
2739 Oid element_type = arrayExpr->element_typeid;
2744 /* Set default values for result flags: non-null, not a set result */
2747 *isDone = ExprSingleResult;
2749 if (!arrayExpr->multidims)
2751 /* Elements are presumably of scalar type */
2758 nelems = list_length(astate->elements);
2760 /* Shouldn't happen here, but if length is 0, return empty array */
2762 return PointerGetDatum(construct_empty_array(element_type));
2764 dvalues = (Datum *) palloc(nelems * sizeof(Datum));
2765 dnulls = (bool *) palloc(nelems * sizeof(bool));
2767 /* loop through and build array of datums */
2768 foreach(element, astate->elements)
2770 ExprState *e = (ExprState *) lfirst(element);
2772 dvalues[i] = ExecEvalExpr(e, econtext, &dnulls[i], NULL);
2776 /* setup for 1-D array of the given length */
2780 result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
2788 /* Must be nested array expressions */
2791 int outer_nelems = 0;
2793 int *elem_dims = NULL;
2794 int *elem_lbs = NULL;
2795 bool firstone = true;
2796 bool havenulls = false;
2797 bool haveempty = false;
2807 i = list_length(astate->elements);
2808 subdata = (char **) palloc(i * sizeof(char *));
2809 subbitmaps = (bits8 **) palloc(i * sizeof(bits8 *));
2810 subbytes = (int *) palloc(i * sizeof(int));
2811 subnitems = (int *) palloc(i * sizeof(int));
2813 /* loop through and get data area from each element */
2814 foreach(element, astate->elements)
2816 ExprState *e = (ExprState *) lfirst(element);
2822 arraydatum = ExecEvalExpr(e, econtext, &eisnull, NULL);
2823 /* temporarily ignore null subarrays */
2830 array = DatumGetArrayTypeP(arraydatum);
2832 /* run-time double-check on element type */
2833 if (element_type != ARR_ELEMTYPE(array))
2835 (errcode(ERRCODE_DATATYPE_MISMATCH),
2836 errmsg("cannot merge incompatible arrays"),
2837 errdetail("Array with element type %s cannot be "
2838 "included in ARRAY construct with element type %s.",
2839 format_type_be(ARR_ELEMTYPE(array)),
2840 format_type_be(element_type))));
2842 this_ndims = ARR_NDIM(array);
2843 /* temporarily ignore zero-dimensional subarrays */
2844 if (this_ndims <= 0)
2852 /* Get sub-array details from first member */
2853 elem_ndims = this_ndims;
2854 ndims = elem_ndims + 1;
2855 if (ndims <= 0 || ndims > MAXDIM)
2857 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2858 errmsg("number of array dimensions (%d) exceeds " \
2859 "the maximum allowed (%d)", ndims, MAXDIM)));
2861 elem_dims = (int *) palloc(elem_ndims * sizeof(int));
2862 memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
2863 elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
2864 memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
2870 /* Check other sub-arrays are compatible */
2871 if (elem_ndims != this_ndims ||
2872 memcmp(elem_dims, ARR_DIMS(array),
2873 elem_ndims * sizeof(int)) != 0 ||
2874 memcmp(elem_lbs, ARR_LBOUND(array),
2875 elem_ndims * sizeof(int)) != 0)
2877 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2878 errmsg("multidimensional arrays must have array "
2879 "expressions with matching dimensions")));
2882 subdata[outer_nelems] = ARR_DATA_PTR(array);
2883 subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
2884 subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
2885 nbytes += subbytes[outer_nelems];
2886 subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
2888 nitems += subnitems[outer_nelems];
2889 havenulls |= ARR_HASNULL(array);
2894 * If all items were null or empty arrays, return an empty array;
2895 * otherwise, if some were and some weren't, raise error. (Note: we
2896 * must special-case this somehow to avoid trying to generate a 1-D
2897 * array formed from empty arrays. It's not ideal...)
2901 if (ndims == 0) /* didn't find any nonempty array */
2902 return PointerGetDatum(construct_empty_array(element_type));
2904 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2905 errmsg("multidimensional arrays must have array "
2906 "expressions with matching dimensions")));
2909 /* setup for multi-D array */
2910 dims[0] = outer_nelems;
2912 for (i = 1; i < ndims; i++)
2914 dims[i] = elem_dims[i - 1];
2915 lbs[i] = elem_lbs[i - 1];
2920 dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
2921 nbytes += dataoffset;
2925 dataoffset = 0; /* marker for no null bitmap */
2926 nbytes += ARR_OVERHEAD_NONULLS(ndims);
2929 result = (ArrayType *) palloc(nbytes);
2930 SET_VARSIZE(result, nbytes);
2931 result->ndim = ndims;
2932 result->dataoffset = dataoffset;
2933 result->elemtype = element_type;
2934 memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
2935 memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
2937 dat = ARR_DATA_PTR(result);
2939 for (i = 0; i < outer_nelems; i++)
2941 memcpy(dat, subdata[i], subbytes[i]);
2944 array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
2947 iitem += subnitems[i];
2951 return PointerGetDatum(result);
2954 /* ----------------------------------------------------------------
2955 * ExecEvalRow - ROW() expressions
2956 * ----------------------------------------------------------------
2959 ExecEvalRow(RowExprState *rstate,
2960 ExprContext *econtext,
2961 bool *isNull, ExprDoneCond *isDone)
2970 /* Set default values for result flags: non-null, not a set result */
2973 *isDone = ExprSingleResult;
2975 /* Allocate workspace */
2976 natts = rstate->tupdesc->natts;
2977 values = (Datum *) palloc0(natts * sizeof(Datum));
2978 isnull = (bool *) palloc(natts * sizeof(bool));
2980 /* preset to nulls in case rowtype has some later-added columns */
2981 memset(isnull, true, natts * sizeof(bool));
2983 /* Evaluate field values */
2985 foreach(arg, rstate->args)
2987 ExprState *e = (ExprState *) lfirst(arg);
2989 values[i] = ExecEvalExpr(e, econtext, &isnull[i], NULL);
2993 tuple = heap_form_tuple(rstate->tupdesc, values, isnull);
2998 return HeapTupleGetDatum(tuple);
3001 /* ----------------------------------------------------------------
3002 * ExecEvalRowCompare - ROW() comparison-op ROW()
3003 * ----------------------------------------------------------------
3006 ExecEvalRowCompare(RowCompareExprState *rstate,
3007 ExprContext *econtext,
3008 bool *isNull, ExprDoneCond *isDone)
3011 RowCompareType rctype = ((RowCompareExpr *) rstate->xprstate.expr)->rctype;
3012 int32 cmpresult = 0;
3018 *isDone = ExprSingleResult;
3019 *isNull = true; /* until we get a result */
3022 forboth(l, rstate->largs, r, rstate->rargs)
3024 ExprState *le = (ExprState *) lfirst(l);
3025 ExprState *re = (ExprState *) lfirst(r);
3026 FunctionCallInfoData locfcinfo;
3028 InitFunctionCallInfoData(locfcinfo, &(rstate->funcs[i]), 2,
3030 locfcinfo.arg[0] = ExecEvalExpr(le, econtext,
3031 &locfcinfo.argnull[0], NULL);
3032 locfcinfo.arg[1] = ExecEvalExpr(re, econtext,
3033 &locfcinfo.argnull[1], NULL);
3034 if (rstate->funcs[i].fn_strict &&
3035 (locfcinfo.argnull[0] || locfcinfo.argnull[1]))
3036 return (Datum) 0; /* force NULL result */
3037 locfcinfo.isnull = false;
3038 cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo));
3039 if (locfcinfo.isnull)
3040 return (Datum) 0; /* force NULL result */
3042 break; /* no need to compare remaining columns */
3048 /* EQ and NE cases aren't allowed here */
3050 result = (cmpresult < 0);
3053 result = (cmpresult <= 0);
3056 result = (cmpresult >= 0);
3059 result = (cmpresult > 0);
3062 elog(ERROR, "unrecognized RowCompareType: %d", (int) rctype);
3063 result = 0; /* keep compiler quiet */
3068 return BoolGetDatum(result);
3071 /* ----------------------------------------------------------------
3073 * ----------------------------------------------------------------
3076 ExecEvalCoalesce(CoalesceExprState *coalesceExpr, ExprContext *econtext,
3077 bool *isNull, ExprDoneCond *isDone)
3082 *isDone = ExprSingleResult;
3084 /* Simply loop through until something NOT NULL is found */
3085 foreach(arg, coalesceExpr->args)
3087 ExprState *e = (ExprState *) lfirst(arg);
3090 value = ExecEvalExpr(e, econtext, isNull, NULL);
3095 /* Else return NULL */
3100 /* ----------------------------------------------------------------
3102 * ----------------------------------------------------------------
3105 ExecEvalMinMax(MinMaxExprState *minmaxExpr, ExprContext *econtext,
3106 bool *isNull, ExprDoneCond *isDone)
3108 Datum result = (Datum) 0;
3109 MinMaxOp op = ((MinMaxExpr *) minmaxExpr->xprstate.expr)->op;
3110 FunctionCallInfoData locfcinfo;
3114 *isDone = ExprSingleResult;
3115 *isNull = true; /* until we get a result */
3117 InitFunctionCallInfoData(locfcinfo, &minmaxExpr->cfunc, 2, NULL, NULL);
3118 locfcinfo.argnull[0] = false;
3119 locfcinfo.argnull[1] = false;
3121 foreach(arg, minmaxExpr->args)
3123 ExprState *e = (ExprState *) lfirst(arg);
3128 value = ExecEvalExpr(e, econtext, &valueIsNull, NULL);
3130 continue; /* ignore NULL inputs */
3134 /* first nonnull input, adopt value */
3140 /* apply comparison function */
3141 locfcinfo.arg[0] = result;
3142 locfcinfo.arg[1] = value;
3143 locfcinfo.isnull = false;
3144 cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo));
3145 if (locfcinfo.isnull) /* probably should not happen */
3147 if (cmpresult > 0 && op == IS_LEAST)
3149 else if (cmpresult < 0 && op == IS_GREATEST)
3157 /* ----------------------------------------------------------------
3159 * ----------------------------------------------------------------
3162 ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
3163 bool *isNull, ExprDoneCond *isDone)
3165 XmlExpr *xexpr = (XmlExpr *) xmlExpr->xprstate.expr;
3172 *isDone = ExprSingleResult;
3173 *isNull = true; /* until we get a result */
3181 foreach(arg, xmlExpr->args)
3183 ExprState *e = (ExprState *) lfirst(arg);
3185 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3187 values = lappend(values, DatumGetPointer(value));
3190 if (list_length(values) > 0)
3193 return PointerGetDatum(xmlconcat(values));
3204 initStringInfo(&buf);
3205 forboth(arg, xmlExpr->named_args, narg, xexpr->arg_names)
3207 ExprState *e = (ExprState *) lfirst(arg);
3208 char *argname = strVal(lfirst(narg));
3210 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3213 appendStringInfo(&buf, "<%s>%s</%s>",
3215 map_sql_value_to_xml_value(value, exprType((Node *) e->expr)),
3230 result = cstring_to_text_with_len(buf.data, buf.len);
3233 return PointerGetDatum(result);
3240 return PointerGetDatum(xmlelement(xmlExpr, econtext));
3247 bool preserve_whitespace;
3249 /* arguments are known to be text, bool */
3250 Assert(list_length(xmlExpr->args) == 2);
3252 e = (ExprState *) linitial(xmlExpr->args);
3253 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3256 data = DatumGetTextP(value);
3258 e = (ExprState *) lsecond(xmlExpr->args);
3259 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3260 if (isnull) /* probably can't happen */
3262 preserve_whitespace = DatumGetBool(value);
3266 return PointerGetDatum(xmlparse(data,
3268 preserve_whitespace));
3277 /* optional argument is known to be text */
3278 Assert(list_length(xmlExpr->args) <= 1);
3282 e = (ExprState *) linitial(xmlExpr->args);
3283 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3287 arg = DatumGetTextP(value);
3295 return PointerGetDatum(xmlpi(xexpr->name, arg, isnull, isNull));
3306 /* arguments are known to be xml, text, int */
3307 Assert(list_length(xmlExpr->args) == 3);
3309 e = (ExprState *) linitial(xmlExpr->args);
3310 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3313 data = DatumGetXmlP(value);
3315 e = (ExprState *) lsecond(xmlExpr->args);
3316 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3320 version = DatumGetTextP(value);
3322 e = (ExprState *) lthird(xmlExpr->args);
3323 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3324 standalone = DatumGetInt32(value);
3328 return PointerGetDatum(xmlroot(data,
3334 case IS_XMLSERIALIZE:
3338 /* argument type is known to be xml */
3339 Assert(list_length(xmlExpr->args) == 1);
3341 e = (ExprState *) linitial(xmlExpr->args);
3342 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3348 return PointerGetDatum(xmltotext_with_xmloption(DatumGetXmlP(value), xexpr->xmloption));
3356 /* optional argument is known to be xml */
3357 Assert(list_length(xmlExpr->args) == 1);
3359 e = (ExprState *) linitial(xmlExpr->args);
3360 value = ExecEvalExpr(e, econtext, &isnull, NULL);
3366 return BoolGetDatum(xml_is_document(DatumGetXmlP(value)));
3372 elog(ERROR, "unrecognized XML operation");
3376 /* ----------------------------------------------------------------
3379 * Note that this is *always* derived from the equals operator,
3380 * but since we need special processing of the arguments
3381 * we can not simply reuse ExecEvalOper() or ExecEvalFunc().
3382 * ----------------------------------------------------------------
3385 ExecEvalNullIf(FuncExprState *nullIfExpr,
3386 ExprContext *econtext,
3387 bool *isNull, ExprDoneCond *isDone)
3390 FunctionCallInfoData fcinfo;
3391 ExprDoneCond argDone;
3395 *isDone = ExprSingleResult;
3398 * Initialize function cache if first time through
3400 if (nullIfExpr->func.fn_oid == InvalidOid)
3402 NullIfExpr *op = (NullIfExpr *) nullIfExpr->xprstate.expr;
3404 init_fcache(op->opfuncid, nullIfExpr,
3405 econtext->ecxt_per_query_memory, true);
3406 Assert(!nullIfExpr->func.fn_retset);
3410 * extract info from nullIfExpr
3412 argList = nullIfExpr->args;
3414 /* Need to prep callinfo structure */
3415 InitFunctionCallInfoData(fcinfo, &(nullIfExpr->func), 0, NULL, NULL);
3416 argDone = ExecEvalFuncArgs(&fcinfo, argList, econtext);
3417 if (argDone != ExprSingleResult)
3419 (errcode(ERRCODE_DATATYPE_MISMATCH),
3420 errmsg("NULLIF does not support set arguments")));
3421 Assert(fcinfo.nargs == 2);
3423 /* if either argument is NULL they can't be equal */
3424 if (!fcinfo.argnull[0] && !fcinfo.argnull[1])
3426 fcinfo.isnull = false;
3427 result = FunctionCallInvoke(&fcinfo);
3428 /* if the arguments are equal return null */
3429 if (!fcinfo.isnull && DatumGetBool(result))
3436 /* else return first argument */
3437 *isNull = fcinfo.argnull[0];
3438 return fcinfo.arg[0];
3441 /* ----------------------------------------------------------------
3444 * Evaluate a NullTest node.
3445 * ----------------------------------------------------------------
3448 ExecEvalNullTest(NullTestState *nstate,
3449 ExprContext *econtext,
3451 ExprDoneCond *isDone)
3453 NullTest *ntest = (NullTest *) nstate->xprstate.expr;
3456 result = ExecEvalExpr(nstate->arg, econtext, isNull, isDone);
3458 if (isDone && *isDone == ExprEndResult)
3459 return result; /* nothing to check */
3461 if (nstate->argisrow && !(*isNull))
3463 HeapTupleHeader tuple;
3467 HeapTupleData tmptup;
3470 tuple = DatumGetHeapTupleHeader(result);
3472 tupType = HeapTupleHeaderGetTypeId(tuple);
3473 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3475 /* Lookup tupdesc if first time through or if type changes */
3476 tupDesc = get_cached_rowtype(tupType, tupTypmod,
3477 &nstate->argdesc, econtext);
3480 * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
3482 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3483 tmptup.t_data = tuple;
3485 for (att = 1; att <= tupDesc->natts; att++)
3487 /* ignore dropped columns */
3488 if (tupDesc->attrs[att - 1]->attisdropped)
3490 if (heap_attisnull(&tmptup, att))
3492 /* null field disproves IS NOT NULL */
3493 if (ntest->nulltesttype == IS_NOT_NULL)
3494 return BoolGetDatum(false);
3498 /* non-null field disproves IS NULL */
3499 if (ntest->nulltesttype == IS_NULL)
3500 return BoolGetDatum(false);
3504 return BoolGetDatum(true);
3508 /* Simple scalar-argument case, or a null rowtype datum */
3509 switch (ntest->nulltesttype)
3515 return BoolGetDatum(true);
3518 return BoolGetDatum(false);
3523 return BoolGetDatum(false);
3526 return BoolGetDatum(true);
3528 elog(ERROR, "unrecognized nulltesttype: %d",
3529 (int) ntest->nulltesttype);
3530 return (Datum) 0; /* keep compiler quiet */
3535 /* ----------------------------------------------------------------
3536 * ExecEvalBooleanTest
3538 * Evaluate a BooleanTest node.
3539 * ----------------------------------------------------------------
3542 ExecEvalBooleanTest(GenericExprState *bstate,
3543 ExprContext *econtext,
3545 ExprDoneCond *isDone)
3547 BooleanTest *btest = (BooleanTest *) bstate->xprstate.expr;
3550 result = ExecEvalExpr(bstate->arg, econtext, isNull, isDone);
3552 if (isDone && *isDone == ExprEndResult)
3553 return result; /* nothing to check */
3555 switch (btest->booltesttype)
3561 return BoolGetDatum(false);
3563 else if (DatumGetBool(result))
3564 return BoolGetDatum(true);
3566 return BoolGetDatum(false);
3571 return BoolGetDatum(true);
3573 else if (DatumGetBool(result))
3574 return BoolGetDatum(false);
3576 return BoolGetDatum(true);
3581 return BoolGetDatum(false);
3583 else if (DatumGetBool(result))
3584 return BoolGetDatum(false);
3586 return BoolGetDatum(true);
3591 return BoolGetDatum(true);
3593 else if (DatumGetBool(result))
3594 return BoolGetDatum(true);
3596 return BoolGetDatum(false);
3601 return BoolGetDatum(true);
3604 return BoolGetDatum(false);
3605 case IS_NOT_UNKNOWN:
3609 return BoolGetDatum(false);
3612 return BoolGetDatum(true);
3614 elog(ERROR, "unrecognized booltesttype: %d",
3615 (int) btest->booltesttype);
3616 return (Datum) 0; /* keep compiler quiet */
3621 * ExecEvalCoerceToDomain
3623 * Test the provided data against the domain constraint(s). If the data
3624 * passes the constraint specifications, pass it through (return the
3625 * datum) otherwise throw an error.
3628 ExecEvalCoerceToDomain(CoerceToDomainState *cstate, ExprContext *econtext,
3629 bool *isNull, ExprDoneCond *isDone)
3631 CoerceToDomain *ctest = (CoerceToDomain *) cstate->xprstate.expr;
3635 result = ExecEvalExpr(cstate->arg, econtext, isNull, isDone);
3637 if (isDone && *isDone == ExprEndResult)
3638 return result; /* nothing to check */
3640 foreach(l, cstate->constraints)
3642 DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
3644 switch (con->constrainttype)
3646 case DOM_CONSTRAINT_NOTNULL:
3649 (errcode(ERRCODE_NOT_NULL_VIOLATION),
3650 errmsg("domain %s does not allow null values",
3651 format_type_be(ctest->resulttype))));
3653 case DOM_CONSTRAINT_CHECK:
3661 * Set up value to be returned by CoerceToDomainValue
3662 * nodes. We must save and restore prior setting of
3663 * econtext's domainValue fields, in case this node is
3664 * itself within a check expression for another domain.
3666 save_datum = econtext->domainValue_datum;
3667 save_isNull = econtext->domainValue_isNull;
3669 econtext->domainValue_datum = result;
3670 econtext->domainValue_isNull = *isNull;
3672 conResult = ExecEvalExpr(con->check_expr,
3673 econtext, &conIsNull, NULL);
3676 !DatumGetBool(conResult))
3678 (errcode(ERRCODE_CHECK_VIOLATION),
3679 errmsg("value for domain %s violates check constraint \"%s\"",
3680 format_type_be(ctest->resulttype),
3682 econtext->domainValue_datum = save_datum;
3683 econtext->domainValue_isNull = save_isNull;
3688 elog(ERROR, "unrecognized constraint type: %d",
3689 (int) con->constrainttype);
3694 /* If all has gone well (constraints did not fail) return the datum */
3699 * ExecEvalCoerceToDomainValue
3701 * Return the value stored by CoerceToDomain.
3704 ExecEvalCoerceToDomainValue(ExprState *exprstate,
3705 ExprContext *econtext,
3706 bool *isNull, ExprDoneCond *isDone)
3709 *isDone = ExprSingleResult;
3710 *isNull = econtext->domainValue_isNull;
3711 return econtext->domainValue_datum;
3714 /* ----------------------------------------------------------------
3715 * ExecEvalFieldSelect
3717 * Evaluate a FieldSelect node.
3718 * ----------------------------------------------------------------
3721 ExecEvalFieldSelect(FieldSelectState *fstate,
3722 ExprContext *econtext,
3724 ExprDoneCond *isDone)
3726 FieldSelect *fselect = (FieldSelect *) fstate->xprstate.expr;
3727 AttrNumber fieldnum = fselect->fieldnum;
3730 HeapTupleHeader tuple;
3734 Form_pg_attribute attr;
3735 HeapTupleData tmptup;
3737 tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
3739 /* this test covers the isDone exception too: */
3743 tuple = DatumGetHeapTupleHeader(tupDatum);
3745 tupType = HeapTupleHeaderGetTypeId(tuple);
3746 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3748 /* Lookup tupdesc if first time through or if type changes */
3749 tupDesc = get_cached_rowtype(tupType, tupTypmod,
3750 &fstate->argdesc, econtext);
3752 /* Check for dropped column, and force a NULL result if so */
3753 if (fieldnum <= 0 ||
3754 fieldnum > tupDesc->natts) /* should never happen */
3755 elog(ERROR, "attribute number %d exceeds number of columns %d",
3756 fieldnum, tupDesc->natts);
3757 attr = tupDesc->attrs[fieldnum - 1];
3758 if (attr->attisdropped)
3764 /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3765 /* As in ExecEvalVar, we should but can't check typmod */
3766 if (fselect->resulttype != attr->atttypid)
3768 (errmsg("attribute %d has wrong type", fieldnum),
3769 errdetail("Table has type %s, but query expects %s.",
3770 format_type_be(attr->atttypid),
3771 format_type_be(fselect->resulttype))));
3774 * heap_getattr needs a HeapTuple not a bare HeapTupleHeader. We set all
3775 * the fields in the struct just in case user tries to inspect system
3778 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3779 ItemPointerSetInvalid(&(tmptup.t_self));
3780 tmptup.t_tableOid = InvalidOid;
3781 tmptup.t_data = tuple;
3783 result = heap_getattr(&tmptup,
3790 /* ----------------------------------------------------------------
3791 * ExecEvalFieldStore
3793 * Evaluate a FieldStore node.
3794 * ----------------------------------------------------------------
3797 ExecEvalFieldStore(FieldStoreState *fstate,
3798 ExprContext *econtext,
3800 ExprDoneCond *isDone)
3802 FieldStore *fstore = (FieldStore *) fstate->xprstate.expr;
3813 tupDatum = ExecEvalExpr(fstate->arg, econtext, isNull, isDone);
3815 if (isDone && *isDone == ExprEndResult)
3818 /* Lookup tupdesc if first time through or after rescan */
3819 tupDesc = get_cached_rowtype(fstore->resulttype, -1,
3820 &fstate->argdesc, econtext);
3822 /* Allocate workspace */
3823 values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
3824 isnull = (bool *) palloc(tupDesc->natts * sizeof(bool));
3829 * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We
3830 * set all the fields in the struct just in case.
3832 HeapTupleHeader tuphdr;
3833 HeapTupleData tmptup;
3835 tuphdr = DatumGetHeapTupleHeader(tupDatum);
3836 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
3837 ItemPointerSetInvalid(&(tmptup.t_self));
3838 tmptup.t_tableOid = InvalidOid;
3839 tmptup.t_data = tuphdr;
3841 heap_deform_tuple(&tmptup, tupDesc, values, isnull);
3845 /* Convert null input tuple into an all-nulls row */
3846 memset(isnull, true, tupDesc->natts * sizeof(bool));
3849 /* Result is never null */
3852 save_datum = econtext->caseValue_datum;
3853 save_isNull = econtext->caseValue_isNull;
3855 forboth(l1, fstate->newvals, l2, fstore->fieldnums)
3857 ExprState *newval = (ExprState *) lfirst(l1);
3858 AttrNumber fieldnum = lfirst_int(l2);
3860 Assert(fieldnum > 0 && fieldnum <= tupDesc->natts);
3863 * Use the CaseTestExpr mechanism to pass down the old value of the
3864 * field being replaced; this is useful in case we have a nested field
3865 * update situation. It's safe to reuse the CASE mechanism because
3866 * there cannot be a CASE between here and where the value would be
3869 econtext->caseValue_datum = values[fieldnum - 1];
3870 econtext->caseValue_isNull = isnull[fieldnum - 1];
3872 values[fieldnum - 1] = ExecEvalExpr(newval,
3874 &isnull[fieldnum - 1],
3878 econtext->caseValue_datum = save_datum;
3879 econtext->caseValue_isNull = save_isNull;
3881 tuple = heap_form_tuple(tupDesc, values, isnull);
3886 return HeapTupleGetDatum(tuple);
3889 /* ----------------------------------------------------------------
3890 * ExecEvalRelabelType
3892 * Evaluate a RelabelType node.
3893 * ----------------------------------------------------------------
3896 ExecEvalRelabelType(GenericExprState *exprstate,
3897 ExprContext *econtext,
3898 bool *isNull, ExprDoneCond *isDone)
3900 return ExecEvalExpr(exprstate->arg, econtext, isNull, isDone);
3903 /* ----------------------------------------------------------------
3904 * ExecEvalCoerceViaIO
3906 * Evaluate a CoerceViaIO node.
3907 * ----------------------------------------------------------------
3910 ExecEvalCoerceViaIO(CoerceViaIOState *iostate,
3911 ExprContext *econtext,
3912 bool *isNull, ExprDoneCond *isDone)
3918 inputval = ExecEvalExpr(iostate->arg, econtext, isNull, isDone);
3920 if (isDone && *isDone == ExprEndResult)
3921 return inputval; /* nothing to do */
3924 string = NULL; /* output functions are not called on nulls */
3926 string = OutputFunctionCall(&iostate->outfunc, inputval);
3928 result = InputFunctionCall(&iostate->infunc,
3930 iostate->intypioparam,
3933 /* The input function cannot change the null/not-null status */
3937 /* ----------------------------------------------------------------
3938 * ExecEvalArrayCoerceExpr
3940 * Evaluate an ArrayCoerceExpr node.
3941 * ----------------------------------------------------------------
3944 ExecEvalArrayCoerceExpr(ArrayCoerceExprState *astate,
3945 ExprContext *econtext,
3946 bool *isNull, ExprDoneCond *isDone)
3948 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) astate->xprstate.expr;
3951 FunctionCallInfoData locfcinfo;
3953 result = ExecEvalExpr(astate->arg, econtext, isNull, isDone);
3955 if (isDone && *isDone == ExprEndResult)
3956 return result; /* nothing to do */
3958 return result; /* nothing to do */
3961 * If it's binary-compatible, modify the element type in the array header,
3962 * but otherwise leave the array as we received it.
3964 if (!OidIsValid(acoerce->elemfuncid))
3966 /* Detoast input array if necessary, and copy in any case */
3967 array = DatumGetArrayTypePCopy(result);
3968 ARR_ELEMTYPE(array) = astate->resultelemtype;
3969 PG_RETURN_ARRAYTYPE_P(array);
3972 /* Detoast input array if necessary, but don't make a useless copy */
3973 array = DatumGetArrayTypeP(result);
3975 /* Initialize function cache if first time through */
3976 if (astate->elemfunc.fn_oid == InvalidOid)
3978 AclResult aclresult;
3980 /* Check permission to call function */
3981 aclresult = pg_proc_aclcheck(acoerce->elemfuncid, GetUserId(),
3983 if (aclresult != ACLCHECK_OK)
3984 aclcheck_error(aclresult, ACL_KIND_PROC,
3985 get_func_name(acoerce->elemfuncid));
3987 /* Set up the primary fmgr lookup information */
3988 fmgr_info_cxt(acoerce->elemfuncid, &(astate->elemfunc),
3989 econtext->ecxt_per_query_memory);
3991 /* Initialize additional info */
3992 astate->elemfunc.fn_expr = (Node *) acoerce;
3996 * Use array_map to apply the function to each array element.
3998 * We pass on the desttypmod and isExplicit flags whether or not the
3999 * function wants them.
4001 InitFunctionCallInfoData(locfcinfo, &(astate->elemfunc), 3,
4003 locfcinfo.arg[0] = PointerGetDatum(array);
4004 locfcinfo.arg[1] = Int32GetDatum(acoerce->resulttypmod);
4005 locfcinfo.arg[2] = BoolGetDatum(acoerce->isExplicit);
4006 locfcinfo.argnull[0] = false;
4007 locfcinfo.argnull[1] = false;
4008 locfcinfo.argnull[2] = false;
4010 return array_map(&locfcinfo, ARR_ELEMTYPE(array), astate->resultelemtype,
4014 /* ----------------------------------------------------------------
4015 * ExecEvalCurrentOfExpr
4017 * The planner must convert CURRENT OF into a TidScan qualification.
4018 * So, we have to be able to do ExecInitExpr on a CurrentOfExpr,
4019 * but we shouldn't ever actually execute it.
4020 * ----------------------------------------------------------------
4023 ExecEvalCurrentOfExpr(ExprState *exprstate, ExprContext *econtext,
4024 bool *isNull, ExprDoneCond *isDone)
4026 elog(ERROR, "CURRENT OF cannot be executed");
4027 return 0; /* keep compiler quiet */
4032 * ExecEvalExprSwitchContext
4034 * Same as ExecEvalExpr, but get into the right allocation context explicitly.
4037 ExecEvalExprSwitchContext(ExprState *expression,
4038 ExprContext *econtext,
4040 ExprDoneCond *isDone)
4043 MemoryContext oldContext;
4045 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4046 retDatum = ExecEvalExpr(expression, econtext, isNull, isDone);
4047 MemoryContextSwitchTo(oldContext);
4053 * ExecInitExpr: prepare an expression tree for execution
4055 * This function builds and returns an ExprState tree paralleling the given
4056 * Expr node tree. The ExprState tree can then be handed to ExecEvalExpr
4057 * for execution. Because the Expr tree itself is read-only as far as
4058 * ExecInitExpr and ExecEvalExpr are concerned, several different executions
4059 * of the same plan tree can occur concurrently.
4061 * This must be called in a memory context that will last as long as repeated
4062 * executions of the expression are needed. Typically the context will be
4063 * the same as the per-query context of the associated ExprContext.
4065 * Any Aggref and SubPlan nodes found in the tree are added to the lists
4066 * of such nodes held by the parent PlanState. Otherwise, we do very little
4067 * initialization here other than building the state-node tree. Any nontrivial
4068 * work associated with initializing runtime info for a node should happen
4069 * during the first actual evaluation of that node. (This policy lets us
4070 * avoid work if the node is never actually evaluated.)
4072 * Note: there is no ExecEndExpr function; we assume that any resource
4073 * cleanup needed will be handled by just releasing the memory context
4074 * in which the state tree is built. Functions that require additional
4075 * cleanup work can register a shutdown callback in the ExprContext.
4077 * 'node' is the root of the expression tree to examine
4078 * 'parent' is the PlanState node that owns the expression.
4080 * 'parent' may be NULL if we are preparing an expression that is not
4081 * associated with a plan tree. (If so, it can't have aggs or subplans.)
4082 * This case should usually come through ExecPrepareExpr, not directly here.
4085 ExecInitExpr(Expr *node, PlanState *parent)
4092 /* Guard against stack overflow due to overly complex expressions */
4093 check_stack_depth();
4095 switch (nodeTag(node))
4098 state = (ExprState *) makeNode(ExprState);
4099 state->evalfunc = ExecEvalVar;
4102 state = (ExprState *) makeNode(ExprState);
4103 state->evalfunc = ExecEvalConst;
4106 state = (ExprState *) makeNode(ExprState);
4107 state->evalfunc = ExecEvalParam;
4109 case T_CoerceToDomainValue:
4110 state = (ExprState *) makeNode(ExprState);
4111 state->evalfunc = ExecEvalCoerceToDomainValue;
4113 case T_CaseTestExpr:
4114 state = (ExprState *) makeNode(ExprState);
4115 state->evalfunc = ExecEvalCaseTestExpr;
4119 Aggref *aggref = (Aggref *) node;
4120 AggrefExprState *astate = makeNode(AggrefExprState);
4122 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAggref;
4123 if (parent && IsA(parent, AggState))
4125 AggState *aggstate = (AggState *) parent;
4128 aggstate->aggs = lcons(astate, aggstate->aggs);
4129 naggs = ++aggstate->numaggs;
4131 astate->args = (List *) ExecInitExpr((Expr *) aggref->args,
4135 * Complain if the aggregate's arguments contain any
4136 * aggregates; nested agg functions are semantically
4137 * nonsensical. (This should have been caught earlier,
4138 * but we defend against it here anyway.)
4140 if (naggs != aggstate->numaggs)
4142 (errcode(ERRCODE_GROUPING_ERROR),
4143 errmsg("aggregate function calls cannot be nested")));
4147 /* planner messed up */
4148 elog(ERROR, "aggref found in non-Agg plan node");
4150 state = (ExprState *) astate;
4155 ArrayRef *aref = (ArrayRef *) node;
4156 ArrayRefExprState *astate = makeNode(ArrayRefExprState);
4158 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArrayRef;
4159 astate->refupperindexpr = (List *)
4160 ExecInitExpr((Expr *) aref->refupperindexpr, parent);
4161 astate->reflowerindexpr = (List *)
4162 ExecInitExpr((Expr *) aref->reflowerindexpr, parent);
4163 astate->refexpr = ExecInitExpr(aref->refexpr, parent);
4164 astate->refassgnexpr = ExecInitExpr(aref->refassgnexpr,
4166 /* do one-time catalog lookups for type info */
4167 astate->refattrlength = get_typlen(aref->refarraytype);
4168 get_typlenbyvalalign(aref->refelemtype,
4169 &astate->refelemlength,
4170 &astate->refelembyval,
4171 &astate->refelemalign);
4172 state = (ExprState *) astate;
4177 FuncExpr *funcexpr = (FuncExpr *) node;
4178 FuncExprState *fstate = makeNode(FuncExprState);
4180 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFunc;
4181 fstate->args = (List *)
4182 ExecInitExpr((Expr *) funcexpr->args, parent);
4183 fstate->func.fn_oid = InvalidOid; /* not initialized */
4184 state = (ExprState *) fstate;
4189 OpExpr *opexpr = (OpExpr *) node;
4190 FuncExprState *fstate = makeNode(FuncExprState);
4192 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOper;
4193 fstate->args = (List *)
4194 ExecInitExpr((Expr *) opexpr->args, parent);
4195 fstate->func.fn_oid = InvalidOid; /* not initialized */
4196 state = (ExprState *) fstate;
4199 case T_DistinctExpr:
4201 DistinctExpr *distinctexpr = (DistinctExpr *) node;
4202 FuncExprState *fstate = makeNode(FuncExprState);
4204 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalDistinct;
4205 fstate->args = (List *)
4206 ExecInitExpr((Expr *) distinctexpr->args, parent);
4207 fstate->func.fn_oid = InvalidOid; /* not initialized */
4208 state = (ExprState *) fstate;
4211 case T_ScalarArrayOpExpr:
4213 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
4214 ScalarArrayOpExprState *sstate = makeNode(ScalarArrayOpExprState);
4216 sstate->fxprstate.xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalScalarArrayOp;
4217 sstate->fxprstate.args = (List *)
4218 ExecInitExpr((Expr *) opexpr->args, parent);
4219 sstate->fxprstate.func.fn_oid = InvalidOid; /* not initialized */
4220 sstate->element_type = InvalidOid; /* ditto */
4221 state = (ExprState *) sstate;
4226 BoolExpr *boolexpr = (BoolExpr *) node;
4227 BoolExprState *bstate = makeNode(BoolExprState);
4229 switch (boolexpr->boolop)
4232 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalAnd;
4235 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalOr;
4238 bstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNot;
4241 elog(ERROR, "unrecognized boolop: %d",
4242 (int) boolexpr->boolop);
4245 bstate->args = (List *)
4246 ExecInitExpr((Expr *) boolexpr->args, parent);
4247 state = (ExprState *) bstate;
4252 SubPlan *subplan = (SubPlan *) node;
4253 SubPlanState *sstate;
4256 elog(ERROR, "SubPlan found with no parent plan");
4258 sstate = ExecInitSubPlan(subplan, parent);
4260 /* Add SubPlanState nodes to parent->subPlan */
4261 parent->subPlan = lcons(sstate, parent->subPlan);
4263 state = (ExprState *) sstate;
4266 case T_AlternativeSubPlan:
4268 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
4269 AlternativeSubPlanState *asstate;
4272 elog(ERROR, "AlternativeSubPlan found with no parent plan");
4274 asstate = ExecInitAlternativeSubPlan(asplan, parent);
4276 state = (ExprState *) asstate;
4281 FieldSelect *fselect = (FieldSelect *) node;
4282 FieldSelectState *fstate = makeNode(FieldSelectState);
4284 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldSelect;
4285 fstate->arg = ExecInitExpr(fselect->arg, parent);
4286 fstate->argdesc = NULL;
4287 state = (ExprState *) fstate;
4292 FieldStore *fstore = (FieldStore *) node;
4293 FieldStoreState *fstate = makeNode(FieldStoreState);
4295 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalFieldStore;
4296 fstate->arg = ExecInitExpr(fstore->arg, parent);
4297 fstate->newvals = (List *) ExecInitExpr((Expr *) fstore->newvals, parent);
4298 fstate->argdesc = NULL;
4299 state = (ExprState *) fstate;
4304 RelabelType *relabel = (RelabelType *) node;
4305 GenericExprState *gstate = makeNode(GenericExprState);
4307 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRelabelType;
4308 gstate->arg = ExecInitExpr(relabel->arg, parent);
4309 state = (ExprState *) gstate;
4314 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
4315 CoerceViaIOState *iostate = makeNode(CoerceViaIOState);
4319 iostate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoerceViaIO;
4320 iostate->arg = ExecInitExpr(iocoerce->arg, parent);
4321 /* lookup the result type's input function */
4322 getTypeInputInfo(iocoerce->resulttype, &iofunc,
4323 &iostate->intypioparam);
4324 fmgr_info(iofunc, &iostate->infunc);
4325 /* lookup the input type's output function */
4326 getTypeOutputInfo(exprType((Node *) iocoerce->arg),
4327 &iofunc, &typisvarlena);
4328 fmgr_info(iofunc, &iostate->outfunc);
4329 state = (ExprState *) iostate;
4332 case T_ArrayCoerceExpr:
4334 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
4335 ArrayCoerceExprState *astate = makeNode(ArrayCoerceExprState);
4337 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArrayCoerceExpr;
4338 astate->arg = ExecInitExpr(acoerce->arg, parent);
4339 astate->resultelemtype = get_element_type(acoerce->resulttype);
4340 if (astate->resultelemtype == InvalidOid)
4342 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4343 errmsg("target type is not an array")));
4344 /* Arrays over domains aren't supported yet */
4345 Assert(getBaseType(astate->resultelemtype) ==
4346 astate->resultelemtype);
4347 astate->elemfunc.fn_oid = InvalidOid; /* not initialized */
4348 astate->amstate = (ArrayMapState *) palloc0(sizeof(ArrayMapState));
4349 state = (ExprState *) astate;
4352 case T_ConvertRowtypeExpr:
4354 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
4355 ConvertRowtypeExprState *cstate = makeNode(ConvertRowtypeExprState);
4357 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalConvertRowtype;
4358 cstate->arg = ExecInitExpr(convert->arg, parent);
4359 state = (ExprState *) cstate;
4364 CaseExpr *caseexpr = (CaseExpr *) node;
4365 CaseExprState *cstate = makeNode(CaseExprState);
4366 List *outlist = NIL;
4369 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCase;
4370 cstate->arg = ExecInitExpr(caseexpr->arg, parent);
4371 foreach(l, caseexpr->args)
4373 CaseWhen *when = (CaseWhen *) lfirst(l);
4374 CaseWhenState *wstate = makeNode(CaseWhenState);
4376 Assert(IsA(when, CaseWhen));
4377 wstate->xprstate.evalfunc = NULL; /* not used */
4378 wstate->xprstate.expr = (Expr *) when;
4379 wstate->expr = ExecInitExpr(when->expr, parent);
4380 wstate->result = ExecInitExpr(when->result, parent);
4381 outlist = lappend(outlist, wstate);
4383 cstate->args = outlist;
4384 cstate->defresult = ExecInitExpr(caseexpr->defresult, parent);
4385 state = (ExprState *) cstate;
4390 ArrayExpr *arrayexpr = (ArrayExpr *) node;
4391 ArrayExprState *astate = makeNode(ArrayExprState);
4392 List *outlist = NIL;
4395 astate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalArray;
4396 foreach(l, arrayexpr->elements)
4398 Expr *e = (Expr *) lfirst(l);
4401 estate = ExecInitExpr(e, parent);
4402 outlist = lappend(outlist, estate);
4404 astate->elements = outlist;
4405 /* do one-time catalog lookup for type info */
4406 get_typlenbyvalalign(arrayexpr->element_typeid,
4407 &astate->elemlength,
4409 &astate->elemalign);
4410 state = (ExprState *) astate;
4415 RowExpr *rowexpr = (RowExpr *) node;
4416 RowExprState *rstate = makeNode(RowExprState);
4417 Form_pg_attribute *attrs;
4418 List *outlist = NIL;
4422 rstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRow;
4423 /* Build tupdesc to describe result tuples */
4424 if (rowexpr->row_typeid == RECORDOID)
4426 /* generic record, use runtime type assignment */
4427 rstate->tupdesc = ExecTypeFromExprList(rowexpr->args);
4428 BlessTupleDesc(rstate->tupdesc);
4429 /* we won't need to redo this at runtime */
4433 /* it's been cast to a named type, use that */
4434 rstate->tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
4436 /* Set up evaluation, skipping any deleted columns */
4437 Assert(list_length(rowexpr->args) <= rstate->tupdesc->natts);
4438 attrs = rstate->tupdesc->attrs;
4440 foreach(l, rowexpr->args)
4442 Expr *e = (Expr *) lfirst(l);
4445 if (!attrs[i]->attisdropped)
4448 * Guard against ALTER COLUMN TYPE on rowtype since
4449 * the RowExpr was created. XXX should we check
4450 * typmod too? Not sure we can be sure it'll be the
4453 if (exprType((Node *) e) != attrs[i]->atttypid)
4455 (errcode(ERRCODE_DATATYPE_MISMATCH),
4456 errmsg("ROW() column has type %s instead of type %s",
4457 format_type_be(exprType((Node *) e)),
4458 format_type_be(attrs[i]->atttypid))));
4463 * Ignore original expression and insert a NULL. We
4464 * don't really care what type of NULL it is, so
4465 * always make an int4 NULL.
4467 e = (Expr *) makeNullConst(INT4OID, -1);
4469 estate = ExecInitExpr(e, parent);
4470 outlist = lappend(outlist, estate);
4473 rstate->args = outlist;
4474 state = (ExprState *) rstate;
4477 case T_RowCompareExpr:
4479 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
4480 RowCompareExprState *rstate = makeNode(RowCompareExprState);
4481 int nopers = list_length(rcexpr->opnos);
4487 rstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalRowCompare;
4488 Assert(list_length(rcexpr->largs) == nopers);
4490 foreach(l, rcexpr->largs)
4492 Expr *e = (Expr *) lfirst(l);
4495 estate = ExecInitExpr(e, parent);
4496 outlist = lappend(outlist, estate);
4498 rstate->largs = outlist;
4499 Assert(list_length(rcexpr->rargs) == nopers);
4501 foreach(l, rcexpr->rargs)
4503 Expr *e = (Expr *) lfirst(l);
4506 estate = ExecInitExpr(e, parent);
4507 outlist = lappend(outlist, estate);
4509 rstate->rargs = outlist;
4510 Assert(list_length(rcexpr->opfamilies) == nopers);
4511 rstate->funcs = (FmgrInfo *) palloc(nopers * sizeof(FmgrInfo));
4513 forboth(l, rcexpr->opnos, l2, rcexpr->opfamilies)
4515 Oid opno = lfirst_oid(l);
4516 Oid opfamily = lfirst_oid(l2);
4522 get_op_opfamily_properties(opno, opfamily,
4526 proc = get_opfamily_proc(opfamily,
4532 * If we enforced permissions checks on index support
4533 * functions, we'd need to make a check here. But the
4534 * index support machinery doesn't do that, and neither
4537 fmgr_info(proc, &(rstate->funcs[i]));
4540 state = (ExprState *) rstate;
4543 case T_CoalesceExpr:
4545 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
4546 CoalesceExprState *cstate = makeNode(CoalesceExprState);
4547 List *outlist = NIL;
4550 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoalesce;
4551 foreach(l, coalesceexpr->args)
4553 Expr *e = (Expr *) lfirst(l);
4556 estate = ExecInitExpr(e, parent);
4557 outlist = lappend(outlist, estate);
4559 cstate->args = outlist;
4560 state = (ExprState *) cstate;
4565 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
4566 MinMaxExprState *mstate = makeNode(MinMaxExprState);
4567 List *outlist = NIL;
4569 TypeCacheEntry *typentry;
4571 mstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalMinMax;
4572 foreach(l, minmaxexpr->args)
4574 Expr *e = (Expr *) lfirst(l);
4577 estate = ExecInitExpr(e, parent);
4578 outlist = lappend(outlist, estate);
4580 mstate->args = outlist;
4581 /* Look up the btree comparison function for the datatype */
4582 typentry = lookup_type_cache(minmaxexpr->minmaxtype,
4583 TYPECACHE_CMP_PROC);
4584 if (!OidIsValid(typentry->cmp_proc))
4586 (errcode(ERRCODE_UNDEFINED_FUNCTION),
4587 errmsg("could not identify a comparison function for type %s",
4588 format_type_be(minmaxexpr->minmaxtype))));
4591 * If we enforced permissions checks on index support
4592 * functions, we'd need to make a check here. But the index
4593 * support machinery doesn't do that, and neither does this
4596 fmgr_info(typentry->cmp_proc, &(mstate->cfunc));
4597 state = (ExprState *) mstate;
4602 XmlExpr *xexpr = (XmlExpr *) node;
4603 XmlExprState *xstate = makeNode(XmlExprState);
4608 xstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalXml;
4609 xstate->named_outfuncs = (FmgrInfo *)
4610 palloc0(list_length(xexpr->named_args) * sizeof(FmgrInfo));
4613 foreach(arg, xexpr->named_args)
4615 Expr *e = (Expr *) lfirst(arg);
4620 estate = ExecInitExpr(e, parent);
4621 outlist = lappend(outlist, estate);
4623 getTypeOutputInfo(exprType((Node *) e),
4624 &typOutFunc, &typIsVarlena);
4625 fmgr_info(typOutFunc, &xstate->named_outfuncs[i]);
4628 xstate->named_args = outlist;
4631 foreach(arg, xexpr->args)
4633 Expr *e = (Expr *) lfirst(arg);
4636 estate = ExecInitExpr(e, parent);
4637 outlist = lappend(outlist, estate);
4639 xstate->args = outlist;
4641 state = (ExprState *) xstate;
4646 NullIfExpr *nullifexpr = (NullIfExpr *) node;
4647 FuncExprState *fstate = makeNode(FuncExprState);
4649 fstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullIf;
4650 fstate->args = (List *)
4651 ExecInitExpr((Expr *) nullifexpr->args, parent);
4652 fstate->func.fn_oid = InvalidOid; /* not initialized */
4653 state = (ExprState *) fstate;
4658 NullTest *ntest = (NullTest *) node;
4659 NullTestState *nstate = makeNode(NullTestState);
4661 nstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalNullTest;
4662 nstate->arg = ExecInitExpr(ntest->arg, parent);
4663 nstate->argisrow = type_is_rowtype(exprType((Node *) ntest->arg));
4664 nstate->argdesc = NULL;
4665 state = (ExprState *) nstate;
4670 BooleanTest *btest = (BooleanTest *) node;
4671 GenericExprState *gstate = makeNode(GenericExprState);
4673 gstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalBooleanTest;
4674 gstate->arg = ExecInitExpr(btest->arg, parent);
4675 state = (ExprState *) gstate;
4678 case T_CoerceToDomain:
4680 CoerceToDomain *ctest = (CoerceToDomain *) node;
4681 CoerceToDomainState *cstate = makeNode(CoerceToDomainState);
4683 cstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalCoerceToDomain;
4684 cstate->arg = ExecInitExpr(ctest->arg, parent);
4685 cstate->constraints = GetDomainConstraints(ctest->resulttype);
4686 state = (ExprState *) cstate;
4689 case T_CurrentOfExpr:
4690 state = (ExprState *) makeNode(ExprState);
4691 state->evalfunc = ExecEvalCurrentOfExpr;
4695 TargetEntry *tle = (TargetEntry *) node;
4696 GenericExprState *gstate = makeNode(GenericExprState);
4698 gstate->xprstate.evalfunc = NULL; /* not used */
4699 gstate->arg = ExecInitExpr(tle->expr, parent);
4700 state = (ExprState *) gstate;
4705 List *outlist = NIL;
4708 foreach(l, (List *) node)
4710 outlist = lappend(outlist,
4711 ExecInitExpr((Expr *) lfirst(l),
4714 /* Don't fall through to the "common" code below */
4715 return (ExprState *) outlist;
4718 elog(ERROR, "unrecognized node type: %d",
4719 (int) nodeTag(node));
4720 state = NULL; /* keep compiler quiet */
4724 /* Common code for all state-node types */
4731 * ExecPrepareExpr --- initialize for expression execution outside a normal
4732 * Plan tree context.
4734 * This differs from ExecInitExpr in that we don't assume the caller is
4735 * already running in the EState's per-query context. Also, we apply
4736 * fix_opfuncids() to the passed expression tree to be sure it is ready
4737 * to run. (In ordinary Plan trees the planner will have fixed opfuncids,
4738 * but callers outside the executor will not have done this.)
4741 ExecPrepareExpr(Expr *node, EState *estate)
4744 MemoryContext oldcontext;
4746 fix_opfuncids((Node *) node);
4748 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4750 result = ExecInitExpr(node, NULL);
4752 MemoryContextSwitchTo(oldcontext);
4758 /* ----------------------------------------------------------------
4759 * ExecQual / ExecTargetList / ExecProject
4760 * ----------------------------------------------------------------
4763 /* ----------------------------------------------------------------
4766 * Evaluates a conjunctive boolean expression (qual list) and
4767 * returns true iff none of the subexpressions are false.
4768 * (We also return true if the list is empty.)
4770 * If some of the subexpressions yield NULL but none yield FALSE,
4771 * then the result of the conjunction is NULL (ie, unknown)
4772 * according to three-valued boolean logic. In this case,
4773 * we return the value specified by the "resultForNull" parameter.
4775 * Callers evaluating WHERE clauses should pass resultForNull=FALSE,
4776 * since SQL specifies that tuples with null WHERE results do not
4777 * get selected. On the other hand, callers evaluating constraint
4778 * conditions should pass resultForNull=TRUE, since SQL also specifies
4779 * that NULL constraint conditions are not failures.
4781 * NOTE: it would not be correct to use this routine to evaluate an
4782 * AND subclause of a boolean expression; for that purpose, a NULL
4783 * result must be returned as NULL so that it can be properly treated
4784 * in the next higher operator (cf. ExecEvalAnd and ExecEvalOr).
4785 * This routine is only used in contexts where a complete expression
4786 * is being evaluated and we know that NULL can be treated the same
4787 * as one boolean result or the other.
4789 * ----------------------------------------------------------------
4792 ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
4795 MemoryContext oldContext;
4801 EV_printf("ExecQual: qual is ");
4802 EV_nodeDisplay(qual);
4808 * Run in short-lived per-tuple context while computing expressions.
4810 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4813 * Evaluate the qual conditions one at a time. If we find a FALSE result,
4814 * we can stop evaluating and return FALSE --- the AND result must be
4815 * FALSE. Also, if we find a NULL result when resultForNull is FALSE, we
4816 * can stop and return FALSE --- the AND result must be FALSE or NULL in
4817 * that case, and the caller doesn't care which.
4819 * If we get to the end of the list, we can return TRUE. This will happen
4820 * when the AND result is indeed TRUE, or when the AND result is NULL (one
4821 * or more NULL subresult, with all the rest TRUE) and the caller has
4822 * specified resultForNull = TRUE.
4828 ExprState *clause = (ExprState *) lfirst(l);
4832 expr_value = ExecEvalExpr(clause, econtext, &isNull, NULL);
4836 if (resultForNull == false)
4838 result = false; /* treat NULL as FALSE */
4844 if (!DatumGetBool(expr_value))
4846 result = false; /* definitely FALSE */
4852 MemoryContextSwitchTo(oldContext);
4858 * Number of items in a tlist (including any resjunk items!)
4861 ExecTargetListLength(List *targetlist)
4863 /* This used to be more complex, but fjoins are dead */
4864 return list_length(targetlist);
4868 * Number of items in a tlist, not including any resjunk items
4871 ExecCleanTargetListLength(List *targetlist)
4876 foreach(tl, targetlist)
4878 TargetEntry *curTle = (TargetEntry *) lfirst(tl);
4880 Assert(IsA(curTle, TargetEntry));
4881 if (!curTle->resjunk)
4889 * Evaluates a targetlist with respect to the given
4890 * expression context. Returns TRUE if we were able to create
4891 * a result, FALSE if we have exhausted a set-valued expression.
4893 * Results are stored into the passed values and isnull arrays.
4894 * The caller must provide an itemIsDone array that persists across calls.
4896 * As with ExecEvalExpr, the caller should pass isDone = NULL if not
4897 * prepared to deal with sets of result tuples. Otherwise, a return
4898 * of *isDone = ExprMultipleResult signifies a set element, and a return
4899 * of *isDone = ExprEndResult signifies end of the set of tuple.
4902 ExecTargetList(List *targetlist,
4903 ExprContext *econtext,
4906 ExprDoneCond *itemIsDone,
4907 ExprDoneCond *isDone)
4909 MemoryContext oldContext;
4914 * Run in short-lived per-tuple context while computing expressions.
4916 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4919 * evaluate all the expressions in the target list
4922 *isDone = ExprSingleResult; /* until proven otherwise */
4924 haveDoneSets = false; /* any exhausted set exprs in tlist? */
4926 foreach(tl, targetlist)
4928 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4929 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4930 AttrNumber resind = tle->resno - 1;
4932 values[resind] = ExecEvalExpr(gstate->arg,
4935 &itemIsDone[resind]);
4937 if (itemIsDone[resind] != ExprSingleResult)
4939 /* We have a set-valued expression in the tlist */
4942 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4943 errmsg("set-valued function called in context that cannot accept a set")));
4944 if (itemIsDone[resind] == ExprMultipleResult)
4946 /* we have undone sets in the tlist, set flag */
4947 *isDone = ExprMultipleResult;
4951 /* we have done sets in the tlist, set flag for that */
4952 haveDoneSets = true;
4960 * note: can't get here unless we verified isDone != NULL
4962 if (*isDone == ExprSingleResult)
4965 * all sets are done, so report that tlist expansion is complete.
4967 *isDone = ExprEndResult;
4968 MemoryContextSwitchTo(oldContext);
4974 * We have some done and some undone sets. Restart the done ones
4975 * so that we can deliver a tuple (if possible).
4977 foreach(tl, targetlist)
4979 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
4980 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
4981 AttrNumber resind = tle->resno - 1;
4983 if (itemIsDone[resind] == ExprEndResult)
4985 values[resind] = ExecEvalExpr(gstate->arg,
4988 &itemIsDone[resind]);
4990 if (itemIsDone[resind] == ExprEndResult)
4993 * Oh dear, this item is returning an empty set. Guess
4994 * we can't make a tuple after all.
4996 *isDone = ExprEndResult;
5003 * If we cannot make a tuple because some sets are empty, we still
5004 * have to cycle the nonempty sets to completion, else resources
5005 * will not be released from subplans etc.
5007 * XXX is that still necessary?
5009 if (*isDone == ExprEndResult)
5011 foreach(tl, targetlist)
5013 GenericExprState *gstate = (GenericExprState *) lfirst(tl);
5014 TargetEntry *tle = (TargetEntry *) gstate->xprstate.expr;
5015 AttrNumber resind = tle->resno - 1;
5017 while (itemIsDone[resind] == ExprMultipleResult)
5019 values[resind] = ExecEvalExpr(gstate->arg,
5022 &itemIsDone[resind]);
5026 MemoryContextSwitchTo(oldContext);
5032 /* Report success */
5033 MemoryContextSwitchTo(oldContext);
5040 * Evaluates a simple-Variable-list projection.
5042 * Results are stored into the passed values and isnull arrays.
5045 ExecVariableList(ProjectionInfo *projInfo,
5049 ExprContext *econtext = projInfo->pi_exprContext;
5050 int *varSlotOffsets = projInfo->pi_varSlotOffsets;
5051 int *varNumbers = projInfo->pi_varNumbers;
5055 * Force extraction of all input values that we need.
5057 if (projInfo->pi_lastInnerVar > 0)
5058 slot_getsomeattrs(econtext->ecxt_innertuple,
5059 projInfo->pi_lastInnerVar);
5060 if (projInfo->pi_lastOuterVar > 0)
5061 slot_getsomeattrs(econtext->ecxt_outertuple,
5062 projInfo->pi_lastOuterVar);
5063 if (projInfo->pi_lastScanVar > 0)
5064 slot_getsomeattrs(econtext->ecxt_scantuple,
5065 projInfo->pi_lastScanVar);
5068 * Assign to result by direct extraction of fields from source slots ... a
5069 * mite ugly, but fast ...
5071 for (i = list_length(projInfo->pi_targetlist) - 1; i >= 0; i--)
5073 char *slotptr = ((char *) econtext) + varSlotOffsets[i];
5074 TupleTableSlot *varSlot = *((TupleTableSlot **) slotptr);
5075 int varNumber = varNumbers[i] - 1;
5077 values[i] = varSlot->tts_values[varNumber];
5078 isnull[i] = varSlot->tts_isnull[varNumber];
5085 * projects a tuple based on projection info and stores
5086 * it in the previously specified tuple table slot.
5088 * Note: the result is always a virtual tuple; therefore it
5089 * may reference the contents of the exprContext's scan tuples
5090 * and/or temporary results constructed in the exprContext.
5091 * If the caller wishes the result to be valid longer than that
5092 * data will be valid, he must call ExecMaterializeSlot on the
5096 ExecProject(ProjectionInfo *projInfo, ExprDoneCond *isDone)
5098 TupleTableSlot *slot;
5103 Assert(projInfo != NULL);
5106 * get the projection info we want
5108 slot = projInfo->pi_slot;
5111 * Clear any former contents of the result slot. This makes it safe for
5112 * us to use the slot's Datum/isnull arrays as workspace. (Also, we can
5113 * return the slot as-is if we decide no rows can be projected.)
5115 ExecClearTuple(slot);
5118 * form a new result tuple (if possible); if successful, mark the result
5119 * slot as containing a valid virtual tuple
5121 if (projInfo->pi_isVarList)
5123 /* simple Var list: this always succeeds with one result row */
5125 *isDone = ExprSingleResult;
5126 ExecVariableList(projInfo,
5129 ExecStoreVirtualTuple(slot);
5133 if (ExecTargetList(projInfo->pi_targetlist,
5134 projInfo->pi_exprContext,
5137 projInfo->pi_itemIsDone,
5139 ExecStoreVirtualTuple(slot);