Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/15703#discussion_r88382746
--- Diff: sql/hive/src/main/scala/org/apache/spark/sql/hive/hiveUDFs.scala
---
@@ -365,4 +380,66 @@ private[hive] case class HiveUDAFFunction(
val distinct = if (isDistinct) "DISTINCT " else " "
s"$name($distinct${children.map(_.sql).mkString(", ")})"
}
+
+ override def createAggregationBuffer(): AggregationBuffer =
+ partial1ModeEvaluator.getNewAggregationBuffer
+
+ @transient
+ private lazy val inputProjection = new InterpretedProjection(children)
+
+ override def update(buffer: AggregationBuffer, input: InternalRow): Unit
= {
+ partial1ModeEvaluator.iterate(
+ buffer, wrap(inputProjection(input), inputWrappers, cached,
inputDataTypes))
+ }
+
+ override def merge(buffer: AggregationBuffer, input: AggregationBuffer):
Unit = {
+ partial2ModeEvaluator.merge(buffer,
partial1ModeEvaluator.terminatePartial(input))
+ }
+
+ override def eval(buffer: AggregationBuffer): Any = {
+ resultUnwrapper(finalModeEvaluator.terminate(buffer))
+ }
+
+ override def serialize(buffer: AggregationBuffer): Array[Byte] = {
+ aggBufferSerDe.serialize(buffer)
+ }
+
+ override def deserialize(bytes: Array[Byte]): AggregationBuffer = {
+ aggBufferSerDe.deserialize(bytes)
+ }
+
+ // Helper class used to de/serialize Hive UDAF `AggregationBuffer`
objects
+ private class AggregationBufferSerDe {
+ private val partialResultUnwrapper =
unwrapperFor(partialResultInspector)
+
+ private val partialResultWrapper = wrapperFor(partialResultInspector,
partialResultDataType)
+
+ private val projection =
UnsafeProjection.create(Array(partialResultDataType))
+
+ private val mutableRow = new GenericInternalRow(1)
+
+ def serialize(buffer: AggregationBuffer): Array[Byte] = {
+ // `GenericUDAFEvaluator.terminatePartial()` converts an
`AggregationBuffer` into an object
+ // that can be inspected by the `ObjectInspector` returned by
`GenericUDAFEvaluator.init()`.
+ // Then we can unwrap it to a Spark SQL value.
+ mutableRow.update(0,
partialResultUnwrapper(partial1ModeEvaluator.terminatePartial(buffer)))
+ val unsafeRow = projection(mutableRow)
+ val bytes = ByteBuffer.allocate(unsafeRow.getSizeInBytes)
+ unsafeRow.writeTo(bytes)
+ bytes.array()
--- End diff --
but you also create an unnecessary `ByteBuffer`... as they are equivalent,
doesn't `unsafeRow.getBytes` simpler?
---
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]