Skip to content

Commit 2d9414a

Browse files
authored
list_contains([], null) = false (#10028)
list_contains returned false for empty list and null needle in array case but null in scalar case. Make the semantics the same in both Signed-off-by: Mikhail Kot <[email protected]>
1 parent 32895ce commit 2d9414a

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

  • vortex-array/src/scalar_fn/fns/list_contains

‎vortex-array/src/scalar_fn/fns/list_contains/mod.rs‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ impl ScalarFnVTable for ListContains {
146146
fn compute_contains_scalar(list: &Scalar, needle: &Scalar) -> VortexResult<Scalar> {
147147
let nullability = list.dtype().nullability() | needle.dtype().nullability();
148148

149-
// Handle null list or null needle
150-
if list.is_null() || needle.is_null() {
149+
if list.is_null() {
151150
return Ok(Scalar::null(DType::Bool(nullability)));
152151
}
153152

@@ -156,6 +155,14 @@ fn compute_contains_scalar(list: &Scalar, needle: &Scalar) -> VortexResult<Scala
156155
.elements()
157156
.ok_or_else(|| vortex_err!("Expected non-null list"))?;
158157

158+
if elements.is_empty() {
159+
return Ok(Scalar::bool(false, nullability));
160+
}
161+
162+
if needle.is_null() {
163+
return Ok(Scalar::null(DType::Bool(nullability)));
164+
}
165+
159166
let contains = elements.iter().any(|elem| elem == needle);
160167
Ok(Scalar::bool(contains, nullability))
161168
}
@@ -680,7 +687,35 @@ mod tests {
680687
);
681688
}
682689

683-
// -- Tests migrated from compute/list_contains.rs --
690+
#[test]
691+
fn test_null_in_empty_list() -> VortexResult<()> {
692+
let mut ctx = array_session().create_execution_ctx();
693+
let array = test_array();
694+
let dtype = Arc::new(DType::Primitive(I32, Nullability::NonNullable));
695+
696+
let list = Scalar::list(Arc::clone(&dtype), vec![], Nullability::NonNullable);
697+
let needle = Scalar::null(DType::Primitive(I32, Nullability::Nullable));
698+
699+
let expr = list_contains(lit(list.clone()), lit(needle.clone()));
700+
let result = array.clone().apply(&expr)?;
701+
assert_eq!(
702+
result.execute_scalar(0, &mut ctx)?,
703+
Scalar::bool(false, Nullability::Nullable)
704+
);
705+
706+
let expr = list_contains(lit(list), lit(2i32));
707+
let result = array.clone().apply(&expr)?;
708+
assert_eq!(
709+
result.execute_scalar(0, &mut ctx)?,
710+
Scalar::bool(false, Nullability::NonNullable)
711+
);
712+
713+
let list = Scalar::null(DType::List(dtype, Nullability::Nullable));
714+
let expr = list_contains(lit(list), lit(needle));
715+
let result = array.apply(&expr)?;
716+
assert!(result.execute_scalar(0, &mut ctx)?.is_null());
717+
Ok(())
718+
}
684719

685720
fn nonnull_strings(values: Vec<Vec<&str>>) -> ArrayRef {
686721
let mut ctx = array_session().create_execution_ctx();

0 commit comments

Comments
 (0)