hvanhovell commented on a change in pull request #26012: [SPARK-29346] Add Aggregating Accumulator URL: https://github.com/apache/spark/pull/26012#discussion_r331035279
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/AggregatingAccumulator.scala ########## @@ -0,0 +1,256 @@ +/* + * 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 + +import scala.collection.mutable + +import org.apache.spark.TaskContext +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference, AttributeSeq, BindReferences, Expression, InterpretedMutableProjection, InterpretedUnsafeProjection, JoinedRow, MutableProjection, NamedExpression, Projection, SpecificInternalRow, UnsafeProjection} +import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, DeclarativeAggregate, ImperativeAggregate, NoOp, TypedImperativeAggregate} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.{DataType, StructField, StructType} +import org.apache.spark.util.AccumulatorV2 + +/** + * Accumulator that computes a global aggregate. + */ +class AggregatingAccumulator private( + bufferSchema: Seq[DataType], + initialValues: Seq[Expression], + updateExpressions: Seq[Expression], + @transient private var mergeExpressions: Seq[Expression], + @transient private var resultExpressions: Seq[Expression], + imperatives: Array[ImperativeAggregate], + typedImperatives: Array[TypedImperativeAggregate[_]], + @transient private var conf: SQLConf) + extends AccumulatorV2[InternalRow, InternalRow] { + assert(bufferSchema.size == initialValues.size) + assert(bufferSchema.size == updateExpressions.size) + assert(bufferSchema.size == mergeExpressions.size) + + private[this] var joinedRow: JoinedRow = _ + + private var buffer: SpecificInternalRow = _ + + private def createBuffer(): SpecificInternalRow = { + val buffer = new SpecificInternalRow(bufferSchema) + + // Initialize the buffer. Note that we do not use a code generated projection here because + // generating and compiling a projection is probably more expensive than using an interpreted + // projection. + InterpretedMutableProjection.createProjection(initialValues) + .target(buffer) + .apply(InternalRow.empty) + imperatives.foreach(_.initialize(buffer)) + typedImperatives.foreach(_.initialize(buffer)) + buffer + } + + private def getOrCreateBuffer(): SpecificInternalRow = { + if (buffer == null) { + buffer = createBuffer() + + // Create the joined row and set the buffer as its 'left' row. + joinedRow = new JoinedRow() + joinedRow.withLeft(buffer) + } + buffer + } + + private def initializeProjection[T <: Projection](projection: T): T = { + projection.initialize(TaskContext.getPartitionId()) + projection + } + + @transient + private[this] lazy val updateProjection = initializeProjection { + MutableProjection.create(updateExpressions) + } + + @transient + private[this] lazy val mergeProjection = SQLConf.withExistingConf(conf) { + initializeProjection { + InterpretedMutableProjection.createProjection(mergeExpressions) + } + } + + @transient + private[this] lazy val resultProjection = SQLConf.withExistingConf(conf) { + initializeProjection { + InterpretedUnsafeProjection.createProjection(resultExpressions) + } + } + + override def reset(): Unit = { + buffer = null + joinedRow = null + } + + override def isZero: Boolean = buffer == null + + override def copyAndReset(): AggregatingAccumulator = { + new AggregatingAccumulator( + bufferSchema, + initialValues, + updateExpressions, + mergeExpressions, + resultExpressions, + imperatives, + typedImperatives, + conf) + } + + override def copy(): AggregatingAccumulator = { + val copy = copyAndReset() + copy.merge(this) + copy + } + + override def add(v: InternalRow): Unit = { + val buffer = getOrCreateBuffer() + updateProjection.target(buffer)(joinedRow.withRight(v)) + var i = 0 + while (i < imperatives.length) { + imperatives(i).update(buffer, v) + i += 1 + } + i = 0 + while (i < typedImperatives.length) { + typedImperatives(i).update(buffer, v) + i += 1 + } + } + + override def merge(other: AccumulatorV2[InternalRow, InternalRow]): Unit = { + SQLConf.withExistingConf(conf) { Review comment: We need to set the sql conf because this is executed in the DAGScheduler thread. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
