Skip to content

Commit 56084cc

Browse files
committed
Add support for HAVING clauses in Hive queries.
1 parent 2f6a835 commit 56084cc

File tree

1 file changed

+13
-6
lines changed
  • sql/hive/src/main/scala/org/apache/spark/sql/hive

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ private[hive] object HiveQl {
480480
whereClause ::
481481
groupByClause ::
482482
orderByClause ::
483+
havingClause ::
483484
sortByClause ::
484485
clusterByClause ::
485486
distributeByClause ::
@@ -494,6 +495,7 @@ private[hive] object HiveQl {
494495
"TOK_WHERE",
495496
"TOK_GROUPBY",
496497
"TOK_ORDERBY",
498+
"TOK_HAVING",
497499
"TOK_SORTBY",
498500
"TOK_CLUSTERBY",
499501
"TOK_DISTRIBUTEBY",
@@ -576,21 +578,26 @@ private[hive] object HiveQl {
576578
val withDistinct =
577579
if (selectDistinctClause.isDefined) Distinct(withProject) else withProject
578580

581+
val withHaving = havingClause.map { h =>
582+
val Seq(havingExpr) = h.getChildren.toSeq
583+
Filter(nodeToExpr(havingExpr), withDistinct)
584+
}.getOrElse(withDistinct)
585+
579586
val withSort =
580587
(orderByClause, sortByClause, distributeByClause, clusterByClause) match {
581588
case (Some(totalOrdering), None, None, None) =>
582-
Sort(totalOrdering.getChildren.map(nodeToSortOrder), withDistinct)
589+
Sort(totalOrdering.getChildren.map(nodeToSortOrder), withHaving)
583590
case (None, Some(perPartitionOrdering), None, None) =>
584-
SortPartitions(perPartitionOrdering.getChildren.map(nodeToSortOrder), withDistinct)
591+
SortPartitions(perPartitionOrdering.getChildren.map(nodeToSortOrder), withHaving)
585592
case (None, None, Some(partitionExprs), None) =>
586-
Repartition(partitionExprs.getChildren.map(nodeToExpr), withDistinct)
593+
Repartition(partitionExprs.getChildren.map(nodeToExpr), withHaving)
587594
case (None, Some(perPartitionOrdering), Some(partitionExprs), None) =>
588595
SortPartitions(perPartitionOrdering.getChildren.map(nodeToSortOrder),
589-
Repartition(partitionExprs.getChildren.map(nodeToExpr), withDistinct))
596+
Repartition(partitionExprs.getChildren.map(nodeToExpr), withHaving))
590597
case (None, None, None, Some(clusterExprs)) =>
591598
SortPartitions(clusterExprs.getChildren.map(nodeToExpr).map(SortOrder(_, Ascending)),
592-
Repartition(clusterExprs.getChildren.map(nodeToExpr), withDistinct))
593-
case (None, None, None, None) => withDistinct
599+
Repartition(clusterExprs.getChildren.map(nodeToExpr), withHaving))
600+
case (None, None, None, None) => withHaving
594601
case _ => sys.error("Unsupported set of ordering / distribution clauses.")
595602
}
596603

0 commit comments

Comments
 (0)