Yibo Dong created SPARK-62279:
---------------------------------

             Summary: Correlated CAST predicates split aggregates and duplicate 
outer rows
                 Key: SPARK-62279
                 URL: https://issues.apache.org/jira/browse/SPARK-62279
             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


A correlated equality involving a CAST expression under an aggregate or window 
can produce incorrect results when the cast does not preserve the grouping 
equivalence of its input.

For example, CAST(timestamp AS BIGINT) maps distinct timestamps within the same 
second to the same value. Spark incorrectly groups by the original timestamp 
after decorrelation, splitting the aggregate and duplicating outer rows.

The same problem occurs with narrowing day-time interval casts and CAST(double 
AS STRING) involving positive and negative zero.

h2. How to reproduce

Run the following SQL in a fresh Spark SQL session.

{code:sql}
CREATE OR REPLACE TEMP VIEW o(sec) AS
VALUES (CAST(100 AS BIGINT)), (CAST(200 AS BIGINT)), (CAST(300 AS BIGINT));

CREATE OR REPLACE TEMP VIEW s(ts, x) AS VALUES
(timestamp_seconds(100), 1),
(timestamp_seconds(100.5), 2),
(timestamp_seconds(200), 3);

-- Controls: replace the outer reference with a literal.
SELECT count(), sum(x) FROM s WHERE CAST(s.ts AS BIGINT) = 100;
SELECT count(), sum(x) FROM s WHERE CAST(s.ts AS BIGINT) = 200;
SELECT count(*), sum(x) FROM s WHERE CAST(s.ts AS BIGINT) = 300;

-- Q1: correlated scalar aggregates.
SELECT sec,
(SELECT count(*) FROM s WHERE CAST(s.ts AS BIGINT) = o.sec) AS c,
(SELECT sum(x) FROM s WHERE CAST(s.ts AS BIGINT) = o.sec) AS sx
FROM o ORDER BY sec;

-- Q2: lateral aggregate.
SELECT sec, t.c
FROM o, LATERAL (
SELECT count(*) AS c
FROM s WHERE CAST(s.ts AS BIGINT) = o.sec
) t
ORDER BY sec, c;

-- Q3: lateral window.
SELECT sec, t.x, t.n
FROM o, LATERAL (
SELECT x, count(*) OVER () AS n
FROM s WHERE CAST(s.ts AS BIGINT) = o.sec
) t
ORDER BY sec, x;

-- Q4: narrowing interval cast.
CREATE OR REPLACE TEMP VIEW o2(d) AS
VALUES (INTERVAL '1' DAY), (INTERVAL '2' DAY);

CREATE OR REPLACE TEMP VIEW s2(iv) AS VALUES
(INTERVAL '1 00:00:00' DAY TO SECOND),
(INTERVAL '1 12:00:00' DAY TO SECOND),
(INTERVAL '2 01:00:00' DAY TO SECOND);

SELECT d,
(SELECT count(*) FROM s2
WHERE CAST(s2.iv AS INTERVAL DAY) = o2.d) AS c
FROM o2 ORDER BY d;

-- Q5: DOUBLE to STRING with positive and negative zero.
CREATE OR REPLACE TEMP VIEW o3(ks) AS VALUES ('0.0'), ('-0.0');

CREATE OR REPLACE TEMP VIEW s3(dv) AS
VALUES (CAST(0.0 AS DOUBLE)), (-CAST(0.0 AS DOUBLE));

SELECT ks,
(SELECT count() FROM s3 WHERE CAST(s3.dv AS STRING) = '0.0') AS c_literal_0,
(SELECT count() FROM s3 WHERE CAST(s3.dv AS STRING) = '-0.0') AS c_literal_neg0,
(SELECT count(*) FROM s3 WHERE CAST(s3.dv AS STRING) = o3.ks) AS c
FROM o3 ORDER BY ks;
{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

The literal controls return (2, 3), (1, 3), and (0, NULL).

Q1 should return exactly one row per outer row:

{noformat}
sec | c | sx
----+---+-----
100 | 2 | 3
200 | 1 | 3
300 | 0 | NULL
{noformat}

Q2 should return (100, 2), (200, 1), (300, 0).

Q3 should return (100, 1, 2), (100, 2, 2), (200, 3, 1), since both timestamps 
in second 100 belong to the same window.

Q4 should return:

{noformat}
d                | c
-----------------+---
INTERVAL '1' DAY | 2
INTERVAL '2' DAY | 1
{noformat}

Q5 should return c = 1 for both outer rows, matching the literal controls. 
Casting 0.0 and -0.0 to STRING produces distinct values.

h2. Actual result

The literal controls return the expected results.

Q1 returns six rows instead of three, splitting the count and sum for sec = 100:

{noformat}
sec | c | sx
----+---+-----
100 | 1 | 2
100 | 1 | 1
100 | 1 | 2
100 | 1 | 1
200 | 1 | 3
300 | 0 | NULL
{noformat}

Q2 returns (100, 1), (100, 1), (200, 1), (300, 0).

Q3 returns (100, 1, 1), (100, 2, 1), (200, 3, 1), incorrectly computing the 
window count separately for each timestamp.

Q4 returns three rows instead of two:

{noformat}
d                | c
-----------------+---
INTERVAL '1' DAY | 1
INTERVAL '1' DAY | 1
INTERVAL '2' DAY | 1
{noformat}

Q5 returns:

{noformat}
ks   | c_literal_0 | c_literal_neg0 | c
-----+-------------+----------------+---
-0.0 | 1           | 1              | 0
0.0  | 1           | 1              | 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]

Reply via email to