Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Apr 12, 2023
1 parent 050e021 commit 9b61c62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
26 changes: 5 additions & 21 deletions arrow-array/src/array/list_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,31 +463,15 @@ mod tests {
use crate::builder::{Int32Builder, ListBuilder};
use crate::types::Int32Type;
use crate::Int32Array;
use arrow_buffer::{bit_util, Buffer, ToByteSlice};
use arrow_buffer::{bit_util, Buffer, ScalarBuffer};
use arrow_schema::Field;

fn create_from_buffers() -> ListArray {
// Construct a value array
let value_data = ArrayData::builder(DataType::Int32)
.len(8)
.add_buffer(Buffer::from(&[0, 1, 2, 3, 4, 5, 6, 7].to_byte_slice()))
.build()
.unwrap();

// Construct a buffer for value offsets, for the nested array:
// [[0, 1, 2], [3, 4, 5], [6, 7]]
let value_offsets = Buffer::from(&[0, 3, 6, 8].to_byte_slice());

// Construct a list array from the above two
let list_data_type =
DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
let list_data = ArrayData::builder(list_data_type)
.len(3)
.add_buffer(value_offsets)
.add_child_data(value_data)
.build()
.unwrap();
ListArray::from(list_data)
let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
let offsets = OffsetBuffer::new(ScalarBuffer::from(vec![0, 3, 6, 8]));
let field = Arc::new(Field::new("item", DataType::Int32, true));
ListArray::new(field, offsets, Arc::new(values), None)
}

#[test]
Expand Down
14 changes: 11 additions & 3 deletions arrow-buffer/src/buffer/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {
/// # Panics
///
/// Panics if `buffer` is not a non-empty buffer containing
/// monotonically increasing values greater than zero
/// monotonically increasing values greater than or equal to zero
pub fn new(buffer: ScalarBuffer<O>) -> Self {
assert!(!buffer.is_empty(), "offsets cannot be empty");
assert!(buffer[0] > O::usize_as(0), "offsets must be greater than 0");
assert!(
buffer[0] >= O::usize_as(0),
"offsets must be greater than 0"
);
assert!(
buffer.windows(2).all(|w| w[0] <= w[1]),
"offsets must be monotonically increasing"
Expand All @@ -45,7 +48,7 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {
/// # Safety
///
/// `buffer` must be a non-empty buffer containing monotonically increasing
/// values greater than zero
/// values greater than or equal to zero
pub unsafe fn new_unchecked(buffer: ScalarBuffer<O>) -> Self {
Self(buffer)
}
Expand Down Expand Up @@ -110,6 +113,11 @@ mod tests {
OffsetBuffer::new(vec![-1, 0, 1].into());
}

#[test]
fn offsets() {
OffsetBuffer::new(vec![0, 1, 2, 3].into());
}

#[test]
#[should_panic(expected = "offsets must be monotonically increasing")]
fn non_monotonic_offsets() {
Expand Down

0 comments on commit 9b61c62

Please sign in to comment.