Skip to content

Conversation

@qlong
Copy link

@qlong qlong commented Dec 3, 2025

What changes were proposed in this pull request?

This PR removes the suggestion to disable ANSI mode from CAST error messages and replaces it with try_cast() function suggestions when applicable.

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. Instead, error messages should guide users toward ANSI-compliant alternatives like try_cast() that handle cast failures gracefully by returning NULL.

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 "INT" to "BINARY" with ANSI mode on.
If you have to cast INT to 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 (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
  • 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

This PR removes the suggestion to disable ANSI mode  from CAST error
messages and replaces it with try_cast() function suggestions
when applicable.

In Spark 4.0.0, ANSI mode is enabled by default. Error messages now
guide users toward ANSI-compliant alternatives like try_cast() instead
of suggesting to disable ANSI mode. This implementation only suggests
try_cast when the cast operation is supported, avoiding misleading
suggestions.

Changes:
- Modified Cast.typeCheckFailureMessage to suggest try_cast when fallbackConf is
  None and Cast.canTryCast returns true
- Changed ANSI mode error case to pass None instead of ANSI_ENABLED config
- Added test case for complex type casts (Array[Int] to Array[Binary])
- Updated test expectations to reflect accurate try_cast support
@github-actions github-actions bot added the SQL label Dec 3, 2025
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.

1 participant