From 07044c43c9c8872b1ce7ec49fbb8b36927925243 Mon Sep 17 00:00:00 2001 From: Nikita Klimenko Date: Tue, 29 Apr 2025 20:03:23 +0300 Subject: [PATCH] Migrate usages of AccessOverload APIs in inline functions to public alternatives --- core/api/core.api | 1 + .../org/jetbrains/kotlinx/dataframe/api/into.kt | 14 +++++++++++++- .../jetbrains/kotlinx/dataframe/api/toDataFrame.kt | 2 +- .../org/jetbrains/kotlinx/dataframe/api/with.kt | 2 +- .../dataframe/impl/aggregation/aggregations.kt | 2 +- .../impl/aggregation/modes/aggregateBy.kt | 2 +- .../dataframe/testSets/person/DataFrameTests.kt | 3 ++- .../dataframe/testSets/person/PivotTests.kt | 9 ++++++++- 8 files changed, 28 insertions(+), 7 deletions(-) diff --git a/core/api/core.api b/core/api/core.api index ba0a430a1e..b585b69cc4 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -2548,6 +2548,7 @@ public final class org/jetbrains/kotlinx/dataframe/api/IntoKt { public static final fun into (Lorg/jetbrains/kotlinx/dataframe/api/GroupBy;Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame; public static final fun into (Lorg/jetbrains/kotlinx/dataframe/api/GroupBy;Lkotlin/reflect/KProperty;)Lorg/jetbrains/kotlinx/dataframe/DataFrame; public static final fun into (Lorg/jetbrains/kotlinx/dataframe/api/GroupBy;Lorg/jetbrains/kotlinx/dataframe/columns/ColumnAccessor;)Lorg/jetbrains/kotlinx/dataframe/DataFrame; + public static final fun into (Lorg/jetbrains/kotlinx/dataframe/api/GroupBy;Lorg/jetbrains/kotlinx/dataframe/columns/ColumnPath;Lkotlin/jvm/functions/Function2;Lkotlin/reflect/KType;)Lorg/jetbrains/kotlinx/dataframe/DataFrame; public static final fun into (Lorg/jetbrains/kotlinx/dataframe/api/ReducedGroupBy;Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame; public static final fun into (Lorg/jetbrains/kotlinx/dataframe/api/ReducedGroupBy;Lkotlin/reflect/KProperty;)Lorg/jetbrains/kotlinx/dataframe/DataFrame; public static final fun into (Lorg/jetbrains/kotlinx/dataframe/api/ReducedGroupBy;Lorg/jetbrains/kotlinx/dataframe/columns/ColumnAccessor;)Lorg/jetbrains/kotlinx/dataframe/DataFrame; diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/into.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/into.kt index 7965f3317a..a1d1a78b1a 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/into.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/into.kt @@ -8,10 +8,12 @@ import org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload import org.jetbrains.kotlinx.dataframe.annotations.Interpretable import org.jetbrains.kotlinx.dataframe.annotations.Refine import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor +import org.jetbrains.kotlinx.dataframe.columns.ColumnPath import org.jetbrains.kotlinx.dataframe.impl.aggregation.internal import org.jetbrains.kotlinx.dataframe.impl.aggregation.withExpr import org.jetbrains.kotlinx.dataframe.impl.columnName import kotlin.reflect.KProperty +import kotlin.reflect.KType import kotlin.reflect.typeOf // region GroupBy @@ -29,7 +31,7 @@ public fun GroupBy.into(column: KProperty): DataFrame = t public inline fun GroupBy.into( columnName: String? = null, noinline expression: RowExpression, -): DataFrame = into(pathOf(columnName ?: groups.name()).cast(), expression) +): DataFrame = into(pathOf(columnName ?: groups.name()), expression, typeOf()) // @Hide @AccessApiOverload @@ -44,6 +46,16 @@ public inline fun GroupBy.into( } } +@PublishedApi +internal fun GroupBy.into( + path: ColumnPath, + expression: RowExpression, + type: KType, +): DataFrame = + aggregate { + internal().withExpr(type, path, expression) + } + @AccessApiOverload public inline fun GroupBy.into( column: KProperty, diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt index 1aab859a76..0f9ec60b62 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt @@ -31,7 +31,7 @@ public inline fun Iterable.toDataFrame(): DataFrame = // or has no properties if (!T::class.canBeUnfolded) { // create a single `value` column - ValueProperty::value from { it } + ValueProperty::value.name from { it } } else { // otherwise creates columns based on properties properties() diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/with.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/with.kt index bb039e2b98..190d0cfe7e 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/with.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/with.kt @@ -44,7 +44,7 @@ public inline fun ReducedPivotGroupBy.with(noinline expression val value = reducer(this)?.let { val value = expression(it, it) if (value is AnyColumnReference) { - it[value] + value.getValue(it) } else { value } diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregations.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregations.kt index 67809f6da7..84b83801aa 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregations.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/aggregations.kt @@ -95,7 +95,7 @@ internal fun AggregateInternalDsl.withExpr(type: KType, path: ColumnPa val values = df.rows().map { val value = expression(it, it) if (value is AnyColumnReference) { - it[value] + value.getValue(it) } else { value } diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/modes/aggregateBy.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/modes/aggregateBy.kt index ebf10a5fd8..77cf7848da 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/modes/aggregateBy.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/modes/aggregateBy.kt @@ -112,7 +112,7 @@ internal inline fun Aggregator.aggregateByOrNu ): DataRow? = data.getOrNull( indexOfAggregationResult( - values = data.asSequence().map { it[column] }, + values = data.asSequence().map { column.getValue(it) }, valueType = typeOf(), ), ) diff --git a/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt b/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt index a13b51a782..5a4ea0d43c 100644 --- a/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt +++ b/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt @@ -1176,8 +1176,9 @@ class DataFrameTests : BaseTest() { .split { others }.intoRows() .add(sum) { name.length + other().length } - val matrix = src.pivot { other }.groupBy { name }.with { sum } + val matrix = src.pivot { other }.groupBy { name }.with { sum() } matrix.getColumnGroup(other.name()).ncol shouldBe names.size + matrix.getColumnGroup(other.name())["Alice"].type() shouldBe typeOf>() } @Test diff --git a/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/PivotTests.kt b/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/PivotTests.kt index ddbd57f368..84be5b6027 100644 --- a/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/PivotTests.kt +++ b/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/PivotTests.kt @@ -161,6 +161,13 @@ class PivotTests { .groupBy { name } .default("-") .values { value } shouldBe res + typed + .pivot { key } + .groupBy { name } + .default("-") + .with { value() } shouldBe res + + // special case that looks very odd, but kept it for compatibility typed .pivot { key } .groupBy { name } @@ -180,7 +187,7 @@ class PivotTests { .pivot(key) .groupBy(name) .default("-") - .with { value } shouldBe res + .with { value() } shouldBe res typed .groupBy { name } .pivot { key }