This is an automated email from the ASF dual-hosted git repository.

MaxGekk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new ed83bbf762fb [SPARK-60746][SQL] Raise `INTERVAL_ARITHMETIC_OVERFLOW` 
for an overflowing interval division in every evaluation mode
ed83bbf762fb is described below

commit ed83bbf762fb1e2d8ac990f6976ef047efcc5c8d
Author: Max Gekk <[email protected]>
AuthorDate: Mon Nov 9 07:37:34 2026 +0100

    [SPARK-60746][SQL] Raise `INTERVAL_ARITHMETIC_OVERFLOW` for an overflowing 
interval division in every evaluation mode
    
    ### What changes were proposed in this pull request?
    
    Dividing the minimum ANSI interval by `-1` overflows. `DivideYMInterval` 
and `DivideDTInterval` detect that case in both of their evaluation paths, but 
they raised different errors:
    
    - **Interpreted** (`IntervalDivide.checkDivideOverflow`): 
`INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION`, SQLSTATE 22015, suggesting 
`try_divide`. The interpreted path has raised the interval error class since 
the check was added; SPARK-50226 moved it to the `WITH_SUGGESTION` 
sub-condition.
    - **Generated code** (`doGenCode`): `ARITHMETIC_OVERFLOW`, SQLSTATE 22003, 
"Overflow in integral divide", through 
`QueryExecutionErrors.overflowInIntegralDivideError`, since the generated code 
had its own copy of the check.
    
    This PR makes both expressions call one function per interval type and kind 
of divisor, `IntervalMathUtils.divide(months | micros, num, context)`, from the 
interpreted path and from the generated code, as `Add`, `Subtract` and 
`UnaryMinus` do with `IntervalMathUtils.addExact` and the like. The function 
holds the division by zero check, the overflow checks and the error, with the 
query context, so the two paths cannot drift apart. `doGenCode` is a single 
call with no generated checks,  [...]
    
    The same expressions also overflow with a decimal or a floating-point 
divisor, when the quotient, once rounded, does not fit the interval type, e.g. 
the minimum interval divided by `-1.0` or `-1.0D`, or when it is infinite, e.g. 
`INTERVAL '1' DAY / 1e-300D`. Both evaluation paths let the 
`java.lang.ArithmeticException` of `intValueExact`/`longValueExact` or 
`DoubleMath.roundToInt`/`roundToLong` escape, with no error condition and no 
SQLSTATE. The shared functions catch it and raise th [...]
    
    ### Why are the changes needed?
    
    The error class and SQLSTATE a user sees should not depend on whether 
whole-stage codegen ran. On Spark 4.2.0, with `spark.sql.ansi.enabled=true`:
    
    ```sql
    CREATE TABLE t USING parquet AS SELECT INTERVAL '-106751991 
04:00:54.775808' DAY TO SECOND AS i;
    SELECT i / -1 FROM t;
    -- codegen:     [ARITHMETIC_OVERFLOW] Overflow in integral divide. ... 
SQLSTATE: 22003
    -- interpreted: [INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION] Integer 
overflow while operating
    --              with intervals. Use `try_divide` to tolerate overflow and 
return NULL instead.
    --              SQLSTATE: 22015
    ```
    
    The interpreted path was run with `spark.sql.codegen.wholeStage=false` and 
`spark.sql.codegen.factoryMode=NO_CODEGEN`; with the default factory mode, a 
projection uses generated code even with whole-stage codegen off. A literal 
operand is constant-folded through the interpreted path, so the SQL golden 
files already show the interval error for `-1`.
    
    With a decimal or a floating-point divisor, the overflow is a bare JVM 
exception in both modes, as the golden file `interval.sql.out` showed before 
this PR:
    
    ```sql
    SELECT (INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0;
    -- java.lang.ArithmeticException: Overflow
    SELECT (INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0D;
    -- java.lang.ArithmeticException: rounded value is out of range for input 
2.147483648E9 and rounding mode HALF_UP
    SELECT INTERVAL '1' DAY / 1e-300D;
    -- java.lang.ArithmeticException: input is infinite or NaN
    ```
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. In generated code, dividing the minimum year-month or day-time 
interval by `-1` now raises `INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION` 
(SQLSTATE 22015) instead of `ARITHMETIC_OVERFLOW` (SQLSTATE 22003). This 
matches what the interpreted path, and therefore constant-folded queries, 
already raise. Dividing an interval by a decimal or a floating-point number 
whose quotient, once rounded, does not fit the interval type, or is infinite, 
raises the same error, in both modes, instead [...]
    
    ### How was this patch tested?
    
    Two new tests in `IntervalExpressionsSuite` check the whole error message, 
with the error condition and the suggested `try_divide`, in both `CODEGEN_ONLY` 
and `NO_CODEGEN` mode (`checkErrorInExpression` cannot match the empty query 
context the error carries). One covers `DivideYMInterval` and 
`DivideDTInterval` with a `-1` of each integral type, as a decimal, as a float 
and as a double; the other covers a quotient that is out of range once rounded, 
with a decimal and a double divisor, [...]
    
    The golden files of `interval.sql` (`interval.sql.out` and 
`nonansi/interval.sql.out`) are regenerated: the four queries that divide the 
minimum intervals by `-1.0` and `-1.0D`, and the two that divide `2147483647 
months` and `-2147483648 months` by `0.5`, now show 
`INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION` instead of the bare 
`java.lang.ArithmeticException`.
    
    ```
    build/sbt 'catalyst/testOnly *IntervalExpressionsSuite'
    build/sbt 'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z 
interval'
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Opus 5.5)
    
    
    
    Closes #59956 from MaxGekk/SPARK-60746-interval-divide-overflow.
    
    Authored-by: Max Gekk <[email protected]>
    Signed-off-by: Max Gekk <[email protected]>
    
    
    skip-checks: true
---
 docs/sql-migration-guide.md                        |   1 +
 .../catalyst/expressions/intervalExpressions.scala | 160 ++++-----------------
 .../sql/catalyst/util/IntervalMathUtils.scala      |  94 ++++++++++--
 .../expressions/IntervalExpressionsSuite.scala     |  40 +++++-
 .../resources/sql-tests/results/interval.sql.out   | 102 +++++++++++--
 .../sql-tests/results/nonansi/interval.sql.out     | 102 +++++++++++--
 6 files changed, 332 insertions(+), 167 deletions(-)

diff --git a/docs/sql-migration-guide.md b/docs/sql-migration-guide.md
index 3dc35e778643..9d08a61d7460 100644
--- a/docs/sql-migration-guide.md
+++ b/docs/sql-migration-guide.md
@@ -29,6 +29,7 @@ license: |
 
 ## Upgrading from Spark SQL 4.3 to 4.4
 
+- Since Spark 4.4, dividing an ANSI interval by a number raises 
`INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION` (SQLSTATE 22015) whenever the 
result does not fit the interval type, in every evaluation mode. Previously, 
dividing the minimum year-month or day-time interval by `-1` raised 
`ARITHMETIC_OVERFLOW` (SQLSTATE 22003) in generated code, and dividing an 
interval by a decimal or a floating-point number whose quotient, once rounded, 
does not fit the type, or is infinite, raised a `java [...]
 - Since Spark 4.4, when a file source reads a partition column with a 
user-specified `DECIMAL` type from directory names, it converts each value to 
the precision and scale of that type, rounding half up as `CAST` does. 
Previously, the vectorized Parquet and ORC readers applied the declared scale 
to the digits in the directory name without an error, for example `d=1.5` read 
as `DECIMAL(10,2)` returned `0.15`. Data derived from affected query results 
may need to be recomputed. A value that [...]
 - Since Spark 4.4, when a file source reads a partition column with a 
user-specified `TINYINT` or `SMALLINT` type from directory names, a value out 
of the range of that type fails with `INVALID_PARTITION_VALUE` regardless of 
`spark.sql.ansi.enabled`, as it does for `INT`. Previously, the value wrapped 
around without an error, for example `b=300` read as `TINYINT` returned `44`. 
Data derived from affected query results may need to be recomputed. To get 
`NULL` for such a value, set `spark. [...]
 - Since Spark 4.4, a cast from `DECIMAL(19, 0)` to `BIGINT` is no longer 
treated as an up-cast because it can overflow. Previously, the optimizer could 
remove such a cast or unwrap it in a comparison, so a query returned a wrong 
result for a value out of the range of `BIGINT` instead of failing with 
`CAST_OVERFLOW`, or instead of using the wrapped-around value when 
`spark.sql.ansi.enabled` is `false`. For example, for a `DECIMAL(19, 0)` column 
`d` with the value `9999999999999999999`, `t [...]
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
index f80f14d06f72..128ffb42eee6 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
@@ -20,13 +20,13 @@ package org.apache.spark.sql.catalyst.expressions
 import java.math.RoundingMode
 import java.util.Locale
 
-import com.google.common.math.{DoubleMath, IntMath, LongMath}
+import com.google.common.math.DoubleMath
 
 import org.apache.spark.QueryContext
-import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, 
CodeGenerator, ExprCode}
-import org.apache.spark.sql.catalyst.util.IntervalUtils
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, 
ExprCode}
+import org.apache.spark.sql.catalyst.util.{IntervalMathUtils, IntervalUtils}
 import org.apache.spark.sql.catalyst.util.IntervalUtils._
-import org.apache.spark.sql.errors.{QueryCompilationErrors, 
QueryExecutionErrors}
+import org.apache.spark.sql.errors.QueryCompilationErrors
 import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.sql.types._
 import org.apache.spark.sql.types.DayTimeIntervalType.{DAY, HOUR, MINUTE, 
SECOND}
@@ -729,42 +729,22 @@ case class MultiplyDTInterval(
     copy(interval = newLeft, num = newRight)
 }
 
-trait IntervalDivide {
-  def checkDivideOverflow(
-      value: Any,
-      minValue: Any,
-      num: Expression,
-      numValue: Any,
-      context: QueryContext): Unit = {
-    if (value == minValue && num.dataType.isInstanceOf[IntegralType]) {
-      if (numValue.asInstanceOf[Number].longValue() == -1) {
-        throw 
QueryExecutionErrors.withSuggestionIntervalArithmeticOverflowError(
-          "try_divide", context)
-      }
+/**
+ * The division of an interval by a number. The interpreted path and the 
generated code both call
+ * the `IntervalMathUtils.divide` overload for the interval type and the kind 
of the divisor, which
+ * holds the checks and the error, so that the two paths cannot drift apart.
+ */
+trait IntervalDivide extends BinaryExpression {
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    val errorContext = ctx.addReferenceObj("errCtx", origin.context)
+    val mathUtils = 
IntervalMathUtils.getClass.getCanonicalName.stripSuffix("$")
+    // The divisor is widened to the type of the overload, as the interpreted 
path does.
+    val divisor = right.dataType match {
+      case _: IntegralType => "(long)"
+      case _: DecimalType => ""
+      case _: FractionalType => "(double)"
     }
-  }
-
-  def divideByZeroCheck(
-      dataType: DataType,
-      num: Any,
-      context: QueryContext): Unit = dataType match {
-    case _: DecimalType =>
-      if (num.asInstanceOf[Decimal].isZero) {
-        throw QueryExecutionErrors.intervalDividedByZeroError(context)
-      }
-    case _ => if (num == 0) throw 
QueryExecutionErrors.intervalDividedByZeroError(context)
-  }
-
-  def divideByZeroCheckCodegen(
-      dataType: DataType,
-      value: String,
-      errorContextReference: String): String = dataType match {
-    // scalastyle:off line.size.limit
-    case _: DecimalType =>
-      s"if ($value.isZero()) throw 
QueryExecutionErrors.intervalDividedByZeroError($errorContextReference);"
-    case _ =>
-      s"if ($value == 0) throw 
QueryExecutionErrors.intervalDividedByZeroError($errorContextReference);"
-    // scalastyle:on line.size.limit
+    defineCodeGen(ctx, ev, (m, n) => s"$mathUtils.divide($m, $divisor$n, 
$errorContext)")
   }
 }
 
@@ -772,7 +752,7 @@ trait IntervalDivide {
 case class DivideYMInterval(
     interval: Expression,
     num: Expression)
-  extends BinaryExpression with ImplicitCastInputTypes with IntervalDivide 
with Serializable {
+  extends IntervalDivide with ImplicitCastInputTypes with Serializable {
   override def nullIntolerant: Boolean = true
   override def left: Expression = interval
   override def right: Expression = num
@@ -782,66 +762,18 @@ case class DivideYMInterval(
 
   @transient
   private lazy val evalFunc: (Int, Any) => Any = right.dataType match {
-    case LongType => (months: Int, num) =>
-      // Year-month interval has `Int` as the internal type. The result of the 
divide operation
-      // of `Int` by `Long` must fit to `Int`. So, the casting to `Int` cannot 
cause overflow.
-      LongMath.divide(months, num.asInstanceOf[Long], 
RoundingMode.HALF_UP).toInt
     case _: IntegralType => (months: Int, num) =>
-      IntMath.divide(months, num.asInstanceOf[Number].intValue(), 
RoundingMode.HALF_UP)
+      IntervalMathUtils.divide(months, num.asInstanceOf[Number].longValue(), 
origin.context)
     case _: DecimalType => (months: Int, num) =>
-      val decimalRes = ((new Decimal).set(months) / 
num.asInstanceOf[Decimal]).toJavaBigDecimal
-      decimalRes.setScale(0, java.math.RoundingMode.HALF_UP).intValueExact()
+      IntervalMathUtils.divide(months, num.asInstanceOf[Decimal], 
origin.context)
     case _: FractionalType => (months: Int, num) =>
-      DoubleMath.roundToInt(months / num.asInstanceOf[Number].doubleValue(), 
RoundingMode.HALF_UP)
+      IntervalMathUtils.divide(months, num.asInstanceOf[Number].doubleValue(), 
origin.context)
   }
 
   override def nullSafeEval(interval: Any, num: Any): Any = {
-    checkDivideOverflow(
-      interval.asInstanceOf[Int], Int.MinValue, right, num, origin.context)
-    divideByZeroCheck(right.dataType, num, origin.context)
     evalFunc(interval.asInstanceOf[Int], num)
   }
 
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    val errorContext = ctx.addReferenceObj("errCtx", origin.context)
-    right.dataType match {
-      case t: IntegralType =>
-        val math = t match {
-          case LongType => classOf[LongMath].getName
-          case _ => classOf[IntMath].getName
-        }
-        val javaType = CodeGenerator.javaType(dataType)
-        nullSafeCodeGen(ctx, ev, (m, n) => {
-          val checkIntegralDivideOverflow =
-            s"""
-               |if ($m == ${Int.MinValue} && $n == -1)
-               |  throw 
QueryExecutionErrors.overflowInIntegralDivideError($errorContext);
-               |""".stripMargin
-          // Similarly to non-codegen code. The result of `divide(Int, Long, 
...)` must fit
-          // to `Int`. Casting to `Int` is safe here.
-          s"""
-             |${divideByZeroCheckCodegen(right.dataType, n, errorContext)}
-             |$checkIntegralDivideOverflow
-             |${ev.value} = ($javaType)$math.divide($m, $n, 
java.math.RoundingMode.HALF_UP);
-          """.stripMargin
-        })
-      case _: DecimalType =>
-        nullSafeCodeGen(ctx, ev, (m, n) =>
-          s"""
-             |${divideByZeroCheckCodegen(right.dataType, n, errorContext)}
-             |${ev.value} = ((new 
Decimal()).set($m).$$div($n)).toJavaBigDecimal()
-             |  .setScale(0, java.math.RoundingMode.HALF_UP).intValueExact();
-          """.stripMargin)
-      case _: FractionalType =>
-        val math = classOf[DoubleMath].getName
-        nullSafeCodeGen(ctx, ev, (m, n) =>
-          s"""
-             |${divideByZeroCheckCodegen(right.dataType, n, errorContext)}
-             |${ev.value} = $math.roundToInt($m / (double)$n, 
java.math.RoundingMode.HALF_UP);
-          """.stripMargin)
-    }
-  }
-
   override def toString: String = s"($left / $right)"
   override def sql: String = s"(${left.sql} / ${right.sql})"
 
@@ -854,7 +786,7 @@ case class DivideYMInterval(
 case class DivideDTInterval(
     interval: Expression,
     num: Expression)
-  extends BinaryExpression with ImplicitCastInputTypes with IntervalDivide 
with Serializable {
+  extends IntervalDivide with ImplicitCastInputTypes with Serializable {
   override def nullIntolerant: Boolean = true
 
   override def left: Expression = interval
@@ -866,55 +798,17 @@ case class DivideDTInterval(
   @transient
   private lazy val evalFunc: (Long, Any) => Any = right.dataType match {
     case _: IntegralType => (micros: Long, num) =>
-      LongMath.divide(micros, num.asInstanceOf[Number].longValue(), 
RoundingMode.HALF_UP)
+      IntervalMathUtils.divide(micros, num.asInstanceOf[Number].longValue(), 
origin.context)
     case _: DecimalType => (micros: Long, num) =>
-      val decimalRes = ((new Decimal).set(micros) / 
num.asInstanceOf[Decimal]).toJavaBigDecimal
-      decimalRes.setScale(0, java.math.RoundingMode.HALF_UP).longValueExact()
+      IntervalMathUtils.divide(micros, num.asInstanceOf[Decimal], 
origin.context)
     case _: FractionalType => (micros: Long, num) =>
-      DoubleMath.roundToLong(micros / num.asInstanceOf[Number].doubleValue(), 
RoundingMode.HALF_UP)
+      IntervalMathUtils.divide(micros, num.asInstanceOf[Number].doubleValue(), 
origin.context)
   }
 
   override def nullSafeEval(interval: Any, num: Any): Any = {
-    checkDivideOverflow(
-      interval.asInstanceOf[Long], Long.MinValue, right, num, origin.context)
-    divideByZeroCheck(right.dataType, num, origin.context)
     evalFunc(interval.asInstanceOf[Long], num)
   }
 
-  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    val errorContext = ctx.addReferenceObj("errCtx", origin.context)
-    right.dataType match {
-      case _: IntegralType =>
-        val math = classOf[LongMath].getName
-        nullSafeCodeGen(ctx, ev, (m, n) => {
-          val checkIntegralDivideOverflow =
-            s"""
-               |if ($m == ${Long.MinValue}L && $n == -1L)
-               |  throw 
QueryExecutionErrors.overflowInIntegralDivideError($errorContext);
-               |""".stripMargin
-          s"""
-             |${divideByZeroCheckCodegen(right.dataType, n, errorContext)}
-             |$checkIntegralDivideOverflow
-             |${ev.value} = $math.divide($m, $n, 
java.math.RoundingMode.HALF_UP);
-          """.stripMargin
-        })
-      case _: DecimalType =>
-        nullSafeCodeGen(ctx, ev, (m, n) =>
-          s"""
-             |${divideByZeroCheckCodegen(right.dataType, n, errorContext)}
-             |${ev.value} = ((new 
Decimal()).set($m).$$div($n)).toJavaBigDecimal()
-             |  .setScale(0, java.math.RoundingMode.HALF_UP).longValueExact();
-          """.stripMargin)
-      case _: FractionalType =>
-        val math = classOf[DoubleMath].getName
-        nullSafeCodeGen(ctx, ev, (m, n) =>
-          s"""
-             |${divideByZeroCheckCodegen(right.dataType, n, errorContext)}
-             |${ev.value} = $math.roundToLong($m / (double)$n, 
java.math.RoundingMode.HALF_UP);
-          """.stripMargin)
-    }
-  }
-
   override def toString: String = s"($left / $right)"
   override def sql: String = s"(${left.sql} / ${right.sql})"
 
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalMathUtils.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalMathUtils.scala
index 756f2598f13f..233bcb2ebee2 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalMathUtils.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalMathUtils.scala
@@ -16,34 +16,110 @@
  */
 package org.apache.spark.sql.catalyst.util
 
+import java.math.RoundingMode
+
+import com.google.common.math.{DoubleMath, LongMath}
+
+import org.apache.spark.QueryContext
 import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.types.Decimal
 
 /**
  * Helper functions for interval arithmetic operations with overflow.
  */
 object IntervalMathUtils {
 
-  def addExact(a: Int, b: Int): Int = withOverflow(Math.addExact(a, b), 
"try_add")
+  def addExact(a: Int, b: Int): Int = withOverflow(Math.addExact(a, b), 
"try_add", null)
+
+  def addExact(a: Long, b: Long): Long = withOverflow(Math.addExact(a, b), 
"try_add", null)
 
-  def addExact(a: Long, b: Long): Long = withOverflow(Math.addExact(a, b), 
"try_add")
+  def subtractExact(a: Int, b: Int): Int =
+    withOverflow(Math.subtractExact(a, b), "try_subtract", null)
 
-  def subtractExact(a: Int, b: Int): Int = withOverflow(Math.subtractExact(a, 
b), "try_subtract")
+  def subtractExact(a: Long, b: Long): Long =
+    withOverflow(Math.subtractExact(a, b), "try_subtract", null)
 
-  def subtractExact(a: Long, b: Long): Long = 
withOverflow(Math.subtractExact(a, b), "try_subtract")
+  def negateExact(a: Int): Int = withOverflow(Math.negateExact(a), "", null)
 
-  def negateExact(a: Int): Int = withOverflow(Math.negateExact(a))
+  def negateExact(a: Long): Long = withOverflow(Math.negateExact(a), "", null)
 
-  def negateExact(a: Long): Long = withOverflow(Math.negateExact(a))
+  // The divisions of a year-month interval, in months, and of a day-time 
interval, in micros, by
+  // a number, rounded half up. The interpreted and the generated code of 
`DivideYMInterval` and
+  // `DivideDTInterval` both call them, so that the checks and the error are 
in one place. A zero
+  // divisor raises `INTERVAL_DIVIDED_BY_ZERO`, and a quotient that does not 
fit the interval
+  // type raises the interval overflow error with `try_divide` suggested: the 
minimum interval
+  // divided by -1, or a decimal or fractional quotient out of the range once 
rounded, an infinite
+  // one included. A NaN quotient, from a NaN divisor, is not an overflow and 
is left to the
+  // rounding, which raises for it.
+
+  def divide(months: Int, num: Long, context: QueryContext): Int = {
+    checkDivisor(num == 0, context)
+    if (months == Int.MinValue && num == -1) {
+      throw QueryExecutionErrors.withSuggestionIntervalArithmeticOverflowError(
+        "try_divide", context)
+    }
+    // The quotient of an Int by a Long fits an Int.
+    LongMath.divide(months, num, RoundingMode.HALF_UP).toInt
+  }
+
+  def divide(months: Int, num: Decimal, context: QueryContext): Int = {
+    checkDivisor(num.isZero, context)
+    withOverflow(
+      ((new Decimal).set(months) / num).toJavaBigDecimal
+        .setScale(0, RoundingMode.HALF_UP).intValueExact(),
+      "try_divide", context)
+  }
+
+  def divide(months: Int, num: Double, context: QueryContext): Int = {
+    checkDivisor(num == 0, context)
+    val quotient = months / num
+    if (quotient.isNaN) {
+      DoubleMath.roundToInt(quotient, RoundingMode.HALF_UP)
+    } else {
+      withOverflow(DoubleMath.roundToInt(quotient, RoundingMode.HALF_UP), 
"try_divide", context)
+    }
+  }
+
+  def divide(micros: Long, num: Long, context: QueryContext): Long = {
+    checkDivisor(num == 0, context)
+    if (micros == Long.MinValue && num == -1) {
+      throw QueryExecutionErrors.withSuggestionIntervalArithmeticOverflowError(
+        "try_divide", context)
+    }
+    LongMath.divide(micros, num, RoundingMode.HALF_UP)
+  }
+
+  def divide(micros: Long, num: Decimal, context: QueryContext): Long = {
+    checkDivisor(num.isZero, context)
+    withOverflow(
+      ((new Decimal).set(micros) / num).toJavaBigDecimal
+        .setScale(0, RoundingMode.HALF_UP).longValueExact(),
+      "try_divide", context)
+  }
+
+  def divide(micros: Long, num: Double, context: QueryContext): Long = {
+    checkDivisor(num == 0, context)
+    val quotient = micros / num
+    if (quotient.isNaN) {
+      DoubleMath.roundToLong(quotient, RoundingMode.HALF_UP)
+    } else {
+      withOverflow(DoubleMath.roundToLong(quotient, RoundingMode.HALF_UP), 
"try_divide", context)
+    }
+  }
+
+  private def checkDivisor(isZero: Boolean, context: QueryContext): Unit = {
+    if (isZero) throw QueryExecutionErrors.intervalDividedByZeroError(context)
+  }
 
-  private def withOverflow[A](f: => A, suggestedFunc: String = ""): A = {
+  private def withOverflow[A](f: => A, suggestedFunc: String, context: 
QueryContext): A = {
     try {
       f
     } catch {
       case _: ArithmeticException if suggestedFunc.isEmpty =>
-        throw 
QueryExecutionErrors.withoutSuggestionIntervalArithmeticOverflowError(context = 
null)
+        throw 
QueryExecutionErrors.withoutSuggestionIntervalArithmeticOverflowError(context)
       case _: ArithmeticException =>
         throw 
QueryExecutionErrors.withSuggestionIntervalArithmeticOverflowError(
-          suggestedFunc, context = null)
+          suggestedFunc, context)
     }
   }
 }
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala
index e7d8620d8c61..babd0868cc0c 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/IntervalExpressionsSuite.scala
@@ -22,7 +22,7 @@ import java.time.temporal.ChronoUnit
 
 import scala.language.implicitConversions
 
-import org.apache.spark.SparkFunSuite
+import org.apache.spark.{SparkArithmeticException, SparkFunSuite}
 import org.apache.spark.sql.catalyst.analysis.TypeCoercion
 import org.apache.spark.sql.catalyst.util.{DateTimeTestUtils, IntervalUtils}
 import org.apache.spark.sql.catalyst.util.DateTimeConstants._
@@ -476,6 +476,44 @@ class IntervalExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     }
   }
 
+  // The error of an interval division that overflows. It carries an empty 
query context, which
+  // `checkErrorInExpression` cannot match, so the whole message is checked, 
with the suggested
+  // function.
+  private val intervalDivideOverflowErrMsg =
+    "[INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION] Integer overflow while 
operating with " +
+      "intervals. Use `try_divide` to tolerate overflow and return NULL 
instead."
+
+  test("SPARK-60746: an interval division whose quotient does not fit raises 
the overflow error") {
+    // A quotient that is out of the range of the interval type once rounded, 
or infinite, from
+    // a decimal or a fractional divisor, in every mode. A NaN quotient is not 
an overflow, see
+    // the SPARK-34868 and SPARK-34875 tests.
+    Seq(
+      DivideYMInterval(Literal(Period.ofMonths(Int.MaxValue)), 
Literal(Decimal(0.5))),
+      DivideYMInterval(Literal(Period.ofMonths(Int.MaxValue)), 
Literal(1e-300d)),
+      DivideDTInterval(Literal(Duration.ofDays(1)), Literal(1e-290d)),
+      DivideDTInterval(Literal(Duration.ofDays(1)), Literal(1e-300d))
+    ).foreach { expr =>
+      withClue(s"$expr: ") {
+        checkExceptionInExpression[SparkArithmeticException](expr, 
intervalDivideOverflowErrMsg)
+      }
+    }
+  }
+
+  test("SPARK-60746: the min interval divided by -1 raises the same error in 
every mode") {
+    // An integral divisor is checked before the division, a decimal or a 
fractional one when the
+    // rounded quotient does not fit the interval type.
+    Seq(-1.toByte, -1.toShort, -1, -1L, Decimal(-1), -1.0f, -1.0d).foreach { 
num =>
+      withClue(s"divisor $num: ") {
+        checkExceptionInExpression[SparkArithmeticException](
+          DivideYMInterval(Literal(Period.ofMonths(Int.MinValue)), 
Literal(num)),
+          intervalDivideOverflowErrMsg)
+        checkExceptionInExpression[SparkArithmeticException](
+          DivideDTInterval(Literal(Duration.of(Long.MinValue, 
ChronoUnit.MICROS)), Literal(num)),
+          intervalDivideOverflowErrMsg)
+      }
+    }
+  }
+
   test("ANSI: extract years and months") {
     Seq(Period.ZERO,
       Period.ofMonths(100),
diff --git a/sql/core/src/test/resources/sql-tests/results/interval.sql.out 
b/sql/core/src/test/resources/sql-tests/results/interval.sql.out
index 99be15155ce2..2f92f61665d3 100644
--- a/sql/core/src/test/resources/sql-tests/results/interval.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/interval.sql.out
@@ -66,8 +66,21 @@ select interval 2147483647 month / 0.5
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 38,
+    "fragment" : "interval 2147483647 month / 0.5"
+  } ]
+}
 
 
 -- !query
@@ -2588,8 +2601,21 @@ select a / 0.5 from values (interval '-2147483648 
months', interval '2147483647
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 14,
+    "fragment" : "a / 0.5"
+  } ]
+}
 
 
 -- !query
@@ -2920,8 +2946,21 @@ SELECT (INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 53,
+    "fragment" : "(INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0"
+  } ]
+}
 
 
 -- !query
@@ -2929,8 +2968,21 @@ SELECT (INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0D
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-rounded value is out of range for input 2.147483648E9 and rounding mode HALF_UP
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 54,
+    "fragment" : "(INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0D"
+  } ]
+}
 
 
 -- !query
@@ -2998,8 +3050,21 @@ SELECT (INTERVAL '-106751991 04:00:54.775808' DAY TO 
SECOND) / -1.0
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 67,
+    "fragment" : "(INTERVAL '-106751991 04:00:54.775808' DAY TO SECOND) / -1.0"
+  } ]
+}
 
 
 -- !query
@@ -3007,8 +3072,21 @@ SELECT (INTERVAL '-106751991 04:00:54.775808' DAY TO 
SECOND) / -1.0D
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-rounded value is out of range for input 9.223372036854776E18 and rounding mode 
HALF_UP
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 68,
+    "fragment" : "(INTERVAL '-106751991 04:00:54.775808' DAY TO SECOND) / 
-1.0D"
+  } ]
+}
 
 
 -- !query
diff --git 
a/sql/core/src/test/resources/sql-tests/results/nonansi/interval.sql.out 
b/sql/core/src/test/resources/sql-tests/results/nonansi/interval.sql.out
index 0e25cd3045b5..3fc13d67d610 100644
--- a/sql/core/src/test/resources/sql-tests/results/nonansi/interval.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/nonansi/interval.sql.out
@@ -66,8 +66,21 @@ select interval 2147483647 month / 0.5
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 38,
+    "fragment" : "interval 2147483647 month / 0.5"
+  } ]
+}
 
 
 -- !query
@@ -2401,8 +2414,21 @@ select a / 0.5 from values (interval '-2147483648 
months', interval '2147483647
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 14,
+    "fragment" : "a / 0.5"
+  } ]
+}
 
 
 -- !query
@@ -2733,8 +2759,21 @@ SELECT (INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 53,
+    "fragment" : "(INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0"
+  } ]
+}
 
 
 -- !query
@@ -2742,8 +2781,21 @@ SELECT (INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0D
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-rounded value is out of range for input 2.147483648E9 and rounding mode HALF_UP
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 54,
+    "fragment" : "(INTERVAL '-178956970-8' YEAR TO MONTH) / -1.0D"
+  } ]
+}
 
 
 -- !query
@@ -2811,8 +2863,21 @@ SELECT (INTERVAL '-106751991 04:00:54.775808' DAY TO 
SECOND) / -1.0
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-Overflow
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 67,
+    "fragment" : "(INTERVAL '-106751991 04:00:54.775808' DAY TO SECOND) / -1.0"
+  } ]
+}
 
 
 -- !query
@@ -2820,8 +2885,21 @@ SELECT (INTERVAL '-106751991 04:00:54.775808' DAY TO 
SECOND) / -1.0D
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArithmeticException
-rounded value is out of range for input 9.223372036854776E18 and rounding mode 
HALF_UP
+org.apache.spark.SparkArithmeticException
+{
+  "errorClass" : "INTERVAL_ARITHMETIC_OVERFLOW.WITH_SUGGESTION",
+  "sqlState" : "22015",
+  "messageParameters" : {
+    "functionName" : "`try_divide`"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 8,
+    "stopIndex" : 68,
+    "fragment" : "(INTERVAL '-106751991 04:00:54.775808' DAY TO SECOND) / 
-1.0D"
+  } ]
+}
 
 
 -- !query


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to