Skip to content

Commit 4604a58

Browse files
committed
added test and small fix to verify that primary constructor arguments keep their order as dataframe columns, even when they have @ColumnName annotations.
1 parent 59ebff8 commit 4604a58

File tree

2 files changed

+17
-2
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/impl/schema
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

2 files changed

+17
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ internal fun <T> Iterable<KCallable<T>>.sortWithConstructor(klass: KClass<*>): L
192192
// else sort the ones in the primary constructor first according to the order in there
193193
// leave the rest at the end in lexicographical order
194194
val (propsInConstructor, propsNotInConstructor) =
195-
lexicographicalColumns.partition { it.columnName in primaryConstructorOrder.keys }
195+
lexicographicalColumns.partition { it.name in primaryConstructorOrder.keys }
196196

197197
return propsInConstructor
198-
.sortedBy { primaryConstructorOrder[it.columnName] } + propsNotInConstructor
198+
.sortedBy { primaryConstructorOrder[it.name] } + propsNotInConstructor
199199
}

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.kotest.matchers.shouldBe
55
import org.jetbrains.kotlinx.dataframe.DataColumn
66
import org.jetbrains.kotlinx.dataframe.DataFrame
77
import org.jetbrains.kotlinx.dataframe.DataRow
8+
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
89
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
910
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
1011
import org.jetbrains.kotlinx.dataframe.kind
@@ -79,14 +80,28 @@ class CreateDataFrameTests {
7980

8081
@Test
8182
fun `preserve fields order`() {
83+
// constructor properties will keep order, so x, c
8284
class B(val x: Int, val c: String, d: Double) {
85+
// then child properties will be sorted lexicographically by column name, so a, b
8386
val b: Int = x
8487
val a: Double = d
8588
}
8689

8790
listOf(B(1, "a", 2.0)).toDataFrame().columnNames() shouldBe listOf("x", "c", "a", "b")
8891
}
8992

93+
@Test
94+
fun `preserve fields order with @ColumnName`() {
95+
// constructor properties will keep order, so z, y
96+
class B(@ColumnName("z") val x: Int, @ColumnName("y") val c: String, d: Double) {
97+
// then child properties will be sorted lexicographically by column name, so w, x
98+
@ColumnName("x") val a: Double = d
99+
@ColumnName("w") val b: Int = x
100+
}
101+
102+
listOf(B(1, "a", 2.0)).toDataFrame().columnNames() shouldBe listOf("z", "y", "w", "x")
103+
}
104+
90105
@DataSchema
91106
data class A(val v: Int)
92107

0 commit comments

Comments
 (0)