fsk119 commented on a change in pull request #15307: URL: https://github.com/apache/flink/pull/15307#discussion_r606676488
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/plan/rules/logical/PushFilterInCalcIntoTableSourceScanRule.java ########## @@ -0,0 +1,148 @@ +/* + * 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.table.api.TableConfig; +import org.apache.flink.table.api.config.OptimizerConfigOptions; +import org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown; +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.schema.FlinkPreparingTableBase; +import org.apache.flink.table.planner.plan.schema.TableSourceTable; +import org.apache.flink.table.planner.utils.ShortcutUtils; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rel.logical.LogicalTableScan; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexProgramBuilder; +import org.apache.calcite.tools.RelBuilder; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import scala.Tuple2; + +/** + * Pushes a {@link org.apache.calcite.rel.logical.LogicalFilter} from the {@link org.apache.calcite.rel.logical.LogicalCalc} + * and into a {@link LogicalTableScan} + */ +public class PushFilterInCalcIntoTableSourceScanRule extends PushFilterIntoSourceScanRuleBase { + public static final PushFilterInCalcIntoTableSourceScanRule INSTANCE = + new PushFilterInCalcIntoTableSourceScanRule(); + + public PushFilterInCalcIntoTableSourceScanRule() { + super( + operand(FlinkLogicalCalc.class, operand(FlinkLogicalTableSourceScan.class, none())), + "PushFilterInCalcIntoTableSourceScanRule"); + } + + @Override + public boolean matches(RelOptRuleCall call) { + TableConfig config = ShortcutUtils.unwrapContext(call.getPlanner()).getTableConfig(); + if (!config.getConfiguration() + .getBoolean( + OptimizerConfigOptions.TABLE_OPTIMIZER_SOURCE_PREDICATE_PUSHDOWN_ENABLED)) { + return false; + } + + FlinkLogicalCalc calc = call.rel(0); + RexProgram originProgram = calc.getProgram(); + + if (originProgram.getCondition() == null) { + return false; + } + + FlinkLogicalTableSourceScan scan = call.rel(1); + TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class); + // we can not push filter twice + return canPushdownFilter(tableSourceTable); + } + + @Override + public void onMatch(RelOptRuleCall call) { + FlinkLogicalCalc calc = call.rel(0); + FlinkLogicalTableSourceScan scan = call.rel(1); + TableSourceTable table = scan.getTable().unwrap(TableSourceTable.class); + pushFilterIntoScan(call, calc, scan, table); + } + + private void pushFilterIntoScan( + RelOptRuleCall call, + FlinkLogicalCalc calc, + FlinkLogicalTableSourceScan scan, + FlinkPreparingTableBase relOptTable) { + + RexProgram originProgram = calc.getProgram(); + + RelBuilder relBuilder = call.builder(); + Tuple2<RexNode[], RexNode[]> tuple2 = extractPredicates( + originProgram.getInputRowType().getFieldNames().toArray(new String[0]), + originProgram.expandLocalRef(originProgram.getCondition()), + scan, + relBuilder.getRexBuilder()); + + RexNode[] convertiblePredicates = tuple2._1; + if (convertiblePredicates.length == 0) { + // no condition can be translated to expression + return; + } + + Tuple2<SupportsFilterPushDown.Result, FlinkLogicalTableSourceScan> pushdownResultWithScan = createTableScanAfterPushdown( + convertiblePredicates, + relOptTable.unwrap(TableSourceTable.class), + scan, + relBuilder); + + SupportsFilterPushDown.Result result = pushdownResultWithScan._1; + FlinkLogicalTableSourceScan newScan = pushdownResultWithScan._2; + + RexNode[] unconvertedPredicates = tuple2._2; + // check whether framework still need to do a filter + if (result.getRemainingFilters().isEmpty() && unconvertedPredicates.length == 0) { + call.transformTo(newScan); + } else { + List<RexNode> remainingPredicates = + convertExpressionToRexNode(result.getRemainingFilters(), relBuilder); + remainingPredicates.addAll(Arrays.asList(unconvertedPredicates)); + RexNode remainingCondition = relBuilder.and(remainingPredicates); Review comment: Add a method to reuse these codes. -- 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: us...@infra.apache.org