KurtYoung commented on a change in pull request #8099: [FLINK-12081][table-planner-blink] Introduce aggregation operator code generator to blink batch URL: https://github.com/apache/flink/pull/8099#discussion_r272001068
########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/codegen/agg/batch/HashAggCodeGenHelper.scala ########## @@ -0,0 +1,864 @@ +/* + * 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.flink.table.codegen.agg.batch + +import org.apache.flink.api.java.tuple.{Tuple2 => JTuple2} +import org.apache.flink.metrics.Gauge +import org.apache.flink.table.`type`.{InternalType, RowType} +import org.apache.flink.table.api.TableConfig +import org.apache.flink.table.calcite.FlinkPlannerImpl +import org.apache.flink.table.codegen.CodeGenUtils.{binaryRowFieldSetAccess, binaryRowSetNull} +import org.apache.flink.table.codegen.agg.batch.AggCodeGenHelper.buildAggregateArgsMapping +import org.apache.flink.table.codegen.{CodeGenUtils, CodeGeneratorContext, ExprCodeGenerator, GenerateUtils, GeneratedExpression, OperatorCodeGenerator, SortCodeGenerator} +import org.apache.flink.table.dataformat.{BaseRow, BinaryRow, GenericRow, JoinedRow} +import org.apache.flink.table.expressions.{CallExpression, Expression, ExpressionVisitor, FieldReferenceExpression, ResolvedAggInputReference, RexNodeConverter, SymbolExpression, TypeLiteralExpression, UnresolvedFieldReferenceExpression, ValueLiteralExpression} +import org.apache.flink.table.functions.{AggregateFunction, DeclarativeAggregateFunction, UserDefinedFunction} +import org.apache.flink.table.generated.{NormalizedKeyComputer, RecordComparator} +import org.apache.flink.table.runtime.aggregate.{BytesHashMap, BytesHashMapSpillMemorySegmentPool} +import org.apache.flink.table.runtime.sort.BufferedKVExternalSorter +import org.apache.flink.table.typeutils.BinaryRowSerializer + +import org.apache.calcite.rel.core.AggregateCall +import org.apache.calcite.tools.RelBuilder + +import scala.collection.JavaConversions._ + +object HashAggCodeGenHelper { + + def prepareHashAggKVTypes( + ctx: CodeGeneratorContext, + aggMapKeyTypesTerm: String, + aggBufferTypesTerm: String, + aggMapKeyType: RowType, + aggBufferType: RowType): Unit = { + ctx.addReusableObjectWithName(aggMapKeyType.getFieldTypes, aggMapKeyTypesTerm) + ctx.addReusableObjectWithName(aggBufferType.getFieldTypes, aggBufferTypesTerm) + } + + private[flink] def prepareHashAggMap( + ctx: CodeGeneratorContext, + config: TableConfig, + reservedManagedMemory: Long, + maxManagedMemory: Long, + groupKeyTypesTerm: String, + aggBufferTypesTerm: String, + aggregateMapTerm: String): Unit = { + // allocate memory segments for aggregate map + + // create aggregate map + val mapTypeTerm = classOf[BytesHashMap].getName + ctx.addReusableMember(s"private transient $mapTypeTerm $aggregateMapTerm;") + ctx.addReusableOpenStatement(s"$aggregateMapTerm " + + s"= new $mapTypeTerm(" + + s"this.getContainingTask()," + + s"this.getContainingTask().getEnvironment().getMemoryManager()," + + s"${reservedManagedMemory}L," + + s" $groupKeyTypesTerm," + + s" $aggBufferTypesTerm);") + // close aggregate map and release memory segments + ctx.addReusableCloseStatement(s"$aggregateMapTerm.free();") + ctx.addReusableCloseStatement(s"") + } + + private[flink] def prepareTermForAggMapIteration( + ctx: CodeGeneratorContext, + outputTerm: String, + outputType: RowType, + aggMapKeyType: RowType, + aggBufferType: RowType, + outputClass: Class[_ <: BaseRow]): (String, String, String) = { + // prepare iteration var terms + val reuseAggMapKeyTerm = CodeGenUtils.newName("reuseAggMapKey") + val reuseAggBufferTerm = CodeGenUtils.newName("reuseAggBuffer") + val reuseAggMapEntryTerm = CodeGenUtils.newName("reuseAggMapEntry") + // gen code to prepare agg output using agg buffer and key from the aggregate map + val binaryRow = classOf[BinaryRow].getName + val mapEntryTypeTerm = classOf[BytesHashMap.Entry].getCanonicalName + + ctx.addReusableOutputRecord(outputType, outputClass, outputTerm) + ctx.addReusableMember( + s"private transient $binaryRow $reuseAggMapKeyTerm = " + + s"new $binaryRow(${aggMapKeyType.getArity});") + ctx.addReusableMember( + s"private transient $binaryRow $reuseAggBufferTerm = " + + s"new $binaryRow(${aggBufferType.getArity});") + ctx.addReusableMember( + s"private transient $mapEntryTypeTerm $reuseAggMapEntryTerm = " + + s"new $mapEntryTypeTerm($reuseAggMapKeyTerm, $reuseAggBufferTerm);" + ) + (reuseAggMapEntryTerm, reuseAggMapKeyTerm, reuseAggBufferTerm) + } + + private[flink] def genHashAggCodes( + isMerge: Boolean, + isFinal: Boolean, + ctx: CodeGeneratorContext, + config: TableConfig, + builder: RelBuilder, + groupingAndAuxGrouping: (Array[Int], Array[Int]), + inputTerm: String, + inputType: RowType, + aggregateCalls: Seq[AggregateCall], + aggCallToAggFunction: Seq[(AggregateCall, UserDefinedFunction)], + aggArgs: Array[Array[Int]], + aggregates: Seq[UserDefinedFunction], + currentAggBufferTerm: String, + aggBufferRowType: RowType, + aggBufferNames: Array[Array[String]], + aggBufferTypes: Array[Array[InternalType]], + outputTerm: String, + outputType: RowType, + groupKeyTerm: String, + aggBufferTerm: String): (GeneratedExpression, GeneratedExpression, GeneratedExpression) = { + Review comment: delete blank line ---------------------------------------------------------------- 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
