Skip to content

Commit 3bbaf26

Browse files
committed
Added casts to HAVING expressions
1 parent 83f1340 commit 3bbaf26

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,11 @@ private[hive] object HiveQl {
587587
throw new SemanticException("HAVING specified without GROUP BY")
588588
}
589589

590-
val Seq(havingExpr) = h.getChildren.toSeq
591-
Filter(nodeToExpr(havingExpr), withDistinct)
590+
val havingExpr = h.getChildren.toSeq match {
591+
case Seq(hexpr) => nodeToExpr(hexpr)
592+
}
593+
594+
Filter(Cast(havingExpr, BooleanType), withDistinct)
592595
}.getOrElse(withDistinct)
593596

594597
val withSort =

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ class HiveQuerySuite extends HiveComparisonTest {
230230

231231
TestHive.sparkContext.parallelize(fixture).registerAsTable("having_test")
232232

233-
val results = hql("SELECT value, max(attr) AS attr FROM having_test GROUP BY value HAVING attr > 3")
233+
val results =
234+
hql("SELECT value, max(attr) AS attr FROM having_test GROUP BY value HAVING attr > 3")
234235
.collect()
235236
.map(x => Pair(x.getString(0), x.getInt(1)))
236237

@@ -244,6 +245,10 @@ class HiveQuerySuite extends HiveComparisonTest {
244245
hql("SELECT value, attr FROM having_test HAVING attr > 3")
245246
}
246247
}
248+
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()
251+
}
247252

248253
test("Query Hive native command execution result") {
249254
val tableName = "test_native_commands"

0 commit comments

Comments
 (0)