Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
choose longSum or count agg for sql count, based on presence of count…
Browse files Browse the repository at this point in the history
… metric in DruidIndex
  • Loading branch information
Harish Butani committed Sep 9, 2016
1 parent 9f9ae5b commit fb8d5e7
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ trait AggregateTransform {
case _ => Seq()
}

private def addCountAgg(aggExp: AggregateExpression,
dqb : DruidQueryBuilder) : DruidQueryBuilder = {
val a = dqb.nextAlias

/*
* if the Druid Index has a count metric then translate to a sum of the count,
* else translate to a count metric.
*/
val druidAggFunc = if (dqb.drInfo.druidDS.metrics.contains("count")) {
"longSum"
} else {
"count"
}

dqb.aggregate(FunctionAggregationSpec(druidAggFunc, a, "count")).
outputAttribute(a, aggExp, aggExp.dataType, LongType)
}

def aggregateExpression(dqb: DruidQueryBuilder, aggExp: AggregateExpression)(
implicit expandOpProjection: Seq[Expression],
aEExprIdToPos: Map[ExprId, Int],
Expand All @@ -332,17 +350,10 @@ trait AggregateTransform {
val nativeAgg =
new DruidNativeAggregator(dqb, aggExp, expandOpProjection, aEExprIdToPos, aEToLiteralExpr)
(aggExp, aggExp.aggregateFunction) match {
case (_, Count(Literal(1, IntegerType) :: Nil)) => {
val a = dqb.nextAlias
Some(dqb.aggregate(FunctionAggregationSpec("longSum", a, "count")).
outputAttribute(a, aggExp, aggExp.dataType, LongType))
case (_, Count(Literal(1, IntegerType) :: Nil)) |
(_, Count(AttributeReference("1", _, _, _) :: Nil)) => {
Some(addCountAgg(aggExp, dqb))
}
case (_, Count(AttributeReference("1", _, _, _) :: Nil)) => {
val a = dqb.nextAlias
Some(dqb.aggregate(FunctionAggregationSpec("longSum", a, "count")).
outputAttribute(a, aggExp, aggExp.dataType, LongType))
}

// TODO:
// Instead of JS rewriting AVG as Sum, Cnt, Sum/Cnt
// the expression should be rewritten generically. Introduce
Expand Down

0 comments on commit fb8d5e7

Please sign in to comment.