diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala index 67ab17dd1fc20..fe05757753b1f 100644 --- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala +++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala @@ -204,6 +204,9 @@ private[hive] object HiveQl { class ParseException(sql: String, cause: Throwable) extends Exception(s"Failed to parse: $sql", cause) + class SemanticException(msg: String) + extends Exception(msg) + /** * Returns the AST for the given SQL string. */ @@ -579,6 +582,11 @@ private[hive] object HiveQl { if (selectDistinctClause.isDefined) Distinct(withProject) else withProject val withHaving = havingClause.map { h => + + if (groupByClause == None) { + throw new SemanticException("Error in semantic analysis: HAVING specified without GROUP BY") + } + val Seq(havingExpr) = h.getChildren.toSeq Filter(nodeToExpr(havingExpr), withDistinct) }.getOrElse(withDistinct)