cloud-fan commented on a change in pull request #23304: [SPARK-26353][SQL]Add
typed aggregate functions(max/min) to the example module.
URL: https://github.com/apache/spark/pull/23304#discussion_r257271503
##########
File path:
examples/src/main/scala/org/apache/spark/examples/sql/SimpleTypedAggregator.scala
##########
@@ -84,3 +91,71 @@ class TypedAverage[IN](val f: IN => Double) extends
Aggregator[IN, (Double, Long
}
override def outputEncoder: Encoder[Double] = Encoders.scalaDouble
}
+
+class TypedMin[IN](val f: IN => Double) extends Aggregator[IN, MutableDouble,
Option[Double]] {
+ override def zero: MutableDouble = null
+ override def reduce(b: MutableDouble, a: IN): MutableDouble = {
+ if (b == null) {
+ new MutableDouble(f(a))
+ } else {
+ b.value = math.min(b.value, f(a))
+ b
+ }
+ }
+ override def merge(b1: MutableDouble, b2: MutableDouble): MutableDouble = {
+ if (b1 == null) {
+ b2
+ } else if (b2 == null) {
+ b1
+ } else {
+ b1.value = math.min(b1.value, b2.value)
+ b1
+ }
+ }
+ override def finish(reduction: MutableDouble): Option[Double] = {
+ if (reduction != null) {
+ Some(reduction.value)
+ } else {
+ None
+ }
+ }
+
+ override def bufferEncoder: Encoder[MutableDouble] =
Encoders.kryo[MutableDouble]
+ override def outputEncoder: Encoder[Option[Double]] =
ExpressionEncoder[Option[Double]]()
Review comment:
can we use `Encoders.product[Option[Double]]`? Or we can use
`Encoders.DOUBLE` and use null to represent `None`.
The thing is, `ExpressionEncoder` is an internal class and we should avoid
exposing it in the example module.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]