[SPARK-57170][SQL] Simplify Cast string to float/double codegen under ANSI mode#56220
Open
gengliangwang wants to merge 1 commit into
Open
[SPARK-57170][SQL] Simplify Cast string to float/double codegen under ANSI mode#56220gengliangwang wants to merge 1 commit into
gengliangwang wants to merge 1 commit into
Conversation
… ANSI mode
### What changes were proposed in this pull request?
Add `CastUtils.stringToFloatExact(UTF8String, QueryContext)` and
`stringToDoubleExact(UTF8String, QueryContext)`, and route the ANSI
(`ansiEnabled = true`) string -> float/double eval and codegen paths through
them. Each helper parses the string and, on a `NumberFormatException`, falls
back to `Cast.processFloatingPointSpecialLiterals` (inf / +inf / -inf / nan,
case-insensitive); if that also yields no value it throws the ANSI
`CAST_INVALID_INPUT` error citing the original input string.
`castToFloatCode` / `castToDoubleCode` previously emitted the same ~10-line
`try { Float.valueOf } catch (NumberFormatException) { special-literal fallback }`
block at both string call sites. They now dispatch on `ansiEnabled`: the ANSI
branch emits a single helper call; the non-ANSI branch keeps the inline
`try/catch -> isNull` form. The eval paths delegate to the same helpers.
### Why are the changes needed?
Part of SPARK-56908 (umbrella). Collapsing the duplicated inline parse + special
-literal fallback to a single helper call shrinks the generated Java for the
common `CAST(string AS FLOAT/DOUBLE)`.
### Does this PR introduce _any_ user-facing change?
No. The compiled behavior is identical; only the emitted Java source text changes.
### How was this patch tested?
```
build/sbt "catalyst/testOnly *CastSuite *CastWithAnsiOnSuite \
*CastWithAnsiOffSuite *TryCastSuite"
```
All pass (exercised both with and without whole-stage codegen).
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)
Co-authored-by: Isaac
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?
Add
CastUtils.stringToFloatExact(UTF8String, QueryContext)andCastUtils.stringToDoubleExact(UTF8String, QueryContext), and route the ANSI (ansiEnabled = true) string -> float/double eval and codegen paths through them. Each helper parses the string and, on aNumberFormatException, falls back toCast.processFloatingPointSpecialLiterals(inf / +inf / -inf / infinity / nan, case-insensitive); if that also yields no value it throws the ANSICAST_INVALID_INPUTerror citing the original input string.castToFloatCode/castToDoubleCodepreviously emitted the same ~10-linetry { Float.valueOf(...) } catch (NumberFormatException) { ...special-literal fallback... }block at both string call sites. They now dispatch onansiEnabled: the ANSI branch emits a single helper call, while the non-ANSI branch keeps the inlinetry/catch -> isNullform (matching the pattern used by the already-merged Cast-to-decimal /MakeDate/MakeIntervalcleanups). The eval paths delegate to the same helpers for consistency.Why are the changes needed?
Part of SPARK-56908 (umbrella). Collapsing the duplicated inline parse + special-literal fallback to a single helper call shrinks the generated Java for the common
CAST(string AS FLOAT/DOUBLE), helping with the JVM 64KB method / constant-pool limits, Janino compile time, and JIT work.Does this PR introduce any user-facing change?
No. The compiled behavior is identical; only the emitted Java source text changes.
How was this patch tested?
All pass (411 tests; exercised both with and without whole-stage codegen).
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)