dianfu commented on a change in pull request #13504: URL: https://github.com/apache/flink/pull/13504#discussion_r503147661
########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/stream/StreamExecPythonOverAggregate.scala ########## @@ -0,0 +1,312 @@ +/* + * 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.physical.stream + +import org.apache.flink.api.dag.Transformation +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.TableException +import org.apache.flink.table.data.RowData +import org.apache.flink.table.functions.python.PythonFunctionInfo +import org.apache.flink.table.planner.calcite.FlinkTypeFactory +import org.apache.flink.table.planner.delegation.StreamPlanner +import org.apache.flink.table.planner.plan.nodes.common.CommonPythonAggregate +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamExecPythonOverAggregate.{ + ARROW_PYTHON_OVER_WINDOW_RANGE_ROW_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME, + ARROW_PYTHON_OVER_WINDOW_RANGE_PROC_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME, + ARROW_PYTHON_OVER_WINDOW_ROWS_ROW_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME, + ARROW_PYTHON_OVER_WINDOW_ROWS_PROC_TIME_AGGREGATE_FUNCTION_OPERATOR_NAME +} +import org.apache.flink.table.planner.plan.utils.{KeySelectorUtil, OverAggregateUtil} +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo +import org.apache.flink.table.types.logical.RowType + +import org.apache.calcite.plan.{RelOptCluster, RelTraitSet} +import org.apache.calcite.rel.RelFieldCollation.Direction.ASCENDING +import org.apache.calcite.rel.RelNode +import org.apache.calcite.rel.`type`.RelDataType +import org.apache.calcite.rel.core.Window.Group +import org.apache.calcite.rel.core.{AggregateCall, Window} + +import java.util + +import scala.collection.JavaConverters._ + +/** + * Stream physical RelNode for python time-based over [[Window]]. + */ +class StreamExecPythonOverAggregate( + cluster: RelOptCluster, + traitSet: RelTraitSet, + inputRel: RelNode, + outputRowType: RelDataType, + inputRowType: RelDataType, + logicWindow: Window) + extends StreamExecOverAggregateBase( + cluster, + traitSet, + inputRel, + outputRowType, + inputRowType, + logicWindow) + with CommonPythonAggregate { + + override def copy(traitSet: RelTraitSet, inputs: util.List[RelNode]): RelNode = { + new StreamExecPythonOverAggregate( + cluster, + traitSet, + inputs.get(0), + outputRowType, + inputRowType, + logicWindow + ) + } + + override protected def translateToPlanInternal( + planner: StreamPlanner): Transformation[RowData] = { + val tableConfig = planner.getTableConfig + + val overWindow: Group = logicWindow.groups.get(0) + + val orderKeys = overWindow.orderKeys.getFieldCollations + + if (orderKeys.size() != 1) { + throw new TableException( + "The window can only be ordered by a single time column.") + } + val orderKey = orderKeys.get(0) + + if (!orderKey.direction.equals(ASCENDING)) { + throw new TableException( + "The window can only be ordered in ASCENDING mode.") + } + + val inputDS = getInputNodes.get(0).translateToPlan(planner) + .asInstanceOf[Transformation[RowData]] + + if (!logicWindow.groups.get(0).keys.isEmpty && tableConfig.getMinIdleStateRetentionTime < 0) { + LOG.warn( + "No state retention interval configured for a query which accumulates state. " + + "Please provide a query configuration with valid retention interval to prevent " + + "excessive state size. You may specify a retention time of 0 to not clean up the state.") + } + + val timeType = outputRowType.getFieldList.get(orderKey.getFieldIndex).getType + + // check time field + if (!FlinkTypeFactory.isRowtimeIndicatorType(timeType) + && !FlinkTypeFactory.isProctimeIndicatorType(timeType)) { + throw new TableException( + "OVER windows' ordering in stream mode must be defined on a time attribute.") + } + + // identify window rowtime attribute + val rowTimeIdx: Option[Int] = if (FlinkTypeFactory.isRowtimeIndicatorType(timeType)) { + Some(orderKey.getFieldIndex) + } else if (FlinkTypeFactory.isProctimeIndicatorType(timeType)) { + None + } else { + throw new TableException( + "OVER windows can only be applied on time attributes.") + } + + if (overWindow.lowerBound.isPreceding + && overWindow.lowerBound.isUnbounded) { + throw new TableException( + "OVER PRECEDING windows are not supported yet." + ) + } else if (!overWindow.upperBound.isCurrentRow) { + throw new TableException( + "OVER FOLLOWING windows are not supported yet." Review comment: ```suggestion "Python UDAF are not supported to be used in UNBOUNDED FOLLOWING OVER windows." ``` ---------------------------------------------------------------- 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]
