cloud-fan commented on a change in pull request #23304: [SPARK-26353][SQL]Add 
typed aggregate functions: max&&min
URL: https://github.com/apache/spark/pull/23304#discussion_r245674841
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/typedaggregators.scala
 ##########
 @@ -99,3 +99,179 @@ class TypedAverage[IN](val f: IN => Double) extends 
Aggregator[IN, (Double, Long
     toColumn.asInstanceOf[TypedColumn[IN, java.lang.Double]]
   }
 }
+
+trait TypedMinDouble[IN, OUT] extends Aggregator[IN, MutableDouble, OUT] {
+  val f: IN => 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 bufferEncoder: Encoder[MutableDouble] = 
Encoders.kryo[MutableDouble]
+}
+
+class JavaTypedMinDouble[IN](override val f: IN => Double)
+  extends TypedMinDouble[IN, java.lang.Double] {
+  override def outputEncoder: Encoder[java.lang.Double] = 
ExpressionEncoder[java.lang.Double]()
+  override def finish(reduction: MutableDouble): java.lang.Double = 
reduction.value
+  def this(f: MapFunction[IN, java.lang.Double]) = this((x: IN) => 
f.call(x).asInstanceOf[Double])
+}
+
+class ScalaTypedMinDouble[IN](override val f: IN => Double)
+  extends TypedMinDouble[IN, Option[Double]] {
+  override def outputEncoder: Encoder[Option[Double]] = 
ExpressionEncoder[Option[Double]]()
+  override def finish(reduction: MutableDouble): Option[Double] = {
+    if (reduction != null) {
+      Some(reduction.value)
+    } else {
+      None
+    }
+  }
+}
+
+trait TypedMaxDouble[IN, OUT] extends Aggregator[IN, MutableDouble, OUT] {
+  val f: IN => 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.max(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.max(b1.value, b2.value)
+      b1
+    }
+  }
+
+  override def bufferEncoder: Encoder[MutableDouble] = 
Encoders.kryo[MutableDouble]
+}
+
+class JavaTypedMaxDouble[IN](override val f: IN => Double)
+  extends TypedMaxDouble[IN, java.lang.Double] {
+  override def outputEncoder: Encoder[java.lang.Double] = 
ExpressionEncoder[java.lang.Double]()
+  override def finish(reduction: MutableDouble): java.lang.Double = 
reduction.value
+  def this(f: MapFunction[IN, java.lang.Double]) = this((x: IN) => 
f.call(x).asInstanceOf[Double])
+}
+
+class ScalaTypedMaxDouble[IN](override val f: IN => Double)
+  extends TypedMaxDouble[IN, Option[Double]] {
+  override def outputEncoder: Encoder[Option[Double]] = 
ExpressionEncoder[Option[Double]]()
+  override def finish(reduction: MutableDouble): Option[Double] = {
+    if (reduction != null) {
+      Some(reduction.value)
+    } else {
+      None
+    }
+  }
+}
+
+trait TypedMinLong[IN, OUT] extends Aggregator[IN, MutableLong, OUT] {
+  val f: IN => Long
+  override def zero: MutableLong = null
+  override def reduce(b: MutableLong, a: IN): MutableLong = {
+    if (b == null) {
+      new MutableLong(f(a))
+    } else {
+      b.value = math.min(b.value, f(a))
+      b
+    }
+  }
+  override def merge(b1: MutableLong, b2: MutableLong): MutableLong = {
+    if (b1 == null) {
+      b2
+    } else if (b2 == null) {
+      b1
+    } else {
+      b1.value = math.min(b1.value, b2.value)
+      b1
+    }
+  }
+
+  override def bufferEncoder: Encoder[MutableLong] = Encoders.kryo[MutableLong]
+}
+
+class JavaTypedMinLong[IN](override val f: IN => Long) extends 
TypedMinLong[IN, java.lang.Long] {
+  override def outputEncoder: Encoder[java.lang.Long] = 
ExpressionEncoder[java.lang.Long]()
+  override def finish(reduction: MutableLong): java.lang.Long = reduction.value
+  def this(f: MapFunction[IN, java.lang.Long]) = this((x: IN) => 
f.call(x).asInstanceOf[Long])
+}
+
+class ScalaTypedMinLong[IN](override val f: IN => Long) extends 
TypedMinLong[IN, Option[Long]] {
+  override def outputEncoder: Encoder[Option[Long]] = 
ExpressionEncoder[Option[Long]]()
+  override def finish(reduction: MutableLong): Option[Long] = {
+    if (reduction != null) {
+      Some(reduction.value)
+    } else {
+      None
+    }
+  }
+}
+
+trait TypedMaxLong[IN, OUT] extends Aggregator[IN, MutableLong, OUT] {
+  val f: IN => Long
+  override def zero: MutableLong = null
+  override def reduce(b: MutableLong, a: IN): MutableLong = {
+    if (b == null) {
+      new MutableLong(f(a))
+    } else {
+      b.value = math.max(b.value, f(a))
+      b
+    }
+  }
+  override def merge(b1: MutableLong, b2: MutableLong): MutableLong = {
+    if (b1 == null) {
+      b2
+    } else if (b2 == null) {
+      b1
+    } else {
+      b1.value = math.max(b1.value, b2.value)
+      b1
+    }
+  }
+
+  override def bufferEncoder: Encoder[MutableLong] = Encoders.kryo[MutableLong]
+}
+
+class JavaTypedMaxLong[IN](override val f: IN => Long) extends 
TypedMaxLong[IN, java.lang.Long] {
+  override def outputEncoder: Encoder[java.lang.Long] = 
ExpressionEncoder[java.lang.Long]()
+  override def finish(reduction: MutableLong): java.lang.Long = reduction.value
 
 Review comment:
   ditto

----------------------------------------------------------------
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]

Reply via email to