Skip to content

Commit 1395adf

Browse files
authored
fix: Specify row count in sort_batch for empty batch (#10094)
1 parent b914409 commit 1395adf

File tree

1 file changed

+25
-2
lines changed
  • datafusion/physical-plan/src/sorts

1 file changed

+25
-2
lines changed

datafusion/physical-plan/src/sorts/sort.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use arrow::datatypes::SchemaRef;
4646
use arrow::ipc::reader::FileReader;
4747
use arrow::record_batch::RecordBatch;
4848
use arrow::row::{RowConverter, SortField};
49-
use arrow_array::{Array, UInt32Array};
49+
use arrow_array::{Array, RecordBatchOptions, UInt32Array};
5050
use arrow_schema::DataType;
5151
use datafusion_common::{exec_err, DataFusionError, Result};
5252
use datafusion_common_runtime::SpawnedTask;
@@ -617,7 +617,12 @@ pub(crate) fn sort_batch(
617617
.map(|c| take(c.as_ref(), &indices, None))
618618
.collect::<Result<_, _>>()?;
619619

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+
)?)
621626
}
622627

623628
#[inline]
@@ -1008,6 +1013,8 @@ mod tests {
10081013
use datafusion_execution::config::SessionConfig;
10091014
use datafusion_execution::runtime_env::RuntimeConfig;
10101015

1016+
use datafusion_common::ScalarValue;
1017+
use datafusion_physical_expr::expressions::Literal;
10111018
use futures::FutureExt;
10121019

10131020
#[tokio::test]
@@ -1415,4 +1422,20 @@ mod tests {
14151422

14161423
Ok(())
14171424
}
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+
}
14181441
}

0 commit comments

Comments
 (0)