|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Tests for the chunk offsets child of uniformly bit-packed arrays. |
| 5 | +
|
| 6 | +use std::sync::LazyLock; |
| 7 | + |
| 8 | +use vortex_array::IntoArray; |
| 9 | +use vortex_array::VortexSessionExecute; |
| 10 | +use vortex_array::arrays::PrimitiveArray; |
| 11 | +use vortex_array::assert_arrays_eq; |
| 12 | +use vortex_error::VortexResult; |
| 13 | +use vortex_session::VortexSession; |
| 14 | + |
| 15 | +use crate::BitPacked; |
| 16 | +use crate::BitPackedArray; |
| 17 | +use crate::BitPackedArrayExt; |
| 18 | +use crate::BitPackedArraySlotsExt; |
| 19 | +use crate::bitpacking::bitpack_compress::bitpack_to_best_bit_width; |
| 20 | + |
| 21 | +static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { |
| 22 | + let session = vortex_array::array_session(); |
| 23 | + crate::initialize(&session); |
| 24 | + session |
| 25 | +}); |
| 26 | + |
| 27 | +fn encode(values: &[u32]) -> VortexResult<BitPackedArray> { |
| 28 | + let mut ctx = SESSION.create_execution_ctx(); |
| 29 | + bitpack_to_best_bit_width(&PrimitiveArray::from_iter(values.iter().copied()), &mut ctx) |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn chunk_offsets_are_validated() -> VortexResult<()> { |
| 34 | + let mut ctx = SESSION.create_execution_ctx(); |
| 35 | + let uniform = encode(&(0..3000u32).map(|i| i % 128).collect::<Vec<_>>())?; |
| 36 | + assert_eq!(uniform.bit_width(), 7); |
| 37 | + assert_arrays_eq!( |
| 38 | + uniform.chunk_offsets(), |
| 39 | + vortex_buffer::buffer![0u64, 896, 1792, 2688].into_array(), |
| 40 | + &mut ctx |
| 41 | + ); |
| 42 | + for offsets in [ |
| 43 | + vortex_buffer::buffer![0u64, 896, 1792], |
| 44 | + vortex_buffer::buffer![0u64, 896, 1791, 2688], |
| 45 | + vortex_buffer::buffer![0u64, 896, 768, 2688], |
| 46 | + vortex_buffer::buffer![0u64, 768, 1792, 2688], |
| 47 | + ] { |
| 48 | + assert!(BitPacked::with_chunk_offsets(uniform.clone(), offsets.into_array()).is_err()); |
| 49 | + } |
| 50 | + let wrong_dtype = PrimitiveArray::from_iter([0u32, 896, 1792, 2688]).into_array(); |
| 51 | + assert!(BitPacked::with_chunk_offsets(uniform.clone(), wrong_dtype).is_err()); |
| 52 | + let nullable = |
| 53 | + PrimitiveArray::from_option_iter([Some(0u64), Some(896), None, Some(2688)]).into_array(); |
| 54 | + assert!(BitPacked::with_chunk_offsets(uniform.clone(), nullable).is_err()); |
| 55 | + let rebased = vortex_buffer::buffer![128u64, 1024, 1920, 2816].into_array(); |
| 56 | + let replaced = BitPacked::with_chunk_offsets(uniform.clone(), rebased)?; |
| 57 | + assert_arrays_eq!(uniform, replaced, &mut ctx); |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | + |
| 61 | +#[test] |
| 62 | +fn empty_and_zero_width_offsets() -> VortexResult<()> { |
| 63 | + let mut ctx = SESSION.create_execution_ctx(); |
| 64 | + let empty = encode(&[])?; |
| 65 | + assert_arrays_eq!( |
| 66 | + empty.chunk_offsets(), |
| 67 | + vortex_buffer::buffer![0u64].into_array(), |
| 68 | + &mut ctx |
| 69 | + ); |
| 70 | + let zeros = encode(&vec![0u32; 2049])?; |
| 71 | + assert_arrays_eq!( |
| 72 | + zeros.chunk_offsets(), |
| 73 | + vortex_buffer::buffer![0u64, 0, 0, 0].into_array(), |
| 74 | + &mut ctx |
| 75 | + ); |
| 76 | + assert_arrays_eq!(zeros, PrimitiveArray::from_iter(vec![0u32; 2049]), &mut ctx); |
| 77 | + Ok(()) |
| 78 | +} |
0 commit comments