Search in sources :

Example 31 with Expression

use of org.apache.flink.table.expressions.Expression in project flink by apache.

the class FilterUtils method binaryFilterApplies.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static boolean binaryFilterApplies(CallExpression binExpr, Function<String, Comparable<?>> getter) {
    List<Expression> children = binExpr.getChildren();
    Preconditions.checkArgument(children.size() == 2);
    Comparable lhsValue = getValue(children.get(0), getter);
    Comparable rhsValue = getValue(children.get(1), getter);
    FunctionDefinition functionDefinition = binExpr.getFunctionDefinition();
    if (BuiltInFunctionDefinitions.GREATER_THAN.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) > 0;
    } else if (BuiltInFunctionDefinitions.LESS_THAN.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) < 0;
    } else if (BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) >= 0;
    } else if (BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) <= 0;
    } else if (BuiltInFunctionDefinitions.EQUALS.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) == 0;
    } else if (BuiltInFunctionDefinitions.NOT_EQUALS.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) != 0;
    } else {
        throw new UnsupportedOperationException("Unsupported operator: " + functionDefinition);
    }
}
Also used : CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition)

Example 32 with Expression

use of org.apache.flink.table.expressions.Expression 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 33 with Expression

use of org.apache.flink.table.expressions.Expression in project flink by apache.

the class TestValuesCatalog method listPartitionsByFilter.

@Override
public List<CatalogPartitionSpec> listPartitionsByFilter(ObjectPath tablePath, List<Expression> filters) throws TableNotExistException, TableNotPartitionedException, CatalogException {
    if (!supportListPartitionByFilter) {
        throw new UnsupportedOperationException("TestValuesCatalog doesn't support list partition by filters");
    }
    List<CatalogPartitionSpec> partitions = listPartitions(tablePath);
    if (partitions.isEmpty()) {
        return partitions;
    }
    CatalogBaseTable table = this.getTable(tablePath);
    TableSchema schema = table.getSchema();
    List<ResolvedExpression> resolvedExpressions = filters.stream().map(filter -> {
        if (filter instanceof ResolvedExpression) {
            return (ResolvedExpression) filter;
        }
        throw new UnsupportedOperationException(String.format("TestValuesCatalog only works with resolved expressions. Get unresolved expression: %s", filter));
    }).collect(Collectors.toList());
    return partitions.stream().filter(partition -> {
        Function<String, Comparable<?>> getter = getValueGetter(partition.getPartitionSpec(), schema);
        return FilterUtils.isRetainedAfterApplyingFilterPredicates(resolvedExpressions, getter);
    }).collect(Collectors.toList());
}
Also used : DataType(org.apache.flink.table.types.DataType) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) FilterUtils(org.apache.flink.table.planner.utils.FilterUtils) IntType(org.apache.flink.table.types.logical.IntType) TableException(org.apache.flink.table.api.TableException) TableSchema(org.apache.flink.table.api.TableSchema) VarCharType(org.apache.flink.table.types.logical.VarCharType) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) Expression(org.apache.flink.table.expressions.Expression) ObjectPath(org.apache.flink.table.catalog.ObjectPath) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) CharType(org.apache.flink.table.types.logical.CharType) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) List(java.util.List) TableNotPartitionedException(org.apache.flink.table.catalog.exceptions.TableNotPartitionedException) DoubleType(org.apache.flink.table.types.logical.DoubleType) LogicalType(org.apache.flink.table.types.logical.LogicalType) BooleanType(org.apache.flink.table.types.logical.BooleanType) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) Map(java.util.Map) Optional(java.util.Optional) GenericInMemoryCatalog(org.apache.flink.table.catalog.GenericInMemoryCatalog) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) Function(java.util.function.Function) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) TableSchema(org.apache.flink.table.api.TableSchema) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec)

Aggregations

Expression (org.apache.flink.table.expressions.Expression)33 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)18 ArrayList (java.util.ArrayList)13 CallExpression (org.apache.flink.table.expressions.CallExpression)12 UnresolvedCallExpression (org.apache.flink.table.expressions.UnresolvedCallExpression)12 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)11 UnresolvedReferenceExpression (org.apache.flink.table.expressions.UnresolvedReferenceExpression)9 RexNode (org.apache.calcite.rex.RexNode)8 ValidationException (org.apache.flink.table.api.ValidationException)8 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)8 SqlExpressionResolver (org.apache.flink.table.expressions.resolver.SqlExpressionResolver)8 QueryOperation (org.apache.flink.table.operations.QueryOperation)8 List (java.util.List)7 TableException (org.apache.flink.table.api.TableException)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)6 Collectors (java.util.stream.Collectors)5 DistinctQueryOperation (org.apache.flink.table.operations.DistinctQueryOperation)5 FilterQueryOperation (org.apache.flink.table.operations.FilterQueryOperation)5 ValuesQueryOperation (org.apache.flink.table.operations.ValuesQueryOperation)5 HashSet (java.util.HashSet)3