Skip to content

[SPARK-49635][SQL] Remove ANSI config suggestion in CAST error messages#53295

Closed
qlong wants to merge 1 commit intoapache:masterfrom
qlong:SPARK-49635-remove-ansi-suggestion
Closed

[SPARK-49635][SQL] Remove ANSI config suggestion in CAST error messages#53295
qlong wants to merge 1 commit intoapache:masterfrom
qlong:SPARK-49635-remove-ansi-suggestion

Conversation

@qlong
Copy link

@qlong qlong commented Dec 3, 2025

What changes were proposed in this pull request?

This PR improves CAST error messages to provide suggestions based on whether casts work in ANSI mode, non-ANSI mode, or try_cast():

  1. Suggest try_cast() when the cast is valid but fails in ANSI mode and try_cast() supports it
  2. Suggest disabling ANSI mode when the cast works in non-ANSI mode but try_cast() doesn't support it
  3. Provide no suggestion when the cast is invalid in all modes

Why are the changes needed?

In Spark 4.0.0, ANSI mode is now enabled by default. The previous error messages suggested users disable ANSI mode when encountering cast failures, which goes against the direction of the project. However, completely removing these suggestions would leave users without guidance when migrating code from Spark 3.x that relied on non-ANSI cast behavior.

Does this PR introduce any user-facing change?

Yes. Error messages for CAST operations that fail in ANSI mode now suggest using try_cast() instead of disabling ANSI mode, but only when try_cast() actually supports that cast operation.

Before:

cannot cast "ARRAY<INT>" to "ARRAY<BINARY>" with ANSI mode on.
If you have to cast ARRAY<INT> to ARRAY<BINARY>, you can set "spark.sql.ansi.enabled" as 'false'.

After (for casts supported by try_cast):

cannot cast "ARRAY<INT>" to "ARRAY<BINARY>".
To convert values from "ARRAY<INT>" to "ARRAY<BINARY>", you can use the functions `try_cast` instead.

After (suggests disabling ANSI when try_cast doesn't support it):

cannot cast "TIMESTAMP" to "BOOLEAN" with ANSI mode on.
This cast is not allowed in ANSI mode but works when "spark.sql.ansi.enabled" is 'false'.

After (for casts not supported by try_cast):

cannot cast "INT" to "BINARY".

How was this patch tested?

  • Added new test case for complex type casts (Array[Int] to Array[Binary]) that
    demonstrates try_cast suggestion
  • Added test case for TIMESTAMP → BOOLEAN cast that demonstrates config suggestion
  • Updated existing test expectations in CastWithAnsiOnSuite to reflect the new
    behavior
  • All Cast-related unit tests pass
  • Verified no regression in legacy (ANSI off) mode
  • Manually tested with spark-shell to verify error messages

Was this patch authored or co-authored using generative AI tooling?

No

@github-actions github-actions bot added the SQL label Dec 3, 2025
@qlong qlong force-pushed the SPARK-49635-remove-ansi-suggestion branch from ab0aade to 2fc68b5 Compare December 3, 2025 22:33
@qlong qlong force-pushed the SPARK-49635-remove-ansi-suggestion branch 2 times, most recently from 60a4c3c to a93b2b4 Compare December 8, 2025 19:26
@qlong qlong requested a review from allisonwang-db December 8, 2025 23:48
Copy link
Contributor

@allisonwang-db allisonwang-db left a comment

Choose a reason for hiding this comment

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

This PR improves CAST error messages to provide suggestions based on
whether casts work in non-ANSI mode.

SPARK-49635 identified that suggesting users disable ANSI mode in error
messages goes against Spark 4.0's direction (ANSI enabled by default).
This PR addresses that issue by implementing suggestion logic:
- Suggest try_cast() for ANSI-compliant alternatives
- Suggest disabling ANSI mode only when the cast actually works in
non-ANSI mode but try_cast doesn't support it
- Provide no suggestion when the cast doesn't work in any mode

This ensures error messages guide users toward ANSI-compliant solutions
while still helping users migrate from previous versions.
@qlong qlong force-pushed the SPARK-49635-remove-ansi-suggestion branch from a93b2b4 to 4804c03 Compare December 9, 2025 03:35
@gengliangwang
Copy link
Member

Thanks, merging to master

@gengliangwang
Copy link
Member

@qlong what is your jira id? I am trying to assign https://issues.apache.org/jira/browse/SPARK-49635 to you

@qlong
Copy link
Author

qlong commented Dec 9, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants