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