|
| 1 | +package org.jetbrains.kotlinx.dataframe.impl.api |
| 2 | + |
| 3 | +import org.jetbrains.kotlinx.dataframe.columns.ColumnPath |
| 4 | +import org.jetbrains.kotlinx.dataframe.impl.schema.DataFrameSchemaImpl |
| 5 | +import org.jetbrains.kotlinx.dataframe.impl.schema.getSchema |
| 6 | +import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema |
| 7 | +import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema |
| 8 | +import kotlin.reflect.KClass |
| 9 | + |
| 10 | +@PublishedApi |
| 11 | +internal fun compileTimeSchemaImpl(runtimeSchema: DataFrameSchema, klass: KClass<*>): DataFrameSchema { |
| 12 | + val compileSchema = getSchema(klass) |
| 13 | + val root = ColumnPath(emptyList()) |
| 14 | + val order = mutableMapOf<ColumnPath, Int>() |
| 15 | + runtimeSchema.putColumnsOrder(order, path = root) |
| 16 | + return compileSchema.sorted(order, path = root) |
| 17 | +} |
| 18 | + |
| 19 | +internal fun DataFrameSchema.putColumnsOrder(order: MutableMap<ColumnPath, Int>, path: ColumnPath) { |
| 20 | + columns.entries.forEachIndexed { i, (name, column) -> |
| 21 | + val columnPath = path + name |
| 22 | + order[columnPath] = i |
| 23 | + when (column) { |
| 24 | + is ColumnSchema.Frame -> { |
| 25 | + column.schema.putColumnsOrder(order, columnPath) |
| 26 | + } |
| 27 | + |
| 28 | + is ColumnSchema.Group -> { |
| 29 | + column.schema.putColumnsOrder(order, columnPath) |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +internal fun DataFrameSchema.sorted(order: Map<ColumnPath, Int>, path: ColumnPath): DataFrameSchema { |
| 36 | + val sorted = columns.map { (name, column) -> |
| 37 | + name to when (column) { |
| 38 | + is ColumnSchema.Frame -> ColumnSchema.Frame( |
| 39 | + column.schema.sorted(order, path + name), |
| 40 | + column.nullable, |
| 41 | + column.contentType, |
| 42 | + ) |
| 43 | + |
| 44 | + is ColumnSchema.Group -> ColumnSchema.Group(column.schema.sorted(order, path + name), column.contentType) |
| 45 | + |
| 46 | + is ColumnSchema.Value -> column |
| 47 | + |
| 48 | + else -> TODO("unexpected ColumnSchema class ${column::class}") |
| 49 | + } |
| 50 | + }.sortedBy { (name, _) -> |
| 51 | + order[path + name] |
| 52 | + }.toMap() |
| 53 | + return DataFrameSchemaImpl(sorted) |
| 54 | +} |
0 commit comments