vortex_array/arrays/bool/vtable/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_buffer::BufferHandle;
5use vortex_dtype::DType;
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_vector::Vector;
10use vortex_vector::bool::BoolVector;
11
12use crate::DeserializeMetadata;
13use crate::ProstMetadata;
14use crate::SerializeMetadata;
15use crate::arrays::BoolArray;
16use crate::execution::ExecutionCtx;
17use crate::serde::ArrayChildren;
18use crate::validity::Validity;
19use crate::vtable;
20use crate::vtable::ArrayVTableExt;
21use crate::vtable::NotSupported;
22use crate::vtable::VTable;
23use crate::vtable::ValidityVTableFromValidityHelper;
24
25mod array;
26mod canonical;
27mod operations;
28pub mod operator;
29mod validity;
30mod visitor;
31
32pub use operator::BoolMaskedValidityRule;
33
34use crate::vtable::ArrayId;
35use crate::vtable::ArrayVTable;
36
37vtable!(Bool);
38
39#[derive(prost::Message)]
40pub struct BoolMetadata {
41    // The offset in bits must be <8
42    #[prost(uint32, tag = "1")]
43    pub offset: u32,
44}
45
46impl VTable for BoolVTable {
47    type Array = BoolArray;
48
49    type Metadata = ProstMetadata<BoolMetadata>;
50
51    type ArrayVTable = Self;
52    type CanonicalVTable = Self;
53    type OperationsVTable = Self;
54    type ValidityVTable = ValidityVTableFromValidityHelper;
55    type VisitorVTable = Self;
56    type ComputeVTable = NotSupported;
57    type EncodeVTable = NotSupported;
58
59    fn id(&self) -> ArrayId {
60        ArrayId::new_ref("vortex.bool")
61    }
62
63    fn encoding(_array: &Self::Array) -> ArrayVTable {
64        BoolVTable.as_vtable()
65    }
66
67    fn metadata(array: &BoolArray) -> VortexResult<Self::Metadata> {
68        let bit_offset = array.bit_buffer().offset();
69        assert!(bit_offset < 8, "Offset must be <8, got {bit_offset}");
70        Ok(ProstMetadata(BoolMetadata {
71            offset: u32::try_from(bit_offset).vortex_expect("checked"),
72        }))
73    }
74
75    fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
76        Ok(Some(metadata.serialize()))
77    }
78
79    fn deserialize(bytes: &[u8]) -> VortexResult<Self::Metadata> {
80        let metadata = <Self::Metadata as DeserializeMetadata>::deserialize(bytes)?;
81        Ok(ProstMetadata(metadata))
82    }
83
84    fn build(
85        &self,
86        dtype: &DType,
87        len: usize,
88        metadata: &Self::Metadata,
89        buffers: &[BufferHandle],
90        children: &dyn ArrayChildren,
91    ) -> VortexResult<BoolArray> {
92        if buffers.len() != 1 {
93            vortex_bail!("Expected 1 buffer, got {}", buffers.len());
94        }
95
96        let validity = if children.is_empty() {
97            Validity::from(dtype.nullability())
98        } else if children.len() == 1 {
99            let validity = children.get(0, &Validity::DTYPE, len)?;
100            Validity::Array(validity)
101        } else {
102            vortex_bail!("Expected 0 or 1 child, got {}", children.len());
103        };
104
105        let buffer = buffers[0].clone().try_to_bytes()?;
106        BoolArray::try_new(buffer, metadata.offset as usize, len, validity)
107    }
108
109    fn batch_execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<Vector> {
110        Ok(BoolVector::new(array.bit_buffer().clone(), array.validity_mask()).into())
111    }
112}
113
114#[derive(Debug)]
115pub struct BoolVTable;