Skip to content

Commit d2f8fa5

Browse files
authored
fix: SortExec TopK OOM (apache#17622)
1 parent a6a1289 commit d2f8fa5

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

  • datafusion/physical-plan/src/topk

datafusion/physical-plan/src/topk/mod.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -754,19 +754,18 @@ impl TopKHeap {
754754
return Ok((None, topk_rows));
755755
}
756756

757-
// Indices for each row within its respective RecordBatch
758-
let indices: Vec<_> = topk_rows
759-
.iter()
760-
.enumerate()
761-
.map(|(i, k)| (i, k.index))
762-
.collect();
757+
// Collect the batches into a vec and store the "batch_id -> array_pos" mapping, to then
758+
// build the `indices` vec below. This is needed since the batch ids are not continuous.
759+
let mut record_batches = Vec::new();
760+
let mut batch_id_array_pos = HashMap::new();
761+
for (array_pos, (batch_id, batch)) in self.store.batches.iter().enumerate() {
762+
record_batches.push(&batch.batch);
763+
batch_id_array_pos.insert(*batch_id, array_pos);
764+
}
763765

764-
let record_batches: Vec<_> = topk_rows
766+
let indices: Vec<_> = topk_rows
765767
.iter()
766-
.map(|k| {
767-
let entry = self.store.get(k.batch_id).expect("invalid stored batch id");
768-
&entry.batch
769-
})
768+
.map(|k| (batch_id_array_pos[&k.batch_id], k.index))
770769
.collect();
771770

772771
// At this point `indices` contains indexes within the

0 commit comments

Comments
 (0)