Skip to content

Commit

Permalink
fix length error with array_has (#12459)
Browse files Browse the repository at this point in the history
* fix length error with array_has

* move test

* add license

* move test

* add pure sql reproduction

* bump ci

* update slt, remove dedicated test
  • Loading branch information
samuelcolvin committed Sep 14, 2024
1 parent befac37 commit 6590ea3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion datafusion/functions-nested/src/array_has.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use arrow::array::{Array, ArrayRef, BooleanArray, OffsetSizeTrait};
use arrow::datatypes::DataType;
use arrow::row::{RowConverter, Rows, SortField};
use arrow_array::{Datum, GenericListArray, Scalar};
use arrow_buffer::BooleanBuffer;
use datafusion_common::cast::as_generic_list_array;
use datafusion_common::utils::string_utils::string_array_to_vec;
use datafusion_common::{exec_err, Result, ScalarValue};
Expand Down Expand Up @@ -200,7 +201,10 @@ fn array_has_dispatch_for_scalar<O: OffsetSizeTrait>(
// If first argument is empty list (second argument is non-null), return false
// i.e. array_has([], non-null element) -> false
if values.len() == 0 {
return Ok(Arc::new(BooleanArray::from(vec![Some(false)])));
return Ok(Arc::new(BooleanArray::new(
BooleanBuffer::new_unset(haystack.len()),
None,
)));
}
let eq_array = compare_with_eq(values, needle, is_nested)?;
let mut final_contained = vec![None; haystack.len()];
Expand Down
13 changes: 13 additions & 0 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -7086,6 +7086,16 @@ select [1,2,3]::int[], [['1']]::int[][], arrow_typeof([]::text[]);
----
[1, 2, 3] [[1]] List(Field { name: "item", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })

# test empty arrays return length
# issue: https://github.com/apache/datafusion/pull/12459
statement ok
create table values_all_empty (a int[]) as values ([]), ([]);

query B
select array_has(a, 1) from values_all_empty;
----
false
false

### Delete tables

Expand Down Expand Up @@ -7259,3 +7269,6 @@ drop table fixed_size_arrays_values_without_nulls;

statement ok
drop table test_create_array_table;

statement ok
drop table values_all_empty;

0 comments on commit 6590ea3

Please sign in to comment.