Skip to content

Commit 68cf8a9

Browse files
committed
feat: release memory for LimitedBatchCoalescer once reached the limit/finished
1 parent 3a29d6b commit 68cf8a9

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

  • datafusion/physical-plan/src/coalesce

datafusion/physical-plan/src/coalesce/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use std::sync::Arc;
1819
use arrow::array::RecordBatch;
1920
use arrow::compute::BatchCoalescer;
2021
use arrow::datatypes::SchemaRef;
22+
use arrow_schema::Schema;
2123
use 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

Comments
 (0)