Skip to content

Commit 0a7b3a9

Browse files
committed
Fix incorrect NULL IN () optimization
1 parent 3aded9c commit 0a7b3a9

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,20 +1663,20 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
16631663
// expr IN () --> false
16641664
// expr NOT IN () --> true
16651665
Expr::InList(InList {
1666-
expr,
1666+
expr: _,
16671667
list,
16681668
negated,
1669-
}) if list.is_empty() && *expr != Expr::Literal(ScalarValue::Null, None) => {
1670-
Transformed::yes(lit(negated))
1671-
}
1669+
}) if list.is_empty() => Transformed::yes(lit(negated)),
16721670

16731671
// null in (x, y, z) --> null
16741672
// null not in (x, y, z) --> null
16751673
Expr::InList(InList {
16761674
expr,
1677-
list: _,
1675+
list,
16781676
negated: _,
1679-
}) if is_null(expr.as_ref()) => Transformed::yes(lit_bool_null()),
1677+
}) if is_null(expr.as_ref()) && !list.is_empty() => {
1678+
Transformed::yes(lit_bool_null())
1679+
}
16801680

16811681
// expr IN ((subquery)) -> expr IN (subquery), see ##5529
16821682
Expr::InList(InList {

datafusion/sqllogictest/test_files/predicates.slt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,5 +811,26 @@ explain select x from t where x NOT IN (1,2,3,4,5) AND x IN (1,2,3);
811811
logical_plan EmptyRelation
812812
physical_plan EmptyExec
813813

814+
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 \}\)
815+
WITH empty AS (SELECT 10 WHERE false)
816+
SELECT
817+
NULL IN (SELECT * FROM empty), -- should be false, as the right side is empty relation
818+
NULL NOT IN (SELECT * FROM empty) -- should be true, as the right side is empty relation
819+
FROM (SELECT 1) t;
820+
821+
query I
822+
WITH empty AS (SELECT 10 WHERE false)
823+
SELECT * FROM (SELECT 1) t
824+
WHERE NOT (NULL IN (SELECT * FROM empty)); -- all rows should be returned
825+
----
826+
1
827+
828+
query I
829+
WITH empty AS (SELECT 10 WHERE false)
830+
SELECT * FROM (SELECT 1) t
831+
WHERE NULL NOT IN (SELECT * FROM empty); -- all rows should be returned
832+
----
833+
1
834+
814835
statement ok
815836
drop table t;

0 commit comments

Comments
 (0)