@@ -25,63 +25,83 @@ internal interface Aggregator<in Value, out Return> {
2525    /* *
2626     * Base function of [Aggregator]. 
2727     * 
28-      * Aggregates the given values, taking [type ] into account, 
28+      * Aggregates the given values, taking [valueType ] into account, 
2929     * filtering nulls (only if [type.isMarkedNullable][KType.isMarkedNullable]), 
3030     * and computes a single resulting value. 
3131     * 
32-      * When using [AggregatorBase], this can be supplied by the [AggregatorBase.aggregator ] argument. 
32+      * When using [AggregatorBase], this can be supplied by the [AggregatorBase.aggregateSingle ] argument. 
3333     * 
34-      * When the exact [type ] is unknown, use [aggregateCalculatingType ]. 
34+      * When the exact [valueType ] is unknown, use [aggregateCalculatingValueType ]. 
3535     */  
36-     fun  aggregate (values :  Iterable <Value ?>, type :  KType ): Return 
36+     fun  aggregateSingleIterable (values :  Iterable <Value ?>, valueType :  KType ): Return 
37+ 
38+     fun  calculateValueType (valueTypes :  Set <KType >): KType 
39+ 
40+     fun  calculateValueType (values :  Iterable <Value ?>): KType 
3741
3842    /* *
3943     * Aggregates the data in the given column and computes a single resulting value. 
40-      * Calls [aggregate ] (with [Iterable] and [KType]). 
44+      * Calls [aggregateSingleColumn ] (with [Iterable] and [KType]). 
4145     * 
42-      * See [AggregatorBase.aggregate ]. 
46+      * See [AggregatorBase.aggregateSingleIterable ]. 
4347     */  
44-     fun  aggregate (column :  DataColumn <Value ?>): Return 
48+     fun  aggregateSingleColumn (column :  DataColumn <Value ?>): Return 
4549
4650    /* *
4751     * Aggregates the data in the multiple given columns and computes a single resulting value. 
4852     */  
49-     fun  aggregate (columns :  Iterable <DataColumn <Value ?>>): Return 
53+     fun  aggregateMultipleColumns (columns :  Iterable <DataColumn <Value ?>>): Return 
5054
5155    /* *
52-      * Special case of [aggregate] with [Iterable] that calculates the common type of the values at runtime. 
53-      * Without [valueTypes], this is a heavy operation and should be avoided when possible. 
54-      * 
55-      * @param values The values to be aggregated. 
56-      * @param valueTypes The types of the values. 
57-      *   If provided, this can be used to avoid calculating the types of [values] at runtime with reflection. 
58-      *   It should contain all types of [values]. 
59-      *   If `null` or empty, the types of [values] will be calculated at runtime (heavy!). 
60-      */  
61-     fun  aggregateCalculatingType (values :  Iterable <Value ?>, valueTypes :  Set <KType >?  = null): Return 
62- 
63-     /* *
64-      * Function that can give the return type of [aggregate] as [KType], given the type of the input. 
56+      * Function that can give the return type of [aggregateSingleIterable] as [KType], given the type of the input. 
6557     * This allows aggregators to avoid runtime type calculations. 
6658     * 
6759     * @param type The type of the input values. 
6860     * @param emptyInput If `true`, the input values are considered empty. This often affects the return type. 
69-      * @return The return type of [aggregate ] as [KType]. 
61+      * @return The return type of [aggregateSingleIterable ] as [KType]. 
7062     */  
7163    fun  calculateReturnTypeOrNull (type :  KType , emptyInput :  Boolean ): KType ? 
7264
7365    /* *
74-      * Function that can give the return type of [aggregate ] with columns as [KType], 
66+      * Function that can give the return type of [aggregateSingleIterable ] with columns as [KType], 
7567     * given the multiple types of the input. 
7668     * This allows aggregators to avoid runtime type calculations. 
7769     * 
7870     * @param colTypes The types of the input columns. 
7971     * @param colsEmpty If `true`, all the input columns are considered empty. This often affects the return type. 
80-      * @return The return type of [aggregate ] as [KType]. 
72+      * @return The return type of [aggregateSingleIterable ] as [KType]. 
8173     */  
82-     fun  calculateReturnTypeOrNull (colTypes :  Set <KType >, colsEmpty :  Boolean ): KType ? 
74+     fun  calculateReturnTypeMultipleColumnsOrNull (colTypes :  Set <KType >, colsEmpty :  Boolean ): KType ? 
75+ 
76+     val  ref:  Aggregator <Value , Return > get() =  this 
8377}
8478
79+ internal  fun  <Value , Return > Aggregator <Value , Return >.aggregate (values :  Iterable <Value ?>, valueType :  KType ) = 
80+     aggregateSingleIterable(values, valueType)
81+ 
82+ internal  fun  <Value , Return > Aggregator <Value , Return >.calculateValueType (
83+     values :  Iterable <Value ?>,
84+     valueTypes :  Set <KType >?  = null,
85+ ) =  if  (valueTypes !=  null  &&  valueTypes.isNotEmpty()) {
86+     calculateValueType(valueTypes)
87+ } else  {
88+     calculateValueType(values)
89+ }
90+ 
91+ internal  fun  <Value , Return > Aggregator <Value , Return >.aggregateCalculatingValueType (
92+     values :  Iterable <Value ?>,
93+     valueTypes :  Set <KType >?  = null,
94+ ) =  aggregateSingleIterable(
95+     values =  values,
96+     valueType =  calculateValueType(values, valueTypes),
97+ )
98+ 
99+ internal  fun  <Value , Return > Aggregator <Value , Return >.aggregate (column :  DataColumn <Value ?>) = 
100+     aggregateSingleColumn(column)
101+ 
102+ internal  fun  <Value , Return > Aggregator <Value , Return >.aggregate (columns :  Iterable <DataColumn <Value ?>>) = 
103+     aggregateMultipleColumns(columns)
104+ 
85105@Suppress(" UNCHECKED_CAST" 
86106@PublishedApi
87107internal  fun  <Type > Aggregator <* , * >.cast (): Aggregator <Type , Type > =  this  as  Aggregator <Type , Type >
@@ -90,15 +110,18 @@ internal fun <Type> Aggregator<*, *>.cast(): Aggregator<Type, Type> = this as Ag
90110@PublishedApi
91111internal  fun  <Value , Return > Aggregator <* , * >.cast2 (): Aggregator <Value , Return > =  this  as  Aggregator <Value , Return >
92112
93- /* * Type alias for [Aggregator.calculateReturnTypeOrNull ] */ 
113+ /* * Type alias for [Aggregator.calculateReturnTypeMultipleColumnsOrNull ] */ 
94114internal  typealias  CalculateReturnTypeOrNull  =  (type:  KType , emptyInput:  Boolean ) ->  KType ? 
95115
96116/* *
97-  * Type alias for the argument for [Aggregator.aggregate ]. 
117+  * Type alias for the argument for [Aggregator.aggregateSingleIterable ]. 
98118 * Nulls have already been filtered out when this argument is called. 
99119 */  
100120internal  typealias  Aggregate <Value , Return > =  Iterable <Value  & Any >.(type:  KType ) ->  Return 
101121
122+ internal  typealias  AggregateBy <Source , Value , Return > = 
123+     Iterable <Source >.(sourceType:  KType , valueType:  KType , selector:  (Source ) ->  Value ) ->  Return 
124+ 
102125/* * Common case for [CalculateReturnTypeOrNull], preserves return type, but makes it nullable for empty inputs. */ 
103126internal  val  preserveReturnTypeNullIfEmpty:  CalculateReturnTypeOrNull  =  { type, emptyInput -> 
104127    type.withNullability(emptyInput)
0 commit comments