Skip to content

[SPARK-57170][SQL] Simplify Cast string to float/double codegen under ANSI mode#56220

Closed
gengliangwang wants to merge 1 commit into
apache:masterfrom
gengliangwang:spark-cast-string-float-codegen
Closed

[SPARK-57170][SQL] Simplify Cast string to float/double codegen under ANSI mode#56220
gengliangwang wants to merge 1 commit into
apache:masterfrom
gengliangwang:spark-cast-string-float-codegen

Conversation

@gengliangwang
Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Add CastUtils.stringToFloatExact(UTF8String, QueryContext) and CastUtils.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 / infinity / 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, while the non-ANSI branch keeps the inline try/catch -> isNull form (matching the pattern used by the already-merged Cast-to-decimal / MakeDate / MakeInterval cleanups). 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?

build/sbt "catalyst/testOnly *CastSuite *CastWithAnsiOnSuite *CastWithAnsiOffSuite *TryCastSuite"

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)

… 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
@gengliangwang gengliangwang requested a review from LuciferYang May 31, 2026 02:31
Copy link
Copy Markdown
Contributor

@LuciferYang LuciferYang left a comment

Choose a reason for hiding this comment

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

LGTM

@gengliangwang
Copy link
Copy Markdown
Member Author

@LuciferYang thanks for the review. Merging to master/4.x

gengliangwang added a commit that referenced this pull request Jun 1, 2026
… ANSI mode

### What changes were proposed in this pull request?

Add `CastUtils.stringToFloatExact(UTF8String, QueryContext)` and `CastUtils.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 / infinity / 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, while the non-ANSI branch keeps the inline `try/catch -> isNull` form (matching the pattern used by the already-merged Cast-to-decimal / `MakeDate` / `MakeInterval` cleanups). 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?

```
build/sbt "catalyst/testOnly *CastSuite *CastWithAnsiOnSuite *CastWithAnsiOffSuite *TryCastSuite"
```

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)

Closes #56220 from gengliangwang/spark-cast-string-float-codegen.

Authored-by: Gengliang Wang <gengliang@apache.org>
Signed-off-by: Gengliang Wang <gengliang@apache.org>
(cherry picked from commit f9e75b2)
Signed-off-by: Gengliang Wang <gengliang@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants