This commit is contained in:
lizhen
2023-03-14 04:33:37 -07:00
parent 6c2ec6da1a
commit ae7631a4ab
17 changed files with 2645 additions and 495 deletions

View File

@ -196,6 +196,201 @@ typedef struct AggStatePerAggData {
TupleTableSlot *evalslot; /* current input tuple */
} AggStatePerAggData;
/*
* AggStatePerTransData - per aggregate state value information
*
* Working state for updating the aggregate's state value, by calling the
* transition function with an input row. This struct does not store the
* information needed to produce the final aggregate result from the transition
* state, that's stored in AggStatePerAggForFlattenedExprData instead. This separation allows
* multiple aggregate results to be produced from a single state value.
*/
typedef struct AggStatePerTransData {
Aggref *aggref;
/* number of input arguments for aggregate function proper */
int numArguments;
/* number of inputs including ORDER BY expressions */
int numInputs;
/* offset of input columns in AggState->evalslot */
int inputoff;
bool is_avg;
/*
* Number of aggregated input columns to pass to the transfn. This
* includes the ORDER BY columns for ordered-set aggs, but not for plain
* aggs. (This doesn't count the transition state value!)
*/
int numTransInputs;
/* Oid of the state transition function */
Oid transfn_oid;
/* Oid of state value's datatype */
Oid aggtranstype;
/* ExprStates of the FILTER and argument expressions. */
ExprState *aggfilter; /* state of FILTER expression, if any */
List *args; /* states of aggregated-argument expressions */
List *aggdirectargs; /* states of direct-argument expressions */
/*
* fmgr lookup data for transition function. Note in particular that the
* fn_strict flag is kept here.
*/
FmgrInfo transfn;
#ifdef PGXC
FmgrInfo collectfn;
#endif /* PGXC */
/* Input collation derived for aggregate */
Oid aggCollation;
/* number of sorting columns */
int numSortCols;
/* number of sorting columns to consider in DISTINCT comparisons */
/* (this is either zero or the same as numSortCols) */
int numDistinctCols;
/* deconstructed sorting information (arrays of length numSortCols) */
AttrNumber *sortColIdx;
Oid *sortOperators;
Oid *sortCollations;
bool *sortNullsFirst;
/*
* fmgr lookup data for input columns' equality operators --- only
* set/used when aggregate has DISTINCT flag. Note that these are in
* order of sort column index, not parameter index.
*/
FmgrInfo *equalfns; /* array of length numDistinctCols */
/*
* initial value from pg_aggregate entry
*/
Datum initValue;
bool initValueIsNull;
#ifdef PGXC
Datum initCollectValue;
bool initCollectValueIsNull;
#endif /* PGXC */
/*
* We need the len and byval info for the agg's input, result, and
* transition data types in order to know how to copy/delete values.
*
* Note that the info for the input type is used only when handling
* DISTINCT aggs with just one argument, so there is only one input type.
*/
int16 inputtypeLen, resulttypeLen, transtypeLen;
bool inputtypeByVal, resulttypeByVal, transtypeByVal;
/*
* Stuff for evaluation of aggregate inputs in cases where the aggregate
* requires sorted input. The arguments themselves will be evaluated via
* AggState->evalslot/evalproj for all aggregates at once, but we only
* want to sort the relevant columns for individual aggregates.
*/
TupleDesc sortdesc; /* descriptor of input tuples */
/*
* Slots for holding the evaluated input arguments. These are set up
* during ExecInitAgg() and then used for each input row requiring
* procesessing besides what's done in AggState->evalproj.
*/
TupleTableSlot *sortslot; /* current input tuple */
TupleTableSlot *uniqslot; /* used for multi-column DISTINCT */
/*
* These values are working state that is initialized at the start of an
* input tuple group and updated for each input tuple.
*
* For a simple (non DISTINCT/ORDER BY) aggregate, we just feed the input
* values straight to the transition function. If it's DISTINCT or
* requires ORDER BY, we pass the input values into a Tuplesort object;
* then at completion of the input tuple group, we scan the sorted values,
* eliminate duplicates if needed, and run the transition function on the
* rest.
*/
Tuplesortstate **sortstates; /* sort object, if DISTINCT or ORDER BY */
Tuplesortstate *sortstate; /* sort object, if DISTINCT or ORDER BY */
/*
* This field is a pre-initialized FunctionCallInfo struct used for
* calling this aggregate's transfn. We save a few cycles per row by not
* re-initializing the unchanging fields; which isn't much, but it seems
* worth the extra space consumption.
*/
FunctionCallInfoData transfn_fcinfo;
FunctionCallInfoData collectfn_fcinfo;
/* XXX: use for vector engine now, better remove later*/
TupleDesc evaldesc; /* descriptor of input tuples */
ProjectionInfo *evalproj; /* projection machinery */
TupleTableSlot *evalslot; /* current input tuple */
} AggStatePerTransData;
/*
* AggStatePerAggForFlattenedExprData - per-aggregate information
*
* This contains the information needed to call the final function, to produce
* a final aggregate result from the state value. If there are multiple
* identical Aggrefs in the query, they can all share the same per-agg data.
*
* These values are set up during ExecInitAgg() and do not change thereafter.
*/
typedef struct AggStatePerAggForFlattenedExprData {
/*
* Link to an Aggref expr this state value is for.
*
* There can be multiple identical Aggref's sharing the same per-agg. This
* points to the first one of them.
*/
Aggref *aggref;
/* index to the state value which this agg should use */
int transno;
/* Optional Oid of final function (may be InvalidOid) */
Oid finalfn_oid;
/*
* fmgr lookup data for final function --- only valid when finalfn_oid oid
* is not InvalidOid.
*/
FmgrInfo finalfn;
#ifdef PGXC
FmgrInfo collectfn;
#endif /* PGXC */
/* ExprStates for any direct-argument expressions */
List *aggdirectargs;
/*
* Number of arguments to pass to the finalfn. This is always at least 1
* (the transition state value) plus any ordered-set direct args. If the
* finalfn wants extra args then we pass nulls corresponding to the
* aggregated input columns.
*/
int numFinalArgs;
/*
* We need the len and byval info for the agg's result data type in order
* to know how to copy/delete values.
*/
int16 resulttypeLen;
bool resulttypeByVal;
#ifdef PGXC
bool is_avg;
#endif /* PGXC */
} AggStatePerAggForFlattenedExprData;
/*
* AggStatePerPhaseData - per-grouping-set-phase state
*
@ -214,6 +409,8 @@ typedef struct AggStatePerPhaseData {
FmgrInfo* eqfunctions; /* per-grouping-field equality fns */
Agg* aggnode; /* Agg node for phase data */
Sort* sortnode; /* Sort node for input ordering for phase */
AggStrategy aggstrategy; /* strategy mode */
ExprState *evaltrans; /* evaluation of transition functions */
} AggStatePerPhaseData;
/*