Search in sources :

Example 1 with TumblingWindowSpec

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

the class WindowTableFunctionUtil method createWindowAssigner.

/**
 * Creates window assigner based on input window strategy.
 *
 * @param windowingStrategy input window strategy
 * @return new created window assigner
 */
public static WindowAssigner<TimeWindow> createWindowAssigner(TimeAttributeWindowingStrategy windowingStrategy) {
    WindowSpec windowSpec = windowingStrategy.getWindow();
    boolean isProctime = windowingStrategy.isProctime();
    if (windowSpec instanceof TumblingWindowSpec) {
        TumblingWindowSpec tumblingWindowSpec = (TumblingWindowSpec) windowSpec;
        TumblingWindowAssigner windowAssigner = TumblingWindowAssigner.of(tumblingWindowSpec.getSize());
        if (isProctime) {
            windowAssigner = windowAssigner.withProcessingTime();
        }
        if (tumblingWindowSpec.getOffset() != null) {
            windowAssigner = windowAssigner.withOffset(tumblingWindowSpec.getOffset());
        }
        return windowAssigner;
    } else if (windowSpec instanceof HoppingWindowSpec) {
        HoppingWindowSpec hoppingWindowSpec = (HoppingWindowSpec) windowSpec;
        SlidingWindowAssigner windowAssigner = SlidingWindowAssigner.of(hoppingWindowSpec.getSize(), hoppingWindowSpec.getSlide());
        if (isProctime) {
            windowAssigner = windowAssigner.withProcessingTime();
        }
        if (hoppingWindowSpec.getOffset() != null) {
            windowAssigner = windowAssigner.withOffset(hoppingWindowSpec.getOffset());
        }
        return windowAssigner;
    } else if (windowSpec instanceof CumulativeWindowSpec) {
        CumulativeWindowSpec cumulativeWindowSpec = (CumulativeWindowSpec) windowSpec;
        CumulativeWindowAssigner windowAssigner = CumulativeWindowAssigner.of(cumulativeWindowSpec.getMaxSize(), cumulativeWindowSpec.getStep());
        if (isProctime) {
            windowAssigner = windowAssigner.withProcessingTime();
        }
        if (cumulativeWindowSpec.getOffset() != null) {
            windowAssigner = windowAssigner.withOffset(cumulativeWindowSpec.getOffset());
        }
        return windowAssigner;
    } else {
        throw new TableException(String.format("Unknown window spec: %s", windowSpec.getClass().getSimpleName()));
    }
}
Also used : SlidingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.SlidingWindowAssigner) TableException(org.apache.flink.table.api.TableException) CumulativeWindowSpec(org.apache.flink.table.planner.plan.logical.CumulativeWindowSpec) CumulativeWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner) TumblingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner) TumblingWindowSpec(org.apache.flink.table.planner.plan.logical.TumblingWindowSpec) WindowSpec(org.apache.flink.table.planner.plan.logical.WindowSpec) HoppingWindowSpec(org.apache.flink.table.planner.plan.logical.HoppingWindowSpec) CumulativeWindowSpec(org.apache.flink.table.planner.plan.logical.CumulativeWindowSpec) HoppingWindowSpec(org.apache.flink.table.planner.plan.logical.HoppingWindowSpec) TumblingWindowSpec(org.apache.flink.table.planner.plan.logical.TumblingWindowSpec)

Example 2 with TumblingWindowSpec

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

the class StreamExecWindowAggregateBase method createSliceAssigner.

protected SliceAssigner createSliceAssigner(WindowSpec windowSpec, int timeAttributeIndex, ZoneId shiftTimeZone) {
    if (windowSpec instanceof TumblingWindowSpec) {
        Duration size = ((TumblingWindowSpec) windowSpec).getSize();
        SliceAssigners.TumblingSliceAssigner assigner = SliceAssigners.tumbling(timeAttributeIndex, shiftTimeZone, size);
        Duration offset = ((TumblingWindowSpec) windowSpec).getOffset();
        if (offset != null) {
            assigner = assigner.withOffset(offset);
        }
        return assigner;
    } else if (windowSpec instanceof HoppingWindowSpec) {
        Duration size = ((HoppingWindowSpec) windowSpec).getSize();
        Duration slide = ((HoppingWindowSpec) windowSpec).getSlide();
        if (size.toMillis() % slide.toMillis() != 0) {
            throw new TableException(String.format("HOP table function based aggregate requires size must be an " + "integral multiple of slide, but got size %s ms and slide %s ms", size.toMillis(), slide.toMillis()));
        }
        SliceAssigners.HoppingSliceAssigner assigner = SliceAssigners.hopping(timeAttributeIndex, shiftTimeZone, size, slide);
        Duration offset = ((HoppingWindowSpec) windowSpec).getOffset();
        if (offset != null) {
            assigner = assigner.withOffset(offset);
        }
        return assigner;
    } else if (windowSpec instanceof CumulativeWindowSpec) {
        Duration maxSize = ((CumulativeWindowSpec) windowSpec).getMaxSize();
        Duration step = ((CumulativeWindowSpec) windowSpec).getStep();
        if (maxSize.toMillis() % step.toMillis() != 0) {
            throw new TableException(String.format("CUMULATE table function based aggregate requires maxSize must be an " + "integral multiple of step, but got maxSize %s ms and step %s ms", maxSize.toMillis(), step.toMillis()));
        }
        SliceAssigners.CumulativeSliceAssigner assigner = SliceAssigners.cumulative(timeAttributeIndex, shiftTimeZone, maxSize, step);
        Duration offset = ((CumulativeWindowSpec) windowSpec).getOffset();
        if (offset != null) {
            assigner = assigner.withOffset(offset);
        }
        return assigner;
    } else {
        throw new UnsupportedOperationException(windowSpec + " is not supported yet.");
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) SliceAssigners(org.apache.flink.table.runtime.operators.window.slicing.SliceAssigners) Duration(java.time.Duration) CumulativeWindowSpec(org.apache.flink.table.planner.plan.logical.CumulativeWindowSpec) HoppingWindowSpec(org.apache.flink.table.planner.plan.logical.HoppingWindowSpec) TumblingWindowSpec(org.apache.flink.table.planner.plan.logical.TumblingWindowSpec)

Aggregations

TableException (org.apache.flink.table.api.TableException)2 CumulativeWindowSpec (org.apache.flink.table.planner.plan.logical.CumulativeWindowSpec)2 HoppingWindowSpec (org.apache.flink.table.planner.plan.logical.HoppingWindowSpec)2 TumblingWindowSpec (org.apache.flink.table.planner.plan.logical.TumblingWindowSpec)2 Duration (java.time.Duration)1 WindowSpec (org.apache.flink.table.planner.plan.logical.WindowSpec)1 CumulativeWindowAssigner (org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner)1 SlidingWindowAssigner (org.apache.flink.table.runtime.operators.window.assigners.SlidingWindowAssigner)1 TumblingWindowAssigner (org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner)1 SliceAssigners (org.apache.flink.table.runtime.operators.window.slicing.SliceAssigners)1