Nick Young created SPARK-60861:
----------------------------------

             Summary: Adding a window expression changes HAVING name resolution 
for a same-named grouping column
                 Key: SPARK-60861
                 URL: https://issues.apache.org/jira/browse/SPARK-60861
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
            Reporter: Nick Young


When an input grouping column and a SELECT-list alias have the same name, 
adding a window expression changes which name HAVING resolves. HAVING should 
continue to prefer the input grouping column. The changed binding silently 
filters the wrong groups.

Minimal reproduction using two synthetic rows:

{code:sql}
-- Returns 10: HAVING refers to the grouping key t.k.
SELECT sum(v) AS k
FROM VALUES (1, 10), (2, 20) AS t(k, v)
GROUP BY k
HAVING k = 1;

-- Returns no rows; expected (10, 1).
SELECT sum(v) AS k, row_number() OVER (ORDER BY sum(v)) AS rn
FROM VALUES (1, 10), (2, 20) AS t(k, v)
GROUP BY k
HAVING k = 1;

-- Qualifying the grouping key returns the expected (10, 1).
SELECT sum(v) AS k, row_number() OVER (ORDER BY sum(v)) AS rn
FROM VALUES (1, 10), (2, 20) AS t(k, v)
GROUP BY k
HAVING t.k = 1;
{code}

The analyzed plan for the failing query applies the filter to the BIGINT 
aggregate alias sum(v), rather than the INT grouping key. The control queries 
filter the input grouping key.

The public Analyzer's ExtractWindowExpressions rule converts unresolved HAVING 
into a Filter over the extracted Aggregate. This allows SELECT aliases to 
shadow grouping columns that would otherwise take precedence.

Reproduced with ANSI mode enabled and disabled against the HAVING analyzer code 
from Apache Spark master 523596fdc5495bb191a0af903622355999a62eaa. 
SQLQueryTestSuite execution and analyzed-plan output confirm the different 
bindings and results.

A candidate fix and regression coverage are available at:
https://github.com/apache/spark/pull/60076




--
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