marko-radoicic-db opened a new pull request, #59463:
URL: https://github.com/apache/spark/pull/59463

   ### What changes were proposed in this pull request?
   
   Reject aliases on integer-literal grouping keys in pipe `AGGREGATE`. For 
example,
   `TABLE t |> AGGREGATE sum(x) AS s GROUP BY 2 AS grp` now raises the same
   `PARSE_SYNTAX_ERROR` (`42601`) as regular `GROUP BY 2 AS grp`:
   
   ```text
   [PARSE_SYNTAX_ERROR] Syntax error at or near 'grp': extra input 'grp'. 
SQLSTATE: 42601
   ```
   
   `AstBuilder.withAggregationClause` already rejects grouping aliases when
   `allowNamedGroupingExpressions` is false (regular `GROUP BY`). This PR also 
rejects them
   in pipe `AGGREGATE` when the aliased expression is an integer literal, which 
covers both
   `AS grp` and implicit `grp` aliases, as well as parenthesized and negative 
literals.
   Bare ordinals and aliases on other grouping expressions (including `1 + 1`,
   `CAST(2 AS INT)`, `2L` and `2.0`) remain supported.
   
   ### Why are the changes needed?
   
   In pipe `AGGREGATE`, a bare integer grouping key is an ordinal, but an 
aliased integer
   grouping key is not replaced by an ordinal because the literal is wrapped in 
an `Alias`.
   `GROUP BY 2 AS grp` therefore silently groups by the constant `2` and 
outputs a column
   `grp = 2`, while `GROUP BY 2` groups by the second aggregate-list position. 
The aliased
   form is ambiguous, and users who write it most likely mean the ordinal. 
Rejecting it
   matches regular `GROUP BY`, which already reports a syntax error for the 
same text.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Pipe `AGGREGATE` queries that alias an integer-literal grouping key, 
such as
   `TABLE t |> AGGREGATE sum(x) AS s GROUP BY 2 AS grp`, previously grouped by 
a constant and
   now fail with `PARSE_SYNTAX_ERROR`. Bare ordinals and aliases on non-ordinal 
grouping
   expressions are unchanged.
   
   ### How was this patch tested?
   
   Added three tests to `PlanParserSuite`:
   
   - Pipe and regular `GROUP BY` raise the same error for 24 aliased 
integer-literal forms
     each (ordinals `0`, `1`, `2`, `3`, `-1` and `(2)`; `AS grp` and `grp` 
aliases; with and
     without a preceding grouping key).
   - Bare ordinals in pipe `AGGREGATE` still parse to `UnresolvedOrdinal` /
     `UnresolvedPipeAggregateOrdinal`.
   - Aliases on non-ordinal grouping expressions are preserved.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to