Skip to content

Commit

Permalink
feat: simplify null in list
Browse files Browse the repository at this point in the history
  • Loading branch information
asimsedhain committed Dec 31, 2023
1 parent 6403222 commit 7fcfb91
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
lit(negated)
}

// null in (x, y, z) --> false
// null not in (x, y, z) --> true
Expr::InList(InList {
expr,
list: _,
negated,
}) if is_null(&expr) => lit(negated),

// expr IN ((subquery)) -> expr IN (subquery), see ##5529
Expr::InList(InList {
expr,
Expand Down Expand Up @@ -3096,6 +3104,18 @@ mod tests {
assert_eq!(simplify(in_list(col("c1"), vec![], false)), lit(false));
assert_eq!(simplify(in_list(col("c1"), vec![], true)), lit(true));

// null in (...) is always false
assert_eq!(
simplify(in_list(lit_bool_null(), vec![col("c1"), lit(1)], false)),
lit(false)
);

// null not in (...) is always true
assert_eq!(
simplify(in_list(lit_bool_null(), vec![col("c1"), lit(1)], true)),
lit(true)
);

assert_eq!(
simplify(in_list(col("c1"), vec![lit(1)], false)),
col("c1").eq(lit(1))
Expand Down

0 comments on commit 7fcfb91

Please sign in to comment.