File tree Expand file tree Collapse file tree
datafusion/physical-plan/src/topk Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments