-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-17187][SQL] Supports using arbitrary Java object as internal aggregation buffer object #14753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-17187][SQL] Supports using arbitrary Java object as internal aggregation buffer object #14753
Changes from all commits
10861b2
0fdc1ea
d3108ab
2873765
7190eb0
5904bcd
8c8bd9a
7e7cb85
86166a1
e060d21
ac8e36a
ca574e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -389,3 +389,144 @@ abstract class DeclarativeAggregate | |
| def right: AttributeReference = inputAggBufferAttributes(aggBufferAttributes.indexOf(a)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Aggregation function which allows **arbitrary** user-defined java object to be used as internal | ||
| * aggregation buffer object. | ||
| * | ||
| * {{{ | ||
| * aggregation buffer for normal aggregation function `avg` | ||
| * | | ||
| * v | ||
| * +--------------+---------------+-----------------------------------+ | ||
| * | sum1 (Long) | count1 (Long) | generic user-defined java objects | | ||
| * +--------------+---------------+-----------------------------------+ | ||
| * ^ | ||
| * | | ||
| * Aggregation buffer object for `TypedImperativeAggregate` aggregation function | ||
| * }}} | ||
| * | ||
| * Work flow (Partial mode aggregate at Mapper side, and Final mode aggregate at Reducer side): | ||
| * | ||
| * Stage 1: Partial aggregate at Mapper side: | ||
| * | ||
| * 1. The framework calls `createAggregationBuffer(): T` to create an empty internal aggregation | ||
| * buffer object. | ||
| * 2. Upon each input row, the framework calls | ||
| * `update(buffer: T, input: InternalRow): Unit` to update the aggregation buffer object T. | ||
| * 3. After processing all rows of current group (group by key), the framework will serialize | ||
| * aggregation buffer object T to storage format (Array[Byte]) and persist the Array[Byte] | ||
| * to disk if needed. | ||
| * 4. The framework moves on to next group, until all groups have been processed. | ||
| * | ||
| * Shuffling exchange data to Reducer tasks... | ||
| * | ||
| * Stage 2: Final mode aggregate at Reducer side: | ||
| * | ||
| * 1. The framework calls `createAggregationBuffer(): T` to create an empty internal aggregation | ||
| * buffer object (type T) for merging. | ||
| * 2. For each aggregation output of Stage 1, The framework de-serializes the storage | ||
| * format (Array[Byte]) and produces one input aggregation object (type T). | ||
| * 3. For each input aggregation object, the framework calls `merge(buffer: T, input: T): Unit` | ||
| * to merge the input aggregation object into aggregation buffer object. | ||
| * 4. After processing all input aggregation objects of current group (group by key), the framework | ||
| * calls method `eval(buffer: T)` to generate the final output for this group. | ||
| * 5. The framework moves on to next group, until all groups have been processed. | ||
| * | ||
| * NOTE: SQL with TypedImperativeAggregate functions is planned in sort based aggregation, | ||
| * instead of hash based aggregation, as TypedImperativeAggregate use BinaryType as aggregation | ||
| * buffer's storage format, which is not supported by hash based aggregation. Hash based | ||
| * aggregation only support aggregation buffer of mutable types (like LongType, IntType that have | ||
| * fixed length and can be mutated in place in UnsafeRow) | ||
| */ | ||
| abstract class TypedImperativeAggregate[T] extends ImperativeAggregate { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this the wrong way around? Isn't I know this has been done for engineering purposes, but I still wonder if we shouldn't reverse the hierarchy here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| /** | ||
| * Creates an empty aggregation buffer object. This is called before processing each key group | ||
| * (group by key). | ||
| * | ||
| * @return an aggregation buffer object | ||
| */ | ||
| def createAggregationBuffer(): T | ||
|
|
||
| /** | ||
| * In-place updates the aggregation buffer object with an input row. buffer = buffer + input. | ||
| * 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): Unit | ||
|
||
|
|
||
| /** | ||
| * Merges an input aggregation object into aggregation buffer object. buffer = buffer + input. | ||
| * 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): Unit | ||
|
||
|
|
||
| /** | ||
| * Generates the final aggregation result value for current key group with the aggregation buffer | ||
| * object. | ||
| * | ||
| * @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] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we limit the serializable format to The reason is that SpecialMutableRow will do type check for atomic types for each There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This detail deserves a comment in the code. |
||
|
|
||
| /** De-serializes the serialized format Array[Byte], and produces aggregation buffer object T */ | ||
| def deserialize(storageFormat: Array[Byte]): T | ||
|
|
||
| final override def initialize(buffer: MutableRow): Unit = { | ||
| val bufferObject = createAggregationBuffer() | ||
| buffer.update(mutableAggBufferOffset, bufferObject) | ||
| } | ||
|
|
||
| final override def update(buffer: MutableRow, input: InternalRow): Unit = { | ||
| val bufferObject = getField[T](buffer, mutableAggBufferOffset) | ||
| update(bufferObject, input) | ||
| } | ||
|
|
||
| final override def merge(buffer: MutableRow, inputBuffer: InternalRow): Unit = { | ||
| val bufferObject = getField[T](buffer, mutableAggBufferOffset) | ||
| // The inputBuffer stores serialized aggregation buffer object produced by partial aggregate | ||
| val inputObject = deserialize(inputBuffer.getBinary(inputAggBufferOffset)) | ||
| merge(bufferObject, inputObject) | ||
| } | ||
|
|
||
| final override def eval(buffer: InternalRow): Any = { | ||
| val bufferObject = getField[T](buffer, mutableAggBufferOffset) | ||
| eval(bufferObject) | ||
| } | ||
|
|
||
| private[this] val anyObjectType = ObjectType(classOf[AnyRef]) | ||
| private def getField[U](input: InternalRow, fieldIndex: Int): U = { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems we only need |
||
| input.get(fieldIndex, anyObjectType).asInstanceOf[U] | ||
| } | ||
|
|
||
| final override lazy val aggBufferAttributes: Seq[AttributeReference] = { | ||
| // Underlying storage type for the aggregation buffer object | ||
| Seq(AttributeReference("buf", BinaryType)()) | ||
| } | ||
|
|
||
| final override lazy val inputAggBufferAttributes: Seq[AttributeReference] = | ||
| aggBufferAttributes.map(_.newInstance()) | ||
|
|
||
| final 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). | ||
| */ | ||
| final def serializeAggregateBufferInPlace(buffer: MutableRow): Unit = { | ||
| val bufferObject = getField[T](buffer, mutableAggBufferOffset) | ||
| buffer(mutableAggBufferOffset) = serialize(bufferObject) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,7 +234,22 @@ abstract class AggregationIterator( | |
| val resultProjection = UnsafeProjection.create( | ||
| groupingAttributes ++ bufferAttributes, | ||
| groupingAttributes ++ bufferAttributes) | ||
|
|
||
| // TypedImperativeAggregate stores generic object in aggregation buffer, and requires | ||
| // calling serialization before shuffling. See [[TypedImperativeAggregate]] for more info. | ||
| val typedImperativeAggregates: Array[TypedImperativeAggregate[_]] = { | ||
| aggregateFunctions.collect { | ||
| case (ag: TypedImperativeAggregate[_]) => ag | ||
| } | ||
| } | ||
|
|
||
| (currentGroupingKey: UnsafeRow, currentBuffer: MutableRow) => { | ||
| // Serializes the generic object stored in aggregation buffer | ||
| var i = 0 | ||
| while (i < typedImperativeAggregates.length) { | ||
| typedImperativeAggregates(i).serializeAggregateBufferInPlace(currentBuffer) | ||
| i += 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @clockfly can you also address https://github.com/apache/spark/pull/14753/files#r76154000 ? |
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. foreach is equivalent with while |
||
| resultProjection(joinedRow(currentGroupingKey, currentBuffer)) | ||
| } | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add a normal agg buffer after the generic one. So, readers will not assume that generic ones will always be put at the end.