-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add COUNT_DISTINCT aggregation (#594)
* scala COUNT_DISTINCT aggregation * cr change comment table to match expected results * cr - fix comment * cr - remove showing df
- Loading branch information
1 parent
7dbdead
commit 4b28801
Showing
4 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/scala/com/linkedin/feathr/swj/aggregate/CountDistinctAggregate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.linkedin.feathr.swj.aggregate | ||
|
||
import com.linkedin.feathr.swj.aggregate.AggregationType._ | ||
import org.apache.spark.sql.types._ | ||
|
||
/** | ||
* COUNT_DISTINCT aggregation implementation. | ||
* | ||
* @param metricCol Name of the metric column or a Spark SQL column expression for derived metric | ||
* that will be aggregated using COUNT_DISTINCT. | ||
*/ | ||
class CountDistinctAggregate(val metricCol: String) extends AggregationSpec { | ||
override def aggregation: AggregationType = COUNT_DISTINCT | ||
|
||
override def metricName = "count_distinct_col" | ||
|
||
override def isIncrementalAgg = false | ||
|
||
override def isCalculateAggregateNeeded: Boolean = true | ||
|
||
override def calculateAggregate(aggregate: Any, dataType: DataType): Any = { | ||
if (aggregate == null) { | ||
aggregate | ||
} else { | ||
dataType match { | ||
case IntegerType => aggregate.asInstanceOf[Set[Int]].size | ||
case LongType => aggregate.asInstanceOf[Set[Long]].size | ||
case DoubleType => aggregate.asInstanceOf[Set[Double]].size | ||
case FloatType => aggregate.asInstanceOf[Set[Float]].size | ||
case StringType => aggregate.asInstanceOf[Set[String]].size | ||
case _ => throw new RuntimeException(s"Invalid data type for COUNT_DISTINCT metric col $metricCol. " + | ||
s"Only Int, Long, Double, Float, and String are supported, but got ${dataType.typeName}") | ||
} | ||
} | ||
} | ||
|
||
override def agg(aggregate: Any, record: Any, dataType: DataType): Any = { | ||
if (aggregate == null) { | ||
Set(record) | ||
} else if (record == null) { | ||
aggregate | ||
} else { | ||
dataType match { | ||
case IntegerType => aggregate.asInstanceOf[Set[Int]] + record.asInstanceOf[Int] | ||
case LongType => aggregate.asInstanceOf[Set[Long]] + record.asInstanceOf[Long] | ||
case DoubleType => aggregate.asInstanceOf[Set[Double]] + record.asInstanceOf[Double] | ||
case FloatType => aggregate.asInstanceOf[Set[Float]] + record.asInstanceOf[Float] | ||
case StringType=> aggregate.asInstanceOf[Set[String]] + record.asInstanceOf[String] | ||
case _ => throw new RuntimeException(s"Invalid data type for COUNT_DISTINCT metric col $metricCol. " + | ||
s"Only Int, Long, Double, Float, and String are supported, but got ${dataType.typeName}") | ||
} | ||
} | ||
} | ||
|
||
override def deagg(aggregate: Any, record: Any, dataType: DataType): Any = { | ||
throw new RuntimeException("Method deagg for COUNT_DISTINCT aggregate is not implemented because COUNT_DISTINCT is " + | ||
"not an incremental aggregation.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters