pgandhi999 commented on a change in pull request #23778: [SPARK-24935][SQL] :
Problem with Executing Hive UDF's from Spark 2.2 Onwards
URL: https://github.com/apache/spark/pull/23778#discussion_r260839543
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/interfaces.scala
##########
@@ -524,23 +524,142 @@ abstract class TypedImperativeAggregate[T] extends
ImperativeAggregate {
/** De-serializes the serialized format Array[Byte], and produces
aggregation buffer object T */
def deserialize(storageFormat: Array[Byte]): T
+ override def initialize(buffer: InternalRow): Unit = {
+ buffer(mutableAggBufferOffset) = createAggregationBuffer()
+ }
+
+ override def update(buffer: InternalRow, input: InternalRow): Unit = {
+ buffer(mutableAggBufferOffset) = update(getBufferObject(buffer), input)
+ }
+
+ override def merge(buffer: InternalRow, inputBuffer: InternalRow): Unit = {
+ val bufferObject = getBufferObject(buffer)
+ // The inputBuffer stores serialized aggregation buffer object produced by
partial aggregate
+ val inputObject = deserialize(inputBuffer.getBinary(inputAggBufferOffset))
+ buffer(mutableAggBufferOffset) = merge(bufferObject, inputObject)
+ }
+
+ override def eval(buffer: InternalRow): Any = {
+ eval(getBufferObject(buffer))
+ }
+
+ private[this] val anyObjectType = ObjectType(classOf[AnyRef])
+ private def getBufferObject(bufferRow: InternalRow): T = {
+ bufferRow.get(mutableAggBufferOffset, anyObjectType).asInstanceOf[T]
+ }
+
+ override lazy val aggBufferAttributes: Seq[AttributeReference] = {
+ // Underlying storage type for the aggregation buffer object
+ Seq(AttributeReference("buf", BinaryType)())
+ }
+
+ override lazy val inputAggBufferAttributes: Seq[AttributeReference] =
+ aggBufferAttributes.map(_.newInstance())
+
+ override def aggBufferSchema: StructType =
StructType.fromAttributes(aggBufferAttributes)
+
+ /**
+ * In-place replaces the aggregation buffer object stored at buffer's index
+ * `mutableAggBufferOffset`, with SparkSQL internally supported underlying
storage format
+ * (BinaryType).
+ *
+ * This is only called when doing Partial or PartialMerge mode aggregation,
before the framework
+ * shuffle out aggregate buffers.
+ */
+ def serializeAggregateBufferInPlace(buffer: InternalRow): Unit = {
+ buffer(mutableAggBufferOffset) = serialize(getBufferObject(buffer))
+ }
+}
+
+/**
+ * Aggregation function which allows **arbitrary** user-defined java object to
be used as internal
+ * aggregation buffer for Hive.
+ */
+abstract class HiveTypedImperativeAggregate[T] extends
TypedImperativeAggregate[T] {
+
+ /**
+ * Creates an empty aggregation buffer object for partial 1 mode. This is
called
+ * before processing each key group(group by key).
+ *
+ * @return an aggregation buffer object
+ */
+ def createAggregationBuffer(): T
+
+ /**
+ * Creates an empty aggregation buffer object for partial 2 mode.
+ *
+ * @return an aggregation buffer object
+ */
+ def createPartial2ModeAggregationBuffer(): T
+
+ var partial2ModeBuffer: InternalRow = _
+
+ /**
+ * Updates the aggregation buffer object with an input row and returns a new
buffer object. For
+ * performance, the function may do in-place update and return it instead of
constructing new
+ * buffer object.
+ *
+ * This is typically called when doing Partial or Complete mode aggregation.
+ *
+ * @param buffer The aggregation buffer object.
+ * @param input an input row
+ */
+ def update(buffer: T, input: InternalRow): T
+
+ /**
+ * Merges an input aggregation object into aggregation buffer object and
returns a new buffer
+ * object. For performance, the function may do in-place merge and return it
instead of
+ * constructing new buffer object.
+ *
+ * This is typically called when doing PartialMerge or Final mode
aggregation.
+ *
+ * @param buffer the aggregation buffer object used to store the aggregation
result.
+ * @param input an input aggregation object. Input aggregation object can be
produced by
+ * de-serializing the partial aggregate's output from Mapper
side.
+ */
+ def merge(buffer: T, input: T): T
+
+ /**
+ * Generates the final aggregation result value for current key group with
the aggregation buffer
+ * object.
+ *
+ * Developer note: the only return types accepted by Spark are:
+ * - primitive types
+ * - InternalRow and subclasses
+ * - ArrayData
+ * - MapData
+ *
+ * @param buffer aggregation buffer object.
+ * @return The aggregation result of current key group
+ */
+ def eval(buffer: T): Any
+
+ /** Serializes the aggregation buffer object T to Array[Byte] */
+ def serialize(buffer: T): Array[Byte]
+
+ /** De-serializes the serialized format Array[Byte], and produces
aggregation buffer object T */
+ def deserialize(storageFormat: Array[Byte]): T
+
final override def initialize(buffer: InternalRow): Unit = {
+ partial2ModeBuffer = buffer.copy()
+ partial2ModeBuffer(mutableAggBufferOffset) =
createPartial2ModeAggregationBuffer()
Review comment:
Sure. Take for an example, the test **"testDISTs"** in
`HiveWindowFunctionQuerySuite` where the query `"""
|select p_mfgr,p_name, p_size,
|histogram_numeric(p_retailprice, 5) over w1 as hist,
|percentile(p_partkey, cast(0.5 as double)) over w1 as per,
|row_number() over(distribute by p_mfgr sort by p_name) as rn
|from part
|window w1 as (distribute by p_mfgr sort by p_mfgr, p_name
| rows between 2 preceding and 2 following)
""".stripMargin` is a Hive UDAF that does not support partial aggregate.
The steps here that follow in the execution of this UDAF are:
**1. Initialize:** initialize the UDAF as well as the buffers for partial1
and partial2.
**2. Update:** update the UDAF with input data. Here, the partial1 buffer
holds the result of the update. Each time the update occurs, a copy operation
happens where the partial1 buffer is copied to the partial2 buffer.
**3. Eval(Terminate):** return the final result held by the partial2 buffer.
The actual sequence of calls goes like this: Initialize, Update, Update,
Update...., Eval, Initialize, Update, Update...., Eval and so on.
I admit that the part in the update where partial1 buffer is copied into
partial2 buffer each time seems a little less efficient to me and can be worked
on.
----------------------------------------------------------------
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]