1515// specific language governing permissions and limitations
1616// under the License.
1717
18+ use std:: sync:: Arc ;
1819use arrow:: array:: RecordBatch ;
1920use arrow:: compute:: BatchCoalescer ;
2021use arrow:: datatypes:: SchemaRef ;
22+ use arrow_schema:: Schema ;
2123use datafusion_common:: { Result , assert_or_internal_err} ;
2224
2325/// Concatenate multiple [`RecordBatch`]es and apply a limit
@@ -97,6 +99,7 @@ impl LimitedBatchCoalescer {
9799 if let Some ( fetch) = self . fetch {
98100 // limit previously reached
99101 if self . total_rows >= fetch {
102+ self . release_memory ( ) ;
100103 return Ok ( PushBatchStatus :: LimitReached ) ;
101104 }
102105
@@ -109,6 +112,8 @@ impl LimitedBatchCoalescer {
109112 let batch_head = batch. slice ( 0 , remaining_rows) ;
110113 self . total_rows += batch_head. num_rows ( ) ;
111114 self . inner . push_batch ( batch_head) ?;
115+
116+ self . release_memory ( ) ;
112117 return Ok ( PushBatchStatus :: LimitReached ) ;
113118 }
114119 }
@@ -131,6 +136,7 @@ impl LimitedBatchCoalescer {
131136 pub fn finish ( & mut self ) -> Result < ( ) > {
132137 self . inner . finish_buffered_batch ( ) ?;
133138 self . finished = true ;
139+ self . release_memory ( ) ;
134140 Ok ( ( ) )
135141 }
136142
@@ -142,6 +148,10 @@ impl LimitedBatchCoalescer {
142148 pub fn next_completed_batch ( & mut self ) -> Option < RecordBatch > {
143149 self . inner . next_completed_batch ( )
144150 }
151+
152+ fn release_memory ( & mut self ) {
153+ self . inner = BatchCoalescer :: new ( Arc :: new ( Schema :: empty ( ) ) , 1 ) ;
154+ }
145155}
146156
147157#[ cfg( test) ]
0 commit comments