@@ -99,7 +99,6 @@ impl LimitedBatchCoalescer {
9999 if let Some ( fetch) = self . fetch {
100100 // limit previously reached
101101 if self . total_rows >= fetch {
102- self . release_memory ( ) ;
103102 return Ok ( PushBatchStatus :: LimitReached ) ;
104103 }
105104
@@ -112,8 +111,6 @@ impl LimitedBatchCoalescer {
112111 let batch_head = batch. slice ( 0 , remaining_rows) ;
113112 self . total_rows += batch_head. num_rows ( ) ;
114113 self . inner . push_batch ( batch_head) ?;
115-
116- self . release_memory ( ) ;
117114 return Ok ( PushBatchStatus :: LimitReached ) ;
118115 }
119116 }
@@ -136,17 +133,26 @@ impl LimitedBatchCoalescer {
136133 pub fn finish ( & mut self ) -> Result < ( ) > {
137134 self . inner . finish_buffered_batch ( ) ?;
138135 self . finished = true ;
139- self . release_memory ( ) ;
140136 Ok ( ( ) )
141137 }
142138
143139 pub ( crate ) fn is_finished ( & self ) -> bool {
144140 self . finished
145141 }
146142
143+ fn limit_reached ( & self ) -> bool {
144+ self . fetch . is_some_and ( |fetch| self . total_rows >= fetch)
145+ }
146+
147147 /// Return the next completed batch, if any
148148 pub fn next_completed_batch ( & mut self ) -> Option < RecordBatch > {
149- self . inner . next_completed_batch ( )
149+ let next = self . inner . next_completed_batch ( ) ;
150+
151+ if self . inner . is_empty ( ) && ( self . finished || self . limit_reached ( ) ) {
152+ self . release_memory ( ) ;
153+ }
154+
155+ next
150156 }
151157
152158 fn release_memory ( & mut self ) {
0 commit comments