Skip to main content

vortex_array/arrays/bool/vtable/
operations.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ExecutionCtx;
7use crate::array::ArrayView;
8use crate::array::OperationsVTable;
9use crate::arrays::Bool;
10use crate::arrays::bool::BoolArrayExt;
11use crate::scalar::Scalar;
12
13impl OperationsVTable<Bool> for Bool {
14    fn scalar_at(
15        array: ArrayView<'_, Bool>,
16        index: usize,
17        _ctx: &mut ExecutionCtx,
18    ) -> VortexResult<Scalar> {
19        Ok(Scalar::bool(
20            array.to_bit_buffer().value(index),
21            array.dtype().nullability(),
22        ))
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use std::iter;
29
30    use crate::IntoArray;
31    use crate::ToCanonical;
32    use crate::arrays::BoolArray;
33    use crate::arrays::bool::BoolArrayExt;
34    use crate::assert_arrays_eq;
35
36    #[test]
37    fn test_slice_hundred_elements() {
38        let arr = BoolArray::from_iter(iter::repeat_n(Some(true), 100));
39        let sliced_arr = arr.into_array().slice(8..16).unwrap().to_bool();
40        assert_eq!(sliced_arr.len(), 8);
41        assert_eq!(sliced_arr.to_bit_buffer().len(), 8);
42        assert_eq!(sliced_arr.to_bit_buffer().offset(), 0);
43    }
44
45    #[test]
46    fn test_slice() {
47        let arr = BoolArray::from_iter([Some(true), Some(true), None, Some(false), None]);
48        let sliced_arr = arr.into_array().slice(1..4).unwrap().to_bool();
49
50        assert_arrays_eq!(
51            sliced_arr,
52            BoolArray::from_iter([Some(true), None, Some(false)])
53        );
54    }
55}