1use vortex_array::ArrayView;
5use vortex_array::ExecutionCtx;
6use vortex_array::scalar::Scalar;
7use vortex_array::vtable::OperationsVTable;
8use vortex_error::VortexResult;
9
10use crate::Sparse;
11use crate::SparseExt as _;
12
13impl OperationsVTable<Sparse> for Sparse {
14 fn scalar_at(
15 array: ArrayView<'_, Sparse>,
16 index: usize,
17 _ctx: &mut ExecutionCtx,
18 ) -> VortexResult<Scalar> {
19 Ok(array
20 .patches()
21 .get_patched(index)?
22 .unwrap_or_else(|| array.fill_scalar().clone()))
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use std::sync::LazyLock;
29
30 use vortex_array::IntoArray;
31 use vortex_array::VortexSessionExecute;
32 use vortex_array::arrays::PrimitiveArray;
33 use vortex_array::assert_arrays_eq;
34 use vortex_buffer::buffer;
35 use vortex_session::VortexSession;
36
37 use crate::Sparse;
38
39 static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
40 let session = vortex_array::array_session();
41 crate::initialize(&session);
42 session
43 });
44
45 #[test]
46 fn slice_partially_invalid() {
47 let mut ctx = SESSION.create_execution_ctx();
48 let values = buffer![0u64].into_array();
49 let indices = buffer![0u8].into_array();
50
51 let sparse = Sparse::try_new(indices, values, 1000, 999u64.into()).unwrap();
52 let sliced = sparse.slice(0..1000).unwrap();
53 let mut expected = vec![999u64; 1000];
54 expected[0] = 0;
55
56 let values = sliced.execute::<PrimitiveArray>(&mut ctx).unwrap();
57 assert_arrays_eq!(values, PrimitiveArray::from_iter(expected), &mut ctx);
58 }
59}