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_error::VortexExpect;
6use vortex_error::VortexResult;
7use vortex_error::vortex_bail;
8use vortex_error::vortex_ensure;
9use vortex_error::vortex_panic;
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::dtype::DType;
20use crate::serde::ArrayChildren;
21use crate::validity::Validity;
22use crate::vtable;
23use crate::vtable::VTable;
24use crate::vtable::ValidityVTableFromValidityHelper;
25use crate::vtable::validity_nchildren;
26use crate::vtable::validity_to_child;
27mod canonical;
28mod kernel;
29mod operations;
30mod validity;
31
32use std::hash::Hash;
33
34use crate::Precision;
35use crate::arrays::bool::compute::rules::RULES;
36use crate::hash::ArrayEq;
37use crate::hash::ArrayHash;
38use crate::stats::StatsSetRef;
39use crate::vtable::ArrayId;
40
41vtable!(Bool);
42
43#[derive(prost::Message)]
44pub struct BoolMetadata {
45    // The offset in bits must be <8
46    #[prost(uint32, tag = "1")]
47    pub offset: u32,
48}
49
50impl VTable for BoolVTable {
51    type Array = BoolArray;
52
53    type Metadata = ProstMetadata<BoolMetadata>;
54    type OperationsVTable = Self;
55    type ValidityVTable = ValidityVTableFromValidityHelper;
56
57    fn id(_array: &Self::Array) -> ArrayId {
58        Self::ID
59    }
60
61    fn len(array: &BoolArray) -> usize {
62        array.len
63    }
64
65    fn dtype(array: &BoolArray) -> &DType {
66        &array.dtype
67    }
68
69    fn stats(array: &BoolArray) -> StatsSetRef<'_> {
70        array.stats_set.to_ref(array.as_ref())
71    }
72
73    fn array_hash<H: std::hash::Hasher>(array: &BoolArray, state: &mut H, precision: Precision) {
74        array.dtype.hash(state);
75        array.to_bit_buffer().array_hash(state, precision);
76        array.validity.array_hash(state, precision);
77    }
78
79    fn array_eq(array: &BoolArray, other: &BoolArray, precision: Precision) -> bool {
80        if array.dtype != other.dtype {
81            return false;
82        }
83        array
84            .to_bit_buffer()
85            .array_eq(&other.to_bit_buffer(), precision)
86            && array.validity.array_eq(&other.validity, precision)
87    }
88
89    fn nbuffers(_array: &BoolArray) -> usize {
90        1
91    }
92
93    fn buffer(array: &BoolArray, idx: usize) -> BufferHandle {
94        match idx {
95            0 => array.bits.clone(),
96            _ => vortex_panic!("BoolArray buffer index {idx} out of bounds"),
97        }
98    }
99
100    fn buffer_name(_array: &BoolArray, idx: usize) -> Option<String> {
101        match idx {
102            0 => Some("bits".to_string()),
103            _ => None,
104        }
105    }
106
107    fn nchildren(array: &BoolArray) -> usize {
108        validity_nchildren(&array.validity)
109    }
110
111    fn child(array: &BoolArray, idx: usize) -> ArrayRef {
112        match idx {
113            0 => validity_to_child(&array.validity, array.len())
114                .vortex_expect("BoolArray child index out of bounds"),
115            _ => vortex_panic!("BoolArray child index {idx} out of bounds"),
116        }
117    }
118
119    fn child_name(_array: &BoolArray, _idx: usize) -> String {
120        "validity".to_string()
121    }
122
123    fn metadata(array: &BoolArray) -> VortexResult<Self::Metadata> {
124        assert!(array.offset < 8, "Offset must be <8, got {}", array.offset);
125        Ok(ProstMetadata(BoolMetadata {
126            offset: u32::try_from(array.offset).vortex_expect("checked"),
127        }))
128    }
129
130    fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
131        Ok(Some(metadata.serialize()))
132    }
133
134    fn deserialize(
135        bytes: &[u8],
136        _dtype: &DType,
137        _len: usize,
138        _buffers: &[BufferHandle],
139        _session: &VortexSession,
140    ) -> VortexResult<Self::Metadata> {
141        let metadata = <Self::Metadata as DeserializeMetadata>::deserialize(bytes)?;
142        Ok(ProstMetadata(metadata))
143    }
144
145    fn build(
146        dtype: &DType,
147        len: usize,
148        metadata: &Self::Metadata,
149        buffers: &[BufferHandle],
150        children: &dyn ArrayChildren,
151    ) -> VortexResult<BoolArray> {
152        if buffers.len() != 1 {
153            vortex_bail!("Expected 1 buffer, got {}", buffers.len());
154        }
155
156        let validity = if children.is_empty() {
157            Validity::from(dtype.nullability())
158        } else if children.len() == 1 {
159            let validity = children.get(0, &Validity::DTYPE, len)?;
160            Validity::Array(validity)
161        } else {
162            vortex_bail!("Expected 0 or 1 child, got {}", children.len());
163        };
164
165        let buffer = buffers[0].clone();
166
167        BoolArray::try_new_from_handle(buffer, metadata.offset as usize, len, validity)
168    }
169
170    fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
171        vortex_ensure!(
172            children.len() <= 1,
173            "BoolArray can have at most 1 child (validity), got {}",
174            children.len()
175        );
176
177        array.validity = if children.is_empty() {
178            Validity::from(array.dtype().nullability())
179        } else {
180            Validity::Array(children.into_iter().next().vortex_expect("checked"))
181        };
182
183        Ok(())
184    }
185
186    fn execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
187        Ok(array.to_array())
188    }
189
190    fn reduce_parent(
191        array: &Self::Array,
192        parent: &ArrayRef,
193        child_idx: usize,
194    ) -> VortexResult<Option<ArrayRef>> {
195        RULES.evaluate(array, parent, child_idx)
196    }
197
198    fn execute_parent(
199        array: &Self::Array,
200        parent: &ArrayRef,
201        child_idx: usize,
202        ctx: &mut ExecutionCtx,
203    ) -> VortexResult<Option<ArrayRef>> {
204        PARENT_KERNELS.execute(array, parent, child_idx, ctx)
205    }
206}
207
208#[derive(Debug)]
209pub struct BoolVTable;
210
211impl BoolVTable {
212    pub const ID: ArrayId = ArrayId::new_ref("vortex.bool");
213}