Yibo Dong created SPARK-62276:
---------------------------------

             Summary: Correlated subquery with an outer reference in a FULL 
OUTER JOIN condition loses the unmatched rows of the join
                 Key: SPARK-62276
                 URL: https://issues.apache.org/jira/browse/SPARK-62276
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
         Environment: Stock Apache Spark 5.0.0-SNAPSHOT at commit 
082cf160890a0a0a748f70ffb5a993144b0de079, Java 25.0.1, macOS 15.7.4 aarch64, 
local[2], in-memory session catalog, built-in Parquet data source (V1), default 
SQL configuration.
            Reporter: Yibo Dong


h2. What happened

A correlated scalar or EXISTS subquery whose inner query is {{x FULL JOIN y ON 
... AND y.yk = o.k}} returns wrong results. A FULL OUTER JOIN preserves every 
row of both sides, so for each outer row the inner query must return the 
matched rows plus the unmatched rows of {{x}} and {{y}}. Spark returns only the 
matched rows: the scalar {{count(*)}} is 1 and 0 instead of 2 and 3, and an 
EXISTS that tests a preserved {{x}} row is false for every outer row. Running 
the inner query alone with the outer value written as a literal gives the 
correct counts.

h2. How to reproduce

{code:sql}
CREATE OR REPLACE TEMP VIEW o(k) AS VALUES (1), (2);
CREATE OR REPLACE TEMP VIEW x(xk, xv) AS VALUES (1, 'x1'), (2, 'x2');
CREATE OR REPLACE TEMP VIEW y(yk) AS VALUES (1);

-- Controls: the inner query with the outer value written as a literal.
SELECT count(*) FROM x FULL JOIN y ON x.xk = y.yk AND y.yk = 1;
SELECT count(*) FROM x FULL JOIN y ON x.xk = y.yk AND y.yk = 2;

-- Correlated scalar subquery: the outer column is referenced in the FULL JOIN 
condition.
SELECT k, (SELECT count(*) FROM x FULL JOIN y ON x.xk = y.yk AND y.yk = o.k) AS 
c
FROM o ORDER BY k;

-- Correlated EXISTS: the x row 'x2' is preserved by the FULL JOIN for every k.
SELECT k FROM o
WHERE EXISTS (SELECT 1 FROM x FULL JOIN y ON x.xk = y.yk AND y.yk = o.k WHERE 
x.xv = 'x2')
ORDER BY k;
{code}

Save the SQL as {{tmp.sql}} and run:

{code:bash}
spark-sql --master 'local[2]' --conf spark.sql.catalogImplementation=in-memory 
-f tmp.sql
{code}

h2. Expected result

The controls return 2 and 3. For {{k = 1}} the join produces {{(1, 1)}} and the 
unmatched {{(2, NULL)}}; for {{k = 2}} no pair satisfies the condition, so the 
join produces {{(1, NULL)}}, {{(2, NULL)}} and {{(NULL, 1)}}. The correlated 
scalar subquery returns the same counts:

{noformat}
+---+---+
|k  |c  |
+---+---+
|1  |2  |
|2  |3  |
+---+---+
{noformat}

The row {{x2}} is never matched and is preserved for both outer rows, so the 
EXISTS query returns both rows:

{noformat}
+---+
|k  |
+---+
|1  |
|2  |
+---+
{noformat}

h2. Actual result

The controls return 2 and 3. The correlated queries return:

{noformat}
+---+---+
|k  |c  |
+---+---+
|1  |1  |
|2  |0  |
+---+---+

+---+
|k  |
+---+
+---+
{noformat}



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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to