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