@@ -40,12 +40,13 @@ use crate::{
4040 SendableRecordBatchStream , Statistics ,
4141} ;
4242
43- use arrow:: compute:: { concat_batches, lexsort_to_indices, take , SortColumn } ;
43+ use arrow:: compute:: { concat_batches, lexsort_to_indices, SortColumn } ;
4444use arrow:: datatypes:: SchemaRef ;
4545use arrow:: record_batch:: RecordBatch ;
4646use arrow:: row:: { RowConverter , SortField } ;
4747use arrow_array:: { Array , RecordBatchOptions , UInt32Array } ;
4848use arrow_schema:: DataType ;
49+ use datafusion_common:: utils:: get_arrayref_at_indices;
4950use datafusion_common:: { internal_err, Result } ;
5051use datafusion_execution:: disk_manager:: RefCountedTempFile ;
5152use datafusion_execution:: memory_pool:: { MemoryConsumer , MemoryReservation } ;
@@ -350,12 +351,8 @@ impl ExternalSorter {
350351 self . fetch ,
351352 self . reservation . new_empty ( ) ,
352353 )
353- } else if !self . in_mem_batches . is_empty ( ) {
354- self . in_mem_sort_stream ( self . metrics . baseline . clone ( ) )
355354 } else {
356- Ok ( Box :: pin ( EmptyRecordBatchStream :: new ( Arc :: clone (
357- & self . schema ,
358- ) ) ) )
355+ self . in_mem_sort_stream ( self . metrics . baseline . clone ( ) )
359356 }
360357 }
361358
@@ -500,15 +497,19 @@ impl ExternalSorter {
500497 & mut self ,
501498 metrics : BaselineMetrics ,
502499 ) -> Result < SendableRecordBatchStream > {
503- assert_ne ! ( self . in_mem_batches. len( ) , 0 ) ;
500+ if self . in_mem_batches . is_empty ( ) {
501+ return Ok ( Box :: pin ( EmptyRecordBatchStream :: new ( Arc :: clone (
502+ & self . schema ,
503+ ) ) ) ) ;
504+ }
504505
505506 // The elapsed compute timer is updated when the value is dropped.
506507 // There is no need for an explicit call to drop.
507508 let elapsed_compute = metrics. elapsed_compute ( ) . clone ( ) ;
508509 let _timer = elapsed_compute. timer ( ) ;
509510
510511 if self . in_mem_batches . len ( ) == 1 {
511- let batch = self . in_mem_batches . remove ( 0 ) ;
512+ let batch = self . in_mem_batches . swap_remove ( 0 ) ;
512513 let reservation = self . reservation . take ( ) ;
513514 return self . sort_batch_stream ( batch, metrics, reservation) ;
514515 }
@@ -616,11 +617,7 @@ pub fn sort_batch(
616617 lexsort_to_indices ( & sort_columns, fetch) ?
617618 } ;
618619
619- let columns = batch
620- . columns ( )
621- . iter ( )
622- . map ( |c| take ( c. as_ref ( ) , & indices, None ) )
623- . collect :: < Result < _ , _ > > ( ) ?;
620+ let columns = get_arrayref_at_indices ( batch. columns ( ) , & indices) ?;
624621
625622 let options = RecordBatchOptions :: new ( ) . with_row_count ( Some ( indices. len ( ) ) ) ;
626623 Ok ( RecordBatch :: try_new_with_options (
0 commit comments