@@ -150,16 +150,14 @@ fn uncompressed_size_in_bytes_u64(
150150 opts : UncompressedSizeOpts ,
151151 ctx : & mut ExecutionCtx ,
152152) -> VortexResult < u64 > {
153- // An inexact query is happy to reuse an exact size or an inexact upper bound.
154- let cached = match (
155- opts. is_exact ( ) ,
156- array. statistics ( ) . get ( Stat :: UncompressedSizeInBytes ) ,
157- ) {
158- ( _, Precision :: Exact ( size) ) | ( false , Precision :: Inexact ( size) ) => Some ( size) ,
159- _ => None ,
160- } ;
161- if let Some ( size_scalar) = cached {
162- return u64:: try_from ( & size_scalar)
153+ // The stat slot holds only the exact size. The inexact size is a different measure, which can
154+ // be larger or smaller than the exact one, so it neither reads nor writes the slot: reusing an
155+ // exact value would make it depend on what was computed before, and caching it would put a
156+ // value in the slot that bounds nothing.
157+ if opts. is_exact ( )
158+ && let Precision :: Exact ( size) = array. statistics ( ) . get ( Stat :: UncompressedSizeInBytes )
159+ {
160+ return u64:: try_from ( & size)
163161 . map_err ( |e| e. with_context ( "Failed to convert uncompressed size stat to u64" ) ) ;
164162 }
165163
@@ -168,19 +166,10 @@ fn uncompressed_size_in_bytes_u64(
168166 acc. accumulate ( array, ctx) ?;
169167 let result = acc. finish ( ) ?;
170168
171- let size = result
169+ Ok ( result
172170 . as_primitive ( )
173171 . typed_value :: < u64 > ( )
174- . vortex_expect ( "uncompressed_size_in_bytes result should not be null" ) ;
175-
176- // An inexact result is cached as an upper bound, so exact queries still compute.
177- if !opts. is_exact ( ) {
178- array. statistics ( ) . set (
179- Stat :: UncompressedSizeInBytes ,
180- Precision :: Inexact ( size. into ( ) ) ,
181- ) ;
182- }
183- Ok ( size)
172+ . vortex_expect ( "uncompressed_size_in_bytes result should not be null" ) )
184173}
185174
186175/// The byte size of all buffers in children in their canonical representation.
@@ -855,35 +844,62 @@ mod tests {
855844 }
856845
857846 #[ test]
858- fn inexact_result_is_cached_as_upper_bound ( ) -> VortexResult < ( ) > {
847+ fn inexact_result_is_not_cached_and_ignores_exact_stat ( ) -> VortexResult < ( ) > {
859848 let sliced = views_into_shared_page ( 0 ..10 ) ?;
860849 let mut ctx = SESSION . create_execution_ctx ( ) ;
861850
862851 let inexact =
863852 uncompressed_size_in_bytes ( & sliced, UncompressedSizeOpts :: inexact ( ) , & mut ctx) ?;
864- assert_eq ! (
865- sliced. statistics( ) . get( Stat :: UncompressedSizeInBytes ) ,
866- Precision :: inexact( u64 :: try_from( inexact) ?)
853+ assert ! (
854+ sliced
855+ . statistics( )
856+ . get( Stat :: UncompressedSizeInBytes )
857+ . is_absent( )
867858 ) ;
868859
869- // An exact query ignores the bound and computes, then replaces it .
860+ // An exact query computes and caches the exact size .
870861 let exact = uncompressed_size_in_bytes ( & sliced, UncompressedSizeOpts :: exact ( ) , & mut ctx) ?;
871- assert ! ( exact < inexact) ;
872862 assert_eq ! (
873863 sliced. statistics( ) . get( Stat :: UncompressedSizeInBytes ) ,
874864 Precision :: exact( u64 :: try_from( exact) ?)
875865 ) ;
876866
877- // An inexact query reuses the exact value .
867+ // An inexact query still measures the held buffers rather than reusing the exact size .
878868 assert_eq ! (
879869 uncompressed_size_in_bytes( & sliced, UncompressedSizeOpts :: inexact( ) , & mut ctx) ?,
880- exact
870+ inexact
881871 ) ;
882872 assert_eq ! ( exact, 430 ) ;
883873 assert_eq ! ( inexact, 27160 ) ;
884874 Ok ( ( ) )
885875 }
886876
877+ #[ test]
878+ fn inexact_can_be_smaller_than_exact ( ) -> VortexResult < ( ) > {
879+ // Every row references the same out-of-line value, which the data buffer holds once.
880+ const VALUE_LEN : u32 = 20 ;
881+ let page = ByteBuffer :: from ( vec ! [ b'x' ; VALUE_LEN as usize ] ) ;
882+ let views = ( 0 ..10 )
883+ . map ( |_| BinaryView :: make_view ( & [ b'x' ; VALUE_LEN as usize ] , 0 , 0 ) )
884+ . collect :: < Buffer < BinaryView > > ( ) ;
885+ // SAFETY: every view points at the `VALUE_LEN` bytes of the page.
886+ let one_value = unsafe {
887+ VarBinViewArray :: new_unchecked (
888+ views,
889+ Arc :: from ( [ page] ) ,
890+ DType :: Utf8 ( Nullability :: NonNullable ) ,
891+ Validity :: NonNullable ,
892+ )
893+ }
894+ . into_array ( ) ;
895+
896+ let exact = aggregate_with ( & one_value, UncompressedSizeOpts :: exact ( ) ) ?;
897+ let inexact = aggregate_with ( & one_value, UncompressedSizeOpts :: inexact ( ) ) ?;
898+ assert_eq ! ( exact, 10 * ( 16 + 20 ) ) ;
899+ assert_eq ! ( inexact, 10 * 16 + 20 ) ;
900+ Ok ( ( ) )
901+ }
902+
887903 #[ test]
888904 fn only_exact_aggregate_has_stat_slot ( ) {
889905 assert_eq ! (
0 commit comments