|
1 | | -use itertools::Itertools; |
2 | | -use vortex_dtype::{match_each_integer_ptype, match_each_native_ptype}; |
| 1 | +use arrow_buffer::ArrowNativeType; |
| 2 | +use vortex_dtype::{match_each_integer_ptype, match_each_native_ptype, NativePType}; |
3 | 3 | use vortex_error::VortexResult; |
4 | 4 |
|
5 | 5 | use crate::array::PrimitiveArray; |
6 | 6 | use crate::patches::Patches; |
| 7 | +use crate::validity::Validity; |
7 | 8 | use crate::variants::PrimitiveArrayTrait; |
8 | 9 | use crate::{ArrayLen, IntoArrayVariant}; |
9 | 10 |
|
10 | 11 | impl PrimitiveArray { |
11 | 12 | #[allow(clippy::cognitive_complexity)] |
12 | 13 | pub fn patch(self, patches: Patches) -> VortexResult<Self> { |
13 | | - let (_, indices, values) = patches.into_parts(); |
14 | | - let indices = indices.into_primitive()?; |
15 | | - let values = values.into_primitive()?; |
| 14 | + let (_, patch_indices, patch_values) = patches.into_parts(); |
| 15 | + let patch_indices = patch_indices.into_primitive()?; |
| 16 | + let patch_values = patch_values.into_primitive()?; |
16 | 17 |
|
17 | 18 | let patched_validity = |
18 | 19 | self.validity() |
19 | | - .patch(self.len(), indices.as_ref(), values.validity())?; |
| 20 | + .patch(self.len(), patch_indices.as_ref(), patch_values.validity())?; |
20 | 21 |
|
21 | | - match_each_integer_ptype!(indices.ptype(), |$I| { |
| 22 | + match_each_integer_ptype!(patch_indices.ptype(), |$I| { |
22 | 23 | match_each_native_ptype!(self.ptype(), |$T| { |
23 | | - let mut own_values = self.into_maybe_null_slice::<$T>(); |
24 | | - for (idx, value) in indices.into_maybe_null_slice::<$I>().into_iter().zip_eq(values.into_maybe_null_slice::<$T>().into_iter()) { |
25 | | - own_values[idx as usize] = value; |
26 | | - } |
27 | | - Ok(Self::from_vec(own_values, patched_validity)) |
| 24 | + self.patch_typed::<$T, $I>(patch_indices, patch_values, patched_validity) |
28 | 25 | }) |
29 | 26 | }) |
30 | 27 | } |
| 28 | + |
| 29 | + fn patch_typed<T, I>( |
| 30 | + self, |
| 31 | + patch_indices: PrimitiveArray, |
| 32 | + patch_values: PrimitiveArray, |
| 33 | + patched_validity: Validity, |
| 34 | + ) -> VortexResult<Self> |
| 35 | + where |
| 36 | + T: NativePType + ArrowNativeType, |
| 37 | + I: NativePType + ArrowNativeType, |
| 38 | + { |
| 39 | + let mut own_values = self.into_maybe_null_slice::<T>(); |
| 40 | + |
| 41 | + let patch_indices = patch_indices.into_maybe_null_slice::<I>(); |
| 42 | + let patch_values = patch_values.into_maybe_null_slice::<T>(); |
| 43 | + for (idx, value) in itertools::zip_eq(patch_indices, patch_values) { |
| 44 | + own_values[idx.as_usize()] = value; |
| 45 | + } |
| 46 | + Ok(Self::from_vec(own_values, patched_validity)) |
| 47 | + } |
31 | 48 | } |
32 | 49 |
|
33 | 50 | #[cfg(test)] |
|
0 commit comments