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-12101-739f9cc34a59a3af35ef57875e426c302f0a371a
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 5cdc728dcf5937ede19b381fa83ae20a221a7a93
Author: Phoenix <[email protected]>
AuthorDate: Fri Nov 6 18:24:45 2026 +0000

    fix(array): validate decimals with negative scale (#12101)
    
    # Which issue does this PR close?
    
    - Closes #12095
    
    # Rationale for this change
    
    `validate_decimal_precision` rejects valid decimal arrays with a
    negative scale because casting the scale to `u8` makes it a large
    positive number. For example, an array storing `1` with precision 5 and
    scale -2 represents 100, but validation incorrectly reports that
    precision 5 is less than scale -2.
    
    # What changes are included in this PR?
    
    Only compare scale against precision when scale is positive, matching
    `validate_decimal_precision_and_scale`. The per-value precision checks
    remain unchanged.
    
    # Are these changes tested?
    
    A regression test covers Decimal32, Decimal64, Decimal128 and Decimal256
    with negative scales (including `i8::MIN`), zero and positive scales,
    nulls, and values at the precision boundaries. It also verifies that
    out-of-range values and positive scales exceeding the requested
    precision still fail. The test failed before the fix and passed
    afterward. The full workspace test suite, 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 the implementation, regression
    test, validation and PR description.
---
 arrow-array/src/array/primitive_array.rs | 52 +++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/arrow-array/src/array/primitive_array.rs 
b/arrow-array/src/array/primitive_array.rs
index 7069037c5e..a3f2d87dc5 100644
--- a/arrow-array/src/array/primitive_array.rs
+++ b/arrow-array/src/array/primitive_array.rs
@@ -1706,7 +1706,7 @@ impl<T: DecimalType + ArrowPrimitiveType> 
PrimitiveArray<T> {
     /// Validates values in this array can be properly interpreted
     /// with the specified precision.
     pub fn validate_decimal_precision(&self, precision: u8) -> Result<(), 
ArrowError> {
-        if precision < self.scale() as u8 {
+        if self.scale() > 0 && precision < self.scale() as u8 {
             return Err(ArrowError::InvalidArgumentError(format!(
                 "Decimal precision {precision} is less than scale {}",
                 self.scale()
@@ -2616,6 +2616,56 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_decimal_validate_precision_with_negative_scale() {
+        fn check<T: DecimalType>()
+        where
+            T::Native: From<i32>,
+        {
+            for scale in [i8::MIN, -2, -1, 0, 2] {
+                let array = PrimitiveArray::<T>::from_iter(
+                    [Some(-99), Some(0), Some(1), Some(99), None]
+                        .into_iter()
+                        .map(|v| v.map(T::Native::from)),
+                )
+                .with_precision_and_scale(2, scale)
+                .unwrap();
+                array.validate_decimal_precision(2).unwrap();
+
+                for value in [-100, 100] {
+                    let array = 
PrimitiveArray::<T>::from_iter_values([T::Native::from(value)])
+                        .with_precision_and_scale(3, scale)
+                        .unwrap();
+                    assert!(array.validate_decimal_precision(2).is_err());
+                }
+            }
+
+            let array = 
PrimitiveArray::<T>::from_iter_values([T::Native::from(100)])
+                .with_precision_and_scale(3, -2)
+                .unwrap();
+            assert_eq!(
+                array.validate_decimal_precision(2).unwrap_err().to_string(),
+                format!(
+                    "Invalid argument error: 10000 is too large to store in a 
{} of precision 2. Max is 9900",
+                    T::PREFIX
+                )
+            );
+
+            let array = 
PrimitiveArray::<T>::from_iter_values([T::Native::from(0)])
+                .with_precision_and_scale(2, 2)
+                .unwrap();
+            assert_eq!(
+                array.validate_decimal_precision(1).unwrap_err().to_string(),
+                "Invalid argument error: Decimal precision 1 is less than 
scale 2"
+            );
+        }
+
+        check::<Decimal32Type>();
+        check::<Decimal64Type>();
+        check::<Decimal128Type>();
+        check::<Decimal256Type>();
+    }
+
     #[test]
     fn test_decimal_from_iter_values() {
         let array = Decimal128Array::from_iter_values(vec![-100, 0, 101]);

Reply via email to