Github user nongli commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10840#discussion_r50293986
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/TungstenAggregate.scala
 ---
    @@ -112,6 +113,103 @@ case class TungstenAggregate(
         }
       }
     
    +  override def supportCodegen: Boolean = {
    +    groupingExpressions.isEmpty &&
    +      // ImperativeAggregate is not supported right now
    +      
!aggregateExpressions.exists(_.aggregateFunction.isInstanceOf[ImperativeAggregate])
 &&
    +      // final aggregation only have one row, do not need to codegen
    +      !aggregateExpressions.exists(e => e.mode == Final || e.mode == 
Complete)
    +  }
    +
    +  // The variables used as aggregation buffer
    +  private var bufVars: Seq[ExprCode] = _
    +
    +  private val modes = aggregateExpressions.map(_.mode).distinct
    +
    +  protected override def doProduce(ctx: CodegenContext): 
(RDD[InternalRow], String) = {
    +    val initAgg = ctx.freshName("initAgg")
    +    ctx.addMutableState("boolean", initAgg, s"$initAgg = false;")
    +
    +    // generate variables for aggregation buffer
    +    val functions = 
aggregateExpressions.map(_.aggregateFunction.asInstanceOf[DeclarativeAggregate])
    +    val initExpr = functions.flatMap(f => f.initialValues)
    +    bufVars = initExpr.map { e =>
    +      var isNull = ctx.freshName("bufIsNull")
    +      val value = ctx.freshName("bufValue")
    +      // The initial expression should not access any column
    +      val ev = e.gen(ctx)
    +      val initVars = if (e.nullable) {
    +        s"""
    +           | boolean $isNull = ${ev.isNull};
    +           | ${ctx.javaType(e.dataType)} $value = 
${ctx.defaultValue(e.dataType)};
    +           | if (!${ev.isNull}) {
    +           |   $value = ${ev.value};
    +           | }
    +         """.stripMargin
    +      } else {
    +        isNull = "false"
    +        s"${ctx.javaType(e.dataType)} $value = ${ev.value};"
    +      }
    +      ExprCode(ev.code + initVars, isNull, value)
    +    }
    +
    +    val (rdd, childSource) = 
child.asInstanceOf[CodegenSupport].produce(ctx, this)
    +    val source =
    +      s"""
    +         | if (!$initAgg) {
    +         |   $initAgg = true;
    +         |
    +         |   // initialize aggregation buffer
    +         |   ${bufVars.map(_.code).mkString("\n")}
    +         |
    +         |   $childSource
    +         |
    +         |   // output the result
    +         |   ${consume(ctx, bufVars)}
    +         | }
    +       """.stripMargin
    +
    +    (rdd, source)
    +  }
    +
    +  override def doConsume(ctx: CodegenContext, child: SparkPlan, input: 
Seq[ExprCode]): String = {
    +    // only have DeclarativeAggregate
    +    val functions = 
aggregateExpressions.map(_.aggregateFunction.asInstanceOf[DeclarativeAggregate])
    +    // the model could be only Partial or PartialMerge
    --- End diff --
    
    model -> mode


---
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]

Reply via email to