Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.apache.spark.sql.catalyst.encoders.{encoderFor, ExpressionEncoder, Ou
import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, CreateStruct}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.execution.QueryExecution
import org.apache.spark.sql.expressions.Aggregator

/**
* :: Experimental ::
Expand Down Expand Up @@ -177,10 +178,33 @@ class KeyValueGroupedDataset[K, V] private[sql](
* @since 1.6.0
*/
def reduceGroups(f: (V, V) => V): Dataset[(K, V)] = {
val func = (key: K, it: Iterator[V]) => Iterator((key, it.reduce(f)))
val encoder = encoderFor[V]
val intEncoder: ExpressionEncoder[Int] = ExpressionEncoder()
val aggregator: TypedColumn[V, V] = new Aggregator[V, (Int, V), V] {
def bufferEncoder: Encoder[(Int, V)] = ExpressionEncoder.tuple(intEncoder, encoder)
def outputEncoder: Encoder[V] = encoder

implicit val resultEncoder = ExpressionEncoder.tuple(kExprEnc, vExprEnc)
flatMapGroups(func)
def zero: (Int, V) = (0, null.asInstanceOf[V])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem with Aggregator here is the zero value. This PR uses an Int (can be Boolean too) to indicate if the buffer is initialized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pull this out to be a generic ReduceAggregator and add unit test for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I will update this.

def reduce(reducedValue: (Int, V), value: V): (Int, V) = {
if (reducedValue._1 == 0) {
(1, value)
} else {
(1, f(reducedValue._2, value))
}
}
def merge(buf1: (Int, V), buf2: (Int, V)): (Int, V) = {
if (buf1._1 == 0) {
buf2
} else if (buf2._2 == 0) {
buf1
} else {
(1, f(buf1._2, buf2._2))
}
}
def finish(result: (Int, V)): V = result._2
}.toColumn

agg(aggregator)
}

/**
Expand Down