Search in sources :

Example 6 with SessionGroupWindow

use of org.apache.flink.table.planner.plan.logical.SessionGroupWindow in project flink by apache.

the class StreamExecGroupWindowAggregate method createWindowOperator.

private WindowOperator<?, ?> createWindowOperator(ReadableConfig config, GeneratedClass<?> aggsHandler, GeneratedRecordEqualiser recordEqualiser, LogicalType[] accTypes, LogicalType[] windowPropertyTypes, LogicalType[] aggValueTypes, LogicalType[] inputFields, int timeFieldIndex, ZoneId shiftTimeZone, int inputCountIndex) {
    WindowOperatorBuilder builder = WindowOperatorBuilder.builder().withInputFields(inputFields).withShiftTimezone(shiftTimeZone).withInputCountIndex(inputCountIndex);
    if (window instanceof TumblingGroupWindow) {
        TumblingGroupWindow tumblingWindow = (TumblingGroupWindow) window;
        FieldReferenceExpression timeField = tumblingWindow.timeField();
        ValueLiteralExpression size = tumblingWindow.size();
        if (isProctimeAttribute(timeField) && hasTimeIntervalType(size)) {
            builder = builder.tumble(toDuration(size)).withProcessingTime();
        } else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
            builder = builder.tumble(toDuration(size)).withEventTime(timeFieldIndex);
        } else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
            builder = builder.countWindow(toLong(size));
        } else {
            // ProcessingTimeTumblingGroupWindow
            throw new UnsupportedOperationException("Event-time grouping windows on row intervals are currently not supported.");
        }
    } else if (window instanceof SlidingGroupWindow) {
        SlidingGroupWindow slidingWindow = (SlidingGroupWindow) window;
        FieldReferenceExpression timeField = slidingWindow.timeField();
        ValueLiteralExpression size = slidingWindow.size();
        ValueLiteralExpression slide = slidingWindow.slide();
        if (isProctimeAttribute(timeField) && hasTimeIntervalType(size)) {
            builder = builder.sliding(toDuration(size), toDuration(slide)).withProcessingTime();
        } else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
            builder = builder.sliding(toDuration(size), toDuration(slide)).withEventTime(timeFieldIndex);
        } else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
            builder = builder.countWindow(toLong(size), toLong(slide));
        } else {
            // ProcessingTimeTumblingGroupWindow
            throw new UnsupportedOperationException("Event-time grouping windows on row intervals are currently not supported.");
        }
    } else if (window instanceof SessionGroupWindow) {
        SessionGroupWindow sessionWindow = (SessionGroupWindow) window;
        FieldReferenceExpression timeField = sessionWindow.timeField();
        ValueLiteralExpression gap = sessionWindow.gap();
        if (isProctimeAttribute(timeField)) {
            builder = builder.session(toDuration(gap)).withProcessingTime();
        } else if (isRowtimeAttribute(timeField)) {
            builder = builder.session(toDuration(gap)).withEventTime(timeFieldIndex);
        } else {
            throw new UnsupportedOperationException("This should not happen.");
        }
    } else {
        throw new TableException("Unsupported window: " + window.toString());
    }
    WindowEmitStrategy emitStrategy = WindowEmitStrategy.apply(config, window);
    if (emitStrategy.produceUpdates()) {
        // mark this operator will send retraction and set new trigger
        builder.produceUpdates().triggering(emitStrategy.getTrigger()).withAllowedLateness(Duration.ofMillis(emitStrategy.getAllowLateness()));
    }
    if (aggsHandler instanceof GeneratedNamespaceAggsHandleFunction) {
        return builder.aggregate((GeneratedNamespaceAggsHandleFunction<?>) aggsHandler, recordEqualiser, accTypes, aggValueTypes, windowPropertyTypes).build();
    } else if (aggsHandler instanceof GeneratedNamespaceTableAggsHandleFunction) {
        return builder.aggregate((GeneratedNamespaceTableAggsHandleFunction<?>) aggsHandler, accTypes, aggValueTypes, windowPropertyTypes).build();
    } else {
        throw new TableException("Unsupported agg handler class: " + aggsHandler.getClass().getSimpleName());
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) WindowEmitStrategy(org.apache.flink.table.planner.plan.utils.WindowEmitStrategy) GeneratedNamespaceAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedNamespaceAggsHandleFunction) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) WindowOperatorBuilder(org.apache.flink.table.runtime.operators.window.WindowOperatorBuilder) GeneratedNamespaceTableAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedNamespaceTableAggsHandleFunction) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 7 with SessionGroupWindow

use of org.apache.flink.table.planner.plan.logical.SessionGroupWindow 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)

Example 8 with SessionGroupWindow

use of org.apache.flink.table.planner.plan.logical.SessionGroupWindow in project flink by apache.

the class StreamExecPythonGroupWindowAggregate method getGeneralPythonStreamGroupWindowAggregateFunctionOperator.

@SuppressWarnings({ "unchecked", "rawtypes" })
private OneInputStreamOperator<RowData, RowData> getGeneralPythonStreamGroupWindowAggregateFunctionOperator(Configuration config, RowType inputType, RowType outputType, WindowAssigner<?> windowAssigner, PythonAggregateFunctionInfo[] aggregateFunctions, DataViewSpec[][] dataViewSpecs, int inputTimeFieldIndex, int indexOfCountStar, boolean generateUpdateBefore, boolean countStarInserted, long allowance, ZoneId shiftTimeZone) {
    Class clazz = CommonPythonUtil.loadClass(GENERAL_STREAM_PYTHON_GROUP_WINDOW_AGGREGATE_FUNCTION_OPERATOR_NAME);
    boolean isRowTime = AggregateUtil.isRowtimeAttribute(window.timeAttribute());
    try {
        if (window instanceof TumblingGroupWindow) {
            ValueLiteralExpression size = ((TumblingGroupWindow) window).size();
            Method create = clazz.getMethod(GENERAL_STREAM_PYTHON_CREATE_TUMBLING_GROUP_WINDOW_METHOD, Configuration.class, RowType.class, RowType.class, PythonAggregateFunctionInfo[].class, DataViewSpec[][].class, int[].class, int.class, boolean.class, boolean.class, int.class, WindowAssigner.class, boolean.class, boolean.class, long.class, long.class, NamedWindowProperty[].class, ZoneId.class);
            return (OneInputStreamOperator<RowData, RowData>) create.invoke(null, config, inputType, outputType, aggregateFunctions, dataViewSpecs, grouping, indexOfCountStar, generateUpdateBefore, countStarInserted, inputTimeFieldIndex, windowAssigner, isRowTime, AggregateUtil.hasTimeIntervalType(size), AggregateUtil.toDuration(size).toMillis(), allowance, namedWindowProperties, shiftTimeZone);
        } else if (window instanceof SlidingGroupWindow) {
            ValueLiteralExpression size = ((SlidingGroupWindow) window).size();
            ValueLiteralExpression slide = ((SlidingGroupWindow) window).slide();
            Method create = clazz.getMethod(GENERAL_STREAM_PYTHON_CREATE_SLIDING_GROUP_WINDOW_METHOD, Configuration.class, RowType.class, RowType.class, PythonAggregateFunctionInfo[].class, DataViewSpec[][].class, int[].class, int.class, boolean.class, boolean.class, int.class, WindowAssigner.class, boolean.class, boolean.class, long.class, long.class, long.class, NamedWindowProperty[].class, ZoneId.class);
            return (OneInputStreamOperator<RowData, RowData>) create.invoke(null, config, inputType, outputType, aggregateFunctions, dataViewSpecs, grouping, indexOfCountStar, generateUpdateBefore, countStarInserted, inputTimeFieldIndex, windowAssigner, isRowTime, AggregateUtil.hasTimeIntervalType(size), AggregateUtil.toDuration(size).toMillis(), AggregateUtil.toDuration(slide).toMillis(), allowance, namedWindowProperties, shiftTimeZone);
        } else if (window instanceof SessionGroupWindow) {
            ValueLiteralExpression gap = ((SessionGroupWindow) window).gap();
            Method create = clazz.getMethod(GENERAL_STREAM_PYTHON_CREATE_SESSION_GROUP_WINDOW_METHOD, Configuration.class, RowType.class, RowType.class, PythonAggregateFunctionInfo[].class, DataViewSpec[][].class, int[].class, int.class, boolean.class, boolean.class, int.class, WindowAssigner.class, boolean.class, long.class, long.class, NamedWindowProperty[].class, ZoneId.class);
            return (OneInputStreamOperator<RowData, RowData>) create.invoke(null, config, inputType, outputType, aggregateFunctions, dataViewSpecs, grouping, indexOfCountStar, generateUpdateBefore, countStarInserted, inputTimeFieldIndex, windowAssigner, isRowTime, AggregateUtil.toDuration(gap).toMillis(), allowance, namedWindowProperties, shiftTimeZone);
        }
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new TableException("Python PythonStreamGroupWindowAggregateOperator constructed failed.", e);
    }
    throw new RuntimeException(String.format("Unsupported LogicWindow Type %s", window));
}
Also used : NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) Configuration(org.apache.flink.configuration.Configuration) DataViewSpec(org.apache.flink.table.runtime.dataview.DataViewSpec) ZoneId(java.time.ZoneId) RowType(org.apache.flink.table.types.logical.RowType) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SlidingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.SlidingWindowAssigner) CountTumblingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CountTumblingWindowAssigner) WindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.WindowAssigner) TumblingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner) CountSlidingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CountSlidingWindowAssigner) SessionWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.SessionWindowAssigner) RowData(org.apache.flink.table.data.RowData) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) PythonAggregateFunctionInfo(org.apache.flink.table.functions.python.PythonAggregateFunctionInfo) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Aggregations

TableException (org.apache.flink.table.api.TableException)8 SessionGroupWindow (org.apache.flink.table.planner.plan.logical.SessionGroupWindow)8 SlidingGroupWindow (org.apache.flink.table.planner.plan.logical.SlidingGroupWindow)7 TumblingGroupWindow (org.apache.flink.table.planner.plan.logical.TumblingGroupWindow)7 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)4 Duration (java.time.Duration)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 RelNode (org.apache.calcite.rel.RelNode)2 AggregateCall (org.apache.calcite.rel.core.AggregateCall)2 LogicalWindow (org.apache.flink.table.planner.plan.logical.LogicalWindow)2 FlinkLogicalWindowAggregate (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate)2 FlinkRelDistribution (org.apache.flink.table.planner.plan.trait.FlinkRelDistribution)2 WindowEmitStrategy (org.apache.flink.table.planner.plan.utils.WindowEmitStrategy)2 NamedWindowProperty (org.apache.flink.table.runtime.groupwindow.NamedWindowProperty)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ZoneId (java.time.ZoneId)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelCollation (org.apache.calcite.rel.RelCollation)1