use of org.apache.flink.table.planner.plan.schema.TableSourceTable 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.planner.plan.schema.TableSourceTable in project flink by apache.
the class PushLimitIntoTableSourceScanRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
Sort sort = call.rel(0);
FlinkLogicalTableSourceScan scan = call.rel(1);
int offset = sort.offset == null ? 0 : RexLiteral.intValue(sort.offset);
int limit = offset + RexLiteral.intValue(sort.fetch);
TableSourceTable newTableSourceTable = applyLimit(limit, scan);
FlinkLogicalTableSourceScan newScan = FlinkLogicalTableSourceScan.create(scan.getCluster(), scan.getHints(), newTableSourceTable);
Sort newSort = sort.copy(sort.getTraitSet(), Collections.singletonList(newScan));
call.transformTo(newSort);
}
use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.
the class PushLimitIntoTableSourceScanRule method applyLimit.
private TableSourceTable applyLimit(long limit, FlinkLogicalTableSourceScan scan) {
TableSourceTable relOptTable = scan.getTable().unwrap(TableSourceTable.class);
TableSourceTable oldTableSourceTable = relOptTable.unwrap(TableSourceTable.class);
DynamicTableSource newTableSource = oldTableSourceTable.tableSource().copy();
LimitPushDownSpec limitPushDownSpec = new LimitPushDownSpec(limit);
limitPushDownSpec.apply(newTableSource, SourceAbilityContext.from(scan));
FlinkStatistic statistic = relOptTable.getStatistic();
final long newRowCount;
if (statistic.getRowCount() != null) {
newRowCount = Math.min(limit, statistic.getRowCount().longValue());
} else {
newRowCount = limit;
}
// update TableStats after limit push down
TableStats newTableStats = new TableStats(newRowCount);
FlinkStatistic newStatistic = FlinkStatistic.builder().statistic(statistic).tableStats(newTableStats).build();
return oldTableSourceTable.copy(newTableSource, newStatistic, new SourceAbilitySpec[] { limitPushDownSpec });
}
use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.
the class LookupJoinUtil method getLookupFunction.
/**
* Gets LookupFunction from temporal table according to the given lookup keys.
*/
public static UserDefinedFunction getLookupFunction(RelOptTable temporalTable, Collection<Integer> lookupKeys) {
int[] lookupKeyIndicesInOrder = getOrderedLookupKeys(lookupKeys);
if (temporalTable instanceof TableSourceTable) {
// TODO: support nested lookup keys in the future,
// currently we only support top-level lookup keys
int[][] indices = IntStream.of(lookupKeyIndicesInOrder).mapToObj(i -> new int[] { i }).toArray(int[][]::new);
LookupTableSource tableSource = (LookupTableSource) ((TableSourceTable) temporalTable).tableSource();
LookupRuntimeProviderContext providerContext = new LookupRuntimeProviderContext(indices);
LookupTableSource.LookupRuntimeProvider provider = tableSource.getLookupRuntimeProvider(providerContext);
if (provider instanceof TableFunctionProvider) {
return ((TableFunctionProvider<?>) provider).createTableFunction();
} else if (provider instanceof AsyncTableFunctionProvider) {
return ((AsyncTableFunctionProvider<?>) provider).createAsyncTableFunction();
}
}
if (temporalTable instanceof LegacyTableSourceTable) {
String[] lookupFieldNamesInOrder = IntStream.of(lookupKeyIndicesInOrder).mapToObj(temporalTable.getRowType().getFieldNames()::get).toArray(String[]::new);
LegacyTableSourceTable<?> legacyTableSourceTable = (LegacyTableSourceTable<?>) temporalTable;
LookupableTableSource<?> tableSource = (LookupableTableSource<?>) legacyTableSourceTable.tableSource();
if (tableSource.isAsyncEnabled()) {
return tableSource.getAsyncLookupFunction(lookupFieldNamesInOrder);
} else {
return tableSource.getLookupFunction(lookupFieldNamesInOrder);
}
}
throw new TableException(String.format("table %s is neither TableSourceTable not LegacyTableSourceTable", temporalTable.getQualifiedName()));
}
use of org.apache.flink.table.planner.plan.schema.TableSourceTable in project flink by apache.
the class PushWatermarkIntoTableSourceScanRuleBase method supportsWatermarkPushDown.
protected boolean supportsWatermarkPushDown(FlinkLogicalTableSourceScan scan) {
TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class);
if (tableSourceTable == null) {
return false;
}
final DynamicTableSource tableSource = tableSourceTable.tableSource();
return (tableSource instanceof SupportsWatermarkPushDown) || (tableSource instanceof SupportsSourceWatermark && hasSourceWatermarkDeclaration(tableSourceTable));
}
Aggregations