|
| 1 | +package org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.aggregationHandlers |
| 2 | + |
| 3 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.Aggregator |
| 4 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.AggregatorAggregationHandler |
| 5 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.CalculateReturnType |
| 6 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.IndexOfResult |
| 7 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.Reducer |
| 8 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.ValueType |
| 9 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.aggregateCalculatingValueType |
| 10 | +import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.calculateValueType |
| 11 | +import kotlin.reflect.KType |
| 12 | +import kotlin.reflect.full.withNullability |
| 13 | + |
| 14 | +/** |
| 15 | + * Implementation of [AggregatorAggregationHandler] which functions like a selector ánd reducer: |
| 16 | + * it takes a sequence of values and returns a single value, which is likely part of the input, but not necessarily. |
| 17 | + * |
| 18 | + * In practice, this means the handler implements both [indexOfAggregationResultSingleSequence] |
| 19 | + * (meaning it can give an index of the result in the input), and [aggregateSequence] with a return type that is |
| 20 | + * potentially different from the input. |
| 21 | + * The return value of [aggregateSequence] and the value at the index retrieved from [indexOfAggregationResultSingleSequence] |
| 22 | + * may differ. |
| 23 | + * |
| 24 | + * @param reducer This function actually does the selection/reduction. |
| 25 | + * Before it is called, nulls are filtered out. The type of the values is passed as [KType] to the selector. |
| 26 | + * @param indexOfResult This function must be supplied to give the index of the result in the input values. |
| 27 | + * @param getReturnType This function must be supplied to give the return type of [reducer] given some input type and |
| 28 | + * whether the input is empty. |
| 29 | + * When selecting, the return type is always `typeOf<Value>()` or `typeOf<Value?>()`, when reducing it can be anything. |
| 30 | + * @see [ReducingAggregationHandler] |
| 31 | + */ |
| 32 | +internal class HybridAggregationHandler<in Value : Any, out Return : Any?>( |
| 33 | + val reducer: Reducer<Value, Return>, |
| 34 | + val indexOfResult: IndexOfResult<Value>, |
| 35 | + val getReturnType: CalculateReturnType, |
| 36 | +) : AggregatorAggregationHandler<Value, Return> { |
| 37 | + |
| 38 | + /** |
| 39 | + * Function that can give the index of the aggregation result in the input [values]. |
| 40 | + * Calls the supplied [indexOfResult] after preprocessing the input. |
| 41 | + */ |
| 42 | + @Suppress("UNCHECKED_CAST") |
| 43 | + override fun indexOfAggregationResultSingleSequence(values: Sequence<Value?>, valueType: ValueType): Int { |
| 44 | + val (values, valueType) = aggregator!!.preprocessAggregation(values, valueType) |
| 45 | + return indexOfResult(values, valueType) |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Base function of [Aggregator]. |
| 50 | + * |
| 51 | + * Aggregates the given values, taking [valueType] into account, |
| 52 | + * filtering nulls (only if [valueType.type.isMarkedNullable][KType.isMarkedNullable]), |
| 53 | + * and computes a single resulting value. |
| 54 | + * |
| 55 | + * When the exact [valueType] is unknown, use [calculateValueType] or [aggregateCalculatingValueType]. |
| 56 | + * |
| 57 | + * Calls the supplied [reducer]. |
| 58 | + */ |
| 59 | + @Suppress("UNCHECKED_CAST") |
| 60 | + override fun aggregateSequence(values: Sequence<Value?>, valueType: ValueType): Return { |
| 61 | + val (values, valueType) = aggregator!!.preprocessAggregation(values, valueType) |
| 62 | + return reducer( |
| 63 | + // values = |
| 64 | + if (valueType.isMarkedNullable) { |
| 65 | + values.filterNotNull() |
| 66 | + } else { |
| 67 | + values as Sequence<Value> |
| 68 | + }, |
| 69 | + // type = |
| 70 | + valueType.withNullability(false), |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Give the return type of [reducer] given some input type and whether the input is empty. |
| 76 | + * Calls the supplied [getReturnType]. |
| 77 | + */ |
| 78 | + override fun calculateReturnType(valueType: KType, emptyInput: Boolean): KType = |
| 79 | + getReturnType(valueType.withNullability(false), emptyInput) |
| 80 | + |
| 81 | + override var aggregator: Aggregator<@UnsafeVariance Value, @UnsafeVariance Return>? = null |
| 82 | +} |
0 commit comments