Skip to content

Commit 94b68cb

Browse files
authored
fix: correct edge case where null haystack returns false instead of null (#17818)
* fix: correct edge case where haystack with null element returns false instead of null * clippy
1 parent f1246a9 commit 94b68cb

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

datafusion/functions-nested/src/array_has.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn array_has_dispatch_for_scalar(
333333
let is_nested = values.data_type().is_nested();
334334
// If first argument is empty list (second argument is non-null), return false
335335
// i.e. array_has([], non-null element) -> false
336-
if values.is_empty() {
336+
if haystack.len() == 0 {
337337
return Ok(Arc::new(BooleanArray::new(
338338
BooleanBuffer::new_unset(haystack.len()),
339339
None,
@@ -658,11 +658,20 @@ fn general_array_has_all_and_any_kernel(
658658

659659
#[cfg(test)]
660660
mod tests {
661-
use arrow::array::create_array;
662-
use datafusion_common::utils::SingleRowListArrayBuilder;
661+
use std::sync::Arc;
662+
663+
use arrow::{
664+
array::{create_array, Array, ArrayRef, AsArray, Int32Array, ListArray},
665+
buffer::OffsetBuffer,
666+
datatypes::{DataType, Field},
667+
};
668+
use datafusion_common::{
669+
config::ConfigOptions, utils::SingleRowListArrayBuilder, DataFusionError,
670+
ScalarValue,
671+
};
663672
use datafusion_expr::{
664-
col, execution_props::ExecutionProps, lit, simplify::ExprSimplifyResult, Expr,
665-
ScalarUDFImpl,
673+
col, execution_props::ExecutionProps, lit, simplify::ExprSimplifyResult,
674+
ColumnarValue, Expr, ScalarFunctionArgs, ScalarUDFImpl,
666675
};
667676

668677
use crate::expr_fn::make_array;
@@ -737,4 +746,44 @@ mod tests {
737746

738747
assert_eq!(args, vec![col("c1"), col("c2")],);
739748
}
749+
750+
#[test]
751+
fn test_array_has_list_empty_child() -> Result<(), DataFusionError> {
752+
let haystack_field = Arc::new(Field::new_list(
753+
"haystack",
754+
Field::new_list("", Field::new("", DataType::Int32, true), true),
755+
true,
756+
));
757+
let needle_field = Arc::new(Field::new("needle", DataType::Int32, true));
758+
let return_field = Arc::new(Field::new_list(
759+
"return",
760+
Field::new("", DataType::Boolean, true),
761+
true,
762+
));
763+
764+
let haystack = ListArray::new(
765+
Field::new_list_field(DataType::Int32, true).into(),
766+
OffsetBuffer::new(vec![0, 0].into()),
767+
Arc::new(Int32Array::from(Vec::<i32>::new())) as ArrayRef,
768+
Some(vec![false].into()),
769+
);
770+
771+
let haystack = ColumnarValue::Array(Arc::new(haystack));
772+
let needle = ColumnarValue::Scalar(ScalarValue::Int32(Some(1)));
773+
774+
let result = ArrayHas::new().invoke_with_args(ScalarFunctionArgs {
775+
args: vec![haystack, needle],
776+
arg_fields: vec![haystack_field, needle_field],
777+
number_rows: 1,
778+
return_field,
779+
config_options: Arc::new(ConfigOptions::default()),
780+
})?;
781+
782+
let output = result.into_array(1)?;
783+
let output = output.as_boolean();
784+
assert_eq!(output.len(), 1);
785+
assert!(output.is_null(0));
786+
787+
Ok(())
788+
}
740789
}

0 commit comments

Comments
 (0)