Skip to content

Commit 16c0755

Browse files
authored
Resolve named windows in HAVING (apache#26515)
## Which issue does this PR close? - Closes apache#26506. ## Rationale for this change A named window in `HAVING` was never resolved, so `MAX(price) OVER w` was planned as a plain aggregate and the query returned rows instead of being rejected. An undefined window name in `HAVING` was not reported either. ## What changes are included in this PR? Resolve named windows in `HAVING` together with the other clauses. The query now fails like the inline form (`Window function calls are not allowed in HAVING`), and an undefined name reports `The window ... is not defined!`. This builds on apache#26400, which makes the resolver usable for any clause. Only the last commit is new; the diff shrinks to it once apache#26400 merges. ## Are these changes tested? Yes, `window.slt` cases for `HAVING` (inline, named, undefined name) and `WHERE` (inline, named). The `HAVING` cases fail without the fix. ## Are there any user-facing changes? Queries that used a named window in `HAVING` now return an error instead of wrong results.
1 parent 9504942 commit 16c0755

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎datafusion/sql/src/select.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
132132
// Handle named windows before processing the projection expression
133133
check_conflicting_windows(&select.named_window)?;
134134
self.match_window_definitions(&mut select.projection, &select.named_window)?;
135+
self.match_window_definitions(&mut select.having, &select.named_window)?;
135136
self.match_window_definitions(&mut select.qualify, &select.named_window)?;
136137
self.match_window_definitions(&mut query_order_by, &select.named_window)?;
137138

‎datafusion/sqllogictest/test_files/window.slt‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,6 +3629,30 @@ SELECT (SELECT MAX(rn) FROM (
36293629
FROM named_window_prices
36303630
WINDOW w AS (ORDER BY ts)
36313631

3632+
# A named window in HAVING is rejected like an inline one
3633+
statement error DataFusion error: Error during planning: Window function calls are not allowed in HAVING
3634+
SELECT ticker, price FROM named_window_prices GROUP BY ticker, price
3635+
HAVING MAX(price) OVER (PARTITION BY ticker) > 1
3636+
3637+
statement error DataFusion error: Error during planning: Window function calls are not allowed in HAVING
3638+
SELECT ticker, price FROM named_window_prices GROUP BY ticker, price
3639+
HAVING MAX(price) OVER w > 1
3640+
WINDOW w AS (PARTITION BY ticker)
3641+
3642+
statement error DataFusion error: Error during planning: The window no_such_window is not defined!
3643+
SELECT ticker, price FROM named_window_prices GROUP BY ticker, price
3644+
HAVING MAX(price) OVER no_such_window > 1
3645+
3646+
# A named window in WHERE is rejected like an inline one
3647+
statement error
3648+
SELECT ticker FROM named_window_prices
3649+
WHERE MAX(price) OVER (PARTITION BY ticker) > 1
3650+
3651+
statement error
3652+
SELECT ticker FROM named_window_prices
3653+
WHERE MAX(price) OVER w > 1
3654+
WINDOW w AS (PARTITION BY ticker)
3655+
36323656
statement ok
36333657
DROP TABLE named_window_prices;
36343658

0 commit comments

Comments
 (0)