spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nongli <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-12797] [SQL] Generated TungstenAggregat...
Date Wed, 20 Jan 2016 18:11:55 GMT
Github user nongli commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10840#discussion_r50294103
  
    --- 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
    +    val updateExpr = if (modes.contains(Partial)) {
    +      functions.flatMap(_.updateExpressions)
    +    } else {
    +      functions.flatMap(_.mergeExpressions)
    +    }
    +
    +    val inputAttr = functions.flatMap(_.aggBufferAttributes) ++ child.output
    +    val boundExpr = updateExpr.map(e => BindReferences.bindReference(e, inputAttr))
    +    ctx.currentVars = bufVars ++ input
    +    // TODO: support subexpression elimination
    +    val codes = boundExpr.zipWithIndex.map { case (e, i) =>
    +      val ev = e.gen(ctx)
    +      if (e.nullable) {
    +        s"""
    +           | ${ev.code}
    +           | ${bufVars(i).isNull} = ${ev.isNull};
    +           | if (!${ev.isNull}) {
    --- End diff --
    
    is this null check necessary? Probably faster to just assign always (maybe garbage) if
that is safe.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message