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::scalar::Scalar;
5use vortex_array::vtable::OperationsVTable;
6use vortex_error::VortexResult;
7
8use crate::SparseArray;
9use crate::SparseVTable;
10
11impl OperationsVTable<SparseVTable> for SparseVTable {
12    fn scalar_at(array: &SparseArray, index: usize) -> VortexResult<Scalar> {
13        Ok(array
14            .patches()
15            .get_patched(index)?
16            .unwrap_or_else(|| array.fill_scalar().clone()))
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use vortex_array::IntoArray;
23    use vortex_array::ToCanonical;
24    use vortex_array::arrays::PrimitiveArray;
25    use vortex_array::assert_arrays_eq;
26    use vortex_buffer::buffer;
27
28    use super::*;
29
30    #[test]
31    fn slice_partially_invalid() {
32        let values = buffer![0u64].into_array();
33        let indices = buffer![0u8].into_array();
34
35        let sparse = SparseArray::try_new(indices, values, 1000, 999u64.into()).unwrap();
36        let sliced = sparse.slice(0..1000).unwrap();
37        let mut expected = vec![999u64; 1000];
38        expected[0] = 0;
39
40        let values = sliced.to_primitive();
41        assert_arrays_eq!(values, PrimitiveArray::from_iter(expected));
42    }
43}