Skip to content

Commit

Permalink
Declare the value_length of decimal array as a const (#1968)
Browse files Browse the repository at this point in the history
* declare the value_length of decimal array as a const

Signed-off-by: remzi <13716567376yh@gmail.com>

* simpl the value function

Signed-off-by: remzi <13716567376yh@gmail.com>
  • Loading branch information
HaoYang670 authored Jun 30, 2022
1 parent a89c1cf commit 46d8316
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions arrow/src/array/array_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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`.
Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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,
}
}
}
Expand Down

0 comments on commit 46d8316

Please sign in to comment.