Search in sources :

Example 26 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTableIQv2Test method shouldReturnValuesForClosedEndBounds.

@Test
public void shouldReturnValuesForClosedEndBounds() {
    // Given:
    final Range<Instant> end = Range.closed(NOW, NOW.plusSeconds(10));
    final Range<Instant> startEqiv = Range.closed(end.lowerEndpoint().minus(WINDOW_SIZE), end.lowerEndpoint().minus(WINDOW_SIZE));
    final StateQueryResult partitionResult = new StateQueryResult();
    final QueryResult queryResult = QueryResult.forResult(fetchIterator);
    queryResult.setPosition(POSITION);
    partitionResult.addResult(PARTITION, queryResult);
    when(kafkaStreams.query(any())).thenReturn(partitionResult);
    when(fetchIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(fetchIterator.next()).thenReturn(new KeyValue<>(startEqiv.lowerEndpoint().toEpochMilli(), VALUE_1)).thenReturn(new KeyValue<>(startEqiv.upperEndpoint().toEpochMilli(), VALUE_2)).thenThrow(new AssertionError());
    // When:
    final KsMaterializedQueryResult<WindowedRow> result = table.get(A_KEY, PARTITION, Range.all(), end);
    // Then:
    final Iterator<WindowedRow> rowIterator = result.getRowIterator();
    assertThat(rowIterator.hasNext(), is(true));
    final List<WindowedRow> resultList = Lists.newArrayList(rowIterator);
    assertThat(resultList, contains(WindowedRow.of(SCHEMA, windowedKey(startEqiv.lowerEndpoint()), VALUE_1.value(), VALUE_1.timestamp()), WindowedRow.of(SCHEMA, windowedKey(startEqiv.upperEndpoint()), VALUE_2.value(), VALUE_2.timestamp())));
    assertThat(result.getPosition(), not(Optional.empty()));
    assertThat(result.getPosition().get(), is(POSITION));
}
Also used : StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) QueryResult(org.apache.kafka.streams.query.QueryResult) KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Example 27 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTableIQv2Test method shouldReturnValuesForOpenEndBounds_fetchAll.

@Test
public void shouldReturnValuesForOpenEndBounds_fetchAll() {
    // Given:
    final Range<Instant> end = Range.open(NOW, NOW.plusSeconds(10));
    final Range<Instant> startEquiv = Range.open(end.lowerEndpoint().minus(WINDOW_SIZE), end.upperEndpoint().minus(WINDOW_SIZE));
    final StateQueryResult partitionResult = new StateQueryResult();
    final QueryResult queryResult = QueryResult.forResult(keyValueIterator);
    queryResult.setPosition(POSITION);
    partitionResult.addResult(PARTITION, queryResult);
    when(kafkaStreams.query(any())).thenReturn(partitionResult);
    when(keyValueIterator.hasNext()).thenReturn(true, true, true, false);
    when(keyValueIterator.next()).thenReturn(new KeyValue<>(new Windowed<>(A_KEY, new TimeWindow(startEquiv.lowerEndpoint().toEpochMilli(), startEquiv.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_1)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY2, new TimeWindow(startEquiv.lowerEndpoint().plusMillis(1).toEpochMilli(), startEquiv.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis() + 1)), VALUE_2)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY3, new TimeWindow(startEquiv.upperEndpoint().toEpochMilli(), startEquiv.upperEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_3)).thenThrow(new AssertionError());
    // When:
    final KsMaterializedQueryResult<WindowedRow> result = table.get(PARTITION, Range.all(), end);
    // Then:
    final Iterator<WindowedRow> rowIterator = result.getRowIterator();
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(A_KEY2, startEquiv.lowerEndpoint().plusMillis(1)), VALUE_2.value(), VALUE_2.timestamp())));
    assertThat(rowIterator.hasNext(), is(false));
    assertThat(result.getPosition(), not(Optional.empty()));
    assertThat(result.getPosition().get(), is(POSITION));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Windowed(org.apache.kafka.streams.kstream.Windowed) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) QueryResult(org.apache.kafka.streams.query.QueryResult) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Example 28 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTableIQv2Test method shouldReturnValuesForClosedStartBounds.

@Test
public void shouldReturnValuesForClosedStartBounds() {
    // Given:
    final Range<Instant> start = Range.closed(Instant.ofEpochMilli(System.currentTimeMillis()), NOW.plusSeconds(10));
    final StateQueryResult partitionResult = new StateQueryResult();
    final QueryResult result = QueryResult.forResult(fetchIterator);
    result.setPosition(POSITION);
    partitionResult.addResult(PARTITION, result);
    when(kafkaStreams.query(any())).thenReturn(partitionResult);
    when(fetchIterator.hasNext()).thenReturn(true, true, false);
    when(fetchIterator.next()).thenReturn(new KeyValue<>(start.lowerEndpoint().toEpochMilli(), VALUE_1)).thenReturn(new KeyValue<>(start.upperEndpoint().toEpochMilli(), VALUE_2)).thenThrow(new AssertionError());
    // When:
    final Iterator<WindowedRow> rowIterator = table.get(A_KEY, PARTITION, start, Range.all()).rowIterator;
    // Then:
    assertThat(rowIterator.hasNext(), is(true));
    final List<WindowedRow> resultList = Lists.newArrayList(rowIterator);
    assertThat(resultList, contains(WindowedRow.of(SCHEMA, windowedKey(start.lowerEndpoint()), VALUE_1.value(), VALUE_1.timestamp()), WindowedRow.of(SCHEMA, windowedKey(start.upperEndpoint()), VALUE_2.value(), VALUE_2.timestamp())));
}
Also used : StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) QueryResult(org.apache.kafka.streams.query.QueryResult) KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Example 29 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedSessionTableIQv2Test method shouldReturnValueIfSessionStartsAtUpperBoundIfUpperBoundClosed.

@Test
public void shouldReturnValueIfSessionStartsAtUpperBoundIfUpperBoundClosed() {
    // Given:
    final Range<Instant> startBounds = Range.closed(LOWER_INSTANT, UPPER_INSTANT);
    final Instant wend = UPPER_INSTANT.plusMillis(1);
    givenSingleSession(UPPER_INSTANT, wend);
    // When:
    final KsMaterializedQueryResult<WindowedRow> result = table.get(A_KEY, PARTITION, startBounds, Range.all());
    // Then:
    final Iterator<WindowedRow> rowIterator = result.getRowIterator();
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, sessionKey(UPPER_INSTANT, wend), A_VALUE, wend.toEpochMilli())));
    assertThat(result.getPosition(), not(Optional.empty()));
    assertThat(result.getPosition().get(), is(POSITION));
}
Also used : Instant(java.time.Instant) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Example 30 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedSessionTableIQv2Test method shouldReturnValueIfSessionStartsBetweenBounds.

@Test
public void shouldReturnValueIfSessionStartsBetweenBounds() {
    // Given:
    final Instant wend = UPPER_INSTANT.plusMillis(5);
    givenSingleSession(LOWER_INSTANT.plusMillis(1), wend);
    // When:
    final KsMaterializedQueryResult<WindowedRow> result = table.get(A_KEY, PARTITION, WINDOW_START_BOUNDS, Range.all());
    // Then:
    final Iterator<WindowedRow> rowIterator = result.getRowIterator();
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, sessionKey(LOWER_INSTANT.plusMillis(1), wend), A_VALUE, wend.toEpochMilli())));
    assertThat(result.getPosition(), not(Optional.empty()));
    assertThat(result.getPosition().get(), is(POSITION));
}
Also used : Instant(java.time.Instant) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Aggregations

WindowedRow (io.confluent.ksql.execution.streams.materialization.WindowedRow)42 Test (org.junit.Test)33 Instant (java.time.Instant)32 KeyValue (org.apache.kafka.streams.KeyValue)16 Windowed (org.apache.kafka.streams.kstream.Windowed)15 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)12 QueryResult (org.apache.kafka.streams.query.QueryResult)12 StateQueryResult (org.apache.kafka.streams.query.StateQueryResult)12 GenericKey (io.confluent.ksql.GenericKey)9 GenericRow (io.confluent.ksql.GenericRow)8 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)5 MaterializedWindowedTable (io.confluent.ksql.execution.streams.materialization.MaterializedWindowedTable)5 ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)4 Window (io.confluent.ksql.Window)3 Materialization (io.confluent.ksql.execution.streams.materialization.Materialization)3 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)3 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)3 Optional (java.util.Optional)3 KeyValueIterator (org.apache.kafka.streams.state.KeyValueIterator)3 WindowStoreIterator (org.apache.kafka.streams.state.WindowStoreIterator)3