spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mengxr <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-12913] [SQL] Improve performance of sta...
Date Tue, 02 Feb 2016 07:36:24 GMT
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10960#discussion_r51533886
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/CentralMomentAgg.scala
---
    @@ -57,173 +55,170 @@ abstract class CentralMomentAgg(child: Expression) extends ImperativeAggregate
w
     
       override def dataType: DataType = DoubleType
     
    -  override def inputTypes: Seq[AbstractDataType] = Seq(NumericType)
    +  override def inputTypes: Seq[AbstractDataType] = Seq(DoubleType)
    +
    +  protected val count = AttributeReference("count", DoubleType, nullable = false)()
    +  protected val avg = AttributeReference("avg", DoubleType, nullable = false)()
    +  protected val m2 = AttributeReference("m2", DoubleType, nullable = false)()
    +  protected val m3 = AttributeReference("m3", DoubleType, nullable = false)()
    +  protected val m4 = AttributeReference("m4", DoubleType, nullable = false)()
    +
    +  override val aggBufferAttributes = Seq(count, avg, m2, m3, m4).take(momentOrder + 1)
    +
    +  override val initialValues: Seq[Expression] = Seq(
    +    /* count = */ Literal(0.0),
    +    /* avg = */ Literal(0.0),
    +    /* m2 = */ Literal(0.0),
    +    /* m3 = */ Literal(0.0),
    +    /* m4 = */ Literal(0.0)
    +  ).take(momentOrder + 1)
    +
    +  override lazy val updateExpressions: Seq[Expression] = {
    +    val n = count + Literal(1.0)
    +    val delta = child - avg
    +    val deltaN = delta / n
    +    val newAvg = avg + deltaN
    +    val newM2 = m2 + delta * (delta - deltaN)
    +
    +    val delta2 = delta * delta
    +    val deltaN2 = deltaN * deltaN
    +    val newM3 = m3 - Literal(3.0) * deltaN * newM2 + delta * (delta2 - deltaN2)
    +
    +    val newM4 = m4 - Literal(4.0) * deltaN * newM3 - Literal(6.0) * deltaN2 * newM2 +
    +      delta * (delta * delta2 - deltaN * deltaN2)
    +
    +    if (child.nullable) {
    +      Seq(
    +        /* count = */ If(IsNull(child), count, n),
    +        /* avg = */ If(IsNull(child), avg, newAvg),
    +        /* m2 = */ If(IsNull(child), m2, newM2),
    +        /* m3 = */ If(IsNull(child), m3, newM3),
    +        /* m4 = */ If(IsNull(child), m4, newM4)
    +      ).take(momentOrder + 1)
    +    } else {
    +      Seq(
    +        /* count = */ n,
    +        /* avg = */ newAvg,
    +        /* m2 = */ newM2,
    +        /* m3 = */ newM3,
    +        /* m4 = */ newM4
    +      ).take(momentOrder + 1)
    +    }
    +  }
     
    -  override def checkInputDataTypes(): TypeCheckResult =
    -    TypeUtils.checkForNumericExpr(child.dataType, s"function $prettyName")
    +  override lazy val mergeExpressions: Seq[Expression] = {
     
    -  override def aggBufferSchema: StructType = StructType.fromAttributes(aggBufferAttributes)
    +    val n1 = count.left
    +    val n2 = count.right
    +    val n = n1 + n2
    +    val delta = avg.right - avg.left
    +    val deltaN = If(EqualTo(n, Literal(0.0)), Literal(0.0), delta / n)
    +    val newAvg = avg.left + deltaN * n2
     
    -  /**
    -   * Size of aggregation buffer.
    -   */
    -  private[this] val bufferSize = 5
    +    // higher order moments computed according to:
    +    // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Higher-order_statistics
    +    val newM2 = m2.left + m2.right + delta * deltaN * n1 * n2
    +    val newM3 = if (momentOrder >= 3) {
    +      m3.left + m3.right + deltaN * deltaN * delta * n1 * n2 * (n1 - n2) +
    +        Literal(3.0) * deltaN * (n1 * m2.right - n2 * m2.left)
    +    } else {
    +      Literal(0.0)
    +    }
    +    val newM4 = if (momentOrder >= 4) {
    +      m4.left + m4.right +
    +        deltaN * deltaN * deltaN * delta * n1 * n2 * (n1 * n1 - n1 * n2 + n2 * n2) +
    +        Literal(6.0) * deltaN * deltaN * (n1 * n1 * m2.right + n2 * n2 * m2.left) +
    +        Literal(4.0) * deltaN * (n1 * m3.right - n2 * m3.left)
    +    } else {
    +      Literal(0.0)
    +    }
     
    -  override val aggBufferAttributes: Seq[AttributeReference] = Seq.tabulate(bufferSize)
{ i =>
    -    AttributeReference(s"M$i", DoubleType)()
    +    Seq(
    +      /* count = */ n,
    +      /* avg = */ newAvg,
    +      /* m2 = */ newM2,
    +      /* m3 = */ newM3,
    +      /* m4 = */ newM4
    +    ).take(momentOrder + 1)
    --- End diff --
    
    This could be simplified to `trimHigherOrder(Seq(n, newAvg, newM2, newM3, newM4))`, which
won't reduce readability.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message