Skip to content

fix: accept signed minima in ANSI decimal casts to INT and BIGINT - #7806

Open
sunchao wants to merge 6 commits into
apache:mainfrom
sunchao:fix/decimal-cast-signed-min
Open

sunchao wants to merge 6 commits into
apache:mainfrom
sunchao:fix/decimal-cast-signed-min

Conversation

@sunchao

@sunchao sunchao commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

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_MIN needs one more check. The shared kernel handles nulls, legacy wrapping and error formatting. The planner and try_cast routing 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' as DECIMAL(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 return INT_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.

Input Base c7a0480 Separate fast path efcdebe Revised 93254d3
INT, no nulls 9.89 10.21 8.91
INT, 20% random nulls 8.29 8.29 7.52
INT, 1% truncating to MIN overflow 10.32 9.03
BIGINT, no nulls 9.97 9.31 8.66

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.

@github-actions github-actions Bot added bug Something isn't working area:expressions Expression evaluation labels Dec 1, 2026
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.
@sunchao
sunchao requested review from andygrove, comphead and viirya and a balanced review from Copilot December 1, 2026 03:51
@sunchao
sunchao removed the request for review from Copilot December 1, 2026 03:51

@rich7420 rich7420 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@sunchao thanks for the patch!

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks @sunchao, the PR goes into right direction, please check some nits!

@viirya viirya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@sunchao

sunchao commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Thanks for the reviews. I updated the cast and the fixtures, replied inline, and filed #7866 for the remaining rounding differences.

@viirya I added backport-1.2. That branch still has the same range check, and the revised patch applies cleanly there. The backport can use the squash commit once this lands on main. The push started a fresh fork CI run on 4e3f61d.

@rich7420 rich7420 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update! LGTM

@viirya viirya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:expressions Expression evaluation backport-1.2 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ANSI decimal casts to INT and BIGINT raise CAST_OVERFLOW at the signed minimum

4 participants