Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10960#discussion_r51533782
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Covariance.scala
 ---
    @@ -28,171 +26,90 @@ import org.apache.spark.sql.types._
      * When applied on empty data (i.e., count is zero), it returns NULL.
      *
      */
    -abstract class Covariance(left: Expression, right: Expression) extends 
ImperativeAggregate
    -    with Serializable {
    -  override def children: Seq[Expression] = Seq(left, right)
    +abstract class Covariance(x: Expression, y: Expression) extends 
DeclarativeAggregate {
     
    +  override def children: Seq[Expression] = Seq(x, y)
       override def nullable: Boolean = true
    -
       override def dataType: DataType = DoubleType
    -
       override def inputTypes: Seq[AbstractDataType] = Seq(DoubleType, 
DoubleType)
     
    -  override def checkInputDataTypes(): TypeCheckResult = {
    -    if (left.dataType.isInstanceOf[DoubleType] && 
right.dataType.isInstanceOf[DoubleType]) {
    -      TypeCheckResult.TypeCheckSuccess
    +  protected val count = AttributeReference("count", DoubleType, nullable = 
false)()
    +  protected val xAvg = AttributeReference("xAvg", DoubleType, nullable = 
false)()
    +  protected val yAvg = AttributeReference("yAvg", DoubleType, nullable = 
false)()
    +  protected val ck = AttributeReference("ck", DoubleType, nullable = 
false)()
    +
    +  override val aggBufferAttributes: Seq[AttributeReference] = Seq(count, 
xAvg, yAvg, ck)
    +
    +  override val initialValues: Seq[Expression] = Seq(
    +    /* count = */ Literal(0.0),
    +    /* xAvg = */ Literal(0.0),
    +    /* yAvg = */ Literal(0.0),
    +    /* ck = */ Literal(0.0)
    +  )
    +
    +  override lazy val updateExpressions: Seq[Expression] = {
    +    val n = count + Literal(1.0)
    +    val dx = x - xAvg
    +    val dy = y - yAvg
    +    val dyN = dy / n
    +    val newXAvg = xAvg + dx / n
    +    val newYAvg = yAvg + dyN
    +    val newCk = ck + dx * (dy - dyN)
    +
    +    val isNull = Or(IsNull(x), IsNull(y))
    --- End diff --
    
    Ditto.


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

Reply via email to