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

    https://github.com/apache/spark/pull/17164#discussion_r105407263
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/AggregateCodegenHelper.scala
 ---
    @@ -0,0 +1,226 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.execution.aggregate
    +
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.expressions.aggregate._
    +import org.apache.spark.sql.catalyst.expressions.codegen._
    +import org.apache.spark.sql.execution.CodegenSupport
    +import org.apache.spark.sql.types.StructType
    +
    +trait AggregateCodegenHelper {
    +  self: AggregateExec with CodegenSupport =>
    +
    +  protected val groupingAttributes = groupingExpressions.map(_.toAttribute)
    +  protected val groupingKeySchema = 
StructType.fromAttributes(groupingAttributes)
    +  protected val bufferSchema = 
StructType.fromAttributes(aggregateBufferAttributes)
    +
    +  protected lazy val declFunctions =
    +    
aggregateExpressions.map(_.aggregateFunction.asInstanceOf[DeclarativeAggregate])
    +
    +  protected var bufVars: Seq[ExprCode] = _
    +
    +  override def usedInputs: AttributeSet = inputSet
    +
    +  protected def generateBufVarsInitCode(ctx: CodegenContext): String = {
    +    // generate variables for aggregation buffer
    +    val initExpr = declFunctions.flatMap(f => f.initialValues)
    +    bufVars = initExpr.map { e =>
    +      val isNull = ctx.freshName("bufIsNull")
    +      val value = ctx.freshName("bufValue")
    +      ctx.addMutableState("boolean", isNull, "")
    +      ctx.addMutableState(ctx.javaType(e.dataType), value, "")
    +      // The initial expression should not access any column
    +      val ev = e.genCode(ctx)
    +      val initVars = s"""
    +         | $isNull = ${ev.isNull};
    +         | $value = ${ev.value};
    +       """.stripMargin
    +      ExprCode(ev.code + initVars, isNull, value)
    +    }
    +    evaluateVariables(bufVars)
    +  }
    +
    +  protected def generateBufVarsEvalCode(ctx: CodegenContext): String = {
    +    val initAgg = ctx.freshName("initAgg")
    +    ctx.addMutableState("boolean", initAgg, s"$initAgg = false;")
    +
    +    val initBufVar = generateBufVarsInitCode(ctx)
    +
    +    // generate variables for output
    +    val (resultVars, genResult) = if (modes.contains(Final) || 
modes.contains(Complete)) {
    +      // evaluate aggregate results
    +      ctx.currentVars = bufVars
    +      val aggResults = declFunctions.map(_.evaluateExpression).map { e =>
    +        BindReferences.bindReference(e, 
aggregateBufferAttributes).genCode(ctx)
    +      }
    +      val evaluateAggResults = evaluateVariables(aggResults)
    +      // evaluate result expressions
    +      ctx.currentVars = aggResults
    +      val resultVars = resultExpressions.map { e =>
    +        BindReferences.bindReference(e, aggregateAttributes).genCode(ctx)
    +      }
    +      (resultVars, s"""
    +        |$evaluateAggResults
    +        |${evaluateVariables(resultVars)}
    +       """.stripMargin)
    +    } else if (modes.contains(Partial) || modes.contains(PartialMerge)) {
    +      // output the aggregate buffer directly
    +      (bufVars, "")
    +    } else {
    +      // no aggregate function, the result should be literals
    +      val resultVars = resultExpressions.map(_.genCode(ctx))
    +      (resultVars, evaluateVariables(resultVars))
    +    }
    +
    +    val doAgg = ctx.freshName("doAggregateWithoutKey")
    +    ctx.addNewFunction(doAgg,
    +      s"""
    +         | private void $doAgg() throws java.io.IOException {
    +         |   // initialize aggregation buffer
    +         |   $initBufVar
    +         |
    +         |   ${child.asInstanceOf[CodegenSupport].produce(ctx, this)}
    +         | }
    +       """.stripMargin)
    +
    +    val numOutput = metricTerm(ctx, "numOutputRows")
    +    val aggTime = metricTerm(ctx, "aggTime")
    +    val beforeAgg = ctx.freshName("beforeAgg")
    +    s"""
    +       | while (!$initAgg) {
    --- End diff --
    
    I see, thanks


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