Skip to content

Apache DataFusion SQL Query Engine

Fix incorrect `NULL IN ()` optimization (#17092)

Piotr Findeisen9463cf679246
diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
index 4f63697ff9..0a68734d32 100644
--- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
+++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
@@ -1662,20 +1662,20 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
// expr IN () --> false
// expr NOT IN () --> true
Expr::InList(InList {
- expr,
+ expr: _,
list,
negated,
- }) if list.is_empty() && *expr != Expr::Literal(ScalarValue::Null, None) => {
- Transformed::yes(lit(negated))
- }
+ }) if list.is_empty() => Transformed::yes(lit(negated)),
// null in (x, y, z) --> null
// null not in (x, y, z) --> null
Expr::InList(InList {
expr,
- list: _,
+ list,
negated: _,
- }) if is_null(expr.as_ref()) => Transformed::yes(lit_bool_null()),
+ }) if is_null(expr.as_ref()) && !list.is_empty() => {
+ Transformed::yes(lit_bool_null())
+ }
// expr IN ((subquery)) -> expr IN (subquery), see ##5529
Expr::InList(InList {
@@ -3948,6 +3948,56 @@ mod tests {
assert_eq!(simplify(expr.clone()), expr);
}
+ #[test]
+ fn simplify_null_in_empty_inlist() {
+ // `NULL::boolean IN ()` == `NULL::boolean IN (SELECT foo FROM empty)` == false
+ let expr = in_list(lit_bool_null(), vec![], false);
+ assert_eq!(simplify(expr), lit(false));
+
+ // `NULL::boolean NOT IN ()` == `NULL::boolean NOT IN (SELECT foo FROM empty)` == true
+ let expr = in_list(lit_bool_null(), vec![], true);
+ assert_eq!(simplify(expr), lit(true));
+
+ // `NULL IN ()` == `NULL IN (SELECT foo FROM empty)` == false
+ let null_null = || Expr::Literal(ScalarValue::Null, None);
+ let expr = in_list(null_null(), vec![], false);
+ assert_eq!(simplify(expr), lit(false));
+
+ // `NULL NOT IN ()` == `NULL NOT IN (SELECT foo FROM empty)` == true
+ let expr = in_list(null_null(), vec![], true);
+ assert_eq!(simplify(expr), lit(true));
+ }
+
+ #[test]
+ fn just_simplifier_simplify_null_in_empty_inlist() {
+ let simplify = |expr: Expr| -> Expr {
+ let schema = expr_test_schema();
+ let execution_props = ExecutionProps::new();
+ let info = SimplifyContext::new(&execution_props).with_schema(schema);
+ let simplifier = &mut Simplifier::new(&info);
+ expr.rewrite(simplifier)
+ .expect("Failed to simplify expression")
+ .data
+ };
+
+ // `NULL::boolean IN ()` == `NULL::boolean IN (SELECT foo FROM empty)` == false
+ let expr = in_list(lit_bool_null(), vec![], false);
+ assert_eq!(simplify(expr), lit(false));
+
+ // `NULL::boolean NOT IN ()` == `NULL::boolean NOT IN (SELECT foo FROM empty)` == true
+ let expr = in_list(lit_bool_null(), vec![], true);
+ assert_eq!(simplify(expr), lit(true));
+
+ // `NULL IN ()` == `NULL IN (SELECT foo FROM empty)` == false
+ let null_null = || Expr::Literal(ScalarValue::Null, None);
+ let expr = in_list(null_null(), vec![], false);
+ assert_eq!(simplify(expr), lit(false));
+
+ // `NULL NOT IN ()` == `NULL NOT IN (SELECT foo FROM empty)` == true
+ let expr = in_list(null_null(), vec![], true);
+ assert_eq!(simplify(expr), lit(true));
+ }
+
#[test]
fn simplify_large_or() {
let expr = (0..5)
diff --git a/datafusion/sqllogictest/test_files/predicates.slt b/datafusion/sqllogictest/test_files/predicates.slt
index b4b31fa78a..f4bf75c83c 100644
--- a/datafusion/sqllogictest/test_files/predicates.slt
+++ b/datafusion/sqllogictest/test_files/predicates.slt
@@ -811,5 +811,26 @@ explain select x from t where x NOT IN (1,2,3,4,5) AND x IN (1,2,3);
logical_plan EmptyRelation
physical_plan EmptyExec
+query error DataFusion error: This feature is not implemented: Physical plan does not support logical expression InSubquery\(InSubquery \{ expr: Literal\(Int64\(NULL\), None\), subquery: <subquery>, negated: false \}\)
+WITH empty AS (SELECT 10 WHERE false)
+SELECT
+ NULL IN (SELECT * FROM empty), -- should be false, as the right side is empty relation
+ NULL NOT IN (SELECT * FROM empty) -- should be true, as the right side is empty relation
+FROM (SELECT 1) t;
+
+query I
+WITH empty AS (SELECT 10 WHERE false)
+SELECT * FROM (SELECT 1) t
+WHERE NOT (NULL IN (SELECT * FROM empty)); -- all rows should be returned
+----
+1
+
+query I
+WITH empty AS (SELECT 10 WHERE false)
+SELECT * FROM (SELECT 1) t
+WHERE NULL NOT IN (SELECT * FROM empty); -- all rows should be returned
+----
+1
+
statement ok
drop table t;