gratus00 commented on code in PR #21600:
URL: https://github.com/apache/datafusion/pull/21600#discussion_r3079433371
##########
datafusion/physical-plan/src/sorts/sort.rs:
##########
@@ -323,56 +312,185 @@ impl ExternalSorter {
self.reserve_memory_for_batch_and_maybe_spill(&input)
.await?;
- self.in_mem_batches.push(input);
+ let coalescer = self
+ .coalescer
+ .as_mut()
+ .expect("coalescer must exist during insert phase");
+ coalescer
+ .push_batch(input)
+ .map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?;
+
+ self.drain_completed_batches()?;
+
+ Ok(())
+ }
+
+ /// Drains completed (full) batches from the coalescer, sorts each,
+ /// and appends the sorted chunks to `sorted_runs`.
+ fn drain_completed_batches(&mut self) -> Result<()> {
+ // Collect completed batches first to avoid borrow conflict
+ let mut completed = vec![];
+ if let Some(coalescer) = self.coalescer.as_mut() {
+ while let Some(batch) = coalescer.next_completed_batch() {
+ completed.push(batch);
+ }
+ }
+ for batch in &completed {
+ self.sort_and_store_run(batch)?;
+ }
+ Ok(())
+ }
+
+ /// Sorts a single coalesced batch and stores the result as a new run.
+ ///
+ /// Uses radix sort when the batch is large enough to amortize encoding
+ /// overhead (more than `batch_size` rows). Otherwise falls back to
lexsort.
+ fn sort_and_store_run(&mut self, batch: &RecordBatch) -> Result<()> {
+ let use_radix_for_this_batch =
+ self.use_radix && batch.num_rows() > self.batch_size;
+
Review Comment:
also just realizing, the "graceful degradation" section says the pipeline
falls back to lexsort below batch_size rows, but wouldn't the else branch here
still take the radix path? When use_radix_for_this_batch is false, this calls
sort_batch, and sort_batch independently checks use_radix_sort when
fetch.is_none()
for radix-eligible schemas it takes the radix path regardless of row count.
Wouldn't that be slower?
--
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]