berkaysynnada commented on code in PR #16881:
URL: https://github.com/apache/datafusion/pull/16881#discussion_r2230512203
##########
datafusion/physical-plan/src/sorts/partial_sort.rs:
##########
@@ -375,34 +375,52 @@ impl PartialSortStream {
return Poll::Ready(None);
}
loop {
+ // Check if we've already reached the fetch limit
+ if self.fetch == Some(0) {
+ self.is_closed = true;
+ return Poll::Ready(None);
+ }
+
return Poll::Ready(match ready!(self.input.poll_next_unpin(cx)) {
Some(Ok(batch)) => {
- if let Some(slice_point) =
- self.get_slice_point(self.common_prefix_length,
&batch)?
+ // Merge new batch into in_mem_batch
+ self.in_mem_batch = concat_batches(
+ &self.schema(),
+ &[self.in_mem_batch.clone(), batch],
+ )?;
+
+ // Check if we have a slice point, otherwise keep
accumulating in `self.in_mem_batch`.
+ if let Some(slice_point) = self
+ .get_slice_point(self.common_prefix_length,
&self.in_mem_batch)?
{
- self.in_mem_batches.push(batch.slice(0, slice_point));
- let remaining_batch =
- batch.slice(slice_point, batch.num_rows() -
slice_point);
- // Extract the sorted batch
- let sorted_batch = self.sort_in_mem_batches();
- // Refill with the remaining batch
- self.in_mem_batches.push(remaining_batch);
-
- debug_assert!(sorted_batch
- .as_ref()
- .map(|batch| batch.num_rows() > 0)
- .unwrap_or(true));
- Some(sorted_batch)
+ let sorted = self.in_mem_batch.slice(0, slice_point);
+ self.in_mem_batch = self.in_mem_batch.slice(
+ slice_point,
+ self.in_mem_batch.num_rows() - slice_point,
+ );
+ let sorted_batch = sort_batch(&sorted, &self.expr,
self.fetch)?;
+ if let Some(fetch) = self.fetch.as_mut() {
+ *fetch -= sorted_batch.num_rows();
+ // If we've reached the fetch limit, close the
stream
+ if *fetch == 0 {
Review Comment:
😳
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]