From dc3d8b7f328cd606f5f119b8eb3c130bd8976942 Mon Sep 17 00:00:00 2001 From: Connor Tsui Date: Wed, 18 Nov 2026 14:01:15 -0500 Subject: [PATCH] Move patch take search logic out of type dispatch Signed-off-by: Connor Tsui --- vortex-array/src/patches.rs | 131 ++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 51 deletions(-) diff --git a/vortex-array/src/patches.rs b/vortex-array/src/patches.rs index a3bc6574b58..dfbfa839d7b 100644 --- a/vortex-array/src/patches.rs +++ b/vortex-array/src/patches.rs @@ -966,10 +966,6 @@ impl Patches { } } - #[expect( - clippy::cognitive_complexity, - reason = "complexity is from nested match_each_* macros" - )] pub fn take_search( &self, take_indices: PrimitiveArray, @@ -988,56 +984,20 @@ impl Patches { .map(|co| co.clone().execute::(ctx)) .transpose()?; - let (values_indices, new_indices): (BufferMut, BufferMut) = + let take_validity = take_indices_validity.execute_mask(take_indices.len(), ctx)?; + let (values_indices, new_indices) = match_each_unsigned_integer_ptype!(patch_indices.ptype(), |PatchT| { - let patch_indices_slice = patch_indices.as_slice::(); match_each_unsigned_integer_ptype!(take_indices_unsigned.ptype(), |TakeT| { - let take_slice = take_indices_unsigned.as_slice::(); - - if let Some(chunk_offsets) = chunk_offsets { - match_each_unsigned_integer_ptype!(chunk_offsets.ptype(), |OffsetT| { - let chunk_offsets = chunk_offsets.as_slice::(); - take_indices_with_search_fn( - patch_indices_slice, - take_slice, - take_indices - .as_ref() - .validity()? - .execute_mask(take_indices.as_ref().len(), ctx)?, - include_nulls, - ctx.allocator(), - |take_idx| { - self.search_index_chunked_batch( - patch_indices_slice, - chunk_offsets, - take_idx, - ) - }, - )? - }) - } else { - take_indices_with_search_fn( - patch_indices_slice, - take_slice, - take_indices - .as_ref() - .validity()? - .execute_mask(take_indices.as_ref().len(), ctx)?, - include_nulls, - ctx.allocator(), - |take_idx| { - let Some(offset) = ::from(self.offset) else { - // If the offset cannot be converted to T, it's larger than all values in this array. - return Ok(SearchResult::NotFound(patch_indices_slice.len())); - }; - - patch_indices_slice - .search_sorted(&(take_idx + offset), SearchSortedSide::Left) - }, - )? - } + self.take_search_typed( + patch_indices.as_slice::(), + take_indices_unsigned.as_slice::(), + chunk_offsets.as_ref(), + take_validity, + include_nulls, + ctx.allocator(), + ) }) - }); + })?; if new_indices.is_empty() { return Ok(None); @@ -1059,6 +1019,75 @@ impl Patches { })) } + fn take_search_typed( + &self, + patch_indices: &[PatchT], + take_indices: &[TakeT], + chunk_offsets: Option<&PrimitiveArray>, + take_validity: Mask, + include_nulls: bool, + allocator: &BufferAllocatorRef, + ) -> VortexResult<(BufferMut, BufferMut)> + where + PatchT: UnsignedPType, + TakeT: UnsignedPType, + usize: TryFrom, + { + if let Some(chunk_offsets) = chunk_offsets { + return match_each_unsigned_integer_ptype!(chunk_offsets.ptype(), |OffsetT| { + self.take_search_chunked::( + patch_indices, + take_indices, + chunk_offsets.as_slice::(), + take_validity, + include_nulls, + allocator, + ) + }); + } + + take_indices_with_search_fn( + patch_indices, + take_indices, + take_validity, + include_nulls, + allocator, + |take_idx| { + let Some(offset) = ::from(self.offset) else { + // An offset too large for `PatchT` exceeds every patch index. + return Ok(SearchResult::NotFound(patch_indices.len())); + }; + + patch_indices.search_sorted(&(take_idx + offset), SearchSortedSide::Left) + }, + ) + } + + fn take_search_chunked( + &self, + patch_indices: &[PatchT], + take_indices: &[TakeT], + chunk_offsets: &[OffsetT], + take_validity: Mask, + include_nulls: bool, + allocator: &BufferAllocatorRef, + ) -> VortexResult<(BufferMut, BufferMut)> + where + PatchT: UnsignedPType, + TakeT: UnsignedPType, + OffsetT: UnsignedPType, + usize: TryFrom + TryFrom, + { + take_indices_with_search_fn( + patch_indices, + take_indices, + take_validity, + include_nulls, + allocator, + |take_idx| self.search_index_chunked_batch(patch_indices, chunk_offsets, take_idx), + ) + } + pub fn take_map( &self, take_indices: PrimitiveArray,