gengliangwang commented on a change in pull request #35379:
URL: https://github.com/apache/spark/pull/35379#discussion_r809611693



##########
File path: 
external/avro/src/main/scala/org/apache/spark/sql/avro/AvroSerializer.scala
##########
@@ -166,30 +183,85 @@ private[sql] class AvroSerializer(
         (getter, ordinal) => ByteBuffer.wrap(getter.getBinary(ordinal))
 
       case (DateType, INT) =>
-        (getter, ordinal) => dateRebaseFunc(getter.getInt(ordinal))
+        (getter, ordinal) =>
+          val epochDays: Int = getter.get(ordinal, DateType) match {
+            case epochDays: java.lang.Integer => epochDays
+            case date: java.sql.Date => date.toLocalDate().toEpochDay().toInt
+            case localDate: java.time.LocalDate => 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)
+          }
+          dateRebaseFunc(epochDays)
 
-      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 =>
+                  throw new IncompatibleSchemaException(s"""
+                       |Expected java.lang.Long, java.sql.Timestamp, or 
java.time.Instant,
+                       | but found ${other.getClass}""".stripMargin)
+              }
+          case _: TimestampMicros =>
+            (getter, ordinal) =>
+              getter.get(ordinal, TimestampType) match {
+                case micros: java.lang.Long => timestampRebaseFunc(micros)
+                case javaTimestamp: java.sql.Timestamp =>
+                  
timestampRebaseFunc(DateTimeUtils.millisToMicros(javaTimestamp.getTime))
+                case instant: java.time.Instant =>
+                  
timestampRebaseFunc(DateTimeUtils.millisToMicros(instant.toEpochMilli))
+                case other =>
+                  throw new IncompatibleSchemaException(s"""
+                       |Expected Expected java.lang.Long, java.sql.Timestamp, 
or java.time.Instant,
+                       | but found ${other.getClass}""".stripMargin)
+              }
+          case other =>
+            throw new IncompatibleSchemaException(
+              errorPrefix +
+                s"SQL type ${TimestampType.sql} cannot be converted to Avro 
logical type $other")
         }
 
-      case (TimestampNTZType, LONG) => avroType.getLogicalType match {
-        // To keep consistent with TimestampType, if the Avro type is Long and 
it is not
-        // logical type (the `null` case), output the TimestampNTZ as long 
value
-        // in millisecond precision.
-        case null | _: LocalTimestampMillis => (getter, ordinal) =>
-          DateTimeUtils.microsToMillis(getter.getLong(ordinal))
-        case _: LocalTimestampMicros => (getter, ordinal) =>
-          getter.getLong(ordinal)
-        case other => throw new IncompatibleSchemaException(errorPrefix +
-          s"SQL type ${TimestampNTZType.sql} cannot be converted to Avro 
logical type $other")
-      }
+      case (TimestampNTZType, LONG) =>
+        avroType.getLogicalType match {
+          // To keep consistent with TimestampType, if the Avro type is Long 
and it is not
+          // logical type (the `null` case), output the TimestampNTZ as long 
value
+          // in millisecond precision.
+          case null | _: LocalTimestampMillis | _: TimestampMillis =>
+            (getter, ordinal) =>
+              getter.get(ordinal, TimestampNTZType) match {
+                case micros: java.lang.Long => 
DateTimeUtils.microsToMillis(micros)
+                case localDateTime: java.time.LocalDateTime =>
+                  
localDateTime.atZone(java.time.ZoneId.of("UTC")).toInstant().toEpochMilli()
+                case other =>
+                  throw new IncompatibleSchemaException(s"""
+                       |Expected java.lang.Long or java.time.LocalDateTime,
+                       | but found ${other.getClass}""".stripMargin)
+              }
+          case _: LocalTimestampMicros | _: TimestampMicros =>

Review comment:
       ```suggestion
             case _: LocalTimestampMicros =>
   ```




-- 
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]

Reply via email to