You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/02/02 21:06:49 UTC

[GitHub] [spark] xkrogen commented on a change in pull request #35379: [SPARK-38091][SQL] fix bugs in AvroSerializer

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



##########
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:
       Similar to my comment above about BigDecimal, can we share more of this conversion logic?

##########
File path: external/avro/src/main/scala/org/apache/spark/sql/avro/AvroSerializer.scala
##########
@@ -124,26 +136,51 @@ private[sql] class AvroSerializer(
       case (DoubleType, DOUBLE) =>
         (getter, ordinal) => getter.getDouble(ordinal)
       case (d: DecimalType, FIXED)
-        if avroType.getLogicalType == LogicalTypes.decimal(d.precision, d.scale) =>
+          if avroType.getLogicalType == LogicalTypes.decimal(d.precision, d.scale) =>
         (getter, ordinal) =>
-          val decimal = getter.getDecimal(ordinal, d.precision, d.scale)
-          decimalConversions.toFixed(decimal.toJavaBigDecimal, avroType,
-            LogicalTypes.decimal(d.precision, d.scale))
+          getter.get(ordinal, d) match {
+            case bigDecimal: java.math.BigDecimal =>
+              decimalConversions.toFixed(
+                bigDecimal,
+                avroType,
+                LogicalTypes.decimal(bigDecimal.precision, bigDecimal.scale))
+            case decimal: Decimal =>
+              decimalConversions.toFixed(
+                decimal.toJavaBigDecimal,
+                avroType,
+                LogicalTypes.decimal(d.precision, d.scale))
+            case other =>
+              throw new IncompatibleSchemaException(
+                s"Expected java.math.BigDecimal or Decimal, found ${other.getClass}")
+          }
 
       case (d: DecimalType, BYTES)
-        if avroType.getLogicalType == LogicalTypes.decimal(d.precision, d.scale) =>
+          if avroType.getLogicalType == LogicalTypes.decimal(d.precision, d.scale) =>
         (getter, ordinal) =>
-          val decimal = getter.getDecimal(ordinal, d.precision, d.scale)
-          decimalConversions.toBytes(decimal.toJavaBigDecimal, avroType,
-            LogicalTypes.decimal(d.precision, d.scale))
+          getter.get(ordinal, d) match {
+            case bigDecimal: java.math.BigDecimal =>
+              decimalConversions.toBytes(
+                bigDecimal,
+                avroType,
+                LogicalTypes.decimal(bigDecimal.precision, bigDecimal.scale))
+            case decimal: Decimal =>
+              decimalConversions.toBytes(
+                decimal.toJavaBigDecimal,
+                avroType,
+                LogicalTypes.decimal(d.precision, d.scale))

Review comment:
       Assuming that we pull the precision/scale from `d` as I suggest above, we can simplify this to just:
   ```
   val decimal = getter.get(ordinal, d) {
     case bd: java.math.BigDecimal => bd
     case dec: Decimal => dec.toJavaBigDecimal
   }
   decimalConversions.toBytes(decimal, avroType, LogicalTypes.decimal(d.precision, d.scale))
   ```
   
   It would also be good to share this impl with the `(DecimalType, FIXED)` branch. Maybe a method like
   ```scala
   def extractDecimal(getter: xxx, ordinal: Int, d: DecimalType): java.math.BigDecimal = getter.get(ordinal, d) {
     case bd: java.math.BigDecimal => bd
     case dec: Decimal => dec.toJavaBigDecimal
   }
   ```

##########
File path: external/avro/src/main/scala/org/apache/spark/sql/avro/AvroSerializer.scala
##########
@@ -124,26 +136,51 @@ private[sql] class AvroSerializer(
       case (DoubleType, DOUBLE) =>
         (getter, ordinal) => getter.getDouble(ordinal)
       case (d: DecimalType, FIXED)
-        if avroType.getLogicalType == LogicalTypes.decimal(d.precision, d.scale) =>
+          if avroType.getLogicalType == LogicalTypes.decimal(d.precision, d.scale) =>
         (getter, ordinal) =>
-          val decimal = getter.getDecimal(ordinal, d.precision, d.scale)
-          decimalConversions.toFixed(decimal.toJavaBigDecimal, avroType,
-            LogicalTypes.decimal(d.precision, d.scale))
+          getter.get(ordinal, d) match {
+            case bigDecimal: java.math.BigDecimal =>
+              decimalConversions.toFixed(
+                bigDecimal,
+                avroType,
+                LogicalTypes.decimal(bigDecimal.precision, bigDecimal.scale))

Review comment:
       Is it correct for us to use the precision/scale from `bigDecimal`? I think we should still be using `d.precision` and `d.scale`, respecting the values from the Catalyst schema, right?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org