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);
}
}
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()));
}
}
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());
}
Aggregations