Skip to content

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

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

[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
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.

1 participant