You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/01/13 09:58:29 UTC

[GitHub] [flink] godfreyhe commented on a change in pull request #14605: [FLINK-20883][table-planner-blink] Separate the implementation of BatchExecOverAggregate and StreamExecOverAggregate

godfreyhe commented on a change in pull request #14605:
URL: https://github.com/apache/flink/pull/14605#discussion_r556397774



##########
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:
       I think it's easy for debug




----------------------------------------------------------------
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:
users@infra.apache.org