Skip to content

Commit

Permalink
Add support for HAVING clauses in Hive queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
willb committed Jun 20, 2014
1 parent 2f6a835 commit 56084cc
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ private[hive] object HiveQl {
whereClause ::
groupByClause ::
orderByClause ::
havingClause ::
sortByClause ::
clusterByClause ::
distributeByClause ::
Expand All @@ -494,6 +495,7 @@ private[hive] object HiveQl {
"TOK_WHERE",
"TOK_GROUPBY",
"TOK_ORDERBY",
"TOK_HAVING",
"TOK_SORTBY",
"TOK_CLUSTERBY",
"TOK_DISTRIBUTEBY",
Expand Down Expand Up @@ -576,21 +578,26 @@ private[hive] object HiveQl {
val withDistinct =
if (selectDistinctClause.isDefined) Distinct(withProject) else withProject

val withHaving = havingClause.map { h =>
val Seq(havingExpr) = h.getChildren.toSeq
Filter(nodeToExpr(havingExpr), withDistinct)
}.getOrElse(withDistinct)

val withSort =
(orderByClause, sortByClause, distributeByClause, clusterByClause) match {
case (Some(totalOrdering), None, None, None) =>
Sort(totalOrdering.getChildren.map(nodeToSortOrder), withDistinct)
Sort(totalOrdering.getChildren.map(nodeToSortOrder), withHaving)
case (None, Some(perPartitionOrdering), None, None) =>
SortPartitions(perPartitionOrdering.getChildren.map(nodeToSortOrder), withDistinct)
SortPartitions(perPartitionOrdering.getChildren.map(nodeToSortOrder), withHaving)
case (None, None, Some(partitionExprs), None) =>
Repartition(partitionExprs.getChildren.map(nodeToExpr), withDistinct)
Repartition(partitionExprs.getChildren.map(nodeToExpr), withHaving)
case (None, Some(perPartitionOrdering), Some(partitionExprs), None) =>
SortPartitions(perPartitionOrdering.getChildren.map(nodeToSortOrder),
Repartition(partitionExprs.getChildren.map(nodeToExpr), withDistinct))
Repartition(partitionExprs.getChildren.map(nodeToExpr), withHaving))
case (None, None, None, Some(clusterExprs)) =>
SortPartitions(clusterExprs.getChildren.map(nodeToExpr).map(SortOrder(_, Ascending)),
Repartition(clusterExprs.getChildren.map(nodeToExpr), withDistinct))
case (None, None, None, None) => withDistinct
Repartition(clusterExprs.getChildren.map(nodeToExpr), withHaving))
case (None, None, None, None) => withHaving
case _ => sys.error("Unsupported set of ordering / distribution clauses.")
}

Expand Down

0 comments on commit 56084cc

Please sign in to comment.