vortex_array/arrays/varbin/vtable/
mod.rs1use std::sync::Arc;
5
6use vortex_error::VortexExpect;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_error::vortex_err;
10use vortex_error::vortex_panic;
11
12use crate::ArrayRef;
13use crate::DeserializeMetadata;
14use crate::ExecutionCtx;
15use crate::ExecutionResult;
16use crate::IntoArray;
17use crate::ProstMetadata;
18use crate::SerializeMetadata;
19use crate::arrays::VarBinArray;
20use crate::buffer::BufferHandle;
21use crate::dtype::DType;
22use crate::dtype::Nullability;
23use crate::dtype::PType;
24use crate::serde::ArrayChildren;
25use crate::validity::Validity;
26use crate::vtable;
27use crate::vtable::Array;
28use crate::vtable::ArrayId;
29use crate::vtable::VTable;
30use crate::vtable::ValidityVTableFromValidityHelper;
31use crate::vtable::validity_nchildren;
32use crate::vtable::validity_to_child;
33mod canonical;
34mod kernel;
35mod operations;
36mod validity;
37use std::hash::Hash;
38
39use canonical::varbin_to_canonical;
40use kernel::PARENT_KERNELS;
41use vortex_session::VortexSession;
42
43use crate::Precision;
44use crate::arrays::varbin::compute::rules::PARENT_RULES;
45use crate::hash::ArrayEq;
46use crate::hash::ArrayHash;
47use crate::stats::StatsSetRef;
48
49vtable!(VarBin);
50
51#[derive(Clone, prost::Message)]
52pub struct VarBinMetadata {
53 #[prost(enumeration = "PType", tag = "1")]
54 pub(crate) offsets_ptype: i32,
55}
56
57impl VTable for VarBin {
58 type Array = VarBinArray;
59
60 type Metadata = ProstMetadata<VarBinMetadata>;
61 type OperationsVTable = Self;
62 type ValidityVTable = ValidityVTableFromValidityHelper;
63 fn vtable(_array: &Self::Array) -> &Self {
64 &VarBin
65 }
66
67 fn id(&self) -> ArrayId {
68 Self::ID
69 }
70
71 fn len(array: &VarBinArray) -> usize {
72 array.offsets().len().saturating_sub(1)
73 }
74
75 fn dtype(array: &VarBinArray) -> &DType {
76 &array.dtype
77 }
78
79 fn stats(array: &VarBinArray) -> StatsSetRef<'_> {
80 array.stats_set.to_ref(array.as_ref())
81 }
82
83 fn array_hash<H: std::hash::Hasher>(array: &VarBinArray, state: &mut H, precision: Precision) {
84 array.dtype.hash(state);
85 array.bytes().array_hash(state, precision);
86 array.offsets().array_hash(state, precision);
87 array.validity.array_hash(state, precision);
88 }
89
90 fn array_eq(array: &VarBinArray, other: &VarBinArray, precision: Precision) -> bool {
91 array.dtype == other.dtype
92 && array.bytes().array_eq(other.bytes(), precision)
93 && array.offsets().array_eq(other.offsets(), precision)
94 && array.validity.array_eq(&other.validity, precision)
95 }
96
97 fn nbuffers(_array: &VarBinArray) -> usize {
98 1
99 }
100
101 fn buffer(array: &VarBinArray, idx: usize) -> BufferHandle {
102 match idx {
103 0 => array.bytes_handle().clone(),
104 _ => vortex_panic!("VarBinArray buffer index {idx} out of bounds"),
105 }
106 }
107
108 fn buffer_name(_array: &VarBinArray, idx: usize) -> Option<String> {
109 match idx {
110 0 => Some("bytes".to_string()),
111 _ => vortex_panic!("VarBinArray buffer_name index {idx} out of bounds"),
112 }
113 }
114
115 fn nchildren(array: &VarBinArray) -> usize {
116 1 + validity_nchildren(&array.validity)
117 }
118
119 fn child(array: &VarBinArray, idx: usize) -> ArrayRef {
120 match idx {
121 0 => array.offsets().clone(),
122 1 => validity_to_child(&array.validity, array.len())
123 .vortex_expect("VarBinArray validity child out of bounds"),
124 _ => vortex_panic!("VarBinArray child index {idx} out of bounds"),
125 }
126 }
127
128 fn child_name(_array: &VarBinArray, idx: usize) -> String {
129 match idx {
130 0 => "offsets".to_string(),
131 1 => "validity".to_string(),
132 _ => vortex_panic!("VarBinArray child_name index {idx} out of bounds"),
133 }
134 }
135
136 fn metadata(array: &VarBinArray) -> VortexResult<Self::Metadata> {
137 Ok(ProstMetadata(VarBinMetadata {
138 offsets_ptype: PType::try_from(array.offsets().dtype())
139 .vortex_expect("Must be a valid PType") as i32,
140 }))
141 }
142
143 fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
144 Ok(Some(metadata.serialize()))
145 }
146
147 fn deserialize(
148 bytes: &[u8],
149 _dtype: &DType,
150 _len: usize,
151 _buffers: &[BufferHandle],
152 _session: &VortexSession,
153 ) -> VortexResult<Self::Metadata> {
154 Ok(ProstMetadata(ProstMetadata::<VarBinMetadata>::deserialize(
155 bytes,
156 )?))
157 }
158
159 fn build(
160 dtype: &DType,
161 len: usize,
162 metadata: &Self::Metadata,
163 buffers: &[BufferHandle],
164 children: &dyn ArrayChildren,
165 ) -> VortexResult<VarBinArray> {
166 let validity = if children.len() == 1 {
167 Validity::from(dtype.nullability())
168 } else if children.len() == 2 {
169 let validity = children.get(1, &Validity::DTYPE, len)?;
170 Validity::Array(validity)
171 } else {
172 vortex_bail!("Expected 1 or 2 children, got {}", children.len());
173 };
174
175 let offsets = children.get(
176 0,
177 &DType::Primitive(metadata.offsets_ptype(), Nullability::NonNullable),
178 len + 1,
179 )?;
180
181 if buffers.len() != 1 {
182 vortex_bail!("Expected 1 buffer, got {}", buffers.len());
183 }
184 let bytes = buffers[0].clone().try_to_host_sync()?;
185
186 VarBinArray::try_new(offsets, bytes, dtype.clone(), validity)
187 }
188
189 fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
190 match children.len() {
191 1 => {
192 let [offsets]: [ArrayRef; 1] = children
193 .try_into()
194 .map_err(|_| vortex_err!("Failed to convert children to array"))?;
195 array.offsets = offsets;
196 }
197 2 => {
198 let [offsets, validity]: [ArrayRef; 2] = children
199 .try_into()
200 .map_err(|_| vortex_err!("Failed to convert children to array"))?;
201 array.offsets = offsets;
202 array.validity = Validity::Array(validity);
203 }
204 _ => vortex_bail!(
205 "VarBinArray expects 1 or 2 children (offsets, validity?), got {}",
206 children.len()
207 ),
208 }
209 Ok(())
210 }
211
212 fn reduce_parent(
213 array: &Array<Self>,
214 parent: &ArrayRef,
215 child_idx: usize,
216 ) -> VortexResult<Option<ArrayRef>> {
217 PARENT_RULES.evaluate(array, parent, child_idx)
218 }
219
220 fn execute_parent(
221 array: &Array<Self>,
222 parent: &ArrayRef,
223 child_idx: usize,
224 ctx: &mut ExecutionCtx,
225 ) -> VortexResult<Option<ArrayRef>> {
226 PARENT_KERNELS.execute(array, parent, child_idx, ctx)
227 }
228
229 fn execute(array: Arc<Array<Self>>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
230 Ok(ExecutionResult::done(
231 varbin_to_canonical(&array, ctx)?.into_array(),
232 ))
233 }
234}
235
236#[derive(Clone, Debug)]
237pub struct VarBin;
238
239impl VarBin {
240 pub const ID: ArrayId = ArrayId::new_ref("vortex.varbin");
241}