Skip to content

Commit 30d059a

Browse files
committed
fix
Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>
1 parent a46e3d2 commit 30d059a

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

vortex-array/src/arrays/chunked/tests.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use vortex_buffer::buffer;
88

99
use crate::IntoArray;
1010
use crate::accessor::ArrayAccessor;
11+
use crate::arrays::Chunked;
1112
use crate::arrays::ChunkedArray;
1213
use crate::arrays::ListArray;
1314
use crate::arrays::PrimitiveArray;
@@ -20,6 +21,7 @@ use crate::dtype::Nullability;
2021
use crate::dtype::PType;
2122
use crate::dtype::PType::I32;
2223
use crate::validity::Validity;
24+
use crate::vtable::VTable;
2325

2426
fn chunked_array() -> ChunkedArray {
2527
ChunkedArray::try_new(
@@ -194,3 +196,33 @@ pub fn pack_nested_lists() {
194196
assert_eq!(l1.scalar_at(0).unwrap(), canon_values.scalar_at(0).unwrap());
195197
assert_eq!(l2.scalar_at(0).unwrap(), canon_values.scalar_at(1).unwrap());
196198
}
199+
200+
#[test]
201+
fn with_slots_updates_nchunks_len_and_offsets() {
202+
let mut array = chunked_array();
203+
let slots = vec![
204+
Some(buffer![0u64, 4, 9].into_array()),
205+
Some(buffer![10u64, 11, 12, 13].into_array()),
206+
Some(buffer![14u64, 15, 16, 17, 18].into_array()),
207+
];
208+
let expected_nchunks = slots.len() - 1;
209+
let expected_len = array.len();
210+
211+
<Chunked as VTable>::with_slots(&mut array, slots).unwrap();
212+
213+
assert_eq!(array.nchunks(), expected_nchunks);
214+
assert_eq!(array.len(), expected_len);
215+
assert_eq!(array.chunk_offsets(), buffer![0u64, 4, 9]);
216+
assert_arrays_eq!(
217+
array.chunk(0).clone(),
218+
PrimitiveArray::from_iter([10u64, 11, 12, 13])
219+
);
220+
assert_arrays_eq!(
221+
array.chunk(1).clone(),
222+
PrimitiveArray::from_iter([14u64, 15, 16, 17, 18])
223+
);
224+
assert_arrays_eq!(
225+
array,
226+
PrimitiveArray::from_iter([10u64, 11, 12, 13, 14, 15, 16, 17, 18])
227+
);
228+
}

vortex-array/src/arrays/chunked/vtable/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::hash::Hash;
66
use itertools::Itertools;
77
use vortex_error::VortexResult;
88
use vortex_error::vortex_bail;
9+
use vortex_error::vortex_ensure;
910
use vortex_error::vortex_err;
1011
use vortex_error::vortex_panic;
1112
use vortex_session::VortexSession;
@@ -19,6 +20,7 @@ use crate::IntoArray;
1920
use crate::Precision;
2021
use crate::ToCanonical;
2122
use crate::arrays::ChunkedArray;
23+
use crate::arrays::Primitive;
2224
use crate::arrays::PrimitiveArray;
2325
use crate::arrays::chunked::compute::kernel::PARENT_KERNELS;
2426
use crate::arrays::chunked::compute::rules::PARENT_RULES;
@@ -210,6 +212,46 @@ impl VTable for Chunked {
210212
}
211213

212214
fn with_slots(array: &mut ChunkedArray, slots: Vec<Option<ArrayRef>>) -> VortexResult<()> {
215+
vortex_ensure!(!slots.is_empty(), "Chunked array needs at least one slot");
216+
217+
let chunk_offsets = slots[0]
218+
.as_ref()
219+
.ok_or_else(|| vortex_err!("Chunked array chunk_offsets slot must be present"))?;
220+
let chunk_offsets_dtype = DType::Primitive(PType::U64, Nullability::NonNullable);
221+
vortex_ensure!(
222+
chunk_offsets.dtype() == &chunk_offsets_dtype,
223+
MismatchedTypes: &chunk_offsets_dtype,
224+
chunk_offsets.dtype()
225+
);
226+
227+
#[cfg(debug_assertions)]
228+
{
229+
let chunk_offsets = chunk_offsets.as_opt::<Primitive>().unwrap_or_else(|| {
230+
vortex_panic!("Chunked array chunk_offsets slot must be primitive")
231+
});
232+
debug_assert_eq!(
233+
chunk_offsets.len(),
234+
slots.len(),
235+
"Expected {} chunk offsets, found {}",
236+
slots.len(),
237+
chunk_offsets.len(),
238+
);
239+
}
240+
241+
#[cfg(debug_assertions)]
242+
for (idx, chunk_slot) in slots[1..].iter().enumerate() {
243+
let chunk = chunk_slot
244+
.as_ref()
245+
.unwrap_or_else(|| vortex_panic!("Chunked array chunk slot {idx} must be present"));
246+
debug_assert!(
247+
chunk.dtype() == array.dtype(),
248+
"Chunked array chunk slot {} has dtype {:?}, expected {:?}",
249+
idx,
250+
chunk.dtype(),
251+
array.dtype(),
252+
);
253+
}
254+
213255
array.slots = slots;
214256
Ok(())
215257
}

0 commit comments

Comments
 (0)