Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/1935#discussion_r16509744
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregates.scala
---
@@ -161,13 +162,88 @@ case class Count(child: Expression) extends
PartialAggregate with trees.UnaryNod
override def newInstance() = new CountFunction(child, this)
}
-case class CountDistinct(expressions: Seq[Expression]) extends
AggregateExpression {
+case class CountDistinct(expressions: Seq[Expression]) extends
PartialAggregate {
+ def this() = this(null)
+
override def children = expressions
override def references = expressions.flatMap(_.references).toSet
override def nullable = false
override def dataType = LongType
override def toString = s"COUNT(DISTINCT ${expressions.mkString(",")})"
override def newInstance() = new CountDistinctFunction(expressions, this)
+
+ override def asPartial = {
+ val partialSet = Alias(CollectHashSet(expressions), "partialSets")()
+ SplitEvaluation(
+ CombineSetsAndCount(partialSet.toAttribute),
+ partialSet :: Nil)
+ }
+}
+
+case class CollectHashSet(expressions: Seq[Expression]) extends
AggregateExpression {
+ def this() = this(null)
+
+ override def children = expressions
+ override def references = expressions.flatMap(_.references).toSet
+ override def nullable = false
+ override def dataType = ArrayType(expressions.head.dataType)
+ override def toString = s"AddToHashSet(${expressions.mkString(",")})"
+ override def newInstance() = new CollectHashSetFunction(expressions,
this)
+}
+
+case class CollectHashSetFunction(
+ @transient expr: Seq[Expression],
+ @transient base: AggregateExpression)
+ extends AggregateFunction {
+
+ def this() = this(null, null) // Required for serialization.
+
+ val seen = new OpenHashSet[Any]()
+
+ @transient
+ val distinctValue = new InterpretedProjection(expr)
+
+ override def update(input: Row): Unit = {
+ val evaluatedExpr = distinctValue(input)
+ if (!evaluatedExpr.anyNull) {
+ seen.add(evaluatedExpr)
+ }
+ }
+
+ override def eval(input: Row): Any = {
+ seen
+ }
+}
+
+case class CombineSetsAndCount(inputSet: Expression) extends
AggregateExpression {
+ def this() = this(null)
+
+ override def children = inputSet :: Nil
+ override def references = inputSet.references
+ override def nullable = false
+ override def dataType = LongType
+ override def toString = s"CombineAndCount($inputSet)"
+ override def newInstance() = new CombineSetsAndCountFunction(inputSet,
this)
+}
+
+case class CombineSetsAndCountFunction(
+ @transient inputSet: Expression,
+ @transient base: AggregateExpression)
+ extends AggregateFunction {
+
+ def this() = this(null, null) // Required for serialization.
+
+ val seen = new OpenHashSet[Any]()
--- End diff --
does this support null?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]