Skip to content

Commit 04b5ba6

Browse files
committed
rewrote product encoding to support scala case classes
1 parent d62e3af commit 04b5ba6

File tree

1 file changed

+12
-2
lines changed
  • kotlin-spark-api/3.2/src/main/kotlin/org/jetbrains/kotlinx/spark/api

1 file changed

+12
-2
lines changed

kotlin-spark-api/3.2/src/main/kotlin/org/jetbrains/kotlinx/spark/api/Encoding.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,18 @@ fun schema(type: KType, map: Map<String, KType> = mapOf()): DataType {
271271
KDataTypeWrapper(structType, klass.java, true)
272272
}
273273
klass.isSubclassOf(Product::class) -> {
274-
val params = type.arguments.mapIndexed { i, it ->
275-
"_${i + 1}" to it.type!!
274+
275+
// create map from T1, T2 to Int, String etc.
276+
val typeMap = klass.constructors.first().typeParameters.map { it.name }
277+
.zip(
278+
type.arguments.map { it.type }
279+
)
280+
.toMap()
281+
282+
// collect params by name and actual type
283+
val params = klass.constructors.first().parameters.map {
284+
val typeName = it.type.toString().replace("!", "")
285+
it.name to (typeMap[typeName] ?: it.type)
276286
}
277287

278288
val structType = DataTypes.createStructType(

0 commit comments

Comments
 (0)