Skip to main content

vortex_sparse/
ops.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use 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;
11
12impl OperationsVTable<Sparse> for Sparse {
13    fn scalar_at(
14        array: ArrayView<'_, Sparse>,
15        index: usize,
16        _ctx: &mut ExecutionCtx,
17    ) -> VortexResult<Scalar> {
18        Ok(array
19            .patches()
20            .get_patched(index)?
21            .unwrap_or_else(|| array.fill_scalar().clone()))
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use vortex_array::IntoArray;
28    use vortex_array::ToCanonical;
29    use vortex_array::arrays::PrimitiveArray;
30    use vortex_array::assert_arrays_eq;
31    use vortex_buffer::buffer;
32
33    use crate::Sparse;
34
35    #[test]
36    fn slice_partially_invalid() {
37        let values = buffer![0u64].into_array();
38        let indices = buffer![0u8].into_array();
39
40        let sparse = Sparse::try_new(indices, values, 1000, 999u64.into()).unwrap();
41        let sliced = sparse.slice(0..1000).unwrap();
42        let mut expected = vec![999u64; 1000];
43        expected[0] = 0;
44
45        let values = sliced.to_primitive();
46        assert_arrays_eq!(values, PrimitiveArray::from_iter(expected));
47    }
48}