Skip to content

Commit

Permalink
Fix the behavior of from_fixed_size_list when offset > 0 (#1964)
Browse files Browse the repository at this point in the history
* fix the behaviour of from_fixed_size_list when offset > 0

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

* Update arrow/src/array/array_binary.rs

Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com>

Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com>
  • Loading branch information
HaoYang670 and tustvold committed Jun 29, 2022
1 parent 420d669 commit 8bb494e
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions arrow/src/array/array_binary.rs
Expand Up @@ -831,22 +831,26 @@ impl DecimalArray {
precision: usize,
scale: usize,
) -> Self {
let child_data = &v.data_ref().child_data()[0];
assert_eq!(
v.data_ref().child_data()[0].child_data().len(),
child_data.child_data().len(),
0,
"DecimalArray can only be created from list array of u8 values \
(i.e. FixedSizeList<PrimitiveArray<u8>>)."
);
assert_eq!(
v.data_ref().child_data()[0].data_type(),
child_data.data_type(),
&DataType::UInt8,
"DecimalArray can only be created from FixedSizeList<u8> arrays, mismatched data types."
);

let list_offset = v.offset();
let child_offset = child_data.offset();
let builder = ArrayData::builder(DataType::Decimal(precision, scale))
.len(v.len())
.add_buffer(v.data_ref().child_data()[0].buffers()[0].clone())
.null_bit_buffer(v.data_ref().null_buffer().cloned());
.add_buffer(child_data.buffers()[0].slice(child_offset))
.null_bit_buffer(v.data_ref().null_buffer().cloned())
.offset(list_offset);

let array_data = unsafe { builder.build_unchecked() };
Self::from(array_data)
Expand Down Expand Up @@ -1677,6 +1681,37 @@ mod tests {
);
}

#[test]
fn test_decimal_array_from_fixed_size_list() {
let value_data = ArrayData::builder(DataType::UInt8)
.offset(16)
.len(48)
.add_buffer(Buffer::from_slice_ref(&[99999_i128, 12, 34, 56]))
.build()
.unwrap();

let null_buffer = Buffer::from_slice_ref(&[0b101]);

// Construct a list array from the above two
let list_data_type = DataType::FixedSizeList(
Box::new(Field::new("item", DataType::UInt8, false)),
16,
);
let list_data = ArrayData::builder(list_data_type)
.len(2)
.null_bit_buffer(Some(null_buffer))
.offset(1)
.add_child_data(value_data)
.build()
.unwrap();
let list_array = FixedSizeListArray::from(list_data);
let decimal = DecimalArray::from_fixed_size_list_array(list_array, 38, 0);

assert_eq!(decimal.len(), 2);
assert!(decimal.is_null(0));
assert_eq!(decimal.value_as_string(1), "56".to_string());
}

#[test]
fn test_fixed_size_binary_array_from_iter() {
let input_arg = vec![vec![1, 2], vec![3, 4], vec![5, 6]];
Expand Down

0 comments on commit 8bb494e

Please sign in to comment.