-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GLUTEN-8108] fix: Update logic to throw on failure for cast #8107
base: main
Are you sure you want to change the base?
Conversation
Thanks for opening a pull request! Could you open an issue for this pull request on Github Issues? https://github.com/apache/incubator-gluten/issues Then could you also rename commit message and pull request title in the following format?
See also: |
Run Gluten Clickhouse CI on x86 |
@zhli1142015 @rui-mo can you please review? |
gluten-substrait/src/main/scala/org/apache/gluten/expression/UnaryExpressionTransformer.scala
Outdated
Show resolved
Hide resolved
backends-velox/src/test/scala/org/apache/gluten/execution/ScalarFunctionsValidateSuite.scala
Outdated
Show resolved
Hide resolved
...k35/src/test/scala/org/apache/spark/sql/catalyst/expressions/GlutenCastWithAnsiOnSuite.scala
Outdated
Show resolved
Hide resolved
Run Gluten Clickhouse CI on x86 |
Run Gluten Clickhouse CI on x86 |
Run Gluten Clickhouse CI on x86 |
1 similar comment
Run Gluten Clickhouse CI on x86 |
Run Gluten Clickhouse CI on x86 |
Run Gluten Clickhouse CI on x86 |
Run Gluten Clickhouse CI on x86 |
Run Gluten Clickhouse CI on x86 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
.exclude( | ||
"Process Infinity, -Infinity, NaN in case insensitive manner" // +inf not supported in folly. | ||
) | ||
.exclude("ANSI mode: Throw exception on casting out-of-range value to byte type") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you add some comments on why these tests are excluded? I remember ANSI ON causes execution fallback, can the tests produce Spark expected result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests come from https://github.com/apache/spark/blob/master/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastWithAnsiOnSuite.scala and when the suite is run separately with SQLConf.get.setConf(SQLConf.ANSI_ENABLED, true)
falls back but when run as part of GlutenTryCastSuite
where the conf is not set does not give expected results. (Also, the tests fail without the code changes in this PR.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the conf is not set does not give expected results
Thanks for providing the details. These tests are for ANSI OFF cases, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are for ANSI enabled (Spark views ANSI is enabled for TRY mode).
@acvictor, for these tests triggered from TryCastSuite, exceptions are not thrown, but null results are returned? It should be an expected behavior for Velox cast.
I cannot figure out how these exception checks can pass in vanilla Spark, assuming exception is handled internally in TRY mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PHILO-HE I don't fully understand what you mean. Are you saying Velox behaviour is incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@acvictor, no. I am just curious why these tests of TryCastSuite can pass for vanilla Spark. I am assuming the exceptions are handled internally by Spark and null result is returned by internally catching these exceptions. But seems in these tests, exception can still be caught from external side in vanilla Spark.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
Run Gluten Clickhouse CI on x86 |
ExpressionBuilder.makeCast( | ||
typeNode, | ||
child.doTransform(args), | ||
SparkShimLoader.getSparkShims.withAnsiEvalMode(original)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I note Spark also sets ansiEnabled = true
for EvalMode.TRY
, which is for re-using the code logic of EvalMode.ANSI
. The difference is, in TRY mode any exception is caught and then NULL is returned.
Velox has different implementation, which requires us to set this flag simply according to whether EvalMode is ANSI or not.
@acvictor, please leave some comments here to clarify, which should be helpful for future code maintenance.
.exclude( | ||
"Process Infinity, -Infinity, NaN in case insensitive manner" // +inf not supported in folly. | ||
) | ||
.exclude("ANSI mode: Throw exception on casting out-of-range value to byte type") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are for ANSI enabled (Spark views ANSI is enabled for TRY mode).
@acvictor, for these tests triggered from TryCastSuite, exceptions are not thrown, but null results are returned? It should be an expected behavior for Velox cast.
I cannot figure out how these exception checks can pass in vanilla Spark, assuming exception is handled internally in TRY mode.
What changes were proposed in this pull request?
There are 3 failure behaviors defined for cast in Substrait
Failure behavior should be set to 2 only when evaluation mode is ANSI. The current logic in Spark 3.4 and 3.5 uses the evaluation of
ansiEnabled = evalMode == EvalMode.ANSI || (evalMode == EvalMode.TRY && !canUseLegacyCastForTryCast)
to set the failure behavior and is incorrectly evaluated in some cases depending on the value ofcanUseLegacyCastForTryCast
.canUseLegacyCastForTryCast
should not matter in Velox/Gluten since it is used in Spark to optimize execution by avoiding adding a try-catch block around the cast.A minimal repro shows that
select try_cast(value as bigint) from tbl translates to Velox plan cast((trim( :VARCHAR, n0_0)) as BIGINT), implying that failure behavior is incorrectly set to 2 and so
SparkCastExpr
in Velox is created withnullOnFailure_
set to false instead of true https://github.com/facebookincubator/velox/blob/main/velox/expression/ExprCompiler.cpp#L411.(Fixes: #ISSUE-ID #8108)
How was this patch tested?
Unit tests.