Search in sources :

Example 1 with GeneratedNamespaceTableAggsHandleFunction

use of org.apache.flink.table.runtime.generated.GeneratedNamespaceTableAggsHandleFunction 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)

Aggregations

TableException (org.apache.flink.table.api.TableException)1 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)1 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)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 WindowEmitStrategy (org.apache.flink.table.planner.plan.utils.WindowEmitStrategy)1 GeneratedNamespaceAggsHandleFunction (org.apache.flink.table.runtime.generated.GeneratedNamespaceAggsHandleFunction)1 GeneratedNamespaceTableAggsHandleFunction (org.apache.flink.table.runtime.generated.GeneratedNamespaceTableAggsHandleFunction)1 WindowOperatorBuilder (org.apache.flink.table.runtime.operators.window.WindowOperatorBuilder)1