Search in sources :

Example 16 with TableSourceTable

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

the class PushFilterIntoSourceScanRuleBase method resolveFiltersAndCreateTableSourceTable.

/**
 * Resolves filters using the underlying sources {@link SupportsFilterPushDown} and creates a
 * new {@link TableSourceTable} with the supplied predicates.
 *
 * @param convertiblePredicates Predicates to resolve
 * @param oldTableSourceTable TableSourceTable to copy
 * @param scan Underlying table scan to push to
 * @param relBuilder Builder to push the scan to
 * @return A tuple, constituting of the resolved filters and the newly created {@link
 *     TableSourceTable}
 */
protected Tuple2<SupportsFilterPushDown.Result, TableSourceTable> resolveFiltersAndCreateTableSourceTable(RexNode[] convertiblePredicates, TableSourceTable oldTableSourceTable, TableScan scan, RelBuilder relBuilder) {
    // record size before applyFilters for update statistics
    int originPredicatesSize = convertiblePredicates.length;
    // update DynamicTableSource
    DynamicTableSource newTableSource = oldTableSourceTable.tableSource().copy();
    SupportsFilterPushDown.Result result = FilterPushDownSpec.apply(Arrays.asList(convertiblePredicates), newTableSource, SourceAbilityContext.from(scan));
    relBuilder.push(scan);
    List<RexNode> acceptedPredicates = convertExpressionToRexNode(result.getAcceptedFilters(), relBuilder);
    FilterPushDownSpec filterPushDownSpec = new FilterPushDownSpec(acceptedPredicates);
    // record size after applyFilters for update statistics
    int updatedPredicatesSize = result.getRemainingFilters().size();
    // set the newStatistic newTableSource and sourceAbilitySpecs
    TableSourceTable newTableSourceTable = oldTableSourceTable.copy(newTableSource, getNewFlinkStatistic(oldTableSourceTable, originPredicatesSize, updatedPredicatesSize), new SourceAbilitySpec[] { filterPushDownSpec });
    return new Tuple2<>(result, newTableSourceTable);
}
Also used : FilterPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.FilterPushDownSpec) Tuple2(scala.Tuple2) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) RexNode(org.apache.calcite.rex.RexNode)

Example 17 with TableSourceTable

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

the class PushLimitIntoTableSourceScanRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    Sort sort = call.rel(0);
    FlinkLogicalTableSourceScan scan = call.rel(1);
    int offset = sort.offset == null ? 0 : RexLiteral.intValue(sort.offset);
    int limit = offset + RexLiteral.intValue(sort.fetch);
    TableSourceTable newTableSourceTable = applyLimit(limit, scan);
    FlinkLogicalTableSourceScan newScan = FlinkLogicalTableSourceScan.create(scan.getCluster(), scan.getHints(), newTableSourceTable);
    Sort newSort = sort.copy(sort.getTraitSet(), Collections.singletonList(newScan));
    call.transformTo(newSort);
}
Also used : FlinkLogicalTableSourceScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableSourceScan) Sort(org.apache.calcite.rel.core.Sort) FlinkLogicalSort(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalSort) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable)

Example 18 with TableSourceTable

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

the class PushLimitIntoTableSourceScanRule method applyLimit.

private TableSourceTable applyLimit(long limit, FlinkLogicalTableSourceScan scan) {
    TableSourceTable relOptTable = scan.getTable().unwrap(TableSourceTable.class);
    TableSourceTable oldTableSourceTable = relOptTable.unwrap(TableSourceTable.class);
    DynamicTableSource newTableSource = oldTableSourceTable.tableSource().copy();
    LimitPushDownSpec limitPushDownSpec = new LimitPushDownSpec(limit);
    limitPushDownSpec.apply(newTableSource, SourceAbilityContext.from(scan));
    FlinkStatistic statistic = relOptTable.getStatistic();
    final long newRowCount;
    if (statistic.getRowCount() != null) {
        newRowCount = Math.min(limit, statistic.getRowCount().longValue());
    } else {
        newRowCount = limit;
    }
    // update TableStats after limit push down
    TableStats newTableStats = new TableStats(newRowCount);
    FlinkStatistic newStatistic = FlinkStatistic.builder().statistic(statistic).tableStats(newTableStats).build();
    return oldTableSourceTable.copy(newTableSource, newStatistic, new SourceAbilitySpec[] { limitPushDownSpec });
}
Also used : LimitPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.LimitPushDownSpec) FlinkStatistic(org.apache.flink.table.planner.plan.stats.FlinkStatistic) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) TableStats(org.apache.flink.table.plan.stats.TableStats) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource)

Example 19 with TableSourceTable

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

the class LookupJoinUtil method getLookupFunction.

/**
 * Gets LookupFunction from temporal table according to the given lookup keys.
 */
public static UserDefinedFunction getLookupFunction(RelOptTable temporalTable, Collection<Integer> lookupKeys) {
    int[] lookupKeyIndicesInOrder = getOrderedLookupKeys(lookupKeys);
    if (temporalTable instanceof TableSourceTable) {
        // TODO: support nested lookup keys in the future,
        // currently we only support top-level lookup keys
        int[][] indices = IntStream.of(lookupKeyIndicesInOrder).mapToObj(i -> new int[] { i }).toArray(int[][]::new);
        LookupTableSource tableSource = (LookupTableSource) ((TableSourceTable) temporalTable).tableSource();
        LookupRuntimeProviderContext providerContext = new LookupRuntimeProviderContext(indices);
        LookupTableSource.LookupRuntimeProvider provider = tableSource.getLookupRuntimeProvider(providerContext);
        if (provider instanceof TableFunctionProvider) {
            return ((TableFunctionProvider<?>) provider).createTableFunction();
        } else if (provider instanceof AsyncTableFunctionProvider) {
            return ((AsyncTableFunctionProvider<?>) provider).createAsyncTableFunction();
        }
    }
    if (temporalTable instanceof LegacyTableSourceTable) {
        String[] lookupFieldNamesInOrder = IntStream.of(lookupKeyIndicesInOrder).mapToObj(temporalTable.getRowType().getFieldNames()::get).toArray(String[]::new);
        LegacyTableSourceTable<?> legacyTableSourceTable = (LegacyTableSourceTable<?>) temporalTable;
        LookupableTableSource<?> tableSource = (LookupableTableSource<?>) legacyTableSourceTable.tableSource();
        if (tableSource.isAsyncEnabled()) {
            return tableSource.getAsyncLookupFunction(lookupFieldNamesInOrder);
        } else {
            return tableSource.getLookupFunction(lookupFieldNamesInOrder);
        }
    }
    throw new TableException(String.format("table %s is neither TableSourceTable not LegacyTableSourceTable", temporalTable.getQualifiedName()));
}
Also used : IntStream(java.util.stream.IntStream) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) JsonCreator(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) JsonSubTypes(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes) RexLiteral(org.apache.calcite.rex.RexLiteral) Collection(java.util.Collection) TableException(org.apache.flink.table.api.TableException) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LookupableTableSource(org.apache.flink.table.sources.LookupableTableSource) JsonProperty(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) LookupTableSource(org.apache.flink.table.connector.source.LookupTableSource) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonTypeInfo(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeInfo) Internal(org.apache.flink.annotation.Internal) TableFunctionProvider(org.apache.flink.table.connector.source.TableFunctionProvider) JsonIgnoreProperties(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnoreProperties) LookupRuntimeProviderContext(org.apache.flink.table.runtime.connector.source.LookupRuntimeProviderContext) JsonTypeName(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName) TableException(org.apache.flink.table.api.TableException) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) TableFunctionProvider(org.apache.flink.table.connector.source.TableFunctionProvider) LookupableTableSource(org.apache.flink.table.sources.LookupableTableSource) LookupRuntimeProviderContext(org.apache.flink.table.runtime.connector.source.LookupRuntimeProviderContext) LookupTableSource(org.apache.flink.table.connector.source.LookupTableSource) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable)

Example 20 with TableSourceTable

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

the class PushWatermarkIntoTableSourceScanRuleBase method supportsWatermarkPushDown.

protected boolean supportsWatermarkPushDown(FlinkLogicalTableSourceScan scan) {
    TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class);
    if (tableSourceTable == null) {
        return false;
    }
    final DynamicTableSource tableSource = tableSourceTable.tableSource();
    return (tableSource instanceof SupportsWatermarkPushDown) || (tableSource instanceof SupportsSourceWatermark && hasSourceWatermarkDeclaration(tableSourceTable));
}
Also used : SupportsSourceWatermark(org.apache.flink.table.connector.source.abilities.SupportsSourceWatermark) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) SupportsWatermarkPushDown(org.apache.flink.table.connector.source.abilities.SupportsWatermarkPushDown)

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