@@ -46,7 +46,7 @@ use arrow::datatypes::SchemaRef;
46
46
use arrow:: ipc:: reader:: FileReader ;
47
47
use arrow:: record_batch:: RecordBatch ;
48
48
use arrow:: row:: { RowConverter , SortField } ;
49
- use arrow_array:: { Array , UInt32Array } ;
49
+ use arrow_array:: { Array , RecordBatchOptions , UInt32Array } ;
50
50
use arrow_schema:: DataType ;
51
51
use datafusion_common:: { exec_err, DataFusionError , Result } ;
52
52
use datafusion_common_runtime:: SpawnedTask ;
@@ -617,7 +617,12 @@ pub(crate) fn sort_batch(
617
617
. map ( |c| take ( c. as_ref ( ) , & indices, None ) )
618
618
. collect :: < Result < _ , _ > > ( ) ?;
619
619
620
- Ok ( RecordBatch :: try_new ( batch. schema ( ) , columns) ?)
620
+ let options = RecordBatchOptions :: new ( ) . with_row_count ( Some ( indices. len ( ) ) ) ;
621
+ Ok ( RecordBatch :: try_new_with_options (
622
+ batch. schema ( ) ,
623
+ columns,
624
+ & options,
625
+ ) ?)
621
626
}
622
627
623
628
#[ inline]
@@ -1008,6 +1013,8 @@ mod tests {
1008
1013
use datafusion_execution:: config:: SessionConfig ;
1009
1014
use datafusion_execution:: runtime_env:: RuntimeConfig ;
1010
1015
1016
+ use datafusion_common:: ScalarValue ;
1017
+ use datafusion_physical_expr:: expressions:: Literal ;
1011
1018
use futures:: FutureExt ;
1012
1019
1013
1020
#[ tokio:: test]
@@ -1415,4 +1422,20 @@ mod tests {
1415
1422
1416
1423
Ok ( ( ) )
1417
1424
}
1425
+
1426
+ #[ test]
1427
+ fn test_empty_sort_batch ( ) {
1428
+ let schema = Arc :: new ( Schema :: empty ( ) ) ;
1429
+ let options = RecordBatchOptions :: new ( ) . with_row_count ( Some ( 1 ) ) ;
1430
+ let batch =
1431
+ RecordBatch :: try_new_with_options ( schema. clone ( ) , vec ! [ ] , & options) . unwrap ( ) ;
1432
+
1433
+ let expressions = vec ! [ PhysicalSortExpr {
1434
+ expr: Arc :: new( Literal :: new( ScalarValue :: Int64 ( Some ( 1 ) ) ) ) ,
1435
+ options: SortOptions :: default ( ) ,
1436
+ } ] ;
1437
+
1438
+ let result = sort_batch ( & batch, & expressions, None ) . unwrap ( ) ;
1439
+ assert_eq ! ( result. num_rows( ) , 1 ) ;
1440
+ }
1418
1441
}
0 commit comments