Search in sources :

Example 21 with TableSourceTable

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

the class PushLocalAggIntoScanRuleBase method canPushDown.

protected boolean canPushDown(RelOptRuleCall call, BatchPhysicalGroupAggregateBase aggregate, BatchPhysicalTableSourceScan tableSourceScan) {
    TableConfig tableConfig = ShortcutUtils.unwrapContext(call.getPlanner()).getTableConfig();
    if (!tableConfig.getConfiguration().getBoolean(OptimizerConfigOptions.TABLE_OPTIMIZER_SOURCE_AGGREGATE_PUSHDOWN_ENABLED)) {
        return false;
    }
    if (aggregate.isFinal() || aggregate.getAggCallList().isEmpty()) {
        return false;
    }
    List<AggregateCall> aggCallList = JavaScalaConversionUtil.toJava(aggregate.getAggCallList());
    for (AggregateCall aggCall : aggCallList) {
        if (aggCall.isDistinct() || aggCall.isApproximate() || aggCall.getArgList().size() > 1 || aggCall.hasFilter() || !aggCall.getCollation().getFieldCollations().isEmpty()) {
            return false;
        }
    }
    TableSourceTable tableSourceTable = tableSourceScan.tableSourceTable();
    // we can not push aggregates twice
    return tableSourceTable != null && tableSourceTable.tableSource() instanceof SupportsAggregatePushDown && Arrays.stream(tableSourceTable.abilitySpecs()).noneMatch(spec -> spec instanceof AggregatePushDownSpec);
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) SupportsAggregatePushDown(org.apache.flink.table.connector.source.abilities.SupportsAggregatePushDown) Arrays(java.util.Arrays) RexProgram(org.apache.calcite.rex.RexProgram) AggregatePushDownSpec(org.apache.flink.table.planner.plan.abilities.source.AggregatePushDownSpec) SourceAbilityContext(org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext) RexNodeExtractor(org.apache.flink.table.planner.plan.utils.RexNodeExtractor) ShortcutUtils(org.apache.flink.table.planner.utils.ShortcutUtils) ArrayUtils(org.apache.commons.lang3.ArrayUtils) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RowType(org.apache.flink.table.types.logical.RowType) ArrayList(java.util.ArrayList) OptimizerConfigOptions(org.apache.flink.table.api.config.OptimizerConfigOptions) BatchPhysicalCalc(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalCalc) RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand) RexNode(org.apache.calcite.rex.RexNode) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) TableConfig(org.apache.flink.table.api.TableConfig) ProjectPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.ProjectPushDownSpec) BatchPhysicalTableSourceScan(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalTableSourceScan) BatchPhysicalGroupAggregateBase(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalGroupAggregateBase) Collectors(java.util.stream.Collectors) SourceAbilitySpec(org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptRule(org.apache.calcite.plan.RelOptRule) List(java.util.List) BatchPhysicalExchange(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalExchange) FlinkStatistic(org.apache.flink.table.planner.plan.stats.FlinkStatistic) JavaScalaConversionUtil(org.apache.flink.table.planner.utils.JavaScalaConversionUtil) AggregateCall(org.apache.calcite.rel.core.AggregateCall) Collections(java.util.Collections) AggregatePushDownSpec(org.apache.flink.table.planner.plan.abilities.source.AggregatePushDownSpec) TableConfig(org.apache.flink.table.api.TableConfig) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) SupportsAggregatePushDown(org.apache.flink.table.connector.source.abilities.SupportsAggregatePushDown)

Example 22 with TableSourceTable

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

the class PushLimitIntoTableSourceScanRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    Sort sort = call.rel(0);
    TableSourceTable tableSourceTable = call.rel(1).getTable().unwrap(TableSourceTable.class);
    // a limit can be pushed down only if it satisfies the two conditions: 1) do not have order
    // by keys, 2) have limit.
    boolean onlyLimit = sort.getCollation().getFieldCollations().isEmpty() && sort.fetch != null;
    return onlyLimit && tableSourceTable != null && tableSourceTable.tableSource() instanceof SupportsLimitPushDown && Arrays.stream(tableSourceTable.abilitySpecs()).noneMatch(spec -> spec instanceof LimitPushDownSpec);
}
Also used : Arrays(java.util.Arrays) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) SupportsLimitPushDown(org.apache.flink.table.connector.source.abilities.SupportsLimitPushDown) SourceAbilityContext(org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext) RexLiteral(org.apache.calcite.rex.RexLiteral) LimitPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.LimitPushDownSpec) FlinkLogicalTableSourceScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableSourceScan) SourceAbilitySpec(org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RelOptRule(org.apache.calcite.plan.RelOptRule) FlinkStatistic(org.apache.flink.table.planner.plan.stats.FlinkStatistic) Sort(org.apache.calcite.rel.core.Sort) TableStats(org.apache.flink.table.plan.stats.TableStats) FlinkLogicalSort(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalSort) Collections(java.util.Collections) LimitPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.LimitPushDownSpec) Sort(org.apache.calcite.rel.core.Sort) FlinkLogicalSort(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalSort) SupportsLimitPushDown(org.apache.flink.table.connector.source.abilities.SupportsLimitPushDown) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable)

Example 23 with TableSourceTable

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

the class PushProjectIntoTableSourceScanRule method getPrimaryKeyProjections.

private List<RexNode> getPrimaryKeyProjections(LogicalTableScan scan) {
    final TableSourceTable source = scan.getTable().unwrap(TableSourceTable.class);
    final ResolvedSchema schema = source.contextResolvedTable().getResolvedSchema();
    if (!schema.getPrimaryKey().isPresent()) {
        return Collections.emptyList();
    }
    final FlinkTypeFactory typeFactory = unwrapTypeFactory(scan);
    final UniqueConstraint primaryKey = schema.getPrimaryKey().get();
    return primaryKey.getColumns().stream().map(columnName -> {
        final int idx = scan.getRowType().getFieldNames().indexOf(columnName);
        final Column column = schema.getColumn(idx).orElseThrow(() -> new TableException(String.format("Column at index %d not found.", idx)));
        return new RexInputRef(idx, typeFactory.createFieldTypeFromLogicalType(column.getDataType().getLogicalType()));
    }).collect(Collectors.toList());
}
Also used : IntStream(java.util.stream.IntStream) NestedProjectionUtil(org.apache.flink.table.planner.plan.utils.NestedProjectionUtil) Arrays(java.util.Arrays) ShortcutUtils.unwrapTypeFactory(org.apache.flink.table.planner.utils.ShortcutUtils.unwrapTypeFactory) SourceAbilityContext(org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext) Column(org.apache.flink.table.catalog.Column) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) RexNodeExtractor(org.apache.flink.table.planner.plan.utils.RexNodeExtractor) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RowType(org.apache.flink.table.types.logical.RowType) SupportsProjectionPushDown(org.apache.flink.table.connector.source.abilities.SupportsProjectionPushDown) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode) NestedSchema(org.apache.flink.table.planner.plan.utils.NestedSchema) Projection(org.apache.flink.table.connector.Projection) ProjectRemoveRule(org.apache.calcite.rel.rules.ProjectRemoveRule) DynamicSourceUtils.createProducedType(org.apache.flink.table.planner.connectors.DynamicSourceUtils.createProducedType) RelDataType(org.apache.calcite.rel.type.RelDataType) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) TableConfig(org.apache.flink.table.api.TableConfig) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) ProjectPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.ProjectPushDownSpec) TableException(org.apache.flink.table.api.TableException) ShortcutUtils.unwrapContext(org.apache.flink.table.planner.utils.ShortcutUtils.unwrapContext) RelRule(org.apache.calcite.plan.RelRule) NestedColumn(org.apache.flink.table.planner.plan.utils.NestedColumn) Collectors(java.util.stream.Collectors) DynamicSourceUtils.createRequiredMetadataKeys(org.apache.flink.table.planner.connectors.DynamicSourceUtils.createRequiredMetadataKeys) SourceAbilitySpec(org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RexInputRef(org.apache.calcite.rex.RexInputRef) Objects(java.util.Objects) DynamicSourceUtils(org.apache.flink.table.planner.connectors.DynamicSourceUtils) RelOptRule(org.apache.calcite.plan.RelOptRule) List(java.util.List) Stream(java.util.stream.Stream) UniqueConstraint(org.apache.flink.table.catalog.UniqueConstraint) SupportsReadingMetadata(org.apache.flink.table.connector.source.abilities.SupportsReadingMetadata) ReadingMetadataSpec(org.apache.flink.table.planner.plan.abilities.source.ReadingMetadataSpec) Internal(org.apache.flink.annotation.Internal) Collections(java.util.Collections) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) TableException(org.apache.flink.table.api.TableException) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) Column(org.apache.flink.table.catalog.Column) NestedColumn(org.apache.flink.table.planner.plan.utils.NestedColumn) UniqueConstraint(org.apache.flink.table.catalog.UniqueConstraint) RexInputRef(org.apache.calcite.rex.RexInputRef) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema)

Example 24 with TableSourceTable

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

the class PushProjectIntoTableSourceScanRule method getProjections.

private List<RexNode> getProjections(LogicalProject project, LogicalTableScan scan) {
    final TableSourceTable source = scan.getTable().unwrap(TableSourceTable.class);
    final TableConfig tableConfig = unwrapContext(scan).getTableConfig();
    final List<RexNode> projections = new ArrayList<>(project.getProjects());
    if (supportsProjectionPushDown(source.tableSource()) && requiresPrimaryKey(source, tableConfig)) {
        projections.addAll(getPrimaryKeyProjections(scan));
    }
    return projections;
}
Also used : ArrayList(java.util.ArrayList) TableConfig(org.apache.flink.table.api.TableConfig) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) RexNode(org.apache.calcite.rex.RexNode)

Example 25 with TableSourceTable

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

the class DynamicSourceUtils method pushTableScan.

private static void pushTableScan(boolean isBatchMode, FlinkRelBuilder relBuilder, ContextResolvedTable contextResolvedTable, FlinkStatistic statistic, List<RelHint> hints, DynamicTableSource tableSource) {
    final RowType producedType = createProducedType(contextResolvedTable.getResolvedSchema(), tableSource);
    final RelDataType producedRelDataType = relBuilder.getTypeFactory().buildRelNodeRowType(producedType);
    final TableSourceTable tableSourceTable = new TableSourceTable(relBuilder.getRelOptSchema(), producedRelDataType, statistic, tableSource, !isBatchMode, contextResolvedTable, ShortcutUtils.unwrapContext(relBuilder), new SourceAbilitySpec[0]);
    final LogicalTableScan scan = LogicalTableScan.create(relBuilder.getCluster(), tableSourceTable, hints);
    relBuilder.push(scan);
}
Also used : RowType(org.apache.flink.table.types.logical.RowType) RelDataType(org.apache.calcite.rel.type.RelDataType) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

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