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