the result values around.
The last entry in a completed ExprState->steps array is always an
-EEOP_DONE step; this removes the need to test for end-of-array while
-iterating. Also, if the expression contains any variable references (to
-user columns of the ExprContext's INNER, OUTER, or SCAN tuples), the steps
+EEOP_DONE_RETURN or EEOP_DONE_NO_RETURN step; this removes the need to
+test for end-of-array while iterating. The former is used when the
+expression returns a value directly, the latter when side-effects of
+expression initialization are the goal (e.g. for projection or
+aggregate transition value computation).
+
+Also, if the expression contains any variable references (to user
+columns of the ExprContext's INNER, OUTER, or SCAN tuples), the steps
array begins with EEOP_*_FETCHSOME steps that ensure that the relevant
tuples have been deconstructed to make the required columns directly
available (cf. slot_getsomeattrs()). This allows individual Var-fetching
* using ExecInitExpr() et al. This converts the tree into a flat array
* of ExprEvalSteps, which may be thought of as instructions in a program.
* At runtime, we'll execute steps, starting with the first, until we reach
- * an EEOP_DONE opcode.
+ * an EEOP_DONE_{RETURN|NO_RETURN} opcode.
*
* This file contains the "compilation" logic. It is independent of the
* specific execution technology we use (switch statement, computed goto,
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
/* Finally, append a DONE step */
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
/* Finally, append a DONE step */
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
* have yielded TRUE, and since its result is stored in the desired output
* location, we're done.
*/
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
}
}
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_NO_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
}
}
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_NO_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
else
{
/* Not trivial, so append a DONE step */
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(elemstate, &scratch);
/* and ready the subexpression */
ExecReadyExpr(elemstate);
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_NO_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
/* Simple validity checks on expression */
Assert(state->steps_len >= 1);
- Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE);
+ Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE_RETURN ||
+ state->steps[state->steps_len - 1].opcode == EEOP_DONE_NO_RETURN);
/*
* Don't perform redundant initialization. This is unreachable in current
*/
#if defined(EEO_USE_COMPUTED_GOTO)
static const void *const dispatch_table[] = {
- &&CASE_EEOP_DONE,
+ &&CASE_EEOP_DONE_RETURN,
+ &&CASE_EEOP_DONE_NO_RETURN,
&&CASE_EEOP_INNER_FETCHSOME,
&&CASE_EEOP_OUTER_FETCHSOME,
&&CASE_EEOP_SCAN_FETCHSOME,
EEO_SWITCH()
{
- EEO_CASE(EEOP_DONE)
+ EEO_CASE(EEOP_DONE_RETURN)
{
- goto out;
+ *isnull = state->resnull;
+ return state->resvalue;
+ }
+
+ EEO_CASE(EEOP_DONE_NO_RETURN)
+ {
+ Assert(isnull == NULL);
+ return (Datum) 0;
}
EEO_CASE(EEOP_INNER_FETCHSOME)
{
/* unreachable */
Assert(false);
- goto out;
+ goto out_error;
}
}
-out:
- *isnull = state->resnull;
- return state->resvalue;
+out_error:
+ pg_unreachable();
+ return (Datum) 0;
}
/*
static void
advance_aggregates(AggState *aggstate)
{
- bool dummynull;
-
- ExecEvalExprSwitchContext(aggstate->phase->evaltrans,
- aggstate->tmpcontext,
- &dummynull);
+ ExecEvalExprNoReturnSwitchContext(aggstate->phase->evaltrans,
+ aggstate->tmpcontext);
}
/*
switch (opcode)
{
- case EEOP_DONE:
+ case EEOP_DONE_RETURN:
{
LLVMValueRef v_tmpisnull;
LLVMValueRef v_tmpvalue;
break;
}
+ case EEOP_DONE_NO_RETURN:
+ LLVMBuildRet(b, l_sizet_const(0));
+ break;
+
case EEOP_INNER_FETCHSOME:
case EEOP_OUTER_FETCHSOME:
case EEOP_SCAN_FETCHSOME:
*/
typedef enum ExprEvalOp
{
- /* entire expression has been evaluated completely, return */
- EEOP_DONE,
+ /* entire expression has been evaluated, return value */
+ EEOP_DONE_RETURN,
+
+ /* entire expression has been evaluated, no return value */
+ EEOP_DONE_NO_RETURN,
/* apply slot_getsomeattrs on corresponding tuple slot */
EEOP_INNER_FETCHSOME,
}
#endif
+/*
+ * ExecEvalExprNoReturn
+ *
+ * Like ExecEvalExpr(), but for cases where no return value is expected,
+ * because the side-effects of expression evaluation are what's desired. This
+ * is e.g. used for projection and aggregate transition computation.
+
+ * Evaluate expression identified by "state" in the execution context
+ * given by "econtext".
+ *
+ * The caller should already have switched into the temporary memory context
+ * econtext->ecxt_per_tuple_memory. The convenience entry point
+ * ExecEvalExprNoReturnSwitchContext() is provided for callers who don't
+ * prefer to do the switch in an outer loop.
+ */
+#ifndef FRONTEND
+static inline void
+ExecEvalExprNoReturn(ExprState *state,
+ ExprContext *econtext)
+{
+ PG_USED_FOR_ASSERTS_ONLY Datum retDatum;
+
+ retDatum = state->evalfunc(state, econtext, NULL);
+
+ Assert(retDatum == (Datum) 0);
+}
+#endif
+
/*
* ExecEvalExprSwitchContext
*
}
#endif
+/*
+ * ExecEvalExprNoReturnSwitchContext
+ *
+ * Same as ExecEvalExprNoReturn, but get into the right allocation context
+ * explicitly.
+ */
+#ifndef FRONTEND
+static inline void
+ExecEvalExprNoReturnSwitchContext(ExprState *state,
+ ExprContext *econtext)
+{
+ MemoryContext oldContext;
+
+ oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
+ ExecEvalExprNoReturn(state, econtext);
+ MemoryContextSwitchTo(oldContext);
+}
+#endif
+
/*
* ExecProject
*
ExprContext *econtext = projInfo->pi_exprContext;
ExprState *state = &projInfo->pi_state;
TupleTableSlot *slot = state->resultslot;
- bool isnull;
/*
* Clear any former contents of the result slot. This makes it safe for
*/
ExecClearTuple(slot);
- /* Run the expression, discarding scalar result from the last column. */
- (void) ExecEvalExprSwitchContext(state, econtext, &isnull);
+ /* Run the expression */
+ ExecEvalExprNoReturnSwitchContext(state, econtext);
/*
* Successfully formed a result row. Mark the result slot as containing a