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

viirya 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 46d831611 Declare the value_length of decimal array as a `const` 
(#1968)
46d831611 is described below

commit 46d8316116979bde5c298255e14dd12cd94d8fd0
Author: Remzi Yang <[email protected]>
AuthorDate: Fri Jul 1 07:26:59 2022 +0800

    Declare the value_length of decimal array as a `const` (#1968)
    
    * declare the value_length of decimal array as a const
    
    Signed-off-by: remzi <[email protected]>
    
    * simpl the value function
    
    Signed-off-by: remzi <[email protected]>
---
 arrow/src/array/array_binary.rs | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/arrow/src/array/array_binary.rs b/arrow/src/array/array_binary.rs
index cbc6653c7..09f374561 100644
--- a/arrow/src/array/array_binary.rs
+++ b/arrow/src/array/array_binary.rs
@@ -772,10 +772,11 @@ pub struct DecimalArray {
     value_data: RawPtrBox<u8>,
     precision: usize,
     scale: usize,
-    length: i32,
 }
 
 impl DecimalArray {
+    const VALUE_LENGTH: i32 = 16;
+
     /// Returns the element at index `i`.
     pub fn value(&self, i: usize) -> Decimal128 {
         assert!(i < self.data.len(), "DecimalArray out of bounds access");
@@ -784,15 +785,15 @@ impl DecimalArray {
             let pos = self.value_offset_at(offset);
             std::slice::from_raw_parts(
                 self.value_data.as_ptr().offset(pos as isize),
-                (self.value_offset_at(offset + 1) - pos) as usize,
+                Self::VALUE_LENGTH as usize,
             )
         };
-        let as_array = raw_val.try_into();
-        let integer = match as_array {
-            Ok(v) if raw_val.len() == 16 => i128::from_le_bytes(v),
-            _ => panic!("DecimalArray elements are not 128bit integers."),
-        };
-        Decimal128::new_from_i128(self.precision, self.scale, integer)
+        let as_array = raw_val.try_into().unwrap();
+        Decimal128::new_from_i128(
+            self.precision,
+            self.scale,
+            i128::from_le_bytes(as_array),
+        )
     }
 
     /// Returns the offset for the element at index `i`.
@@ -807,8 +808,8 @@ impl DecimalArray {
     ///
     /// All elements have the same length as the array is a fixed size.
     #[inline]
-    pub fn value_length(&self) -> i32 {
-        self.length
+    pub const fn value_length(&self) -> i32 {
+        Self::VALUE_LENGTH
     }
 
     /// Returns a clone of the value data buffer
@@ -818,7 +819,7 @@ impl DecimalArray {
 
     #[inline]
     fn value_offset_at(&self, i: usize) -> i32 {
-        self.length * i as i32
+        Self::VALUE_LENGTH * i as i32
     }
 
     #[inline]
@@ -956,13 +957,11 @@ impl From<ArrayData> for DecimalArray {
             DataType::Decimal(precision, scale) => (*precision, *scale),
             _ => panic!("Expected data type to be Decimal"),
         };
-        let length = 16;
         Self {
             data,
             value_data: unsafe { RawPtrBox::new(values) },
             precision,
             scale,
-            length,
         }
     }
 }

Reply via email to