diff --git a/datafusion/functions-nested/src/length.rs b/datafusion/functions-nested/src/length.rs index dbdbb4cedeaec..397a18edb4fa8 100644 --- a/datafusion/functions-nested/src/length.rs +++ b/datafusion/functions-nested/src/length.rs @@ -204,6 +204,9 @@ fn compute_array_length( if current_dimension == dimension { return Ok(Some(value.len() as u64)); } + if value.is_empty() { + return Ok(None); + } match value.data_type() { List(..) => { diff --git a/datafusion/sqllogictest/test_files/array/array_length.slt b/datafusion/sqllogictest/test_files/array/array_length.slt index a4cf882efa82f..41c9eed02a77e 100644 --- a/datafusion/sqllogictest/test_files/array/array_length.slt +++ b/datafusion/sqllogictest/test_files/array/array_length.slt @@ -58,6 +58,12 @@ select array_length(make_array()), array_length(make_array(), 1), array_length(m ---- 0 0 NULL +# array_length of a dimension below an empty nested list is NULL +query IIII +select array_length(arrow_cast(make_array(), 'List(List(Int64))'), 1), array_length(arrow_cast(make_array(), 'List(List(Int64))'), 2), array_length(arrow_cast(make_array(), 'LargeList(List(Int64))'), 2), array_length(arrow_cast(make_array(make_array()), 'List(List(List(Int64)))'), 3); +---- +0 NULL NULL NULL + # array_length scalar function #6 nested array query III select array_length([[1, 2, 3, 4], [5, 6, 7, 8]]), array_length([[1, 2, 3, 4], [5, 6, 7, 8]], 1), array_length([[1, 2, 3, 4], [5, 6, 7, 8]], 2);