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 a894f70
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) --> null
// null not in (x, y, z) --> null
Expr::InList(InList {
expr,
list: _,
negated: _,
}) if is_null(&expr) => lit_bool_null(),

// 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 (...) --> null
assert_eq!(
simplify(in_list(lit_bool_null(), vec![col("c1"), lit(1)], false)),
lit_bool_null()
);

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

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

0 comments on commit a894f70

Please sign in to comment.