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