You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/07/02 19:57:42 UTC

[GitHub] [spark] mgaido91 commented on a change in pull request #21599: [SPARK-26218][SQL] Overflow on arithmetic operations returns incorrect result

mgaido91 commented on a change in pull request #21599: [SPARK-26218][SQL] Overflow on arithmetic operations returns incorrect result
URL: https://github.com/apache/spark/pull/21599#discussion_r299659190
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
 ##########
 @@ -129,17 +131,41 @@ abstract class BinaryArithmetic extends BinaryOperator with NullIntolerant {
   def calendarIntervalMethod: String =
     sys.error("BinaryArithmetics must override either calendarIntervalMethod or genCode")
 
+  def checkOverflowCode(result: String, op1: String, op2: String): String =
+    sys.error("BinaryArithmetics must override either checkOverflowCode or genCode")
+
   override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = dataType match {
     case _: DecimalType =>
       defineCodeGen(ctx, ev, (eval1, eval2) => s"$eval1.$decimalMethod($eval2)")
     case CalendarIntervalType =>
       defineCodeGen(ctx, ev, (eval1, eval2) => s"$eval1.$calendarIntervalMethod($eval2)")
+    // In the following cases, overflow can happen, so we need to check the result is valid.
+    // Otherwise we throw an ArithmeticException
     // byte and short are casted into int when add, minus, times or divide
     case ByteType | ShortType =>
-      defineCodeGen(ctx, ev,
-        (eval1, eval2) => s"(${CodeGenerator.javaType(dataType)})($eval1 $symbol $eval2)")
+      nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
+        val overflowCheck = if (checkOverflow) {
+          checkOverflowCode(ev.value, eval1, eval2)
+        } else {
+          ""
+        }
+        s"""
+           |${ev.value} = (${CodeGenerator.javaType(dataType)})($eval1 $symbol $eval2);
+           |$overflowCheck
+         """.stripMargin
+      })
     case _ =>
-      defineCodeGen(ctx, ev, (eval1, eval2) => s"$eval1 $symbol $eval2")
+      nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
+        val overflowCheck = if (checkOverflow) {
+          checkOverflowCode(ev.value, eval1, eval2)
+        } else {
+          ""
+        }
+        s"""
+           |${ev.value} = $eval1 $symbol $eval2;
 
 Review comment:
   yes, this is good point actually. As I mentioned in https://github.com/apache/spark/pull/21599#issuecomment-505924180, the issue with this is that `addExact` exists only for few datatypes. So we should specialize many cases in the code here. So here we have a tradeoff between performances (in the check overflow case only) and code maintainability. What is your opinion on this?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org