|
| 1 | +package org.jetbrains.kotlinx.dataframe.plugin.impl |
| 2 | + |
| 3 | +import org.jetbrains.kotlin.fir.types.ConeKotlinType |
| 4 | +import org.jetbrains.kotlin.fir.types.ConeNullability |
| 5 | +import org.jetbrains.kotlin.fir.types.classId |
| 6 | +import org.jetbrains.kotlin.fir.types.isNullable |
| 7 | +import org.jetbrains.kotlinx.dataframe.impl.api.DataFrameLikeContainer |
| 8 | +import org.jetbrains.kotlinx.dataframe.impl.api.GenericColumn |
| 9 | +import org.jetbrains.kotlinx.dataframe.impl.api.GenericColumnGroup |
| 10 | +import org.jetbrains.kotlinx.dataframe.plugin.extensions.KotlinTypeFacade |
| 11 | +import org.jetbrains.kotlinx.dataframe.plugin.extensions.wrap |
| 12 | +import org.jetbrains.kotlinx.dataframe.plugin.impl.api.TypeApproximation |
| 13 | +import org.jetbrains.kotlinx.dataframe.plugin.pluginDataFrameSchema |
| 14 | +import org.jetbrains.kotlinx.dataframe.plugin.utils.Names |
| 15 | + |
| 16 | +data class PluginDataFrameSchema( |
| 17 | + private val columns: List<SimpleCol> |
| 18 | +) : DataFrameLikeContainer<SimpleCol> { |
| 19 | + override fun columns(): List<SimpleCol> { |
| 20 | + return columns |
| 21 | + } |
| 22 | + |
| 23 | + override fun toString(): String { |
| 24 | + return columns.asString() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +private fun List<SimpleCol>.asString(indent: String = ""): String { |
| 29 | + return joinToString("\n") { |
| 30 | + val col = when (it) { |
| 31 | + is SimpleFrameColumn -> { |
| 32 | + "${it.name}*\n" + it.columns().asString("$indent ") |
| 33 | + } |
| 34 | + is SimpleColumnGroup -> { |
| 35 | + "${it.name}\n" + it.columns().asString("$indent ") |
| 36 | + } |
| 37 | + is SimpleCol -> { |
| 38 | +// val type = (it.type as TypeApproximationImpl).let { |
| 39 | +// val nullability = if (it.nullable) "?" else "" |
| 40 | +// "${it.fqName}$nullability" |
| 41 | +// } |
| 42 | + "${it.name}: ${it.type}" |
| 43 | + } |
| 44 | + else -> TODO() |
| 45 | + } |
| 46 | + "$indent$col" |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +open class SimpleCol( |
| 51 | + val name: String, |
| 52 | + open val type: TypeApproximation |
| 53 | +) : GenericColumn { |
| 54 | + |
| 55 | + override fun name(): String { |
| 56 | + return name |
| 57 | + } |
| 58 | + |
| 59 | + open fun rename(s: String): SimpleCol { |
| 60 | + return SimpleCol(s, type) |
| 61 | + } |
| 62 | + |
| 63 | + open fun changeType(type: TypeApproximation): SimpleCol { |
| 64 | + return SimpleCol(name, type) |
| 65 | + } |
| 66 | + |
| 67 | + override fun equals(other: Any?): Boolean { |
| 68 | + if (this === other) return true |
| 69 | + if (javaClass != other?.javaClass) return false |
| 70 | + |
| 71 | + other as SimpleCol |
| 72 | + |
| 73 | + if (name != other.name) return false |
| 74 | + if (type != other.type) return false |
| 75 | + |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + override fun hashCode(): Int { |
| 80 | + var result = name.hashCode() |
| 81 | + result = 31 * result + type.hashCode() |
| 82 | + return result |
| 83 | + } |
| 84 | + |
| 85 | + override fun toString(): String { |
| 86 | + return "SimpleCol(name='$name', type=$type)" |
| 87 | + } |
| 88 | + |
| 89 | + open fun kind(): SimpleColumnKind { |
| 90 | + return SimpleColumnKind.VALUE |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +enum class SimpleColumnKind { |
| 95 | + VALUE, GROUP, FRAME |
| 96 | +} |
| 97 | + |
| 98 | +data class SimpleFrameColumn( |
| 99 | + private val name1: String, |
| 100 | + private val columns: List<SimpleCol>, |
| 101 | + // probably shouldn't be called at all? |
| 102 | + // exists only because SimpleCol has it |
| 103 | + // but in fact it's for `materialize` to decide what should be the type of the property / accessors |
| 104 | + val anyFrameType: TypeApproximation, |
| 105 | +) : GenericColumnGroup<SimpleCol>, SimpleCol(name1, anyFrameType) { |
| 106 | + override fun columns(): List<SimpleCol> { |
| 107 | + return columns |
| 108 | + } |
| 109 | + |
| 110 | + override fun rename(s: String): SimpleFrameColumn { |
| 111 | + return SimpleFrameColumn(name1, columns, anyFrameType) |
| 112 | + } |
| 113 | + |
| 114 | + override fun kind(): SimpleColumnKind { |
| 115 | + return SimpleColumnKind.FRAME |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +class SimpleColumnGroup( |
| 120 | + name: String, |
| 121 | + private val columns: List<SimpleCol>, |
| 122 | + columnGroupType: TypeApproximation |
| 123 | +) : GenericColumnGroup<SimpleCol>, SimpleCol(name, columnGroupType) { |
| 124 | + |
| 125 | + override fun columns(): List<SimpleCol> { |
| 126 | + return columns |
| 127 | + } |
| 128 | + |
| 129 | + override fun rename(s: String): SimpleColumnGroup { |
| 130 | + return SimpleColumnGroup(s, columns, type) |
| 131 | + } |
| 132 | + |
| 133 | + override fun changeType(type: TypeApproximation): SimpleCol { |
| 134 | + return TODO() |
| 135 | + } |
| 136 | + |
| 137 | + override fun equals(other: Any?): Boolean { |
| 138 | + if (this === other) return true |
| 139 | + if (javaClass != other?.javaClass) return false |
| 140 | + if (!super.equals(other)) return false |
| 141 | + |
| 142 | + other as SimpleColumnGroup |
| 143 | + |
| 144 | + if (name != other.name) return false |
| 145 | + if (columns != other.columns) return false |
| 146 | + |
| 147 | + return true |
| 148 | + } |
| 149 | + |
| 150 | + override fun hashCode(): Int { |
| 151 | + var result = super.hashCode() |
| 152 | + result = 31 * result + name.hashCode() |
| 153 | + result = 31 * result + columns.hashCode() |
| 154 | + return result |
| 155 | + } |
| 156 | + |
| 157 | + override fun kind(): SimpleColumnKind { |
| 158 | + return SimpleColumnKind.GROUP |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +fun KotlinTypeFacade.simpleColumnOf(name: String, type: ConeKotlinType): SimpleCol { |
| 163 | + return if (type.classId == Names.DATA_ROW_CLASS_ID) { |
| 164 | + val schema = pluginDataFrameSchema(type) |
| 165 | + val group = SimpleColumnGroup(name, schema.columns(), anyRow) |
| 166 | + val column = if (type.isNullable) { |
| 167 | + makeNullable(group) |
| 168 | + } else { |
| 169 | + group |
| 170 | + } |
| 171 | + column |
| 172 | + } else if (type.classId == Names.DF_CLASS_ID && type.nullability == ConeNullability.NOT_NULL) { |
| 173 | + val schema = pluginDataFrameSchema(type) |
| 174 | + SimpleFrameColumn(name, schema.columns(), anyDataFrame) |
| 175 | + } else { |
| 176 | + SimpleCol(name, type.wrap()) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +private fun KotlinTypeFacade.makeNullable(column: SimpleCol): SimpleCol { |
| 181 | + return when (column) { |
| 182 | + is SimpleColumnGroup -> { |
| 183 | + SimpleColumnGroup(column.name, column.columns().map { makeNullable(column) }, anyRow) |
| 184 | + } |
| 185 | + is SimpleFrameColumn -> column |
| 186 | + else -> SimpleCol(column.name, column.type.changeNullability { true }) |
| 187 | + } |
| 188 | +} |
0 commit comments