use of io.confluent.ksql.execution.streams.materialization.MaterializedTable in project ksql by confluentinc.
the class KsMaterializationFunctionalTest method shouldQueryMaterializedTableWithKeyFieldsInProjection.
@Test
public void shouldQueryMaterializedTableWithKeyFieldsInProjection() {
// Given:
final PersistentQueryMetadata query = executeQuery("CREATE TABLE " + output + " AS" + " SELECT USERID, COUNT(*), AS_VALUE(USERID) AS USERID_2 FROM " + USER_TABLE + " GROUP BY USERID;");
final LogicalSchema schema = schema("KSQL_COL_0", SqlTypes.BIGINT, "USERID_2", SqlTypes.STRING);
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 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));
});
}
use of io.confluent.ksql.execution.streams.materialization.MaterializedTable 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));
}
use of io.confluent.ksql.execution.streams.materialization.MaterializedTable 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));
});
}
use of io.confluent.ksql.execution.streams.materialization.MaterializedTable in project ksql by confluentinc.
the class KsMaterializationFunctionalTest method shouldQueryMaterializedTableForAggregatedStream.
@Test
public void shouldQueryMaterializedTableForAggregatedStream() {
// Given:
final PersistentQueryMetadata query = executeQuery("CREATE TABLE " + output + " AS" + " SELECT USERID, COUNT(*) AS COUNT FROM " + USER_STREAM + " GROUP BY USERID;");
final LogicalSchema schema = schema("COUNT", 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));
}
use of io.confluent.ksql.execution.streams.materialization.MaterializedTable in project ksql by confluentinc.
the class KsMaterializationFunctionalTest method shouldQueryMaterializedTableWithMultipleAggregationColumns.
@Test
public void shouldQueryMaterializedTableWithMultipleAggregationColumns() {
// Given:
final PersistentQueryMetadata query = executeQuery("CREATE TABLE " + output + " AS" + " SELECT USERID, COUNT(1) AS COUNT, SUM(REGISTERTIME) AS SUM FROM " + USER_TABLE + " GROUP BY USERID;");
final LogicalSchema schema = schema("COUNT", SqlTypes.BIGINT, "SUM", 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 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));
});
}
Aggregations