-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-28503][SQL] Return null result on cast an out-of-range value to a integral type #25300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
83190e5
f140561
b504f2e
e62551a
a19535d
e087b1a
9a262c0
a429869
63a6e62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1023,4 +1023,103 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| checkEvaluation(ret, InternalRow(null)) | ||
| } | ||
| } | ||
|
|
||
| private def testIntMaxAndMin(dt: DataType): Unit = { | ||
| Seq(Int.MaxValue + 1L, Int.MinValue - 1L).foreach { value => | ||
| checkEvaluation(cast(value, dt), null) | ||
| checkEvaluation(cast(value.toString, dt), null) | ||
| checkEvaluation(cast(Decimal(value.toString), dt), null) | ||
| checkEvaluation(cast(Literal(value * MICROS_PER_SECOND, TimestampType), dt), null) | ||
| checkEvaluation(cast(Literal(value * 2.0f, FloatType), dt), null) | ||
| checkEvaluation(cast(Literal(value * 1.0, DoubleType), dt), null) | ||
| } | ||
| } | ||
|
|
||
| private def testLongMaxAndMin(dt: DataType): Unit = { | ||
| Seq(Decimal(Long.MaxValue) + Decimal(1), Decimal(Long.MinValue) - Decimal(1)).foreach { value => | ||
| checkEvaluation(cast(value.toString, dt), null) | ||
| checkEvaluation(cast(value, dt), null) | ||
| checkEvaluation(cast((value * Decimal(1.1)).toFloat, dt), null) | ||
| checkEvaluation(cast((value * Decimal(1.1)).toDouble, dt), null) | ||
| } | ||
| } | ||
|
|
||
| test("Cast to byte") { | ||
| testIntMaxAndMin(ByteType) | ||
| Seq(Byte.MaxValue + 1, Byte.MinValue - 1).foreach { value => | ||
| checkEvaluation(cast(value, ByteType), null) | ||
| checkEvaluation(cast(value.toString, ByteType), null) | ||
| checkEvaluation(cast(Decimal(value.toString), ByteType), null) | ||
| checkEvaluation(cast(Literal(value * MICROS_PER_SECOND, TimestampType), ByteType), null) | ||
| checkEvaluation(cast(Literal(value, DateType), ByteType), null) | ||
| checkEvaluation(cast(Literal(value * 1.0f, FloatType), ByteType), null) | ||
| checkEvaluation(cast(Literal(value * 1.0, DoubleType), ByteType), null) | ||
| } | ||
|
|
||
| Seq(Byte.MaxValue, 0.toByte, Byte.MinValue).foreach { value => | ||
| checkEvaluation(cast(value, ByteType), value) | ||
| checkEvaluation(cast(value.toString, ByteType), value) | ||
| checkEvaluation(cast(Decimal(value.toString), ByteType), value) | ||
| checkEvaluation(cast(Literal(value * MICROS_PER_SECOND, TimestampType), ByteType), value) | ||
| checkEvaluation(cast(Literal(value.toInt, DateType), ByteType), null) | ||
| checkEvaluation(cast(Literal(value * 1.0f, FloatType), ByteType), value) | ||
| checkEvaluation(cast(Literal(value * 1.0, DoubleType), ByteType), value) | ||
| } | ||
| } | ||
|
|
||
| test("Cast to short") { | ||
| testIntMaxAndMin(ShortType) | ||
| Seq(Short.MaxValue + 1, Short.MinValue - 1).foreach { value => | ||
| checkEvaluation(cast(value, ShortType), null) | ||
| checkEvaluation(cast(value.toString, ShortType), null) | ||
| checkEvaluation(cast(Decimal(value.toString), ShortType), null) | ||
| checkEvaluation(cast(Literal(value * MICROS_PER_SECOND, TimestampType), ShortType), null) | ||
| checkEvaluation(cast(Literal(value, DateType), ShortType), null) | ||
| checkEvaluation(cast(Literal(value * 1.0f, FloatType), ShortType), null) | ||
| checkEvaluation(cast(Literal(value * 1.0, DoubleType), ShortType), null) | ||
| } | ||
|
|
||
| Seq(Short.MaxValue, 0.toShort, Short.MinValue).foreach { value => | ||
| checkEvaluation(cast(value, ShortType), value) | ||
| checkEvaluation(cast(value.toString, ShortType), value) | ||
| checkEvaluation(cast(Decimal(value.toString), ShortType), value) | ||
| checkEvaluation(cast(Literal(value * MICROS_PER_SECOND, TimestampType), ShortType), value) | ||
| checkEvaluation(cast(Literal(value.toInt, DateType), ShortType), null) | ||
| checkEvaluation(cast(Literal(value * 1.0f, FloatType), ShortType), value) | ||
| checkEvaluation(cast(Literal(value * 1.0, DoubleType), ShortType), value) | ||
| } | ||
| } | ||
|
|
||
| test("Cast to int") { | ||
| testIntMaxAndMin(IntegerType) | ||
| testLongMaxAndMin(IntegerType) | ||
|
|
||
| Seq(Int.MaxValue, 0, Int.MinValue).foreach { value => | ||
| checkEvaluation(cast(value, IntegerType), value) | ||
| checkEvaluation(cast(value.toString, IntegerType), value) | ||
| checkEvaluation(cast(Decimal(value.toString), IntegerType), value) | ||
| checkEvaluation(cast(Literal(value * MICROS_PER_SECOND, TimestampType), IntegerType), value) | ||
| checkEvaluation(cast(Literal(value * 1.0, DoubleType), IntegerType), value) | ||
| } | ||
| checkEvaluation(cast(2147483647.4f, IntegerType), 2147483647) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this test, you tried to check this case? https://github.com/apache/spark/pull/25300/files#r309043580
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better to use |
||
| checkEvaluation(cast(-2147483648.4f, IntegerType), -2147483648) | ||
| checkEvaluation(cast(2147483647.4D, IntegerType), 2147483647) | ||
| checkEvaluation(cast(-2147483648.4D, IntegerType), -2147483648) | ||
| } | ||
|
|
||
| test("Cast to long") { | ||
| testLongMaxAndMin(LongType) | ||
|
|
||
| Seq(Long.MaxValue, 0, Long.MinValue).foreach { value => | ||
| checkEvaluation(cast(value, LongType), value) | ||
| checkEvaluation(cast(value.toString, LongType), value) | ||
| checkEvaluation(cast(Decimal(value.toString), LongType), value) | ||
| checkEvaluation(cast(Literal(value, TimestampType), LongType), | ||
| Math.floorDiv(value, MICROS_PER_SECOND)) | ||
| } | ||
| checkEvaluation(cast(9223372036854775807.4f, LongType), 9223372036854775807L) | ||
| checkEvaluation(cast(-9223372036854775808.4f, LongType), -9223372036854775808L) | ||
| checkEvaluation(cast(9223372036854775807.4D, LongType), 9223372036854775807L) | ||
| checkEvaluation(cast(-9223372036854775808.4D, LongType), -9223372036854775808L) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -246,6 +246,10 @@ class HiveCompatibilitySuite extends HiveQueryFileTest with BeforeAndAfter { | |
| // Limit clause without a ordering, which causes failure. | ||
| "orc_predicate_pushdown", | ||
|
|
||
| // On casting a out-of-range value to a integral type, Hive returns the low-order bits, while | ||
| // Spark returns null. | ||
| "udf_to_byte", | ||
|
|
||
| // Requires precision decimal support: | ||
| "udf_when", | ||
| "udf_case", | ||
|
|
@@ -1086,7 +1090,6 @@ class HiveCompatibilitySuite extends HiveQueryFileTest with BeforeAndAfter { | |
| "udf_sum", | ||
| "udf_tan", | ||
| "udf_tinyint", | ||
| "udf_to_byte", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change this behaviour? cc: @gatorsmile @dongjoon-hyun @wangyum
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is within expectation. There are two cases in The results will be null for Spark, while the result of HIve is not null. |
||
| "udf_to_date", | ||
| "udf_to_double", | ||
| "udf_to_float", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you changed this? You hit some test failures?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line 38
The grouping expr is casting a integer to byte, so the nullability is true.