Skip to content

Commit 23fc4f5

Browse files
authored
[AURON #2446] Flush buffered data based on row count (#2447)
# Which issue does this PR close? Closes #2446 this PR is to fix the incorrect flushing logic, that should be based on the row count rather than the bytes # Rationale for this change Without this fix, the flushing will be triggered too frequently. # What changes are included in this PR? # Are there any user-facing changes? # How was this patch tested? # Was this patch authored or co-authored using generative AI tooling? - [x] Yes - [ ] No Generated-by: OpenAI Codex (GPT-5)
1 parent 4447275 commit 23fc4f5

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

native-engine/datafusion-ext-plans/src/shuffle/buffered_data.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl BufferedData {
9494

9595
let suggested_batch_size =
9696
compute_suggested_batch_size_for_output(self.staging_mem_used, self.staging_num_rows);
97-
if self.staging_mem_used > suggested_batch_size {
97+
if self.staging_num_rows >= suggested_batch_size {
9898
self.flush_staging()?;
9999
}
100100
Ok(())
@@ -399,6 +399,37 @@ mod test {
399399
Ok(batch)
400400
}
401401

402+
#[test]
403+
fn test_add_batch_flushes_by_row_count() -> Result<()> {
404+
let values = (0..1000).collect::<Vec<_>>();
405+
let record_batch = build_table_i32(("a", &values), ("b", &values), ("c", &values))?;
406+
let batch_mem_used = record_batch.get_batch_mem_size() * 2;
407+
let suggested_batch_size =
408+
compute_suggested_batch_size_for_output(batch_mem_used, record_batch.num_rows());
409+
410+
// Ensure this fixture distinguishes byte count from row count.
411+
assert!(batch_mem_used > suggested_batch_size);
412+
assert!(record_batch.num_rows() < suggested_batch_size);
413+
414+
let num_batches = suggested_batch_size.div_ceil(record_batch.num_rows());
415+
let mut buffered_data =
416+
BufferedData::new(Partitioning::RoundRobinPartitioning(4), 0, Time::new());
417+
418+
for _ in 1..num_batches {
419+
buffered_data.add_batch(record_batch.clone())?;
420+
}
421+
assert!(buffered_data.sorted_batches.is_empty());
422+
423+
buffered_data.add_batch(record_batch)?;
424+
assert_eq!(buffered_data.sorted_batches.len(), 1);
425+
assert_eq!(
426+
buffered_data.sorted_batches[0].num_rows(),
427+
num_batches * values.len()
428+
);
429+
assert!(buffered_data.staging_batches.is_empty());
430+
Ok(())
431+
}
432+
402433
#[tokio::test]
403434
async fn test_round_robin() -> Result<()> {
404435
let record_batch = build_table_i32(

0 commit comments

Comments
 (0)