n-young commented on code in PR #60090:
URL: https://github.com/apache/spark/pull/60090#discussion_r4516227910
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -4935,19 +4963,48 @@ object ResolveUnresolvedHaving extends
Rule[LogicalPlan] {
*
* Returns None unless the condition can be evaluated below every Window in
the chain.
*/
- private def insertFilterBeforeWindow(filter: Filter): Option[LogicalPlan] =
filter.child match {
- case project: Project =>
- insertFilterBeforeWindow(filter.copy(child = project.child))
+ private def insertFilterBeforeWindow(filter: Filter): Option[LogicalPlan] = {
+ if
(SQLConf.get.getConf(SQLConf.LEGACY_EVALUATE_HAVING_AFTER_INNER_WINDOW)) {
+ insertFilterBeforeOuterWindow(filter)
+ } else {
+ insertFilterBeforeAllWindows(filter, foundWindow = false)
+ }
+ }
+
+ private def insertFilterBeforeAllWindows(
+ filter: Filter,
+ foundWindow: Boolean): Option[LogicalPlan] = filter.child match {
+ case project: Project if
filter.condition.references.subsetOf(project.child.outputSet) =>
+ insertFilterBeforeAllWindows(filter.copy(child = project.child),
foundWindow)
.map(child => project.withNewChildren(Seq(child)))
- case generate: Generate =>
- insertFilterBeforeWindow(filter.copy(child = generate.child))
+ case generate: Generate if
filter.condition.references.subsetOf(generate.child.outputSet) =>
+ insertFilterBeforeAllWindows(filter.copy(child = generate.child),
foundWindow)
.map(child => generate.withNewChildren(Seq(child)))
- case window: Window =>
- insertFilterBeforeWindowChain(filter, window)
+ case window: Window if
filter.condition.references.subsetOf(window.child.outputSet) =>
+ insertFilterBeforeAllWindows(filter.copy(child = window.child),
foundWindow = true)
+ .map(child => window.withNewChildren(Seq(child)))
+ case _ if foundWindow && !filter.child.containsPattern(WINDOW) &&
Review Comment:
Addressed at the current head. The stop check now follows only the
SELECT-list `Project`/`Generate`/`Window` spine, so a window in a derived-table
input or WHERE subquery no longer blocks HAVING placement. I added default and
legacy goldens for windowed inputs, windowed subqueries, and nested SELECT-list
windows over a windowed input.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -978,11 +978,23 @@ class Analyzer(
case a: AttributeReference => attrMap.getOrElse(a, a)
}
- if (extraAggExprs.isEmpty) {
+ val windowOutputSet = AttributeSet(
+ aggregate.aggregateExpressions.zip(newChild.output).collect {
+ case (expr, output) if expr.containsPattern(WINDOW_EXPRESSION) =>
output
+ })
+ val withHaving = if (
+ !conf.getConf(SQLConf.LEGACY_EVALUATE_HAVING_AFTER_INNER_WINDOW) &&
+ windowOutputSet.nonEmpty &&
newCond.references.intersect(windowOutputSet).isEmpty) {
+ UnresolvedHaving(newCond, newChild)
Review Comment:
Addressed at the current head. Grouping-analytics deferral now resolves
lateral column aliases to a fixed point before wrapping the plan in
`DeferredHaving`. If the lift cannot complete, or the condition uses a grouping
function, it retains the existing `Filter` path. Default and legacy
lateral-alias goldens cover the corrected behavior.
##########
sql/core/src/test/resources/sql-tests/inputs/window-resolution-edge-cases.sql:
##########
@@ -99,6 +99,148 @@ SELECT col1, MAX(ROW_NUMBER() OVER w) OVER () AS max_rn
FROM t1 WINDOW w AS (ORD
-- Nested windows over an aggregate, and in QUALIFY together with HAVING.
SELECT col1, MAX(RANK() OVER (ORDER BY SUM(col2))) OVER () AS m FROM t1 GROUP
BY col1;
+
+-- HAVING must be evaluated before every nested window layer.
+SELECT
+ k,
+ SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER () AS rank_sum
+FROM VALUES ('drop', 2), ('keep', 1) AS input(k, v)
+GROUP BY k
+HAVING k = 'keep'
+ORDER BY k;
+
+-- HAVING must also precede windows nested in the outer window specification.
+SELECT
+ k,
+ SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER (
+ PARTITION BY RANK() OVER (ORDER BY SUM(v) DESC)) AS partition_rank_sum,
+ SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER (
+ ORDER BY RANK() OVER (ORDER BY SUM(v) DESC)) AS ordered_rank_sum
+FROM VALUES ('drop', 2), ('keep', 1) AS input(k, v)
+GROUP BY k
+HAVING k = 'keep'
+ORDER BY k;
+
+-- Three nested window levels, mixed with single-level and two-level windows.
+SELECT
+ k,
+ COUNT(*) OVER () AS group_count,
+ SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER () AS rank_sum,
+ MAX(SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER ()) OVER () AS max_rank_sum
+FROM VALUES ('drop', 2), ('keep', 1) AS input(k, v)
+GROUP BY k
+HAVING k = 'keep'
+ORDER BY k;
+
+-- HAVING on an aggregate alias must precede every nested window layer.
+SELECT
+ k,
+ COUNT(*) AS row_count,
+ SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER () AS rank_sum
+FROM VALUES ('drop', 1), ('drop', 1), ('keep', 1) AS input(k, v)
+GROUP BY k
+HAVING row_count = 1
+ORDER BY k;
+
+-- A window alias is not valid in HAVING.
+SELECT
+ k,
+ SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER () AS rank_sum
+FROM VALUES ('drop', 2), ('keep', 1) AS input(k, v)
+GROUP BY k
+HAVING rank_sum = 1;
+
+-- Grouping analytics HAVING must be evaluated before every nested window
layer.
+SELECT
+ k,
+ SUM(RANK() OVER (ORDER BY SUM(v) DESC)) OVER () AS rank_sum
+FROM VALUES ('drop', 2), ('keep', 1) AS input(k, v)
+GROUP BY ROLLUP(k)
+HAVING k = 'keep'
+ORDER BY k;
+
+-- Grouping analytics HAVING must also precede a single window layer.
+SELECT
+ k,
+ COUNT(*) OVER () AS group_count
+FROM VALUES ('drop', 2), ('keep', 1) AS input(k, v)
+GROUP BY GROUPING SETS ((k), ())
+HAVING k = 'keep'
+ORDER BY k;
+
+-- Grouping analytics HAVING on a window alias keeps its existing analysis
behavior.
+SELECT
+ k,
+ COUNT(*) OVER () AS group_count
+FROM VALUES ('drop', 2), ('keep', 1) AS input(k, v)
+GROUP BY GROUPING SETS ((k), ())
+HAVING group_count = 3
Review Comment:
Addressed. The grouping-analytics cases now use type-matched `3L`/`1L`
literals so they reach the new branch in `tryResolveHavingCondition`. The
goldens cover the window-output path, HAVING-only aggregate projection,
grouping-function handling, and lateral aliases under both default and legacy
settings.
--
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]