Search in sources :

Example 16 with WindowedRow

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

the class KsMaterializedWindowTableTest 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));
    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 Iterator<WindowedRow> rowIterator = table.get(PARTITION, Range.all(), end).rowIterator;
    // Then:
    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));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Test(org.junit.Test)

Example 17 with WindowedRow

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

the class KsMaterializedWindowTableTest method shouldReturnValuesForOpenStartBounds_fetchAll.

@Test
public void shouldReturnValuesForOpenStartBounds_fetchAll() {
    // Given:
    final Range<Instant> start = Range.open(NOW, NOW.plusSeconds(10));
    when(keyValueIterator.hasNext()).thenReturn(true, true, true, false);
    when(keyValueIterator.next()).thenReturn(new KeyValue<>(new Windowed<>(A_KEY, new TimeWindow(start.lowerEndpoint().toEpochMilli(), start.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_1)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY2, new TimeWindow(start.lowerEndpoint().plusMillis(1).toEpochMilli(), start.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis() + 1)), VALUE_2)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY3, new TimeWindow(start.upperEndpoint().toEpochMilli(), start.upperEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_3)).thenThrow(new AssertionError());
    // When:
    final Iterator<WindowedRow> rowIterator = table.get(PARTITION, start, Range.all()).rowIterator;
    // Then:
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(A_KEY2, start.lowerEndpoint().plusMillis(1)), VALUE_2.value(), VALUE_2.timestamp())));
    assertThat(rowIterator.hasNext(), is(false));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Test(org.junit.Test)

Example 18 with WindowedRow

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

the class KsMaterializedWindowTableIQv2Test method shouldReturnEmptyIfKeyNotPresent_fetchAll.

@Test
public void shouldReturnEmptyIfKeyNotPresent_fetchAll() {
    // When:
    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(false);
    final Iterator<WindowedRow> rowIterator = table.get(PARTITION, WINDOW_START_BOUNDS, WINDOW_END_BOUNDS).rowIterator;
    // Then:
    assertThat(rowIterator.hasNext(), is(false));
}
Also used : 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 19 with WindowedRow

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

the class KsMaterializedWindowTableIQv2Test method shouldReturnEmptyIfKeyNotPresent.

@Test
public void shouldReturnEmptyIfKeyNotPresent() {
    // When:
    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(false);
    final Iterator<WindowedRow> rowIterator = table.get(A_KEY, PARTITION, WINDOW_START_BOUNDS, WINDOW_END_BOUNDS).rowIterator;
    // Then:
    assertThat(rowIterator.hasNext(), is(false));
}
Also used : 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 20 with WindowedRow

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

the class KsMaterializedWindowTableIQv2Test method shouldReturnValuesForOpenStartBounds_fetchAll.

@Test
public void shouldReturnValuesForOpenStartBounds_fetchAll() {
    // Given:
    final Range<Instant> start = Range.open(NOW, NOW.plusSeconds(10));
    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(start.lowerEndpoint().toEpochMilli(), start.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_1)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY2, new TimeWindow(start.lowerEndpoint().plusMillis(1).toEpochMilli(), start.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis() + 1)), VALUE_2)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY3, new TimeWindow(start.upperEndpoint().toEpochMilli(), start.upperEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_3)).thenThrow(new AssertionError());
    // When:
    final KsMaterializedQueryResult<WindowedRow> result = table.get(PARTITION, start, Range.all());
    // Then:
    final Iterator<WindowedRow> rowIterator = result.getRowIterator();
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(A_KEY2, start.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)

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