Skip to content
Closed
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 @@ -141,6 +141,12 @@ private[sql] class AvroDeserializer(
case (INT, IntegerType) => (updater, ordinal, value) =>
updater.setInt(ordinal, value.asInstanceOf[Int])

case (INT, LongType) => (updater, ordinal, value) =>
updater.setLong(ordinal, value.asInstanceOf[Int])

case (INT, DoubleType) => (updater, ordinal, value) =>
updater.setDouble(ordinal, value.asInstanceOf[Int])

case (INT, dt: DatetimeType)
if preventReadingIncorrectType && realDataType.isInstanceOf[YearMonthIntervalType] =>
throw QueryCompilationErrors.avroIncompatibleReadError(toFieldStr(avroPath),
Expand Down Expand Up @@ -194,6 +200,9 @@ private[sql] class AvroDeserializer(
case (FLOAT, FloatType) => (updater, ordinal, value) =>
updater.setFloat(ordinal, value.asInstanceOf[Float])

case (FLOAT, DoubleType) => (updater, ordinal, value) =>
updater.setDouble(ordinal, value.asInstanceOf[Float])

case (DOUBLE, DoubleType) => (updater, ordinal, value) =>
updater.setDouble(ordinal, value.asInstanceOf[Double])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,39 @@ abstract class AvroSuite
}
}

test("SPARK-49082: Widening type promotions in AvroDeserializer") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing this (I posted the original ticket :D)!

Is there a plan to convert Date -> TimestampNTZ (Parquet PR) and Int -> Decimal (Parquet PR)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will seperate another two PR.

withTempPath { tempPath =>
// Int -> Long
val intPath = s"$tempPath/int_data"
val intDf = Seq(1, Int.MinValue, Int.MaxValue).toDF("col")
intDf.write.format("avro").save(intPath)
checkAnswer(
spark.read.schema("col Long").format("avro").load(intPath),
Seq(Row(1L), Row(-2147483648L), Row(2147483647L))
)

// Int -> Double
checkAnswer(
spark.read.schema("col Double").format("avro").load(intPath),
Seq(Row(1D), Row(-2147483648D), Row(2147483647D))
)

// Float -> Double
val floatPath = s"$tempPath/float_data"
val floatDf = Seq(1F,
Float.MinValue, Float.MinPositiveValue, Float.MaxValue,
Float.NaN, Float.NegativeInfinity, Float.PositiveInfinity
).toDF("col")
floatDf.write.format("avro").save(floatPath)
checkAnswer(
spark.read.schema("col Double").format("avro").load(floatPath),
Seq(Row(1D),
Row(-3.4028234663852886E38D), Row(1.401298464324817E-45D), Row(3.4028234663852886E38D),
Row(Double.NaN), Row(Double.NegativeInfinity), Row(Double.PositiveInfinity))
)
}
}

test("SPARK-43380: Fix Avro data type conversion" +
" of DayTimeIntervalType to avoid producing incorrect results") {
withTempPath { path =>
Expand Down