vortex_array/arrays/patched/vtable/
mod.rs1use crate::ArrayEq;
5use crate::ArrayHash;
6mod kernels;
7mod operations;
8mod plugin;
9mod slice;
10
11use std::hash::Hash;
12use std::hash::Hasher;
13
14use vortex_buffer::Buffer;
15use vortex_error::VortexExpect;
16use vortex_error::VortexResult;
17use vortex_error::vortex_panic;
18use vortex_session::VortexSession;
19use vortex_session::registry::CachedId;
20
21use crate::ArrayRef;
22use crate::Canonical;
23use crate::EqMode;
24use crate::ExecutionCtx;
25use crate::ExecutionResult;
26use crate::IntoArray;
27use crate::array::Array;
28use crate::array::ArrayId;
29use crate::array::ArrayParts;
30use crate::array::ArrayView;
31use crate::array::VTable;
32use crate::array::ValidityChild;
33use crate::array::ValidityVTableFromChild;
34use crate::array::with_empty_buffers;
35use crate::arrays::Primitive;
36use crate::arrays::PrimitiveArray;
37use crate::arrays::patched::PatchedArrayExt;
38use crate::arrays::patched::PatchedArraySlotsExt;
39use crate::arrays::patched::PatchedData;
40use crate::arrays::patched::PatchedSlots;
41use crate::arrays::patched::PatchedSlotsView;
42use crate::arrays::primitive::PrimitiveDataParts;
43use crate::buffer::BufferHandle;
44use crate::builders::ArrayBuilder;
45use crate::builders::PrimitiveBuilder;
46use crate::dtype::DType;
47use crate::dtype::NativePType;
48use crate::match_each_native_ptype;
49use crate::require_child;
50
51pub type PatchedArray = Array<Patched>;
53
54pub(crate) fn initialize(session: &VortexSession) {
55 kernels::initialize(session);
56}
57
58#[derive(Clone, Debug)]
59pub struct Patched;
60
61impl ValidityChild<Patched> for Patched {
62 fn validity_child(array: ArrayView<'_, Patched>) -> ArrayRef {
63 array.inner().clone()
64 }
65}
66
67#[derive(Clone, prost::Message)]
82pub struct PatchedMetadata {
83 #[prost(uint32, tag = "1")]
85 pub(crate) n_patches: u32,
86
87 #[prost(uint32, tag = "2")]
89 pub(crate) n_lanes: u32,
90
91 #[prost(uint32, tag = "3")]
95 pub(crate) offset: u32,
96}
97
98impl ArrayHash for PatchedData {
99 fn array_hash<H: Hasher>(&self, state: &mut H, _accuracy: EqMode) {
100 self.offset.hash(state);
101 self.n_lanes.hash(state);
102 }
103}
104
105impl ArrayEq for PatchedData {
106 fn array_eq(&self, other: &Self, _accuracy: EqMode) -> bool {
107 self.offset == other.offset && self.n_lanes == other.n_lanes
108 }
109}
110
111impl VTable for Patched {
112 type TypedArrayData = PatchedData;
113 type OperationsVTable = Self;
114 type ValidityVTable = ValidityVTableFromChild;
115
116 fn id(&self) -> ArrayId {
117 static ID: CachedId = CachedId::new("vortex.patched");
118 *ID
119 }
120
121 fn validate(
122 &self,
123 data: &PatchedData,
124 dtype: &DType,
125 len: usize,
126 slots: &[Option<ArrayRef>],
127 ) -> VortexResult<()> {
128 data.validate(dtype, len, &PatchedSlotsView::from_slots(slots))
129 }
130
131 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
132 0
133 }
134
135 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
136 vortex_panic!("invalid buffer index for PatchedArray: {idx}");
137 }
138
139 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
140 vortex_panic!("invalid buffer index for PatchedArray: {idx}");
141 }
142
143 fn with_buffers(
144 &self,
145 array: ArrayView<'_, Self>,
146 buffers: &[BufferHandle],
147 ) -> VortexResult<ArrayParts<Self>> {
148 with_empty_buffers(self, array, buffers)
149 }
150
151 fn append_to_builder(
152 array: ArrayView<'_, Self>,
153 builder: &mut dyn ArrayBuilder,
154 ctx: &mut ExecutionCtx,
155 ) -> VortexResult<()> {
156 let dtype = array.array().dtype();
157
158 if !dtype.is_primitive() {
159 let canonical = array
161 .array()
162 .clone()
163 .execute::<Canonical>(ctx)?
164 .into_array();
165 return canonical.append_to_builder(builder, ctx);
166 }
167
168 let ptype = dtype.as_ptype();
169
170 let len = array.len();
171
172 array.inner().append_to_builder(builder, ctx)?;
173
174 let offset = array.offset();
175 let lane_offsets = array
176 .lane_offsets()
177 .clone()
178 .execute::<PrimitiveArray>(ctx)?;
179 let indices = array
180 .patch_indices()
181 .clone()
182 .execute::<PrimitiveArray>(ctx)?;
183 let values = array
184 .patch_values()
185 .clone()
186 .execute::<PrimitiveArray>(ctx)?;
187
188 match_each_native_ptype!(ptype, |V| {
189 let typed_builder = builder
190 .as_any_mut()
191 .downcast_mut::<PrimitiveBuilder<V>>()
192 .vortex_expect("correctly typed builder");
193
194 let output = typed_builder.values_mut();
197 let trailer = output.len() - len;
198
199 apply_patches_primitive::<V>(
200 &mut output[trailer..],
201 offset,
202 len,
203 array.n_lanes(),
204 lane_offsets.as_slice::<u32>(),
205 indices.as_slice::<u16>(),
206 values.as_slice::<V>(),
207 );
208 });
209
210 Ok(())
211 }
212
213 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
214 PatchedSlots::NAMES[idx].to_string()
215 }
216
217 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
218 let array = require_child!(array, array.inner(), PatchedSlots::INNER => Primitive);
219 let array =
220 require_child!(array, array.lane_offsets(), PatchedSlots::LANE_OFFSETS => Primitive);
221 let array =
222 require_child!(array, array.patch_indices(), PatchedSlots::PATCH_INDICES => Primitive);
223 let array =
224 require_child!(array, array.patch_values(), PatchedSlots::PATCH_VALUES => Primitive);
225
226 let len = array.len();
227
228 let n_lanes = array.n_lanes;
229 let offset = array.offset;
230 let slots = match array.try_into_parts() {
231 Ok(parts) => PatchedSlots::from_slots(parts.slots),
232 Err(array) => array.slots_view().to_owned(),
233 };
234
235 let PrimitiveDataParts {
237 buffer,
238 ptype,
239 validity,
240 } = slots.inner.downcast::<Primitive>().into_data_parts();
241
242 let values = slots.patch_values.downcast::<Primitive>();
243 let lane_offsets = slots.lane_offsets.downcast::<Primitive>();
244 let patch_indices = slots.patch_indices.downcast::<Primitive>();
245
246 let patched_values = match_each_native_ptype!(values.ptype(), |V| {
247 let mut output = Buffer::<V>::from_byte_buffer(buffer.unwrap_host()).into_mut();
248
249 apply_patches_primitive::<V>(
250 &mut output,
251 offset,
252 len,
253 n_lanes,
254 lane_offsets.as_slice::<u32>(),
255 patch_indices.as_slice::<u16>(),
256 values.as_slice::<V>(),
257 );
258
259 let output = output.freeze();
260
261 PrimitiveArray::from_byte_buffer(output.into_byte_buffer(), ptype, validity)
262 });
263
264 Ok(ExecutionResult::done(patched_values.into_array()))
265 }
266}
267
268fn apply_patches_primitive<V: NativePType>(
270 output: &mut [V],
271 offset: usize,
272 len: usize,
273 n_lanes: usize,
274 lane_offsets: &[u32],
275 indices: &[u16],
276 values: &[V],
277) {
278 let n_chunks = (offset + len).div_ceil(1024);
279 for chunk in 0..n_chunks {
280 let start = lane_offsets[chunk * n_lanes] as usize;
281 let stop = lane_offsets[chunk * n_lanes + n_lanes] as usize;
282
283 for idx in start..stop {
284 let index = chunk * 1024 + indices[idx] as usize;
286 if index < offset || index >= offset + len {
287 continue;
288 }
289
290 let value = values[idx];
291 output[index - offset] = value;
292 }
293 }
294}
295
296#[cfg(test)]
297mod tests;