Skip to content

Commit 3e9261f

Browse files
committed
made name hack optional as it is not waterproof
1 parent 66a42ac commit 3e9261f

File tree

1 file changed

+29
-19
lines changed
  • kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api

1 file changed

+29
-19
lines changed

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

+29-19
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ fun schema(kType: KType): DataType = kotlinEncoderFor<Any?>(kType).schema()
109109

110110
object KotlinTypeInference {
111111

112+
// TODO this hack is a WIP and can give errors
113+
// TODO it's to make data classes get column names like "age" with functions like "getAge"
114+
// TODO instead of column names like "getAge"
115+
var DO_NAME_HACK = false
116+
112117
/**
113118
* @param kClass the class for which to infer the encoder.
114119
* @param arguments the generic type arguments for the class.
@@ -255,22 +260,22 @@ object KotlinTypeInference {
255260
currentType == typeOf<scala.Double>() -> AgnosticEncoders.`PrimitiveDoubleEncoder$`.`MODULE$`
256261

257262
// boxed primitives java / kotlin
258-
currentType == typeOf<Boolean?>() -> AgnosticEncoders.`BoxedBooleanEncoder$`.`MODULE$`
259-
currentType == typeOf<Byte?>() -> AgnosticEncoders.`BoxedByteEncoder$`.`MODULE$`
260-
currentType == typeOf<Short?>() -> AgnosticEncoders.`BoxedShortEncoder$`.`MODULE$`
261-
currentType == typeOf<Int?>() -> AgnosticEncoders.`BoxedIntEncoder$`.`MODULE$`
262-
currentType == typeOf<Long?>() -> AgnosticEncoders.`BoxedLongEncoder$`.`MODULE$`
263-
currentType == typeOf<Float?>() -> AgnosticEncoders.`BoxedFloatEncoder$`.`MODULE$`
264-
currentType == typeOf<Double?>() -> AgnosticEncoders.`BoxedDoubleEncoder$`.`MODULE$`
263+
currentType.isSubtypeOf<Boolean?>() -> AgnosticEncoders.`BoxedBooleanEncoder$`.`MODULE$`
264+
currentType.isSubtypeOf<Byte?>() -> AgnosticEncoders.`BoxedByteEncoder$`.`MODULE$`
265+
currentType.isSubtypeOf<Short?>() -> AgnosticEncoders.`BoxedShortEncoder$`.`MODULE$`
266+
currentType.isSubtypeOf<Int?>() -> AgnosticEncoders.`BoxedIntEncoder$`.`MODULE$`
267+
currentType.isSubtypeOf<Long?>() -> AgnosticEncoders.`BoxedLongEncoder$`.`MODULE$`
268+
currentType.isSubtypeOf<Float?>() -> AgnosticEncoders.`BoxedFloatEncoder$`.`MODULE$`
269+
currentType.isSubtypeOf<Double?>() -> AgnosticEncoders.`BoxedDoubleEncoder$`.`MODULE$`
265270

266271
// boxed primitives scala
267-
currentType == typeOf<scala.Boolean?>() -> AgnosticEncoders.`BoxedBooleanEncoder$`.`MODULE$`
268-
currentType == typeOf<scala.Byte?>() -> AgnosticEncoders.`BoxedByteEncoder$`.`MODULE$`
269-
currentType == typeOf<scala.Short?>() -> AgnosticEncoders.`BoxedShortEncoder$`.`MODULE$`
270-
currentType == typeOf<scala.Int?>() -> AgnosticEncoders.`BoxedIntEncoder$`.`MODULE$`
271-
currentType == typeOf<scala.Long?>() -> AgnosticEncoders.`BoxedLongEncoder$`.`MODULE$`
272-
currentType == typeOf<scala.Float?>() -> AgnosticEncoders.`BoxedFloatEncoder$`.`MODULE$`
273-
currentType == typeOf<scala.Double?>() -> AgnosticEncoders.`BoxedDoubleEncoder$`.`MODULE$`
272+
currentType.isSubtypeOf<scala.Boolean?>() -> AgnosticEncoders.`BoxedBooleanEncoder$`.`MODULE$`
273+
currentType.isSubtypeOf<scala.Byte?>() -> AgnosticEncoders.`BoxedByteEncoder$`.`MODULE$`
274+
currentType.isSubtypeOf<scala.Short?>() -> AgnosticEncoders.`BoxedShortEncoder$`.`MODULE$`
275+
currentType.isSubtypeOf<scala.Int?>() -> AgnosticEncoders.`BoxedIntEncoder$`.`MODULE$`
276+
currentType.isSubtypeOf<scala.Long?>() -> AgnosticEncoders.`BoxedLongEncoder$`.`MODULE$`
277+
currentType.isSubtypeOf<scala.Float?>() -> AgnosticEncoders.`BoxedFloatEncoder$`.`MODULE$`
278+
currentType.isSubtypeOf<scala.Double?>() -> AgnosticEncoders.`BoxedDoubleEncoder$`.`MODULE$`
274279

275280
// leaf encoders
276281
currentType.isSubtypeOf<String?>() -> AgnosticEncoders.`StringEncoder$`.`MODULE$`
@@ -482,7 +487,8 @@ object KotlinTypeInference {
482487
val writeMethodName = (prop as? KMutableProperty<*>)?.setter?.javaMethod?.name
483488

484489
DirtyProductEncoderField(
485-
name = paramName,
490+
doNameHack = DO_NAME_HACK,
491+
columnName = paramName,
486492
readMethodName = readMethodName,
487493
writeMethodName = writeMethodName,
488494
encoder = encoder,
@@ -535,9 +541,10 @@ object KotlinTypeInference {
535541
}
536542

537543
internal open class DirtyProductEncoderField(
538-
private val name: String, // the name used for the column
544+
private val columnName: String, // the name used for the column
539545
private val readMethodName: String, // the name of the method used to read the value
540546
private val writeMethodName: String?,
547+
private val doNameHack: Boolean,
541548
encoder: AgnosticEncoder<*>,
542549
nullable: Boolean,
543550
metadata: Metadata = Metadata.empty(),
@@ -554,12 +561,15 @@ internal open class DirtyProductEncoderField(
554561

555562
/**
556563
* This dirty trick only works because in [SerializerBuildHelper], [ProductEncoder]
557-
* creates an [Invoke] using [name] first and then calls [name] again to retrieve
564+
* creates an [Invoke] using [columnName] first and then calls [columnName] again to retrieve
558565
* the name of the column. This way, we can alternate between the two names.
559566
*/
560567
override fun name(): String =
561-
if (i++ % 2 == 0) readMethodName
562-
else name
568+
when (doNameHack) {
569+
true -> if (i++ % 2 == 0) readMethodName else columnName
570+
false -> readMethodName
571+
}
572+
563573

564574
override fun canEqual(that: Any?): Boolean = that is AgnosticEncoders.EncoderField
565575

0 commit comments

Comments
 (0)