cloud-fan commented on a change in pull request #26995: [SPARK-30341][SQL] 
Overflow check for interval arithmetic operations
URL: https://github.com/apache/spark/pull/26995#discussion_r361084666
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
 ##########
 @@ -75,12 +80,29 @@ case class UnaryMinus(child: Expression) extends 
UnaryExpression
       """})
     case _: CalendarIntervalType =>
       val iu = IntervalUtils.getClass.getCanonicalName.stripSuffix("$")
-      defineCodeGen(ctx, ev, c => s"$iu.negate($c)")
+      nullSafeCodeGen(ctx, ev, interval => s"""
+        try {
+          ${ev.value} = $iu.negate($interval);
+        } catch (ArithmeticException e) {
+          if ($checkOverflow) {
+            throw new ArithmeticException("-($interval) caused interval 
overflow.");
+          } else {
+            ${ev.isNull} = true;
+          }
+        }
+      """)
   }
 
   protected override def nullSafeEval(input: Any): Any = dataType match {
-    case CalendarIntervalType => 
IntervalUtils.negate(input.asInstanceOf[CalendarInterval])
-    case  _ => numeric.negate(input)
+    case CalendarIntervalType =>
+      try {
+      IntervalUtils.negate(input.asInstanceOf[CalendarInterval])
 
 Review comment:
   nit: indentation

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

Reply via email to