Skip to content

Commit 56bc82f

Browse files
committed
fix: πŸ› Fixes incorrect order of fields in data class
We were reading class properties, not constructor params. That lead to the fact, that sometimes we were reading properties in incorrect (alphabetical) order. βœ… Closes: #38
1 parent 868e479 commit 56bc82f

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

β€Žcore/src/main/scala/org/apache/spark/sql/KotlinReflection.scala

+9-10
Original file line numberDiff line numberDiff line change
@@ -674,23 +674,22 @@ object KotlinReflection extends KotlinReflection {
674674
case dataType: KDataTypeWrapper =>
675675
val cls = dataType.cls
676676
val properties = getJavaBeanReadableProperties(cls)
677-
val fields = properties.map { prop =>
678-
679-
val maybeField = dataType.dt.fields.map(_.asInstanceOf[KStructField]).find(it => it.getterName == prop.getReadMethod.getName)
680-
if (maybeField.isEmpty)
681-
throw new IllegalArgumentException(s"Field ${prop.getName} is not found among available fields, which are: ${dataType.dt.fields.map(_.name).mkString(", ")}")
682-
val fieldName = maybeField.get.name
683-
val propClass = maybeField.map(it => it.dataType.asInstanceOf[DataTypeWithClass].cls).get
684-
val propDt = maybeField.map(it => it.dataType.asInstanceOf[DataTypeWithClass]).get
685-
677+
val structFields = dataType.dt.fields.map(_.asInstanceOf[KStructField])
678+
val fields = structFields.map { structField =>
679+
val maybeProp = properties.find(it => it.getReadMethod.getName == structField.getterName)
680+
if (maybeProp.isEmpty) throw new IllegalArgumentException(s"Field ${structField.name} is not found among available props, which are: ${properties.map(_.getName).mkString(", ")}")
681+
val fieldName = structField.name
682+
val propClass = structField.dataType.asInstanceOf[DataTypeWithClass].cls
683+
val propDt = structField.dataType.asInstanceOf[DataTypeWithClass]
686684
val fieldValue = Invoke(
687685
inputObject,
688-
prop.getReadMethod.getName,
686+
maybeProp.get.getReadMethod.getName,
689687
inferExternalType(propClass),
690688
returnNullable = propDt.nullable
691689
)
692690
val newPath = walkedTypePath.recordField(propClass.getName, fieldName)
693691
(fieldName, serializerFor(fieldValue, getType(propClass), newPath, seenTypeSet, if (propDt.isInstanceOf[ComplexWrapper]) Some(propDt) else None))
692+
694693
}
695694
createSerializerForObject(inputObject, fields)
696695

β€Žkotlin-spark-api/src/main/kotlin/org/jetbrains/spark/api/ApiV1.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ import java.beans.PropertyDescriptor
3333
import java.math.BigDecimal
3434
import java.sql.Date
3535
import java.sql.Timestamp
36+
import java.time.Instant
37+
import java.time.LocalDate
3638
import java.util.concurrent.ConcurrentHashMap
3739
import kotlin.reflect.KClass
3840
import kotlin.reflect.KType
3941
import kotlin.reflect.full.declaredMemberProperties
4042
import kotlin.reflect.full.findAnnotation
4143
import kotlin.reflect.full.isSubclassOf
44+
import kotlin.reflect.full.primaryConstructor
4245
import kotlin.reflect.typeOf
4346

4447
@JvmField
@@ -287,20 +290,22 @@ fun schema(type: KType, map: Map<String, KType> = mapOf()): DataType {
287290
mapValueParam.isMarkedNullable
288291
)
289292
}
290-
else -> {
293+
klass.isData -> {
291294
val structType = StructType(
292295
klass
293-
.declaredMemberProperties
296+
.primaryConstructor!!
297+
.parameters
294298
.filter { it.findAnnotation<Transient>() == null }
295299
.map {
296-
val projectedType = types[it.returnType.toString()] ?: it.returnType
297-
val propertyDescriptor = PropertyDescriptor(it.name, klass.java, "is" + it.name.capitalize(), null)
300+
val projectedType = types[it.type.toString()] ?: it.type
301+
val propertyDescriptor = PropertyDescriptor(it.name, klass.java, "is" + it.name?.capitalize(), null)
298302
KStructField(propertyDescriptor.readMethod.name, StructField(it.name, schema(projectedType, types), projectedType.isMarkedNullable, Metadata.empty()))
299303
}
300304
.toTypedArray()
301305
)
302306
KDataTypeWrapper(structType, klass.java, true)
303307
}
308+
else -> throw IllegalArgumentException("$type is unsupported")
304309
}
305310
}
306311

0 commit comments

Comments
Β (0)