Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class Analyzer(
}.isDefined
}

private def hasGroupingFunction(e: Expression): Boolean = {
private[sql] def hasGroupingFunction(e: Expression): Boolean = {
e.collectFirst {
case g: Grouping => g
case g: GroupingID => g
Expand Down Expand Up @@ -1207,6 +1207,19 @@ class Analyzer(
val alias = Alias(ae, ae.toString)()
aggregateExpressions += alias
alias.toAttribute
// Grouping functions are handled in the rule [[ResolveGroupingAnalytics]].
case e: Expression if grouping.exists(_.semanticEquals(e)) &&
Copy link
Member Author

@viirya viirya Jul 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not push down all expressions or the whole expression. Only push down the group by expressions or aggregate expressions.

!ResolveGroupingAnalytics.hasGroupingFunction(e) &&
!aggregate.output.exists(_.semanticEquals(e)) =>
e match {
case ne: NamedExpression =>
aggregateExpressions += ne
ne.toAttribute
case _ =>
val alias = Alias(e, e.toString)()
aggregateExpressions += alias
alias.toAttribute
}
}

// Push the aggregate expressions into the aggregate (if any).
Expand Down
22 changes: 17 additions & 5 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,23 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
setupTestData()

test("having clause") {
Seq(("one", 1), ("two", 2), ("three", 3), ("one", 5)).toDF("k", "v")
.createOrReplaceTempView("hav")
checkAnswer(
sql("SELECT k, sum(v) FROM hav GROUP BY k HAVING sum(v) > 2"),
Row("one", 6) :: Row("three", 3) :: Nil)
withTempView("hav") {
Seq(("one", 1), ("two", 2), ("three", 3), ("one", 5)).toDF("k", "v")
.createOrReplaceTempView("hav")
checkAnswer(
sql("SELECT k, sum(v) FROM hav GROUP BY k HAVING sum(v) > 2"),
Row("one", 6) :: Row("three", 3) :: Nil)
}
}

test("having condition contains grouping column") {
withTempView("hav") {
Seq(("one", 1), ("two", 2), ("three", 3), ("one", 5)).toDF("k", "v")
.createOrReplaceTempView("hav")
checkAnswer(
sql("SELECT count(k) FROM hav GROUP BY v + 1 HAVING v + 1 = 2"),
Row(1) :: Nil)
}
}

test("SPARK-8010: promote numeric to string") {
Expand Down