Search in sources :

Example 11 with TableSourceTable

use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.

the class PushFilterInCalcIntoTableSourceScanRule method pushFilterIntoScan.

private void pushFilterIntoScan(RelOptRuleCall call, Calc calc, FlinkLogicalTableSourceScan scan, FlinkPreparingTableBase relOptTable) {
    RexProgram originProgram = calc.getProgram();
    RelBuilder relBuilder = call.builder();
    Tuple2<RexNode[], RexNode[]> extractedPredicates = extractPredicates(originProgram.getInputRowType().getFieldNames().toArray(new String[0]), originProgram.expandLocalRef(originProgram.getCondition()), scan, relBuilder.getRexBuilder());
    RexNode[] convertiblePredicates = extractedPredicates._1;
    RexNode[] unconvertedPredicates = extractedPredicates._2;
    if (convertiblePredicates.length == 0) {
        // no condition can be translated to expression
        return;
    }
    Tuple2<SupportsFilterPushDown.Result, TableSourceTable> pushdownResultWithScan = resolveFiltersAndCreateTableSourceTable(convertiblePredicates, relOptTable.unwrap(TableSourceTable.class), scan, relBuilder);
    SupportsFilterPushDown.Result result = pushdownResultWithScan._1;
    TableSourceTable tableSourceTable = pushdownResultWithScan._2;
    FlinkLogicalTableSourceScan newScan = FlinkLogicalTableSourceScan.create(scan.getCluster(), scan.getHints(), tableSourceTable);
    // build new calc program
    RexProgramBuilder programBuilder = RexProgramBuilder.forProgram(originProgram, call.builder().getRexBuilder(), true);
    programBuilder.clearCondition();
    if (!result.getRemainingFilters().isEmpty() || unconvertedPredicates.length != 0) {
        RexNode remainingCondition = createRemainingCondition(relBuilder, result.getRemainingFilters(), unconvertedPredicates);
        RexNode simplifiedRemainingCondition = FlinkRexUtil.simplify(relBuilder.getRexBuilder(), remainingCondition, calc.getCluster().getPlanner().getExecutor());
        programBuilder.addCondition(simplifiedRemainingCondition);
    }
    RexProgram program = programBuilder.getProgram();
    if (program.isTrivial()) {
        call.transformTo(newScan);
    } else {
        FlinkLogicalCalc newCalc = FlinkLogicalCalc.create(newScan, program);
        call.transformTo(newCalc);
    }
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RexProgram(org.apache.calcite.rex.RexProgram) FlinkLogicalTableSourceScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableSourceScan) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 12 with TableSourceTable

use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.

the class PushFilterInCalcIntoTableSourceScanRule method onMatch.

@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);
}
Also used : FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) FlinkLogicalTableSourceScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableSourceScan) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable)

Example 13 with TableSourceTable

use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.

the class PushFilterIntoTableSourceScanRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    if (!super.matches(call)) {
        return false;
    }
    Filter filter = call.rel(0);
    if (filter.getCondition() == null) {
        return false;
    }
    LogicalTableScan scan = call.rel(1);
    TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class);
    return canPushdownFilter(tableSourceTable);
}
Also used : Filter(org.apache.calcite.rel.core.Filter) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 14 with TableSourceTable

use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.

the class PushFilterIntoTableSourceScanRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    Filter filter = call.rel(0);
    LogicalTableScan scan = call.rel(1);
    TableSourceTable table = scan.getTable().unwrap(TableSourceTable.class);
    pushFilterIntoScan(call, filter, scan, table);
}
Also used : Filter(org.apache.calcite.rel.core.Filter) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 15 with TableSourceTable

use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.

the class PushFilterIntoTableSourceScanRule method pushFilterIntoScan.

private void pushFilterIntoScan(RelOptRuleCall call, Filter filter, LogicalTableScan scan, FlinkPreparingTableBase relOptTable) {
    RelBuilder relBuilder = call.builder();
    Tuple2<RexNode[], RexNode[]> extractedPredicates = extractPredicates(filter.getInput().getRowType().getFieldNames().toArray(new String[0]), filter.getCondition(), scan, relBuilder.getRexBuilder());
    RexNode[] convertiblePredicates = extractedPredicates._1;
    RexNode[] unconvertedPredicates = extractedPredicates._2;
    if (convertiblePredicates.length == 0) {
        // no condition can be translated to expression
        return;
    }
    Tuple2<SupportsFilterPushDown.Result, TableSourceTable> scanAfterPushdownWithResult = resolveFiltersAndCreateTableSourceTable(convertiblePredicates, relOptTable.unwrap(TableSourceTable.class), scan, relBuilder);
    SupportsFilterPushDown.Result result = scanAfterPushdownWithResult._1;
    TableSourceTable tableSourceTable = scanAfterPushdownWithResult._2;
    LogicalTableScan newScan = LogicalTableScan.create(scan.getCluster(), tableSourceTable, scan.getHints());
    if (result.getRemainingFilters().isEmpty() && unconvertedPredicates.length == 0) {
        call.transformTo(newScan);
    } else {
        RexNode remainingCondition = createRemainingCondition(relBuilder, result.getRemainingFilters(), unconvertedPredicates);
        RexNode simplifiedRemainingCondition = FlinkRexUtil.simplify(relBuilder.getRexBuilder(), remainingCondition, filter.getCluster().getPlanner().getExecutor());
        Filter newFilter = filter.copy(filter.getTraitSet(), newScan, simplifiedRemainingCondition);
        call.transformTo(newFilter);
    }
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) Filter(org.apache.calcite.rel.core.Filter) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

TableSourceTable (org.apache.flink.table.planner.plan.schema.TableSourceTable)25 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)13 LogicalTableScan (org.apache.calcite.rel.logical.LogicalTableScan)9 SourceAbilitySpec (org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec)9 RexNode (org.apache.calcite.rex.RexNode)8 ArrayList (java.util.ArrayList)7 RelDataType (org.apache.calcite.rel.type.RelDataType)7 SourceAbilityContext (org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext)7 RowType (org.apache.flink.table.types.logical.RowType)7 Arrays (java.util.Arrays)5 List (java.util.List)5 RelOptRule (org.apache.calcite.plan.RelOptRule)5 RelOptRuleCall (org.apache.calcite.plan.RelOptRuleCall)5 Filter (org.apache.calcite.rel.core.Filter)5 TableException (org.apache.flink.table.api.TableException)5 FlinkTypeFactory (org.apache.flink.table.planner.calcite.FlinkTypeFactory)5 Collectors (java.util.stream.Collectors)4 RexInputRef (org.apache.calcite.rex.RexInputRef)4 ProjectPushDownSpec (org.apache.flink.table.planner.plan.abilities.source.ProjectPushDownSpec)4 FlinkLogicalTableSourceScan (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableSourceScan)4