wenlong88 commented on a change in pull request #14605:
URL: https://github.com/apache/flink/pull/14605#discussion_r556283456
##########
File path:
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/over/MultiFieldRangeBoundComparatorCodeGenerator.scala
##########
@@ -48,9 +46,14 @@ class MultiFieldRangeBoundComparatorCodeGenerator(
}
val ctx = CodeGeneratorContext(conf)
-
val compareCode = GenerateUtils.generateRowCompare(
Review comment:
use the new method accepts sortSpec
##########
File path:
flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecOverAggregate.java
##########
@@ -0,0 +1,380 @@
+/*
+ * 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.stream;
+
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
+import org.apache.flink.streaming.api.operators.KeyedProcessOperator;
+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.data.RowData;
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory;
+import org.apache.flink.table.planner.codegen.CodeGeneratorContext;
+import org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator;
+import org.apache.flink.table.planner.delegation.PlannerBase;
+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.ExecNodeBase;
+import org.apache.flink.table.planner.plan.nodes.exec.utils.OverSpec;
+import org.apache.flink.table.planner.plan.utils.AggregateInfoList;
+import org.apache.flink.table.planner.plan.utils.AggregateUtil;
+import org.apache.flink.table.planner.plan.utils.KeySelectorUtil;
+import org.apache.flink.table.planner.plan.utils.OverAggregateUtil;
+import org.apache.flink.table.planner.utils.JavaScalaConversionUtil;
+import org.apache.flink.table.runtime.generated.GeneratedAggsHandleFunction;
+import org.apache.flink.table.runtime.keyselector.RowDataKeySelector;
+import
org.apache.flink.table.runtime.operators.over.ProcTimeRangeBoundedPrecedingFunction;
+import
org.apache.flink.table.runtime.operators.over.ProcTimeRowsBoundedPrecedingFunction;
+import
org.apache.flink.table.runtime.operators.over.ProcTimeUnboundedPrecedingFunction;
+import
org.apache.flink.table.runtime.operators.over.RowTimeRangeBoundedPrecedingFunction;
+import
org.apache.flink.table.runtime.operators.over.RowTimeRangeUnboundedPrecedingFunction;
+import
org.apache.flink.table.runtime.operators.over.RowTimeRowsBoundedPrecedingFunction;
+import
org.apache.flink.table.runtime.operators.over.RowTimeRowsUnboundedPrecedingFunction;
+import org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.tools.RelBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.IntStream;
+
+/** Stream {@link ExecNode} for time-based over operator. */
+public class StreamExecOverAggregate extends ExecNodeBase<RowData>
+ implements StreamExecNode<RowData> {
+ private static final Logger LOG =
LoggerFactory.getLogger(StreamExecOverAggregate.class);
+
+ private final OverSpec overSpec;
+
+ public StreamExecOverAggregate(
+ OverSpec overSpec, ExecEdge inputEdge, RowType outputType, String
description) {
+ super(Collections.singletonList(inputEdge), outputType, description);
+ this.overSpec = overSpec;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected Transformation<RowData> translateToPlanInternal(PlannerBase
planner) {
+ if (overSpec.getGroups().size() > 1) {
+ throw new TableException("All aggregates must be computed on the
same window.");
+ }
+
+ final OverSpec.GroupSpec group = overSpec.getGroups().get(0);
+ final int[] orderKeys = group.getSort().getFieldIndices();
+ final boolean[] isAscendingOrders =
group.getSort().getAscendingOrders();
+ if (orderKeys.length != 1 || isAscendingOrders.length != 1) {
+ throw new TableException("The window can only be ordered by a
single time column.");
+ }
+
+ if (!isAscendingOrders[0]) {
+ throw new TableException("The window can only be ordered in
ASCENDING mode.");
+ }
+
+ final int[] partitionKeys = overSpec.getPartition().getFieldIndices();
+ final TableConfig tableConfig = planner.getTableConfig();
+ if (partitionKeys.length > 0 &&
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.");
+ }
+
+ final ExecNode<RowData> inputNode = (ExecNode<RowData>)
getInputNodes().get(0);
+ final Transformation<RowData> inputTransform =
inputNode.translateToPlan(planner);
+ final RowType inputRowType = (RowType) inputNode.getOutputType();
+
+ final int orderKey = orderKeys[0];
+ final LogicalType orderKeyType =
inputRowType.getFields().get(orderKey).getType();
+ // check time field && identify window rowtime attribute
+ final int rowTimeIdx;
+ if (orderKeyType instanceof TimestampType
+ && ((TimestampType) orderKeyType).getKind() ==
TimestampKind.ROWTIME) {
+ rowTimeIdx = orderKey;
+ } else if (orderKeyType instanceof TimestampType
+ && ((TimestampType) orderKeyType).getKind() ==
TimestampKind.PROCTIME) {
+ rowTimeIdx = -1;
+ } else {
+ throw new TableException(
+ "OVER windows' ordering in stream mode must be defined on
a time attribute.");
+ }
+
+ final List<RexLiteral> constants = overSpec.getConstants();
+ final List<String> fieldNames = new
ArrayList<>(inputRowType.getFieldNames());
+ final List<LogicalType> fieldTypes = new
ArrayList<>(inputRowType.getChildren());
+ IntStream.range(0, constants.size()).forEach(i -> fieldNames.add("TMP"
+ i));
+ for (int i = 0; i < constants.size(); ++i) {
+ fieldNames.add("TMP" + i);
+
fieldTypes.add(FlinkTypeFactory.toLogicalType(constants.get(i).getType()));
+ }
+
+ final RowType aggInputRowType =
+ RowType.of(
+ fieldTypes.toArray(new LogicalType[0]),
fieldNames.toArray(new String[0]));
Review comment:
not need to create the fieldName?
##########
File path:
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalOverAggregateBase.scala
##########
@@ -18,88 +18,50 @@
package org.apache.flink.table.planner.plan.nodes.physical.batch
-import org.apache.flink.table.data.RowData
-import org.apache.flink.table.functions.UserDefinedFunction
+import org.apache.flink.table.api.TableException
import org.apache.flink.table.planner.CalcitePair
-import org.apache.flink.table.planner.calcite.FlinkTypeFactory
import org.apache.flink.table.planner.plan.`trait`.{FlinkRelDistribution,
FlinkRelDistributionTraitDef}
import org.apache.flink.table.planner.plan.cost.{FlinkCost, FlinkCostFactory}
-import org.apache.flink.table.planner.plan.nodes.exec.{LegacyBatchExecNode,
ExecEdge}
-import
org.apache.flink.table.planner.plan.nodes.physical.batch.OverWindowMode.OverWindowMode
import
org.apache.flink.table.planner.plan.rules.physical.batch.BatchExecJoinRuleBase
+import
org.apache.flink.table.planner.plan.utils.OverAggregateUtil.splitOutOffsetOrInsensitiveGroup
import org.apache.flink.table.planner.plan.utils.{FlinkRelOptUtil,
OverAggregateUtil, RelExplainUtil}
-import com.google.common.collect.ImmutableList
import org.apache.calcite.plan._
import org.apache.calcite.rel.RelDistribution.Type._
import org.apache.calcite.rel._
import org.apache.calcite.rel.`type`.RelDataType
import org.apache.calcite.rel.core.Window.Group
import org.apache.calcite.rel.core.{AggregateCall, Window}
import org.apache.calcite.rel.metadata.RelMetadataQuery
-import org.apache.calcite.rex.RexLiteral
-import org.apache.calcite.sql.fun.SqlLeadLagAggFunction
-import org.apache.calcite.tools.RelBuilder
import org.apache.calcite.util.ImmutableIntList
import java.util
import scala.collection.JavaConversions._
-import scala.collection.mutable.ArrayBuffer
/**
- * Batch physical RelNode for sort-based over [[Window]] aggregate.
- */
-abstract class BatchExecOverAggregateBase(
+ * Base batch physical RelNode for sort-based over [[Window]] aggregate.
+ */
+abstract class BatchPhysicalOverAggregateBase(
cluster: RelOptCluster,
- relBuilder: RelBuilder,
traitSet: RelTraitSet,
inputRel: RelNode,
outputRowType: RelDataType,
inputRowType: RelDataType,
- grouping: Array[Int],
- orderKeyIndices: Array[Int],
- orders: Array[Boolean],
- nullIsLasts: Array[Boolean],
- windowGroupToAggCallToAggFunction: Seq[
- (Window.Group, Seq[(AggregateCall, UserDefinedFunction)])],
+ windowGroups: Seq[Window.Group],
logicWindow: Window)
extends SingleRel(cluster, traitSet, inputRel)
- with BatchPhysicalRel
- with LegacyBatchExecNode[RowData] {
-
- protected lazy val modeToGroupToAggCallToAggFunction:
- Seq[(OverWindowMode, Window.Group, Seq[(AggregateCall,
UserDefinedFunction)])] =
- splitOutOffsetOrInsensitiveGroup()
+ with BatchPhysicalRel {
- protected val constants: ImmutableList[RexLiteral] = logicWindow.constants
- protected val inputTypeWithConstants: RelDataType = {
- val constantTypes = constants.map(c =>
FlinkTypeFactory.toLogicalType(c.getType))
- val inputTypeNamesWithConstants =
- inputType.getFieldNames ++ constants.indices.map(i => "TMP" + i)
- val inputTypesWithConstants = inputType.getChildren ++ constantTypes
- cluster.getTypeFactory.asInstanceOf[FlinkTypeFactory]
- .buildRelNodeRowType(inputTypeNamesWithConstants,
inputTypesWithConstants)
+ val partitionKeyIndices: Array[Int] = windowGroups.head.keys.toArray
+ windowGroups.tail.foreach { g =>
+ if (!util.Arrays.equals(partitionKeyIndices, g.keys.toArray)) {
+ throw new TableException("" +
+ "BatchPhysicalOverAggregateBase requires all groups should have same
partition key.")
+ }
}
- lazy val aggregateCalls: Seq[AggregateCall] =
- windowGroupToAggCallToAggFunction.flatMap(_._2).map(_._1)
-
- protected lazy val inputType =
FlinkTypeFactory.toLogicalRowType(inputRowType)
-
- protected def isUnboundedWindow(group: Window.Group) =
- group.lowerBound.isUnbounded && group.upperBound.isUnbounded
-
- protected def isUnboundedPrecedingWindow(group: Window.Group) =
- group.lowerBound.isUnbounded && !group.upperBound.isUnbounded
-
- protected def isUnboundedFollowingWindow(group: Window.Group) =
- !group.lowerBound.isUnbounded && group.upperBound.isUnbounded
-
- protected def isSlidingWindow(group: Window.Group) =
- !group.lowerBound.isUnbounded && !group.upperBound.isUnbounded
-
- def getGrouping: Array[Int] = grouping
+ protected lazy val newWindowGroups: Seq[Group] =
splitOutOffsetOrInsensitiveGroup(windowGroups)
Review comment:
how about a more specified name?maybe reOrganizedGroups?
----------------------------------------------------------------
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]