rui-mo commented on code in PR #13697:
URL: https://github.com/apache/gluten/pull/13697#discussion_r4701833801


##########
gluten-core/src/main/scala/org/apache/spark/sql/errors/NativeErrorTranslator.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.errors
+
+import org.apache.gluten.exception.GlutenException
+
+import org.apache.spark.{QueryContext, SparkArithmeticException}
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Translates native errors that carry a Spark error code into the matching 
Spark exception.
+ *
+ * Native code sets the error code, for example "SPARK:DIVIDE_BY_ZERO", on the 
GlutenException that
+ * the JNI boundary raises for a native error. Any other exception, including 
one with a native
+ * error as its cause, keeps the existing handling.
+ */
+object NativeErrorTranslator extends Logging {
+
+  /**
+   * Returns the Spark exception for a native error, with the native error as 
its cause. Returns
+   * null if the error has no error code or its error code is not supported.
+   */
+  def translate(error: Throwable): RuntimeException = error match {
+    case e: GlutenException if e.getErrorCode != null =>
+      val errorCode = e.getErrorCode
+      toSparkException(errorCode) match {
+        case Some(sparkError) =>
+          sparkError.initCause(e)
+          logInfo(
+            s"Translated native error with error code $errorCode to " +
+              s"${sparkError.getClass.getName}.")
+          sparkError
+        case None =>
+          if (errorCode.startsWith(NativeErrorCodes.SparkErrorCodePrefix)) {
+            logWarning(
+              s"Native error has error code $errorCode, which is not supported 
yet. " +
+                "Keeping the native error.")
+          }
+          null
+      }
+    case _ => null
+  }
+
+  // The native error has no Spark query context. 
QueryExecutionErrors.divideByZeroError(null)
+  // would report a null context entry, which PySpark fails to read, so the 
context is left empty.
+  private def toSparkException(errorCode: String): Option[RuntimeException] = 
errorCode match {
+    case NativeErrorCodes.DivideByZero =>
+      Some(
+        new SparkArithmeticException(
+          "DIVIDE_BY_ZERO",

Review Comment:
   Reuse the "DIVIDE_BY_ZERO" defined in ErrorCodes, please avoid hand-writing 
strings.



##########
gluten-core/src/main/scala/org/apache/spark/sql/errors/NativeErrorCodes.scala:
##########
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.errors
+
+/**
+ * Error codes that native code sets on errors that match a Spark error 
condition. Each code is
+ * "SPARK:" followed by the Spark error condition.

Review Comment:
   nit: could you add the reference to Spark json file in the comment?



##########
gluten-core/src/main/scala/org/apache/spark/sql/errors/NativeErrorTranslator.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.errors
+
+import org.apache.gluten.exception.GlutenException
+
+import org.apache.spark.{QueryContext, SparkArithmeticException}
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Translates native errors that carry a Spark error code into the matching 
Spark exception.
+ *
+ * Native code sets the error code, for example "SPARK:DIVIDE_BY_ZERO", on the 
GlutenException that
+ * the JNI boundary raises for a native error. Any other exception, including 
one with a native
+ * error as its cause, keeps the existing handling.
+ */
+object NativeErrorTranslator extends Logging {
+
+  /**
+   * Returns the Spark exception for a native error, with the native error as 
its cause. Returns
+   * null if the error has no error code or its error code is not supported.
+   */
+  def translate(error: Throwable): RuntimeException = error match {
+    case e: GlutenException if e.getErrorCode != null =>
+      val errorCode = e.getErrorCode
+      toSparkException(errorCode) match {
+        case Some(sparkError) =>
+          sparkError.initCause(e)
+          logInfo(
+            s"Translated native error with error code $errorCode to " +
+              s"${sparkError.getClass.getName}.")
+          sparkError
+        case None =>
+          if (errorCode.startsWith(NativeErrorCodes.SparkErrorCodePrefix)) {
+            logWarning(
+              s"Native error has error code $errorCode, which is not supported 
yet. " +
+                "Keeping the native error.")

Review Comment:
   Could we refactor this to reuse the existing message-based error translation 
as a fallback when the corresponding error code hasn't been added to Velox yet?
   
   This way, we can gradually transition from message-based to error-code-based 
translation.
   
   
https://github.com/apache/gluten/commit/b9357c1a5d0f919ed6a61a2535bec85c3cbd2405#diff-4552fdcd7933bcc531cc27ea75dd884ee83077809fe49531728c3b8e325ffbffR161



##########
gluten-core/src/main/scala/org/apache/spark/sql/errors/NativeErrorCodes.scala:
##########
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.errors
+
+/**
+ * Error codes that native code sets on errors that match a Spark error 
condition. Each code is
+ * "SPARK:" followed by the Spark error condition.
+ */
+object NativeErrorCodes {

Review Comment:
   Could we rename this to SparkErrorCodes to make it clear that these are 
copied from Spark's error codes?



##########
gluten-arrow/src/main/java/org/apache/gluten/vectorized/ColumnarBatchOutIterator.java:
##########
@@ -145,6 +146,10 @@ private static RuntimeException 
translateToSchemaException(String msg) {
 
   @Override
   protected RuntimeException translateException(Exception e) {
+    RuntimeException sparkEx = NativeErrorTranslator.translate(e);
+    if (sparkEx != null) {
+      return sparkEx;
+    }

Review Comment:
   With ANSI on and `spark.gluten.sql.ansiFallback.enabled=false`, Spark 4.0.2 
throws `SparkArithmeticException` `[DIVIDE_BY_ZERO]` unwrapped for `SELECT a / 
b AS c FROM div_tab DISTRIBUTE BY c` 
([DAGScheduler.scala#L2928-L2936](https://github.com/apache/spark/blob/v4.0.2/core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala#L2928-L2936)).
 In Gluten, the default `VeloxResizeBatchesExec` before the hash shuffle reads 
the translated error through `ColumnarBatchInIterator`. `JNI_METHOD_END` 
rethrows it, and the unchanged fallback at lines 165-168 wraps it in 
`GlutenException`, so users get `Job aborted due to stage failure` again 
(source trace).
   
   Return already-translated Spark exceptions unchanged, at least those from 
`NativeErrorTranslator`, as lines 165-166 do for `GlutenException`. Add a 
shuffle test that checks the top-level exception on Spark 4.x.



##########
gluten-core/src/main/scala/org/apache/spark/sql/errors/NativeErrorCodes.scala:
##########
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.errors
+
+/**
+ * Error codes that native code sets on errors that match a Spark error 
condition. Each code is
+ * "SPARK:" followed by the Spark error condition.
+ */
+object NativeErrorCodes {
+
+  val SparkErrorCodePrefix = "SPARK:"
+
+  val DivideByZero: String = SparkErrorCodePrefix + "DIVIDE_BY_ZERO"

Review Comment:
   We need to keep the Spark prefix in Velox to distinguish Spark error codes 
from those of other engine, such as Presto. Once Gluten receives the error 
codes, let's strip the Spark prefix on the native side before passing them to 
Java.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to