Repository navigation
Conversation
Dispatch the ANSI INT cast for types that can round across the lower bound on Spark releases without SPARK-60843. Keep BIGINT and lower-scale decimal casts in Rust. Pin the dispatch decision and reproduce the overflow on a Parquet column.
Restore the original planner and remove its compatibility assertions. Only change ANSI results for quotients equal to the destination minimum. Preserve the existing overflow where expanded decimals round down to INT_MIN - 1, since Arrow cannot identify the original Spark representation. Exercise the midpoint and its next decimal neighbor in the SQL fixture.
Return immediately after truncation for values between -INT_MAX and INT_MAX. Keep INT_MIN on the existing rounding path and leave the BIGINT conversion unchanged.
comphead
left a comment
There was a problem hiding this comment.
The boundaries are right at this head. INT and BIGINT accept MIN, reject MIN - 1, and accept fractional values that truncate to MIN. SMALLINT and TINYINT go through cast_decimal_to_int16_down, which checks against i32 first, so they never had this bug. I did not run anything. The comments cover a known compact-decimal mismatch that a precision gate would close and gaps in the fixture. A git merge-tree of this head with #7802 reports no conflict.
viirya
left a comment
There was a problem hiding this comment.
Thanks @sunchao. The signed-range check is right, and keeping the INT overflow in the half-ulp window is the safer side to err on. A few comments inline. Since this is a correctness fix, please consider a backport to branch-1.2, where the kernel is the same. The fork CI failures are in the build and setup steps, so a rerun should be enough.
|
Thanks for the reviews. I updated the cast and the fixtures, replied inline, and filed #7866 for the remaining rounding differences. @viirya I added |
rich7420
left a comment
There was a problem hiding this comment.
Thanks for the update! LGTM
viirya
left a comment
There was a problem hiding this comment.
Thanks @sunchao, this addresses everything I raised. The helper is easier to follow than the macro, and pinning the nested string cast in the fixture makes the reason to skip a precision gate explicit. Thanks also for correcting me on the Spark branches. SPARK-60843 is on 4.5 as well, not only 5.0. LGTM once the fork CI run is green.
Which issue does this PR close?
Closes #7805.
Rationale for this change
ANSI casts from DECIMAL to INT and BIGINT rejected the signed minimum. The old check compared the magnitude of the truncated value with MAX. That rejects MIN because its magnitude is MAX + 1.
What changes are included in this PR?
Truncate toward zero and use the destination's checked integer conversion. BIGINT needs only that check. For INT, only a result of
INT_MINneeds one more check. The shared kernel handles nulls, legacy wrapping and error formatting. The planner andtry_castrouting are unchanged.On the Spark versions Comet currently supports, Spark rounds expanded decimals through DOUBLE before checking the INT range. A decimal up to and including half a DOUBLE ulp (2⁻²²) above −2147483649 rounds to that integer and overflows. The INT helper preserves that error using the unscaled remainder.
For example, parsing
'-2147483648.9999999'asDECIMAL(18,7)can leave Spark with an expanded decimal, so the outer INT cast overflows. Reading the same decimal from Parquet can give Spark a compact long and returnINT_MIN. Arrow carries the same precision, scale and unscaled value in both cases. Comet preserves overflow in this interval, so the compact case can still fail. #7866 covers that distinction, other rounding differences and bypassing the check on Spark versions with SPARK-60843.How are these changes tested?
The Parquet fixtures run with dictionary encoding on and off. The separate ANSI fixture checks both signed bounds and errors immediately outside each, fractional truncation, the exact DOUBLE midpoint and its neighbor, the nested string cast, SMALLINT and TINYINT minima, and native
try_cast. The legacy fixture pairs inputs with outputs to check wrapping.A standalone Arrow 59.3.2 probe compared the revised kernel with efcdebe over scales 0–38, including null masks and legacy casts. All 23,376 comparisons matched (21,899 arrays and 1,477 overflow errors including their payloads).
The earlier, smaller SQL fixture passed on Spark 3.5.9, 4.0.4, 4.1.3 and 4.2.0 before the separate fast path. A 31-query Spark 4.1.3 comparison of that fast path with 6e2408a found the same results, errors and routing, including 12 preexisting differences from Spark. The expanded fixtures are part of the current fork CI run.
The Arrow probe timed the extracted ANSI kernels on identical DECIMAL(38,18) arrays. Numbers are median ns per input row, including nulls. Each of three pinned AMD EPYC Milan cores ran six warmups and 36 measured rounds, rotating the order. Each sample cast 81,920 rows 12 times. The build used Rust 1.97.1, thin LTO and one codegen unit.
Removing the separate fast path did not slow any of these inputs. These measurements cover the extracted kernels only. Commit 4e3f61d only updates the source comment.