-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #990 from Kotlin/compileTimeSchemaOrder
Sort df.compileTimeSchema() columns according to df.schema() so they're easier to compare
- Loading branch information
Showing
4 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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
55 changes: 55 additions & 0 deletions
55
core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/schema.kt
This file contains 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,55 @@ | ||
package org.jetbrains.kotlinx.dataframe.impl.api | ||
|
||
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath | ||
import org.jetbrains.kotlinx.dataframe.impl.schema.DataFrameSchemaImpl | ||
import org.jetbrains.kotlinx.dataframe.impl.schema.getSchema | ||
import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema | ||
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema | ||
import kotlin.reflect.KClass | ||
|
||
@PublishedApi | ||
internal fun compileTimeSchemaImpl(runtimeSchema: DataFrameSchema, klass: KClass<*>): DataFrameSchema { | ||
val compileSchema = getSchema(klass) | ||
val root = ColumnPath(emptyList()) | ||
val order = buildMap { | ||
putColumnsOrder(runtimeSchema, path = root) | ||
} | ||
return compileSchema.sortedBy(order, path = root) | ||
} | ||
|
||
internal fun MutableMap<ColumnPath, Int>.putColumnsOrder(schema: DataFrameSchema, path: ColumnPath) { | ||
schema.columns.entries.forEachIndexed { i, (name, column) -> | ||
val columnPath = path + name | ||
this[columnPath] = i | ||
when (column) { | ||
is ColumnSchema.Frame -> { | ||
putColumnsOrder(column.schema, columnPath) | ||
} | ||
|
||
is ColumnSchema.Group -> { | ||
putColumnsOrder(column.schema, columnPath) | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal fun DataFrameSchema.sortedBy(order: Map<ColumnPath, Int>, path: ColumnPath): DataFrameSchema { | ||
val sorted = columns.map { (name, column) -> | ||
name to when (column) { | ||
is ColumnSchema.Frame -> ColumnSchema.Frame( | ||
column.schema.sortedBy(order, path + name), | ||
column.nullable, | ||
column.contentType, | ||
) | ||
|
||
is ColumnSchema.Group -> ColumnSchema.Group(column.schema.sortedBy(order, path + name), column.contentType) | ||
|
||
is ColumnSchema.Value -> column | ||
|
||
else -> TODO("unexpected ColumnSchema class ${column::class}") | ||
} | ||
}.sortedBy { (name, _) -> | ||
order[path + name] | ||
}.toMap() | ||
return DataFrameSchemaImpl(sorted) | ||
} |
26 changes: 26 additions & 0 deletions
26
core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/schema.kt
This file contains 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,26 @@ | ||
package org.jetbrains.kotlinx.dataframe.api | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.jetbrains.kotlinx.dataframe.DataRow | ||
import org.junit.Test | ||
|
||
class SchemaTests { | ||
@Test | ||
fun `columns order test`() { | ||
val row = dataFrameOf("c", "b")(4, 5).first() | ||
val df = dataFrameOf("abc", "a", "a123", "nested")(1, 2, 3, row).cast<Schema>() | ||
df.schema().toString() shouldBe df.compileTimeSchema().toString() | ||
} | ||
} | ||
|
||
private interface Schema { | ||
val a: Int | ||
val abc: Int | ||
val a123: Int | ||
val nested: DataRow<Nested> | ||
} | ||
|
||
private interface Nested { | ||
val b: Int | ||
val c: Int | ||
} |