felipepessoto commented on code in PR #13697: URL: https://github.com/apache/gluten/pull/13697#discussion_r4806012364
########## cpp/velox/jni/VeloxErrorMetadata.cc: ########## @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#include "jni/VeloxErrorMetadata.h" + +#include <string_view> + +#include "velox/common/base/VeloxException.h" + +namespace gluten { + +std::optional<JniExceptionMetadata> describeVeloxError(const std::exception& error) { + constexpr std::string_view kSparkErrorCodePrefix = "SPARK:"; + const auto* veloxError = dynamic_cast<const facebook::velox::VeloxException*>(&error); + if (veloxError == nullptr) { + return std::nullopt; + } + std::string_view condition = veloxError->errorCode(); + if (!condition.starts_with(kSparkErrorCodePrefix)) { + return std::nullopt; + } + condition.remove_prefix(kSparkErrorCodePrefix.size()); + if (condition.empty()) { + return std::nullopt; + } + return JniExceptionMetadata{"", veloxError->message(), veloxError->what(), std::string(condition)}; Review Comment: Velox already [records the failing function name](https://github.com/IBM/velox/blob/b33d1af2498a4fa52fff9ddbfa20ef0b17327121/velox/expression/Expr.cpp#L776-L785) in `ExpressionExceptionProperties`. Can we read it from `veloxError->properties()` when available and only use `""` when it is missing? Otherwise Java gets an empty `getFunctionName()` even when Velox knows it was `checked_divide`. ########## backends-velox/src/test/scala/org/apache/gluten/functions/ArithmeticAnsiValidateSuite.scala: ########## @@ -86,8 +99,38 @@ class ArithmeticAnsiValidateSuite extends FunctionsValidateSuite { checkGlutenPlan[ProjectExecTransformer] } // Spark 3.4+ throws exception for division by zero in ANSI mode - intercept[SparkException] { - sql("SELECT 1 / 0").collect() + divideByZeroError(intercept[Exception](sql("SELECT 1 / 0").collect())) + } + + test("divide by zero throws Spark DIVIDE_BY_ZERO error") { + withTempPath { + path => + sql("SELECT 1 AS a, 0 AS b").write.parquet(path.getCanonicalPath) + withTempView("div_tab") { + spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("div_tab") + val df = sql("SELECT a / b FROM div_tab") + checkGlutenPlan[ProjectExecTransformer](df) + val sparkError = divideByZeroError(intercept[Exception](df.collect())) + // The native error is kept as the cause. + assert(sparkError.getCause.getMessage.contains("division by zero")) + } + } + } + + test("divide by zero before a shuffle throws Spark DIVIDE_BY_ZERO error") { + withTempPath { + path => + sql("SELECT 1 AS a, 0 AS b").write.parquet(path.getCanonicalPath) + withTempView("div_tab") { + spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("div_tab") + // The shuffle reads the translated error from a nested native iterator. + val e = + intercept[Exception](sql("SELECT a / b AS c FROM div_tab DISTRIBUTE BY c").collect()) Review Comment: Spark fallback would produce the same exception and SQLSTATE. This can also pass with a native project but no outer resize iterator to rewrap the error. Can we assert before `collect()` that the shuffle input has a `VeloxResizeBatchesExec` above the native divide projection, then check that the arithmetic exception has a `GlutenNativeException` cause? That would tie the test to the nested JNI path it is meant to protect. ########## gluten-arrow/src/main/java/org/apache/gluten/vectorized/ColumnarBatchOutIterator.java: ########## @@ -134,6 +135,11 @@ public void requestBarrier() { @Override protected RuntimeException translateException(Exception e) { + // A Spark exception, for example one translated from a native error by a nested iterator such + // as the input of a shuffle, is returned as-is rather than nested in a GlutenException. + if (e instanceof SparkThrowable && e instanceof RuntimeException) { Review Comment: This also changes what callers see when a Java input callback throws its own Spark exception. For example, a `SparkNumberFormatException` (`CAST_INVALID_INPUT`) used to come out of public `hasNext()` wrapped in a `GlutenException`; now it escapes directly, even though no native translation happened. Can we limit the pass-through to translated errors? If the broader behavior is intentional, please document it and cover that callback case through `hasNext()`, not just `hasNext0()`. -- 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]
