Skip to content

Commit 6590ea3

Browse files
authored
fix length error with array_has (#12459)
* fix length error with array_has * move test * add license * move test * add pure sql reproduction * bump ci * update slt, remove dedicated test
1 parent befac37 commit 6590ea3

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

datafusion/functions-nested/src/array_has.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use arrow::array::{Array, ArrayRef, BooleanArray, OffsetSizeTrait};
2121
use arrow::datatypes::DataType;
2222
use arrow::row::{RowConverter, Rows, SortField};
2323
use arrow_array::{Datum, GenericListArray, Scalar};
24+
use arrow_buffer::BooleanBuffer;
2425
use datafusion_common::cast::as_generic_list_array;
2526
use datafusion_common::utils::string_utils::string_array_to_vec;
2627
use datafusion_common::{exec_err, Result, ScalarValue};
@@ -200,7 +201,10 @@ fn array_has_dispatch_for_scalar<O: OffsetSizeTrait>(
200201
// If first argument is empty list (second argument is non-null), return false
201202
// i.e. array_has([], non-null element) -> false
202203
if values.len() == 0 {
203-
return Ok(Arc::new(BooleanArray::from(vec![Some(false)])));
204+
return Ok(Arc::new(BooleanArray::new(
205+
BooleanBuffer::new_unset(haystack.len()),
206+
None,
207+
)));
204208
}
205209
let eq_array = compare_with_eq(values, needle, is_nested)?;
206210
let mut final_contained = vec![None; haystack.len()];

datafusion/sqllogictest/test_files/array.slt

+13
Original file line numberDiff line numberDiff line change
@@ -7086,6 +7086,16 @@ select [1,2,3]::int[], [['1']]::int[][], arrow_typeof([]::text[]);
70867086
----
70877087
[1, 2, 3] [[1]] List(Field { name: "item", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })
70887088

7089+
# test empty arrays return length
7090+
# issue: https://github.com/apache/datafusion/pull/12459
7091+
statement ok
7092+
create table values_all_empty (a int[]) as values ([]), ([]);
7093+
7094+
query B
7095+
select array_has(a, 1) from values_all_empty;
7096+
----
7097+
false
7098+
false
70897099

70907100
### Delete tables
70917101

@@ -7259,3 +7269,6 @@ drop table fixed_size_arrays_values_without_nulls;
72597269

72607270
statement ok
72617271
drop table test_create_array_table;
7272+
7273+
statement ok
7274+
drop table values_all_empty;

0 commit comments

Comments
 (0)