use of org.apache.flink.table.planner.plan.abilities.source.AggregatePushDownSpec in project flink by apache.
the class PushLocalAggIntoScanRuleBase method pushLocalAggregateIntoScan.
protected void pushLocalAggregateIntoScan(RelOptRuleCall call, BatchPhysicalGroupAggregateBase localAgg, BatchPhysicalTableSourceScan oldScan, int[] calcRefFields) {
RowType inputType = FlinkTypeFactory.toLogicalRowType(oldScan.getRowType());
List<int[]> groupingSets = Collections.singletonList(ArrayUtils.addAll(localAgg.grouping(), localAgg.auxGrouping()));
List<AggregateCall> aggCallList = JavaScalaConversionUtil.toJava(localAgg.getAggCallList());
// map arg index in aggregate to field index in scan through referred fields by calc.
if (calcRefFields != null) {
groupingSets = translateGroupingArgIndex(groupingSets, calcRefFields);
aggCallList = translateAggCallArgIndex(aggCallList, calcRefFields);
}
RowType producedType = FlinkTypeFactory.toLogicalRowType(localAgg.getRowType());
TableSourceTable oldTableSourceTable = oldScan.tableSourceTable();
DynamicTableSource newTableSource = oldScan.tableSource().copy();
boolean isPushDownSuccess = AggregatePushDownSpec.apply(inputType, groupingSets, aggCallList, producedType, newTableSource, SourceAbilityContext.from(oldScan));
if (!isPushDownSuccess) {
// aggregate push down failed, just return without changing any nodes.
return;
}
// create new source table with new spec and statistic.
AggregatePushDownSpec aggregatePushDownSpec = new AggregatePushDownSpec(inputType, groupingSets, aggCallList, producedType);
TableSourceTable newTableSourceTable = oldTableSourceTable.copy(newTableSource, localAgg.getRowType(), new SourceAbilitySpec[] { aggregatePushDownSpec }).copy(FlinkStatistic.UNKNOWN());
// transform to new nodes.
BatchPhysicalTableSourceScan newScan = oldScan.copy(oldScan.getTraitSet(), newTableSourceTable);
BatchPhysicalExchange oldExchange = call.rel(0);
BatchPhysicalExchange newExchange = oldExchange.copy(oldExchange.getTraitSet(), newScan, oldExchange.getDistribution());
call.transformTo(newExchange);
}
use of org.apache.flink.table.planner.plan.abilities.source.AggregatePushDownSpec in project flink by apache.
the class PushLocalAggIntoScanRuleBase method canPushDown.
protected boolean canPushDown(RelOptRuleCall call, BatchPhysicalGroupAggregateBase aggregate, BatchPhysicalTableSourceScan tableSourceScan) {
TableConfig tableConfig = ShortcutUtils.unwrapContext(call.getPlanner()).getTableConfig();
if (!tableConfig.getConfiguration().getBoolean(OptimizerConfigOptions.TABLE_OPTIMIZER_SOURCE_AGGREGATE_PUSHDOWN_ENABLED)) {
return false;
}
if (aggregate.isFinal() || aggregate.getAggCallList().isEmpty()) {
return false;
}
List<AggregateCall> aggCallList = JavaScalaConversionUtil.toJava(aggregate.getAggCallList());
for (AggregateCall aggCall : aggCallList) {
if (aggCall.isDistinct() || aggCall.isApproximate() || aggCall.getArgList().size() > 1 || aggCall.hasFilter() || !aggCall.getCollation().getFieldCollations().isEmpty()) {
return false;
}
}
TableSourceTable tableSourceTable = tableSourceScan.tableSourceTable();
// we can not push aggregates twice
return tableSourceTable != null && tableSourceTable.tableSource() instanceof SupportsAggregatePushDown && Arrays.stream(tableSourceTable.abilitySpecs()).noneMatch(spec -> spec instanceof AggregatePushDownSpec);
}
Aggregations