|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_buffer::ByteBufferMut; |
| 5 | +use vortex_buffer::buffer; |
| 6 | +use vortex_error::VortexResult; |
| 7 | +use vortex_session::registry::ReadContext; |
| 8 | + |
| 9 | +use crate::ArrayContext; |
| 10 | +use crate::IntoArray; |
| 11 | +use crate::VortexSessionExecute; |
| 12 | +use crate::array_session; |
| 13 | +use crate::arrays::ConstantArray; |
| 14 | +use crate::arrays::PiecewiseSequential; |
| 15 | +use crate::arrays::PiecewiseSequentialArray; |
| 16 | +use crate::arrays::PrimitiveArray; |
| 17 | +use crate::assert_arrays_eq; |
| 18 | +use crate::serde::SerializeOptions; |
| 19 | +use crate::serde::SerializedArray; |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn materializes_piecewise_indices() -> VortexResult<()> { |
| 23 | + let starts = buffer![3u64, 15, 21].into_array(); |
| 24 | + let lengths = buffer![3u64, 3, 3].into_array(); |
| 25 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 9)?.into_array(); |
| 26 | + |
| 27 | + let expected = PrimitiveArray::from_iter([3u64, 4, 5, 15, 16, 17, 21, 22, 23]).into_array(); |
| 28 | + assert_arrays_eq!(array, expected, &mut array_session().create_execution_ctx()); |
| 29 | + Ok(()) |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn materializes_repeated_and_empty_ranges() -> VortexResult<()> { |
| 34 | + let starts = buffer![5u64, 2, 5].into_array(); |
| 35 | + let lengths = buffer![2u64, 0, 2].into_array(); |
| 36 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 4)?.into_array(); |
| 37 | + |
| 38 | + let expected = PrimitiveArray::from_iter([5u64, 6, 5, 6]).into_array(); |
| 39 | + assert_arrays_eq!(array, expected, &mut array_session().create_execution_ctx()); |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn supports_constant_lengths() -> VortexResult<()> { |
| 45 | + let starts = buffer![0u64, 10, 20].into_array(); |
| 46 | + let lengths = ConstantArray::new(2u64, 3).into_array(); |
| 47 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 6)?.into_array(); |
| 48 | + |
| 49 | + let expected = PrimitiveArray::from_iter([0u64, 1, 10, 11, 20, 21]).into_array(); |
| 50 | + assert_arrays_eq!(array, expected, &mut array_session().create_execution_ctx()); |
| 51 | + Ok(()) |
| 52 | +} |
| 53 | + |
| 54 | +#[test] |
| 55 | +fn scalar_at_maps_into_piece() -> VortexResult<()> { |
| 56 | + let starts = buffer![3u64, 15, 21].into_array(); |
| 57 | + let lengths = buffer![3u64, 3, 3].into_array(); |
| 58 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 9)?.into_array(); |
| 59 | + let mut ctx = array_session().create_execution_ctx(); |
| 60 | + |
| 61 | + assert_eq!(array.execute_scalar(0, &mut ctx)?, 3u64.into()); |
| 62 | + assert_eq!(array.execute_scalar(4, &mut ctx)?, 16u64.into()); |
| 63 | + assert_eq!(array.execute_scalar(8, &mut ctx)?, 23u64.into()); |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn constructor_defers_range_value_validation() -> VortexResult<()> { |
| 69 | + let starts = buffer![u64::MAX].into_array(); |
| 70 | + let lengths = buffer![2u64].into_array(); |
| 71 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 2)?.into_array(); |
| 72 | + |
| 73 | + assert!( |
| 74 | + array |
| 75 | + .execute::<PrimitiveArray>(&mut array_session().create_execution_ctx()) |
| 76 | + .is_err() |
| 77 | + ); |
| 78 | + Ok(()) |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn execution_checks_declared_length() -> VortexResult<()> { |
| 83 | + let starts = buffer![0u64, 3].into_array(); |
| 84 | + let lengths = buffer![2u64, 2].into_array(); |
| 85 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 3)?.into_array(); |
| 86 | + |
| 87 | + assert!( |
| 88 | + array |
| 89 | + .execute::<PrimitiveArray>(&mut array_session().create_execution_ctx()) |
| 90 | + .is_err() |
| 91 | + ); |
| 92 | + Ok(()) |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn serde_roundtrip_preserves_piecewise_indices() -> VortexResult<()> { |
| 97 | + let array = PiecewiseSequentialArray::try_new( |
| 98 | + buffer![3u32, 15, 21].into_array(), |
| 99 | + buffer![2u16, 0, 2].into_array(), |
| 100 | + 4, |
| 101 | + )? |
| 102 | + .into_array(); |
| 103 | + let dtype = array.dtype().clone(); |
| 104 | + let len = array.len(); |
| 105 | + |
| 106 | + let array_ctx = ArrayContext::empty(); |
| 107 | + let serialized = array.serialize(&array_ctx, &array_session(), &SerializeOptions::default())?; |
| 108 | + |
| 109 | + let mut concat = ByteBufferMut::empty(); |
| 110 | + for buffer in serialized { |
| 111 | + concat.extend_from_slice(buffer.as_ref()); |
| 112 | + } |
| 113 | + |
| 114 | + let parts = SerializedArray::try_from(concat.freeze())?; |
| 115 | + let decoded = parts.decode( |
| 116 | + &dtype, |
| 117 | + len, |
| 118 | + &ReadContext::new(array_ctx.to_ids()), |
| 119 | + &array_session(), |
| 120 | + )?; |
| 121 | + |
| 122 | + assert!(decoded.is::<PiecewiseSequential>()); |
| 123 | + assert_arrays_eq!( |
| 124 | + decoded, |
| 125 | + PrimitiveArray::from_iter([3u64, 4, 21, 22]).into_array(), |
| 126 | + &mut array_session().create_execution_ctx() |
| 127 | + ); |
| 128 | + Ok(()) |
| 129 | +} |
0 commit comments