1- use enum_iterator:: all;
21use flatbuffers:: { FlatBufferBuilder , Follow , WIPOffset } ;
3- use vortex_error:: VortexError ;
4- use vortex_flatbuffers:: { ReadFlatBuffer , WriteFlatBuffer } ;
2+ use vortex_error:: { VortexError , vortex_bail } ;
3+ use vortex_flatbuffers:: { ReadFlatBuffer , WriteFlatBuffer , array as fba } ;
54use vortex_scalar:: ScalarValue ;
65
76use super :: traits:: { StatsProvider , StatsProviderExt } ;
87use crate :: stats:: { Precision , Stat , StatsSet } ;
98
109impl WriteFlatBuffer for StatsSet {
11- type Target < ' t > = crate :: flatbuffers :: ArrayStats < ' t > ;
10+ type Target < ' t > = fba :: ArrayStats < ' t > ;
1211
1312 /// All statistics written must be exact
1413 fn write_flatbuffer < ' fb > (
1514 & self ,
1615 fbb : & mut FlatBufferBuilder < ' fb > ,
1716 ) -> WIPOffset < Self :: Target < ' fb > > {
18- let min = self
17+ let ( min_precision , min) = self
1918 . get ( Stat :: Min )
20- . and_then ( Precision :: as_exact)
21- . map ( |min| fbb. create_vector ( & min. to_protobytes :: < Vec < u8 > > ( ) ) ) ;
19+ . map ( |sum| {
20+ (
21+ if sum. is_exact ( ) {
22+ fba:: Precision :: Exact
23+ } else {
24+ fba:: Precision :: Inexact
25+ } ,
26+ Some ( fbb. create_vector ( & sum. into_inner ( ) . to_protobytes :: < Vec < u8 > > ( ) ) ) ,
27+ )
28+ } )
29+ . unwrap_or_else ( || ( fba:: Precision :: Inexact , None ) ) ;
2230
23- let max = self
31+ let ( max_precision , max) = self
2432 . get ( Stat :: Max )
25- . and_then ( Precision :: as_exact)
26- . map ( |max| fbb. create_vector ( & max. to_protobytes :: < Vec < u8 > > ( ) ) ) ;
33+ . map ( |sum| {
34+ (
35+ if sum. is_exact ( ) {
36+ fba:: Precision :: Exact
37+ } else {
38+ fba:: Precision :: Inexact
39+ } ,
40+ Some ( fbb. create_vector ( & sum. into_inner ( ) . to_protobytes :: < Vec < u8 > > ( ) ) ) ,
41+ )
42+ } )
43+ . unwrap_or_else ( || ( fba:: Precision :: Inexact , None ) ) ;
2744
2845 let sum = self
2946 . get ( Stat :: Sum )
3047 . and_then ( Precision :: as_exact)
3148 . map ( |sum| fbb. create_vector ( & sum. to_protobytes :: < Vec < u8 > > ( ) ) ) ;
3249
33- let stat_args = & crate :: flatbuffers :: ArrayStatsArgs {
50+ let stat_args = & fba :: ArrayStatsArgs {
3451 min,
52+ min_precision,
3553 max,
54+ max_precision,
3655 sum,
3756 is_sorted : self
3857 . get_as :: < bool > ( Stat :: IsSorted )
@@ -54,20 +73,20 @@ impl WriteFlatBuffer for StatsSet {
5473 . and_then ( Precision :: as_exact) ,
5574 } ;
5675
57- crate :: flatbuffers :: ArrayStats :: create ( fbb, stat_args)
76+ fba :: ArrayStats :: create ( fbb, stat_args)
5877 }
5978}
6079
6180impl ReadFlatBuffer for StatsSet {
62- type Source < ' a > = crate :: flatbuffers :: ArrayStats < ' a > ;
81+ type Source < ' a > = fba :: ArrayStats < ' a > ;
6382 type Error = VortexError ;
6483
6584 fn read_flatbuffer < ' buf > (
6685 fb : & <Self :: Source < ' buf > as Follow < ' buf > >:: Inner ,
6786 ) -> Result < Self , Self :: Error > {
6887 let mut stats_set = StatsSet :: default ( ) ;
6988
70- for stat in all :: < Stat > ( ) {
89+ for stat in Stat :: all ( ) {
7190 match stat {
7291 Stat :: IsConstant => {
7392 if let Some ( is_constant) = fb. is_constant ( ) {
@@ -89,17 +108,27 @@ impl ReadFlatBuffer for StatsSet {
89108 }
90109 Stat :: Max => {
91110 if let Some ( max) = fb. max ( ) {
111+ let value = ScalarValue :: from_protobytes ( max. bytes ( ) ) ?;
92112 stats_set. set (
93113 Stat :: Max ,
94- Precision :: Exact ( ScalarValue :: from_protobytes ( max. bytes ( ) ) ?) ,
114+ match fb. max_precision ( ) {
115+ fba:: Precision :: Exact => Precision :: Exact ( value) ,
116+ fba:: Precision :: Inexact => Precision :: Inexact ( value) ,
117+ _ => vortex_bail ! ( "Corrupted max_precision field" ) ,
118+ } ,
95119 ) ;
96120 }
97121 }
98122 Stat :: Min => {
99123 if let Some ( min) = fb. min ( ) {
124+ let value = ScalarValue :: from_protobytes ( min. bytes ( ) ) ?;
100125 stats_set. set (
101126 Stat :: Min ,
102- Precision :: Exact ( ScalarValue :: from_protobytes ( min. bytes ( ) ) ?) ,
127+ match fb. min_precision ( ) {
128+ fba:: Precision :: Exact => Precision :: Exact ( value) ,
129+ fba:: Precision :: Inexact => Precision :: Inexact ( value) ,
130+ _ => vortex_bail ! ( "Corrupted min_precision field" ) ,
131+ } ,
103132 ) ;
104133 }
105134 }
0 commit comments