Zhen-hao commented on a change in pull request #35379:
URL: https://github.com/apache/spark/pull/35379#discussion_r798911092
##########
File path:
external/avro/src/main/scala/org/apache/spark/sql/avro/AvroSerializer.scala
##########
@@ -166,35 +204,91 @@ private[sql] class AvroSerializer(
(getter, ordinal) => ByteBuffer.wrap(getter.getBinary(ordinal))
case (DateType, INT) =>
- (getter, ordinal) => dateRebaseFunc(getter.getInt(ordinal))
+ (getter, ordinal) =>
+ getter.get(ordinal, DateType) match {
+ case epochDays: java.lang.Integer => dateRebaseFunc(epochDays)
+ case date: java.sql.Date =>
dateRebaseFunc(date.toLocalDate().toEpochDay().toInt)
+ case localDate: java.time.LocalDate =>
dateRebaseFunc(localDate.toEpochDay().toInt)
+ case other =>
+ throw new IncompatibleSchemaException(s"""
+ |Expected java.lang.Integer, java.sql.Date, or
java.time.LocalDate,
+ | but found ${other.getClass}""".stripMargin)
+ }
- case (TimestampType, LONG) => avroType.getLogicalType match {
+ case (TimestampType, LONG) =>
+ avroType.getLogicalType match {
// For backward compatibility, if the Avro type is Long and it is
not logical type
// (the `null` case), output the timestamp value as with millisecond
precision.
- case null | _: TimestampMillis => (getter, ordinal) =>
-
DateTimeUtils.microsToMillis(timestampRebaseFunc(getter.getLong(ordinal)))
- case _: TimestampMicros => (getter, ordinal) =>
- timestampRebaseFunc(getter.getLong(ordinal))
- case other => throw new IncompatibleSchemaException(errorPrefix +
- s"SQL type ${TimestampType.sql} cannot be converted to Avro
logical type $other")
+ case null | _: TimestampMillis =>
+ (getter, ordinal) =>
+ getter.get(ordinal, TimestampType) match {
+ case micros: java.lang.Long =>
+ DateTimeUtils.microsToMillis(timestampRebaseFunc(micros))
+ case javaTimestamp: java.sql.Timestamp => javaTimestamp.getTime
+ case instant: java.time.Instant => instant.toEpochMilli
+ case other =>
Review comment:
I realized that it is not as easy as the other case.
We can refactor out an `extractMillis` or `extractMicros` method, but it
will introduce some computational overhead. There would always be a case of
unnecessary back and forth conversion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]