-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-27296][SQL] Allows Aggregator to be registered as a UDF #25024
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
Changes from all commits
95156fd
4a1e01a
b0ec357
f7ac390
dbef793
3fa704c
8e886f6
a85ab11
e76c0bc
fc57259
651dedd
cc48d05
6e4d99f
5ae329a
a584a33
7b1ddbe
912a87a
ad56238
be55f93
b51506c
0c0e46e
ef8392e
b4ad3bc
6012f30
4eaf751
986a3b4
1dead9d
3f0cfec
eb95998
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 |
|---|---|---|
|
|
@@ -17,13 +17,17 @@ | |
|
|
||
| package org.apache.spark.sql.execution.aggregate | ||
|
|
||
| import scala.reflect.runtime.universe.TypeTag | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.{Column, Row} | ||
| import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow} | ||
| import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression, _} | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.ImperativeAggregate | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.GenerateMutableProjection | ||
| import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction} | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete} | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.{ImperativeAggregate, TypedImperativeAggregate} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{GenerateMutableProjection, GenerateSafeProjection} | ||
| import org.apache.spark.sql.expressions.{Aggregator, MutableAggregationBuffer, UserDefinedAggregateFunction} | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
|
|
@@ -450,3 +454,63 @@ case class ScalaUDAF( | |
|
|
||
| override def nodeName: String = udaf.getClass.getSimpleName | ||
| } | ||
|
|
||
| case class ScalaAggregator[IN, BUF, OUT]( | ||
| children: Seq[Expression], | ||
| agg: Aggregator[IN, BUF, OUT], | ||
| inputEncoderNR: ExpressionEncoder[IN], | ||
| nullable: Boolean = true, | ||
| isDeterministic: Boolean = true, | ||
| mutableAggBufferOffset: Int = 0, | ||
| inputAggBufferOffset: Int = 0) | ||
| extends TypedImperativeAggregate[BUF] | ||
| with NonSQLExpression | ||
| with UserDefinedExpression | ||
| with ImplicitCastInputTypes | ||
| with Logging { | ||
|
|
||
| private[this] lazy val inputEncoder = inputEncoderNR.resolveAndBind() | ||
|
Contributor
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. I think this is the problem. We shouldn't keep the encoder unresolved in the query plan, and resolve it in the executor side. We can follow
Member
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. Yea, this defers resolving encoder to executors, we should resolve it on the driver.
Member
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. Thank you for pinging me, @cloud-fan . |
||
| private[this] lazy val bufferEncoder = | ||
| agg.bufferEncoder.asInstanceOf[ExpressionEncoder[BUF]].resolveAndBind() | ||
| private[this] lazy val outputEncoder = agg.outputEncoder.asInstanceOf[ExpressionEncoder[OUT]] | ||
|
|
||
| def dataType: DataType = outputEncoder.objSerializer.dataType | ||
|
|
||
| def inputTypes: Seq[DataType] = inputEncoder.schema.map(_.dataType) | ||
|
|
||
| override lazy val deterministic: Boolean = isDeterministic | ||
|
|
||
| def withNewMutableAggBufferOffset(newMutableAggBufferOffset: Int): ScalaAggregator[IN, BUF, OUT] = | ||
| copy(mutableAggBufferOffset = newMutableAggBufferOffset) | ||
|
|
||
| def withNewInputAggBufferOffset(newInputAggBufferOffset: Int): ScalaAggregator[IN, BUF, OUT] = | ||
| copy(inputAggBufferOffset = newInputAggBufferOffset) | ||
|
|
||
| private[this] lazy val inputProjection = UnsafeProjection.create(children) | ||
|
|
||
| def createAggregationBuffer(): BUF = agg.zero | ||
|
|
||
| def update(buffer: BUF, input: InternalRow): BUF = | ||
| agg.reduce(buffer, inputEncoder.fromRow(inputProjection(input))) | ||
|
|
||
| def merge(buffer: BUF, input: BUF): BUF = agg.merge(buffer, input) | ||
|
|
||
| def eval(buffer: BUF): Any = { | ||
| val row = outputEncoder.toRow(agg.finish(buffer)) | ||
| if (outputEncoder.isSerializedAsStruct) row else row.get(0, dataType) | ||
| } | ||
|
|
||
| private[this] lazy val bufferRow = new UnsafeRow(bufferEncoder.namedExpressions.length) | ||
|
|
||
| def serialize(agg: BUF): Array[Byte] = | ||
| bufferEncoder.toRow(agg).asInstanceOf[UnsafeRow].getBytes() | ||
|
|
||
| def deserialize(storageFormat: Array[Byte]): BUF = { | ||
| bufferRow.pointTo(storageFormat, storageFormat.length) | ||
| bufferEncoder.fromRow(bufferRow) | ||
| } | ||
|
|
||
| override def toString: String = s"""${nodeName}(${children.mkString(",")})""" | ||
|
|
||
| override def nodeName: String = agg.getClass.getSimpleName | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.