@@ -50,8 +50,10 @@ use std::fmt;
5050use std:: fmt:: { Debug , Formatter } ;
5151use std:: fs:: File ;
5252use std:: io:: BufReader ;
53+ use std:: path:: { Path , PathBuf } ;
5354use std:: sync:: Arc ;
5455use tokio:: sync:: mpsc:: { Receiver , Sender } ;
56+ use tempfile:: NamedTempFile ;
5557use tokio:: task;
5658
5759/// Sort arbitrary size of data to get a total order (may spill several times during sorting based on free memory available).
@@ -67,7 +69,7 @@ struct ExternalSorter {
6769 id : MemoryConsumerId ,
6870 schema : SchemaRef ,
6971 in_mem_batches : Mutex < Vec < RecordBatch > > ,
70- spills : Mutex < Vec < String > > ,
72+ spills : Mutex < Vec < NamedTempFile > > ,
7173 /// Sort expressions
7274 expr : Vec < PhysicalSortExpr > ,
7375 runtime : Arc < RuntimeEnv > ,
@@ -222,20 +224,19 @@ impl MemoryConsumer for ExternalSorter {
222224
223225 let baseline_metrics = self . metrics . new_intermediate_baseline ( partition) ;
224226
225- let path = self . runtime . disk_manager . create_tmp_file ( ) ?;
227+ let spillfile = self . runtime . disk_manager . create_tmp_file ( ) ?;
226228 let stream = in_mem_partial_sort (
227229 & mut * in_mem_batches,
228230 self . schema . clone ( ) ,
229231 & * self . expr ,
230232 baseline_metrics,
231233 ) ;
232234
233- spill_partial_sorted_stream ( & mut stream?, path. clone ( ) , self . schema . clone ( ) )
234- . await ?;
235+ spill_partial_sorted_stream ( & mut stream?, spillfile. path ( ) , self . schema . clone ( ) ) . await ?;
235236 let mut spills = self . spills . lock ( ) . await ;
236237 let used = self . inner_metrics . mem_used ( ) . set ( 0 ) ;
237238 self . inner_metrics . record_spill ( used) ;
238- spills. push ( path ) ;
239+ spills. push ( spillfile ) ;
239240 Ok ( used)
240241 }
241242
@@ -280,12 +281,12 @@ fn in_mem_partial_sort(
280281
281282async fn spill_partial_sorted_stream (
282283 in_mem_stream : & mut SendableRecordBatchStream ,
283- path : String ,
284+ path : & Path ,
284285 schema : SchemaRef ,
285286) -> Result < ( ) > {
286287 let ( sender, receiver) = tokio:: sync:: mpsc:: channel ( 2 ) ;
287- let path_clone = path. clone ( ) ;
288- let handle = task:: spawn_blocking ( move || write_sorted ( receiver, path_clone , schema) ) ;
288+ let path : PathBuf = path. into ( ) ;
289+ let handle = task:: spawn_blocking ( move || write_sorted ( receiver, path , schema) ) ;
289290 while let Some ( item) = in_mem_stream. next ( ) . await {
290291 sender. send ( item) . await . ok ( ) ;
291292 }
@@ -300,17 +301,16 @@ async fn spill_partial_sorted_stream(
300301}
301302
302303fn read_spill_as_stream (
303- path : String ,
304+ path : NamedTempFile ,
304305 schema : SchemaRef ,
305306) -> Result < SendableRecordBatchStream > {
306307 let ( sender, receiver) : (
307308 Sender < ArrowResult < RecordBatch > > ,
308309 Receiver < ArrowResult < RecordBatch > > ,
309310 ) = tokio:: sync:: mpsc:: channel ( 2 ) ;
310- let path_clone = path. clone ( ) ;
311311 let join_handle = task:: spawn_blocking ( move || {
312- if let Err ( e) = read_spill ( sender, path_clone ) {
313- error ! ( "Failure while reading spill file: {}. Error: {}" , path, e) ;
312+ if let Err ( e) = read_spill ( sender, path . path ( ) ) {
313+ error ! ( "Failure while reading spill file: {:? }. Error: {}" , path, e) ;
314314 }
315315 } ) ;
316316 Ok ( RecordBatchReceiverStream :: create (
@@ -322,7 +322,7 @@ fn read_spill_as_stream(
322322
323323fn write_sorted (
324324 mut receiver : Receiver < ArrowResult < RecordBatch > > ,
325- path : String ,
325+ path : PathBuf ,
326326 schema : SchemaRef ,
327327) -> Result < ( ) > {
328328 let mut writer = IPCWriter :: new ( path. as_ref ( ) , schema. as_ref ( ) ) ?;
@@ -337,7 +337,7 @@ fn write_sorted(
337337 Ok ( ( ) )
338338}
339339
340- fn read_spill ( sender : Sender < ArrowResult < RecordBatch > > , path : String ) -> Result < ( ) > {
340+ fn read_spill ( sender : Sender < ArrowResult < RecordBatch > > , path : & Path ) -> Result < ( ) > {
341341 let file = BufReader :: new ( File :: open ( & path) ?) ;
342342 let reader = FileReader :: try_new ( file) ?;
343343 for batch in reader {
0 commit comments