Skip to content

Commit

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

// expr in (null, x, y, z) --> expr in (x, y, z)
// expr not in (null, x, y, z) --> expr not in (x, y, z)
Expr::InList(InList {
expr,
list,
negated,
}) if !is_null(&expr) => Expr::InList(InList {
expr,
list: list.into_iter().filter(|expr| !is_null(expr)).collect(),
negated,
}),

//
// Rules for NotEq
//
Expand Down Expand Up @@ -3116,6 +3129,27 @@ mod tests {
lit(true)
);

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


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

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

0 comments on commit 665ca39

Please sign in to comment.