Skip to content

Commit fa8359a

Browse files
committed
[SPARK-2225] Turn HAVING without GROUP BY into WHERE.
1 parent 171ebb3 commit fa8359a

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ private[hive] object HiveQl {
563563
withWhere)
564564
}.getOrElse(withWhere)
565565

566-
567566
// The projection of the query can either be a normal projection, an aggregation
568567
// (if there is a group by) or a script transformation.
569568
val withProject = transformation.getOrElse {
@@ -581,16 +580,10 @@ private[hive] object HiveQl {
581580
val withDistinct =
582581
if (selectDistinctClause.isDefined) Distinct(withProject) else withProject
583582

584-
val withHaving = havingClause.map { h =>
585-
586-
if (groupByClause == None) {
587-
throw new SemanticException("HAVING specified without GROUP BY")
588-
}
589-
590-
val havingExpr = h.getChildren.toSeq match {
591-
case Seq(hexpr) => nodeToExpr(hexpr)
592-
}
593-
583+
val withHaving = havingClause.map { h =>
584+
val havingExpr = h.getChildren.toSeq match { case Seq(hexpr) => nodeToExpr(hexpr) }
585+
// Note that we added a cast to boolean. If the expression itself is already boolean,
586+
// the optimizer will get rid of the unnecessary cast.
594587
Filter(Cast(havingExpr, BooleanType), withDistinct)
595588
}.getOrElse(withDistinct)
596589

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,27 +227,22 @@ class HiveQuerySuite extends HiveComparisonTest {
227227
test("SPARK-2180: HAVING support in GROUP BY clauses (positive)") {
228228
val fixture = List(("foo", 2), ("bar", 1), ("foo", 4), ("bar", 3))
229229
.zipWithIndex.map {case Pair(Pair(value, attr), key) => HavingRow(key, value, attr)}
230-
231230
TestHive.sparkContext.parallelize(fixture).registerAsTable("having_test")
232-
233231
val results =
234232
hql("SELECT value, max(attr) AS attr FROM having_test GROUP BY value HAVING attr > 3")
235233
.collect()
236234
.map(x => Pair(x.getString(0), x.getInt(1)))
237-
235+
238236
assert(results === Array(Pair("foo", 4)))
239-
240237
TestHive.reset()
241238
}
242-
243-
test("SPARK-2180: HAVING without GROUP BY raises exception") {
244-
intercept[Exception] {
245-
hql("SELECT value, attr FROM having_test HAVING attr > 3")
246-
}
247-
}
248239

249-
test("SPARK-2180: HAVING with non-boolean clause raises no exceptions") {
250-
val results = hql("select key, count(*) c from src group by key having c").collect()
240+
test("SPARK-2180: HAVING with non-boolean clause raises no exceptions") {
241+
hql("select key, count(*) c from src group by key having c").collect()
242+
}
243+
244+
test("SPARK-2225: turn HAVING without GROUP BY into a simple filter") {
245+
assert(hql("select key from src having key > 490").collect().size < 100)
251246
}
252247

253248
test("Query Hive native command execution result") {

0 commit comments

Comments
 (0)