cloud-fan commented on code in PR #54011:
URL: https://github.com/apache/spark/pull/54011#discussion_r2740088663
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/vectorExpressions.scala:
##########
@@ -328,3 +344,469 @@ case class VectorNormalize(vector: Expression, degree:
Expression)
copy(vector = newChildren(0), degree = newChildren(1))
}
}
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = """
+ _FUNC_(array) - Returns the element-wise mean of float vectors in a group.
+ All vectors must have the same dimension.
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_(col) FROM VALUES (array(1.0F, 2.0F)), (array(3.0F,
4.0F)) AS tab(col);
+ [2.0,3.0]
+ """,
+ since = "4.2.0",
+ group = "vector_funcs"
+)
+// scalastyle:on line.size.limit
+case class VectorAvg(
+ child: Expression,
+ mutableAggBufferOffset: Int = 0,
+ inputAggBufferOffset: Int = 0
+) extends ImperativeAggregate
+ with UnaryLike[Expression]
+ with QueryErrorsBase {
+
+ def this(child: Expression) = this(child, 0, 0)
+
+ override def prettyName: String = "vector_avg"
+
+ override def nullable: Boolean = true
+
+ override def dataType: DataType = ArrayType(FloatType, containsNull = false)
+
+ override def checkInputDataTypes(): TypeCheckResult = {
+ child.dataType match {
+ case ArrayType(FloatType, _) =>
+ TypeCheckResult.TypeCheckSuccess
+ case _ =>
+ DataTypeMismatch(
+ errorSubClass = "UNEXPECTED_INPUT_TYPE",
+ messageParameters = Map(
+ "paramIndex" -> ordinalNumber(0),
+ "requiredType" -> toSQLType(ArrayType(FloatType)),
+ "inputSql" -> toSQLExpr(child),
+ "inputType" -> toSQLType(child.dataType)
+ )
+ )
+ }
+ }
+
+ // Aggregate buffer schema: (avg: BINARY, dim: INTEGER, count: LONG)
+ // avg is a BINARY representation of the average vector of floats in the
group
+ // dim is the dimension of the vector
+ // count is the number of vectors in the group
+ // null avg means no valid input has been seen yet
+ private lazy val avgAttr = AttributeReference(
+ "avg",
+ BinaryType,
+ nullable = true
+ )()
+ private lazy val dimAttr = AttributeReference(
+ "dim",
+ IntegerType,
+ nullable = true
+ )()
+ private lazy val countAttr =
+ AttributeReference("count", LongType, nullable = false)()
+
+ override def aggBufferSchema: StructType = StructType(
Review Comment:
I think this has a default implementation based on `aggBufferAttributes`, we
don't need to override it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]