Search in sources :

Example 61 with GenericKey

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

the class TableAggregateBuilder method build.

public static KTableHolder<GenericKey> build(final KGroupedTableHolder groupedTable, final TableAggregate aggregate, final RuntimeBuildContext buildContext, final MaterializedFactory materializedFactory, final AggregateParamsFactory aggregateParamsFactory) {
    final LogicalSchema sourceSchema = groupedTable.getSchema();
    final List<ColumnName> nonFuncColumns = aggregate.getNonAggregateColumns();
    final AggregateParams aggregateParams = aggregateParamsFactory.createUndoable(sourceSchema, nonFuncColumns, buildContext.getFunctionRegistry(), aggregate.getAggregationFunctions(), buildContext.getKsqlConfig());
    final LogicalSchema aggregateSchema = aggregateParams.getAggregateSchema();
    final LogicalSchema resultSchema = aggregateParams.getSchema();
    final Materialized<GenericKey, GenericRow, KeyValueStore<Bytes, byte[]>> materialized = MaterializationUtil.buildMaterialized(aggregate, aggregateSchema, aggregate.getInternalFormats(), buildContext, materializedFactory, ExecutionKeyFactory.unwindowed(buildContext));
    final KTable<GenericKey, GenericRow> aggregated = groupedTable.getGroupedTable().aggregate(aggregateParams.getInitializer(), aggregateParams.getAggregator(), aggregateParams.getUndoAggregator().get(), materialized).transformValues(() -> new KsTransformer<>(aggregateParams.<GenericKey>getAggregator().getResultMapper()), Named.as(StreamsUtil.buildOpName(AggregateBuilderUtils.outputContext(aggregate))));
    final MaterializationInfo.Builder materializationBuilder = AggregateBuilderUtils.materializationInfoBuilder(aggregateParams.getAggregator(), aggregate, aggregateSchema, resultSchema);
    return KTableHolder.materialized(aggregated, resultSchema, ExecutionKeyFactory.unwindowed(buildContext), materializationBuilder);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) ColumnName(io.confluent.ksql.name.ColumnName) MaterializationInfo(io.confluent.ksql.execution.materialization.MaterializationInfo) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) GenericKey(io.confluent.ksql.GenericKey)

Example 62 with GenericKey

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

the class StreamAggregateBuilder method build.

static KTableHolder<GenericKey> build(final KGroupedStreamHolder groupedStream, final StreamAggregate aggregate, final RuntimeBuildContext buildContext, final MaterializedFactory materializedFactory, final AggregateParamsFactory aggregateParamsFactory) {
    final LogicalSchema sourceSchema = groupedStream.getSchema();
    final List<ColumnName> nonFuncColumns = aggregate.getNonAggregateColumns();
    final AggregateParams aggregateParams = aggregateParamsFactory.create(sourceSchema, nonFuncColumns, buildContext.getFunctionRegistry(), aggregate.getAggregationFunctions(), false, buildContext.getKsqlConfig());
    final LogicalSchema aggregateSchema = aggregateParams.getAggregateSchema();
    final LogicalSchema resultSchema = aggregateParams.getSchema();
    final Materialized<GenericKey, GenericRow, KeyValueStore<Bytes, byte[]>> materialized = MaterializationUtil.buildMaterialized(aggregate, aggregateSchema, aggregate.getInternalFormats(), buildContext, materializedFactory, ExecutionKeyFactory.unwindowed(buildContext));
    final KudafAggregator<GenericKey> aggregator = aggregateParams.getAggregator();
    final KTable<GenericKey, GenericRow> aggregated = groupedStream.getGroupedStream().aggregate(aggregateParams.getInitializer(), aggregateParams.getAggregator(), materialized);
    final MaterializationInfo.Builder materializationBuilder = AggregateBuilderUtils.materializationInfoBuilder(aggregateParams.getAggregator(), aggregate, aggregateSchema, resultSchema);
    final KTable<GenericKey, GenericRow> result = aggregated.transformValues(() -> new KsTransformer<>(aggregator.getResultMapper()), Named.as(StreamsUtil.buildOpName(AggregateBuilderUtils.outputContext(aggregate))));
    return KTableHolder.materialized(result, resultSchema, ExecutionKeyFactory.unwindowed(buildContext), materializationBuilder);
}
Also used : LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) GenericRow(io.confluent.ksql.GenericRow) ColumnName(io.confluent.ksql.name.ColumnName) MaterializationInfo(io.confluent.ksql.execution.materialization.MaterializationInfo) GenericKey(io.confluent.ksql.GenericKey)

Example 63 with GenericKey

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

the class CliTest method testSelectProject.

@Test
public void testSelectProject() {
    final Map<GenericKey, GenericRow> expectedResults = ImmutableMap.<GenericKey, GenericRow>builder().put(genericKey("ORDER_1"), genericRow("ITEM_1", 10.0, ImmutableList.of(100.0, 110.99, 90.0))).put(genericKey("ORDER_2"), genericRow("ITEM_2", 20.0, ImmutableList.of(10.0, 10.99, 9.0))).put(genericKey("ORDER_3"), genericRow("ITEM_3", 30.0, ImmutableList.of(10.0, 10.99, 91.0))).put(genericKey("ORDER_4"), genericRow("ITEM_4", 40.0, ImmutableList.of(10.0, 140.99, 94.0))).put(genericKey("ORDER_5"), genericRow("ITEM_5", 50.0, ImmutableList.of(160.0, 160.99, 98.0))).put(genericKey("ORDER_6"), genericRow("ITEM_8", 80.0, ImmutableList.of(1100.0, 1110.99, 970.0))).build();
    final PhysicalSchema resultSchema = PhysicalSchema.from(LogicalSchema.builder().keyColumn(ColumnName.of("ORDERID"), SqlTypes.STRING).valueColumn(ColumnName.of("ITEMID"), SqlTypes.STRING).valueColumn(ColumnName.of("ORDERUNITS"), SqlTypes.DOUBLE).valueColumn(ColumnName.of("PRICEARRAY"), SqlTypes.array(SqlTypes.DOUBLE)).build(), SerdeFeatures.of(), SerdeFeatures.of());
    testCreateStreamAsSelect("SELECT ORDERID, ITEMID, ORDERUNITS, PRICEARRAY " + "FROM " + ORDER_DATA_PROVIDER.sourceName() + ";", resultSchema, expectedResults);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) GenericKey(io.confluent.ksql.GenericKey) Test(org.junit.Test) IntegrationTest(io.confluent.common.utils.IntegrationTest)

Example 64 with GenericKey

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

the class CliTest method testSelectUDFs.

@Test
public void testSelectUDFs() {
    final String queryString = String.format("SELECT " + "ORDERID, " + "ITEMID, " + "ORDERUNITS*10 AS Col1, " + "PRICEARRAY[1]+10 AS Col2, " + "KEYVALUEMAP['key1']*KEYVALUEMAP['key2']+10 AS Col3, " + "PRICEARRAY[2]>1000 AS Col4 " + "FROM %s " + "WHERE ORDERUNITS > 20 AND ITEMID LIKE '%%_8';", ORDER_DATA_PROVIDER.sourceName());
    final PhysicalSchema resultSchema = PhysicalSchema.from(LogicalSchema.builder().keyColumn(ColumnName.of("ORDERID"), SqlTypes.STRING).valueColumn(ColumnName.of("ITEMID"), SqlTypes.STRING).valueColumn(ColumnName.of("COL1"), SqlTypes.DOUBLE).valueColumn(ColumnName.of("COL2"), SqlTypes.DOUBLE).valueColumn(ColumnName.of("COL3"), SqlTypes.DOUBLE).valueColumn(ColumnName.of("COL4"), SqlTypes.BOOLEAN).build(), SerdeFeatures.of(), SerdeFeatures.of());
    final Map<GenericKey, GenericRow> expectedResults = ImmutableMap.of(genericKey("ORDER_6"), genericRow("ITEM_8", 800.0, 1110.0, 12.0, true));
    testCreateStreamAsSelect(queryString, resultSchema, expectedResults);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericKey(io.confluent.ksql.GenericKey) Test(org.junit.Test) IntegrationTest(io.confluent.common.utils.IntegrationTest)

Example 65 with GenericKey

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

the class DecimalMaxKudafTest method shouldFindCorrectMaxForMerge.

@Test
public void shouldFindCorrectMaxForMerge() {
    final DecimalMaxKudaf doubleMaxKudaf = getDecimalMaxKudaf(3);
    final Merger<GenericKey, BigDecimal> merger = doubleMaxKudaf.getMerger();
    final BigDecimal mergeResult1 = merger.apply(null, new BigDecimal(10.0), new BigDecimal(12.0));
    assertThat(mergeResult1, equalTo(new BigDecimal(12.0, new MathContext(3))));
    final BigDecimal mergeResult2 = merger.apply(null, new BigDecimal(10.0), new BigDecimal(-12.0));
    assertThat(mergeResult2, equalTo(new BigDecimal(10.0, new MathContext(3))));
    final BigDecimal mergeResult3 = merger.apply(null, new BigDecimal(-10.0), new BigDecimal(0.0));
    assertThat(mergeResult3, equalTo(new BigDecimal(0.0, new MathContext(3))));
}
Also used : GenericKey(io.confluent.ksql.GenericKey) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) 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