Yibo Dong created SPARK-62280:
---------------------------------
Summary: SELECT DISTINCT returns duplicate rows when hidden join
keys or file metadata columns are referenced
Key: SPARK-62280
URL: https://issues.apache.org/jira/browse/SPARK-62280
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
SELECT DISTINCT returns duplicate rows when a later operator references a
hidden column that is not part of the SELECT list.
This occurs with the hidden right-side key of a USING join and with Parquet
file metadata columns. Spark adds the referenced column below DISTINCT,
changing the deduplication key and preserving rows that should have been
deduplicated.
h2. How to reproduce
Run the following SQL in a fresh Spark SQL session:
{code:sql}
CREATE OR REPLACE TEMP VIEW l(id, b) AS
VALUES (1, 10), (2, 20);
CREATE OR REPLACE TEMP VIEW r(b, y) AS
VALUES (10, 'p'), (20, 'p');
-- Control: DISTINCT alone returns one row.
SELECT DISTINCT y FROM l JOIN r USING (b);
-- Q1: ORDER BY a hidden USING join key.
SELECT DISTINCT y
FROM l JOIN r USING (b)
ORDER BY r.b;
-- Q2: Pipe filter after DISTINCT.
FROM l JOIN r USING (b)
|> SELECT DISTINCT y
|> WHERE r.b > 0;
-- File metadata cases.
CREATE TABLE pt (a INT) USING parquet;
INSERT INTO pt VALUES (1);
INSERT INTO pt VALUES (1);
-- Control: one distinct row.
SELECT DISTINCT a FROM pt;
-- Q3: ORDER BY a file metadata column.
SELECT DISTINCT a FROM pt
ORDER BY _metadata.file_name;
-- Q4: Outer filter on file metadata after DISTINCT.
SELECT count(*) AS n
FROM (SELECT DISTINCT a FROM pt)
WHERE _metadata.file_size > 0;
-- Control: an unselected column from an ON join is rejected.
SELECT DISTINCT y
FROM l JOIN r ON l.b = r.b
ORDER BY r.b;
DROP TABLE pt;
{code}
Save as tmp.sql and execute:
{code:bash}
spark-sql --master 'local[2]' --conf spark.sql.catalogImplementation=in-memory
-f tmp.sql
{code}
h2. Expected result
SELECT DISTINCT must eliminate duplicate rows based on the SELECT list.
Q1 and Q2 should return at most one row containing 'p'. Q3 should return at
most one row containing 1. Q4 should return 1 if the metadata reference is
permitted and evaluated without changing the distinct rows.
Alternatively, Spark may reject references to columns unavailable after
DISTINCT, as it does for the ON join control. It must not silently change the
deduplication key.
h2. Actual result
Both DISTINCT-only controls return one row, and the ON join control fails with
UNRESOLVED_COLUMN.WITH_SUGGESTION.
Q1 and Q2 each return:
{noformat}
+---+
|y |
+---+
|p |
|p |
+---+
{noformat}
Q3 returns:
{noformat}
+---+
|a |
+---+
|1 |
|1 |
+---+
{noformat}
Q4 returns:
{noformat}
+---+
|n |
+---+
|2 |
+---+
{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]