Skip to content

fix: do not overflow in unwrap_cast for decimals with a negative scale - #24897

Open
edubraqd wants to merge 1 commit into
apache:mainfrom
edubraqd:fix/unwrap-cast-negative-scale
Open

fix: do not overflow in unwrap_cast for decimals with a negative scale#24897
edubraqd wants to merge 1 commit into
apache:mainfrom
edubraqd:fix/unwrap-cast-negative-scale

Conversation

@edubraqd

@edubraqd edubraqd commented Sep 3, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

try_cast_numeric_literal computes 10_i128.pow(scale as u32) for both the literal's and the target's decimal scale. A negative scale wraps to a huge exponent, so simplifying arrow_cast(1, 'Decimal128(10, -2)') = 100 panicked with "attempt to multiply with overflow" in the unwrap_cast simplifier.

What changes are included in this PR?

A negative scale means the decimal holds multiples of 10^-scale, so the integer rescaling this path relies on does not apply. try_cast_numeric_literal now returns None when either side has a negative scale, so the simplifier leaves the cast in place, the same way it already does for other casts it cannot express.

Are these changes tested?

Yes. test_try_cast_to_type_negative_scale_decimal covers a negative-scale target for Decimal32/Decimal64/Decimal128 and a negative-scale literal; each used to panic.

Are there any user-facing changes?

No panic. Such comparisons are no longer simplified by unwrap_cast; they were never simplified correctly before, since the process panicked.

`try_cast_numeric_literal` computes `10_i128.pow(scale as u32)` for both the
literal's and the target's decimal scale. A negative scale wraps to a huge
exponent, so simplifying an expression such as

    arrow_cast(1, 'Decimal128(10, -2)') = 100

panicked with "attempt to multiply with overflow" in the `unwrap_cast`
simplifier.

A negative scale means the decimal holds multiples of `10^-scale`, so the
integer rescaling this path relies on does not apply. Return `None` for such
casts so the simplifier leaves them in place.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comparing a decimal with a negative scale to an integer literal panics in unwrap_cast

1 participant