Skip to content

Commit

Permalink
feat: support LargeList in array_empty (apache#8321)
Browse files Browse the repository at this point in the history
* support LargeList in array_empty

* update err info
  • Loading branch information
Weijun-H authored and appletreeisyellow committed Dec 14, 2023
1 parent 26b1e13 commit e4662a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
16 changes: 13 additions & 3 deletions datafusion/physical-expr/src/array_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use arrow_buffer::NullBuffer;

use arrow_schema::FieldRef;
use datafusion_common::cast::{
as_generic_string_array, as_int64_array, as_list_array, as_string_array,
as_generic_list_array, as_generic_string_array, as_int64_array, as_list_array,
as_null_array, as_string_array,
};
use datafusion_common::utils::array_into_list_array;
use datafusion_common::{
Expand Down Expand Up @@ -939,12 +940,21 @@ pub fn array_concat(args: &[ArrayRef]) -> Result<ArrayRef> {

/// Array_empty SQL function
pub fn array_empty(args: &[ArrayRef]) -> Result<ArrayRef> {
if args[0].as_any().downcast_ref::<NullArray>().is_some() {
if as_null_array(&args[0]).is_ok() {
// Make sure to return Boolean type.
return Ok(Arc::new(BooleanArray::new_null(args[0].len())));
}
let array_type = args[0].data_type();

let array = as_list_array(&args[0])?;
match array_type {
DataType::List(_) => array_empty_dispatch::<i32>(&args[0]),
DataType::LargeList(_) => array_empty_dispatch::<i64>(&args[0]),
_ => internal_err!("array_empty does not support type '{array_type:?}'."),
}
}

fn array_empty_dispatch<O: OffsetSizeTrait>(array: &ArrayRef) -> Result<ArrayRef> {
let array = as_generic_list_array::<O>(array)?;
let builder = array
.iter()
.map(|arr| arr.map(|arr| arr.len() == arr.null_count()))
Expand Down
26 changes: 26 additions & 0 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3056,18 +3056,33 @@ select empty(make_array(1));
----
false

query B
select empty(arrow_cast(make_array(1), 'LargeList(Int64)'));
----
false

# empty scalar function #2
query B
select empty(make_array());
----
true

query B
select empty(arrow_cast(make_array(), 'LargeList(Null)'));
----
true

# empty scalar function #3
query B
select empty(make_array(NULL));
----
false

query B
select empty(arrow_cast(make_array(NULL), 'LargeList(Null)'));
----
false

# empty scalar function #4
query B
select empty(NULL);
Expand All @@ -3086,6 +3101,17 @@ NULL
false
false

query B
select empty(arrow_cast(column1, 'LargeList(List(Int64))')) from arrays;
----
false
false
false
false
NULL
false
false

query ?
SELECT string_to_array('abcxxxdef', 'xxx')
----
Expand Down

0 comments on commit e4662a1

Please sign in to comment.