Search in sources :

Example 1 with SupportsFilterPushDown

use of org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown 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 2 with SupportsFilterPushDown

use of org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown in project flink by apache.

the class FilterPushDownSpec method apply.

public static SupportsFilterPushDown.Result apply(List<RexNode> predicates, DynamicTableSource tableSource, SourceAbilityContext context) {
    if (tableSource instanceof SupportsFilterPushDown) {
        RexNodeToExpressionConverter converter = new RexNodeToExpressionConverter(new RexBuilder(FlinkTypeFactory.INSTANCE()), context.getSourceRowType().getFieldNames().toArray(new String[0]), context.getFunctionCatalog(), context.getCatalogManager(), TimeZone.getTimeZone(context.getTableConfig().getLocalTimeZone()));
        List<Expression> filters = predicates.stream().map(p -> {
            scala.Option<ResolvedExpression> expr = p.accept(converter);
            if (expr.isDefined()) {
                return expr.get();
            } else {
                throw new TableException(String.format("%s can not be converted to Expression, please make sure %s can accept %s.", p.toString(), tableSource.getClass().getSimpleName(), p.toString()));
            }
        }).collect(Collectors.toList());
        ExpressionResolver resolver = ExpressionResolver.resolverFor(context.getTableConfig(), name -> Optional.empty(), context.getFunctionCatalog().asLookup(str -> {
            throw new TableException("We should not need to lookup any expressions at this point");
        }), context.getCatalogManager().getDataTypeFactory(), (sqlExpression, inputRowType, outputType) -> {
            throw new TableException("SQL expression parsing is not supported at this location.");
        }).build();
        return ((SupportsFilterPushDown) tableSource).applyFilters(resolver.resolve(filters));
    } else {
        throw new TableException(String.format("%s does not support SupportsFilterPushDown.", tableSource.getClass().getName()));
    }
}
Also used : DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) FlinkRexUtil(org.apache.flink.table.planner.plan.utils.FlinkRexUtil) JsonCreator(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator) RexBuilder(org.apache.calcite.rex.RexBuilder) TimeZone(java.util.TimeZone) TableException(org.apache.flink.table.api.TableException) Expression(org.apache.flink.table.expressions.Expression) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RowType(org.apache.flink.table.types.logical.RowType) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) Collectors(java.util.stream.Collectors) JsonProperty(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) RexNode(org.apache.calcite.rex.RexNode) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) JavaScalaConversionUtil(org.apache.flink.table.planner.utils.JavaScalaConversionUtil) Optional(java.util.Optional) RexNodeToExpressionConverter(org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) JsonTypeName(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName) TableException(org.apache.flink.table.api.TableException) RexNodeToExpressionConverter(org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter) Expression(org.apache.flink.table.expressions.Expression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) RexBuilder(org.apache.calcite.rex.RexBuilder) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver)

Aggregations

RexNode (org.apache.calcite.rex.RexNode)2 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)2 SupportsFilterPushDown (org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 TimeZone (java.util.TimeZone)1 Collectors (java.util.stream.Collectors)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 JsonCreator (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator)1 JsonProperty (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty)1 JsonTypeName (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName)1 TableException (org.apache.flink.table.api.TableException)1 Expression (org.apache.flink.table.expressions.Expression)1 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)1 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)1 FlinkTypeFactory (org.apache.flink.table.planner.calcite.FlinkTypeFactory)1 FilterPushDownSpec (org.apache.flink.table.planner.plan.abilities.source.FilterPushDownSpec)1 TableSourceTable (org.apache.flink.table.planner.plan.schema.TableSourceTable)1