Search in sources :

Example 1 with StreamPhysicalWindowAggregate

use of org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowAggregate in project flink by apache.

the class TwoStageOptimizedWindowAggregateRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final StreamPhysicalWindowAggregate windowAgg = call.rel(0);
    final RelNode realInput = call.rel(2);
    final WindowingStrategy windowing = windowAgg.windowing();
    RelTraitSet localTraitSet = realInput.getTraitSet().plus(ModifyKindSetTrait.INSERT_ONLY()).plus(UpdateKindTrait.NONE());
    StreamPhysicalLocalWindowAggregate localAgg = new StreamPhysicalLocalWindowAggregate(windowAgg.getCluster(), localTraitSet, realInput, windowAgg.grouping(), windowAgg.aggCalls(), windowing);
    // grouping keys is forwarded by local agg, use indices instead of groupings
    int[] globalGrouping = IntStream.range(0, windowAgg.grouping().length).toArray();
    FlinkRelDistribution globalDistribution = createDistribution(globalGrouping);
    // create exchange if needed
    RelNode newInput = FlinkExpandConversionRule.satisfyDistribution(FlinkConventions.STREAM_PHYSICAL(), localAgg, globalDistribution);
    RelTraitSet globalAggProvidedTraitSet = windowAgg.getTraitSet();
    // we put sliceEnd/windowEnd at the end of local output fields
    int endIndex = localAgg.getRowType().getFieldCount() - 1;
    final WindowingStrategy globalWindowing;
    if (windowing instanceof TimeAttributeWindowingStrategy) {
        globalWindowing = new SliceAttachedWindowingStrategy(windowing.getWindow(), windowing.getTimeAttributeType(), endIndex);
    } else {
        globalWindowing = new WindowAttachedWindowingStrategy(windowing.getWindow(), windowing.getTimeAttributeType(), endIndex);
    }
    StreamPhysicalGlobalWindowAggregate globalAgg = new StreamPhysicalGlobalWindowAggregate(windowAgg.getCluster(), globalAggProvidedTraitSet, newInput, realInput.getRowType(), globalGrouping, windowAgg.aggCalls(), globalWindowing, windowAgg.namedWindowProperties());
    call.transformTo(globalAgg);
}
Also used : StreamPhysicalLocalWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalLocalWindowAggregate) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) RelNode(org.apache.calcite.rel.RelNode) StreamPhysicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowAggregate) TimeAttributeWindowingStrategy(org.apache.flink.table.planner.plan.logical.TimeAttributeWindowingStrategy) SliceAttachedWindowingStrategy(org.apache.flink.table.planner.plan.logical.SliceAttachedWindowingStrategy) WindowAttachedWindowingStrategy(org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy) RelTraitSet(org.apache.calcite.plan.RelTraitSet) StreamPhysicalGlobalWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalGlobalWindowAggregate) WindowAttachedWindowingStrategy(org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy) TimeAttributeWindowingStrategy(org.apache.flink.table.planner.plan.logical.TimeAttributeWindowingStrategy) WindowingStrategy(org.apache.flink.table.planner.plan.logical.WindowingStrategy) SliceAttachedWindowingStrategy(org.apache.flink.table.planner.plan.logical.SliceAttachedWindowingStrategy)

Example 2 with StreamPhysicalWindowAggregate

use of org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowAggregate in project flink by apache.

the class TwoStageOptimizedWindowAggregateRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    final StreamPhysicalWindowAggregate windowAgg = call.rel(0);
    final RelNode realInput = call.rel(2);
    final TableConfig tableConfig = unwrapContext(call.getPlanner()).getTableConfig();
    final WindowingStrategy windowing = windowAgg.windowing();
    // the two-phase optimization must be enabled
    if (getAggPhaseStrategy(tableConfig) == AggregatePhaseStrategy.ONE_PHASE) {
        return false;
    }
    // otherwise the processing-time can't be materialized in a single node
    if (!windowing.isRowtime()) {
        return false;
    }
    // all aggregate function should support merge() method
    if (!AggregateUtil.doAllSupportPartialMerge(windowAgg.aggInfoList().aggInfos())) {
        return false;
    }
    return !isInputSatisfyRequiredDistribution(realInput, windowAgg.grouping());
}
Also used : RelNode(org.apache.calcite.rel.RelNode) StreamPhysicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowAggregate) TableConfig(org.apache.flink.table.api.TableConfig) WindowAttachedWindowingStrategy(org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy) TimeAttributeWindowingStrategy(org.apache.flink.table.planner.plan.logical.TimeAttributeWindowingStrategy) WindowingStrategy(org.apache.flink.table.planner.plan.logical.WindowingStrategy) SliceAttachedWindowingStrategy(org.apache.flink.table.planner.plan.logical.SliceAttachedWindowingStrategy)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)2 SliceAttachedWindowingStrategy (org.apache.flink.table.planner.plan.logical.SliceAttachedWindowingStrategy)2 TimeAttributeWindowingStrategy (org.apache.flink.table.planner.plan.logical.TimeAttributeWindowingStrategy)2 WindowAttachedWindowingStrategy (org.apache.flink.table.planner.plan.logical.WindowAttachedWindowingStrategy)2 WindowingStrategy (org.apache.flink.table.planner.plan.logical.WindowingStrategy)2 StreamPhysicalWindowAggregate (org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalWindowAggregate)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 TableConfig (org.apache.flink.table.api.TableConfig)1 StreamPhysicalGlobalWindowAggregate (org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalGlobalWindowAggregate)1 StreamPhysicalLocalWindowAggregate (org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalLocalWindowAggregate)1 FlinkRelDistribution (org.apache.flink.table.planner.plan.trait.FlinkRelDistribution)1