Search in sources :

Example 1 with BatchPhysicalPythonGroupWindowAggregate

use of org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalPythonGroupWindowAggregate in project flink by apache.

the class BatchPhysicalPythonWindowAggregateRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    FlinkLogicalWindowAggregate agg = call.rel(0);
    RelNode input = agg.getInput();
    LogicalWindow window = agg.getWindow();
    if (!(window instanceof TumblingGroupWindow && AggregateUtil.hasTimeIntervalType(((TumblingGroupWindow) window).size()) || window instanceof SlidingGroupWindow && AggregateUtil.hasTimeIntervalType(((SlidingGroupWindow) window).size()) || window instanceof SessionGroupWindow)) {
        // sliding & tumbling count window and session window not supported
        throw new TableException("Window " + window + " is not supported right now.");
    }
    int[] groupSet = agg.getGroupSet().toArray();
    RelTraitSet traitSet = agg.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    Tuple2<int[], Seq<AggregateCall>> auxGroupSetAndCallsTuple = AggregateUtil.checkAndSplitAggCalls(agg);
    int[] auxGroupSet = auxGroupSetAndCallsTuple._1;
    Seq<AggregateCall> aggCallsWithoutAuxGroupCalls = auxGroupSetAndCallsTuple._2;
    Tuple3<int[][], DataType[][], UserDefinedFunction[]> aggBufferTypesAndFunctions = AggregateUtil.transformToBatchAggregateFunctions(FlinkTypeFactory.toLogicalRowType(input.getRowType()), aggCallsWithoutAuxGroupCalls, null);
    UserDefinedFunction[] aggFunctions = aggBufferTypesAndFunctions._3();
    int inputTimeFieldIndex = AggregateUtil.timeFieldIndex(input.getRowType(), call.builder(), window.timeAttribute());
    RelDataType inputTimeFieldType = input.getRowType().getFieldList().get(inputTimeFieldIndex).getType();
    boolean inputTimeIsDate = inputTimeFieldType.getSqlTypeName() == SqlTypeName.DATE;
    RelTraitSet requiredTraitSet = agg.getTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
    if (groupSet.length != 0) {
        FlinkRelDistribution requiredDistribution = FlinkRelDistribution.hash(groupSet, false);
        requiredTraitSet = requiredTraitSet.replace(requiredDistribution);
    } else {
        requiredTraitSet = requiredTraitSet.replace(FlinkRelDistribution.SINGLETON());
    }
    RelCollation sortCollation = createRelCollation(groupSet, inputTimeFieldIndex);
    requiredTraitSet = requiredTraitSet.replace(sortCollation);
    RelNode newInput = RelOptRule.convert(input, requiredTraitSet);
    BatchPhysicalPythonGroupWindowAggregate windowAgg = new BatchPhysicalPythonGroupWindowAggregate(agg.getCluster(), traitSet, newInput, agg.getRowType(), newInput.getRowType(), groupSet, auxGroupSet, aggCallsWithoutAuxGroupCalls, aggFunctions, window, inputTimeFieldIndex, inputTimeIsDate, agg.getNamedProperties());
    call.transformTo(windowAgg);
}
Also used : TableException(org.apache.flink.table.api.TableException) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) BatchPhysicalPythonGroupWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalPythonGroupWindowAggregate) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AggregateCall(org.apache.calcite.rel.core.AggregateCall) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) RelCollation(org.apache.calcite.rel.RelCollation) LogicalWindow(org.apache.flink.table.planner.plan.logical.LogicalWindow) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) RelNode(org.apache.calcite.rel.RelNode) FlinkLogicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate) DataType(org.apache.flink.table.types.DataType) RelDataType(org.apache.calcite.rel.type.RelDataType) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow) Seq(scala.collection.Seq)

Aggregations

RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 RelCollation (org.apache.calcite.rel.RelCollation)1 RelNode (org.apache.calcite.rel.RelNode)1 AggregateCall (org.apache.calcite.rel.core.AggregateCall)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 TableException (org.apache.flink.table.api.TableException)1 UserDefinedFunction (org.apache.flink.table.functions.UserDefinedFunction)1 LogicalWindow (org.apache.flink.table.planner.plan.logical.LogicalWindow)1 SessionGroupWindow (org.apache.flink.table.planner.plan.logical.SessionGroupWindow)1 SlidingGroupWindow (org.apache.flink.table.planner.plan.logical.SlidingGroupWindow)1 TumblingGroupWindow (org.apache.flink.table.planner.plan.logical.TumblingGroupWindow)1 FlinkLogicalWindowAggregate (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate)1 BatchPhysicalPythonGroupWindowAggregate (org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalPythonGroupWindowAggregate)1 FlinkRelDistribution (org.apache.flink.table.planner.plan.trait.FlinkRelDistribution)1 DataType (org.apache.flink.table.types.DataType)1 Seq (scala.collection.Seq)1