-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16391][SQL] Support partial aggregation for reduceGroups #14576
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1135773
Support partial aggregation for reduceGroups.
viirya 7e8d8c1
Add ReduceAggregator.
viirya 9de56ee
Merge pull request #14222 from viirya/improve-reducegroups
rxin 53d4879
initial checkin
rxin f9cee82
Merge branch 'master' into reduceAggregator
rxin 19b00f5
Updated some comments
rxin e5590af
Merge branch 'master' into reduceAggregator
rxin 1b57b74
Private APIs
rxin 50ed0d8
code review
rxin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add ReduceAggregator.
- Loading branch information
commit 7e8d8c116552642573cc89bd11fc2e82f2a0f82a
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
sql/core/src/main/scala/org/apache/spark/sql/expressions/ReduceAggregator.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.expressions | ||
|
|
||
| import org.apache.spark.annotation.Experimental | ||
| import org.apache.spark.sql.Encoder | ||
| import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
|
|
||
| /** | ||
| * :: Experimental :: | ||
| * A generic class for reduce aggregations, which accepts a reduce function that can be used to take | ||
| * all of the elements of a group and reduce them to a single value. | ||
| * | ||
| * @tparam T The input and output type for the reduce function. | ||
| * @param func The reduce aggregation function. | ||
| * @param encoder The encoder for the input and output type of the reduce function. | ||
| * @since 2.1.0 | ||
| */ | ||
| @Experimental | ||
| private[sql] class ReduceAggregator[T](func: (T, T) => T, encoder: ExpressionEncoder[T]) | ||
| extends Aggregator[T, (Boolean, T), T] { | ||
|
|
||
| /** | ||
| * A zero value for this aggregation. It is represented as a Tuple2. The first element of the | ||
| * tuple is a false boolean value indicating the buffer is not initialized. The second element | ||
| * is initialized as a null value. | ||
| * @since 2.1.0 | ||
| */ | ||
| override def zero: (Boolean, T) = (false, null.asInstanceOf[T]) | ||
|
|
||
| override def bufferEncoder: Encoder[(Boolean, T)] = | ||
| ExpressionEncoder.tuple(ExpressionEncoder[Boolean](), encoder) | ||
|
|
||
| override def outputEncoder: Encoder[T] = encoder | ||
|
|
||
| /** | ||
| * Combine two values to produce a new value. If the buffer `b` is not initialized, it simply | ||
| * takes the value of `a` and set the initialization flag to `true`. | ||
| * @since 2.1.0 | ||
| */ | ||
| override def reduce(b: (Boolean, T), a: T): (Boolean, T) = { | ||
| if (b._1) { | ||
| (true, func(b._2, a)) | ||
| } else { | ||
| (true, a) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Merge two intermediate values. As it is possibly that the buffer is just the `zero` value | ||
| * coming from empty partition, it checks if the buffers are initialized, and only performs | ||
| * merging when they are initialized both. | ||
| * @since 2.1.0 | ||
| */ | ||
| override def merge(b1: (Boolean, T), b2: (Boolean, T)): (Boolean, T) = { | ||
| if (!b1._1) { | ||
| b2 | ||
| } else if (!b2._1) { | ||
| b1 | ||
| } else { | ||
| (true, func(b1._2, b2._2)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Transform the output of the reduction. Simply output the value in the buffer. | ||
| * @since 2.1.0 | ||
| */ | ||
| override def finish(reduction: (Boolean, T)): T = { | ||
| reduction._2 | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
sql/core/src/test/scala/org/apache/spark/sql/expressions/ReduceAggregatorSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.expressions | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
| import org.apache.spark.sql.expressions.ReduceAggregator | ||
|
|
||
| class ReduceAggregatorSuite extends SparkFunSuite { | ||
| test("zero value") { | ||
| val encoder: ExpressionEncoder[Int] = ExpressionEncoder() | ||
| val func = (v1: Int, v2: Int) => v1 + v2 | ||
| val aggregator: ReduceAggregator[Int] = new ReduceAggregator(func, encoder) | ||
| assert(aggregator.zero == (false, null)) | ||
| } | ||
|
|
||
| test("reduce, merge and finish") { | ||
| val encoder: ExpressionEncoder[Int] = ExpressionEncoder() | ||
| val func = (v1: Int, v2: Int) => v1 + v2 | ||
| val aggregator: ReduceAggregator[Int] = new ReduceAggregator(func, encoder) | ||
|
|
||
| val firstReduce = aggregator.reduce(aggregator.zero, 1) | ||
| assert(firstReduce == (true, 1)) | ||
|
|
||
| val secondReduce = aggregator.reduce(firstReduce, 2) | ||
| assert(secondReduce == (true, 3)) | ||
|
|
||
| val thirdReduce = aggregator.reduce(secondReduce, 3) | ||
| assert(thirdReduce == (true, 6)) | ||
|
|
||
| val mergeWithZero1 = aggregator.merge(aggregator.zero, firstReduce) | ||
| assert(mergeWithZero1 == (true, 1)) | ||
|
|
||
| val mergeWithZero2 = aggregator.merge(secondReduce, aggregator.zero) | ||
| assert(mergeWithZero2 == (true, 3)) | ||
|
|
||
| val mergeTwoReduced = aggregator.merge(firstReduce, secondReduce) | ||
| assert(mergeTwoReduced == (true, 4)) | ||
|
|
||
| assert(aggregator.finish(firstReduce)== 1) | ||
| assert(aggregator.finish(secondReduce) == 3) | ||
| assert(aggregator.finish(thirdReduce) == 6) | ||
| assert(aggregator.finish(mergeWithZero1) == 1) | ||
| assert(aggregator.finish(mergeWithZero2) == 3) | ||
| assert(aggregator.finish(mergeTwoReduced) == 4) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Encoders.scalaBoolean?