Skip to content

Commit c3f2e65

Browse files
joroKr21avantgardnerio
authored andcommitted
Fix array_sort for empty record batch (#290)
1 parent 2a53ce5 commit c3f2e65

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

datafusion/functions-nested/src/sort.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::utils::make_scalar_function;
2121
use arrow::compute;
2222
use arrow_array::{Array, ArrayRef, ListArray};
2323
use arrow_buffer::{BooleanBufferBuilder, NullBuffer, OffsetBuffer};
24-
use arrow_schema::DataType::{FixedSizeList, LargeList, List};
2524
use arrow_schema::{DataType, Field, SortOptions};
2625
use datafusion_common::cast::{as_list_array, as_string_array};
2726
use datafusion_common::{exec_err, Result};
@@ -70,19 +69,9 @@ impl ScalarUDFImpl for ArraySort {
7069

7170
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
7271
match &arg_types[0] {
73-
List(field) | FixedSizeList(field, _) => Ok(List(Arc::new(Field::new(
74-
"item",
75-
field.data_type().clone(),
76-
true,
77-
)))),
78-
LargeList(field) => Ok(LargeList(Arc::new(Field::new(
79-
"item",
80-
field.data_type().clone(),
81-
true,
82-
)))),
83-
_ => exec_err!(
84-
"Not reachable, data_type should be List, LargeList or FixedSizeList"
85-
),
72+
DataType::Null => Ok(DataType::Null),
73+
arg_type @ DataType::List(_) => Ok(arg_type.clone()),
74+
arg_type => exec_err!("{} does not support type {arg_type}", self.name()),
8675
}
8776
}
8877

@@ -142,6 +131,16 @@ pub fn array_sort_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
142131
return exec_err!("array_sort expects one to three arguments");
143132
}
144133

134+
if args[0].data_type().is_null() {
135+
return Ok(Arc::clone(&args[0]));
136+
}
137+
138+
let list_array = as_list_array(&args[0])?;
139+
let row_count = list_array.len();
140+
if row_count == 0 || list_array.value_type().is_null() {
141+
return Ok(Arc::clone(&args[0]));
142+
}
143+
145144
let sort_option = match args.len() {
146145
1 => None,
147146
2 => {
@@ -162,12 +161,6 @@ pub fn array_sort_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
162161
_ => return exec_err!("array_sort expects 1 to 3 arguments"),
163162
};
164163

165-
let list_array = as_list_array(&args[0])?;
166-
let row_count = list_array.len();
167-
if row_count == 0 {
168-
return Ok(Arc::clone(&args[0]));
169-
}
170-
171164
let mut array_lengths = vec![];
172165
let mut arrays = vec![];
173166
let mut valid = BooleanBufferBuilder::new(row_count);

datafusion/sqllogictest/test_files/array.slt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,11 +2197,16 @@ NULL
21972197
[, 51, 52, 54, 55, 56, 57, 58, 59, 60]
21982198
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70]
21992199

2200-
# test with empty array
2200+
# test with empty table
22012201
query ?
2202-
select array_sort([]);
2202+
select array_sort(column1, 'DESC', 'NULLS FIRST') from arrays_values where false;
22032203
----
2204-
[]
2204+
2205+
# test with empty array
2206+
query ??
2207+
select array_sort([]), array_sort(NULL);
2208+
----
2209+
[] NULL
22052210

22062211
# test with empty row, the row that does not match the condition has row count 0
22072212
statement ok

0 commit comments

Comments
 (0)