rui-mo commented on code in PR #13697: URL: https://github.com/apache/gluten/pull/13697#discussion_r4670696608
########## 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: Could we extract the `errorCode` directly in native code when catching the exception thrown by Velox? This would avoid the need to combine the `errorCode` with the error message and then split them again on the Scala side. ########## 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: Use constants to manage Spark error codes in Scala. Reuse existing definitions provided by Spark where available; otherwise, define them in a separate file. -- 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]
