Skip to content

Commit 7b4e559

Browse files
Minor: Avoid emitting empty batches in partial sort (#13895)
* Update partial_sort.rs * Update partial_sort.rs * Update partial_sort.rs
1 parent 3864b11 commit 7b4e559

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

datafusion/physical-plan/src/sorts/partial_sort.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,29 +366,41 @@ impl PartialSortStream {
366366
return Poll::Ready(None);
367367
}
368368
loop {
369-
return Poll::Ready(Some(match ready!(self.input.poll_next_unpin(cx)) {
369+
return Poll::Ready(match ready!(self.input.poll_next_unpin(cx)) {
370370
Some(Ok(batch)) => {
371371
if let Some(slice_point) =
372372
self.get_slice_point(self.common_prefix_length, &batch)?
373373
{
374374
self.in_mem_batches.push(batch.slice(0, slice_point));
375375
let remaining_batch =
376376
batch.slice(slice_point, batch.num_rows() - slice_point);
377+
// Extract the sorted batch
377378
let sorted_batch = self.sort_in_mem_batches();
379+
// Refill with the remaining batch
378380
self.in_mem_batches.push(remaining_batch);
379-
sorted_batch
381+
382+
debug_assert!(sorted_batch
383+
.as_ref()
384+
.map(|batch| batch.num_rows() > 0)
385+
.unwrap_or(true));
386+
Some(sorted_batch)
380387
} else {
381388
self.in_mem_batches.push(batch);
382389
continue;
383390
}
384391
}
385-
Some(Err(e)) => Err(e),
392+
Some(Err(e)) => Some(Err(e)),
386393
None => {
387394
self.is_closed = true;
388395
// once input is consumed, sort the rest of the inserted batches
389-
self.sort_in_mem_batches()
396+
let remaining_batch = self.sort_in_mem_batches()?;
397+
if remaining_batch.num_rows() > 0 {
398+
Some(Ok(remaining_batch))
399+
} else {
400+
None
401+
}
390402
}
391-
}));
403+
});
392404
}
393405
}
394406

@@ -409,9 +421,6 @@ impl PartialSortStream {
409421
self.is_closed = true;
410422
}
411423
}
412-
// Empty record batches should not be emitted.
413-
// They need to be treated as [`Option<RecordBatch>`]es and handle separately
414-
debug_assert!(result.num_rows() > 0);
415424
Ok(result)
416425
}
417426

0 commit comments

Comments
 (0)