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_r314973812
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 ##########
 @@ -1182,6 +1234,79 @@ case class Cast(child: Expression, dataType: DataType, 
timeZoneId: Option[String
       (c, evPrim, evNull) => code"$evPrim = $c != 0;"
   }
 
+  private[this] def castTimestampToIntegerCode(
+      ctx: CodegenContext,
+      intType: String): CastFunction = {
+    if (failOnIntegerOverflow) {
+      val longValue = ctx.freshName("longValue")
+      (c, evPrim, evNull) =>
+        code"""
+          long $longValue = ${timestampToIntegerCode(c)};
+          if ($longValue == ($intType) $longValue) {
+            $evPrim = ($intType) $longValue;
+          } else {
+            throw new ArithmeticException("Casting $c to ${intType.capitalize} 
causes overflow");
+          }
+        """
+    } else {
+      (c, evPrim, evNull) => code"$evPrim = ($intType) 
${timestampToIntegerCode(c)};"
+    }
+  }
+
+  private[this] def lowerAndUpperBound(intType: String): (Double, Double) = {
+    intType.toLowerCase(Locale.ROOT) match {
+      case "int" => (Int.MinValue - 1.0, Int.MaxValue.toLong + 1.0)
+      case "short" => (Short.MinValue - 1.0, Short.MaxValue + 1.0)
+      case "byte" => (Byte.MinValue - 1.0, Byte.MaxValue + 1.0)
+    }
+  }
+
+  private[this] def castDecimalToIntegerCode(
+      ctx: CodegenContext,
+      intType: String): CastFunction = {
 
 Review comment:
   It's actually `integerType`. But the full name makes some code longer than 
100 characters. 
   I can change it if you think it is misleading.

----------------------------------------------------------------
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]

Reply via email to