Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 31 additions & 4 deletions dozer-sql/expression/src/in_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,44 @@ pub(crate) fn evaluate_in_list(
record: &Record,
) -> Result<Field, Error> {
let field = expr.evaluate(record, schema)?;
let mut result = false;
let mut match_found = false;
let mut list_contains_null = false;

if field == Field::Null {
return Ok(Field::Null);
}

for item in list {
let item = item.evaluate(record, schema)?;
if item == Field::Null {
list_contains_null = true;
continue;
}

if field == item {
result = true;
match_found = true;
break;
}
}

let result_field = if match_found {
Field::Boolean(true)
} else if list_contains_null {
// No exact match, but NULL was present in the list -> UNKNOWN
Field::Null
} else {
// No exact match, and no NULLs in the list -> FALSE
Field::Boolean(false)
};

// Negate the result if the IN list was negated.
if negated {
result = !result;
match result_field {
Field::Boolean(b) => Ok(Field::Boolean(!b)),
Field::Null => Ok(Field::Null), // NOT NULL is NULL
_ => unreachable!(),
}
} else {
Ok(result_field)
}
Ok(Field::Boolean(result))
}
72 changes: 72 additions & 0 deletions dozer-sql/src/expression/tests/in_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,75 @@ fn test_not_in_list() {
);
assert_eq!(f, Field::Boolean(false));
}

#[test]
fn test_in_list_with_nulls() {
let null_field = Field::Null;

// 1. 42 IN (NULL) -> Field::Null
let f = run_fct("SELECT 42 IN (NULL)", Schema::default(), vec![]);
assert_eq!(f, null_field);

// 2. 42 IN (1, NULL) -> Field::Null
let f = run_fct("SELECT 42 IN (1, NULL)", Schema::default(), vec![]);
assert_eq!(f, null_field);

// 3. 42 IN (1, NULL, 42) -> Field::Boolean(true) (Exact match found)
let f = run_fct("SELECT 42 IN (1, NULL, 42)", Schema::default(), vec![]);
assert_eq!(f, Field::Boolean(true));

// 4. 42 NOT IN (1, NULL) -> Field::Null
let f = run_fct("SELECT 42 NOT IN (1, NULL)", Schema::default(), vec![]);
assert_eq!(f, null_field);

// 5. NULL IN (1, 2, 3) -> Field::Null
let f = run_fct("SELECT NULL IN (1, 2, 3)", Schema::default(), vec![]);
assert_eq!(f, null_field);

// 6. No exact match, list contains NULL -> Field::Null
let f = run_fct("SELECT 99 IN (1, 2, NULL)", Schema::default(), vec![]);
assert_eq!(f, null_field);

// 7. NOT IN (1, 2, 3) -> True (42 is not in list, no NULLs)
let f = run_fct("SELECT 42 NOT IN (1, 2, 3)", Schema::default(), vec![]);
assert_eq!(f, Field::Boolean(true));

// 8. NOT IN (1, 2, 3, 42) -> False (42 is in list)
let f = run_fct("SELECT 42 NOT IN (1, 2, 3, 42)", Schema::default(), vec![]);
assert_eq!(f, Field::Boolean(false));

// 9. NOT IN (1, 2, 3, 42, NULL) -> False (42 matches, so NOT(true) == false)
let f = run_fct("SELECT 42 NOT IN (1, 2, 3, 42, NULL)", Schema::default(), vec![]);
assert_eq!(f, Field::Boolean(false));

// 10. NOT IN (1, 2, 3, NULL) with no match -> Null
let f = run_fct("SELECT 42 NOT IN (1, 2, 3, NULL)", Schema::default(), vec![]);
assert_eq!(f, null_field);

// 11. Column expression evaluates to NULL in record
let schema = Schema::default()
.field(
FieldDefinition::new(
String::from("age"),
FieldType::Int,
true,
SourceDefinition::Dynamic,
),
false,
)
.clone();
let f = run_fct(
"SELECT age IN (1, 2, 3) FROM users",
schema.clone(),
vec![Field::Null],
);
assert_eq!(f, null_field);

// 12. Column expression matches with NULL in list
let f = run_fct(
"SELECT age IN (1, NULL, 42) FROM users",
schema,
vec![Field::Int(42)],
);
assert_eq!(f, Field::Boolean(true));
}