Skip to main content

vortex_array/arrays/bool/vtable/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use kernel::PARENT_KERNELS;
5use vortex_dtype::DType;
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_error::vortex_ensure;
10use vortex_session::VortexSession;
11
12use crate::ArrayRef;
13use crate::DeserializeMetadata;
14use crate::ExecutionCtx;
15use crate::ProstMetadata;
16use crate::SerializeMetadata;
17use crate::arrays::BoolArray;
18use crate::buffer::BufferHandle;
19use crate::serde::ArrayChildren;
20use crate::validity::Validity;
21use crate::vtable;
22use crate::vtable::VTable;
23use crate::vtable::ValidityVTableFromValidityHelper;
24
25mod array;
26mod canonical;
27mod kernel;
28mod operations;
29mod validity;
30mod visitor;
31
32use crate::arrays::bool::compute::rules::RULES;
33use crate::vtable::ArrayId;
34
35vtable!(Bool);
36
37#[derive(prost::Message)]
38pub struct BoolMetadata {
39    // The offset in bits must be <8
40    #[prost(uint32, tag = "1")]
41    pub offset: u32,
42}
43
44impl VTable for BoolVTable {
45    type Array = BoolArray;
46
47    type Metadata = ProstMetadata<BoolMetadata>;
48
49    type ArrayVTable = Self;
50    type OperationsVTable = Self;
51    type ValidityVTable = ValidityVTableFromValidityHelper;
52    type VisitorVTable = Self;
53
54    fn id(_array: &Self::Array) -> ArrayId {
55        Self::ID
56    }
57
58    fn metadata(array: &BoolArray) -> VortexResult<Self::Metadata> {
59        assert!(array.offset < 8, "Offset must be <8, got {}", array.offset);
60        Ok(ProstMetadata(BoolMetadata {
61            offset: u32::try_from(array.offset).vortex_expect("checked"),
62        }))
63    }
64
65    fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
66        Ok(Some(metadata.serialize()))
67    }
68
69    fn deserialize(
70        bytes: &[u8],
71        _dtype: &DType,
72        _len: usize,
73        _buffers: &[BufferHandle],
74        _session: &VortexSession,
75    ) -> VortexResult<Self::Metadata> {
76        let metadata = <Self::Metadata as DeserializeMetadata>::deserialize(bytes)?;
77        Ok(ProstMetadata(metadata))
78    }
79
80    fn build(
81        dtype: &DType,
82        len: usize,
83        metadata: &Self::Metadata,
84        buffers: &[BufferHandle],
85        children: &dyn ArrayChildren,
86    ) -> VortexResult<BoolArray> {
87        if buffers.len() != 1 {
88            vortex_bail!("Expected 1 buffer, got {}", buffers.len());
89        }
90
91        let validity = if children.is_empty() {
92            Validity::from(dtype.nullability())
93        } else if children.len() == 1 {
94            let validity = children.get(0, &Validity::DTYPE, len)?;
95            Validity::Array(validity)
96        } else {
97            vortex_bail!("Expected 0 or 1 child, got {}", children.len());
98        };
99
100        let buffer = buffers[0].clone();
101
102        BoolArray::try_new_from_handle(buffer, metadata.offset as usize, len, validity)
103    }
104
105    fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
106        vortex_ensure!(
107            children.len() <= 1,
108            "BoolArray can have at most 1 child (validity), got {}",
109            children.len()
110        );
111
112        array.validity = if children.is_empty() {
113            Validity::from(array.dtype().nullability())
114        } else {
115            Validity::Array(children.into_iter().next().vortex_expect("checked"))
116        };
117
118        Ok(())
119    }
120
121    fn execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
122        Ok(array.to_array())
123    }
124
125    fn reduce_parent(
126        array: &Self::Array,
127        parent: &ArrayRef,
128        child_idx: usize,
129    ) -> VortexResult<Option<ArrayRef>> {
130        RULES.evaluate(array, parent, child_idx)
131    }
132
133    fn execute_parent(
134        array: &Self::Array,
135        parent: &ArrayRef,
136        child_idx: usize,
137        ctx: &mut ExecutionCtx,
138    ) -> VortexResult<Option<ArrayRef>> {
139        PARENT_KERNELS.execute(array, parent, child_idx, ctx)
140    }
141}
142
143#[derive(Debug)]
144pub struct BoolVTable;
145
146impl BoolVTable {
147    pub const ID: ArrayId = ArrayId::new_ref("vortex.bool");
148}