Search in sources :

Example 6 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan 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 7 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan 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 8 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan 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)

Example 9 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan in project druid by druid-io.

the class DruidTableScanRule method onMatch.

@Override
public void onMatch(final RelOptRuleCall call) {
    final LogicalTableScan scan = call.rel(0);
    final RelOptTable table = scan.getTable();
    final DruidTable druidTable = table.unwrap(DruidTable.class);
    if (druidTable != null) {
        call.transformTo(DruidQueryRel.fullScan(scan.getCluster(), table, druidTable, queryMaker));
    }
}
Also used : DruidTable(io.druid.sql.calcite.table.DruidTable) RelOptTable(org.apache.calcite.plan.RelOptTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 10 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan in project samza by apache.

the class TestQueryPlanner method testRemoteJoinWithFilterHelper.

void testRemoteJoinWithFilterHelper(boolean enableOptimizer) throws SamzaSqlValidatorException {
    Map<String, String> staticConfigs = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(1);
    String sql = "Insert into testavro.enrichedPageViewTopic " + "select pv.pageKey as __key__, pv.pageKey as pageKey, coalesce(null, 'N/A') as companyName," + "       p.name as profileName, p.address as profileAddress " + "from testavro.PAGEVIEW as pv " + "join testRemoteStore.Profile.`$table` as p " + " on p.__key__ = pv.profileId" + " where p.name = pv.pageKey AND p.name = 'Mike' AND pv.profileId = 1";
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMT, sql);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_ENABLE_PLAN_OPTIMIZER, Boolean.toString(enableOptimizer));
    Config samzaConfig = new MapConfig(staticConfigs);
    DslConverter dslConverter = new SamzaSqlDslConverterFactory().create(samzaConfig);
    Collection<RelRoot> relRoots = dslConverter.convertDsl(sql);
    /*
      Query plan without optimization:
      LogicalProject(__key__=[$1], pageKey=[$1], companyName=['N/A'], profileName=[$5], profileAddress=[$7])
        LogicalFilter(condition=[AND(=($5, $1), =($5, 'Mike'), =($2, 1))])
          LogicalJoin(condition=[=($3, $2)], joinType=[inner])
            LogicalTableScan(table=[[testavro, PAGEVIEW]])
            LogicalTableScan(table=[[testRemoteStore, Profile, $table]])

      Query plan with optimization:
      LogicalProject(__key__=[$1], pageKey=[$1], companyName=['N/A'], profileName=[$5], profileAddress=[$7])
        LogicalFilter(condition=[AND(=($5, $1), =($5, 'Mike'))])
          LogicalJoin(condition=[=($3, $2)], joinType=[inner])
            LogicalFilter(condition=[=($2, 1)])
              LogicalTableScan(table=[[testavro, PAGEVIEW]])
            LogicalTableScan(table=[[testRemoteStore, Profile, $table]])
     */
    assertEquals(1, relRoots.size());
    RelRoot relRoot = relRoots.iterator().next();
    RelNode relNode = relRoot.rel;
    assertTrue(relNode instanceof LogicalProject);
    relNode = relNode.getInput(0);
    assertTrue(relNode instanceof LogicalFilter);
    if (enableOptimizer) {
        assertEquals("AND(=($1, $5), =($5, 'Mike'))", ((LogicalFilter) relNode).getCondition().toString());
    } else {
        assertEquals("AND(=(1, $2), =($1, $5), =($5, 'Mike'))", ((LogicalFilter) relNode).getCondition().toString());
    }
    relNode = relNode.getInput(0);
    assertTrue(relNode instanceof LogicalJoin);
    assertEquals(2, relNode.getInputs().size());
    LogicalJoin join = (LogicalJoin) relNode;
    RelNode left = join.getLeft();
    RelNode right = join.getRight();
    assertTrue(right instanceof LogicalTableScan);
    if (enableOptimizer) {
        assertTrue(left instanceof LogicalFilter);
        assertEquals("=(1, $2)", ((LogicalFilter) left).getCondition().toString());
        assertTrue(left.getInput(0) instanceof LogicalTableScan);
    } else {
        assertTrue(left instanceof LogicalTableScan);
    }
}
Also used : DslConverter(org.apache.samza.sql.interfaces.DslConverter) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SamzaSqlDslConverterFactory(org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory) RelRoot(org.apache.calcite.rel.RelRoot) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) RelNode(org.apache.calcite.rel.RelNode) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) MapConfig(org.apache.samza.config.MapConfig) LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Aggregations

LogicalTableScan (org.apache.calcite.rel.logical.LogicalTableScan)23 RelDataType (org.apache.calcite.rel.type.RelDataType)10 TableSourceTable (org.apache.flink.table.planner.plan.schema.TableSourceTable)8 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 RelNode (org.apache.calcite.rel.RelNode)6 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)5 Filter (org.apache.calcite.rel.core.Filter)4 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)4 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)4 RexNode (org.apache.calcite.rex.RexNode)4 RelBuilder (org.apache.calcite.tools.RelBuilder)4 ArrayList (java.util.ArrayList)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 RelOptTable (org.apache.calcite.plan.RelOptTable)3 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)3 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)3 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)3 Before (org.junit.Before)3