[SPARK-16639][SQL] The query with having condition that contains grouping by column should work
## What changes were proposed in this pull request? The query with having condition that contains grouping by column will be failed during analysis. E.g., create table tbl(a int, b string); select count(b) from tbl group by a + 1 having a + 1 = 2; Having condition should be able to use grouping by column. ## How was this patch tested? Jenkins tests. Author: Liang-Chi Hsieh <simonh@tw.ibm.com> Closes #14296 from viirya/having-contains-grouping-column.
This commit is contained in:
parent
762366fd87
commit
9ade77c3fa
|
@ -246,7 +246,7 @@ class Analyzer(
|
||||||
}.isDefined
|
}.isDefined
|
||||||
}
|
}
|
||||||
|
|
||||||
private def hasGroupingFunction(e: Expression): Boolean = {
|
private[sql] def hasGroupingFunction(e: Expression): Boolean = {
|
||||||
e.collectFirst {
|
e.collectFirst {
|
||||||
case g: Grouping => g
|
case g: Grouping => g
|
||||||
case g: GroupingID => g
|
case g: GroupingID => g
|
||||||
|
@ -1207,6 +1207,19 @@ class Analyzer(
|
||||||
val alias = Alias(ae, ae.toString)()
|
val alias = Alias(ae, ae.toString)()
|
||||||
aggregateExpressions += alias
|
aggregateExpressions += alias
|
||||||
alias.toAttribute
|
alias.toAttribute
|
||||||
|
// Grouping functions are handled in the rule [[ResolveGroupingAnalytics]].
|
||||||
|
case e: Expression if grouping.exists(_.semanticEquals(e)) &&
|
||||||
|
!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).
|
// Push the aggregate expressions into the aggregate (if any).
|
||||||
|
|
|
@ -39,11 +39,23 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
|
||||||
setupTestData()
|
setupTestData()
|
||||||
|
|
||||||
test("having clause") {
|
test("having clause") {
|
||||||
Seq(("one", 1), ("two", 2), ("three", 3), ("one", 5)).toDF("k", "v")
|
withTempView("hav") {
|
||||||
.createOrReplaceTempView("hav")
|
Seq(("one", 1), ("two", 2), ("three", 3), ("one", 5)).toDF("k", "v")
|
||||||
checkAnswer(
|
.createOrReplaceTempView("hav")
|
||||||
sql("SELECT k, sum(v) FROM hav GROUP BY k HAVING sum(v) > 2"),
|
checkAnswer(
|
||||||
Row("one", 6) :: Row("three", 3) :: Nil)
|
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") {
|
test("SPARK-8010: promote numeric to string") {
|
||||||
|
|
Loading…
Reference in a new issue