@@ -10,15 +10,13 @@ use vortex_dtype::Nullability::NonNullable;
1010use vortex_dtype:: { DType , NativePType , PType , match_each_integer_ptype} ;
1111use vortex_error:: { VortexError , VortexExpect , VortexResult , vortex_bail, vortex_err} ;
1212use vortex_mask:: { AllOr , Mask } ;
13- use vortex_scalar:: Scalar ;
13+ use vortex_scalar:: { PValue , Scalar } ;
1414
1515use crate :: aliases:: hash_map:: HashMap ;
1616use crate :: arrays:: PrimitiveArray ;
17- use crate :: compute:: {
18- SearchResult , SearchSorted , SearchSortedSide , cast, filter, search_sorted, take,
19- } ;
17+ use crate :: compute:: { SearchResult , SearchSorted , SearchSortedSide , cast, filter, take} ;
2018use crate :: variants:: PrimitiveArrayTrait ;
21- use crate :: { Array , ArrayRef , IntoArray , ToCanonical } ;
19+ use crate :: { Array , ArrayRef , ArrayVariants , IntoArray , ToCanonical } ;
2220
2321#[ derive( Copy , Clone , Serialize , Deserialize , prost:: Message ) ]
2422pub struct PatchesMetadata {
@@ -220,7 +218,14 @@ impl Patches {
220218
221219 /// Return the insertion point of `index` in the [Self::indices].
222220 pub fn search_index ( & self , index : usize ) -> VortexResult < SearchResult > {
223- search_sorted ( & self . indices , index + self . offset , SearchSortedSide :: Left )
221+ Ok ( self
222+ . indices
223+ . as_primitive_typed ( )
224+ . vortex_expect ( "must be primitive" )
225+ . search_sorted (
226+ & PValue :: U64 ( ( index + self . offset ) as u64 ) ,
227+ SearchSortedSide :: Left ,
228+ ) )
224229 }
225230
226231 /// Return the search_sorted result for the given target re-mapped into the original indices.
@@ -229,23 +234,29 @@ impl Patches {
229234 target : T ,
230235 side : SearchSortedSide ,
231236 ) -> VortexResult < SearchResult > {
232- search_sorted ( self . values ( ) , target. into ( ) , side) . and_then ( |sr| {
233- let index_idx = sr. to_offsets_index ( self . indices ( ) . len ( ) , side) ;
234- let index = usize:: try_from ( & self . indices ( ) . scalar_at ( index_idx) ?) ? - self . offset ;
235- Ok ( match sr {
236- // If we reached the end of patched values when searching then the result is one after the last patch index
237- SearchResult :: Found ( i) => SearchResult :: Found (
238- if i == self . indices ( ) . len ( ) || side == SearchSortedSide :: Right {
239- index + 1
240- } else {
241- index
242- } ,
243- ) ,
244- // If the result is NotFound we should return index that's one after the nearest not found index for the corresponding value
245- SearchResult :: NotFound ( i) => {
246- SearchResult :: NotFound ( if i == 0 { index } else { index + 1 } )
247- }
248- } )
237+ let target = target. into ( ) ;
238+
239+ let sr = if let Some ( parray) = self . values ( ) . as_primitive_typed ( ) {
240+ parray. search_sorted ( & target. as_primitive ( ) . pvalue ( ) , side)
241+ } else {
242+ self . values ( ) . search_sorted ( & target, side)
243+ } ;
244+
245+ let index_idx = sr. to_offsets_index ( self . indices ( ) . len ( ) , side) ;
246+ let index = usize:: try_from ( & self . indices ( ) . scalar_at ( index_idx) ?) ? - self . offset ;
247+ Ok ( match sr {
248+ // If we reached the end of patched values when searching then the result is one after the last patch index
249+ SearchResult :: Found ( i) => SearchResult :: Found (
250+ if i == self . indices ( ) . len ( ) || side == SearchSortedSide :: Right {
251+ index + 1
252+ } else {
253+ index
254+ } ,
255+ ) ,
256+ // If the result is NotFound we should return index that's one after the nearest not found index for the corresponding value
257+ SearchResult :: NotFound ( i) => {
258+ SearchResult :: NotFound ( if i == 0 { index } else { index + 1 } )
259+ }
249260 } )
250261 }
251262
0 commit comments