@@ -76,14 +76,14 @@ impl VTable for ALP {
7676 array. dtype . hash ( state) ;
7777 array. encoded ( ) . array_hash ( state, precision) ;
7878 array. exponents . hash ( state) ;
79- array. patches . array_hash ( state, precision) ;
79+ array. patches ( ) . array_hash ( state, precision) ;
8080 }
8181
8282 fn array_eq ( array : & ALPArray , other : & ALPArray , precision : Precision ) -> bool {
8383 array. dtype == other. dtype
8484 && array. encoded ( ) . array_eq ( other. encoded ( ) , precision)
8585 && array. exponents == other. exponents
86- && array. patches . array_eq ( & other. patches , precision)
86+ && array. patches ( ) . array_eq ( & other. patches ( ) , precision)
8787 }
8888
8989 fn nbuffers ( _array : & ALPArray ) -> usize {
@@ -114,23 +114,12 @@ impl VTable for ALP {
114114 slots. len( )
115115 ) ;
116116
117- // Reconstruct patches from slots + existing metadata
118- array. patches = match ( & slots[ PATCH_INDICES_SLOT ] , & slots[ PATCH_VALUES_SLOT ] ) {
119- ( Some ( indices) , Some ( values) ) => {
120- let old = array
121- . patches
122- . as_ref ( )
123- . vortex_expect ( "ALPArray had patch slots but no patches metadata" ) ;
124- Some ( Patches :: new (
125- old. array_len ( ) ,
126- old. offset ( ) ,
127- indices. clone ( ) ,
128- values. clone ( ) ,
129- slots[ PATCH_CHUNK_OFFSETS_SLOT ] . clone ( ) ,
130- ) ?)
131- }
132- _ => None ,
133- } ;
117+ // If patch slots are being cleared, clear the metadata too
118+ if slots[ PATCH_INDICES_SLOT ] . is_none ( ) || slots[ PATCH_VALUES_SLOT ] . is_none ( ) {
119+ array. patch_offset = None ;
120+ array. patch_offset_within_chunk = None ;
121+ }
122+
134123 array. slots = slots;
135124 Ok ( ( ) )
136125 }
@@ -240,7 +229,8 @@ pub(super) const SLOT_NAMES: [&str; NUM_SLOTS] = [
240229#[ derive( Clone , Debug ) ]
241230pub struct ALPArray {
242231 slots : Vec < Option < ArrayRef > > ,
243- patches : Option < Patches > ,
232+ patch_offset : Option < usize > ,
233+ patch_offset_within_chunk : Option < usize > ,
244234 dtype : DType ,
245235 exponents : Exponents ,
246236 stats_set : ArrayStats ,
@@ -409,12 +399,17 @@ impl ALPArray {
409399 } ;
410400
411401 let slots = Self :: make_slots ( & encoded, & patches) ;
402+ let ( patch_offset, patch_offset_within_chunk) = match & patches {
403+ Some ( p) => ( Some ( p. offset ( ) ) , p. offset_within_chunk ( ) ) ,
404+ None => ( None , None ) ,
405+ } ;
412406
413407 Ok ( Self {
414408 dtype,
415409 slots,
416410 exponents,
417- patches,
411+ patch_offset,
412+ patch_offset_within_chunk,
418413 stats_set : Default :: default ( ) ,
419414 } )
420415 }
@@ -430,12 +425,17 @@ impl ALPArray {
430425 dtype : DType ,
431426 ) -> Self {
432427 let slots = Self :: make_slots ( & encoded, & patches) ;
428+ let ( patch_offset, patch_offset_within_chunk) = match & patches {
429+ Some ( p) => ( Some ( p. offset ( ) ) , p. offset_within_chunk ( ) ) ,
430+ None => ( None , None ) ,
431+ } ;
433432
434433 Self {
435434 dtype,
436435 slots,
437436 exponents,
438- patches,
437+ patch_offset,
438+ patch_offset_within_chunk,
439439 stats_set : Default :: default ( ) ,
440440 }
441441 }
@@ -472,17 +472,38 @@ impl ALPArray {
472472 self . exponents
473473 }
474474
475- pub fn patches ( & self ) -> Option < & Patches > {
476- self . patches . as_ref ( )
475+ pub fn patches ( & self ) -> Option < Patches > {
476+ match (
477+ & self . slots [ PATCH_INDICES_SLOT ] ,
478+ & self . slots [ PATCH_VALUES_SLOT ] ,
479+ ) {
480+ ( Some ( indices) , Some ( values) ) => {
481+ let patch_offset = self
482+ . patch_offset
483+ . vortex_expect ( "has patch slots but no patch_offset" ) ;
484+ Some ( unsafe {
485+ Patches :: new_unchecked (
486+ self . encoded ( ) . len ( ) ,
487+ patch_offset,
488+ indices. clone ( ) ,
489+ values. clone ( ) ,
490+ self . slots [ PATCH_CHUNK_OFFSETS_SLOT ] . clone ( ) ,
491+ self . patch_offset_within_chunk ,
492+ )
493+ } )
494+ }
495+ _ => None ,
496+ }
477497 }
478498
479499 /// Consumes the array and returns its parts.
480500 #[ inline]
481501 pub fn into_parts ( mut self ) -> ( ArrayRef , Exponents , Option < Patches > , DType ) {
502+ let patches = self . patches ( ) ;
482503 let encoded = self . slots [ ENCODED_SLOT ]
483504 . take ( )
484505 . vortex_expect ( "ALPArray encoded slot" ) ;
485- ( encoded, self . exponents , self . patches , self . dtype )
506+ ( encoded, self . exponents , patches, self . dtype )
486507 }
487508}
488509
@@ -506,7 +527,6 @@ mod tests {
506527 use vortex_array:: arrays:: PrimitiveArray ;
507528 use vortex_array:: assert_arrays_eq;
508529 use vortex_array:: session:: ArraySession ;
509- use vortex_array:: vtable:: ValidityHelper ;
510530 use vortex_session:: VortexSession ;
511531
512532 use super :: * ;
0 commit comments