fsk119 commented on a change in pull request #13449: URL: https://github.com/apache/flink/pull/13449#discussion_r518470681
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/plan/rules/logical/PushWatermarkIntoFlinkTableSourceScanAcrossCalcRule.java ########## @@ -0,0 +1,113 @@ +/* + * 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.logical; + +import org.apache.flink.api.common.eventtime.WatermarkStrategy; +import org.apache.flink.table.connector.source.abilities.SupportsWatermarkPushDown; +import org.apache.flink.table.planner.calcite.FlinkContext; +import org.apache.flink.table.planner.calcite.FlinkTypeFactory; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableSourceScan; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWatermarkAssigner; +import org.apache.flink.table.planner.plan.schema.TableSourceTable; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexProgramBuilder; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.util.Pair; + +import java.util.List; + +/** + * Rule to push the {@link FlinkLogicalWatermarkAssigner} across the {@link FlinkLogicalCalc} to the {@link FlinkLogicalTableSourceScan}. + * The rule will first look for the computed column in the {@link FlinkLogicalCalc} and then translate the watermark expression + * and the computed column into a {@link WatermarkStrategy}. With the new scan the rule will build a new {@link FlinkLogicalCalc}. + */ +public class PushWatermarkIntoFlinkTableSourceScanAcrossCalcRule extends PushWatermarkIntoFlinkLogicalTableSourceScanBaseRule { + public static final PushWatermarkIntoFlinkTableSourceScanAcrossCalcRule INSTANCE = new PushWatermarkIntoFlinkTableSourceScanAcrossCalcRule(); + + public PushWatermarkIntoFlinkTableSourceScanAcrossCalcRule() { + super(operand(FlinkLogicalWatermarkAssigner.class, + operand(FlinkLogicalCalc.class, + operand(FlinkLogicalTableSourceScan.class, none()))), + "PushWatermarkIntoFlinkTableSourceScanAcrossProjectRule"); + } + + @Override + public boolean matches(RelOptRuleCall call) { + FlinkLogicalTableSourceScan scan = call.rel(2); + TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class); + return tableSourceTable != null && tableSourceTable.tableSource() instanceof SupportsWatermarkPushDown; + } + + @Override + public void onMatch(RelOptRuleCall call) { + FlinkLogicalWatermarkAssigner watermarkAssigner = call.rel(0); + FlinkLogicalCalc calc = call.rel(1); + + RexProgram originProgram = calc.getProgram(); + List<RexLocalRef> projectList = originProgram.getProjectList(); + + //get watermark expression + RexNode computedColumn = originProgram.expandLocalRef(projectList.get(watermarkAssigner.rowtimeFieldIndex())); + RexNode newWatermarkExpr = watermarkAssigner.watermarkExpr().accept(new RexShuttle() { + @Override + public RexNode visitInputRef(RexInputRef inputRef) { Review comment: The watermark expression only has one input ref. But it's a good idea to make it more robust. ---------------------------------------------------------------- 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]
