beyond1920 commented on a change in pull request #16025: URL: https://github.com/apache/flink/pull/16025#discussion_r659691154
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/SimplifyWindowTableFunctionRule.java ########## @@ -0,0 +1,355 @@ +/* + * 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.rules.physical.stream; + +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalExchange; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowJoin; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowRank; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowTableFunction; + +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelNode; + +import java.util.Collections; +import java.util.List; + +/** + * Planner rule that tries to simplify emit behavior of {@link StreamPhysicalWindowTableFunction} to + * emit per record instead of emit after watermark passed window end if the successor node is {@link + * StreamPhysicalWindowRank} or {@link StreamPhysicalWindowJoin} which with + * rowtime-WindowingStrategy. + */ +public class SimplifyWindowTableFunctionRule + extends RelRule<SimplifyWindowTableFunctionRule.Config> { + + public static final RelOptRule WITH_WINDOW_RANK = + Config.EMPTY + .withDescription("SimplifyWindowTableFunctionRuleWithWindowRank") + .as(Config.class) + .withWindowRank() + .toRule(); + + public static final RelOptRule WITH_CALC_WINDOW_RANK = + Config.EMPTY + .withDescription("SimplifyWindowTableFunctionRuleWithCalcWindowRank") + .as(Config.class) + .withCalcWindowRank() + .toRule(); + + public static final RelOptRule WITH_WINDOW_JOIN = + Config.EMPTY + .withDescription("SimplifyWindowTableFunctionRuleWithWindowJoin") + .as(Config.class) + .withWindowJoin() + .toRule(); + + public static final RelOptRule WITH_LEFT_CALC_WINDOW_JOIN = + Config.EMPTY + .withDescription("SimplifyWindowTableFunctionRuleWithLeftCalcWindowJoin") + .as(Config.class) + .withLeftCalcWindowJoin() + .toRule(); + + public static final RelOptRule WITH_RIGHT_CALC_WINDOW_JOIN = + Config.EMPTY + .withDescription("SimplifyWindowTableFunctionRuleWithRightCalcWindowJoin") + .as(Config.class) + .withRightCalcWindowJoin() + .toRule(); + + public static final RelOptRule WITH_LEFT_RIGHT_CALC_WINDOW_JOIN = + Config.EMPTY + .withDescription("SimplifyWindowTableFunctionRuleWithLeftRightCalcWindowJoin") + .as(Config.class) + .withLeftRightCalcWindowJoin() + .toRule(); + + public SimplifyWindowTableFunctionRule(Config config) { + super(config); + } + + @Override + public boolean matches(RelOptRuleCall call) { + List<RelNode> rels = call.getRelList(); + final RelNode node = rels.get(0); + if (node instanceof StreamPhysicalWindowRank) { + final int windowTVFIdx = rels.size() - 1; + final StreamPhysicalWindowTableFunction windowTVF = + (StreamPhysicalWindowTableFunction) rels.get(windowTVFIdx); + return needSimplify(windowTVF); + } else if (node instanceof StreamPhysicalWindowJoin) { + RelNode leftWindowTVFRel; + if (call.rule == WITH_WINDOW_JOIN) { + leftWindowTVFRel = rels.get(2); + } else if (call.rule == WITH_RIGHT_CALC_WINDOW_JOIN) { + leftWindowTVFRel = rels.get(2); + } else if (call.rule == WITH_LEFT_CALC_WINDOW_JOIN) { + leftWindowTVFRel = rels.get(3); + } else if (call.rule == WITH_LEFT_RIGHT_CALC_WINDOW_JOIN) { + leftWindowTVFRel = rels.get(3); + } else { + throw new TableException("This should never happen. Please file an issue."); + } + final StreamPhysicalWindowTableFunction leftWindowTVF = + (StreamPhysicalWindowTableFunction) leftWindowTVFRel; + final StreamPhysicalWindowTableFunction rightWindowTVF = + (StreamPhysicalWindowTableFunction) rels.get(rels.size() - 1); + return needSimplify(leftWindowTVF) || needSimplify(rightWindowTVF); + } else { + throw new IllegalStateException( + this.getClass().getName() + + " matches a wrong relation tree: " + + RelOptUtil.toString(node)); + } + } + + @Override + public void onMatch(RelOptRuleCall call) { + List<RelNode> rels = call.getRelList(); + final RelNode node = rels.get(0); + if (node instanceof StreamPhysicalWindowRank) { + RelNode newTree = rebuild(rels); + call.transformTo(newTree); + } else if (node instanceof StreamPhysicalWindowJoin) { + RelNode newLeft; + RelNode newRight; + if (call.rule == WITH_WINDOW_JOIN) { + newLeft = rebuild(rels.subList(1, 3)); + newRight = rebuild(rels.subList(3, 5)); + } else if (call.rule == WITH_RIGHT_CALC_WINDOW_JOIN) { + newLeft = rebuild(rels.subList(1, 3)); + newRight = rebuild(rels.subList(3, 6)); + } else if (call.rule == WITH_LEFT_CALC_WINDOW_JOIN) { + newLeft = rebuild(rels.subList(1, 4)); + newRight = rebuild(rels.subList(4, 6)); + } else if (call.rule == WITH_LEFT_RIGHT_CALC_WINDOW_JOIN) { + newLeft = rebuild(rels.subList(1, 4)); + newRight = rebuild(rels.subList(4, 7)); + } else { + throw new TableException("This should never happen. Please file an issue."); + } + final StreamPhysicalWindowJoin join = call.rel(0); + final RelNode newJoin = + join.copy( + join.getTraitSet(), + join.getCondition(), + newLeft, + newRight, + join.getJoinType(), + join.isSemiJoinDone()); + call.transformTo(newJoin); + } else { + throw new IllegalStateException( + this.getClass().getName() + + " matches a wrong relation tree: " + + RelOptUtil.toString(node)); + } + } + + private boolean needSimplify(StreamPhysicalWindowTableFunction windowTVF) { + return windowTVF.windowing().isRowtime() && !windowTVF.emitPerRecord(); Review comment: using !windowTVF.emitPerRecord() to exclude those window table function nodes which are already emit per record. Because there is no need to create new nodes for those window table function nodes. I could add some comments here to explain logic. -- 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. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org