rluvaton commented on code in PR #20314:
URL: https://github.com/apache/datafusion/pull/20314#discussion_r2840676251
##########
datafusion/physical-plan/src/sorts/stream.rs:
##########
@@ -276,3 +280,105 @@ impl<T: CursorArray> PartitionedStream for
FieldCursorStream<T> {
}))
}
}
+
+/// A lazy, memory-efficient sort iterator used as a fallback during aggregate
+/// spill when there is not enough memory for an eager sort (which requires ~2x
+/// peak memory to hold both the unsorted and sorted copies simultaneously).
+///
+/// On the first call to `next()`, a sorted index array (`UInt32Array`) is
+/// computed via `lexsort_to_indices`. Subsequent calls yield chunks of
+/// `batch_size` rows by `take`-ing from the original batch using slices of
+/// this index array. Each `take` copies data for the chunk (not zero-copy),
+/// but only one chunk is live at a time since the caller consumes it before
+/// requesting the next. Once all rows have been yielded, the original batch
+/// and index array are dropped to free memory.
Review Comment:
`take` explicitly note that it will try to avoid allocation when not
necessary, although current implementation may allocate when `slice` would have
being possible, it is not guaranteed, and due to the fact that you rely on
`take` to not do zero copy, please add a test to make sure that the memory is
reduced after each call to `next` (which if `take` implementation no longer do
deep copy this test will fail):
> ```
> /// Note that this kernel, similar to other kernels in this crate,
> /// will avoid allocating where not necessary. Consequently
> /// the returned array may share buffers with the inputs
> ```
https://github.com/apache/arrow-rs/blob/3a019d0104668146c44c9da4dca0c5aed0a5324c/arrow-select/src/take.rs#L55-L57
--
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]