Search in sources :

Example 1 with SessionGroupWindow

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

the class StreamExecGroupWindowAggregate method createAggsHandler.

private GeneratedClass<?> createAggsHandler(AggregateInfoList aggInfoList, ExecNodeConfig config, RelBuilder relBuilder, List<LogicalType> fieldTypes, ZoneId shiftTimeZone) {
    final boolean needMerge;
    final Class<?> windowClass;
    if (window instanceof SlidingGroupWindow) {
        ValueLiteralExpression size = ((SlidingGroupWindow) window).size();
        needMerge = hasTimeIntervalType(size);
        windowClass = hasRowIntervalType(size) ? CountWindow.class : TimeWindow.class;
    } else if (window instanceof TumblingGroupWindow) {
        needMerge = false;
        ValueLiteralExpression size = ((TumblingGroupWindow) window).size();
        windowClass = hasRowIntervalType(size) ? CountWindow.class : TimeWindow.class;
    } else if (window instanceof SessionGroupWindow) {
        needMerge = true;
        windowClass = TimeWindow.class;
    } else {
        throw new TableException("Unsupported window: " + window.toString());
    }
    final AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), relBuilder, JavaScalaConversionUtil.toScala(fieldTypes), // copyInputField
    false).needAccumulate();
    if (needMerge) {
        generator.needMerge(0, false, null);
    }
    if (needRetraction) {
        generator.needRetract();
    }
    final List<WindowProperty> windowProperties = Arrays.asList(Arrays.stream(namedWindowProperties).map(NamedWindowProperty::getProperty).toArray(WindowProperty[]::new));
    final boolean isTableAggregate = isTableAggregate(Arrays.asList(aggInfoList.getActualAggregateCalls()));
    if (isTableAggregate) {
        return generator.generateNamespaceTableAggsHandler("GroupingWindowTableAggsHandler", aggInfoList, JavaScalaConversionUtil.toScala(windowProperties), windowClass, shiftTimeZone);
    } else {
        return generator.generateNamespaceAggsHandler("GroupingWindowAggsHandler", aggInfoList, JavaScalaConversionUtil.toScala(windowProperties), windowClass, shiftTimeZone);
    }
}
Also used : NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) CountWindow(org.apache.flink.table.runtime.operators.window.CountWindow) TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) TimeWindow(org.apache.flink.table.runtime.operators.window.TimeWindow) NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) WindowProperty(org.apache.flink.table.runtime.groupwindow.WindowProperty) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 2 with SessionGroupWindow

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

the class StreamExecPythonGroupWindowAggregate method generateWindowAssignerAndTrigger.

private Tuple2<WindowAssigner<?>, Trigger<?>> generateWindowAssignerAndTrigger() {
    WindowAssigner<?> windowAssiger;
    Trigger<?> trigger;
    if (window instanceof TumblingGroupWindow) {
        TumblingGroupWindow tumblingWindow = (TumblingGroupWindow) window;
        FieldReferenceExpression timeField = tumblingWindow.timeField();
        ValueLiteralExpression size = tumblingWindow.size();
        if (isProctimeAttribute(timeField) && hasTimeIntervalType(size)) {
            windowAssiger = TumblingWindowAssigner.of(toDuration(size)).withProcessingTime();
            trigger = ProcessingTimeTriggers.afterEndOfWindow();
        } else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
            windowAssiger = TumblingWindowAssigner.of(toDuration(size)).withEventTime();
            trigger = EventTimeTriggers.afterEndOfWindow();
        } else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
            windowAssiger = CountTumblingWindowAssigner.of(toLong(size));
            trigger = ElementTriggers.count(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)) {
            windowAssiger = SlidingWindowAssigner.of(toDuration(size), toDuration(slide)).withProcessingTime();
            trigger = ProcessingTimeTriggers.afterEndOfWindow();
        } else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
            windowAssiger = SlidingWindowAssigner.of(toDuration(size), toDuration(slide));
            trigger = EventTimeTriggers.afterEndOfWindow();
        } else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
            windowAssiger = CountSlidingWindowAssigner.of(toLong(size), toLong(slide));
            trigger = ElementTriggers.count(toLong(size));
        } 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)) {
            windowAssiger = SessionWindowAssigner.withGap(toDuration(gap));
            trigger = ProcessingTimeTriggers.afterEndOfWindow();
        } else if (isRowtimeAttribute(timeField)) {
            windowAssiger = SessionWindowAssigner.withGap(toDuration(gap));
            trigger = EventTimeTriggers.afterEndOfWindow();
        } else {
            throw new UnsupportedOperationException("This should not happen.");
        }
    } else {
        throw new TableException("Unsupported window: " + window.toString());
    }
    return Tuple2.of(windowAssiger, trigger);
}
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) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 3 with SessionGroupWindow

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

the class StreamPhysicalPythonGroupWindowAggregateRule method convert.

@Override
public RelNode convert(RelNode rel) {
    FlinkLogicalWindowAggregate agg = (FlinkLogicalWindowAggregate) rel;
    LogicalWindow window = agg.getWindow();
    List<AggregateCall> aggCalls = agg.getAggCallList();
    boolean isPandasPythonUDAF = aggCalls.stream().anyMatch(x -> PythonUtil.isPythonAggregate(x, PythonFunctionKind.PANDAS));
    if (isPandasPythonUDAF && window instanceof SessionGroupWindow) {
        throw new TableException("Session Group Window is currently not supported for Pandas UDAF.");
    }
    RelNode input = agg.getInput();
    RelOptCluster cluster = rel.getCluster();
    FlinkRelDistribution requiredDistribution;
    if (agg.getGroupCount() != 0) {
        requiredDistribution = FlinkRelDistribution.hash(agg.getGroupSet().asList(), true);
    } else {
        requiredDistribution = FlinkRelDistribution.SINGLETON();
    }
    RelTraitSet requiredTraitSet = input.getTraitSet().replace(FlinkConventions.STREAM_PHYSICAL()).replace(requiredDistribution);
    RelTraitSet providedTraitSet = rel.getTraitSet().replace(FlinkConventions.STREAM_PHYSICAL());
    RelNode newInput = RelOptRule.convert(input, requiredTraitSet);
    ReadableConfig config = ShortcutUtils.unwrapTableConfig(rel);
    WindowEmitStrategy emitStrategy = WindowEmitStrategy.apply(config, agg.getWindow());
    if (emitStrategy.produceUpdates()) {
        throw new TableException("Python Group Window Aggregate Function is currently not supported for early fired or lately fired.");
    }
    return new StreamPhysicalPythonGroupWindowAggregate(cluster, providedTraitSet, newInput, rel.getRowType(), agg.getGroupSet().toArray(), JavaScalaConversionUtil.toScala(aggCalls), agg.getWindow(), agg.getNamedProperties(), emitStrategy);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) StreamPhysicalPythonGroupWindowAggregate(org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalPythonGroupWindowAggregate) TableException(org.apache.flink.table.api.TableException) RelTraitSet(org.apache.calcite.plan.RelTraitSet) AggregateCall(org.apache.calcite.rel.core.AggregateCall) FlinkRelDistribution(org.apache.flink.table.planner.plan.trait.FlinkRelDistribution) ReadableConfig(org.apache.flink.configuration.ReadableConfig) LogicalWindow(org.apache.flink.table.planner.plan.logical.LogicalWindow) RelNode(org.apache.calcite.rel.RelNode) WindowEmitStrategy(org.apache.flink.table.planner.plan.utils.WindowEmitStrategy) FlinkLogicalWindowAggregate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalWindowAggregate) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 4 with SessionGroupWindow

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

the class LogicalWindowJsonDeserializer method deserialize.

@Override
public LogicalWindow deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    JsonNode jsonNode = jsonParser.readValueAsTree();
    String kind = jsonNode.get(FIELD_NAME_KIND).asText().toUpperCase();
    WindowReference alias = deserializationContext.readValue(jsonNode.get(FIELD_NAME_ALIAS).traverse(jsonParser.getCodec()), WindowReference.class);
    FieldReferenceExpression timeField = deserializeFieldReferenceExpression(jsonNode.get(FIELD_NAME_TIME_FIELD), jsonParser, deserializationContext);
    switch(kind) {
        case KIND_TUMBLING:
            boolean isTimeTumblingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
            if (isTimeTumblingWindow) {
                Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
                return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
            } else {
                long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
                return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
            }
        case KIND_SLIDING:
            boolean isTimeSlidingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
            if (isTimeSlidingWindow) {
                Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
                Duration slide = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SLIDE), jsonParser.getCodec()), Duration.class);
                return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
            } else {
                long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
                long slide = jsonNode.get(FIELD_NAME_SLIDE).asLong();
                return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
            }
        case KIND_SESSION:
            Duration gap = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_GAP), jsonParser.getCodec()), Duration.class);
            return new SessionGroupWindow(alias, timeField, new ValueLiteralExpression(gap));
        default:
            throw new TableException("Unknown Logical Window:" + jsonNode);
    }
}
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) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) Duration(java.time.Duration) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow) WindowReference(org.apache.flink.table.runtime.groupwindow.WindowReference)

Example 5 with SessionGroupWindow

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

the class LogicalWindowJsonSerializer method serialize.

@Override
public void serialize(LogicalWindow logicalWindow, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
    gen.writeStartObject();
    if (logicalWindow instanceof TumblingGroupWindow) {
        TumblingGroupWindow window = (TumblingGroupWindow) logicalWindow;
        gen.writeStringField(FIELD_NAME_KIND, KIND_TUMBLING);
        serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
        FieldReferenceExpression timeField = logicalWindow.timeAttribute();
        serializeFieldReferenceExpression(timeField, gen, serializerProvider);
        ValueLiteralExpression size = window.size();
        if (hasTimeIntervalType(size)) {
            Duration duration = toDuration(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, true);
            gen.writeObjectField(FIELD_NAME_SIZE, duration);
        } else {
            Long duration = toLong(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, false);
            gen.writeNumberField(FIELD_NAME_SIZE, duration);
        }
    } else if (logicalWindow instanceof SlidingGroupWindow) {
        SlidingGroupWindow window = (SlidingGroupWindow) logicalWindow;
        gen.writeStringField(FIELD_NAME_KIND, KIND_SLIDING);
        serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
        serializeFieldReferenceExpression(window.timeAttribute(), gen, serializerProvider);
        ValueLiteralExpression size = window.size();
        if (hasTimeIntervalType(size)) {
            Duration duration = toDuration(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, true);
            gen.writeObjectField(FIELD_NAME_SIZE, duration);
            gen.writeObjectField(FIELD_NAME_SLIDE, toDuration(window.slide()));
        } else {
            Long duration = toLong(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, false);
            gen.writeObjectField(FIELD_NAME_SIZE, duration);
            gen.writeObjectField(FIELD_NAME_SLIDE, toLong(window.slide()));
        }
    } else if (logicalWindow instanceof SessionGroupWindow) {
        gen.writeStringField(FIELD_NAME_KIND, KIND_SESSION);
        SessionGroupWindow window = (SessionGroupWindow) logicalWindow;
        serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
        serializeFieldReferenceExpression(window.timeAttribute(), gen, serializerProvider);
        gen.writeObjectField(FIELD_NAME_GAP, toDuration(window.gap()));
    } else {
        throw new TableException("Unknown LogicalWindow: " + logicalWindow);
    }
    gen.writeEndObject();
}
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) AggregateUtil.toLong(org.apache.flink.table.planner.plan.utils.AggregateUtil.toLong) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) Duration(java.time.Duration) AggregateUtil.toDuration(org.apache.flink.table.planner.plan.utils.AggregateUtil.toDuration) 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