diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index 984216d..d4dcb9b 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -152,15 +152,15 @@ /* - * AggStateTransStateData - per aggregate state data for the Agg scan + * AggStatePerTransData - per aggregate transition state data for the Agg scan * * Working state for calculating the aggregate's transition state, using the * state transition function. This struct does not store the information needed * to produce the final aggregate result from the transition state, that's stored * in AggStatePerAggData instead. This separation allows multiple aggregate - * results to be produced from a single state value. + * results to be produced from a single transition state. */ -typedef struct AggStateTransStateData +typedef struct AggStatePerTransData { /* * These values are set up during ExecInitAgg() and do not change @@ -294,7 +294,7 @@ typedef struct AggStateTransStateData * worth the extra space consumption. */ FunctionCallInfoData transfn_fcinfo; -} AggStateTransStateData; +} AggStatePerTransData; /* * AggStatePerAggData - per-aggregate working state @@ -309,8 +309,8 @@ typedef struct AggStatePerAggData * thereafter: */ - /* index to the corresponding per-aggstate which this agg should use */ - int stateno; + /* index to the corresponding per-trans state which this agg should use */ + int transno; /* Optional Oid of final function (may be InvalidOid) */ Oid finalfn_oid; @@ -424,14 +424,14 @@ static void initialize_aggregates(AggState *aggstate, AggStatePerGroup pergroup, int numReset); static void advance_transition_function(AggState *aggstate, - AggStateTransState transstate, + AggStatePerTrans pertrans, AggStatePerGroup pergroupstate); static void advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup); static void process_ordered_aggregate_single(AggState *aggstate, - AggStateTransState transstate, + AggStatePerTrans pertrans, AggStatePerGroup pergroupstate); static void process_ordered_aggregate_multi(AggState *aggstate, - AggStateTransState transstate, + AggStatePerTrans pertrans, AggStatePerGroup pergroupstate); static void finalize_aggregate(AggState *aggstate, AggStatePerAgg peragg, @@ -454,14 +454,14 @@ static TupleTableSlot *agg_retrieve_direct(AggState *aggstate); static void agg_fill_hash_table(AggState *aggstate); static TupleTableSlot *agg_retrieve_hash_table(AggState *aggstate); static Datum GetAggInitVal(Datum textInitVal, Oid transtype); -static void build_transstate_for_aggref(AggStateTransState transstate, +static void build_pertrans_for_aggref(AggStatePerTrans pertrans, AggState *aggsate, EState *estate, Aggref *aggref, HeapTuple aggtuple, Oid *inputTypes, int numArguments); static AggRefCompatibility find_compatible_aggref(Aggref *newagg, AggState *aggstate, int lastaggno, int *foundaggno); static AggRefCompatibility aggref_has_compatible_states(Aggref *newagg, - AggStatePerAgg peragg, AggStateTransState transstate); + AggStatePerAgg peragg, AggStatePerTrans pertrans); /* @@ -564,20 +564,20 @@ fetch_input_tuple(AggState *aggstate) * When called, CurrentMemoryContext should be the per-query context. */ static void -initialize_aggregate(AggState *aggstate, AggStateTransState transstate, +initialize_aggregate(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate) { /* * Start a fresh sort operation for each DISTINCT/ORDER BY aggregate. */ - if (transstate->numSortCols > 0) + if (pertrans->numSortCols > 0) { /* * In case of rescan, maybe there could be an uncompleted sort * operation? Clean it up if so. */ - if (transstate->sortstates[aggstate->current_set]) - tuplesort_end(transstate->sortstates[aggstate->current_set]); + if (pertrans->sortstates[aggstate->current_set]) + tuplesort_end(pertrans->sortstates[aggstate->current_set]); /* @@ -585,21 +585,21 @@ initialize_aggregate(AggState *aggstate, AggStateTransState transstate, * otherwise sort the full tuple. (See comments for * process_ordered_aggregate_single.) */ - if (transstate->numInputs == 1) - transstate->sortstates[aggstate->current_set] = - tuplesort_begin_datum(transstate->evaldesc->attrs[0]->atttypid, - transstate->sortOperators[0], - transstate->sortCollations[0], - transstate->sortNullsFirst[0], + if (pertrans->numInputs == 1) + pertrans->sortstates[aggstate->current_set] = + tuplesort_begin_datum(pertrans->evaldesc->attrs[0]->atttypid, + pertrans->sortOperators[0], + pertrans->sortCollations[0], + pertrans->sortNullsFirst[0], work_mem, false); else - transstate->sortstates[aggstate->current_set] = - tuplesort_begin_heap(transstate->evaldesc, - transstate->numSortCols, - transstate->sortColIdx, - transstate->sortOperators, - transstate->sortCollations, - transstate->sortNullsFirst, + pertrans->sortstates[aggstate->current_set] = + tuplesort_begin_heap(pertrans->evaldesc, + pertrans->numSortCols, + pertrans->sortColIdx, + pertrans->sortOperators, + pertrans->sortCollations, + pertrans->sortNullsFirst, work_mem, false); } @@ -609,20 +609,20 @@ initialize_aggregate(AggState *aggstate, AggStateTransState transstate, * Note that when the initial value is pass-by-ref, we must copy it (into * the aggcontext) since we will pfree the transValue later. */ - if (transstate->initValueIsNull) - pergroupstate->transValue = transstate->initValue; + if (pertrans->initValueIsNull) + pergroupstate->transValue = pertrans->initValue; else { MemoryContext oldContext; oldContext = MemoryContextSwitchTo( aggstate->aggcontexts[aggstate->current_set]->ecxt_per_tuple_memory); - pergroupstate->transValue = datumCopy(transstate->initValue, - transstate->transtypeByVal, - transstate->transtypeLen); + pergroupstate->transValue = datumCopy(pertrans->initValue, + pertrans->transtypeByVal, + pertrans->transtypeLen); MemoryContextSwitchTo(oldContext); } - pergroupstate->transValueIsNull = transstate->initValueIsNull; + pergroupstate->transValueIsNull = pertrans->initValueIsNull; /* * If the initial value for the transition state doesn't exist in the @@ -631,7 +631,7 @@ initialize_aggregate(AggState *aggstate, AggStateTransState transstate, * aggregates like max() and min().) The noTransValue flag signals that we * still need to do this. */ - pergroupstate->noTransValue = transstate->initValueIsNull; + pergroupstate->noTransValue = pertrans->initValueIsNull; } /* @@ -649,27 +649,27 @@ initialize_aggregates(AggState *aggstate, AggStatePerGroup pergroup, int numReset) { - int stateno; + int transno; int numGroupingSets = Max(aggstate->phase->numsets, 1); int setno = 0; - AggStateTransState transstates = aggstate->transstates; + AggStatePerTrans transstates = aggstate->pertrans; if (numReset < 1) numReset = numGroupingSets; - for (stateno = 0; stateno < aggstate->numstates; stateno++) + for (transno = 0; transno < aggstate->numtrans; transno++) { - AggStateTransState transstate = &transstates[stateno]; + AggStatePerTrans pertrans = &transstates[transno]; for (setno = 0; setno < numReset; setno++) { AggStatePerGroup pergroupstate; - pergroupstate = &pergroup[stateno + (setno * (aggstate->numstates))]; + pergroupstate = &pergroup[transno + (setno * (aggstate->numtrans))]; aggstate->current_set = setno; - initialize_aggregate(aggstate, transstate, pergroupstate); + initialize_aggregate(aggstate, pertrans, pergroupstate); } } } @@ -679,28 +679,28 @@ initialize_aggregates(AggState *aggstate, * state within one grouping set only (already set in aggstate->current_set) * * The new values (and null flags) have been preloaded into argument positions - * 1 and up in transstate->transfn_fcinfo, so that we needn't copy them again - * to pass to the transition function. We also expect that the static fields - * of the fcinfo are already initialized; that was done by ExecInitAgg(). + * 1 and up in pertrans->transfn_fcinfo, so that we needn't copy them again to + * pass to the transition function. We also expect that the static fields of + * the fcinfo are already initialized; that was done by ExecInitAgg(). * * It doesn't matter which memory context this is called in. */ static void advance_transition_function(AggState *aggstate, - AggStateTransState transstate, +AggStatePerTrans pertrans, AggStatePerGroup pergroupstate) { - FunctionCallInfo fcinfo = &transstate->transfn_fcinfo; + FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo; MemoryContext oldContext; Datum newVal; - if (transstate->transfn.fn_strict) + if (pertrans->transfn.fn_strict) { /* * For a strict transfn, nothing happens when there's a NULL input; we * just keep the prior transValue. */ - int numTransInputs = transstate->numTransInputs; + int numTransInputs = pertrans->numTransInputs; int i; for (i = 1; i <= numTransInputs; i++) @@ -722,8 +722,8 @@ advance_transition_function(AggState *aggstate, oldContext = MemoryContextSwitchTo( aggstate->aggcontexts[aggstate->current_set]->ecxt_per_tuple_memory); pergroupstate->transValue = datumCopy(fcinfo->arg[1], - transstate->transtypeByVal, - transstate->transtypeLen); + pertrans->transtypeByVal, + pertrans->transtypeLen); pergroupstate->transValueIsNull = false; pergroupstate->noTransValue = false; MemoryContextSwitchTo(oldContext); @@ -744,8 +744,8 @@ advance_transition_function(AggState *aggstate, /* We run the transition functions in per-input-tuple memory context */ oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory); - /* set up aggstate->curtransstate for AggGetAggref() */ - aggstate->curtransstate = transstate; + /* set up aggstate->curpertrans for AggGetAggref() */ + aggstate->curpertrans = pertrans; /* * OK to call the transition function @@ -756,22 +756,22 @@ advance_transition_function(AggState *aggstate, newVal = FunctionCallInvoke(fcinfo); - aggstate->curtransstate = NULL; + aggstate->curpertrans = NULL; /* * If pass-by-ref datatype, must copy the new value into aggcontext and * pfree the prior transValue. But if transfn returned a pointer to its * first input, we don't need to do anything. */ - if (!transstate->transtypeByVal && + if (!pertrans->transtypeByVal && DatumGetPointer(newVal) != DatumGetPointer(pergroupstate->transValue)) { if (!fcinfo->isnull) { MemoryContextSwitchTo(aggstate->aggcontexts[aggstate->current_set]->ecxt_per_tuple_memory); newVal = datumCopy(newVal, - transstate->transtypeByVal, - transstate->transtypeLen); + pertrans->transtypeByVal, + pertrans->transtypeLen); } if (!pergroupstate->transValueIsNull) pfree(DatumGetPointer(pergroupstate->transValue)); @@ -794,16 +794,16 @@ advance_transition_function(AggState *aggstate, static void advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup) { - int stateno; + int transno; int setno = 0; int numGroupingSets = Max(aggstate->phase->numsets, 1); - int numStates = aggstate->numstates; + int numTrans = aggstate->numtrans; - for (stateno = 0; stateno < numStates; stateno++) + for (transno = 0; transno < numTrans; transno++) { - AggStateTransState transstate = &aggstate->transstates[stateno]; - ExprState *filter = transstate->aggfilter; - int numTransInputs = transstate->numTransInputs; + AggStatePerTrans pertrans = &aggstate->pertrans[transno]; + ExprState *filter = pertrans->aggfilter; + int numTransInputs = pertrans->numTransInputs; int i; TupleTableSlot *slot; @@ -820,12 +820,12 @@ advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup) } /* Evaluate the current input expressions for this aggregate */ - slot = ExecProject(transstate->evalproj, NULL); + slot = ExecProject(pertrans->evalproj, NULL); - if (transstate->numSortCols > 0) + if (pertrans->numSortCols > 0) { /* DISTINCT and/or ORDER BY case */ - Assert(slot->tts_nvalid == transstate->numInputs); + Assert(slot->tts_nvalid == pertrans->numInputs); /* * If the transfn is strict, we want to check for nullity before @@ -834,7 +834,7 @@ advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup) * not numInputs, since nullity in columns used only for sorting * is not relevant here. */ - if (transstate->transfn.fn_strict) + if (pertrans->transfn.fn_strict) { for (i = 0; i < numTransInputs; i++) { @@ -848,18 +848,18 @@ advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup) for (setno = 0; setno < numGroupingSets; setno++) { /* OK, put the tuple into the tuplesort object */ - if (transstate->numInputs == 1) - tuplesort_putdatum(transstate->sortstates[setno], + if (pertrans->numInputs == 1) + tuplesort_putdatum(pertrans->sortstates[setno], slot->tts_values[0], slot->tts_isnull[0]); else - tuplesort_puttupleslot(transstate->sortstates[setno], slot); + tuplesort_puttupleslot(pertrans->sortstates[setno], slot); } } else { /* We can apply the transition function immediately */ - FunctionCallInfo fcinfo = &transstate->transfn_fcinfo; + FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo; /* Load values into fcinfo */ /* Start from 1, since the 0th arg will be the transition value */ @@ -872,12 +872,11 @@ advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup) for (setno = 0; setno < numGroupingSets; setno++) { - AggStatePerGroup pergroupstate = &pergroup[stateno + (setno * numStates)]; + AggStatePerGroup pergroupstate = &pergroup[transno + (setno * numTrans)]; aggstate->current_set = setno; - advance_transition_function(aggstate, transstate, - pergroupstate); + advance_transition_function(aggstate, pertrans, pergroupstate); } } } @@ -908,7 +907,7 @@ advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup) */ static void process_ordered_aggregate_single(AggState *aggstate, - AggStateTransState transstate, + AggStatePerTrans pertrans, AggStatePerGroup pergroupstate) { Datum oldVal = (Datum) 0; @@ -916,14 +915,14 @@ process_ordered_aggregate_single(AggState *aggstate, bool haveOldVal = false; MemoryContext workcontext = aggstate->tmpcontext->ecxt_per_tuple_memory; MemoryContext oldContext; - bool isDistinct = (transstate->numDistinctCols > 0); - FunctionCallInfo fcinfo = &transstate->transfn_fcinfo; + bool isDistinct = (pertrans->numDistinctCols > 0); + FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo; Datum *newVal; bool *isNull; - Assert(transstate->numDistinctCols < 2); + Assert(pertrans->numDistinctCols < 2); - tuplesort_performsort(transstate->sortstates[aggstate->current_set]); + tuplesort_performsort(pertrans->sortstates[aggstate->current_set]); /* Load the column into argument 1 (arg 0 will be transition value) */ newVal = fcinfo->arg + 1; @@ -935,7 +934,7 @@ process_ordered_aggregate_single(AggState *aggstate, * pfree them when they are no longer needed. */ - while (tuplesort_getdatum(transstate->sortstates[aggstate->current_set], + while (tuplesort_getdatum(pertrans->sortstates[aggstate->current_set], true, newVal, isNull)) { /* @@ -954,18 +953,18 @@ process_ordered_aggregate_single(AggState *aggstate, haveOldVal && ((oldIsNull && *isNull) || (!oldIsNull && !*isNull && - DatumGetBool(FunctionCall2(&transstate->equalfns[0], + DatumGetBool(FunctionCall2(&pertrans->equalfns[0], oldVal, *newVal))))) { /* equal to prior, so forget this one */ - if (!transstate->inputtypeByVal && !*isNull) + if (!pertrans->inputtypeByVal && !*isNull) pfree(DatumGetPointer(*newVal)); } else { - advance_transition_function(aggstate, transstate, pergroupstate); + advance_transition_function(aggstate, pertrans, pergroupstate); /* forget the old value, if any */ - if (!oldIsNull && !transstate->inputtypeByVal) + if (!oldIsNull && !pertrans->inputtypeByVal) pfree(DatumGetPointer(oldVal)); /* and remember the new one for subsequent equality checks */ oldVal = *newVal; @@ -976,11 +975,11 @@ process_ordered_aggregate_single(AggState *aggstate, MemoryContextSwitchTo(oldContext); } - if (!oldIsNull && !transstate->inputtypeByVal) + if (!oldIsNull && !pertrans->inputtypeByVal) pfree(DatumGetPointer(oldVal)); - tuplesort_end(transstate->sortstates[aggstate->current_set]); - transstate->sortstates[aggstate->current_set] = NULL; + tuplesort_end(pertrans->sortstates[aggstate->current_set]); + pertrans->sortstates[aggstate->current_set] = NULL; } /* @@ -997,25 +996,25 @@ process_ordered_aggregate_single(AggState *aggstate, */ static void process_ordered_aggregate_multi(AggState *aggstate, - AggStateTransState transstate, +AggStatePerTrans pertrans, AggStatePerGroup pergroupstate) { MemoryContext workcontext = aggstate->tmpcontext->ecxt_per_tuple_memory; - FunctionCallInfo fcinfo = &transstate->transfn_fcinfo; - TupleTableSlot *slot1 = transstate->evalslot; - TupleTableSlot *slot2 = transstate->uniqslot; - int numTransInputs = transstate->numTransInputs; - int numDistinctCols = transstate->numDistinctCols; + FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo; + TupleTableSlot *slot1 = pertrans->evalslot; + TupleTableSlot *slot2 = pertrans->uniqslot; + int numTransInputs = pertrans->numTransInputs; + int numDistinctCols = pertrans->numDistinctCols; bool haveOldValue = false; int i; - tuplesort_performsort(transstate->sortstates[aggstate->current_set]); + tuplesort_performsort(pertrans->sortstates[aggstate->current_set]); ExecClearTuple(slot1); if (slot2) ExecClearTuple(slot2); - while (tuplesort_gettupleslot(transstate->sortstates[aggstate->current_set], + while (tuplesort_gettupleslot(pertrans->sortstates[aggstate->current_set], true, slot1)) { /* @@ -1029,8 +1028,8 @@ process_ordered_aggregate_multi(AggState *aggstate, !haveOldValue || !execTuplesMatch(slot1, slot2, numDistinctCols, - transstate->sortColIdx, - transstate->equalfns, + pertrans->sortColIdx, + pertrans->equalfns, workcontext)) { /* Load values into fcinfo */ @@ -1041,7 +1040,7 @@ process_ordered_aggregate_multi(AggState *aggstate, fcinfo->argnull[i + 1] = slot1->tts_isnull[i]; } - advance_transition_function(aggstate, transstate, pergroupstate); + advance_transition_function(aggstate, pertrans, pergroupstate); if (numDistinctCols > 0) { @@ -1064,8 +1063,8 @@ process_ordered_aggregate_multi(AggState *aggstate, if (slot2) ExecClearTuple(slot2); - tuplesort_end(transstate->sortstates[aggstate->current_set]); - transstate->sortstates[aggstate->current_set] = NULL; + tuplesort_end(pertrans->sortstates[aggstate->current_set]); + pertrans->sortstates[aggstate->current_set] = NULL; } /* @@ -1077,7 +1076,7 @@ process_ordered_aggregate_multi(AggState *aggstate, * The finalfunction will be run, and the result delivered, in the * output-tuple context; caller's CurrentMemoryContext does not matter. * - * The finalfn uses the state as set in the stateno. This also might be + * The finalfn uses the state as set in the transno. This also might be * being used by another aggregate function, so it's important that we do * nothing destructive here. */ @@ -1092,7 +1091,7 @@ finalize_aggregate(AggState *aggstate, MemoryContext oldContext; int i; ListCell *lc; - AggStateTransState transstate = &aggstate->transstates[peragg->stateno]; + AggStatePerTrans pertrans = &aggstate->pertrans[peragg->transno]; oldContext = MemoryContextSwitchTo(aggstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory); @@ -1103,7 +1102,7 @@ finalize_aggregate(AggState *aggstate, * for the transition state value. */ i = 1; - foreach(lc, transstate->aggdirectargs) + foreach(lc, pertrans->aggdirectargs) { ExprState *expr = (ExprState *) lfirst(lc); @@ -1122,12 +1121,12 @@ finalize_aggregate(AggState *aggstate, { int numFinalArgs = peragg->numFinalArgs; - /* set up aggstate->curtransstate for AggGetAggref() */ - aggstate->curtransstate = transstate; + /* set up aggstate->curpertrans for AggGetAggref() */ + aggstate->curpertrans = pertrans; InitFunctionCallInfoData(fcinfo, &peragg->finalfn, numFinalArgs, - transstate->aggCollation, + pertrans->aggCollation, (void *) aggstate, NULL); /* Fill in the transition state value */ @@ -1154,7 +1153,7 @@ finalize_aggregate(AggState *aggstate, *resultVal = FunctionCallInvoke(&fcinfo); *resultIsNull = fcinfo.isnull; } - aggstate->curtransstate = NULL; + aggstate->curpertrans = NULL; } else { @@ -1262,23 +1261,23 @@ finalize_aggregates(AggState *aggstate, for (aggno = 0; aggno < aggstate->numaggs; aggno++) { AggStatePerAgg peragg = &peraggs[aggno]; - int stateno = peragg->stateno; - AggStateTransState transstate = &aggstate->transstates[stateno]; + int transno = peragg->transno; + AggStatePerTrans pertrans = &aggstate->pertrans[transno]; AggStatePerGroup pergroupstate; - pergroupstate = &pergroup[stateno + (currentSet * (aggstate->numstates))]; + pergroupstate = &pergroup[transno + (currentSet * (aggstate->numtrans))]; - if (transstate->numSortCols > 0) + if (pertrans->numSortCols > 0) { Assert(((Agg *) aggstate->ss.ps.plan)->aggstrategy != AGG_HASHED); - if (transstate->numInputs == 1) + if (pertrans->numInputs == 1) process_ordered_aggregate_single(aggstate, - transstate, + pertrans, pergroupstate); else process_ordered_aggregate_multi(aggstate, - transstate, + pertrans, pergroupstate); } @@ -2020,11 +2019,11 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) { AggState *aggstate; AggStatePerAgg peraggs; - AggStateTransState transstates; + AggStatePerTrans pertransstates; Plan *outerPlan; ExprContext *econtext; int numaggs, - stateno, + transno, aggno; int phase; ListCell *l; @@ -2046,14 +2045,14 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) aggstate->aggs = NIL; aggstate->numaggs = 0; - aggstate->numstates = 0; + aggstate->numtrans = 0; aggstate->maxsets = 0; aggstate->hashfunctions = NULL; aggstate->projected_set = -1; aggstate->current_set = 0; aggstate->peragg = NULL; - aggstate->transstates = NULL; - aggstate->curtransstate = NULL; + aggstate->pertrans = NULL; + aggstate->curpertrans = NULL; aggstate->agg_done = false; aggstate->input_done = false; aggstate->pergroup = NULL; @@ -2287,10 +2286,10 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) econtext->ecxt_aggnulls = (bool *) palloc0(sizeof(bool) * numaggs); peraggs = (AggStatePerAgg) palloc0(sizeof(AggStatePerAggData) * numaggs); - transstates = (AggStateTransState) palloc0(sizeof(AggStateTransStateData)* numaggs); + pertransstates = (AggStatePerTrans) palloc0(sizeof(AggStatePerTransData)* numaggs); aggstate->peragg = peraggs; - aggstate->transstates = transstates; + aggstate->pertrans = pertransstates; if (node->aggstrategy == AGG_HASHED) { @@ -2315,13 +2314,13 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) * unchanging fields of the per-agg data. */ aggno = -1; - stateno = -1; + transno = -1; foreach(l, aggstate->aggs) { AggrefExprState *aggrefstate = (AggrefExprState *) lfirst(l); Aggref *aggref = (Aggref *) aggrefstate->xprstate.expr; AggStatePerAgg peragg; - AggStateTransState transstate; + AggStatePerTrans pertrans; AggRefCompatibility agg_match; Oid inputTypes[FUNC_MAX_ARGS]; int numArguments; @@ -2446,21 +2445,21 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) */ if (agg_match == AGGREF_NO_MATCH) { - transstate = &transstates[++stateno]; - build_transstate_for_aggref(transstate, aggstate, estate, + pertrans = &pertransstates[++transno]; + build_pertrans_for_aggref(pertrans, aggstate, estate, aggref, aggTuple, inputTypes, numArguments); - peragg->stateno = stateno; + peragg->transno = transno; } else /* AGGREF_STATE_MATCH */ { - int existing_stateno = peraggs[existing_aggno].stateno; + int existing_transno = peraggs[existing_aggno].transno; - transstate = &transstates[existing_stateno]; - peragg->stateno = existing_stateno; + pertrans = &pertransstates[existing_transno]; + peragg->transno = existing_transno; /* when reusing the state, the transfns should match! */ - Assert(transstate->transfn_oid == aggform->aggtransfn); + Assert(pertrans->transfn_oid == aggform->aggtransfn); } /* Detect how many arguments to pass to the finalfn */ @@ -2477,7 +2476,7 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) { build_aggregate_finalfn_expr(inputTypes, peragg->numFinalArgs, - transstate->aggtranstype, + pertrans->aggtranstype, aggref->aggtype, aggref->inputcollid, finalfn_oid, @@ -2499,7 +2498,7 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) * numstates to the number of unique aggregate states found. */ aggstate->numaggs = aggno + 1; - aggstate->numstates = stateno + 1; + aggstate->numtrans = transno + 1; return aggstate; } @@ -2507,12 +2506,12 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) /* * Build the state needed to calculate a state value for an aggregate. * - * This initializes all the fields in 'transstate'. 'aggTuple', + * This initializes all the fields in 'pertrans'. 'aggTuple', * 'inputTypes' and 'numArguments' could be derived from 'aggref', but the * caller has calculated them already, so might as well pass them. */ static void -build_transstate_for_aggref(AggStateTransState transstate, +build_pertrans_for_aggref(AggStatePerTrans pertrans, AggState *aggstate, EState *estate, Aggref *aggref, HeapTuple aggTuple, Oid *inputTypes, int numArguments) @@ -2531,19 +2530,19 @@ build_transstate_for_aggref(AggStateTransState transstate, Datum textInitVal; Oid transfn_oid; - /* Begin filling in the transstate data */ - transstate->aggref = aggref; - transstate->aggCollation = aggref->inputcollid; - transstate->transfn_oid = transfn_oid = aggform->aggtransfn; + /* Begin filling in the pertrans data */ + pertrans->aggref = aggref; + pertrans->aggCollation = aggref->inputcollid; + pertrans->transfn_oid = transfn_oid = aggform->aggtransfn; /* Count the "direct" arguments, if any */ numDirectArgs = list_length(aggref->aggdirectargs); /* Count the number of aggregated input columns */ - transstate->numInputs = numInputs = list_length(aggref->args); + pertrans->numInputs = numInputs = list_length(aggref->args); /* resolve actual type of transition state, if polymorphic */ - transstate->aggtranstype = + pertrans->aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid, aggform->aggtranstype, inputTypes, @@ -2551,9 +2550,9 @@ build_transstate_for_aggref(AggStateTransState transstate, /* Detect how many arguments to pass to the transfn */ if (AGGKIND_IS_ORDERED_SET(aggref->aggkind)) - transstate->numTransInputs = numInputs; + pertrans->numTransInputs = numInputs; else - transstate->numTransInputs = numArguments; + pertrans->numTransInputs = numArguments; /* * Set up infrastructure for calling the transfn @@ -2562,19 +2561,19 @@ build_transstate_for_aggref(AggStateTransState transstate, numArguments, numDirectArgs, aggref->aggvariadic, - transstate->aggtranstype, + pertrans->aggtranstype, aggref->inputcollid, transfn_oid, InvalidOid, /* invtrans is not needed here */ &transfnexpr, NULL); - fmgr_info(transfn_oid, &transstate->transfn); - fmgr_info_set_expr((Node *) transfnexpr, &transstate->transfn); + fmgr_info(transfn_oid, &pertrans->transfn); + fmgr_info_set_expr((Node *) transfnexpr, &pertrans->transfn); - InitFunctionCallInfoData(transstate->transfn_fcinfo, - &transstate->transfn, - transstate->numTransInputs + 1, - transstate->aggCollation, + InitFunctionCallInfoData(pertrans->transfn_fcinfo, + &pertrans->transfn, + pertrans->numTransInputs + 1, + pertrans->aggCollation, (void *) aggstate, NULL); @@ -2586,13 +2585,13 @@ build_transstate_for_aggref(AggStateTransState transstate, */ textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple, Anum_pg_aggregate_agginitval, - &transstate->initValueIsNull); + &pertrans->initValueIsNull); - if (transstate->initValueIsNull) - transstate->initValue = (Datum) 0; + if (pertrans->initValueIsNull) + pertrans->initValue = (Datum) 0; else - transstate->initValue = GetAggInitVal(textInitVal, - transstate->aggtranstype); + pertrans->initValue = GetAggInitVal(textInitVal, + pertrans->aggtranstype); /* * If the transfn is strict and the initval is NULL, make sure input type @@ -2602,11 +2601,11 @@ build_transstate_for_aggref(AggStateTransState transstate, * we must check again in case the transfn's strictness property has been * changed. */ - if (transstate->transfn.fn_strict && transstate->initValueIsNull) + if (pertrans->transfn.fn_strict && pertrans->initValueIsNull) { if (numArguments <= numDirectArgs || !IsBinaryCoercible(inputTypes[numDirectArgs], - transstate->aggtranstype)) + pertrans->aggtranstype)) ereport(ERROR, (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION), errmsg("aggregate %u needs to have compatible input type and transition type", @@ -2614,27 +2613,27 @@ build_transstate_for_aggref(AggStateTransState transstate, } /* get info about the state value's datatype */ - get_typlenbyval(transstate->aggtranstype, - &transstate->transtypeLen, - &transstate->transtypeByVal); + get_typlenbyval(pertrans->aggtranstype, + &pertrans->transtypeLen, + &pertrans->transtypeByVal); /* * Get a tupledesc corresponding to the aggregated inputs (including sort * expressions) of the agg. */ - transstate->evaldesc = ExecTypeFromTL(aggref->args, false); + pertrans->evaldesc = ExecTypeFromTL(aggref->args, false); /* Create slot we're going to do argument evaluation in */ - transstate->evalslot = ExecInitExtraTupleSlot(estate); - ExecSetSlotDescriptor(transstate->evalslot, transstate->evaldesc); + pertrans->evalslot = ExecInitExtraTupleSlot(estate); + ExecSetSlotDescriptor(pertrans->evalslot, pertrans->evaldesc); /* Initialize the input and FILTER expressions */ naggs = aggstate->numaggs; - transstate->aggfilter = ExecInitExpr(aggref->aggfilter, + pertrans->aggfilter = ExecInitExpr(aggref->aggfilter, (PlanState *) aggstate); - transstate->aggdirectargs = (List *) ExecInitExpr((Expr *)aggref->aggdirectargs, + pertrans->aggdirectargs = (List *) ExecInitExpr((Expr *) aggref->aggdirectargs, (PlanState *) aggstate); - transstate->args = (List *)ExecInitExpr((Expr *)aggref->args, + pertrans->args = (List *) ExecInitExpr((Expr *) aggref->args, (PlanState *) aggstate); /* @@ -2648,10 +2647,10 @@ build_transstate_for_aggref(AggStateTransState transstate, errmsg("aggregate function calls cannot be nested"))); /* Set up projection info for evaluation */ - transstate->evalproj = ExecBuildProjectionInfo(transstate->args, - aggstate->tmpcontext, - transstate->evalslot, - NULL); + pertrans->evalproj = ExecBuildProjectionInfo(pertrans->args, + aggstate->tmpcontext, + pertrans->evalslot, + NULL); /* * If we're doing either DISTINCT or ORDER BY for a plain agg, then we @@ -2680,8 +2679,8 @@ build_transstate_for_aggref(AggStateTransState transstate, numDistinctCols = 0; } - transstate->numSortCols = numSortCols; - transstate->numDistinctCols = numDistinctCols; + pertrans->numSortCols = numSortCols; + pertrans->numDistinctCols = numDistinctCols; if (numSortCols > 0) { @@ -2695,25 +2694,25 @@ build_transstate_for_aggref(AggStateTransState transstate, if (numInputs == 1) { get_typlenbyval(inputTypes[numDirectArgs], - &transstate->inputtypeLen, - &transstate->inputtypeByVal); + &pertrans->inputtypeLen, + &pertrans->inputtypeByVal); } else if (numDistinctCols > 0) { /* we will need an extra slot to store prior values */ - transstate->uniqslot = ExecInitExtraTupleSlot(estate); - ExecSetSlotDescriptor(transstate->uniqslot, - transstate->evaldesc); + pertrans->uniqslot = ExecInitExtraTupleSlot(estate); + ExecSetSlotDescriptor(pertrans->uniqslot, + pertrans->evaldesc); } /* Extract the sort information for use later */ - transstate->sortColIdx = + pertrans->sortColIdx = (AttrNumber *) palloc(numSortCols * sizeof(AttrNumber)); - transstate->sortOperators = + pertrans->sortOperators = (Oid *) palloc(numSortCols * sizeof(Oid)); - transstate->sortCollations = + pertrans->sortCollations = (Oid *) palloc(numSortCols * sizeof(Oid)); - transstate->sortNullsFirst = + pertrans->sortNullsFirst = (bool *) palloc(numSortCols * sizeof(bool)); i = 0; @@ -2725,10 +2724,10 @@ build_transstate_for_aggref(AggStateTransState transstate, /* the parser should have made sure of this */ Assert(OidIsValid(sortcl->sortop)); - transstate->sortColIdx[i] = tle->resno; - transstate->sortOperators[i] = sortcl->sortop; - transstate->sortCollations[i] = exprCollation((Node *)tle->expr); - transstate->sortNullsFirst[i] = sortcl->nulls_first; + pertrans->sortColIdx[i] = tle->resno; + pertrans->sortOperators[i] = sortcl->sortop; + pertrans->sortCollations[i] = exprCollation((Node *) tle->expr); + pertrans->sortNullsFirst[i] = sortcl->nulls_first; i++; } Assert(i == numSortCols); @@ -2742,7 +2741,7 @@ build_transstate_for_aggref(AggStateTransState transstate, * We need the equal function for each DISTINCT comparison we will * make. */ - transstate->equalfns = + pertrans->equalfns = (FmgrInfo *) palloc(numDistinctCols * sizeof(FmgrInfo)); i = 0; @@ -2750,13 +2749,13 @@ build_transstate_for_aggref(AggStateTransState transstate, { SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc); - fmgr_info(get_opcode(sortcl->eqop), &transstate->equalfns[i]); + fmgr_info(get_opcode(sortcl->eqop), &pertrans->equalfns[i]); i++; } Assert(i == numDistinctCols); } - transstate->sortstates = (Tuplesortstate **) + pertrans->sortstates = (Tuplesortstate **) palloc0(sizeof(Tuplesortstate *) * numGroupingSets); } @@ -2834,7 +2833,7 @@ find_compatible_aggref(Aggref *newagg, AggState *aggstate, { int aggno; int statematchaggno; - AggStateTransState transstates; + AggStatePerTrans pertransstates; AggStatePerAgg peraggs; /* we mustn't reuse the aggref if it contains volatile function calls */ @@ -2842,7 +2841,7 @@ find_compatible_aggref(Aggref *newagg, AggState *aggstate, return AGGREF_NO_MATCH; statematchaggno = -1; - transstates = aggstate->transstates; + pertransstates = aggstate->pertrans; peraggs = aggstate->peragg; /* @@ -2855,13 +2854,13 @@ find_compatible_aggref(Aggref *newagg, AggState *aggstate, { AggRefCompatibility matchtype; AggStatePerAgg peragg; - AggStateTransState transstate; + AggStatePerTrans pertrans; peragg = &peraggs[aggno]; - transstate = &transstates[peragg->stateno]; + pertrans = &pertransstates[peragg->transno]; /* lookup the match type of this agg */ - matchtype = aggref_has_compatible_states(newagg, peragg, transstate); + matchtype = aggref_has_compatible_states(newagg, peragg, pertrans); /* if it's an exact match then we're done. */ if (matchtype == AGGREF_EXACT_MATCH) @@ -2893,9 +2892,9 @@ find_compatible_aggref(Aggref *newagg, AggState *aggstate, static AggRefCompatibility aggref_has_compatible_states(Aggref *newagg, AggStatePerAgg peragg, - AggStateTransState transstate) + AggStatePerTrans pertrans) { - Aggref *existingRef = transstate->aggref; + Aggref *existingRef = pertrans->aggref; /* all of the following must be the same or it's no match */ if (newagg->inputcollid != existingRef->inputcollid || @@ -2934,7 +2933,7 @@ aggref_has_compatible_states(Aggref *newagg, aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple); /* if the transfns are not the same then the state can't be shared */ - if (aggform->aggtransfn != transstate->transfn_oid) + if (aggform->aggtransfn != pertrans->transfn_oid) { ReleaseSysCache(aggTuple); return AGGREF_NO_MATCH; @@ -2949,7 +2948,7 @@ aggref_has_compatible_states(Aggref *newagg, * If both INITCONDs are null then the outcome depends on if the * finalfns match. */ - if (initValueIsNull && transstate->initValueIsNull) + if (initValueIsNull && pertrans->initValueIsNull) { if (aggform->aggfinalfn != peragg->finalfn_oid) return AGGREF_STATE_MATCH; @@ -2969,7 +2968,7 @@ void ExecEndAgg(AggState *node) { PlanState *outerPlan; - int stateno; + int transno; int numGroupingSets = Max(node->maxsets, 1); int setno; @@ -2980,14 +2979,14 @@ ExecEndAgg(AggState *node) if (node->sort_out) tuplesort_end(node->sort_out); - for (stateno = 0; stateno < node->numstates; stateno++) + for (transno = 0; transno < node->numtrans; transno++) { - AggStateTransState transstate = &node->transstates[stateno]; + AggStatePerTrans pertrans = &node->pertrans[transno]; for (setno = 0; setno < numGroupingSets; setno++) { - if (transstate->sortstates[setno]) - tuplesort_end(transstate->sortstates[setno]); + if (pertrans->sortstates[setno]) + tuplesort_end(pertrans->sortstates[setno]); } } @@ -3015,7 +3014,7 @@ ExecReScanAgg(AggState *node) ExprContext *econtext = node->ss.ps.ps_ExprContext; PlanState *outerPlan = outerPlanState(node); Agg *aggnode = (Agg *) node->ss.ps.plan; - int stateno; + int transno; int numGroupingSets = Max(node->maxsets, 1); int setno; @@ -3047,16 +3046,16 @@ ExecReScanAgg(AggState *node) } /* Make sure we have closed any open tuplesorts */ - for (stateno = 0; stateno < node->numstates; stateno++) + for (transno = 0; transno < node->numtrans; transno++) { for (setno = 0; setno < numGroupingSets; setno++) { - AggStateTransState transstate = &node->transstates[stateno]; + AggStatePerTrans pertrans = &node->pertrans[transno]; - if (transstate->sortstates[setno]) + if (pertrans->sortstates[setno]) { - tuplesort_end(transstate->sortstates[setno]); - transstate->sortstates[setno] = NULL; + tuplesort_end(pertrans->sortstates[setno]); + pertrans->sortstates[setno] = NULL; } } } @@ -3180,12 +3179,12 @@ AggGetAggref(FunctionCallInfo fcinfo) { if (fcinfo->context && IsA(fcinfo->context, AggState)) { - AggStateTransState curtransstate; + AggStatePerTrans curpertrans; - curtransstate = ((AggState *)fcinfo->context)->curtransstate; + curpertrans = ((AggState *)fcinfo->context)->curpertrans; - if (curtransstate) - return curtransstate->aggref; + if (curpertrans) + return curpertrans->aggref; } return NULL; } diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 65e6a85..e0fb8fb 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -1826,8 +1826,9 @@ resolve_aggregate_transtype(Oid aggfuncid, * (The trees will never actually be executed, however, so we can skimp * a bit on correctness.) * - * agg_input_types identifies the input types of the aggregate. These should - * be resolved to actual types (ie, none should ever be ANYELEMENT etc). + * agg_input_types and agg_state_type identifies the input types of the + * aggregate. These should be resolved to actual types (ie, none should + * ever be ANYELEMENT etc). * agg_input_collation is the aggregate function's input collation. * * For an ordered-set aggregate, remember that agg_input_types describes diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 3579f3b..7091a9d 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -1822,7 +1822,7 @@ typedef struct GroupState */ /* these structs are private in nodeAgg.c: */ typedef struct AggStatePerAggData *AggStatePerAgg; -typedef struct AggStateTransStateData *AggStateTransState; +typedef struct AggStatePerTransData *AggStatePerTrans; typedef struct AggStatePerGroupData *AggStatePerGroup; typedef struct AggStatePerPhaseData *AggStatePerPhase; @@ -1831,16 +1831,16 @@ typedef struct AggState ScanState ss; /* its first field is NodeTag */ List *aggs; /* all Aggref nodes in targetlist & quals */ int numaggs; /* length of list (could be zero!) */ - int numstates; /* number of peraggstate items */ + int numtrans; /* number of pertrans items */ AggStatePerPhase phase; /* pointer to current phase data */ int numphases; /* number of phases */ int current_phase; /* current phase number */ FmgrInfo *hashfunctions; /* per-grouping-field hash fns */ AggStatePerAgg peragg; /* per-Aggref information */ - AggStateTransState transstates; /* per-Agg State information */ + AggStatePerTrans pertrans; /* per-Agg trans state information */ ExprContext **aggcontexts; /* econtexts for long-lived data (per GS) */ ExprContext *tmpcontext; /* econtext for input expressions */ - AggStateTransState curtransstate; /* identifies currently active aggregate */ + AggStatePerTrans curpertrans; /* currently active trans state */ bool input_done; /* indicates end of input */ bool agg_done; /* indicates completion of Agg scan */ int projected_set; /* The last projected grouping set */