vortex_array/arrays/varbin/vtable/
mod.rs1use std::hash::Hasher;
5
6use prost::Message;
7use vortex_error::VortexExpect;
8use vortex_error::VortexResult;
9use vortex_error::vortex_bail;
10use vortex_error::vortex_ensure;
11use vortex_error::vortex_panic;
12
13use crate::ArrayRef;
14use crate::ExecutionCtx;
15use crate::ExecutionResult;
16use crate::IntoArray;
17use crate::array::Array;
18use crate::array::ArrayId;
19use crate::array::ArrayView;
20use crate::array::VTable;
21use crate::arrays::varbin::VarBinArrayExt;
22use crate::arrays::varbin::VarBinData;
23use crate::arrays::varbin::array::NUM_SLOTS;
24use crate::arrays::varbin::array::SLOT_NAMES;
25use crate::buffer::BufferHandle;
26use crate::dtype::DType;
27use crate::dtype::Nullability;
28use crate::dtype::PType;
29use crate::serde::ArrayChildren;
30use crate::validity::Validity;
31mod canonical;
32mod kernel;
33mod operations;
34mod validity;
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;
44
45pub type VarBinArray = Array<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 ArrayHash for VarBinData {
55 fn array_hash<H: Hasher>(&self, state: &mut H, precision: Precision) {
56 self.bytes().array_hash(state, precision);
57 }
58}
59
60impl ArrayEq for VarBinData {
61 fn array_eq(&self, other: &Self, precision: Precision) -> bool {
62 self.bytes().array_eq(other.bytes(), precision)
63 }
64}
65
66impl VTable for VarBin {
67 type ArrayData = VarBinData;
68
69 type OperationsVTable = Self;
70 type ValidityVTable = Self;
71
72 fn id(&self) -> ArrayId {
73 Self::ID
74 }
75
76 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
77 1
78 }
79
80 fn validate(
81 &self,
82 _data: &VarBinData,
83 dtype: &DType,
84 len: usize,
85 slots: &[Option<ArrayRef>],
86 ) -> VortexResult<()> {
87 vortex_ensure!(
88 slots.len() == NUM_SLOTS,
89 "VarBinArray expected {NUM_SLOTS} slots, found {}",
90 slots.len()
91 );
92 let offsets = slots[crate::arrays::varbin::array::OFFSETS_SLOT]
93 .as_ref()
94 .vortex_expect("VarBinArray offsets slot");
95 vortex_ensure!(
96 offsets.len().saturating_sub(1) == len,
97 "VarBinArray length {} does not match outer length {}",
98 offsets.len().saturating_sub(1),
99 len
100 );
101 vortex_ensure!(
102 matches!(dtype, DType::Binary(_) | DType::Utf8(_)),
103 "VarBinArray dtype must be binary or utf8, got {dtype}"
104 );
105 Ok(())
106 }
107
108 fn buffer(array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
109 match idx {
110 0 => array.bytes_handle().clone(),
111 _ => vortex_panic!("VarBinArray buffer index {idx} out of bounds"),
112 }
113 }
114
115 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
116 match idx {
117 0 => Some("bytes".to_string()),
118 _ => vortex_panic!("VarBinArray buffer_name index {idx} out of bounds"),
119 }
120 }
121
122 fn serialize(
123 array: ArrayView<'_, Self>,
124 _session: &VortexSession,
125 ) -> VortexResult<Option<Vec<u8>>> {
126 Ok(Some(
127 VarBinMetadata {
128 offsets_ptype: PType::try_from(array.offsets().dtype())
129 .vortex_expect("Must be a valid PType") as i32,
130 }
131 .encode_to_vec(),
132 ))
133 }
134
135 fn deserialize(
136 &self,
137 dtype: &DType,
138 len: usize,
139 metadata: &[u8],
140
141 buffers: &[BufferHandle],
142 children: &dyn ArrayChildren,
143 _session: &VortexSession,
144 ) -> VortexResult<crate::array::ArrayParts<Self>> {
145 let metadata = VarBinMetadata::decode(metadata)?;
146 let validity = if children.len() == 1 {
147 Validity::from(dtype.nullability())
148 } else if children.len() == 2 {
149 let validity = children.get(1, &Validity::DTYPE, len)?;
150 Validity::Array(validity)
151 } else {
152 vortex_bail!("Expected 1 or 2 children, got {}", children.len());
153 };
154
155 let offsets = children.get(
156 0,
157 &DType::Primitive(metadata.offsets_ptype(), Nullability::NonNullable),
158 len + 1,
159 )?;
160
161 if buffers.len() != 1 {
162 vortex_bail!("Expected 1 buffer, got {}", buffers.len());
163 }
164 let bytes = buffers[0].clone().try_to_host_sync()?;
165
166 let data = VarBinData::try_build(offsets.clone(), bytes, dtype.clone(), validity.clone())?;
167 let slots = VarBinData::make_slots(offsets, &validity, len);
168 Ok(crate::array::ArrayParts::new(self.clone(), dtype.clone(), len, data).with_slots(slots))
169 }
170
171 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
172 SLOT_NAMES[idx].to_string()
173 }
174
175 fn reduce_parent(
176 array: ArrayView<'_, Self>,
177 parent: &ArrayRef,
178 child_idx: usize,
179 ) -> VortexResult<Option<ArrayRef>> {
180 PARENT_RULES.evaluate(array, parent, child_idx)
181 }
182
183 fn execute_parent(
184 array: ArrayView<'_, Self>,
185 parent: &ArrayRef,
186 child_idx: usize,
187 ctx: &mut ExecutionCtx,
188 ) -> VortexResult<Option<ArrayRef>> {
189 PARENT_KERNELS.execute(array, parent, child_idx, ctx)
190 }
191
192 fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
193 Ok(ExecutionResult::done(
194 varbin_to_canonical(array.as_view(), ctx)?.into_array(),
195 ))
196 }
197}
198
199#[derive(Clone, Debug)]
200pub struct VarBin;
201
202impl VarBin {
203 pub const ID: ArrayId = ArrayId::new_ref("vortex.varbin");
204}