Skip to content

Commit d90a102

Browse files
joroKr21avantgardnerio
authored andcommitted
Fix array_sort for empty record batch (#290) (apache#15149) v48
1 parent 2a62d4c commit d90a102

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

datafusion/functions-nested/src/sort.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl ScalarUDFImpl for ArraySort {
7070

7171
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
7272
match &arg_types[0] {
73+
DataType::Null => Ok(DataType::Null),
7374
List(field) | FixedSizeList(field, _) => Ok(List(Arc::new(
7475
Field::new_list_field(field.data_type().clone(), true),
7576
))),
@@ -141,6 +142,16 @@ pub fn array_sort_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
141142
return exec_err!("array_sort expects one to three arguments");
142143
}
143144

145+
if args[0].data_type().is_null() {
146+
return Ok(Arc::clone(&args[0]));
147+
}
148+
149+
let list_array = as_list_array(&args[0])?;
150+
let row_count = list_array.len();
151+
if row_count == 0 || list_array.value_type().is_null() {
152+
return Ok(Arc::clone(&args[0]));
153+
}
154+
144155
let sort_option = match args.len() {
145156
1 => None,
146157
2 => {
@@ -161,12 +172,6 @@ pub fn array_sort_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
161172
_ => return exec_err!("array_sort expects 1 to 3 arguments"),
162173
};
163174

164-
let list_array = as_list_array(&args[0])?;
165-
let row_count = list_array.len();
166-
if row_count == 0 {
167-
return Ok(Arc::clone(&args[0]));
168-
}
169-
170175
let mut array_lengths = vec![];
171176
let mut arrays = vec![];
172177
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
@@ -2209,11 +2209,16 @@ NULL
22092209
[, 51, 52, 54, 55, 56, 57, 58, 59, 60]
22102210
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70]
22112211

2212-
# test with empty array
2212+
# test with empty table
22132213
query ?
2214-
select array_sort([]);
2214+
select array_sort(column1, 'DESC', 'NULLS FIRST') from arrays_values where false;
22152215
----
2216-
[]
2216+
2217+
# test with empty array
2218+
query ??
2219+
select array_sort([]), array_sort(NULL);
2220+
----
2221+
[] NULL
22172222

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

0 commit comments

Comments
 (0)