44use std:: fmt:: Debug ;
55use std:: hash:: Hash ;
66
7- use vortex_array:: patches:: Patches ;
7+ use vortex_array:: patches:: { Patches , PatchesMetadata } ;
8+ use vortex_array:: serde:: ArrayChildren ;
89use vortex_array:: stats:: { ArrayStats , StatsSetRef } ;
910use vortex_array:: vtable:: {
10- ArrayVTable , CanonicalVTable , NotSupported , VTable , ValidityChild , ValidityVTableFromChild ,
11+ ArrayVTable , CanonicalVTable , EncodeVTable , NotSupported , VTable , ValidityChild ,
12+ ValidityVTableFromChild , VisitorVTable ,
1113} ;
1214use vortex_array:: {
13- Array , ArrayEq , ArrayHash , ArrayRef , Canonical , EncodingId , EncodingRef , Precision , vtable,
15+ Array , ArrayBufferVisitor , ArrayChildVisitor , ArrayEq , ArrayHash , ArrayRef , Canonical ,
16+ DeserializeMetadata , EncodingId , EncodingRef , Precision , ProstMetadata , SerializeMetadata ,
17+ vtable,
1418} ;
19+ use vortex_buffer:: ByteBuffer ;
1520use vortex_dtype:: { DType , PType } ;
16- use vortex_error:: { VortexExpect , VortexResult , vortex_ensure} ;
21+ use vortex_error:: { VortexError , VortexExpect , VortexResult , vortex_bail , vortex_ensure} ;
1722
1823use crate :: ALPFloat ;
19- use crate :: alp:: { Exponents , decompress} ;
24+ use crate :: alp:: { Exponents , alp_encode , decompress} ;
2025
2126vtable ! ( ALP ) ;
2227
2328impl VTable for ALPVTable {
2429 type Array = ALPArray ;
2530 type Encoding = ALPEncoding ;
31+ type Metadata = ProstMetadata < ALPMetadata > ;
2632
2733 type ArrayVTable = Self ;
2834 type CanonicalVTable = Self ;
@@ -31,7 +37,6 @@ impl VTable for ALPVTable {
3137 type VisitorVTable = Self ;
3238 type ComputeVTable = NotSupported ;
3339 type EncodeVTable = Self ;
34- type SerdeVTable = Self ;
3540 type OperatorVTable = NotSupported ;
3641
3742 fn id ( _encoding : & Self :: Encoding ) -> EncodingId {
@@ -41,6 +46,73 @@ impl VTable for ALPVTable {
4146 fn encoding ( _array : & Self :: Array ) -> EncodingRef {
4247 EncodingRef :: new_ref ( ALPEncoding . as_ref ( ) )
4348 }
49+
50+ fn metadata ( array : & ALPArray ) -> VortexResult < Self :: Metadata > {
51+ let exponents = array. exponents ( ) ;
52+ Ok ( ProstMetadata ( ALPMetadata {
53+ exp_e : exponents. e as u32 ,
54+ exp_f : exponents. f as u32 ,
55+ patches : array
56+ . patches ( )
57+ . map ( |p| p. to_metadata ( array. len ( ) , array. dtype ( ) ) )
58+ . transpose ( ) ?,
59+ } ) )
60+ }
61+
62+ fn serialize ( metadata : Self :: Metadata ) -> VortexResult < Option < Vec < u8 > > > {
63+ Ok ( Some ( metadata. serialize ( ) ) )
64+ }
65+
66+ fn deserialize ( buffer : & [ u8 ] ) -> VortexResult < Self :: Metadata > {
67+ Ok ( ProstMetadata (
68+ <ProstMetadata < ALPMetadata > as DeserializeMetadata >:: deserialize ( buffer) ?,
69+ ) )
70+ }
71+
72+ fn build (
73+ _encoding : & ALPEncoding ,
74+ dtype : & DType ,
75+ len : usize ,
76+ metadata : & Self :: Metadata ,
77+ _buffers : & [ ByteBuffer ] ,
78+ children : & dyn ArrayChildren ,
79+ ) -> VortexResult < ALPArray > {
80+ let encoded_ptype = match & dtype {
81+ DType :: Primitive ( PType :: F32 , n) => DType :: Primitive ( PType :: I32 , * n) ,
82+ DType :: Primitive ( PType :: F64 , n) => DType :: Primitive ( PType :: I64 , * n) ,
83+ d => vortex_bail ! ( MismatchedTypes : "f32 or f64" , d) ,
84+ } ;
85+ let encoded = children. get ( 0 , & encoded_ptype, len) ?;
86+
87+ let patches = metadata
88+ . patches
89+ . map ( |p| {
90+ let indices = children. get ( 1 , & p. indices_dtype ( ) , p. len ( ) ) ?;
91+ let values = children. get ( 2 , dtype, p. len ( ) ) ?;
92+ let chunk_offsets = p
93+ . chunk_offsets_dtype ( )
94+ . map ( |dtype| children. get ( 3 , & dtype, usize:: try_from ( p. chunk_offsets_len ( ) ) ?) )
95+ . transpose ( ) ?;
96+
97+ Ok :: < _ , VortexError > ( Patches :: new (
98+ len,
99+ p. offset ( ) ,
100+ indices,
101+ values,
102+ chunk_offsets,
103+ ) )
104+ } )
105+ . transpose ( ) ?;
106+
107+ ALPArray :: try_new (
108+ encoded,
109+ Exponents {
110+ e : u8:: try_from ( metadata. exp_e ) ?,
111+ f : u8:: try_from ( metadata. exp_f ) ?,
112+ } ,
113+ patches,
114+ )
115+ }
44116}
45117
46118#[ derive( Clone , Debug ) ]
@@ -55,6 +127,16 @@ pub struct ALPArray {
55127#[ derive( Clone , Debug ) ]
56128pub struct ALPEncoding ;
57129
130+ #[ derive( Clone , prost:: Message ) ]
131+ pub struct ALPMetadata {
132+ #[ prost( uint32, tag = "1" ) ]
133+ pub ( crate ) exp_e : u32 ,
134+ #[ prost( uint32, tag = "2" ) ]
135+ pub ( crate ) exp_f : u32 ,
136+ #[ prost( message, optional, tag = "3" ) ]
137+ pub ( crate ) patches : Option < PatchesMetadata > ,
138+ }
139+
58140impl ALPArray {
59141 fn validate (
60142 encoded : & dyn Array ,
@@ -285,3 +367,28 @@ impl CanonicalVTable<ALPVTable> for ALPVTable {
285367 Canonical :: Primitive ( decompress ( array. clone ( ) ) )
286368 }
287369}
370+
371+ impl EncodeVTable < ALPVTable > for ALPVTable {
372+ fn encode (
373+ _encoding : & ALPEncoding ,
374+ canonical : & Canonical ,
375+ like : Option < & ALPArray > ,
376+ ) -> VortexResult < Option < ALPArray > > {
377+ let parray = canonical. clone ( ) . into_primitive ( ) ;
378+ let exponents = like. map ( |a| a. exponents ( ) ) ;
379+ let alp = alp_encode ( & parray, exponents) ?;
380+
381+ Ok ( Some ( alp) )
382+ }
383+ }
384+
385+ impl VisitorVTable < ALPVTable > for ALPVTable {
386+ fn visit_buffers ( _array : & ALPArray , _visitor : & mut dyn ArrayBufferVisitor ) { }
387+
388+ fn visit_children ( array : & ALPArray , visitor : & mut dyn ArrayChildVisitor ) {
389+ visitor. visit_child ( "encoded" , array. encoded ( ) ) ;
390+ if let Some ( patches) = array. patches ( ) {
391+ visitor. visit_patches ( patches) ;
392+ }
393+ }
394+ }
0 commit comments