Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify NULL [NOT] IN (..) expressions #8691

Merged
Merged
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked that this is consistent with posgres:

postgres=# select null IN (1,2);
 ?column?
----------

(1 row)

postgres=# select null IN (NULL);
 ?column?
----------

(1 row)

postgres=# select null NOT IN (1,2);
 ?column?
----------

(1 row)

postgres=# select null NOT IN (NULL,1,2);
 ?column?
----------

(1 row)

// 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