codekingpro/portable-devtools
115k
1/*-------------------------------------------------------------------------2 *3 * nodeAgg.h4 * prototypes for nodeAgg.c5 *6 *7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group8 * Portions Copyright (c) 1994, Regents of the University of California9 *10 * src/include/executor/nodeAgg.h11 *12 *-------------------------------------------------------------------------13 */14#ifndef NODEAGG_H15#define NODEAGG_H16 17#include "access/parallel.h"18#include "nodes/execnodes.h"19 20 21/*22 * AggStatePerTransData - per aggregate state value information23 *24 * Working state for updating the aggregate's state value, by calling the25 * transition function with an input row. This struct does not store the26 * information needed to produce the final aggregate result from the transition27 * state, that's stored in AggStatePerAggData instead. This separation allows28 * multiple aggregate results to be produced from a single state value.29 */30typedef struct AggStatePerTransData31{32 /*33 * These values are set up during ExecInitAgg() and do not change34 * thereafter:35 */36 37 /*38 * Link to an Aggref expr this state value is for.39 *40 * There can be multiple Aggref's sharing the same state value, so long as41 * the inputs and transition functions are identical and the final42 * functions are not read-write. This points to the first one of them.43 */44 Aggref *aggref;45 46 /*47 * Is this state value actually being shared by more than one Aggref?48 */49 bool aggshared;50 51 /*52 * True for ORDER BY and DISTINCT Aggrefs that are not aggpresorted.53 */54 bool aggsortrequired;55 56 /*57 * Number of aggregated input columns. This includes ORDER BY expressions58 * in both the plain-agg and ordered-set cases. Ordered-set direct args59 * are not counted, though.60 */61 int numInputs;62 63 /*64 * Number of aggregated input columns to pass to the transfn. This65 * includes the ORDER BY columns for ordered-set aggs, but not for plain66 * aggs. (This doesn't count the transition state value!)67 */68 int numTransInputs;69 70 /* Oid of the state transition or combine function */71 Oid transfn_oid;72 73 /* Oid of the serialization function or InvalidOid */74 Oid serialfn_oid;75 76 /* Oid of the deserialization function or InvalidOid */77 Oid deserialfn_oid;78 79 /* Oid of state value's datatype */80 Oid aggtranstype;81 82 /*83 * fmgr lookup data for transition function or combine function. Note in84 * particular that the fn_strict flag is kept here.85 */86 FmgrInfo transfn;87 88 /* fmgr lookup data for serialization function */89 FmgrInfo serialfn;90 91 /* fmgr lookup data for deserialization function */92 FmgrInfo deserialfn;93 94 /* Input collation derived for aggregate */95 Oid aggCollation;96 97 /* number of sorting columns */98 int numSortCols;99 100 /* number of sorting columns to consider in DISTINCT comparisons */101 /* (this is either zero or the same as numSortCols) */102 int numDistinctCols;103 104 /* deconstructed sorting information (arrays of length numSortCols) */105 AttrNumber *sortColIdx;106 Oid *sortOperators;107 Oid *sortCollations;108 bool *sortNullsFirst;109 110 /*111 * Comparators for input columns --- only set/used when aggregate has112 * DISTINCT flag. equalfnOne version is used for single-column113 * comparisons, equalfnMulti for the case of multiple columns.114 */115 FmgrInfo equalfnOne;116 ExprState *equalfnMulti;117 118 /*119 * initial value from pg_aggregate entry120 */121 Datum initValue;122 bool initValueIsNull;123 124 /*125 * We need the len and byval info for the agg's input and transition data126 * types in order to know how to copy/delete values.127 *128 * Note that the info for the input type is used only when handling129 * DISTINCT aggs with just one argument, so there is only one input type.130 */131 int16 inputtypeLen,132 transtypeLen;133 bool inputtypeByVal,134 transtypeByVal;135 136 /*137 * Slots for holding the evaluated input arguments. These are set up138 * during ExecInitAgg() and then used for each input row requiring either139 * FILTER or ORDER BY/DISTINCT processing.140 */141 TupleTableSlot *sortslot; /* current input tuple */142 TupleTableSlot *uniqslot; /* used for multi-column DISTINCT */143 TupleDesc sortdesc; /* descriptor of input tuples */144 Datum lastdatum; /* used for single-column DISTINCT */145 bool lastisnull; /* used for single-column DISTINCT */146 bool haslast; /* got a last value for DISTINCT check */147 148 /*149 * These values are working state that is initialized at the start of an150 * input tuple group and updated for each input tuple.151 *152 * For a simple (non DISTINCT/ORDER BY) aggregate, we just feed the input153 * values straight to the transition function. If it's DISTINCT or154 * requires ORDER BY, we pass the input values into a Tuplesort object;155 * then at completion of the input tuple group, we scan the sorted values,156 * eliminate duplicates if needed, and run the transition function on the157 * rest.158 *159 * We need a separate tuplesort for each grouping set.160 */161 162 Tuplesortstate **sortstates; /* sort objects, if DISTINCT or ORDER BY */163 164 /*165 * This field is a pre-initialized FunctionCallInfo struct used for166 * calling this aggregate's transfn. We save a few cycles per row by not167 * re-initializing the unchanging fields; which isn't much, but it seems168 * worth the extra space consumption.169 */170 FunctionCallInfo transfn_fcinfo;171 172 /* Likewise for serialization and deserialization functions */173 FunctionCallInfo serialfn_fcinfo;174 175 FunctionCallInfo deserialfn_fcinfo;176} AggStatePerTransData;177 178/*179 * AggStatePerAggData - per-aggregate information180 *181 * This contains the information needed to call the final function, to produce182 * a final aggregate result from the state value. If there are multiple183 * identical Aggrefs in the query, they can all share the same per-agg data.184 *185 * These values are set up during ExecInitAgg() and do not change thereafter.186 */187typedef struct AggStatePerAggData188{189 /*190 * Link to an Aggref expr this state value is for.191 *192 * There can be multiple identical Aggref's sharing the same per-agg. This193 * points to the first one of them.194 */195 Aggref *aggref;196 197 /* index to the state value which this agg should use */198 int transno;199 200 /* Optional Oid of final function (may be InvalidOid) */201 Oid finalfn_oid;202 203 /*204 * fmgr lookup data for final function --- only valid when finalfn_oid is205 * not InvalidOid.206 */207 FmgrInfo finalfn;208 209 /*210 * Number of arguments to pass to the finalfn. This is always at least 1211 * (the transition state value) plus any ordered-set direct args. If the212 * finalfn wants extra args then we pass nulls corresponding to the213 * aggregated input columns.214 */215 int numFinalArgs;216 217 /* ExprStates for any direct-argument expressions */218 List *aggdirectargs;219 220 /*221 * We need the len and byval info for the agg's result data type in order222 * to know how to copy/delete values.223 */224 int16 resulttypeLen;225 bool resulttypeByVal;226 227 /*228 * "shareable" is false if this agg cannot share state values with other229 * aggregates because the final function is read-write.230 */231 bool shareable;232} AggStatePerAggData;233 234/*235 * AggStatePerGroupData - per-aggregate-per-group working state236 *237 * These values are working state that is initialized at the start of238 * an input tuple group and updated for each input tuple.239 *240 * In AGG_PLAIN and AGG_SORTED modes, we have a single array of these241 * structs (pointed to by aggstate->pergroup); we re-use the array for242 * each input group, if it's AGG_SORTED mode. In AGG_HASHED mode, the243 * hash table contains an array of these structs for each tuple group.244 *245 * Logically, the sortstate field belongs in this struct, but we do not246 * keep it here for space reasons: we don't support DISTINCT aggregates247 * in AGG_HASHED mode, so there's no reason to use up a pointer field248 * in every entry of the hashtable.249 */250typedef struct AggStatePerGroupData251{252#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE 0253 Datum transValue; /* current transition value */254#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL 1255 bool transValueIsNull;256 257#define FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE 2258 bool noTransValue; /* true if transValue not set yet */259 260 /*261 * Note: noTransValue initially has the same value as transValueIsNull,262 * and if true both are cleared to false at the same time. They are not263 * the same though: if transfn later returns a NULL, we want to keep that264 * NULL and not auto-replace it with a later input value. Only the first265 * non-NULL input will be auto-substituted.266 */267} AggStatePerGroupData;268 269/*270 * AggStatePerPhaseData - per-grouping-set-phase state271 *272 * Grouping sets are divided into "phases", where a single phase can be273 * processed in one pass over the input. If there is more than one phase, then274 * at the end of input from the current phase, state is reset and another pass275 * taken over the data which has been re-sorted in the mean time.276 *277 * Accordingly, each phase specifies a list of grouping sets and group clause278 * information, plus each phase after the first also has a sort order.279 */280typedef struct AggStatePerPhaseData281{282 AggStrategy aggstrategy; /* strategy for this phase */283 int numsets; /* number of grouping sets (or 0) */284 int *gset_lengths; /* lengths of grouping sets */285 Bitmapset **grouped_cols; /* column groupings for rollup */286 ExprState **eqfunctions; /* expression returning equality, indexed by287 * nr of cols to compare */288 Agg *aggnode; /* Agg node for phase data */289 Sort *sortnode; /* Sort node for input ordering for phase */290 291 ExprState *evaltrans; /* evaluation of transition functions */292 293 /*----------294 * Cached variants of the compiled expression.295 * first subscript: 0: outerops; 1: TTSOpsMinimalTuple296 * second subscript: 0: no NULL check; 1: with NULL check297 *----------298 */299 ExprState *evaltrans_cache[2][2];300} AggStatePerPhaseData;301 302/*303 * AggStatePerHashData - per-hashtable state304 *305 * When doing grouping sets with hashing, we have one of these for each306 * grouping set. (When doing hashing without grouping sets, we have just one of307 * them.)308 */309typedef struct AggStatePerHashData310{311 TupleHashTable hashtable; /* hash table with one entry per group */312 TupleHashIterator hashiter; /* for iterating through hash table */313 TupleTableSlot *hashslot; /* slot for loading hash table */314 FmgrInfo *hashfunctions; /* per-grouping-field hash fns */315 Oid *eqfuncoids; /* per-grouping-field equality fns */316 int numCols; /* number of hash key columns */317 int numhashGrpCols; /* number of columns in hash table */318 int largestGrpColIdx; /* largest col required for hashing */319 AttrNumber *hashGrpColIdxInput; /* hash col indices in input slot */320 AttrNumber *hashGrpColIdxHash; /* indices in hash table tuples */321 Agg *aggnode; /* original Agg node, for numGroups etc. */322} AggStatePerHashData;323 324 325extern AggState *ExecInitAgg(Agg *node, EState *estate, int eflags);326extern void ExecEndAgg(AggState *node);327extern void ExecReScanAgg(AggState *node);328 329extern Size hash_agg_entry_size(int numTrans, Size tupleWidth,330 Size transitionSpace);331extern void hash_agg_set_limits(double hashentrysize, double input_groups,332 int used_bits, Size *mem_limit,333 uint64 *ngroups_limit, int *num_partitions);334 335/* parallel instrumentation support */336extern void ExecAggEstimate(AggState *node, ParallelContext *pcxt);337extern void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt);338extern void ExecAggInitializeWorker(AggState *node, ParallelWorkerContext *pwcxt);339extern void ExecAggRetrieveInstrumentation(AggState *node);340 341#endif /* NODEAGG_H */342 