Search in sources :

Example 1 with RexNodeToExpressionConverter

use of org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter in project flink by apache.

the class PushPartitionIntoTableSourceScanRule method readPartitionFromCatalogAndPrune.

private List<Map<String, String>> readPartitionFromCatalogAndPrune(RexBuilder rexBuilder, FlinkContext context, Catalog catalog, ObjectIdentifier tableIdentifier, List<String> allFieldNames, Seq<RexNode> partitionPredicate, Function<List<Map<String, String>>, List<Map<String, String>>> pruner) throws TableNotExistException, TableNotPartitionedException {
    ObjectPath tablePath = tableIdentifier.toObjectPath();
    // build filters
    RexNodeToExpressionConverter converter = new RexNodeToExpressionConverter(rexBuilder, allFieldNames.toArray(new String[0]), context.getFunctionCatalog(), context.getCatalogManager(), TimeZone.getTimeZone(context.getTableConfig().getLocalTimeZone()));
    ArrayList<Expression> partitionFilters = new ArrayList<>();
    Option<ResolvedExpression> subExpr;
    for (RexNode node : JavaConversions.seqAsJavaList(partitionPredicate)) {
        subExpr = node.accept(converter);
        if (!subExpr.isEmpty()) {
            partitionFilters.add(subExpr.get());
        } else {
            // if part of expr is unresolved, we read all partitions and prune.
            return readPartitionFromCatalogWithoutFilterAndPrune(catalog, tablePath, pruner);
        }
    }
    try {
        return catalog.listPartitionsByFilter(tablePath, partitionFilters).stream().map(CatalogPartitionSpec::getPartitionSpec).collect(Collectors.toList());
    } catch (UnsupportedOperationException e) {
        return readPartitionFromCatalogWithoutFilterAndPrune(catalog, tablePath, pruner);
    }
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) RexNodeToExpressionConverter(org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) Expression(org.apache.flink.table.expressions.Expression) ArrayList(java.util.ArrayList) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with RexNodeToExpressionConverter

use of org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter 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)

Example 3 with RexNodeToExpressionConverter

use of org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter in project flink by apache.

the class PushFilterIntoSourceScanRuleBase method extractPredicates.

protected Tuple2<RexNode[], RexNode[]> extractPredicates(String[] inputNames, RexNode filterExpression, TableScan scan, RexBuilder rexBuilder) {
    FlinkContext context = ShortcutUtils.unwrapContext(scan);
    int maxCnfNodeCount = FlinkRelOptUtil.getMaxCnfNodeCount(scan);
    RexNodeToExpressionConverter converter = new RexNodeToExpressionConverter(rexBuilder, inputNames, context.getFunctionCatalog(), context.getCatalogManager(), TimeZone.getTimeZone(context.getTableConfig().getLocalTimeZone()));
    return RexNodeExtractor.extractConjunctiveConditions(filterExpression, maxCnfNodeCount, rexBuilder, converter);
}
Also used : RexNodeToExpressionConverter(org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter) FlinkContext(org.apache.flink.table.planner.calcite.FlinkContext)

Aggregations

RexNodeToExpressionConverter (org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter)3 ArrayList (java.util.ArrayList)2 RexNode (org.apache.calcite.rex.RexNode)2 Expression (org.apache.flink.table.expressions.Expression)2 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)2 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 ObjectPath (org.apache.flink.table.catalog.ObjectPath)1 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)1 SupportsFilterPushDown (org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown)1 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)1 FlinkContext (org.apache.flink.table.planner.calcite.FlinkContext)1