1use prost::Message;
5
6use crate::ArrayEq;
7use crate::ArrayHash;
8mod kernels;
9mod operations;
10mod slice;
11
12use std::hash::Hash;
13use std::hash::Hasher;
14
15use vortex_buffer::Buffer;
16use vortex_error::VortexExpect;
17use vortex_error::VortexResult;
18use vortex_error::vortex_panic;
19use vortex_session::VortexSession;
20use vortex_session::registry::CachedId;
21
22use crate::ArrayRef;
23use crate::Canonical;
24use crate::EqMode;
25use crate::ExecutionCtx;
26use crate::ExecutionResult;
27use crate::IntoArray;
28use crate::array::Array;
29use crate::array::ArrayId;
30use crate::array::ArrayParts;
31use crate::array::ArrayView;
32use crate::array::VTable;
33use crate::array::ValidityChild;
34use crate::array::ValidityVTableFromChild;
35use crate::array::with_empty_buffers;
36use crate::arrays::Primitive;
37use crate::arrays::PrimitiveArray;
38use crate::arrays::patched::PatchedArrayExt;
39use crate::arrays::patched::PatchedArraySlotsExt;
40use crate::arrays::patched::PatchedData;
41use crate::arrays::patched::PatchedSlots;
42use crate::arrays::patched::PatchedSlotsView;
43use crate::arrays::patched::compute::rules::PARENT_RULES;
44use crate::arrays::primitive::PrimitiveDataParts;
45use crate::buffer::BufferHandle;
46use crate::builders::ArrayBuilder;
47use crate::builders::PrimitiveBuilder;
48use crate::dtype::DType;
49use crate::dtype::NativePType;
50use crate::dtype::PType;
51use crate::match_each_native_ptype;
52use crate::require_child;
53use crate::serde::ArrayChildren;
54
55pub type PatchedArray = Array<Patched>;
57
58pub(crate) fn initialize(session: &VortexSession) {
59 kernels::initialize(session);
60}
61
62#[derive(Clone, Debug)]
63pub struct Patched;
64
65impl ValidityChild<Patched> for Patched {
66 fn validity_child(array: ArrayView<'_, Patched>) -> ArrayRef {
67 array.inner().clone()
68 }
69}
70
71#[derive(Clone, prost::Message)]
72pub struct PatchedMetadata {
73 #[prost(uint32, tag = "1")]
75 pub(crate) n_patches: u32,
76
77 #[prost(uint32, tag = "2")]
79 pub(crate) n_lanes: u32,
80
81 #[prost(uint32, tag = "3")]
85 pub(crate) offset: u32,
86}
87
88impl ArrayHash for PatchedData {
89 fn array_hash<H: Hasher>(&self, state: &mut H, _accuracy: EqMode) {
90 self.offset.hash(state);
91 self.n_lanes.hash(state);
92 }
93}
94
95impl ArrayEq for PatchedData {
96 fn array_eq(&self, other: &Self, _accuracy: EqMode) -> bool {
97 self.offset == other.offset && self.n_lanes == other.n_lanes
98 }
99}
100
101impl VTable for Patched {
102 type TypedArrayData = PatchedData;
103 type OperationsVTable = Self;
104 type ValidityVTable = ValidityVTableFromChild;
105
106 fn id(&self) -> ArrayId {
107 static ID: CachedId = CachedId::new("vortex.patched");
108 *ID
109 }
110
111 fn validate(
112 &self,
113 data: &PatchedData,
114 dtype: &DType,
115 len: usize,
116 slots: &[Option<ArrayRef>],
117 ) -> VortexResult<()> {
118 data.validate(dtype, len, &PatchedSlotsView::from_slots(slots))
119 }
120
121 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
122 0
123 }
124
125 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
126 vortex_panic!("invalid buffer index for PatchedArray: {idx}");
127 }
128
129 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
130 vortex_panic!("invalid buffer index for PatchedArray: {idx}");
131 }
132
133 fn with_buffers(
134 &self,
135 array: ArrayView<'_, Self>,
136 buffers: &[BufferHandle],
137 ) -> VortexResult<ArrayParts<Self>> {
138 with_empty_buffers(self, array, buffers)
139 }
140
141 fn serialize(
142 array: ArrayView<'_, Self>,
143 _session: &VortexSession,
144 ) -> VortexResult<Option<Vec<u8>>> {
145 Ok(Some(
146 PatchedMetadata {
147 n_patches: u32::try_from(array.patch_indices().len())?,
148 n_lanes: u32::try_from(array.n_lanes())?,
149 offset: u32::try_from(array.offset())?,
150 }
151 .encode_to_vec(),
152 ))
153 }
154
155 fn deserialize(
156 &self,
157 dtype: &DType,
158 len: usize,
159 metadata: &[u8],
160 _buffers: &[BufferHandle],
161 children: &dyn ArrayChildren,
162 _session: &VortexSession,
163 ) -> VortexResult<ArrayParts<Self>> {
164 let metadata = PatchedMetadata::decode(metadata)?;
165 let n_patches = metadata.n_patches as usize;
166 let n_lanes = metadata.n_lanes as usize;
167 let offset = metadata.offset as usize;
168
169 let n_chunks = (len + offset).div_ceil(1024);
172
173 let inner = children.get(0, dtype, len)?;
174 let lane_offsets = children.get(1, PType::U32.into(), n_chunks * n_lanes + 1)?;
175 let indices = children.get(2, PType::U16.into(), n_patches)?;
176 let values = children.get(3, dtype, n_patches)?;
177
178 let data = PatchedData { n_lanes, offset };
179 let slots = PatchedSlots {
180 inner,
181 lane_offsets,
182 patch_indices: indices,
183 patch_values: values,
184 }
185 .into_slots();
186 Ok(ArrayParts::new(self.clone(), dtype.clone(), len, data).with_slots(slots))
187 }
188
189 fn append_to_builder(
190 array: ArrayView<'_, Self>,
191 builder: &mut dyn ArrayBuilder,
192 ctx: &mut ExecutionCtx,
193 ) -> VortexResult<()> {
194 let dtype = array.array().dtype();
195
196 if !dtype.is_primitive() {
197 let canonical = array
199 .array()
200 .clone()
201 .execute::<Canonical>(ctx)?
202 .into_array();
203 return canonical.append_to_builder(builder, ctx);
204 }
205
206 let ptype = dtype.as_ptype();
207
208 let len = array.len();
209
210 array.inner().append_to_builder(builder, ctx)?;
211
212 let offset = array.offset();
213 let lane_offsets = array
214 .lane_offsets()
215 .clone()
216 .execute::<PrimitiveArray>(ctx)?;
217 let indices = array
218 .patch_indices()
219 .clone()
220 .execute::<PrimitiveArray>(ctx)?;
221 let values = array
222 .patch_values()
223 .clone()
224 .execute::<PrimitiveArray>(ctx)?;
225
226 match_each_native_ptype!(ptype, |V| {
227 let typed_builder = builder
228 .as_any_mut()
229 .downcast_mut::<PrimitiveBuilder<V>>()
230 .vortex_expect("correctly typed builder");
231
232 let output = typed_builder.values_mut();
235 let trailer = output.len() - len;
236
237 apply_patches_primitive::<V>(
238 &mut output[trailer..],
239 offset,
240 len,
241 array.n_lanes(),
242 lane_offsets.as_slice::<u32>(),
243 indices.as_slice::<u16>(),
244 values.as_slice::<V>(),
245 );
246 });
247
248 Ok(())
249 }
250
251 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
252 PatchedSlots::NAMES[idx].to_string()
253 }
254
255 fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
256 let array = require_child!(array, array.inner(), PatchedSlots::INNER => Primitive);
257 let array =
258 require_child!(array, array.lane_offsets(), PatchedSlots::LANE_OFFSETS => Primitive);
259 let array =
260 require_child!(array, array.patch_indices(), PatchedSlots::PATCH_INDICES => Primitive);
261 let array =
262 require_child!(array, array.patch_values(), PatchedSlots::PATCH_VALUES => Primitive);
263
264 let len = array.len();
265
266 let n_lanes = array.n_lanes;
267 let offset = array.offset;
268 let slots = match array.try_into_parts() {
269 Ok(parts) => PatchedSlots::from_slots(parts.slots),
270 Err(array) => PatchedSlotsView::from_slots(array.slots()).to_owned(),
271 };
272
273 let PrimitiveDataParts {
275 buffer,
276 ptype,
277 validity,
278 } = slots.inner.downcast::<Primitive>().into_data_parts();
279
280 let values = slots.patch_values.downcast::<Primitive>();
281 let lane_offsets = slots.lane_offsets.downcast::<Primitive>();
282 let patch_indices = slots.patch_indices.downcast::<Primitive>();
283
284 let patched_values = match_each_native_ptype!(values.ptype(), |V| {
285 let mut output = Buffer::<V>::from_byte_buffer(buffer.unwrap_host()).into_mut();
286
287 apply_patches_primitive::<V>(
288 &mut output,
289 offset,
290 len,
291 n_lanes,
292 lane_offsets.as_slice::<u32>(),
293 patch_indices.as_slice::<u16>(),
294 values.as_slice::<V>(),
295 );
296
297 let output = output.freeze();
298
299 PrimitiveArray::from_byte_buffer(output.into_byte_buffer(), ptype, validity)
300 });
301
302 Ok(ExecutionResult::done(patched_values.into_array()))
303 }
304
305 fn reduce_parent(
306 array: ArrayView<'_, Self>,
307 parent: &ArrayRef,
308 child_idx: usize,
309 ) -> VortexResult<Option<ArrayRef>> {
310 PARENT_RULES.evaluate(array, parent, child_idx)
311 }
312}
313
314fn apply_patches_primitive<V: NativePType>(
316 output: &mut [V],
317 offset: usize,
318 len: usize,
319 n_lanes: usize,
320 lane_offsets: &[u32],
321 indices: &[u16],
322 values: &[V],
323) {
324 let n_chunks = (offset + len).div_ceil(1024);
325 for chunk in 0..n_chunks {
326 let start = lane_offsets[chunk * n_lanes] as usize;
327 let stop = lane_offsets[chunk * n_lanes + n_lanes] as usize;
328
329 for idx in start..stop {
330 let index = chunk * 1024 + indices[idx] as usize;
332 if index < offset || index >= offset + len {
333 continue;
334 }
335
336 let value = values[idx];
337 output[index - offset] = value;
338 }
339 }
340}
341
342#[cfg(test)]
343mod tests {
344 use rstest::rstest;
345 use vortex_buffer::ByteBufferMut;
346 use vortex_buffer::buffer;
347 use vortex_buffer::buffer_mut;
348 use vortex_error::VortexResult;
349 use vortex_session::registry::ReadContext;
350
351 use crate::Array;
352 use crate::ArrayContext;
353 use crate::ArrayParts;
354 use crate::ArraySlots;
355 use crate::Canonical;
356 use crate::IntoArray;
357 use crate::VortexSessionExecute;
358 use crate::array_session;
359 use crate::arrays::Patched;
360 use crate::arrays::PatchedArray;
361 use crate::arrays::PrimitiveArray;
362 use crate::arrays::patched::PatchedArrayExt;
363 use crate::arrays::patched::PatchedArraySlotsExt;
364 use crate::arrays::patched::PatchedData;
365 use crate::arrays::patched::PatchedSlots;
366 use crate::arrays::patched::PatchedSlotsView;
367 use crate::assert_arrays_eq;
368 use crate::builders::builder_with_capacity_in;
369 use crate::patches::Patches;
370 use crate::serde::SerializeOptions;
371 use crate::serde::SerializedArray;
372 use crate::session::ArraySessionExt;
373 use crate::validity::Validity;
374
375 #[test]
376 fn test_execute() {
377 let values = buffer![0u16; 1024].into_array();
378 let patches = Patches::new(
379 1024,
380 0,
381 buffer![1u32, 2, 3].into_array(),
382 buffer![1u16; 3].into_array(),
383 None,
384 )
385 .unwrap();
386
387 let session = array_session();
388 let mut ctx = session.create_execution_ctx();
389
390 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
391 .unwrap()
392 .into_array();
393
394 let executed = array
395 .execute::<Canonical>(&mut ctx)
396 .unwrap()
397 .into_primitive()
398 .into_buffer::<u16>();
399
400 let mut expected = buffer_mut![0u16; 1024];
401 expected[1] = 1;
402 expected[2] = 1;
403 expected[3] = 1;
404
405 assert_eq!(executed, expected.freeze());
406 }
407
408 #[test]
409 fn test_execute_sliced() {
410 let values = buffer![0u16; 1024].into_array();
411 let patches = Patches::new(
412 1024,
413 0,
414 buffer![1u32, 2, 3].into_array(),
415 buffer![1u16; 3].into_array(),
416 None,
417 )
418 .unwrap();
419
420 let session = array_session();
421 let mut ctx = session.create_execution_ctx();
422
423 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
424 .unwrap()
425 .into_array()
426 .slice(3..1024)
427 .unwrap();
428
429 let executed = array
430 .execute::<Canonical>(&mut ctx)
431 .unwrap()
432 .into_primitive()
433 .into_buffer::<u16>();
434
435 let mut expected = buffer_mut![0u16; 1021];
436 expected[0] = 1;
437
438 assert_eq!(executed, expected.freeze());
439 }
440
441 #[test]
442 fn test_append_to_builder_non_nullable() {
443 let values = PrimitiveArray::new(buffer![0u16; 1024], Validity::NonNullable).into_array();
444 let patches = Patches::new(
445 1024,
446 0,
447 buffer![1u32, 2, 3].into_array(),
448 buffer![10u16, 20, 30].into_array(),
449 None,
450 )
451 .unwrap();
452
453 let session = array_session();
454 let mut ctx = session.create_execution_ctx();
455
456 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
457 .unwrap()
458 .into_array();
459
460 let mut builder = builder_with_capacity_in(
461 array.dtype(),
462 array.len(),
463 vortex_buffer::BufferAllocatorRef::static_ref(),
464 );
465 array.append_to_builder(builder.as_mut(), &mut ctx).unwrap();
466
467 let result = builder.finish();
468
469 let mut expected = buffer_mut![0u16; 1024];
470 expected[1] = 10;
471 expected[2] = 20;
472 expected[3] = 30;
473 let expected = expected.into_array();
474
475 assert_arrays_eq!(expected, result, &mut ctx);
476 }
477
478 #[test]
479 fn test_append_to_builder_sliced() {
480 let values = PrimitiveArray::new(buffer![0u16; 1024], Validity::NonNullable).into_array();
481 let patches = Patches::new(
482 1024,
483 0,
484 buffer![1u32, 2, 3].into_array(),
485 buffer![10u16, 20, 30].into_array(),
486 None,
487 )
488 .unwrap();
489
490 let session = array_session();
491 let mut ctx = session.create_execution_ctx();
492
493 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
494 .unwrap()
495 .into_array()
496 .slice(3..1024)
497 .unwrap();
498
499 let mut builder = builder_with_capacity_in(
500 array.dtype(),
501 array.len(),
502 vortex_buffer::BufferAllocatorRef::static_ref(),
503 );
504 array.append_to_builder(builder.as_mut(), &mut ctx).unwrap();
505
506 let result = builder.finish();
507
508 let mut expected = buffer_mut![0u16; 1021];
509 expected[0] = 30;
510 let expected = expected.into_array();
511
512 assert_arrays_eq!(expected, result, &mut ctx);
513 }
514
515 #[test]
516 fn test_append_to_builder_with_validity() {
517 let validity = Validity::from_iter((0..10).map(|i| i != 0 && i != 5));
519 let values = PrimitiveArray::new(buffer![0u16; 10], validity).into_array();
520
521 let patches = Patches::new(
523 10,
524 0,
525 buffer![1u32, 2, 3].into_array(),
526 buffer![10u16, 20, 30].into_array(),
527 None,
528 )
529 .unwrap();
530
531 let session = array_session();
532 let mut ctx = session.create_execution_ctx();
533
534 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
535 .unwrap()
536 .into_array();
537
538 let mut builder = builder_with_capacity_in(
539 array.dtype(),
540 array.len(),
541 vortex_buffer::BufferAllocatorRef::static_ref(),
542 );
543 array.append_to_builder(builder.as_mut(), &mut ctx).unwrap();
544
545 let result = builder.finish();
546
547 let expected = PrimitiveArray::from_option_iter([
549 None,
550 Some(10u16),
551 Some(20),
552 Some(30),
553 Some(0),
554 None,
555 Some(0),
556 Some(0),
557 Some(0),
558 Some(0),
559 ])
560 .into_array();
561
562 assert_arrays_eq!(expected, result, &mut ctx);
563 }
564
565 fn make_patched_array(
566 inner: impl IntoIterator<Item = u16>,
567 patch_indices: &[u32],
568 patch_values: &[u16],
569 ) -> VortexResult<PatchedArray> {
570 let values: Vec<u16> = inner.into_iter().collect();
571 let len = values.len();
572 let array = PrimitiveArray::from_iter(values).into_array();
573
574 let indices = PrimitiveArray::from_iter(patch_indices.iter().copied()).into_array();
575 let patch_vals = PrimitiveArray::from_iter(patch_values.iter().copied()).into_array();
576
577 let patches = Patches::new(len, 0, indices, patch_vals, None)?;
578
579 let session = array_session();
580 let mut ctx = session.create_execution_ctx();
581
582 Patched::from_array_and_patches(array, &patches, &mut ctx)
583 }
584
585 #[rstest]
586 #[case::basic(
587 make_patched_array(vec![0u16; 1024], &[1, 2, 3], &[10, 20, 30]).unwrap().into_array()
588 )]
589 #[case::multi_chunk(
590 make_patched_array(vec![0u16; 4096], &[100, 1500, 2500, 3500], &[11, 22, 33, 44]).unwrap().into_array()
591 )]
592 #[case::sliced({
593 let arr = make_patched_array(vec![0u16; 1024], &[1, 2, 3], &[10, 20, 30]).unwrap();
594 arr.into_array().slice(2..1024).unwrap()
595 })]
596 fn test_serde_roundtrip(#[case] array: crate::ArrayRef) {
597 let dtype = array.dtype().clone();
598 let len = array.len();
599
600 let session = array_session();
601 session.arrays().register(Patched);
602
603 let ctx = ArrayContext::empty().with_allowed_ids(
604 session
605 .arrays()
606 .registry()
607 .read(|map| map.keys().copied().collect()),
608 );
609 let serialized = array
610 .serialize(&ctx, &session, &SerializeOptions::default())
611 .unwrap();
612
613 let mut concat = ByteBufferMut::empty();
615 for buf in serialized {
616 concat.extend_from_slice(buf.as_ref());
617 }
618 let concat = concat.freeze();
619
620 let parts = SerializedArray::try_from(concat).unwrap();
621 let decoded = parts
622 .decode(&dtype, len, &ReadContext::new(ctx.to_ids()), &session)
623 .unwrap();
624
625 assert!(decoded.is::<Patched>());
626 assert_eq!(
627 array.display_values().to_string(),
628 decoded.display_values().to_string()
629 );
630 }
631
632 #[test]
633 fn test_with_slots_basic() -> VortexResult<()> {
634 let array = make_patched_array(vec![0u16; 1024], &[1, 2, 3], &[10, 20, 30])?;
635
636 let slots = PatchedSlots::from_slots(
638 array
639 .as_array()
640 .slots()
641 .iter()
642 .cloned()
643 .collect::<ArraySlots>(),
644 );
645 let view = PatchedSlotsView::from_slots(array.as_array().slots());
646 assert_eq!(view.inner.len(), array.inner().len());
647
648 let array_ref = array.into_array();
650 let new_array = unsafe { array_ref.clone().with_slots(slots.into_slots()) }?;
653
654 assert!(new_array.is::<Patched>());
655 assert_eq!(array_ref.len(), new_array.len());
656 assert_eq!(array_ref.dtype(), new_array.dtype());
657
658 let mut ctx = array_session().create_execution_ctx();
660 let original_executed = array_ref.execute::<Canonical>(&mut ctx)?.into_primitive();
661 let new_executed = new_array.execute::<Canonical>(&mut ctx)?.into_primitive();
662
663 assert_arrays_eq!(original_executed, new_executed, &mut ctx);
664
665 Ok(())
666 }
667
668 #[test]
669 fn test_rebuild_modified_inner_from_parts() -> VortexResult<()> {
670 let array = make_patched_array(vec![0u16; 10], &[1, 2, 3], &[10, 20, 30])?;
671
672 let new_inner = PrimitiveArray::from_iter(vec![5u16; 10]).into_array();
674 let slots = PatchedSlots {
675 inner: new_inner,
676 lane_offsets: array.lane_offsets().clone(),
677 patch_indices: array.patch_indices().clone(),
678 patch_values: array.patch_values().clone(),
679 };
680
681 let data = PatchedData {
682 n_lanes: array.n_lanes(),
683 offset: array.offset(),
684 };
685 let new_array = Array::try_from_parts(
686 ArrayParts::new(Patched, array.dtype().clone(), array.len(), data)
687 .with_slots(slots.into_slots()),
688 )?
689 .into_array();
690
691 let mut ctx = array_session().create_execution_ctx();
693 let executed = new_array.execute::<Canonical>(&mut ctx)?.into_primitive();
694
695 let expected = PrimitiveArray::from_iter([5u16, 10, 20, 30, 5, 5, 5, 5, 5, 5]);
697 assert_arrays_eq!(expected, executed, &mut ctx);
698
699 Ok(())
700 }
701}