c21 commented on a change in pull request #34826: URL: https://github.com/apache/spark/pull/34826#discussion_r765398742
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/AggregateCodegenSupport.scala ########## @@ -0,0 +1,340 @@ +/* + * 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.rdd.RDD +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, Expression, ExpressionEquals, UnsafeRow} +import org.apache.spark.sql.catalyst.expressions.BindReferences.bindReferences +import org.apache.spark.sql.catalyst.expressions.aggregate._ +import org.apache.spark.sql.catalyst.expressions.codegen._ +import org.apache.spark.sql.catalyst.expressions.codegen.Block._ +import org.apache.spark.sql.catalyst.util.DateTimeConstants.NANOS_PER_MILLIS +import org.apache.spark.sql.execution.{BlockingOperatorWithCodegen, CodegenSupport, GeneratePredicateHelper} +import org.apache.spark.util.Utils + +/** + * An interface for those aggregate physical operators that support codegen. + */ +trait AggregateCodegenSupport + extends BaseAggregateExec + with BlockingOperatorWithCodegen + with GeneratePredicateHelper { + + /** + * All the modes of aggregate expressions. + */ + protected val modes: Seq[AggregateMode] = aggregateExpressions.map(_.mode).distinct + + /** + * The variables are used as aggregation buffers and each aggregate function has one or more + * ExprCode to initialize its buffer slots. Only used for aggregation without keys. + */ + private var bufVars: Seq[Seq[ExprCode]] = _ + + /** + * The generated code for `doProduce` call when aggregate has grouping keys. + */ + protected def doProduceWithKeys(ctx: CodegenContext): String + + /** + * The generated code for `doConsume` call when aggregate has grouping keys. + */ + protected def doConsumeWithKeys(ctx: CodegenContext, input: Seq[ExprCode]): String + + protected override def doProduce(ctx: CodegenContext): String = { + if (groupingExpressions.isEmpty) { + doProduceWithoutKeys(ctx) + } else { + doProduceWithKeys(ctx) + } + } + + override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row: ExprCode): String = { + if (groupingExpressions.isEmpty) { + doConsumeWithoutKeys(ctx, input) + } else { + doConsumeWithKeys(ctx, input) + } + } + + override def supportCodegen: Boolean = { + val isMutableAggBuffer = aggregateBufferAttributes.forall(a => UnsafeRow.isMutable(a.dataType)) Review comment: Add check for aggregate buffer mutable or not. This is already implicitly checked for hash aggregate during planning: `HashAggregateExec.supportsAggregate()`. Make it explicit as requirement for code-gen as well here. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
