Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/8587#discussion_r43090262
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/functions.scala
---
@@ -524,6 +525,116 @@ case class Sum(child: Expression) extends
DeclarativeAggregate {
override val evaluateExpression = Cast(currentSum, resultType)
}
+case class Corr(
+ left: Expression,
+ right: Expression,
+ mutableAggBufferOffset: Int = 0,
+ inputAggBufferOffset: Int = 0)
+ extends ImperativeAggregate {
+
+ def children: Seq[Expression] = Seq(left, right)
+
+ def nullable: Boolean = false
+
+ def dataType: DataType = DoubleType
+
+ def inputTypes: Seq[AbstractDataType] = Seq(DoubleType)
+
+ def aggBufferSchema: StructType =
StructType.fromAttributes(aggBufferAttributes)
+
+ def inputAggBufferAttributes: Seq[AttributeReference] =
aggBufferAttributes.map(_.newInstance())
+
+ val aggBufferAttributes: Seq[AttributeReference] = Seq(
+ AttributeReference("xAvg", DoubleType)(),
+ AttributeReference("yAvg", DoubleType)(),
+ AttributeReference("Ck", DoubleType)(),
+ AttributeReference("MkX", DoubleType)(),
+ AttributeReference("MkY", DoubleType)(),
+ AttributeReference("count", LongType)())
+
+ override def withNewMutableAggBufferOffset(newMutableAggBufferOffset:
Int): ImperativeAggregate =
+ copy(mutableAggBufferOffset = newMutableAggBufferOffset)
+
+ override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int):
ImperativeAggregate =
+ copy(inputAggBufferOffset = newInputAggBufferOffset)
+
+ override def initialize(buffer: MutableRow): Unit = {
+ (0 until 5).map(idx => buffer.setDouble(mutableAggBufferOffset + idx,
0.0))
+ buffer.setLong(mutableAggBufferOffset + 5, 0L)
+ }
+
+ override def update(buffer: MutableRow, input: InternalRow): Unit = {
+ val x = left.eval(input).asInstanceOf[Double]
+ val y = right.eval(input).asInstanceOf[Double]
+
+ var xAvg = buffer.getDouble(mutableAggBufferOffset)
+ var yAvg = buffer.getDouble(mutableAggBufferOffset + 1)
--- End diff --
It might be faster to cache the offsets as `private[this] var`s.
---
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]