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_r272000862
########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/codegen/agg/batch/AggCodeGenHelper.scala ########## @@ -0,0 +1,732 @@ +/* + * 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.common.ExecutionConfig +import org.apache.flink.runtime.util.SingleElementIterator +import org.apache.flink.streaming.api.operators.OneInputStreamOperator +import org.apache.flink.table.`type`.TypeConverters.{createInternalTypeFromTypeInfo, createInternalTypeInfoFromInternalType} +import org.apache.flink.table.`type`.{ArrayType, InternalType, MapType, RowType, StringType} +import org.apache.flink.table.api.TableConfig +import org.apache.flink.table.codegen.CodeGenUtils.{boxedTypeTermForExternalType, genToExternal, genToInternal, newName, primitiveTypeTermForType} +import org.apache.flink.table.codegen.OperatorCodeGenerator.STREAM_RECORD +import org.apache.flink.table.codegen.{CodeGenUtils, CodeGeneratorContext, ExprCodeGenerator, GenerateUtils, GeneratedExpression, OperatorCodeGenerator} +import org.apache.flink.table.dataformat.{BaseRow, GenericRow} +import org.apache.flink.table.expressions.{CallExpression, Expression, ExpressionVisitor, FieldReferenceExpression, ResolvedAggInputReference, ResolvedAggLocalReference, RexNodeConverter, SymbolExpression, TypeLiteralExpression, UnresolvedFieldReferenceExpression, ValueLiteralExpression} +import org.apache.flink.table.functions.utils.UserDefinedFunctionUtils.{getAccumulatorTypeOfAggregateFunction, getAggUserDefinedInputTypes, getResultTypeOfAggregateFunction} +import org.apache.flink.table.functions.{AggregateFunction, DeclarativeAggregateFunction, UserDefinedFunction} +import org.apache.flink.table.generated.{GeneratedAggsHandleFunction, GeneratedOperator} +import org.apache.flink.table.runtime.context.ExecutionContextImpl + +import org.apache.calcite.rel.core.AggregateCall +import org.apache.calcite.rex.RexNode +import org.apache.calcite.tools.RelBuilder + +import scala.collection.JavaConverters._ + +/** + * Batch aggregate code generate helper. + */ +object AggCodeGenHelper { + + def getAggBufferNames( + auxGrouping: Array[Int], aggregates: Seq[UserDefinedFunction]): Array[Array[String]] = { + auxGrouping.zipWithIndex.map { + case (_, index) => Array(s"aux_group$index") + } ++ aggregates.zipWithIndex.toArray.map { + case (a: DeclarativeAggregateFunction, index) => + val idx = auxGrouping.length + index + a.aggBufferAttributes.map(attr => s"agg${idx}_${attr.getName}") + case (_: AggregateFunction[_, _], index) => + val idx = auxGrouping.length + index + Array(s"agg$idx") + } + } + + def getAggBufferTypes( + inputType: RowType, auxGrouping: Array[Int], aggregates: Seq[UserDefinedFunction]) + : Array[Array[InternalType]] = { + auxGrouping.map { index => + Array(inputType.getFieldTypes()(index)) + } ++ aggregates.map { + case a: DeclarativeAggregateFunction => a.getAggBufferTypes + case a: AggregateFunction[_, _] => + Array(createInternalTypeFromTypeInfo(getAccumulatorTypeOfAggregateFunction(a))) + }.toArray[Array[InternalType]] + } + + def getUdaggs( + aggregates: Seq[UserDefinedFunction]): Map[AggregateFunction[_, _], String] = { + aggregates + .filter(a => a.isInstanceOf[AggregateFunction[_, _]]) + .map(a => a -> CodeGenUtils.udfFieldName(a)).toMap + .asInstanceOf[Map[AggregateFunction[_, _], String]] + } + + def projectRowType( + rowType: RowType, mapping: Array[Int]): RowType = { + new RowType(mapping.map(rowType.getTypeAt), mapping.map(rowType.getFieldNames()(_))) + } + + /** + * Add agg handler to class member and open it. + */ + private[flink] def addAggsHandler( + aggsHandler: GeneratedAggsHandleFunction, + ctx: CodeGeneratorContext, + aggsHandlerCtx: CodeGeneratorContext): String = { + ctx.addReusableInnerClass(aggsHandler.getClassName, aggsHandler.getCode) + val handler = CodeGenUtils.newName("handler") + ctx.addReusableMember(s"${aggsHandler.getClassName} $handler = null;") + val aggRefers = ctx.addReusableObject(aggsHandlerCtx.references.toArray, "Object[]") + ctx.addReusableOpenStatement( + s""" + |$handler = new ${aggsHandler.getClassName}($aggRefers); + |$handler.open(new ${classOf[ExecutionContextImpl].getCanonicalName}( + | this, getRuntimeContext())); + """.stripMargin) + ctx.addReusableCloseStatement(s"$handler.close();") + handler + } + + private[flink] def projectRowType( + mapping: Array[Int], + inputT: RowType): RowType = Review comment: brace needed ---------------------------------------------------------------- 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
