Github user kiszk commented on a diff in the pull request:
https://github.com/apache/spark/pull/17164#discussion_r105403987
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/SortAggregateExec.scala
---
@@ -106,6 +105,126 @@ case class SortAggregateExec(
}
}
+ override def supportCodegen: Boolean = {
+ val aggregationBufferSchema =
StructType.fromAttributes(aggregateBufferAttributes)
+ aggregationBufferSchema.forall(f => UnsafeRow.isMutable(f.dataType)) &&
+ // ImperativeAggregate is not supported right now
+
!aggregateExpressions.exists(_.aggregateFunction.isInstanceOf[ImperativeAggregate])
+ }
+
+ override def inputRDDs(): Seq[RDD[InternalRow]] = {
+ child.asInstanceOf[CodegenSupport].inputRDDs()
+ }
+
+ override protected def doProduce(ctx: CodegenContext): String = {
+ if (groupingExpressions.isEmpty) {
+ doProduceWithoutKeys(ctx)
+ } else {
+ doProduceWithKeys(ctx)
+ }
+ }
+
+ override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row:
ExprCode): String = {
+ if (groupingExpressions.isEmpty) {
+ doConsumeWithoutKeys(ctx, input)
+ } else {
+ doConsumeWithKeys(ctx, input)
+ }
+ }
+
+ private def doProduceWithoutKeys(ctx: CodegenContext): String = {
+ generateBufVarsEvalCode(ctx)
+ }
+
+ private def doConsumeWithoutKeys(ctx: CodegenContext, input:
Seq[ExprCode]): String = {
+ generateBufVarsUpdateCode(ctx, input)
+ }
+
+ // The grouping keys of a current partition
+ private var currentGroupingKeyTerm: String = _
+
+ // The output code for a single partition
+ private var outputCode: String = _
+
+ private def generateOutputCode(ctx: CodegenContext): String = {
+ ctx.currentVars = bufVars
+ val bufferEv = GenerateUnsafeProjection.createCode(
+ ctx, aggregateBufferAttributes.map(
+ BindReferences.bindReference[Expression](_,
aggregateBufferAttributes)))
+ val sortAggregate = ctx.addReferenceObj("sortAggregate", this)
+ s"""
+ |${bufferEv.code}
+ |${generateResultCode(ctx, currentGroupingKeyTerm, bufferEv.value,
sortAggregate)}
+ """.stripMargin
+ }
+
+ private def doProduceWithKeys(ctx: CodegenContext): String = {
+ val numOutput = metricTerm(ctx, "numOutputRows")
+ s"""
+ |${child.asInstanceOf[CodegenSupport].produce(ctx, this)}
+ |
+ |if ($currentGroupingKeyTerm != null) {
+ | // for the last aggregation
+ | do {
+ | $numOutput.add(1);
+ | $outputCode
+ | $currentGroupingKeyTerm = null;
+ |
+ | if (shouldStop()) return;
+ | } while (false);
--- End diff --
Do we need this `do { } while (false);`?
---
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]