cloud-fan commented on a change in pull request #25461: [SPARK-28741][SQL]Throw
exceptions when casting to integers causes overflow
URL: https://github.com/apache/spark/pull/25461#discussion_r316693418
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
##########
@@ -1075,4 +1075,113 @@ class CastSuite extends SparkFunSuite with
ExpressionEvalHelper {
checkEvaluation(cast("badvalue", dataType), null)
}
}
+
+ private def testIntMaxAndMin(dt: DataType): Unit = {
+ Seq(Int.MaxValue + 1L, Int.MinValue - 1L).foreach { value =>
+ checkExceptionInExpression[ArithmeticException](cast(value, dt),
"overflow")
+
checkExceptionInExpression[ArithmeticException](cast(Decimal(value.toString),
dt), "overflow")
+ checkExceptionInExpression[ArithmeticException](
+ cast(Literal(value * MICROS_PER_SECOND, TimestampType), dt),
"overflow")
+ checkExceptionInExpression[ArithmeticException](
+ cast(Literal(value * 1.5f, FloatType), dt), "overflow")
+ checkExceptionInExpression[ArithmeticException](
+ cast(Literal(value * 1.0, DoubleType), dt), "overflow")
+ }
+ }
+
+ private def testLongMaxAndMin(dt: DataType): Unit = {
+ Seq(Decimal(Long.MaxValue) + Decimal(1), Decimal(Long.MinValue) -
Decimal(1)).foreach { value =>
+ checkExceptionInExpression[ArithmeticException](
+ cast(value, dt), "overflow")
+ checkExceptionInExpression[ArithmeticException](
+ cast((value * Decimal(1.1)).toFloat, dt), "overflow")
+ checkExceptionInExpression[ArithmeticException](
+ cast((value * Decimal(1.1)).toDouble, dt), "overflow")
+ }
+ }
+
+ test("Cast to byte with option FAIL_ON_INTEGER_OVERFLOW enabled") {
+ withSQLConf(SQLConf.FAIL_ON_INTEGER_OVERFLOW.key -> "true") {
+ testIntMaxAndMin(ByteType)
Review comment:
why we need to test `cast int.max +1 to byte`? I think it's good enough to
test `cast byte.max +1 to byte`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]