|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_buffer::buffer; |
| 5 | +use vortex_error::VortexResult; |
| 6 | + |
| 7 | +use crate::IntoArray; |
| 8 | +use crate::VortexSessionExecute; |
| 9 | +use crate::array_session; |
| 10 | +use crate::arrays::ConstantArray; |
| 11 | +use crate::arrays::PiecewiseSequentialArray; |
| 12 | +use crate::arrays::PrimitiveArray; |
| 13 | +use crate::assert_arrays_eq; |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn materializes_piecewise_indices() -> VortexResult<()> { |
| 17 | + let starts = buffer![3u64, 15, 21].into_array(); |
| 18 | + let lengths = buffer![3u64, 3, 3].into_array(); |
| 19 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 9)?.into_array(); |
| 20 | + |
| 21 | + let expected = PrimitiveArray::from_iter([3u64, 4, 5, 15, 16, 17, 21, 22, 23]).into_array(); |
| 22 | + assert_arrays_eq!(array, expected, &mut array_session().create_execution_ctx()); |
| 23 | + Ok(()) |
| 24 | +} |
| 25 | + |
| 26 | +#[test] |
| 27 | +fn materializes_repeated_and_empty_ranges() -> VortexResult<()> { |
| 28 | + let starts = buffer![5u64, 2, 5].into_array(); |
| 29 | + let lengths = buffer![2u64, 0, 2].into_array(); |
| 30 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 4)?.into_array(); |
| 31 | + |
| 32 | + let expected = PrimitiveArray::from_iter([5u64, 6, 5, 6]).into_array(); |
| 33 | + assert_arrays_eq!(array, expected, &mut array_session().create_execution_ctx()); |
| 34 | + Ok(()) |
| 35 | +} |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn supports_constant_lengths() -> VortexResult<()> { |
| 39 | + let starts = buffer![0u64, 10, 20].into_array(); |
| 40 | + let lengths = ConstantArray::new(2u64, 3).into_array(); |
| 41 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 6)?.into_array(); |
| 42 | + |
| 43 | + let expected = PrimitiveArray::from_iter([0u64, 1, 10, 11, 20, 21]).into_array(); |
| 44 | + assert_arrays_eq!(array, expected, &mut array_session().create_execution_ctx()); |
| 45 | + Ok(()) |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn scalar_at_maps_into_piece() -> VortexResult<()> { |
| 50 | + let starts = buffer![3u64, 15, 21].into_array(); |
| 51 | + let lengths = buffer![3u64, 3, 3].into_array(); |
| 52 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 9)?.into_array(); |
| 53 | + let mut ctx = array_session().create_execution_ctx(); |
| 54 | + |
| 55 | + assert_eq!(array.execute_scalar(0, &mut ctx)?, 3u64.into()); |
| 56 | + assert_eq!(array.execute_scalar(4, &mut ctx)?, 16u64.into()); |
| 57 | + assert_eq!(array.execute_scalar(8, &mut ctx)?, 23u64.into()); |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | + |
| 61 | +#[test] |
| 62 | +fn constructor_defers_range_value_validation() -> VortexResult<()> { |
| 63 | + let starts = buffer![u64::MAX].into_array(); |
| 64 | + let lengths = buffer![2u64].into_array(); |
| 65 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 2)?.into_array(); |
| 66 | + |
| 67 | + assert!( |
| 68 | + array |
| 69 | + .execute::<PrimitiveArray>(&mut array_session().create_execution_ctx()) |
| 70 | + .is_err() |
| 71 | + ); |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +#[test] |
| 76 | +fn execution_checks_declared_length() -> VortexResult<()> { |
| 77 | + let starts = buffer![0u64, 3].into_array(); |
| 78 | + let lengths = buffer![2u64, 2].into_array(); |
| 79 | + let array = PiecewiseSequentialArray::try_new(starts, lengths, 3)?.into_array(); |
| 80 | + |
| 81 | + assert!( |
| 82 | + array |
| 83 | + .execute::<PrimitiveArray>(&mut array_session().create_execution_ctx()) |
| 84 | + .is_err() |
| 85 | + ); |
| 86 | + Ok(()) |
| 87 | +} |
0 commit comments