Search in sources :

Example 6 with Row

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

the class KsMaterializationFunctionalTest method shouldQueryMaterializedTableForAggregatedTable.

@Test
public void shouldQueryMaterializedTableForAggregatedTable() {
    // Given:
    final PersistentQueryMetadata query = executeQuery("CREATE TABLE " + output + " AS" + " SELECT USERID, COUNT(*) FROM " + USER_TABLE + " GROUP BY USERID;");
    final LogicalSchema schema = schema("KSQL_COL_0", SqlTypes.BIGINT);
    final Map<String, GenericRow> rows = waitForUniqueUserRows(STRING_DESERIALIZER, schema);
    // When:
    final Materialization materialization = query.getMaterialization(queryId, contextStacker).get();
    // Then:
    assertThat(materialization.windowType(), is(Optional.empty()));
    final MaterializedTable table = materialization.nonWindowed();
    rows.forEach((rowKey, value) -> {
        final GenericKey key = genericKey(rowKey);
        final Iterator<Row> rowIterator = withRetry(() -> table.get(key, PARTITION));
        assertThat(rowIterator.hasNext(), is(true));
        final Row row = rowIterator.next();
        assertThat(row.schema(), is(schema));
        assertThat(row.key(), is(key));
        assertThat(row.value(), is(value));
    });
    final GenericKey key = genericKey("Won't find me");
    assertThat("unknown key", withRetry(() -> table.get(key, PARTITION).hasNext()), is(false));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) Materialization(io.confluent.ksql.execution.streams.materialization.Materialization) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) GenericKey(io.confluent.ksql.GenericKey) Row(io.confluent.ksql.execution.streams.materialization.Row) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) GenericRow(io.confluent.ksql.GenericRow) MaterializedTable(io.confluent.ksql.execution.streams.materialization.MaterializedTable) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 7 with Row

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

the class KsMaterializationFunctionalTest method shouldHandleHavingClause.

@Test
public void shouldHandleHavingClause() {
    // Note: HAVING clause are handled centrally by KsqlMaterialization. This logic will have been
    // installed as part of building the below statement:
    // Given:
    final PersistentQueryMetadata query = executeQuery("CREATE TABLE " + output + " AS" + " SELECT USERID, COUNT(*) AS COUNT FROM " + USER_TABLE + " GROUP BY USERID" + " HAVING SUM(REGISTERTIME) > 2;");
    final LogicalSchema schema = schema("COUNT", SqlTypes.BIGINT);
    final int matches = (int) USER_DATA_PROVIDER.data().values().stream().filter(row -> ((Long) row.get(0)) > 2).count();
    final Map<String, GenericRow> rows = waitForUniqueUserRows(matches, STRING_DESERIALIZER, schema);
    // When:
    final Materialization materialization = query.getMaterialization(queryId, contextStacker).get();
    // Then:
    final MaterializedTable table = materialization.nonWindowed();
    rows.forEach((rowKey, value) -> {
        // Rows passing the HAVING clause:
        final GenericKey key = genericKey(rowKey);
        final List<Row> rowList = withRetry(() -> Lists.newArrayList(table.get(key, PARTITION)));
        assertThat(rowList.size(), is(1));
        assertThat(rowList.get(0).schema(), is(schema));
        assertThat(rowList.get(0).key(), is(key));
        assertThat(rowList.get(0).value(), is(value));
    });
    USER_DATA_PROVIDER.data().entries().stream().filter(e -> !rows.containsKey(e.getKey().get(0))).forEach(e -> {
        // Rows filtered by the HAVING clause:
        final List<Row> rowList = withRetry(() -> Lists.newArrayList(table.get(e.getKey(), PARTITION)));
        assertThat(rowList.isEmpty(), is(true));
    });
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) ColumnName(io.confluent.ksql.name.ColumnName) Row(io.confluent.ksql.execution.streams.materialization.Row) AssertEventually.assertThatEventually(io.confluent.ksql.test.util.AssertEventually.assertThatEventually) WindowedSerdes(org.apache.kafka.streams.kstream.WindowedSerdes) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Duration(java.time.Duration) Map(java.util.Map) WindowType(io.confluent.ksql.model.WindowType) QueryId(io.confluent.ksql.query.QueryId) RetryOnException(io.confluent.ksql.test.util.AssertEventually.RetryOnException) ClassRule(org.junit.ClassRule) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) ZooKeeperClientException(kafka.zookeeper.ZooKeeperClientException) QueryMetadata(io.confluent.ksql.util.QueryMetadata) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Range(com.google.common.collect.Range) Set(java.util.Set) Window(io.confluent.ksql.Window) Instant(java.time.Instant) Category(org.junit.experimental.categories.Category) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) GenericKey.genericKey(io.confluent.ksql.GenericKey.genericKey) Stream(java.util.stream.Stream) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) UserDataProvider(io.confluent.ksql.util.UserDataProvider) Retry(io.confluent.ksql.integration.Retry) StreamsConfig(org.apache.kafka.streams.StreamsConfig) PageViewDataProvider(io.confluent.ksql.util.PageViewDataProvider) BeforeClass(org.junit.BeforeClass) MaterializedTable(io.confluent.ksql.execution.streams.materialization.MaterializedTable) JSON(io.confluent.ksql.serde.FormatFactory.JSON) QueryContext(io.confluent.ksql.execution.context.QueryContext) IntegrationTest(org.apache.kafka.test.IntegrationTest) KsqlIdentifierTestUtil(io.confluent.ksql.test.util.KsqlIdentifierTestUtil) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Windowed(org.apache.kafka.streams.kstream.Windowed) Timeout(org.junit.rules.Timeout) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) Deserializer(org.apache.kafka.common.serialization.Deserializer) Before(org.junit.Before) TestKsqlContext(io.confluent.ksql.integration.TestKsqlContext) Matchers.empty(org.hamcrest.Matchers.empty) LongStream(java.util.stream.LongStream) Iterator(java.util.Iterator) Lists(org.apache.commons.compress.utils.Lists) KAFKA(io.confluent.ksql.serde.FormatFactory.KAFKA) MaterializedWindowedTable(io.confluent.ksql.execution.streams.materialization.MaterializedWindowedTable) Test(org.junit.Test) IntegrationTestHarness(io.confluent.ksql.integration.IntegrationTestHarness) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) Rule(org.junit.Rule) GenericRow(io.confluent.ksql.GenericRow) Format(io.confluent.ksql.serde.Format) GenericKey(io.confluent.ksql.GenericKey) Materialization(io.confluent.ksql.execution.streams.materialization.Materialization) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) Materialization(io.confluent.ksql.execution.streams.materialization.Materialization) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) GenericKey(io.confluent.ksql.GenericKey) Row(io.confluent.ksql.execution.streams.materialization.Row) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) GenericRow(io.confluent.ksql.GenericRow) MaterializedTable(io.confluent.ksql.execution.streams.materialization.MaterializedTable) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 8 with Row

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

the class KsMaterializedTableIQv2 method get.

@Override
public KsMaterializedQueryResult<Row> get(final int partition, final Optional<Position> position) {
    try {
        final RangeQuery<GenericKey, ValueAndTimestamp<GenericRow>> query = RangeQuery.withNoBounds();
        StateQueryRequest<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query).withPartitions(ImmutableSet.of(partition));
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final StateQueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> result = stateStore.getKafkaStreams().query(request);
        final QueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        } else if (queryResult.getResult() == null) {
            return KsMaterializedQueryResult.rowIteratorWithPosition(Collections.emptyIterator(), queryResult.getPosition());
        } else {
            final KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>> iterator = queryResult.getResult();
            return KsMaterializedQueryResult.rowIteratorWithPosition(Streams.stream(IteratorUtil.onComplete(iterator, iterator::close)).map(keyValue -> Row.of(stateStore.schema(), keyValue.key, keyValue.value.value(), keyValue.value.timestamp())).iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to scan materialized table", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) StateQueryRequest.inStore(org.apache.kafka.streams.query.StateQueryRequest.inStore) MaterializedTable(io.confluent.ksql.execution.streams.materialization.MaterializedTable) Position(org.apache.kafka.streams.query.Position) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) Row(io.confluent.ksql.execution.streams.materialization.Row) FailureReason(org.apache.kafka.streams.query.FailureReason) PositionBound(org.apache.kafka.streams.query.PositionBound) RangeQuery(org.apache.kafka.streams.query.RangeQuery) Streams(com.google.common.collect.Streams) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) StreamsMaterializedTable(io.confluent.ksql.execution.streams.materialization.StreamsMaterializedTable) Objects(java.util.Objects) StateQueryRequest(org.apache.kafka.streams.query.StateQueryRequest) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) IteratorUtil(io.confluent.ksql.util.IteratorUtil) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) ImmutableList(com.google.common.collect.ImmutableList) GenericRow(io.confluent.ksql.GenericRow) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) KeyQuery(org.apache.kafka.streams.query.KeyQuery) Collections(java.util.Collections) QueryResult(org.apache.kafka.streams.query.QueryResult) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) GenericRow(io.confluent.ksql.GenericRow) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) GenericKey(io.confluent.ksql.GenericKey)

Example 9 with Row

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

the class KsMaterializedTableIQv2 method get.

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
@Override
public KsMaterializedQueryResult<Row> get(final int partition, final GenericKey from, final GenericKey to, final Optional<Position> position) {
    // CHECKSTYLE_RULES.ON: CyclomaticComplexity
    try {
        final RangeQuery<GenericKey, ValueAndTimestamp<GenericRow>> query;
        if (from != null && to != null) {
            query = RangeQuery.withRange(from, to);
        } else if (from == null && to != null) {
            query = RangeQuery.withUpperBound(to);
        } else if (from != null && to == null) {
            query = RangeQuery.withLowerBound(from);
        } else {
            query = RangeQuery.withNoBounds();
        }
        StateQueryRequest<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query).withPartitions(ImmutableSet.of(partition));
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final StateQueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> result = stateStore.getKafkaStreams().query(request);
        final QueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        } else if (queryResult.getResult() == null) {
            return KsMaterializedQueryResult.rowIteratorWithPosition(Collections.emptyIterator(), queryResult.getPosition());
        } else {
            final KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>> iterator = queryResult.getResult();
            return KsMaterializedQueryResult.rowIteratorWithPosition(Streams.stream(IteratorUtil.onComplete(iterator, iterator::close)).map(keyValue -> Row.of(stateStore.schema(), keyValue.key, keyValue.value.value(), keyValue.value.timestamp())).iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to range scan materialized table", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) StateQueryRequest.inStore(org.apache.kafka.streams.query.StateQueryRequest.inStore) MaterializedTable(io.confluent.ksql.execution.streams.materialization.MaterializedTable) Position(org.apache.kafka.streams.query.Position) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) Row(io.confluent.ksql.execution.streams.materialization.Row) FailureReason(org.apache.kafka.streams.query.FailureReason) PositionBound(org.apache.kafka.streams.query.PositionBound) RangeQuery(org.apache.kafka.streams.query.RangeQuery) Streams(com.google.common.collect.Streams) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) StreamsMaterializedTable(io.confluent.ksql.execution.streams.materialization.StreamsMaterializedTable) Objects(java.util.Objects) StateQueryRequest(org.apache.kafka.streams.query.StateQueryRequest) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) IteratorUtil(io.confluent.ksql.util.IteratorUtil) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) ImmutableList(com.google.common.collect.ImmutableList) GenericRow(io.confluent.ksql.GenericRow) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) KeyQuery(org.apache.kafka.streams.query.KeyQuery) Collections(java.util.Collections) QueryResult(org.apache.kafka.streams.query.QueryResult) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) GenericRow(io.confluent.ksql.GenericRow) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) GenericKey(io.confluent.ksql.GenericKey)

Example 10 with Row

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

the class KeyedTableLookupOperator method getMatIterator.

private Iterator<Row> getMatIterator(final KsqlKey ksqlKey) {
    if (!(nextKey instanceof KeyConstraint)) {
        throw new IllegalStateException(String.format("Keyed lookup queries should be done with " + "key constraints: %s", ksqlKey.toString()));
    }
    final KeyConstraint keyConstraintKey = (KeyConstraint) ksqlKey;
    final Iterator<Row> result;
    if (keyConstraintKey.getOperator() == ConstraintOperator.EQUAL) {
        result = mat.nonWindowed().get(ksqlKey.getKey(), nextLocation.getPartition(), consistencyOffsetVector);
    } else if (keyConstraintKey.getOperator() == ConstraintOperator.GREATER_THAN || keyConstraintKey.getOperator() == ConstraintOperator.GREATER_THAN_OR_EQUAL) {
        // Underlying store will always return keys inclusive the endpoints
        // and filtering is used to trim start and end of the range in case of ">"
        final GenericKey fromKey = keyConstraintKey.getKey();
        final GenericKey toKey = null;
        result = mat.nonWindowed().get(nextLocation.getPartition(), fromKey, toKey, consistencyOffsetVector);
    } else if (keyConstraintKey.getOperator() == ConstraintOperator.LESS_THAN || keyConstraintKey.getOperator() == ConstraintOperator.LESS_THAN_OR_EQUAL) {
        // Underlying store will always return keys inclusive the endpoints
        // and filtering is used to trim start and end of the range in case of "<"
        final GenericKey fromKey = null;
        final GenericKey toKey = keyConstraintKey.getKey();
        result = mat.nonWindowed().get(nextLocation.getPartition(), fromKey, toKey, consistencyOffsetVector);
    } else {
        throw new IllegalStateException(String.format("Invalid comparator type " + keyConstraintKey.getOperator()));
    }
    return result;
}
Also used : KeyConstraint(io.confluent.ksql.planner.plan.KeyConstraint) Row(io.confluent.ksql.execution.streams.materialization.Row) GenericKey(io.confluent.ksql.GenericKey)

Aggregations

Row (io.confluent.ksql.execution.streams.materialization.Row)12 GenericRow (io.confluent.ksql.GenericRow)9 GenericKey (io.confluent.ksql.GenericKey)8 MaterializedTable (io.confluent.ksql.execution.streams.materialization.MaterializedTable)7 Test (org.junit.Test)7 Materialization (io.confluent.ksql.execution.streams.materialization.Materialization)5 WindowedRow (io.confluent.ksql.execution.streams.materialization.WindowedRow)5 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)5 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)5 IntegrationTest (org.apache.kafka.test.IntegrationTest)5 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Streams (com.google.common.collect.Streams)2 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)2 StreamsMaterializedTable (io.confluent.ksql.execution.streams.materialization.StreamsMaterializedTable)2 IteratorUtil (io.confluent.ksql.util.IteratorUtil)2 Collections (java.util.Collections)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 FailureReason (org.apache.kafka.streams.query.FailureReason)2