Skip to content

Commit

Permalink
Add tests for slicing larger arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Jan 21, 2025
1 parent 595a835 commit 988359e
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion arrow-ipc/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ mod tests {
use std::io::Cursor;
use std::io::Seek;

use arrow_array::builder::GenericListBuilder;
use arrow_array::builder::{GenericListBuilder, ListBuilder, StringBuilder};
use arrow_array::builder::MapBuilder;
use arrow_array::builder::UnionBuilder;
use arrow_array::builder::{PrimitiveRunBuilder, UInt32Builder};
Expand Down Expand Up @@ -2433,6 +2433,78 @@ mod tests {
);
}

#[test]
fn test_large_slice_uint32() {
ensure_roundtrip(Arc::new(UInt32Array::from_iter((0..8000).map(|i| {
if i % 2 == 0 {
Some(i)
} else {
None
}
}))));
}

#[test]
fn test_large_slice_string() {
let strings: Vec<_> = (0..8000).map(|i| {
if i % 2 == 0 {
Some(format!("value{}", i))
} else {
None
}
}).collect();

ensure_roundtrip(Arc::new(StringArray::from(strings)));
}

#[test]
fn test_large_slice_string_list() {
let mut ls =
ListBuilder::new(StringBuilder::new());

let mut s = String::new();
for row_number in 0..8000 {
if row_number % 2 == 0 {
for list_element in 0..1000 {
s.clear();
use std::fmt::Write;
write!(&mut s, "value{row_number}-{list_element}");
ls.values().append_value(&s);
}
} else {
ls.values().append_null();
}
}

ensure_roundtrip(Arc::new(ls.finish()));
}

/// Read/write a record batch to a File and Stream and ensure it is the same at the outout
fn ensure_roundtrip(array: ArrayRef) {
let num_rows = array.len();
let orig_batch = RecordBatch::try_from_iter(vec![("a", array)]).unwrap();
// take off the first element
let sliced_batch = orig_batch.slice(1, num_rows-1);

let schema = orig_batch.schema();
let stream_data = {
let mut writer = StreamWriter::try_new(vec![], &schema).unwrap();
writer.write(&sliced_batch).unwrap();
writer.into_inner().unwrap()
};
let read_batch = {
let projection = None;
let mut reader = StreamReader::try_new(Cursor::new(stream_data), projection).unwrap();
reader
.next()
.expect("expect no errors reading batch")
.expect("expect batch")
};
assert_eq!(sliced_batch, read_batch);

// TODO test file writer/reader
}

#[test]
fn encode_bools_slice() {
// Test case for https://github.com/apache/arrow-rs/issues/3496
Expand Down

0 comments on commit 988359e

Please sign in to comment.