@@ -240,6 +240,7 @@ class Analyzer(
240240 ExtractWindowExpressions ::
241241 ResolveTimeZone (conf) ::
242242 GlobalAggregates ::
243+ ResolveHaving ::
243244 ResolveAggregateFunctions ::
244245 TimeWindowing ::
245246 ResolveInlineTables (conf) ::
@@ -2029,16 +2030,24 @@ class Analyzer(
20292030 }
20302031 }
20312032
2033+ /**
2034+ * Replace [[AggregateWithHaving ]]s with [[Filter ]]s.
2035+ */
2036+ object ResolveHaving extends Rule [LogicalPlan ] {
2037+ def apply (plan : LogicalPlan ): LogicalPlan = plan.resolveOperatorsUp {
2038+ case AggregateWithHaving (cond, agg : Aggregate ) if agg.resolved =>
2039+ Filter (cond, agg)
2040+ }
2041+ }
2042+
20322043 /**
20332044 * This rule finds aggregate expressions that are not in an aggregate operator. For example,
20342045 * those in a HAVING clause or ORDER BY clause. These expressions are pushed down to the
20352046 * underlying aggregate operator and then projected away after the original operator.
20362047 */
20372048 object ResolveAggregateFunctions extends Rule [LogicalPlan ] {
20382049 def apply (plan : LogicalPlan ): LogicalPlan = plan.resolveOperatorsUp {
2039- case AggregateWithHaving (
2040- cond, agg @ Aggregate (grouping, originalAggExprs, child)) if agg.resolved =>
2041- val having = Filter (cond, agg)
2050+ case f @ Filter (cond, agg @ Aggregate (grouping, originalAggExprs, child)) if agg.resolved =>
20422051 // Try resolving the condition of the filter as though it is in the aggregate clause
20432052 try {
20442053 val aggregatedCondition =
@@ -2083,15 +2092,15 @@ class Analyzer(
20832092 Filter (transformedAggregateFilter,
20842093 agg.copy(aggregateExpressions = originalAggExprs ++ aggregateExpressions)))
20852094 } else {
2086- having
2095+ f
20872096 }
20882097 } else {
2089- having
2098+ f
20902099 }
20912100 } catch {
20922101 // Attempting to resolve in the aggregate can result in ambiguity. When this happens,
20932102 // just return the original plan.
2094- case ae : AnalysisException => having
2103+ case ae : AnalysisException => f
20952104 }
20962105
20972106 case sort @ Sort (sortOrder, global, aggregate : Aggregate ) if aggregate.resolved =>
0 commit comments