-
Notifications
You must be signed in to change notification settings - Fork 71
Median overhaul #1122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Median overhaul #1122
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b3363ff
small extra type conversion check for min/max
Jolanrensen f6d3309
initial rework of median without percentile
Jolanrensen fc88cfc
wip median
Jolanrensen c40e642
Merge branch 'master' into median
Jolanrensen 2e86d2f
updated from master
Jolanrensen 3f43197
overhauling median
Jolanrensen 817d570
stuck on issue KT-76683
Jolanrensen 9b356c8
added medianBy overloads
Jolanrensen 8effc33
added median tests, added HybridAggregationHandler for median, median…
Jolanrensen da07f3b
Merge branch 'master' into median
Jolanrensen 1082586
Merge branch 'master' into median
Jolanrensen c889f30
fixed tests aside from the compiler plugin
Jolanrensen fe5c4a9
ignoring testGroupBy_median test for now
Jolanrensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
522 changes: 411 additions & 111 deletions
522
core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/median.kt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...nx/dataframe/impl/aggregation/aggregators/aggregationHandlers/HybridAggregationHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.aggregationHandlers | ||
|
||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.Aggregator | ||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.AggregatorAggregationHandler | ||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.CalculateReturnType | ||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.IndexOfResult | ||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.Reducer | ||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.ValueType | ||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.aggregateCalculatingValueType | ||
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.calculateValueType | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.full.withNullability | ||
|
||
/** | ||
* Implementation of [AggregatorAggregationHandler] which functions like a selector ánd reducer: | ||
* it takes a sequence of values and returns a single value, which is likely part of the input, but not necessarily. | ||
* | ||
* In practice, this means the handler implements both [indexOfAggregationResultSingleSequence] | ||
* (meaning it can give an index of the result in the input), and [aggregateSequence] with a return type that is | ||
* potentially different from the input. | ||
* The return value of [aggregateSequence] and the value at the index retrieved from [indexOfAggregationResultSingleSequence] | ||
* may differ. | ||
* | ||
* @param reducer This function actually does the selection/reduction. | ||
* Before it is called, nulls are filtered out. The type of the values is passed as [KType] to the selector. | ||
* @param indexOfResult This function must be supplied to give the index of the result in the input values. | ||
* @param getReturnType This function must be supplied to give the return type of [reducer] given some input type and | ||
* whether the input is empty. | ||
* When selecting, the return type is always `typeOf<Value>()` or `typeOf<Value?>()`, when reducing it can be anything. | ||
* @see [ReducingAggregationHandler] | ||
*/ | ||
internal class HybridAggregationHandler<in Value : Any, out Return : Any?>( | ||
val reducer: Reducer<Value, Return>, | ||
val indexOfResult: IndexOfResult<Value>, | ||
val getReturnType: CalculateReturnType, | ||
) : AggregatorAggregationHandler<Value, Return> { | ||
|
||
/** | ||
* Function that can give the index of the aggregation result in the input [values]. | ||
* Calls the supplied [indexOfResult] after preprocessing the input. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
override fun indexOfAggregationResultSingleSequence(values: Sequence<Value?>, valueType: ValueType): Int { | ||
val (values, valueType) = aggregator!!.preprocessAggregation(values, valueType) | ||
return indexOfResult(values, valueType) | ||
} | ||
|
||
/** | ||
* Base function of [Aggregator]. | ||
* | ||
* Aggregates the given values, taking [valueType] into account, | ||
* filtering nulls (only if [valueType.type.isMarkedNullable][KType.isMarkedNullable]), | ||
* and computes a single resulting value. | ||
* | ||
* When the exact [valueType] is unknown, use [calculateValueType] or [aggregateCalculatingValueType]. | ||
* | ||
* Calls the supplied [reducer]. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
override fun aggregateSequence(values: Sequence<Value?>, valueType: ValueType): Return { | ||
val (values, valueType) = aggregator!!.preprocessAggregation(values, valueType) | ||
return reducer( | ||
// values = | ||
if (valueType.isMarkedNullable) { | ||
values.filterNotNull() | ||
} else { | ||
values as Sequence<Value> | ||
}, | ||
// type = | ||
valueType.withNullability(false), | ||
) | ||
} | ||
|
||
/** | ||
* Give the return type of [reducer] given some input type and whether the input is empty. | ||
* Calls the supplied [getReturnType]. | ||
*/ | ||
override fun calculateReturnType(valueType: KType, emptyInput: Boolean): KType = | ||
getReturnType(valueType.withNullability(false), emptyInput) | ||
|
||
override var aggregator: Aggregator<@UnsafeVariance Value, @UnsafeVariance Return>? = null | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about
crossinline
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crossline
is more efficient thannoinline
and unfortunately we cannot do it without, so yes.