Weiwei Su created FLINK-41617:
---------------------------------

             Summary: Wrong result: INNER JOIN with empty right input returns 
non-empty left groups due to pushed-down empty-group HashAggregate(select=[]) 
and singleRowJoin=[true]
                 Key: FLINK-41617
                 URL: https://issues.apache.org/jira/browse/FLINK-41617
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 1.19.1
         Environment: Apache Flink 1.19.1 (SQL Gateway, batch execution mode, 
Apache Iceberg 1.8.1 table source); cross-checked against DuckDB, Trino 470, 
and Spark 3.5.5 on the exact same frozen Iceberg snapshot

            Reporter: Weiwei Su


h3. Summary
In Flink SQL batch mode (reproduced 3/3 times on Flink 1.19.1 with an Iceberg 
table source), an {{INNER JOIN ... ON TRUE}} where a {{WHERE}} filter makes the 
right input completely empty ({{0}} rows) silently returns *wrong non-empty 
results* when combined with {{GROUP BY}} or {{SELECT DISTINCT}} on a left-side 
column.

On the exact same frozen Iceberg snapshot (4 rows: {{'west'}}, {{'west'}}, 
{{'east'}}, {{'east'}}; no {{NULL}} in {{region}}):
* *DuckDB, Trino 470, and Spark 3.5.5* all return {{0}} rows ({{[]}}).
* *Flink 1.19.1* stably returns 2 rows: {{['west'], ['east']}} (and {{SELECT 
t2.region, COUNT(*)}} returns {{['west', 0], ['east', 0]}}).

h3. Minimal Reproducer
Given a table {{t(order_id BIGINT, v INT, region STRING)}} containing 4 rows 
{{(1,11,'west'), (2,12,'west'), (5,15,'east'), (6,16,'east')}} (no {{NULL}} 
values in {{region}}):

{code:sql}
SELECT t2.region
FROM ld_pd_target_20261006a /*+ OPTIONS('snapshot-id'='2218517376986569101') */ 
AS t2
JOIN ld_pd_target_20261006a /*+ OPTIONS('snapshot-id'='2218517376986569101') */ 
AS t3
  ON TRUE
WHERE t3.region IS NULL
GROUP BY t2.region;
{code}

*Observed Flink 1.19.1 output (3/3 runs):*
{code:text}
['west']
['east']
{code}

*Other affected variants (all fail with wrong non-empty rows):*
* {{SELECT DISTINCT t2.region FROM ... JOIN ... ON TRUE WHERE t3.region IS 
NULL}} -> returns {{['west'], ['east']}}
* {{SELECT t2.region, COUNT(*) FROM ... JOIN ... ON TRUE WHERE t3.region IS 
NULL GROUP BY t2.region}} -> returns {{['west', 0], ['east', 0]}}

*(Note: A pure {{VALUES}} control returns {{[]}} because constant folding folds 
{{t3.region IS NULL}} to an empty {{Values}} node before aggregate-join 
transpose runs. A non-constant table source exposes the optimizer bug.)*

h3. Flink EXPLAIN Plan & Root Cause
{code:text}
== Optimized Physical Plan ==
NestedLoopJoin(joinType=[InnerJoin], where=[true], select=[region], 
build=[right], singleRowJoin=[true])
:- HashAggregate(isMerge=[true], groupBy=[region], select=[region])
:  +- Exchange(distribution=[hash[region]])
:     +- LocalHashAggregate(groupBy=[region], select=[region])
:        +- TableSourceScan(table=[[iceberg, default, ld_pd_target_20261006a, 
project=[region]]], fields=[region], ...)
+- Exchange(distribution=[broadcast])
   +- HashAggregate(isMerge=[true], select=[])
      +- Exchange(distribution=[single])
         +- LocalHashAggregate(select=[])
            +- Calc(select=[order_id], where=[IS NULL(region)])
               +- TableSourceScan(table=[[iceberg, default, 
ld_pd_target_20261006a, filter=[IS NULL(region)], project=[order_id, region]]], 
fields=[order_id, region], ...)
{code}

*Root cause:* Pushing {{Aggregate(group=[{0}])}} below the {{InnerJoin}} 
introduces an empty-group global aggregate {{HashAggregate(isMerge=[true], 
select=[])}} on the right branch and marks the join {{singleRowJoin=[true]}}. 
When the right input is empty ({{0}} rows after {{IS NULL(region)}}), the 
global empty-group aggregate emits *1 row* instead of {{0}} rows, causing the 
{{InnerJoin}} to spuriously preserve all left-side groups.

h3. Expected Behavior
Because {{t3.region IS NULL}} filters the right side of the {{INNER JOIN}} to 
{{0}} rows, the query must return {{0}} rows (matching DuckDB, Trino, and 
Spark).




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to