rednaxelafx commented on a change in pull request #20965: [SPARK-21870][SQL] 
Split aggregation code into small functions
URL: https://github.com/apache/spark/pull/20965#discussion_r316428090
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/HashAggregateExec.scala
 ##########
 @@ -267,29 +302,81 @@ case class HashAggregateExec(
           
e.aggregateFunction.asInstanceOf[DeclarativeAggregate].mergeExpressions
       }
     }
-    ctx.currentVars = bufVars ++ input
-    val boundUpdateExpr = bindReferences(updateExpr, inputAttrs)
-    val subExprs = 
ctx.subexpressionEliminationForWholeStageCodegen(boundUpdateExpr)
-    val effectiveCodes = subExprs.codes.mkString("\n")
-    val aggVals = ctx.withSubExprEliminationExprs(subExprs.states) {
-      boundUpdateExpr.map(_.genCode(ctx))
-    }
-    // aggregate buffer should be updated atomic
-    val updates = aggVals.zipWithIndex.map { case (ev, i) =>
+
+    if (!conf.codegenSplitAggregateFunc) {
+      ctx.currentVars = bufVars ++ input
+      val boundUpdateExpr = updateExpr.map(BindReferences.bindReference(_, 
inputAttrs))
+      val subExprs = 
ctx.subexpressionEliminationForWholeStageCodegen(boundUpdateExpr)
+      val effectiveCodes = subExprs.codes.mkString("\n")
+      val aggVals = ctx.withSubExprEliminationExprs(subExprs.states) {
+        boundUpdateExpr.map(_.genCode(ctx))
+      }
+      // aggregate buffer should be updated atomic
+      val updates = aggVals.zipWithIndex.map { case (ev, i) =>
+        s"""
+           | ${bufVars(i).isNull} = ${ev.isNull};
+           | ${bufVars(i).value} = ${ev.value};
+       """.stripMargin
+      }
+      s"""
+         | // do aggregate
+         | // common sub-expressions
+         | $effectiveCodes
+         | // evaluate aggregate function
+         | ${evaluateVariables(aggVals)}
+         | // update aggregation buffer
+         | ${updates.mkString("\n").trim}
+     """.stripMargin
+    } else {
+      // We need to copy the aggregation buffer to local variables first 
because each aggregate
+      // function directly updates the buffer when it finishes.
+      val localBufVars = bufVars.zip(updateExpr).map { case (ev, e) =>
+        val isNull = ctx.freshName("localBufIsNull")
+        val value = ctx.freshName("localBufValue")
+        val initLocalVars = code"""
+           | boolean $isNull = ${ev.isNull};
+           | ${CodeGenerator.javaType(e.dataType)} $value = ${ev.value};
+         """.stripMargin
+        ExprCode(initLocalVars, JavaCode.isNullVariable(isNull),
+          JavaCode.variable(value, e.dataType))
+      }
+
+      val initLocalBufVar = evaluateVariables(localBufVars)
+
+      ctx.currentVars = localBufVars ++ input
+      val boundUpdateExpr = updateExpr.map(BindReferences.bindReference(_, 
inputAttrs))
+      val subExprs = 
ctx.subexpressionEliminationForWholeStageCodegen(boundUpdateExpr)
+      val effectiveCodes = subExprs.codes.mkString("\n")
+      val aggVals = ctx.withSubExprEliminationExprs(subExprs.states) {
+        boundUpdateExpr.map(_.genCode(ctx))
+      }
+
+      val evalAndUpdateCodes = aggVals.zipWithIndex.map { case (ev, i) =>
 
 Review comment:
   Again, change to `aggVals.zip(bufVars).map { case (ev, bufVar) =>` ?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to