vortex_array/array/sparse/compute/
slice.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use vortex_error::VortexResult;

use crate::array::sparse::SparseArray;
use crate::compute::{slice, SliceFn};
use crate::{Array, IntoArray};

impl SliceFn for SparseArray {
    fn slice(&self, start: usize, stop: usize) -> VortexResult<Array> {
        // Find the index of the first patch index that is greater than or equal to the offset of this array
        let index_start_index = self.search_index(start)?.to_index();
        let index_end_index = self.search_index(stop)?.to_index();

        Ok(Self::try_new_with_offset(
            slice(self.indices(), index_start_index, index_end_index)?,
            slice(self.values(), index_start_index, index_end_index)?,
            stop - start,
            self.indices_offset() + start,
            self.fill_value().clone(),
        )?
        .into_array())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::IntoArrayVariant;

    #[test]
    fn test_slice() {
        let values = vec![15_u32, 135, 13531, 42].into_array();
        let indices = vec![10_u64, 11, 50, 100].into_array();

        let sparse = SparseArray::try_new(indices.clone(), values, 101, 0_u32.into())
            .unwrap()
            .into_array();

        let sliced = slice(&sparse, 15, 100).unwrap();
        assert_eq!(sliced.len(), 100 - 15);
        let primitive = SparseArray::try_from(sliced)
            .unwrap()
            .values()
            .into_primitive()
            .unwrap();

        assert_eq!(primitive.maybe_null_slice::<u32>(), &[13531]);
    }

    #[test]
    fn doubly_sliced() {
        let values = vec![15_u32, 135, 13531, 42].into_array();
        let indices = vec![10_u64, 11, 50, 100].into_array();

        let sparse = SparseArray::try_new(indices.clone(), values, 101, 0_u32.into())
            .unwrap()
            .into_array();

        let sliced = slice(&sparse, 15, 100).unwrap();
        assert_eq!(sliced.len(), 100 - 15);
        let primitive = SparseArray::try_from(sliced.clone())
            .unwrap()
            .values()
            .into_primitive()
            .unwrap();

        assert_eq!(primitive.maybe_null_slice::<u32>(), &[13531]);

        let doubly_sliced = slice(&sliced, 35, 36).unwrap();
        let primitive_doubly_sliced = SparseArray::try_from(doubly_sliced)
            .unwrap()
            .values()
            .into_primitive()
            .unwrap();

        assert_eq!(primitive_doubly_sliced.maybe_null_slice::<u32>(), &[13531]);
    }
}