From 00265b7279358790d3efbefd6a12c6eefe7faf72 Mon Sep 17 00:00:00 2001
From: Bhargava Vadlamani <fullcircle289@gmail.com>
Date: Thu, 25 Sep 2025 15:49:02 -0700
Subject: [PATCH 1/7] rebase

---
 native/spark-expr/benches/decimal_div.rs      |  4 ++-
 native/spark-expr/src/comet_scalar_funcs.rs   |  5 ++--
 native/spark-expr/src/math_funcs/div.rs       | 25 +++++++++++++------
 .../org/apache/comet/serde/arithmetic.scala   | 10 +-------
 .../apache/comet/CometExpressionSuite.scala   | 24 +++++++++++++++---
 5 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/native/spark-expr/benches/decimal_div.rs b/native/spark-expr/benches/decimal_div.rs
index 4262e81238..3ca3e42eb5 100644
--- a/native/spark-expr/benches/decimal_div.rs
+++ b/native/spark-expr/benches/decimal_div.rs
@@ -20,7 +20,7 @@ use arrow::compute::cast;
 use arrow::datatypes::DataType;
 use criterion::{criterion_group, criterion_main, Criterion};
 use datafusion::physical_plan::ColumnarValue;
-use datafusion_comet_spark_expr::{spark_decimal_div, spark_decimal_integral_div};
+use datafusion_comet_spark_expr::{spark_decimal_div, spark_decimal_integral_div, EvalMode};
 use std::hint::black_box;
 use std::sync::Arc;
 
@@ -48,6 +48,7 @@ fn criterion_benchmark(c: &mut Criterion) {
             black_box(spark_decimal_div(
                 black_box(&args),
                 black_box(&DataType::Decimal128(10, 4)),
+                EvalMode::Legacy,
             ))
         })
     });
@@ -57,6 +58,7 @@ fn criterion_benchmark(c: &mut Criterion) {
             black_box(spark_decimal_integral_div(
                 black_box(&args),
                 black_box(&DataType::Decimal128(10, 4)),
+                EvalMode::Legacy,
             ))
         })
     });
diff --git a/native/spark-expr/src/comet_scalar_funcs.rs b/native/spark-expr/src/comet_scalar_funcs.rs
index 393f57662e..19fa11e641 100644
--- a/native/spark-expr/src/comet_scalar_funcs.rs
+++ b/native/spark-expr/src/comet_scalar_funcs.rs
@@ -137,13 +137,14 @@ pub fn create_comet_physical_fun_with_eval_mode(
             make_comet_scalar_udf!("unhex", func, without data_type)
         }
         "decimal_div" => {
-            make_comet_scalar_udf!("decimal_div", spark_decimal_div, data_type)
+            make_comet_scalar_udf!("decimal_div", spark_decimal_div, data_type, eval_mode)
         }
         "decimal_integral_div" => {
             make_comet_scalar_udf!(
                 "decimal_integral_div",
                 spark_decimal_integral_div,
-                data_type
+                data_type,
+                eval_mode
             )
         }
         "checked_add" => {
diff --git a/native/spark-expr/src/math_funcs/div.rs b/native/spark-expr/src/math_funcs/div.rs
index 9fc6692c03..933b28c094 100644
--- a/native/spark-expr/src/math_funcs/div.rs
+++ b/native/spark-expr/src/math_funcs/div.rs
@@ -16,29 +16,33 @@
 // under the License.
 
 use crate::math_funcs::utils::get_precision_scale;
+use crate::{divide_by_zero_error, EvalMode};
 use arrow::array::{Array, Decimal128Array};
 use arrow::datatypes::{DataType, DECIMAL128_MAX_PRECISION};
+use arrow::error::ArrowError;
 use arrow::{
     array::{ArrayRef, AsArray},
     datatypes::Decimal128Type,
 };
 use datafusion::common::DataFusionError;
 use datafusion::physical_plan::ColumnarValue;
-use num::{BigInt, Signed, ToPrimitive};
+use num::{BigInt, Signed, ToPrimitive, Zero};
 use std::sync::Arc;
 
 pub fn spark_decimal_div(
     args: &[ColumnarValue],
     data_type: &DataType,
+    eval_mode: EvalMode,
 ) -> Result<ColumnarValue, DataFusionError> {
-    spark_decimal_div_internal(args, data_type, false)
+    spark_decimal_div_internal(args, data_type, false, eval_mode)
 }
 
 pub fn spark_decimal_integral_div(
     args: &[ColumnarValue],
     data_type: &DataType,
+    eval_mode: EvalMode,
 ) -> Result<ColumnarValue, DataFusionError> {
-    spark_decimal_div_internal(args, data_type, true)
+    spark_decimal_div_internal(args, data_type, true, eval_mode)
 }
 
 // Let Decimal(p3, s3) as return type i.e. Decimal(p1, s1) / Decimal(p2, s2) = Decimal(p3, s3).
@@ -50,6 +54,7 @@ fn spark_decimal_div_internal(
     args: &[ColumnarValue],
     data_type: &DataType,
     is_integral_div: bool,
+    eval_mode: EvalMode,
 ) -> Result<ColumnarValue, DataFusionError> {
     let left = &args[0];
     let right = &args[1];
@@ -80,9 +85,12 @@ fn spark_decimal_div_internal(
         let r_mul = ten.pow(r_exp);
         let five = BigInt::from(5);
         let zero = BigInt::from(0);
-        arrow::compute::kernels::arity::binary(left, right, |l, r| {
+        arrow::compute::kernels::arity::try_binary(left, right, |l, r| {
             let l = BigInt::from(l) * &l_mul;
             let r = BigInt::from(r) * &r_mul;
+            if eval_mode == EvalMode::Ansi && is_integral_div && r.is_zero() {
+                return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
+            }
             let div = if r.eq(&zero) { zero.clone() } else { &l / &r };
             let res = if is_integral_div {
                 div
@@ -91,14 +99,17 @@ fn spark_decimal_div_internal(
             } else {
                 div + &five
             } / &ten;
-            res.to_i128().unwrap_or(i128::MAX)
+            Ok(res.to_i128().unwrap_or(i128::MAX))
         })?
     } else {
         let l_mul = 10_i128.pow(l_exp);
         let r_mul = 10_i128.pow(r_exp);
-        arrow::compute::kernels::arity::binary(left, right, |l, r| {
+        arrow::compute::kernels::arity::try_binary(left, right, |l, r| {
             let l = l * l_mul;
             let r = r * r_mul;
+            if eval_mode == EvalMode::Ansi && is_integral_div && r.is_zero() {
+                return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
+            }
             let div = if r == 0 { 0 } else { l / r };
             let res = if is_integral_div {
                 div
@@ -107,7 +118,7 @@ fn spark_decimal_div_internal(
             } else {
                 div + 5
             } / 10;
-            res.to_i128().unwrap_or(i128::MAX)
+            Ok(res.to_i128().unwrap_or(i128::MAX))
         })?
     };
     let result = result.with_data_type(DataType::Decimal128(p3, s3));
diff --git a/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala b/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
index 4507dc1073..54b348f84d 100644
--- a/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
+++ b/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
@@ -180,14 +180,6 @@ object CometDivide extends CometExpressionSerde[Divide] with MathBase {
 
 object CometIntegralDivide extends CometExpressionSerde[IntegralDivide] with MathBase {
 
-  override def getSupportLevel(expr: IntegralDivide): SupportLevel = {
-    if (expr.evalMode == EvalMode.ANSI) {
-      Incompatible(Some("ANSI mode is not supported"))
-    } else {
-      Compatible(None)
-    }
-  }
-
   override def convert(
       expr: IntegralDivide,
       inputs: Seq[Attribute],
@@ -206,7 +198,7 @@ object CometIntegralDivide extends CometExpressionSerde[IntegralDivide] with Mat
       if (expr.right.dataType.isInstanceOf[DecimalType]) expr.right
       else Cast(expr.right, DecimalType(19, 0))
 
-    val rightExpr = nullIfWhenPrimitive(right)
+    val rightExpr = if (expr.evalMode != EvalMode.ANSI) right else nullIfWhenPrimitive(right)
 
     val dataType = (left.dataType, right.dataType) match {
       case (l: DecimalType, r: DecimalType) =>
diff --git a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
index f391d52f78..79c8566838 100644
--- a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
@@ -58,7 +58,7 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
   val ARITHMETIC_OVERFLOW_EXCEPTION_MSG =
     """org.apache.comet.CometNativeException: [ARITHMETIC_OVERFLOW] integer overflow. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error"""
   val DIVIDE_BY_ZERO_EXCEPTION_MSG =
-    """org.apache.comet.CometNativeException: [DIVIDE_BY_ZERO] Division by zero. Use `try_divide` to tolerate divisor being 0 and return NULL instead"""
+    """Division by zero. Use `try_divide` to tolerate divisor being 0 and return NULL instead"""
 
   test("compare true/false to negative zero") {
     Seq(false, true).foreach { dictionary =>
@@ -2948,7 +2948,6 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
   }
 
   test("ANSI support for divide (division by zero)") {
-    //    TODO : Support ANSI mode in Integral divide -
     val data = Seq((Integer.MIN_VALUE, 0))
     withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
       withParquetTable(data, "tbl") {
@@ -2969,7 +2968,6 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
   }
 
   test("ANSI support for divide (division by zero) float division") {
-    //    TODO : Support ANSI mode in Integral divide -
     val data = Seq((Float.MinPositiveValue, 0.0))
     withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
       withParquetTable(data, "tbl") {
@@ -2989,6 +2987,26 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("ANSI support for integral divide (division by zero)") {
+    val data = Seq((Integer.MIN_VALUE, 0))
+    withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+      withParquetTable(data, "tbl") {
+        val res = spark.sql("""
+                              |SELECT
+                              |  _1 div _2
+                              |  from tbl
+                              |  """.stripMargin)
+
+        checkSparkMaybeThrows(res) match {
+          case (Some(sparkExc), Some(cometExc)) =>
+            assert(cometExc.getMessage.contains(DIVIDE_BY_ZERO_EXCEPTION_MSG))
+            assert(sparkExc.getMessage.contains("Division by zero"))
+          case _ => fail("Exception should be thrown")
+        }
+      }
+    }
+  }
+
   test("test integral divide overflow for decimal") {
     if (isSpark40Plus) {
       Seq(true, false)

From 02132082161a936779a2d4c19aaac0c020b04ed7 Mon Sep 17 00:00:00 2001
From: Bhargava Vadlamani <fullcircle289@gmail.com>
Date: Thu, 25 Sep 2025 20:08:43 -0700
Subject: [PATCH 2/7] rebase

---
 native/spark-expr/src/math_funcs/div.rs                  | 6 ++++--
 .../main/scala/org/apache/comet/serde/arithmetic.scala   | 2 +-
 .../scala/org/apache/comet/CometExpressionSuite.scala    | 9 ++++++++-
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/native/spark-expr/src/math_funcs/div.rs b/native/spark-expr/src/math_funcs/div.rs
index 933b28c094..d93b96d2c1 100644
--- a/native/spark-expr/src/math_funcs/div.rs
+++ b/native/spark-expr/src/math_funcs/div.rs
@@ -88,7 +88,8 @@ fn spark_decimal_div_internal(
         arrow::compute::kernels::arity::try_binary(left, right, |l, r| {
             let l = BigInt::from(l) * &l_mul;
             let r = BigInt::from(r) * &r_mul;
-            if eval_mode == EvalMode::Ansi && is_integral_div && r.is_zero() {
+            if eval_mode == EvalMode::Ansi && r.is_zero() {
+                println!("ANSI MODE ENABLED AND R is ZERO !!!!");
                 return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
             }
             let div = if r.eq(&zero) { zero.clone() } else { &l / &r };
@@ -107,7 +108,8 @@ fn spark_decimal_div_internal(
         arrow::compute::kernels::arity::try_binary(left, right, |l, r| {
             let l = l * l_mul;
             let r = r * r_mul;
-            if eval_mode == EvalMode::Ansi && is_integral_div && r.is_zero() {
+            if eval_mode == EvalMode::Ansi && r.is_zero() {
+                println!("ELSE ::: ANSI MODE ENABLED AND R is ZERO !!!!");
                 return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
             }
             let div = if r == 0 { 0 } else { l / r };
diff --git a/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala b/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
index 54b348f84d..e34d9d5bd9 100644
--- a/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
+++ b/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
@@ -198,7 +198,7 @@ object CometIntegralDivide extends CometExpressionSerde[IntegralDivide] with Mat
       if (expr.right.dataType.isInstanceOf[DecimalType]) expr.right
       else Cast(expr.right, DecimalType(19, 0))
 
-    val rightExpr = if (expr.evalMode != EvalMode.ANSI) right else nullIfWhenPrimitive(right)
+    val rightExpr = if (expr.evalMode != EvalMode.ANSI) nullIfWhenPrimitive(right) else right
 
     val dataType = (left.dataType, right.dataType) match {
       case (l: DecimalType, r: DecimalType) =>
diff --git a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
index 79c8566838..e2dc0a8074 100644
--- a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
@@ -2861,7 +2861,7 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
             randomSize = 10000)
           withParquetTable(path1.toString, "tbl1") {
             withParquetTable(path2.toString, "tbl2") {
-              checkSparkAnswerAndOperator("""
+              val res = spark.sql("""
                   |select
                   | t1._2 div t2._2, div(t1._2, t2._2),
                   | t1._3 div t2._3, div(t1._3, t2._3),
@@ -2873,6 +2873,13 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
                   | from tbl1 t1 join tbl2 t2 on t1._id = t2._id
                   | order by t1._id""".stripMargin)
 
+//              res.show(10, false)
+
+              checkSparkAnswerAndOperator(res)
+//              withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
+//                res.show(10, false)
+//              }
+
               checkSparkAnswerAndOperator("""
                   |select
                   | t1._12 div t2._12, div(t1._12, t2._12),

From be4e16df87592764863929ee6e01fd7089ee7b8d Mon Sep 17 00:00:00 2001
From: Bhargava Vadlamani <fullcircle289@gmail.com>
Date: Thu, 25 Sep 2025 20:12:16 -0700
Subject: [PATCH 3/7] rebase

---
 native/spark-expr/src/math_funcs/div.rs | 2 --
 1 file changed, 2 deletions(-)

diff --git a/native/spark-expr/src/math_funcs/div.rs b/native/spark-expr/src/math_funcs/div.rs
index d93b96d2c1..2a9ba68166 100644
--- a/native/spark-expr/src/math_funcs/div.rs
+++ b/native/spark-expr/src/math_funcs/div.rs
@@ -89,7 +89,6 @@ fn spark_decimal_div_internal(
             let l = BigInt::from(l) * &l_mul;
             let r = BigInt::from(r) * &r_mul;
             if eval_mode == EvalMode::Ansi && r.is_zero() {
-                println!("ANSI MODE ENABLED AND R is ZERO !!!!");
                 return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
             }
             let div = if r.eq(&zero) { zero.clone() } else { &l / &r };
@@ -109,7 +108,6 @@ fn spark_decimal_div_internal(
             let l = l * l_mul;
             let r = r * r_mul;
             if eval_mode == EvalMode::Ansi && r.is_zero() {
-                println!("ELSE ::: ANSI MODE ENABLED AND R is ZERO !!!!");
                 return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
             }
             let div = if r == 0 { 0 } else { l / r };

From 035f1f06bdb5619f0c97ee6c5ed254341dc4fa98 Mon Sep 17 00:00:00 2001
From: Bhargava Vadlamani <fullcircle289@gmail.com>
Date: Thu, 25 Sep 2025 20:13:08 -0700
Subject: [PATCH 4/7] rebase

---
 .../scala/org/apache/comet/CometExpressionSuite.scala    | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
index e2dc0a8074..79c8566838 100644
--- a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
@@ -2861,7 +2861,7 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
             randomSize = 10000)
           withParquetTable(path1.toString, "tbl1") {
             withParquetTable(path2.toString, "tbl2") {
-              val res = spark.sql("""
+              checkSparkAnswerAndOperator("""
                   |select
                   | t1._2 div t2._2, div(t1._2, t2._2),
                   | t1._3 div t2._3, div(t1._3, t2._3),
@@ -2873,13 +2873,6 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
                   | from tbl1 t1 join tbl2 t2 on t1._id = t2._id
                   | order by t1._id""".stripMargin)
 
-//              res.show(10, false)
-
-              checkSparkAnswerAndOperator(res)
-//              withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
-//                res.show(10, false)
-//              }
-
               checkSparkAnswerAndOperator("""
                   |select
                   | t1._12 div t2._12, div(t1._12, t2._12),

From 48514ac3fe93451b35f73232b4ac05e85771ca88 Mon Sep 17 00:00:00 2001
From: Bhargava Vadlamani <fullcircle289@gmail.com>
Date: Thu, 25 Sep 2025 20:16:31 -0700
Subject: [PATCH 5/7] rebase

---
 native/spark-expr/src/math_funcs/div.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/native/spark-expr/src/math_funcs/div.rs b/native/spark-expr/src/math_funcs/div.rs
index 2a9ba68166..933b28c094 100644
--- a/native/spark-expr/src/math_funcs/div.rs
+++ b/native/spark-expr/src/math_funcs/div.rs
@@ -88,7 +88,7 @@ fn spark_decimal_div_internal(
         arrow::compute::kernels::arity::try_binary(left, right, |l, r| {
             let l = BigInt::from(l) * &l_mul;
             let r = BigInt::from(r) * &r_mul;
-            if eval_mode == EvalMode::Ansi && r.is_zero() {
+            if eval_mode == EvalMode::Ansi && is_integral_div && r.is_zero() {
                 return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
             }
             let div = if r.eq(&zero) { zero.clone() } else { &l / &r };
@@ -107,7 +107,7 @@ fn spark_decimal_div_internal(
         arrow::compute::kernels::arity::try_binary(left, right, |l, r| {
             let l = l * l_mul;
             let r = r * r_mul;
-            if eval_mode == EvalMode::Ansi && r.is_zero() {
+            if eval_mode == EvalMode::Ansi && is_integral_div && r.is_zero() {
                 return Err(ArrowError::ComputeError(divide_by_zero_error().to_string()));
             }
             let div = if r == 0 { 0 } else { l / r };

From beb8cf6da4e3995896500f808dd3f0d3647c8bbb Mon Sep 17 00:00:00 2001
From: Bhargava Vadlamani <fullcircle289@gmail.com>
Date: Thu, 9 Oct 2025 19:20:51 -0700
Subject: [PATCH 6/7] support_ansi_mode_integral_divide

---
 native/core/src/execution/planner.rs          |  5 +--
 .../org/apache/comet/serde/arithmetic.scala   |  2 +-
 .../apache/comet/CometExpressionSuite.scala   | 37 ++++++++++++-------
 3 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/native/core/src/execution/planner.rs b/native/core/src/execution/planner.rs
index 329edc5d2f..59090a5bd3 100644
--- a/native/core/src/execution/planner.rs
+++ b/native/core/src/execution/planner.rs
@@ -287,8 +287,6 @@ impl PhysicalPlanner {
                 )
             }
             ExprStruct::IntegralDivide(expr) => {
-                // TODO respect eval mode
-                // https://github.com/apache/datafusion-comet/issues/533
                 let eval_mode = from_protobuf_eval_mode(expr.eval_mode)?;
                 self.create_binary_expr_with_options(
                     expr.left.as_ref().unwrap(),
@@ -987,11 +985,12 @@ impl PhysicalPlanner {
                 } else {
                     "decimal_div"
                 };
-                let fun_expr = create_comet_physical_fun(
+                let fun_expr = create_comet_physical_fun_with_eval_mode(
                     func_name,
                     data_type.clone(),
                     &self.session_ctx.state(),
                     None,
+                    eval_mode,
                 )?;
                 Ok(Arc::new(ScalarFunctionExpr::new(
                     func_name,
diff --git a/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala b/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
index e34d9d5bd9..eed4d7b16a 100644
--- a/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
+++ b/spark/src/main/scala/org/apache/comet/serde/arithmetic.scala
@@ -200,7 +200,7 @@ object CometIntegralDivide extends CometExpressionSerde[IntegralDivide] with Mat
 
     val rightExpr = if (expr.evalMode != EvalMode.ANSI) nullIfWhenPrimitive(right) else right
 
-    val dataType = (left.dataType, right.dataType) match {
+    val dataType = (left.dataType, rightExpr.dataType) match {
       case (l: DecimalType, r: DecimalType) =>
         // copy from IntegralDivide.resultDecimalType
         val intDig = l.precision - l.scale + r.scale
diff --git a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
index 79c8566838..a3fe5f8c13 100644
--- a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
@@ -2988,20 +2988,29 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
   }
 
   test("ANSI support for integral divide (division by zero)") {
-    val data = Seq((Integer.MIN_VALUE, 0))
-    withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
-      withParquetTable(data, "tbl") {
-        val res = spark.sql("""
-                              |SELECT
-                              |  _1 div _2
-                              |  from tbl
-                              |  """.stripMargin)
-
-        checkSparkMaybeThrows(res) match {
-          case (Some(sparkExc), Some(cometExc)) =>
-            assert(cometExc.getMessage.contains(DIVIDE_BY_ZERO_EXCEPTION_MSG))
-            assert(sparkExc.getMessage.contains("Division by zero"))
-          case _ => fail("Exception should be thrown")
+    val data = Seq((Integer.MAX_VALUE, 0))
+    Seq("true", "false").foreach { p =>
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+        withParquetTable(data, "tbl") {
+          val res = spark.sql("""
+                |SELECT
+                |  _1 div _2
+                |  from tbl
+                |  """.stripMargin)
+
+          checkSparkMaybeThrows(res) match {
+            case (Some(sparkException), Some(cometException)) =>
+              assert(sparkException.getMessage.contains(DIVIDE_BY_ZERO_EXCEPTION_MSG))
+              assert(cometException.getMessage.contains(DIVIDE_BY_ZERO_EXCEPTION_MSG))
+            case (None, None) => checkSparkAnswerAndOperator(res)
+            case (None, Some(ex)) =>
+              fail(
+                "Comet threw an exception but Spark did not. Comet exception: " + ex.getMessage)
+            case (Some(sparkException), None) =>
+              fail(
+                "Spark threw an exception but Comet did not. Spark exception: " +
+                  sparkException.getMessage)
+          }
         }
       }
     }

From 57ef65956198893477c389e0b614ae381444435b Mon Sep 17 00:00:00 2001
From: Bhargava Vadlamani <fullcircle289@gmail.com>
Date: Thu, 9 Oct 2025 20:16:50 -0700
Subject: [PATCH 7/7] support_ansi_mode_integral_divide

---
 .../src/test/scala/org/apache/comet/CometExpressionSuite.scala  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
index a3fe5f8c13..f86a934e7b 100644
--- a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
@@ -2990,7 +2990,7 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
   test("ANSI support for integral divide (division by zero)") {
     val data = Seq((Integer.MAX_VALUE, 0))
     Seq("true", "false").foreach { p =>
-      withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> p) {
         withParquetTable(data, "tbl") {
           val res = spark.sql("""
                 |SELECT
