Skip to content

Commit 835ff95

Browse files
committed
attempt to fix #1221 with simple cast, unsufficient for renaming, #663 handles it better
1 parent f671778 commit 835ff95

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/cast.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@ public fun <C> TransformableColumnSet<*>.cast(): TransformableColumnSet<C> = thi
9999
public fun <C> TransformableSingleColumn<*>.cast(): TransformableSingleColumn<C> = this as TransformableSingleColumn<C>
100100

101101
public fun <C> ColumnReference<*>.cast(): ColumnReference<C> = this as ColumnReference<C>
102+
103+
public fun <T> GroupBy<*, *>.cast(): GroupBy<T, T> = this as GroupBy<T, T>

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/groupBy.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,5 @@ public class ReducedGroupBy<T, G>(
115115
}
116116

117117
@PublishedApi
118-
internal fun <T, G> GroupBy<T, G>.reduce(reducer: Selector<DataFrame<G>, DataRow<G>?>) = ReducedGroupBy(this, reducer)
118+
internal fun <T, G> GroupBy<T, G>.reduce(reducer: Selector<DataFrame<G>, DataRow<G>?>): ReducedGroupBy<T, G> =
119+
ReducedGroupBy(this, reducer)

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/codeGen/ReplCodeGeneratorImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.jetbrains.kotlinx.dataframe.AnyRow
55
import org.jetbrains.kotlinx.dataframe.DataFrame
66
import org.jetbrains.kotlinx.dataframe.DataRow
77
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
8+
import org.jetbrains.kotlinx.dataframe.api.GroupBy
89
import org.jetbrains.kotlinx.dataframe.api.schema
910
import org.jetbrains.kotlinx.dataframe.codeGen.Code
1011
import org.jetbrains.kotlinx.dataframe.codeGen.CodeGenerator
@@ -40,6 +41,7 @@ internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
4041
when (type.classifier) {
4142
DataFrame::class -> type.arguments[0].type?.jvmErasure
4243
DataRow::class -> type.arguments[0].type?.jvmErasure
44+
GroupBy::class -> type.arguments[0].type?.jvmErasure
4345
else -> null
4446
}
4547

dataframe-jupyter/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/Integration.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.jetbrains.kotlinx.dataframe.api.SplitWithTransform
2828
import org.jetbrains.kotlinx.dataframe.api.Update
2929
import org.jetbrains.kotlinx.dataframe.api.asDataFrame
3030
import org.jetbrains.kotlinx.dataframe.api.columnsCount
31+
import org.jetbrains.kotlinx.dataframe.api.concat
3132
import org.jetbrains.kotlinx.dataframe.api.isColumnGroup
3233
import org.jetbrains.kotlinx.dataframe.codeGen.CodeGenerator
3334
import org.jetbrains.kotlinx.dataframe.codeGen.CodeWithConverter
@@ -153,6 +154,17 @@ internal class Integration(private val notebook: Notebook, private val options:
153154
null
154155
}
155156

157+
private fun KotlinKernelHost.updateGroupByVariable(
158+
groupBy: GroupBy<*, *>,
159+
property: KProperty<*>,
160+
codeGen: ReplCodeGenerator,
161+
): VariableName? =
162+
execute(
163+
codeWithConverter = codeGen.process(groupBy.concat(), property),
164+
property = property,
165+
type = GroupBy::class.createStarProjectedType(false),
166+
)
167+
156168
override fun Builder.onLoaded() {
157169
if (version != null) {
158170
if (enableExperimentalCsv?.toBoolean() == true) {
@@ -283,12 +295,14 @@ internal class Integration(private val notebook: Notebook, private val options:
283295

284296
addTypeConverter(object : FieldHandler {
285297
override val execution = FieldHandlerFactory.createUpdateExecution<Any> { instance, property ->
298+
// TODO check #1245 here
286299
when (instance) {
287300
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
288301
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
289302
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
290303
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
291304
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
305+
is GroupBy<*, *> -> updateGroupByVariable(instance, property, codeGen)
292306
else -> error("${instance::class} should not be handled by Dataframe field handler")
293307
}
294308
}
@@ -298,7 +312,8 @@ internal class Integration(private val notebook: Notebook, private val options:
298312
value is ColumnGroup<*> ||
299313
value is AnyRow ||
300314
value is AnyFrame ||
301-
value is ImportDataSchema
315+
value is ImportDataSchema ||
316+
value is GroupBy<*, *>
302317
})
303318

304319
fun KotlinKernelHost.addDataSchemas(classes: List<KClass<*>>) {

dataframe-jupyter/src/test/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/CodeGenerationTests.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,39 @@ class CodeGenerationTests : DataFrameJupyterTest() {
9292
df2.group.a
9393
""".checkCompilation()
9494
}
95+
96+
// Issue #1221
97+
@Test
98+
fun `GroupBy code generation`() {
99+
@Language("kts")
100+
val a = """
101+
val ab = dataFrameOf("a", "b")(1, 2)
102+
ab.groupBy { a }.aggregate { sum { b } into "bSum" }
103+
""".checkCompilation()
104+
105+
@Language("kts")
106+
val b = """
107+
val ab = dataFrameOf("a", "b")(1, 2)
108+
val grouped = ab.groupBy { a }
109+
grouped.aggregate { sum { b } into "bSum" }
110+
""".checkCompilation()
111+
112+
@Language("kts")
113+
val c = """
114+
val grouped = dataFrameOf("a", "b")(1, 2).groupBy("a")
115+
grouped.aggregate { sum { b } into "bSum" }
116+
""".checkCompilation()
117+
118+
@Language("kts")
119+
val d = """
120+
val grouped = dataFrameOf("a", "b")(1, 2).groupBy("a")
121+
grouped.keys.a
122+
""".checkCompilation()
123+
124+
@Language("kts")
125+
val e = """
126+
val grouped = dataFrameOf("a", "b")(1, 2).groupBy { "a"<Int>() named "k" }
127+
grouped.keys.k
128+
""".checkCompilation()
129+
}
95130
}

0 commit comments

Comments
 (0)