gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
10KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * nodeAgg.h
  4. * prototypes for nodeAgg.c
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/executor/nodeAgg.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef NODEAGG_H
  15. #define NODEAGG_H
  16. #include "nodes/execnodes.h"
  17. /*
  18. * AggStatePerTransData - per aggregate state value information
  19. *
  20. * Working state for updating the aggregate's state value, by calling the
  21. * transition function with an input row. This struct does not store the
  22. * information needed to produce the final aggregate result from the transition
  23. * state, that's stored in AggStatePerAggData instead. This separation allows
  24. * multiple aggregate results to be produced from a single state value.
  25. */
  26. typedef struct AggStatePerTransData
  27. {
  28. /*
  29. * These values are set up during ExecInitAgg() and do not change
  30. * thereafter:
  31. */
  32. /*
  33. * Link to an Aggref expr this state value is for.
  34. *
  35. * There can be multiple Aggref's sharing the same state value, so long as
  36. * the inputs and transition functions are identical and the final
  37. * functions are not read-write. This points to the first one of them.
  38. */
  39. Aggref *aggref;
  40. /*
  41. * Is this state value actually being shared by more than one Aggref?
  42. */
  43. bool aggshared;
  44. /*
  45. * Number of aggregated input columns. This includes ORDER BY expressions
  46. * in both the plain-agg and ordered-set cases. Ordered-set direct args
  47. * are not counted, though.
  48. */
  49. int numInputs;
  50. /*
  51. * Number of aggregated input columns to pass to the transfn. This
  52. * includes the ORDER BY columns for ordered-set aggs, but not for plain
  53. * aggs. (This doesn't count the transition state value!)
  54. */
  55. int numTransInputs;
  56. /* Oid of the state transition or combine function */
  57. Oid transfn_oid;
  58. /* Oid of the serialization function or InvalidOid */
  59. Oid serialfn_oid;
  60. /* Oid of the deserialization function or InvalidOid */
  61. Oid deserialfn_oid;
  62. /* Oid of state value's datatype */
  63. Oid aggtranstype;
  64. /*
  65. * fmgr lookup data for transition function or combine function. Note in
  66. * particular that the fn_strict flag is kept here.
  67. */
  68. FmgrInfo transfn;
  69. /* fmgr lookup data for serialization function */
  70. FmgrInfo serialfn;
  71. /* fmgr lookup data for deserialization function */
  72. FmgrInfo deserialfn;
  73. /* Input collation derived for aggregate */
  74. Oid aggCollation;
  75. /* number of sorting columns */
  76. int numSortCols;
  77. /* number of sorting columns to consider in DISTINCT comparisons */
  78. /* (this is either zero or the same as numSortCols) */
  79. int numDistinctCols;
  80. /* deconstructed sorting information (arrays of length numSortCols) */
  81. AttrNumber *sortColIdx;
  82. Oid *sortOperators;
  83. Oid *sortCollations;
  84. bool *sortNullsFirst;
  85. /*
  86. * Comparators for input columns --- only set/used when aggregate has
  87. * DISTINCT flag. equalfnOne version is used for single-column
  88. * comparisons, equalfnMulti for the case of multiple columns.
  89. */
  90. FmgrInfo equalfnOne;
  91. ExprState *equalfnMulti;
  92. /*
  93. * initial value from pg_aggregate entry
  94. */
  95. Datum initValue;
  96. bool initValueIsNull;
  97. /*
  98. * We need the len and byval info for the agg's input and transition data
  99. * types in order to know how to copy/delete values.
  100. *
  101. * Note that the info for the input type is used only when handling
  102. * DISTINCT aggs with just one argument, so there is only one input type.
  103. */
  104. int16 inputtypeLen,
  105. transtypeLen;
  106. bool inputtypeByVal,
  107. transtypeByVal;
  108. /*
  109. * Slots for holding the evaluated input arguments. These are set up
  110. * during ExecInitAgg() and then used for each input row requiring either
  111. * FILTER or ORDER BY/DISTINCT processing.
  112. */
  113. TupleTableSlot *sortslot; /* current input tuple */
  114. TupleTableSlot *uniqslot; /* used for multi-column DISTINCT */
  115. TupleDesc sortdesc; /* descriptor of input tuples */
  116. /*
  117. * These values are working state that is initialized at the start of an
  118. * input tuple group and updated for each input tuple.
  119. *
  120. * For a simple (non DISTINCT/ORDER BY) aggregate, we just feed the input
  121. * values straight to the transition function. If it's DISTINCT or
  122. * requires ORDER BY, we pass the input values into a Tuplesort object;
  123. * then at completion of the input tuple group, we scan the sorted values,
  124. * eliminate duplicates if needed, and run the transition function on the
  125. * rest.
  126. *
  127. * We need a separate tuplesort for each grouping set.
  128. */
  129. Tuplesortstate **sortstates; /* sort objects, if DISTINCT or ORDER BY */
  130. /*
  131. * This field is a pre-initialized FunctionCallInfo struct used for
  132. * calling this aggregate's transfn. We save a few cycles per row by not
  133. * re-initializing the unchanging fields; which isn't much, but it seems
  134. * worth the extra space consumption.
  135. */
  136. FunctionCallInfo transfn_fcinfo;
  137. /* Likewise for serialization and deserialization functions */
  138. FunctionCallInfo serialfn_fcinfo;
  139. FunctionCallInfo deserialfn_fcinfo;
  140. } AggStatePerTransData;
  141. /*
  142. * AggStatePerAggData - per-aggregate information
  143. *
  144. * This contains the information needed to call the final function, to produce
  145. * a final aggregate result from the state value. If there are multiple
  146. * identical Aggrefs in the query, they can all share the same per-agg data.
  147. *
  148. * These values are set up during ExecInitAgg() and do not change thereafter.
  149. */
  150. typedef struct AggStatePerAggData
  151. {
  152. /*
  153. * Link to an Aggref expr this state value is for.
  154. *
  155. * There can be multiple identical Aggref's sharing the same per-agg. This
  156. * points to the first one of them.
  157. */
  158. Aggref *aggref;
  159. /* index to the state value which this agg should use */
  160. int transno;
  161. /* Optional Oid of final function (may be InvalidOid) */
  162. Oid finalfn_oid;
  163. /*
  164. * fmgr lookup data for final function --- only valid when finalfn_oid is
  165. * not InvalidOid.
  166. */
  167. FmgrInfo finalfn;
  168. /*
  169. * Number of arguments to pass to the finalfn. This is always at least 1
  170. * (the transition state value) plus any ordered-set direct args. If the
  171. * finalfn wants extra args then we pass nulls corresponding to the
  172. * aggregated input columns.
  173. */
  174. int numFinalArgs;
  175. /* ExprStates for any direct-argument expressions */
  176. List *aggdirectargs;
  177. /*
  178. * We need the len and byval info for the agg's result data type in order
  179. * to know how to copy/delete values.
  180. */
  181. int16 resulttypeLen;
  182. bool resulttypeByVal;
  183. /*
  184. * "shareable" is false if this agg cannot share state values with other
  185. * aggregates because the final function is read-write.
  186. */
  187. bool shareable;
  188. } AggStatePerAggData;
  189. /*
  190. * AggStatePerGroupData - per-aggregate-per-group working state
  191. *
  192. * These values are working state that is initialized at the start of
  193. * an input tuple group and updated for each input tuple.
  194. *
  195. * In AGG_PLAIN and AGG_SORTED modes, we have a single array of these
  196. * structs (pointed to by aggstate->pergroup); we re-use the array for
  197. * each input group, if it's AGG_SORTED mode. In AGG_HASHED mode, the
  198. * hash table contains an array of these structs for each tuple group.
  199. *
  200. * Logically, the sortstate field belongs in this struct, but we do not
  201. * keep it here for space reasons: we don't support DISTINCT aggregates
  202. * in AGG_HASHED mode, so there's no reason to use up a pointer field
  203. * in every entry of the hashtable.
  204. */
  205. typedef struct AggStatePerGroupData
  206. {
  207. #define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE 0
  208. Datum transValue; /* current transition value */
  209. #define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL 1
  210. bool transValueIsNull;
  211. #define FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE 2
  212. bool noTransValue; /* true if transValue not set yet */
  213. /*
  214. * Note: noTransValue initially has the same value as transValueIsNull,
  215. * and if true both are cleared to false at the same time. They are not
  216. * the same though: if transfn later returns a NULL, we want to keep that
  217. * NULL and not auto-replace it with a later input value. Only the first
  218. * non-NULL input will be auto-substituted.
  219. */
  220. } AggStatePerGroupData;
  221. /*
  222. * AggStatePerPhaseData - per-grouping-set-phase state
  223. *
  224. * Grouping sets are divided into "phases", where a single phase can be
  225. * processed in one pass over the input. If there is more than one phase, then
  226. * at the end of input from the current phase, state is reset and another pass
  227. * taken over the data which has been re-sorted in the mean time.
  228. *
  229. * Accordingly, each phase specifies a list of grouping sets and group clause
  230. * information, plus each phase after the first also has a sort order.
  231. */
  232. typedef struct AggStatePerPhaseData
  233. {
  234. AggStrategy aggstrategy; /* strategy for this phase */
  235. int numsets; /* number of grouping sets (or 0) */
  236. int *gset_lengths; /* lengths of grouping sets */
  237. Bitmapset **grouped_cols; /* column groupings for rollup */
  238. ExprState **eqfunctions; /* expression returning equality, indexed by
  239. * nr of cols to compare */
  240. Agg *aggnode; /* Agg node for phase data */
  241. Sort *sortnode; /* Sort node for input ordering for phase */
  242. ExprState *evaltrans; /* evaluation of transition functions */
  243. } AggStatePerPhaseData;
  244. /*
  245. * AggStatePerHashData - per-hashtable state
  246. *
  247. * When doing grouping sets with hashing, we have one of these for each
  248. * grouping set. (When doing hashing without grouping sets, we have just one of
  249. * them.)
  250. */
  251. typedef struct AggStatePerHashData
  252. {
  253. TupleHashTable hashtable; /* hash table with one entry per group */
  254. TupleHashIterator hashiter; /* for iterating through hash table */
  255. TupleTableSlot *hashslot; /* slot for loading hash table */
  256. FmgrInfo *hashfunctions; /* per-grouping-field hash fns */
  257. Oid *eqfuncoids; /* per-grouping-field equality fns */
  258. int numCols; /* number of hash key columns */
  259. int numhashGrpCols; /* number of columns in hash table */
  260. int largestGrpColIdx; /* largest col required for hashing */
  261. AttrNumber *hashGrpColIdxInput; /* hash col indices in input slot */
  262. AttrNumber *hashGrpColIdxHash; /* indices in hashtbl tuples */
  263. Agg *aggnode; /* original Agg node, for numGroups etc. */
  264. } AggStatePerHashData;
  265. extern AggState *ExecInitAgg(Agg *node, EState *estate, int eflags);
  266. extern void ExecEndAgg(AggState *node);
  267. extern void ExecReScanAgg(AggState *node);
  268. extern Size hash_agg_entry_size(int numAggs);
  269. extern Datum aggregate_dummy(PG_FUNCTION_ARGS);
  270. #endif /* NODEAGG_H */
上海开阖软件有限公司 沪ICP备12045867号-1