Skip to content

Commit 10935d2

Browse files
committed
Fix NPE in guess type for values of functional types
From my quick research, reflection doesn't know anything about these values. They don't have invoke methods, nor any supertypes. So for now i decided to simply fix NPE by falling back to generic Function type for such columns. It will then at least work together with compiler plugin
1 parent 6318036 commit 10935d2

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ internal fun guessValueType(values: Sequence<Any?>, upperBound: KType? = null, l
436436
collectionClasses.add(it.javaClass.kotlin)
437437
}
438438

439+
is Function<*> -> classes.add(Function::class)
440+
439441
else -> classes.add(it.javaClass.kotlin)
440442
}
441443
}

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ class DataFrameTests : BaseTest() {
217217
dataFrameOf("name", "age", "city", "weight")(c1, c2, c3, c4) shouldBe df
218218
}
219219

220+
@Test
221+
fun `guess column type for type without classifier`() {
222+
val df = dataFrameOf("a", "b")({ 1 }, 2)
223+
df["a"].type() shouldBe typeOf<Function<*>>()
224+
(df["a"][0] as () -> Int).invoke() shouldBe 1
225+
}
226+
220227
@Test
221228
fun `create with columnOf`() {
222229
val col = columnOf("Alice", "Bob")

plugins/kotlin-dataframe/testData/box/dataFrameOf_vararg.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ import org.jetbrains.kotlinx.dataframe.io.*
66
fun box(): String {
77
val df = dataFrameOf("a")(1, 2, 3)
88
val i: Int = df.a[0]
9+
10+
val df1 = dataFrameOf("a", "b")({ 1 }, 2)
11+
val i1: Int = df1.a[0].invoke()
912
return "OK"
1013
}

0 commit comments

Comments
 (0)