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
use vortex_error::VortexResult;

use crate::array::{BoolArray, BoolEncoding};
use crate::compute::SliceFn;
use crate::{ArrayData, IntoArrayData};

impl SliceFn<BoolArray> for BoolEncoding {
    fn slice(&self, array: &BoolArray, start: usize, stop: usize) -> VortexResult<ArrayData> {
        Ok(BoolArray::try_new(
            array.boolean_buffer().slice(start, stop - start),
            array.validity().slice(start, stop)?,
        )?
        .into_array())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compute::{scalar_at, slice};
    use crate::validity::ArrayValidity;
    use crate::ArrayLen;

    #[test]
    fn test_slice() {
        let arr = BoolArray::from_iter([Some(true), Some(true), None, Some(false), None]);
        let sliced_arr = slice(arr.as_ref(), 1, 4).unwrap();
        let sliced_arr = BoolArray::try_from(sliced_arr).unwrap();

        assert_eq!(sliced_arr.len(), 3);

        let s = scalar_at(sliced_arr.as_ref(), 0).unwrap();
        assert_eq!(s.as_bool().value(), Some(true));

        let s = scalar_at(sliced_arr.as_ref(), 1).unwrap();
        assert!(!sliced_arr.is_valid(1));
        assert!(s.is_null());

        let s = scalar_at(sliced_arr.as_ref(), 2).unwrap();
        assert_eq!(s.as_bool().value(), Some(false));
    }
}