@@ -215,6 +215,13 @@ fn sorter(
215215}
216216
217217async fn assert_released ( pool : & AdjustablePool , runtime : & RuntimeEnv ) {
218+ wait_until_released ( pool, runtime) . await ;
219+ let state = pool. state . lock ( ) . unwrap ( ) ;
220+ assert ! ( state. peak <= pool. capacity) ;
221+ assert_eq ! ( state. unchecked_over_limit, 0 ) ;
222+ }
223+
224+ async fn wait_until_released ( pool : & AdjustablePool , runtime : & RuntimeEnv ) {
218225 // Buffered readers may finish dropping their state after the owning stream.
219226 tokio:: time:: timeout ( Duration :: from_secs ( 5 ) , async {
220227 while pool. reserved ( ) != 0
@@ -227,9 +234,6 @@ async fn assert_released(pool: &AdjustablePool, runtime: &RuntimeEnv) {
227234 . await
228235 . expect ( "sort reservations and spill files must be released" ) ;
229236 assert_eq ! ( runtime. disk_manager. used_disk_space( ) , 0 ) ;
230- let state = pool. state . lock ( ) . unwrap ( ) ;
231- assert ! ( state. peak <= pool. capacity) ;
232- assert_eq ! ( state. unchecked_over_limit, 0 ) ;
233237}
234238
235239async fn check_spill_preserves_merge_workspace_after_limit_decreases ( ) -> Result < ( ) > {
@@ -696,7 +700,7 @@ async fn check_merge_keys_larger_than_batches() -> Result<()> {
696700 ]
697701 } ;
698702 let int_fields = [ ( "a" , DataType :: Int32 ) , ( "b" , DataType :: Int32 ) ] ;
699- check_merge_keys ( & int_fields, 2 , |batch| Ok ( int_keys ( batch) ) ) . await ?;
703+ check_merge_keys ( & int_fields, 2 , 16 , true , |batch| Ok ( int_keys ( batch) ) ) . await ?;
700704 // Longer than 12 bytes, so views keep them in data buffers.
701705 let strings = |batch : i32 | -> StringArray {
702706 ( 0 ..rows)
@@ -707,25 +711,66 @@ async fn check_merge_keys_larger_than_batches() -> Result<()> {
707711 DataType :: Dictionary ( Box :: new ( DataType :: Int32 ) , Box :: new ( DataType :: Utf8 ) ) ;
708712 for payload in [ DataType :: Utf8View , dictionary] {
709713 let fields = [ & int_fields[ ..] , & [ ( "c" , payload. clone ( ) ) ] ] . concat ( ) ;
710- check_merge_keys ( & fields, 2 , |batch| {
714+ check_merge_keys ( & fields, 2 , 16 , true , |batch| {
711715 let payload = cast ( & strings ( batch) , & payload) ?;
712716 Ok ( [ int_keys ( batch) , vec ! [ payload] ] . concat ( ) )
713717 } )
714718 . await ?;
715719 }
716- check_merge_keys ( & [ ( "k" , DataType :: FixedSizeBinary ( 16 ) ) ] , 1 , |batch| {
717- let keys = ( 0 ..rows) . map ( |i| u128:: from ( ( ( i * 31 + batch) % 1000 ) as u32 ) ) ;
718- let keys = FixedSizeBinaryArray :: try_from_iter ( keys. map ( u128:: to_be_bytes) ) ?;
719- Ok ( vec ! [ Arc :: new( keys) ] )
720+ // Views into 1024 dictionary values share their bytes, as a
721+ // dictionary-encoded Parquet column produces. Sorting the buffered batches
722+ // before a spill grows past the pool, on `main` too.
723+ let fields = [ & int_fields[ ..] , & [ ( "c" , DataType :: Utf8View ) ] ] . concat ( ) ;
724+ check_merge_keys ( & fields, 2 , 16 , false , |batch| {
725+ let values: StringArray = ( 0 ..1024 )
726+ . map ( |v| Some ( format ! ( "{batch:>4}-{v:0>59}" ) ) )
727+ . collect ( ) ;
728+ let keys =
729+ Int32Array :: from_iter_values ( ( 0 ..rows) . map ( |i| ( i * 7 + batch) % 1024 ) ) ;
730+ let views = cast (
731+ & DictionaryArray :: try_new ( keys, Arc :: new ( values) ) ?,
732+ & DataType :: Utf8View ,
733+ ) ?;
734+ Ok ( [ int_keys ( batch) , vec ! [ views] ] . concat ( ) )
735+ } )
736+ . await ?;
737+ // Batches that do not overlap, so a merged batch takes the strings of
738+ // several split batches of one run, in as many buffers.
739+ let view_keys = [ ( "a" , DataType :: Utf8View ) , ( "b" , DataType :: Utf8View ) ] ;
740+ check_merge_keys ( & view_keys, 2 , 8 , true , |batch| {
741+ let keys = |key : & str | -> Result < ArrayRef > {
742+ let strings: StringArray = ( 0 ..rows)
743+ . rev ( )
744+ . map ( |i| Some ( format ! ( "{batch:0>4}{key}{i:0>61}" ) ) )
745+ . collect ( ) ;
746+ Ok ( cast ( & strings, & DataType :: Utf8View ) ?)
747+ } ;
748+ Ok ( vec ! [ keys( "a" ) ?, keys( "b" ) ?] )
720749 } )
750+ . await ?;
751+ check_merge_keys (
752+ & [ ( "k" , DataType :: FixedSizeBinary ( 16 ) ) ] ,
753+ 1 ,
754+ 16 ,
755+ true ,
756+ |batch| {
757+ let keys = ( 0 ..rows) . map ( |i| u128:: from ( ( ( i * 31 + batch) % 1000 ) as u32 ) ) ;
758+ let keys = FixedSizeBinaryArray :: try_from_iter ( keys. map ( u128:: to_be_bytes) ) ?;
759+ Ok ( vec ! [ Arc :: new( keys) ] )
760+ } ,
761+ )
721762 . await
722763}
723764
724765/// Sorts 64 batches of `columns` by the first `keys` fields with room to buffer
725- /// 16 of them, then checks the spilled output and that all memory is released.
766+ /// `buffered` of them, then checks the spilled output and that all memory is
767+ /// released. Unless `within_pool`, the pool may have been exceeded by an
768+ /// unchecked `grow`.
726769async fn check_merge_keys (
727770 fields : & [ ( & str , DataType ) ] ,
728771 keys : usize ,
772+ buffered : usize ,
773+ within_pool : bool ,
729774 columns : impl Fn ( i32 ) -> Result < Vec < ArrayRef > > ,
730775) -> Result < ( ) > {
731776 let schema = Arc :: new ( Schema :: new (
@@ -742,7 +787,8 @@ async fn check_merge_keys(
742787 PhysicalSortExpr :: new_default ( Arc :: new ( Column :: new ( fields[ i] . 0 , i) ) )
743788 } ) )
744789 . expect ( "at least one sort key" ) ;
745- let capacity = MERGE_BYTES + 16 * get_reserved_bytes_for_record_batch ( & batches[ 0 ] ) ?;
790+ let capacity =
791+ MERGE_BYTES + buffered * get_reserved_bytes_for_record_batch ( & batches[ 0 ] ) ?;
746792 let pool = AdjustablePool :: new ( capacity) ;
747793 let runtime = RuntimeEnvBuilder :: new ( )
748794 . with_memory_pool ( Arc :: clone ( & pool) as Arc < dyn MemoryPool > )
@@ -766,7 +812,11 @@ async fn check_merge_keys(
766812 let expected = sort_batch ( & concat_batches ( & schema, & batches) ?, & ordering, None ) ?;
767813 assert_eq ! ( concat_batches( & schema, & output) ?, expected) ;
768814 drop ( sorter) ;
769- assert_released ( & pool, & runtime) . await ;
815+ if within_pool {
816+ assert_released ( & pool, & runtime) . await ;
817+ } else {
818+ wait_until_released ( & pool, & runtime) . await ;
819+ }
770820 Ok ( ( ) )
771821}
772822
0 commit comments