This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12104-cf143bd1a0765b62d7a97be73293b5bd99705773 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit b7ff1b3681deec12ace46a9a6903d6657be421b8 Author: Phoenix <[email protected]> AuthorDate: Sat Nov 7 15:41:43 2026 +0000 fix(variant): avoid overflow for minimum scale (#12104) # Which issue does this PR close? - Closes #12103 # Rationale for this change `try_new_with_signed_scale(1, i8::MIN)` panics when integer overflow checks are enabled because negating the scale overflows `i8` before the checked rescaling operations run. The unrepresentable value should instead reach the existing overflow error path, as it does in default release builds. # What changes are included in this PR? Use `scale.unsigned_abs()` and widen the exponent to `u32` in the shared Variant decimal implementation. The existing checked power, checked multiplication and error handling remain unchanged. # Are these changes tested? A regression test covers `VariantDecimal4`, `VariantDecimal8` and `VariantDecimal16`. It checks that scales -128 and -127 return an error, and that ordinary negative-scale conversions preserve positive and negative values correctly. The test panicked on the old debug implementation and passes after the fix in both debug and release builds. The full workspace tests, all-features Clippy, formatting and spelling checks passed locally. # Are there any significant user-facing changes? No public API changes. AI assistance: OpenAI Codex assisted with investigation, implementation, the regression test, validation and this PR description. --- parquet-variant/src/variant/decimal.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/parquet-variant/src/variant/decimal.rs b/parquet-variant/src/variant/decimal.rs index f8b58d61a1..b0cfaf656f 100644 --- a/parquet-variant/src/variant/decimal.rs +++ b/parquet-variant/src/variant/decimal.rs @@ -185,7 +185,8 @@ macro_rules! impl_variant_decimal { fn try_new_with_signed_scale(integer: $native, scale: i8) -> Result<Self, ArrowError> { let (integer, scale) = if scale < 0 { - let multiplier = <$native>::checked_pow(10, -scale as u32); + let multiplier = + <$native>::checked_pow(10, u32::from(scale.unsigned_abs())); let Some(rescaled) = multiplier.and_then(|m| integer.checked_mul(m)) else { return Err(ArrowError::InvalidArgumentError(format!( "Overflow when rescaling {integer} with scale {scale}" @@ -382,6 +383,31 @@ impl_try_from_int_for_decimal!(i128, VariantDecimal16); mod tests { use super::*; + #[test] + fn test_variant_decimal_signed_scale() { + fn check<D: VariantDecimalType>() + where + D::Native: From<i32> + std::fmt::Debug + PartialEq, + { + for scale in [i8::MIN, -127] { + assert!(matches!( + D::try_new_with_signed_scale(1.into(), scale), + Err(ArrowError::InvalidArgumentError(_)) + )); + } + + for value in [-12, 12] { + let decimal = D::try_new_with_signed_scale(value.into(), -2).unwrap(); + assert_eq!(decimal.integer(), (value * 100).into()); + assert_eq!(decimal.scale(), 0); + } + } + + check::<VariantDecimal4>(); + check::<VariantDecimal8>(); + check::<VariantDecimal16>(); + } + #[test] fn test_variant_decimal_invalid_precision() { // Test precision validation for Decimal4
