Github user marmbrus commented on a diff in the pull request: https://github.com/apache/spark/pull/295#discussion_r11236377 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/aggregates.scala --- @@ -55,61 +54,135 @@ case class Aggregate( override def otherCopyArgs = sc :: Nil + // HACK: Generators don't correctly preserve their output through serializations so we grab + // out child's output attributes statically here. + val childOutput = child.output + def output = aggregateExpressions.map(_.toAttribute) - /* Replace all aggregate expressions with spark functions that will compute the result. */ - def createAggregateImplementations() = aggregateExpressions.map { agg => - val impl = agg transform { - case a: AggregateExpression => a.newInstance + case class ComputedAggregate( + unbound: AggregateExpression, // Unbound aggregate used for result substitution + aggregate: AggregateExpression, // A bound copy of this aggregate used to create a buffer + resultAttribute: AttributeReference) // An attribute used to refer to the result of this agg + + // A list of aggregates that need to be computed for each group. + @transient + lazy val computedAggregates = aggregateExpressions.flatMap { agg => + agg.collect { + case a: AggregateExpression => + ComputedAggregate( + a, + BindReferences.bindReference(a, childOutput).asInstanceOf[AggregateExpression], + AttributeReference(s"aggResult:$a", a.dataType, nullable = true)()) } + }.toArray + + // The schema of the result of all aggregate evaluations + @transient + lazy val computedSchema = computedAggregates.map(_.resultAttribute) + + // Creates a new aggregate buffer for a group. + def newAggregateBuffer(): Array[AggregateFunction] = { + val buffer = new Array[AggregateFunction](computedAggregates.size) + var i = 0 + while(i < computedAggregates.size) { + buffer(i) = computedAggregates(i).aggregate.newInstance --- End diff -- I do agree in this case actually, but is an allocation really a side effect?
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---