-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-12797] [SQL] Generated TungstenAggregate (without grouping keys) #10840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
ce5bed0
006f37a
60ebebc
ef2ddd7
7ccd901
1beb7f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,10 @@ import org.apache.spark.rdd.RDD | |
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.errors._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} | ||
| import org.apache.spark.sql.catalyst.plans.physical._ | ||
| import org.apache.spark.sql.execution.{SparkPlan, UnaryNode, UnsafeFixedWidthAggregationMap} | ||
| import org.apache.spark.sql.execution.{CodegenSupport, SparkPlan, UnaryNode, UnsafeFixedWidthAggregationMap} | ||
| import org.apache.spark.sql.execution.metric.SQLMetrics | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
|
|
@@ -35,7 +36,7 @@ case class TungstenAggregate( | |
| initialInputBufferOffset: Int, | ||
| resultExpressions: Seq[NamedExpression], | ||
| child: SparkPlan) | ||
| extends UnaryNode { | ||
| extends UnaryNode with CodegenSupport { | ||
|
|
||
| private[this] val aggregateBufferAttributes = { | ||
| aggregateExpressions.flatMap(_.aggregateFunction.aggBufferAttributes) | ||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. model -> mode |
||
| 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}) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this null check necessary? Probably faster to just assign always (maybe garbage) if that is safe. |
||
| | ${bufVars(i).value} = ${ev.value}; | ||
| | } | ||
| """.stripMargin | ||
| } else { | ||
| s""" | ||
| | ${ev.code} | ||
| | ${bufVars(i).value} = ${ev.value}; | ||
| """.stripMargin | ||
| } | ||
| } | ||
|
|
||
| s""" | ||
| | // do aggregate and update aggregation buffer | ||
| | ${codes.mkString("")} | ||
| """.stripMargin | ||
| } | ||
|
|
||
| override def simpleString: String = { | ||
| val allAggregateExpressions = aggregateExpressions | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,9 @@ | |
|
|
||
| package org.apache.spark.sql.execution | ||
|
|
||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.execution.aggregate.TungstenAggregate | ||
| import org.apache.spark.sql.test.SharedSQLContext | ||
|
|
||
| class WholeStageCodegenSuite extends SparkPlanTest with SharedSQLContext { | ||
|
|
@@ -35,4 +37,13 @@ class WholeStageCodegenSuite extends SparkPlanTest with SharedSQLContext { | |
| sortAnswers = false | ||
| ) | ||
| } | ||
|
|
||
| test("Aggregate should be included in WholeStageCodegen") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a case with multiple agg exprs? |
||
| val df = sqlContext.range(10).filter("id > 1").groupBy().count() | ||
| val plan = df.queryExecution.executedPlan | ||
| assert(plan.find(p => | ||
| p.isInstanceOf[WholeStageCodegen] && | ||
| p.asInstanceOf[WholeStageCodegen].plan.isInstanceOf[TungstenAggregate]).isDefined) | ||
| assert(df.collect() === Array(Row(8))) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit intuitive: To pass "false" as the name of the variable when it is non-nullable. We should consider null or something like that (future patch).