Search in sources :

Example 21 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class TableAggregateBuilderTest method shouldBuildMaterializationCorrectlyForAggregate.

@Test
public void shouldBuildMaterializationCorrectlyForAggregate() {
    // When:
    final KTableHolder<?> result = aggregate.build(planBuilder, planInfo);
    // Then:
    assertThat(result.getMaterializationBuilder().isPresent(), is(true));
    final MaterializationInfo info = result.getMaterializationBuilder().get().build();
    assertThat(info.stateStoreName(), equalTo("agg-regate-Materialize"));
    assertThat(info.getSchema(), equalTo(AGGREGATE_SCHEMA));
    assertThat(info.getStateStoreSchema(), equalTo(AGGREGATE_SCHEMA));
    assertThat(info.getTransforms(), hasSize(1));
    final MapperInfo aggMapInfo = (MapperInfo) info.getTransforms().get(0);
    final KsqlTransformer<Object, GenericRow> mapper = aggMapInfo.getMapper(name -> null);
    // Given:
    final GenericKey key = mock(GenericKey.class);
    final GenericRow value = mock(GenericRow.class);
    // When:
    mapper.transform(key, value, ctx);
    // Then:
    verify(resultMapper).transform(key, value, ctx);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) MaterializationInfo(io.confluent.ksql.execution.materialization.MaterializationInfo) MapperInfo(io.confluent.ksql.execution.materialization.MaterializationInfo.MapperInfo) GenericKey(io.confluent.ksql.GenericKey) Test(org.junit.Test)

Example 22 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class ForeignKeyTableTableJoinBuilderTest method shouldDoInnerJoinOnKey.

// this is actually a PK-PK join and the logical planner would not compile a FK-join plan
// for this case atm
// however, from a physical plan POV this should still work, so we would like to keep this test
// 
// it might be possible to actually change the logical planner to compile a PK-PK join as
// FK-join if input tables are not co-partitioned (instead of throwing an error an rejecting
// the query), ie, if key-format or partition-count do not match -- it's an open question
// if it would be a good idea to do this though
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldDoInnerJoinOnKey() {
    // Given:
    givenInnerJoin(left, L_KEY);
    // When:
    final KTableHolder<Struct> result = join.build(planBuilder, planInfo);
    // Then:
    final ArgumentCaptor<KsqlKeyExtractor> ksqlKeyExtractor = ArgumentCaptor.forClass(KsqlKeyExtractor.class);
    verify(leftKTable).join(same(rightKTable), ksqlKeyExtractor.capture(), eq(new KsqlValueJoiner(LEFT_SCHEMA.value().size(), RIGHT_SCHEMA.value().size(), 0)), any(Materialized.class));
    verifyNoMoreInteractions(leftKTable, rightKTable, resultKTable);
    final GenericKey extractedKey = GenericKey.genericKey(LEFT_KEY);
    assertThat(ksqlKeyExtractor.getValue().apply(LEFT_ROW), is(extractedKey));
    assertThat(result.getTable(), is(resultKTable));
    assertThat(result.getExecutionKeyFactory(), is(executionKeyFactory));
}
Also used : GenericKey(io.confluent.ksql.GenericKey) Materialized(org.apache.kafka.streams.kstream.Materialized) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 23 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class GroupByParamsV1FactoryTest method shouldGenerateSingleExpressionGroupByKey.

@Test
public void shouldGenerateSingleExpressionGroupByKey() {
    // Given:
    when(groupBy0.evaluate(any(), any(), any(), any())).thenReturn(10);
    // When:
    final GenericKey result = singleParams.getMapper().apply(value);
    // Then:
    assertThat(result, is(genericKey(10)));
}
Also used : GenericKey(io.confluent.ksql.GenericKey) Test(org.junit.Test)

Example 24 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class GenericRecordFactory method build.

public KsqlGenericRecord build(final List<ColumnName> columnNames, final List<Expression> expressions, final LogicalSchema schema, final DataSourceType dataSourceType) {
    final List<ColumnName> columns = columnNames.isEmpty() ? implicitColumns(schema) : columnNames;
    if (columns.size() != expressions.size()) {
        throw new KsqlException("Expected a value for each column." + " Expected Columns: " + columnNames + ". Got " + expressions);
    }
    final LogicalSchema schemaWithPseudoColumns = withPseudoColumns(schema, config);
    for (ColumnName col : columns) {
        if (!schemaWithPseudoColumns.findColumn(col).isPresent()) {
            throw new KsqlException("Column name " + col + " does not exist.");
        }
        if (SystemColumns.isDisallowedForInsertValues(col, config)) {
            throw new KsqlException("Inserting into column " + col + " is not allowed.");
        }
    }
    final Map<ColumnName, Object> values = resolveValues(columns, expressions, schemaWithPseudoColumns, functionRegistry, config);
    if (dataSourceType == DataSourceType.KTABLE) {
        final String noValue = schemaWithPseudoColumns.key().stream().map(Column::name).filter(colName -> !values.containsKey(colName)).map(ColumnName::text).collect(Collectors.joining(", "));
        if (!noValue.isEmpty()) {
            throw new KsqlException("Value for primary key column(s) " + noValue + " is required for tables");
        }
    }
    final long ts = (long) values.getOrDefault(SystemColumns.ROWTIME_NAME, clock.getAsLong());
    final GenericKey key = buildKey(schema, values);
    final GenericRow value = buildValue(schema, values);
    return KsqlGenericRecord.of(key, value, ts);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) ColumnName(io.confluent.ksql.name.ColumnName) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) GenericKey(io.confluent.ksql.GenericKey) KsqlException(io.confluent.ksql.util.KsqlException)

Example 25 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class TestDriverPipelineTest method shouldHandleTwoTopologiesWithSameInputDifferentOutput.

@Test
public void shouldHandleTwoTopologiesWithSameInputDifferentOutput() {
    // Given (two topologies that pipe a to b and a to c):
    final TopologyTestDriver driver1 = mock(TopologyTestDriver.class);
    final TopologyTestDriver driver2 = mock(TopologyTestDriver.class);
    final TestInputTopic<GenericKey, GenericRow> inA1 = givenInput(driver1, "a");
    final TestInputTopic<GenericKey, GenericRow> inA2 = givenInput(driver2, "a");
    givenOutput(driver1, "b");
    givenOutput(driver2, "c");
    givenPipe(inA1, "b");
    givenPipe(inA2, "c");
    pipeline.addDriver(driver1, ImmutableList.of(inf("a")), inf("b"));
    pipeline.addDriver(driver2, ImmutableList.of(inf("a")), inf("c"));
    // When:
    pipeline.pipeInput("a", KEY, ROW1, 1);
    // Then:
    assertThat(pipeline.getAllRecordsForTopic("b"), is(ImmutableList.of(new TestRecord<>(KEY, ROW1, Instant.ofEpochMilli(1)))));
    assertThat(pipeline.getAllRecordsForTopic("c"), is(ImmutableList.of(new TestRecord<>(KEY, ROW1, Instant.ofEpochMilli(1)))));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) GenericKey(io.confluent.ksql.GenericKey) Test(org.junit.Test)

Aggregations

GenericKey (io.confluent.ksql.GenericKey)147 GenericRow (io.confluent.ksql.GenericRow)100 Test (org.junit.Test)93 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)24 Windowed (org.apache.kafka.streams.kstream.Windowed)20 WindowedRow (io.confluent.ksql.execution.streams.materialization.WindowedRow)14 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)14 Materialized (org.apache.kafka.streams.kstream.Materialized)13 ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)13 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)12 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)9 IntegrationTest (io.confluent.common.utils.IntegrationTest)8 Materialization (io.confluent.ksql.execution.streams.materialization.Materialization)8 Row (io.confluent.ksql.execution.streams.materialization.Row)8 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)8 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)8 IntegrationTest (org.apache.kafka.test.IntegrationTest)8 InOrder (org.mockito.InOrder)8 MaterializedTable (io.confluent.ksql.execution.streams.materialization.MaterializedTable)7 Objects (java.util.Objects)7