Commit 9e13442
committed
[SPARK-56909][SQL] Simplify Cast to int/long codegen under ANSI mode
### What changes were proposed in this pull request?
In `Cast.scala`, the ANSI codegen for narrowing casts to `int` / `long` previously emitted a 5-line inline body per call site (bounds check + cast + throw). After this PR it emits a single static call into the existing `LongExactNumeric` / `FloatExactNumeric` / `DoubleExactNumeric` objects in `numerics.scala`, which already implement the same overflow check + `castingCauseOverflowError` throw that this codegen needs.
The rewrite uses the same `getClass.getCanonicalName.stripSuffix("$")` pattern as the adjacent `MathUtils` / `IntervalMathUtils` calls. The Scala compiler emits `public static` forwarders on the companion class of top-level objects, so generated Java code can call e.g. `org.apache.spark.sql.types.LongExactNumeric.toInt(v)` directly.
Touched `Cast.scala` helpers:
* `castIntegralTypeToIntegralTypeExactCode`: the `int` target branch now emits `LongExactNumeric.toInt($c)` (byte/short narrowing stays inline; refactored in SPARK-56910).
* `castFractionToIntegralTypeCode`: the `int` / `long` target branches now emit `FloatExactNumeric` / `DoubleExactNumeric` `toInt` / `toLong` (byte/short narrowing stays inline; refactored in SPARK-56910).
Primitive widening branches and the non-ANSI paths are untouched.
### Why are the changes needed?
Part of SPARK-56908 (umbrella). The narrow-cast ANSI branches in `Cast.doGenCode` are some of the longer inline bodies still emitted per call site. Multiplied across the many cast paths in a TPC-DS plan, they contribute meaningfully to the generated source size and Janino compile time, and push whole-stage methods closer to the 64KB JVM method limit.
Compared to v1 of this PR (which added a new `CastUtils.java` with `longToIntExact` / `floatToIntExact` / etc.), this version calls the existing `LongExactNumeric.toInt` / `FloatExactNumeric.toInt` / `toLong` / `DoubleExactNumeric.toInt` / `toLong` directly. Those are public static forwarders on top-level Scala objects that already implement the same `castingCauseOverflowError(v, FROM, TO)` throw — no new helper class needed. (Applying the same lesson cloud-fan called out on #55938.)
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
```
build/sbt "catalyst/testOnly *CastSuite *CastWithAnsiOnSuite \
*CastWithAnsiOffSuite *AnsiCastSuite *TryCastSuite *ExpressionClassIdentitySuite"
```
307/307 pass.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor 1.x
Closes #55934 from gengliangwang/SPARK-56909-cast-int-long.
Authored-by: Gengliang Wang <gengliang@apache.org>
Signed-off-by: Gengliang Wang <gengliang@apache.org>1 parent 12ab2c5 commit 9e13442
1 file changed
Lines changed: 54 additions & 28 deletions
Lines changed: 54 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
1988 | 1988 | | |
1989 | 1989 | | |
1990 | 1990 | | |
1991 | | - | |
1992 | | - | |
1993 | | - | |
1994 | | - | |
1995 | | - | |
1996 | | - | |
1997 | | - | |
1998 | | - | |
1999 | | - | |
2000 | | - | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
2001 | 2013 | | |
2002 | 2014 | | |
2003 | 2015 | | |
| |||
2017 | 2029 | | |
2018 | 2030 | | |
2019 | 2031 | | |
2020 | | - | |
2021 | | - | |
2022 | | - | |
2023 | | - | |
2024 | | - | |
2025 | | - | |
2026 | | - | |
2027 | | - | |
2028 | | - | |
2029 | | - | |
2030 | | - | |
2031 | | - | |
2032 | | - | |
2033 | | - | |
2034 | | - | |
2035 | | - | |
2036 | | - | |
| 2032 | + | |
| 2033 | + | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
| 2039 | + | |
| 2040 | + | |
| 2041 | + | |
| 2042 | + | |
| 2043 | + | |
| 2044 | + | |
| 2045 | + | |
| 2046 | + | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
| 2052 | + | |
| 2053 | + | |
| 2054 | + | |
| 2055 | + | |
| 2056 | + | |
| 2057 | + | |
| 2058 | + | |
| 2059 | + | |
| 2060 | + | |
| 2061 | + | |
| 2062 | + | |
2037 | 2063 | | |
2038 | 2064 | | |
2039 | 2065 | | |
| |||
0 commit comments