Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object EncoderUtils {
} else if (lenientSerialization) {
ObjectType(classOf[java.lang.Object])
} else {
ObjectType(enc.clsTag.runtimeClass)
dataTypeForClass(enc.clsTag.runtimeClass)
}
}

Expand Down Expand Up @@ -158,8 +158,16 @@ object EncoderUtils {
def dataTypeForClass(c: Class[_]): DataType =
javaClassToPrimitiveType.get(c).getOrElse(ObjectType(c))

private val javaClassToPrimitiveType: Map[Class[_], DataType] =
typeJavaMapping.iterator.filter(_._2.isPrimitive).map(_.swap).toMap
private val javaClassToPrimitiveType: Map[Class[_], DataType] = Map(
classOf[Boolean] -> BooleanType,
classOf[Byte] -> ByteType,
classOf[Short] -> ShortType,
classOf[Int] -> IntegerType,
classOf[Long] -> LongType,
classOf[Float] -> FloatType,
classOf[Double] -> DoubleType,
classOf[Array[Byte]] -> BinaryType
)

val typeBoxedJavaMapping: Map[DataType, Class[_]] = Map[DataType, Class[_]](
BooleanType -> classOf[java.lang.Boolean],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,22 +766,27 @@ class ExpressionEncoderSuite extends CodegenInterpretedPlanTest with AnalysisTes
testDataTransformingEnc(enc, data)
}

val transformLongToTimestampEnc: AgnosticEncoder[Long] =
TransformingEncoder[Long, java.sql.Timestamp](
classTag,
TimestampEncoder(true),
() =>
new Codec[Long, java.sql.Timestamp] with Serializable {
override def encode(in: Long): Timestamp = Timestamp.from(microsToInstant(in))
override def decode(out: Timestamp): Long = instantToMicros(out.toInstant)
}
)

test("SPARK-52601 TransformingEncoder from primitive to timestamp") {
val enc: AgnosticEncoder[Long] =
TransformingEncoder[Long, java.sql.Timestamp](
classTag,
TimestampEncoder(true),
() =>
new Codec[Long, java.sql.Timestamp] with Serializable {
override def encode(in: Long): Timestamp = Timestamp.from(microsToInstant(in))
override def decode(out: Timestamp): Long = instantToMicros(out.toInstant)
}
)
val data: Seq[Long] = Seq(0L, 1L, 2L)

assert(enc.dataType === TimestampType)
assert(transformLongToTimestampEnc.dataType === TimestampType)
testDataTransformingEnc(transformLongToTimestampEnc, data)
}

testDataTransformingEnc(enc, data)
test("SPARK-55708 TransformingEncoder in Option") {
val data = Seq(Some(0L), None)
testDataTransformingEnc(OptionEncoder(transformLongToTimestampEnc), data)
}

val longEncForTimestamp: AgnosticEncoder[V[Long]] =
Expand Down