gengliangwang 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_r316901682
########## 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: I think it is always good to have more test cases here if it doesn't increase the testing time by a few seconds. For example, If casting double to byte is implemented as: ``` val x = doubleValue.toShort if (x.toByte == x) { x.toByte } else { throw new ... } ``` We can find that it is wrong with this test case, because ``` (Int.MaxValue+1.0).toShort.toByte == (Int.MaxValue+1.0).toShort ``` is true. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org