Weiwei Su created FLINK-41825:
---------------------------------
Summary: Correlated NOT EXISTS with IS NOT DISTINCT FROM returns
wrong rows because outer LeftOuterJoin uses = instead of IS NOT DISTINCT FROM
Key: FLINK-41825
URL: https://issues.apache.org/jira/browse/FLINK-41825
Project: Flink
Issue Type: Bug
Components: Table SQL / Planner
Affects Versions: 2.3.0
Environment: Apache Flink 2.3.0 (official image
flink:2.3.0-scala_2.12-java17, Scala 2.12, Java 17, Docker / Linux), Flink SQL
Client in batch mode (execution.target=local, execution.runtime-mode=batch).
Reporter: Weiwei Su
h3. Summary
In Apache Flink 2.3.0 (Table SQL, batch mode), a correlated {{NOT EXISTS}}
subquery using {{IS NOT DISTINCT FROM}} on a nullable column returns incorrect
rows when both the outer and inner tables contain {{NULL}} values for the join
key.
Specifically, during subquery decorrelation, Flink uses {{IS NOT DISTINCT
FROM(dept_id0, dept_id)}} in the inner join, but generates an outer
{{HashJoin(joinType=[LeftOuterJoin], where=[=(dept_id, dept_id0)])}} using
standard SQL equality ({{=}}) instead of {{IS NOT DISTINCT FROM}}. Because
{{NULL = NULL}} evaluates to {{UNKNOWN}}, the outer {{NULL}} row fails to match
the decorrelated {{NULL}} key, receives {{$f1 IS NULL}}, and is wrongly
retained by {{NOT EXISTS}}.
h3. Minimal Reproducer (Pure Apache Flink 2.3.0 SQL)
{code:sql}
SET 'execution.target' = 'local';
SET 'execution.runtime-mode' = 'batch';
SET 'sql-client.execution.result-mode' = 'tableau';
CREATE TABLE fa (
id INT,
dept_id INT
) WITH (
'connector' = 'filesystem',
'path' = 'file:///tmp/anti_a',
'format' = 'json'
);
CREATE TABLE fb (
dept_id INT,
flag BOOLEAN
) WITH (
'connector' = 'filesystem',
'path' = 'file:///tmp/anti_b',
'format' = 'json'
);
INSERT INTO fa
VALUES
(1, 10),
(2, CAST(NULL AS INT));
INSERT INTO fb
VALUES
(10, false),
(CAST(NULL AS INT), false);
SELECT COUNT(*) AS cnt
FROM fa AS a
WHERE NOT EXISTS (
SELECT 1
FROM fb AS b
WHERE a.dept_id IS NOT DISTINCT FROM b.dept_id
AND b.flag = false
);
-- Actual: 1 (row (2, NULL) wrongly returned)
-- Expected: 0 (both (1, 10) and (2, NULL) match fb under IS NOT DISTINCT FROM)
{code}
h3. Optimized Physical Plan (Root Cause Evidence)
{noformat}
Calc(select=[id], where=[IS NULL($f1)])
+- HashJoin(joinType=[LeftOuterJoin], where=[=(dept_id, dept_id0)], select=[id,
dept_id, dept_id0, $f1], build=[right])
:- Exchange(distribution=[hash[dept_id]])
: +- TableSourceScan(table=[[default_catalog, default_database, fa,
project=[id, dept_id], metadata=[]]], fields=[id, dept_id])
+- Calc(select=[dept_id0, true AS $f1])
+- HashJoin(joinType=[InnerJoin], where=[IS NOT DISTINCT FROM(dept_id0,
dept_id)], select=[dept_id, dept_id0], build=[left])
:- HashAggregate(isMerge=[true], groupBy=[dept_id], select=[dept_id])
: +- Exchange(distribution=[hash[dept_id]])
: +- LocalHashAggregate(groupBy=[dept_id], select=[dept_id])
: +- Calc(select=[dept_id], where=[NOT(flag)])
: +- TableSourceScan(table=[[default_catalog,
default_database, fb, filter=[NOT(flag)]]], fields=[dept_id, flag])
+- HashAggregate(isMerge=[true], groupBy=[dept_id], select=[dept_id])
+- Exchange(distribution=[hash[dept_id]])
+- LocalHashAggregate(groupBy=[dept_id], select=[dept_id])
+- Calc(select=[dept_id])
+- TableSourceScan(table=[[default_catalog,
default_database, fa, project=[id, dept_id], metadata=[]]], fields=[id,
dept_id])
{noformat}
Notice that the outer {{LeftOuterJoin}} uses {{where=[=(dept_id, dept_id0)]}}
instead of {{IS NOT DISTINCT FROM(dept_id, dept_id0)}}, breaking null-safe
decorrelation.
h3. Expected Behavior
The query should return {{0}} (consistent with Apache Spark 4.1.2, Trino 484,
and DuckDB 2.0.1). The decorrelated outer {{LeftOuterJoin}} must use {{IS NOT
DISTINCT FROM(dept_id, dept_id0)}} (or compile directly to a null-safe
{{LeftAntiJoin}}).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)