Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

acvictor
Copy link
Contributor

@acvictor acvictor commented Nov 29, 2024

What changes were proposed in this pull request?

There are 3 failure behaviors defined for cast in Substrait

enum FailureBehavior { 
    FAILURE_BEHAVIOR_UNSPECIFIED = 0; 
    FAILURE_BEHAVIOR_RETURN_NULL = 1; 
    FAILURE_BEHAVIOR_THROW_EXCEPTION = 2; 
} 

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 of canUseLegacyCastForTryCast. 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 with nullOnFailure_ 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.

@github-actions github-actions bot added CORE works for Gluten Core VELOX labels Nov 29, 2024
Copy link

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?

[GLUTEN-${ISSUES_ID}][COMPONENT]feat/fix: ${detailed message}

See also:

Copy link

Run Gluten Clickhouse CI on x86

@acvictor acvictor changed the title [VL] Update logic to throw on failure for cast [GLUTEN-8108] fix: Update logic to throw on failure for cast Nov 29, 2024
Copy link

#8108

@acvictor
Copy link
Contributor Author

@zhli1142015 @rui-mo can you please review?

@acvictor acvictor marked this pull request as ready for review November 29, 2024 10:35
Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

1 similar comment
Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

Copy link

github-actions bot commented Dec 2, 2024

Run Gluten Clickhouse CI on x86

Copy link
Contributor

@rui-mo rui-mo left a 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")
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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?

Copy link
Contributor

@PHILO-HE PHILO-HE Dec 3, 2024

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

@rui-mo rui-mo requested a review from PHILO-HE December 3, 2024 01:33
Copy link
Contributor

@zhli1142015 zhli1142015 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@zhli1142015
Copy link
Contributor

Run Gluten Clickhouse CI on x86

ExpressionBuilder.makeCast(
typeNode,
child.doTransform(args),
SparkShimLoader.getSparkShims.withAnsiEvalMode(original))
Copy link
Contributor

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.

https://github.com/apache/spark/blob/v3.5.3/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala#L471

.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")
Copy link
Contributor

@PHILO-HE PHILO-HE Dec 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CORE works for Gluten Core VELOX
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants