[SPARK-58316][SQL] Reject non-finite decimal casts in ANSI mode - #58534
Open
charliec05 wants to merge 1 commit into
Open
[SPARK-58316][SQL] Reject non-finite decimal casts in ANSI mode#58534charliec05 wants to merge 1 commit into
charliec05 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Make the
NumberFormatExceptionpath in floating-point-to-decimal casts ANSI-aware in both interpreted evaluation and generated code.When ANSI mode is enabled, casting
NaN, positive infinity, or negative infinity from float or double to decimal now raises the existingCAST_OVERFLOWerror. Non-ANSI casts andTRY_CASTcontinue to return null.Add regression coverage for all six float/double non-finite inputs.
Why are the changes needed?
Decimal cannot represent non-finite floating-point values. Constructing the intermediate decimal throws
NumberFormatException, but the current catch block always converts that exception to null before the ANSI-aware precision handling can run. This makes ANSI mode silently return null instead of reporting an unrepresentable cast.Does this PR introduce any user-facing change?
Yes. With ANSI mode enabled, casting float or double
NaNand infinities to decimal now throwsCAST_OVERFLOW. Behavior with ANSI mode disabled and forTRY_CASTremains unchanged.How was this patch tested?
Added regression tests and ran:
build/sbt "catalyst/Test/testOnly org.apache.spark.sql.catalyst.expressions.CastWithAnsiOnSuite org.apache.spark.sql.catalyst.expressions.CastWithAnsiOffSuite org.apache.spark.sql.catalyst.expressions.TryCastSuite"All 410 tests passed. Also ran
dev/lint-scala; Scalastyle and Scalafmt passed.Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex (GPT-5)