dianfu commented on a change in pull request #14665: URL: https://github.com/apache/flink/pull/14665#discussion_r559287136
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecPythonGroupWindowAggregate.java ########## @@ -0,0 +1,219 @@ +/* + * 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.planner.plan.nodes.exec.batch; + +import org.apache.flink.api.dag.Transformation; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.core.memory.ManagedMemoryUseCase; +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; +import org.apache.flink.streaming.api.transformations.OneInputTransformation; +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.api.config.ExecutionConfigOptions; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.functions.python.PythonFunctionInfo; +import org.apache.flink.table.planner.calcite.FlinkRelBuilder; +import org.apache.flink.table.planner.codegen.agg.batch.WindowCodeGenerator; +import org.apache.flink.table.planner.delegation.PlannerBase; +import org.apache.flink.table.planner.expressions.PlannerProctimeAttribute; +import org.apache.flink.table.planner.expressions.PlannerRowtimeAttribute; +import org.apache.flink.table.planner.expressions.PlannerWindowEnd; +import org.apache.flink.table.planner.expressions.PlannerWindowProperty; +import org.apache.flink.table.planner.expressions.PlannerWindowStart; +import org.apache.flink.table.planner.plan.logical.LogicalWindow; +import org.apache.flink.table.planner.plan.nodes.exec.ExecEdge; +import org.apache.flink.table.planner.plan.nodes.exec.ExecNode; +import org.apache.flink.table.planner.plan.nodes.exec.common.CommonExecPythonAggregate; +import org.apache.flink.table.planner.plan.nodes.exec.utils.CommonPythonUtil; +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; +import org.apache.flink.table.types.logical.RowType; + +import org.apache.calcite.rel.core.AggregateCall; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; + +/** Batch [[ExecNode]] for group widow aggregate (Python user defined aggregate function). */ +public class BatchExecPythonGroupWindowAggregate extends CommonExecPythonAggregate + implements BatchExecNode<RowData> { + + private static final String ARROW_PYTHON_GROUP_WINDOW_AGGREGATE_FUNCTION_OPERATOR_NAME = + "org.apache.flink.table.runtime.operators.python.aggregate.arrow.batch." + + "BatchArrowPythonGroupWindowAggregateFunctionOperator"; + + private final int[] grouping; + private final int[] groupingSet; + private final AggregateCall[] aggCalls; + private final LogicalWindow window; + private final int inputTimeFieldIndex; + private final FlinkRelBuilder.PlannerNamedWindowProperty[] namedWindowProperties; + + public BatchExecPythonGroupWindowAggregate( + int[] grouping, + int[] groupingSet, + AggregateCall[] aggCalls, + LogicalWindow window, + int inputTimeFieldIndex, + FlinkRelBuilder.PlannerNamedWindowProperty[] namedWindowProperties, + ExecEdge inputEdge, + RowType outputType, + String description) { + super(inputEdge, outputType, description); + this.grouping = grouping; + this.groupingSet = groupingSet; + this.aggCalls = aggCalls; + this.window = window; + this.inputTimeFieldIndex = inputTimeFieldIndex; + this.namedWindowProperties = namedWindowProperties; + } + + @Override + protected Transformation<RowData> translateToPlanInternal(PlannerBase planner) { + final ExecNode<RowData> inputNode = (ExecNode<RowData>) getInputNodes().get(0); + final Transformation<RowData> inputTransform = inputNode.translateToPlan(planner); + final RowType inputRowType = (RowType) inputNode.getOutputType(); + final RowType outputRowType = InternalTypeInfo.of(getOutputType()).toRowType(); + + final Tuple2<Long, Long> windowSizeAndSlideSize = WindowCodeGenerator.getWindowDef(window); + final TableConfig tableConfig = planner.getTableConfig(); + final Configuration config = CommonPythonUtil.getConfig(planner.getExecEnv(), tableConfig); + int groupBufferLimitSize = + config.getInteger(ExecutionConfigOptions.TABLE_EXEC_WINDOW_AGG_BUFFER_SIZE_LIMIT); + + OneInputTransformation<RowData, RowData> transform = + createPythonOneInputTransformation( + inputTransform, + inputRowType, + outputRowType, + groupBufferLimitSize, + windowSizeAndSlideSize.f0, + windowSizeAndSlideSize.f1, + config); + if (CommonPythonUtil.isPythonWorkerUsingManagedMemory(config)) { + transform.declareManagedMemoryUseCaseAtSlotScope(ManagedMemoryUseCase.PYTHON); + } + return transform; + } + + private OneInputTransformation<RowData, RowData> createPythonOneInputTransformation( + Transformation<RowData> inputTransform, + RowType inputRowType, + RowType outputRowType, + int maxLimitSize, + long windowSize, + long slideSize, + Configuration config) { + int[] namePropertyTypeArray = + Arrays.stream(namedWindowProperties) + .mapToInt( + p -> { + PlannerWindowProperty property = p.property(); + if (property instanceof PlannerWindowStart) { + return 0; + } + if (property instanceof PlannerWindowEnd) { + return 1; + } + if (property instanceof PlannerRowtimeAttribute) { + return 2; + } + if (property instanceof PlannerProctimeAttribute) { Review comment: There is the corresponding code for this in the original Scala class, could you double check that? ---------------------------------------------------------------- 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: us...@infra.apache.org