Abhinav-Agarwal021 commented on code in PR #13697:
URL: https://github.com/apache/gluten/pull/13697#discussion_r4706504901


##########
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:
   Done in 9d96a3448d, the comment now points to Spark's 
`common/utils/src/main/resources/error/error-conditions.json`.



##########
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:
   Done in 9d96a3448d, renamed to `SparkErrorCodes`.



##########
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:
   Done in 9d96a3448d. Velox keeps the `SPARK:` prefix. `VeloxBackend.cc` now 
passes only codes that start with `SPARK:` and strips the prefix, so Java 
receives the bare condition, e.g. `DIVIDE_BY_ZERO`. Codes of other engines are 
not passed. The prefix constant is removed from Scala.



##########
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:
   Done in 9d96a3448d, `SparkErrorCodes.DivideByZero` is used both to match the 
code and as the error class of the `SparkArithmeticException`.



##########
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:
   Done in 9d96a3448d. Moved the message-based translation (type conversion and 
char/varchar length errors from #13639) from `ColumnarBatchOutIterator` into 
`NativeErrorTranslator`. `translate` uses the error code first and falls back 
to the message when the error has no code or its code is not supported yet. 
`ColumnarBatchOutIterator.translateException` calls it as the single entry 
point. Once Velox attaches a code for one of those errors, we can drop its 
message rule.



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