Skip to content

Apache DataFusion SQL Query Engine

Resolve named windows in HAVING (#26515)

Qi Zhu16c0755b51d0
diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs
index 34907c489c..e03799f729 100644
--- a/datafusion/sql/src/select.rs
+++ b/datafusion/sql/src/select.rs
@@ -132,6 +132,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
// Handle named windows before processing the projection expression
check_conflicting_windows(&select.named_window)?;
self.match_window_definitions(&mut select.projection, &select.named_window)?;
+ self.match_window_definitions(&mut select.having, &select.named_window)?;
self.match_window_definitions(&mut select.qualify, &select.named_window)?;
self.match_window_definitions(&mut query_order_by, &select.named_window)?;
diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.slt
index b224308303..346662ee04 100644
--- a/datafusion/sqllogictest/test_files/window.slt
+++ b/datafusion/sqllogictest/test_files/window.slt
@@ -3629,6 +3629,30 @@ SELECT (SELECT MAX(rn) FROM (
FROM named_window_prices
WINDOW w AS (ORDER BY ts)
+# A named window in HAVING is rejected like an inline one
+statement error DataFusion error: Error during planning: Window function calls are not allowed in HAVING
+SELECT ticker, price FROM named_window_prices GROUP BY ticker, price
+HAVING MAX(price) OVER (PARTITION BY ticker) > 1
+
+statement error DataFusion error: Error during planning: Window function calls are not allowed in HAVING
+SELECT ticker, price FROM named_window_prices GROUP BY ticker, price
+HAVING MAX(price) OVER w > 1
+WINDOW w AS (PARTITION BY ticker)
+
+statement error DataFusion error: Error during planning: The window no_such_window is not defined!
+SELECT ticker, price FROM named_window_prices GROUP BY ticker, price
+HAVING MAX(price) OVER no_such_window > 1
+
+# A named window in WHERE is rejected like an inline one
+statement error
+SELECT ticker FROM named_window_prices
+WHERE MAX(price) OVER (PARTITION BY ticker) > 1
+
+statement error
+SELECT ticker FROM named_window_prices
+WHERE MAX(price) OVER w > 1
+WINDOW w AS (PARTITION BY ticker)
+
statement ok
DROP TABLE named_window_prices;