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

rui-mo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new 47adccc724 [VL] Map TRY-mode +, -, * to Velox's try_add, try_subtract 
and try_mu… (#13604)
47adccc724 is described below

commit 47adccc7245866596214575f800dd7840af9f497
Author: Pedrum Jalali <[email protected]>
AuthorDate: Thu Nov 12 13:16:47 2026 -0800

    [VL] Map TRY-mode +, -, * to Velox's try_add, try_subtract and try_mu… 
(#13604)
    
    Co-authored-by: Pedrum Jalali <[email protected]>
---
 .../backendsapi/velox/VeloxSparkPlanExecApi.scala  | 26 ++++++++++++++++------
 .../functions/ArithmeticAnsiValidateSuite.scala    | 24 ++++++++++++++++++++
 .../apache/gluten/expression/ExpressionNames.scala |  3 +++
 3 files changed, 46 insertions(+), 7 deletions(-)

diff --git 
a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
 
b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
index ce136790e2..f545413017 100644
--- 
a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
+++ 
b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
@@ -143,8 +143,8 @@ class VeloxSparkPlanExecApi extends SparkPlanExecApi with 
Logging {
   }
 
   /**
-   * Map arithmetic expr to different functions: substraitExprName or 
try(checkArithmeticExprName)
-   * based on EvalMode.
+   * Map arithmetic expr to different functions based on EvalMode: 
substraitExprName by default,
+   * checkArithmeticExprName in ANSI mode, and Velox's try_ function in TRY 
mode.
    */
   override def genArithmeticTransformer(
       substraitExprName: String,
@@ -158,11 +158,23 @@ class VeloxSparkPlanExecApi extends SparkPlanExecApi with 
Logging {
         case _ =>
           throw new GlutenNotSupportException(s"$substraitExprName with try 
mode is not supported")
       }
-      // Offload to velox for only IntegralTypes.
-      GenericExpressionTransformer(
-        ExpressionMappings.expressionsMap(classOf[TryEval]),
-        Seq(GenericExpressionTransformer(checkArithmeticExprName, Seq(left, 
right), original)),
-        original)
+      // Offload to velox for only IntegralTypes. The try_ functions return 
NULL only when the
+      // operation overflows, so an error in an argument still throws, as in 
Spark.
+      original match {
+        case _: Add =>
+          GenericExpressionTransformer(ExpressionNames.TRY_ADD, Seq(left, 
right), original)
+        case _: Subtract =>
+          GenericExpressionTransformer(ExpressionNames.TRY_SUBTRACT, Seq(left, 
right), original)
+        case _: Multiply =>
+          GenericExpressionTransformer(ExpressionNames.TRY_MULTIPLY, Seq(left, 
right), original)
+        case _ =>
+          // TODO: Remove try(checkArithmeticExprName) and 
genTryArithmeticTransformer once Velox
+          // supports all relevant try_* functions.
+          GenericExpressionTransformer(
+            ExpressionMappings.expressionsMap(classOf[TryEval]),
+            Seq(GenericExpressionTransformer(checkArithmeticExprName, 
Seq(left, right), original)),
+            original)
+      }
     } else if (ExpressionUtils.withAnsiEvalMode(original)) {
       GenericExpressionTransformer(checkArithmeticExprName, Seq(left, right), 
original)
     } else {
diff --git 
a/backends-velox/src/test/scala/org/apache/gluten/functions/ArithmeticAnsiValidateSuite.scala
 
b/backends-velox/src/test/scala/org/apache/gluten/functions/ArithmeticAnsiValidateSuite.scala
index 795e6a16c4..d96e4581af 100644
--- 
a/backends-velox/src/test/scala/org/apache/gluten/functions/ArithmeticAnsiValidateSuite.scala
+++ 
b/backends-velox/src/test/scala/org/apache/gluten/functions/ArithmeticAnsiValidateSuite.scala
@@ -184,6 +184,30 @@ class ArithmeticAnsiValidateSuite extends 
FunctionsValidateSuite {
     }
   }
 
+  test("integral try_add, try_subtract and try_multiply") {
+    // Only an overflow in the operation itself returns NULL. An error in an 
argument still throws,
+    // as in Spark. Falling back to Spark would also throw, so check that the 
query runs on Velox.
+    withTempPath {
+      path =>
+        sql("SELECT 2147483647 AS a, CAST(-2147483648 AS INT) AS b, 0 AS 
z").write
+          .parquet(path.getCanonicalPath)
+        withTempView("try_tab") {
+          
spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("try_tab")
+          runQueryAndCompare(
+            "SELECT try_add(a, 1), try_subtract(b, 1), try_multiply(a, 2) FROM 
try_tab") {
+            checkGlutenPlan[ProjectExecTransformer]
+          }
+          Seq("try_add", "try_subtract", "try_multiply").foreach {
+            function =>
+              val df = sql(s"SELECT $function(CAST(a / z AS INT), 1) FROM 
try_tab")
+              checkGlutenPlan[ProjectExecTransformer](df)
+              val e = intercept[SparkException](df.collect())
+              assert(e.getMessage.contains("division by zero"), e.getMessage)
+          }
+        }
+    }
+  }
+
   test("decimal overflow with allowPrecisionLoss disabled") {
     // Uses checked_*_deny_precision_loss. A missing function would fail 
validation and fall back
     // to Spark, which also throws on overflow, so check that the query runs 
on Velox first.
diff --git 
a/shims/common/src/main/scala/org/apache/gluten/expression/ExpressionNames.scala
 
b/shims/common/src/main/scala/org/apache/gluten/expression/ExpressionNames.scala
index d1d1896f91..48706e5178 100644
--- 
a/shims/common/src/main/scala/org/apache/gluten/expression/ExpressionNames.scala
+++ 
b/shims/common/src/main/scala/org/apache/gluten/expression/ExpressionNames.scala
@@ -96,6 +96,9 @@ object ExpressionNames {
   final val CHECKED_DIVIDE = "checked_divide"
   final val CHECKED_MULTIPLY = "checked_multiply"
   final val CHECKED_DIV = "checked_div"
+  final val TRY_ADD = "try_add"
+  final val TRY_SUBTRACT = "try_subtract"
+  final val TRY_MULTIPLY = "try_multiply"
 
   // SparkSQL String functions
   final val ASCII = "ascii"


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

Reply via email to