You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2022/11/22 16:55:42 UTC

[arrow-rs] branch master updated: Prevent precision=0 for decimal type (#3162)

This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 6455e3401 Prevent precision=0 for decimal type (#3162)
6455e3401 is described below

commit 6455e340168595e8c69f8d4bae59487e651bd513
Author: Vrishabh <ps...@gmail.com>
AuthorDate: Tue Nov 22 22:25:36 2022 +0530

    Prevent precision=0 for decimal type (#3162)
    
    * Adding decimal precision checks
    
    * Doc edits
---
 arrow-array/src/array/primitive_array.rs | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/arrow-array/src/array/primitive_array.rs b/arrow-array/src/array/primitive_array.rs
index 195e2dc19..487fc2a17 100644
--- a/arrow-array/src/array/primitive_array.rs
+++ b/arrow-array/src/array/primitive_array.rs
@@ -993,12 +993,13 @@ impl<T: ArrowPrimitiveType> From<ArrayData> for PrimitiveArray<T> {
 
 impl<T: DecimalType + ArrowPrimitiveType> PrimitiveArray<T> {
     /// Returns a Decimal array with the same data as self, with the
-    /// specified precision.
+    /// specified precision and scale.
     ///
     /// Returns an Error if:
-    /// 1. `precision` is larger than `T:MAX_PRECISION`
-    /// 2. `scale` is larger than `T::MAX_SCALE`
-    /// 3. `scale` is > `precision`
+    /// - `precision` is zero
+    /// - `precision` is larger than `T:MAX_PRECISION`
+    /// - `scale` is larger than `T::MAX_SCALE`
+    /// - `scale` is > `precision`
     pub fn with_precision_and_scale(
         self,
         precision: u8,
@@ -1025,18 +1026,24 @@ impl<T: DecimalType + ArrowPrimitiveType> PrimitiveArray<T> {
         precision: u8,
         scale: u8,
     ) -> Result<(), ArrowError> {
+        if precision == 0 {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "precision cannot be 0, has to be between [1, {}]",
+                T::MAX_PRECISION
+            )));
+        }
         if precision > T::MAX_PRECISION {
             return Err(ArrowError::InvalidArgumentError(format!(
                 "precision {} is greater than max {}",
                 precision,
-                Decimal128Type::MAX_PRECISION
+                T::MAX_PRECISION
             )));
         }
         if scale > T::MAX_SCALE {
             return Err(ArrowError::InvalidArgumentError(format!(
                 "scale {} is greater than max {}",
                 scale,
-                Decimal128Type::MAX_SCALE
+                T::MAX_SCALE
             )));
         }
         if scale > precision {
@@ -1934,6 +1941,14 @@ mod tests {
         arr.validate_decimal_precision(5).unwrap();
     }
 
+    #[test]
+    #[should_panic(expected = "precision cannot be 0, has to be between [1, 38]")]
+    fn test_decimal_array_with_precision_zero() {
+        Decimal128Array::from_iter_values([12345, 456])
+            .with_precision_and_scale(0, 2)
+            .unwrap();
+    }
+
     #[test]
     #[should_panic(expected = "precision 40 is greater than max 38")]
     fn test_decimal_array_with_precision_and_scale_invalid_precision() {