SubhamSinghal commented on code in PR #27136:
URL: https://github.com/apache/datafusion/pull/27136#discussion_r4657418959
##########
datafusion/physical-plan/src/topk/mod.rs:
##########
@@ -1917,26 +1957,152 @@ impl<S: StoreSlots> PartitionedTopKCore<S> {
Ok(())
}
- /// Phase 3: gather the rows this batch contributed into a single batch and
- /// hand it to the store, then compact the store if it needs it. Only rows
- /// admitted at some point during the pass are kept, so what stays pinned
is
- /// bounded by admissions rather than by input size — and the entry is
- /// freed as soon as the last slot referencing it is released.
+ /// Run every row of `batch` through `admit` against its partition's state,
+ /// then gather the admitted rows into the store and compact it: the whole
+ /// of `insert_batch` but the admission rule, which is all that differs
+ /// between the three operators.
///
- /// No uses left on `pending` means every admission from this batch was
- /// released again before the pass ended, so there is nothing to keep and
- /// the id goes back to the next batch.
+ /// Both column sets are encoded once for the whole batch, then the rows
+ /// are visited in batch order, or back to front for an
+ /// [`StoreSlots::ORDER_FREE`] state, rather than grouped by partition.
+ /// Interleaving partitions cannot change a decision — a state is only read
+ /// and written by its own partition's rows, which still reach it in the
+ /// order the pass walks — and it costs no pass over the partitions seen so
+ /// far, which would be quadratic in the input whenever partition count
+ /// grows with it. Only keys are copied during the pass; an admission
+ /// records where its row will live, and one gather afterwards materializes
+ /// the batch's admissions once, however many partitions they went to. Only
+ /// rows admitted at some point during the pass are kept, so what stays
+ /// pinned is bounded by admissions rather than by input size, and the
+ /// entry is freed as soon as the last slot referencing it is released.
+ ///
+ /// `admit` gets the row's partition state, its encoded ORDER BY key, the
+ /// slot the row takes if admitted, and the [`Admission`] it reports
+ /// through, and returns whether it admitted the row.
///
/// The caller resizes the reservation afterwards, since only it knows its
/// full size.
- fn finish_batch(
+ fn insert_batch_with(
&mut self,
batch: &RecordBatch,
- pending: PendingEntry,
- replacements: usize,
- ) -> Result<()> {
+ mut admit: impl FnMut(&mut S, &[u8], StoreRef, &mut Admission<'_>) ->
bool,
+ ) -> Result<()>
+ where
+ S: Default,
+ {
+ let elapsed_compute = self.metrics.baseline.elapsed_compute().clone();
+ let _timer = elapsed_compute.timer();
+
+ if batch.num_rows() == 0 {
+ return Ok(());
+ }
+ self.encode(batch)?;
+
+ self.admitted_rows.clear();
+ // The gathered batch does not exist yet, but its id does, so a slot
can
+ // point at it during the pass with no back-patching afterwards.
+ let pending_id = self.store.next_batch_id();
+ let mut admission = Admission {
+ store: &mut self.store,
+ pending_id,
+ pending_released: 0,
+ grown: 0,
+ freed: 0,
+ released_slots: 0,
+ replacements: 0,
+ };
+ // Accumulated locally and folded in once, so the running totals are
not
+ // touched per row.
+ let mut interned_bytes = 0usize;
+ let num_rows = batch.num_rows();
+ // Front to back, a batch whose ORDER BY values fall, such as
+ // `ORDER BY ts DESC` over rows stored by ascending `ts`, makes nearly
+ // every row a new best that displaces one. Back to front, the first K
+ // per partition fill the state and the boundary check drops the rest,
+ // which only saves work when partitions get more than K rows each.
+ let backwards = S::ORDER_FREE
+ && num_rows > self.k.saturating_mul(self.partitions.len())
+ && self.scratch_rows.row(num_rows - 1) < self.scratch_rows.row(0);
Review Comment:
Addressed in
[f168960](https://github.com/apache/datafusion/pull/27136/commits/f168960ff125539a09e863de32c4d6138970f180)
--
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]