Abhinav-Agarwal021 commented on code in PR #13697: URL: https://github.com/apache/gluten/pull/13697#discussion_r4695340165
########## gluten-core/src/main/scala/org/apache/spark/sql/errors/NativeErrorTranslator.scala: ########## @@ -0,0 +1,96 @@ +/* + * 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 condition into the matching Spark exception. + * + * Velox reports the condition as the error code "SPARK:<condition>". The native error message holds + * it in the line "Error Code: SPARK:<condition>", for example "Error Code: SPARK:DIVIDE_BY_ZERO". + * + * Only the GlutenException that the JNI boundary raises for a native error is translated. It has + * the native error message and no cause. Any other exception, such as one thrown by a Java callback + * and restored at the JNI boundary, keeps the existing handling even if one of its causes holds a + * native error. + */ +object NativeErrorTranslator extends Logging { + + private val SparkErrorCodePrefix = "SPARK:" + + // Only the first "Error Code:" line is used. It comes before the "Reason:" line, which may hold + // user data, so a value in the reason cannot pose as the error code. + private val ErrorCodeLine = "(?m)^Error Code: (\\S*)\\s*$".r + + /** Returns the Spark error condition in a native error message, if any. */ + def sparkCondition(message: String): Option[String] = { + Option(message) + .flatMap(ErrorCodeLine.findFirstMatchIn(_)) + .map(_.group(1)) + .filter(code => code.startsWith(SparkErrorCodePrefix)) Review Comment: Done in 1a587de37b. The JNI boundary now reads the error code from the native exception and passes it to Java with a new `GlutenException(message, errorCode)` constructor. The Velox backend registers how to get the code (`VeloxException::errorCode()`), so `cpp/core` does not depend on Velox. The translator reads `GlutenException.getErrorCode()`, so the Scala side no longer parses the message. `JavaExceptionJniTest.nativeErrorCode` covers the JNI path. ########## gluten-core/src/main/scala/org/apache/spark/sql/errors/NativeErrorTranslator.scala: ########## @@ -0,0 +1,96 @@ +/* + * 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 condition into the matching Spark exception. + * + * Velox reports the condition as the error code "SPARK:<condition>". The native error message holds + * it in the line "Error Code: SPARK:<condition>", for example "Error Code: SPARK:DIVIDE_BY_ZERO". + * + * Only the GlutenException that the JNI boundary raises for a native error is translated. It has + * the native error message and no cause. Any other exception, such as one thrown by a Java callback + * and restored at the JNI boundary, keeps the existing handling even if one of its causes holds a + * native error. + */ +object NativeErrorTranslator extends Logging { + + private val SparkErrorCodePrefix = "SPARK:" + + // Only the first "Error Code:" line is used. It comes before the "Reason:" line, which may hold + // user data, so a value in the reason cannot pose as the error code. + private val ErrorCodeLine = "(?m)^Error Code: (\\S*)\\s*$".r + + /** Returns the Spark error condition in a native error message, if any. */ + def sparkCondition(message: String): Option[String] = { + Option(message) + .flatMap(ErrorCodeLine.findFirstMatchIn(_)) + .map(_.group(1)) + .filter(code => code.startsWith(SparkErrorCodePrefix)) + .map(_.substring(SparkErrorCodePrefix.length)) + .filter(_.nonEmpty) + } + + /** + * Returns the Spark exception for a native error, with the native error as its cause. Returns + * null if the error is not a native error raised at the JNI boundary, has no Spark error + * condition, or the condition is not supported yet. + */ + def translate(error: Throwable): RuntimeException = { + val condition = + if (isNativeError(error)) sparkCondition(error.getMessage) else None + condition match { + case Some(c) => + toSparkException(c) match { + case Some(sparkError) => + sparkError.initCause(error) + logInfo( + s"Translated native error with Spark error condition $c to " + + s"${sparkError.getClass.getName}.") + sparkError + case None => + logWarning( + s"Native error has Spark error condition $c, which is not supported yet. " + + "Keeping the native error.") + null + } + case None => null + } + } + + // The JNI boundary raises a native error as a GlutenException with no cause. Subclasses and + // exceptions with a cause come from Java code. + private def isNativeError(error: Throwable): Boolean = + error != null && error.getClass == classOf[GlutenException] && error.getCause == null + + private def toSparkException(condition: String): Option[RuntimeException] = condition match { + case "DIVIDE_BY_ZERO" => Review Comment: Done. The supported codes are constants in a separate file, `NativeErrorCodes` (`SparkErrorCodePrefix`, `DivideByZero`). Spark has no constants for its error conditions. They are defined in `error-conditions.json`, and Spark's own code passes the condition name as a string, for example [`QueryExecutionErrors.divideByZeroError`](https://github.com/apache/spark/blob/9c61e02033db7165467c2a3fbeca602f20cc667d/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala#L198-L204). The translator builds the same exception as that method: the same condition, message parameters and SQLSTATE. It no longer calls that method, though. The native error has no query context, and in Spark 4.x `divideByZeroError(null)` returns `Array(null)` as the context. PySpark then fails while converting the error ([base.py](https://github.com/apache/spark/blob/9c61e02033db7165467c2a3fbeca602f20cc667d/python/pyspark/errors/exceptions/base.py#L173-L177)) with `AttributeError: 'NoneType' object has no attribute 'contextType'`. 21c40c0409 builds the exception with an empty context and adds a test for it. It also updates the older divide by zero tests in `ArithmeticAnsiValidateSuite`: Spark 4.0+ throws the Spark error to the caller as it is, while 3.x wraps it in a `SparkException`. -- 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]
