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::array::ProbeState;
10use crate::arrays::Bool;
11use crate::arrays::bool::BoolArrayExt;
12use crate::scalar::Scalar;
13
14impl OperationsVTable<Bool> for Bool {
15    type ProbeState = ();
16
17    fn probe_scalar(
18        state: &mut ProbeState<'_, Bool>,
19        index: usize,
20        ctx: &mut ExecutionCtx,
21    ) -> VortexResult<Scalar> {
22        let array = state.array();
23        if !state.is_valid(index, ctx)? {
24            return Ok(Scalar::null(array.dtype().clone()));
25        }
26        Ok(Scalar::bool(
27            array.bit_buffer_view().value(index),
28            array.dtype().nullability(),
29        ))
30    }
31
32    fn scalar_at(
33        array: ArrayView<'_, Bool>,
34        index: usize,
35        ctx: &mut ExecutionCtx,
36    ) -> VortexResult<Scalar> {
37        Self::probe_scalar(&mut ProbeState::once(array), index, ctx)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use std::iter;
44    use std::sync::LazyLock;
45
46    use vortex_session::VortexSession;
47
48    use crate::IntoArray;
49    use crate::VortexSessionExecute;
50    use crate::arrays::BoolArray;
51    use crate::arrays::bool::BoolArrayExt;
52    use crate::assert_arrays_eq;
53
54    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
55
56    #[test]
57    fn test_slice_hundred_elements() {
58        let mut ctx = SESSION.create_execution_ctx();
59        let arr = BoolArray::from_iter(iter::repeat_n(Some(true), 100));
60        let sliced_arr = arr
61            .into_array()
62            .slice(8..16)
63            .unwrap()
64            .execute::<BoolArray>(&mut ctx)
65            .unwrap();
66        assert_eq!(sliced_arr.len(), 8);
67        assert_eq!(sliced_arr.to_bit_buffer().len(), 8);
68        assert_eq!(sliced_arr.to_bit_buffer().offset(), 0);
69        assert_arrays_eq!(sliced_arr, BoolArray::from_iter([Some(true); 8]), &mut ctx);
70    }
71
72    #[test]
73    fn test_slice() {
74        let mut ctx = SESSION.create_execution_ctx();
75        let arr = BoolArray::from_iter([Some(true), Some(true), None, Some(false), None]);
76        let sliced_arr = arr.into_array().slice(1..4).unwrap();
77
78        assert_arrays_eq!(
79            sliced_arr,
80            BoolArray::from_iter([Some(true), None, Some(false)]),
81            &mut ctx
82        );
83    }
84}