Search in sources :

Example 6 with TimeWindow

use of org.apache.flink.table.runtime.operators.window.TimeWindow in project flink by apache.

the class SessionWindowAssigner method mergeWindows.

@Override
public void mergeWindows(TimeWindow newWindow, NavigableSet<TimeWindow> sortedWindows, MergeCallback<TimeWindow> callback) {
    TimeWindow ceiling = sortedWindows.ceiling(newWindow);
    TimeWindow floor = sortedWindows.floor(newWindow);
    Collection<TimeWindow> mergedWindows = new HashSet<>();
    TimeWindow mergeResult = newWindow;
    if (ceiling != null) {
        mergeResult = mergeWindow(mergeResult, ceiling, mergedWindows);
    }
    if (floor != null) {
        mergeResult = mergeWindow(mergeResult, floor, mergedWindows);
    }
    if (!mergedWindows.isEmpty()) {
        // merge happens, add newWindow into the collection as well.
        mergedWindows.add(newWindow);
        callback.merge(mergeResult, mergedWindows);
    }
}
Also used : TimeWindow(org.apache.flink.table.runtime.operators.window.TimeWindow) HashSet(java.util.HashSet)

Example 7 with TimeWindow

use of org.apache.flink.table.runtime.operators.window.TimeWindow in project flink by apache.

the class SessionWindowAssignerTest method testMergeCoveringWindow.

@SuppressWarnings("unchecked")
@Test
public void testMergeCoveringWindow() {
    MergingWindowAssigner.MergeCallback callback = mock(MergingWindowAssigner.MergeCallback.class);
    SessionWindowAssigner assigner = SessionWindowAssigner.withGap(Duration.ofMillis(5000));
    TreeSet<TimeWindow> sortedWindows = new TreeSet<>();
    sortedWindows.addAll(Arrays.asList(new TimeWindow(1, 4), new TimeWindow(5, 7), new TimeWindow(9, 10)));
    assigner.mergeWindows(new TimeWindow(3, 6), sortedWindows, callback);
    verify(callback, times(1)).merge(eq(new TimeWindow(1, 7)), argThat((ArgumentMatcher<Collection<TimeWindow>>) timeWindows -> containsInAnyOrder(new TimeWindow(1, 4), new TimeWindow(5, 7), new TimeWindow(3, 6)).matches(timeWindows)));
}
Also used : TreeSet(java.util.TreeSet) ArgumentMatcher(org.mockito.ArgumentMatcher) TimeWindow(org.apache.flink.table.runtime.operators.window.TimeWindow) Test(org.junit.Test)

Example 8 with TimeWindow

use of org.apache.flink.table.runtime.operators.window.TimeWindow in project flink by apache.

the class SessionWindowAssignerTest method testMergeConsecutiveWindows.

@SuppressWarnings("unchecked")
@Test
public void testMergeConsecutiveWindows() {
    MergingWindowAssigner.MergeCallback callback = mock(MergingWindowAssigner.MergeCallback.class);
    SessionWindowAssigner assigner = SessionWindowAssigner.withGap(Duration.ofMillis(5000));
    TreeSet<TimeWindow> sortedWindows = new TreeSet<>();
    sortedWindows.addAll(Arrays.asList(new TimeWindow(0, 1), new TimeWindow(2, 3), new TimeWindow(4, 5), new TimeWindow(7, 8)));
    assigner.mergeWindows(new TimeWindow(1, 2), sortedWindows, callback);
    verify(callback, times(1)).merge(eq(new TimeWindow(0, 3)), argThat((ArgumentMatcher<Collection<TimeWindow>>) timeWindows -> containsInAnyOrder(new TimeWindow(0, 1), new TimeWindow(1, 2), new TimeWindow(2, 3)).matches(timeWindows)));
}
Also used : TreeSet(java.util.TreeSet) ArgumentMatcher(org.mockito.ArgumentMatcher) TimeWindow(org.apache.flink.table.runtime.operators.window.TimeWindow) Test(org.junit.Test)

Example 9 with TimeWindow

use of org.apache.flink.table.runtime.operators.window.TimeWindow in project flink by apache.

the class HeapWindowsGroupingTest method testInvalidWindowTrigger.

@Test(expected = IllegalStateException.class)
public void testInvalidWindowTrigger() throws IOException {
    Long[] ts = new Long[] { 8L };
    RowIterator<BinaryRowData> iterator = new HeapWindowsGroupingTest.TestInputIterator(ts);
    HeapWindowsGrouping grouping = new HeapWindowsGrouping(5000, 0L, 8L, 4L, 0, false);
    grouping.addInputToBuffer(iterator.getRow());
    System.out.println("valid window trigger");
    RowIterator<BinaryRowData> iter = grouping.buildTriggerWindowElementsIterator();
    TimeWindow window = grouping.getTriggerWindow();
    List<Long> buffer = new ArrayList<>();
    while (iter.advanceNext()) {
        buffer.add(iter.getRow().getLong(0));
    }
    assertEquals(TimeWindow.of(0, 8L), window);
    assertEquals(Collections.emptyList(), buffer);
    System.out.println("try invalid window trigger");
    grouping.buildTriggerWindowElementsIterator();
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) ArrayList(java.util.ArrayList) TimeWindow(org.apache.flink.table.runtime.operators.window.TimeWindow) Test(org.junit.Test)

Example 10 with TimeWindow

use of org.apache.flink.table.runtime.operators.window.TimeWindow in project flink by apache.

the class HeapWindowsGroupingTest method verify.

private void verify(int limit, Long[] ts, long windowSize, long slideSize, List<List<Long>> expected, List<TimeWindow> expectedWindows) throws IOException {
    List<List<Long>> actual = new ArrayList<>();
    List<TimeWindow> windows = new ArrayList<>();
    HeapWindowsGrouping grouping = new HeapWindowsGrouping(limit, 0L, windowSize, slideSize, 0, false);
    RowIterator<BinaryRowData> iterator = new HeapWindowsGroupingTest.TestInputIterator(ts);
    while (iterator.advanceNext()) {
        BinaryRowData input = iterator.getRow();
        grouping.addInputToBuffer(input);
        processTriggerWindow(actual, windows, grouping);
    }
    grouping.advanceWatermarkToTriggerAllWindows();
    processTriggerWindow(actual, windows, grouping);
    assertEquals(expectedWindows, windows);
    assertEquals(expected, actual);
}
Also used : ArrayList(java.util.ArrayList) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) TimeWindow(org.apache.flink.table.runtime.operators.window.TimeWindow)

Aggregations

TimeWindow (org.apache.flink.table.runtime.operators.window.TimeWindow)17 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)6 GenericRowData (org.apache.flink.table.data.GenericRowData)3 RowData (org.apache.flink.table.data.RowData)3 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)3 List (java.util.List)2 TreeSet (java.util.TreeSet)2 UpdatableRowData (org.apache.flink.table.data.UpdatableRowData)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 IOException (java.io.IOException)1 ZoneId (java.time.ZoneId)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Transformation (org.apache.flink.api.dag.Transformation)1 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)1 DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)1