Search in sources :

Example 16 with PhysicalSchema

use of io.confluent.ksql.schema.ksql.PhysicalSchema in project ksql by confluentinc.

the class SourceBuilderV1 method buildWindowedTable.

KTableHolder<Windowed<GenericKey>> buildWindowedTable(final RuntimeBuildContext buildContext, final SourceStep<KTableHolder<Windowed<GenericKey>>> source, final ConsumedFactory consumedFactory, final MaterializedFactory materializedFactory, final PlanInfo planInfo) {
    final PhysicalSchema physicalSchema = getPhysicalSchema(source);
    final Serde<GenericRow> valueSerde = getValueSerde(buildContext, source, physicalSchema);
    final WindowInfo windowInfo;
    if (source instanceof WindowedTableSource) {
        windowInfo = ((WindowedTableSource) source).getWindowInfo();
    } else {
        throw new IllegalArgumentException("Expected a version of WindowedTableSource");
    }
    final Serde<Windowed<GenericKey>> keySerde = SourceBuilderUtils.getWindowedKeySerde(source, physicalSchema, buildContext, windowInfo);
    final Consumed<Windowed<GenericKey>, GenericRow> consumed = buildSourceConsumed(source, keySerde, valueSerde, AutoOffsetReset.EARLIEST, buildContext, consumedFactory);
    final String stateStoreName = tableChangeLogOpName(source.getProperties());
    final Materialized<Windowed<GenericKey>, GenericRow, KeyValueStore<Bytes, byte[]>> materialized = materializedFactory.create(keySerde, valueSerde, stateStoreName);
    final KTable<Windowed<GenericKey>, GenericRow> ktable = buildKTable(source, buildContext, consumed, windowedKeyGenerator(source.getSourceSchema()), materialized, valueSerde, stateStoreName, planInfo);
    return KTableHolder.materialized(ktable, buildSchema(source, true), ExecutionKeyFactory.windowed(buildContext, windowInfo), MaterializationInfo.builder(stateStoreName, physicalSchema.logicalSchema()));
}
Also used : KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) WindowInfo(io.confluent.ksql.serde.WindowInfo) GenericRow(io.confluent.ksql.GenericRow) Windowed(org.apache.kafka.streams.kstream.Windowed) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) SourceBuilderUtils.getPhysicalSchema(io.confluent.ksql.execution.streams.SourceBuilderUtils.getPhysicalSchema) WindowedTableSource(io.confluent.ksql.execution.plan.WindowedTableSource)

Example 17 with PhysicalSchema

use of io.confluent.ksql.schema.ksql.PhysicalSchema in project ksql by confluentinc.

the class StreamGroupByBuilderBase method buildGrouped.

private static Grouped<GenericKey, GenericRow> buildGrouped(final Formats formats, final LogicalSchema schema, final QueryContext queryContext, final RuntimeBuildContext buildContext, final GroupedFactory groupedFactory) {
    final PhysicalSchema physicalSchema = PhysicalSchema.from(schema, formats.getKeyFeatures(), formats.getValueFeatures());
    final Serde<GenericKey> keySerde = buildContext.buildKeySerde(formats.getKeyFormat(), physicalSchema, queryContext);
    final Serde<GenericRow> valSerde = buildContext.buildValueSerde(formats.getValueFormat(), physicalSchema, queryContext);
    return groupedFactory.create(StreamsUtil.buildOpName(queryContext), keySerde, valSerde);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) GenericKey(io.confluent.ksql.GenericKey)

Example 18 with PhysicalSchema

use of io.confluent.ksql.schema.ksql.PhysicalSchema 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 19 with PhysicalSchema

use of io.confluent.ksql.schema.ksql.PhysicalSchema 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 20 with PhysicalSchema

use of io.confluent.ksql.schema.ksql.PhysicalSchema in project ksql by confluentinc.

the class QueryBuilder method buildPersistentQueryInSharedRuntime.

@SuppressWarnings("ParameterNumber")
PersistentQueryMetadata buildPersistentQueryInSharedRuntime(final KsqlConfig ksqlConfig, final KsqlConstants.PersistentQueryType persistentQueryType, final String statementText, final QueryId queryId, final Optional<DataSource> sinkDataSource, final Set<DataSource> sources, final ExecutionStep<?> physicalPlan, final String planSummary, final QueryMetadata.Listener listener, final Supplier<List<PersistentQueryMetadata>> allPersistentQueries, final String applicationId, final MetricCollectors metricCollectors) {
    final SharedKafkaStreamsRuntime sharedKafkaStreamsRuntime = getKafkaStreamsInstance(applicationId, sources.stream().map(DataSource::getName).collect(Collectors.toSet()), queryId, metricCollectors);
    final Map<String, Object> queryOverrides = sharedKafkaStreamsRuntime.getStreamProperties();
    final LogicalSchema logicalSchema;
    final KeyFormat keyFormat;
    final ValueFormat valueFormat;
    final KsqlTopic ksqlTopic;
    switch(persistentQueryType) {
        // CREATE_SOURCE does not have a sink, so the schema is obtained from the query source
        case CREATE_SOURCE:
            final DataSource dataSource = Iterables.getOnlyElement(sources);
            logicalSchema = dataSource.getSchema();
            keyFormat = dataSource.getKsqlTopic().getKeyFormat();
            valueFormat = dataSource.getKsqlTopic().getValueFormat();
            ksqlTopic = dataSource.getKsqlTopic();
            break;
        default:
            logicalSchema = sinkDataSource.get().getSchema();
            keyFormat = sinkDataSource.get().getKsqlTopic().getKeyFormat();
            valueFormat = sinkDataSource.get().getKsqlTopic().getValueFormat();
            ksqlTopic = sinkDataSource.get().getKsqlTopic();
            break;
    }
    final PhysicalSchema querySchema = PhysicalSchema.from(logicalSchema, keyFormat.getFeatures(), valueFormat.getFeatures());
    final NamedTopologyBuilder namedTopologyBuilder = sharedKafkaStreamsRuntime.getKafkaStreams().newNamedTopologyBuilder(queryId.toString(), PropertiesUtil.asProperties(queryOverrides));
    final RuntimeBuildContext runtimeBuildContext = buildContext(applicationId, queryId, namedTopologyBuilder);
    final Object result = buildQueryImplementation(physicalPlan, runtimeBuildContext);
    final NamedTopology topology = namedTopologyBuilder.build();
    final Optional<MaterializationProviderBuilderFactory.MaterializationProviderBuilder> materializationProviderBuilder = getMaterializationInfo(result).map(info -> materializationProviderBuilderFactory.materializationProviderBuilder(info, querySchema, keyFormat, queryOverrides, applicationId, queryId.toString()));
    final Optional<ScalablePushRegistry> scalablePushRegistry = applyScalablePushProcessor(querySchema.logicalSchema(), result, allPersistentQueries, queryOverrides, applicationId, ksqlConfig, ksqlTopic, serviceContext);
    final BinPackedPersistentQueryMetadataImpl binPackedPersistentQueryMetadata = new BinPackedPersistentQueryMetadataImpl(persistentQueryType, statementText, querySchema, sources.stream().map(DataSource::getName).collect(Collectors.toSet()), planSummary, applicationId, topology, sharedKafkaStreamsRuntime, runtimeBuildContext.getSchemas(), config.getOverrides(), queryId, materializationProviderBuilder, physicalPlan, getUncaughtExceptionProcessingLogger(queryId), sinkDataSource, listener, queryOverrides, scalablePushRegistry, (streamsRuntime) -> getNamedTopology(streamsRuntime, queryId, applicationId, queryOverrides, physicalPlan));
    if (real) {
        return binPackedPersistentQueryMetadata;
    } else {
        return SandboxedBinPackedPersistentQueryMetadataImpl.of(binPackedPersistentQueryMetadata, listener);
    }
}
Also used : ValueFormat(io.confluent.ksql.serde.ValueFormat) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KeyFormat(io.confluent.ksql.serde.KeyFormat) DataSource(io.confluent.ksql.metastore.model.DataSource) ScalablePushRegistry(io.confluent.ksql.physical.scalablepush.ScalablePushRegistry) SharedKafkaStreamsRuntime(io.confluent.ksql.util.SharedKafkaStreamsRuntime) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) NamedTopologyBuilder(org.apache.kafka.streams.processor.internals.namedtopology.NamedTopologyBuilder) NamedTopology(org.apache.kafka.streams.processor.internals.namedtopology.NamedTopology) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic) BinPackedPersistentQueryMetadataImpl(io.confluent.ksql.util.BinPackedPersistentQueryMetadataImpl) SandboxedBinPackedPersistentQueryMetadataImpl(io.confluent.ksql.util.SandboxedBinPackedPersistentQueryMetadataImpl)

Aggregations

PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)34 GenericRow (io.confluent.ksql.GenericRow)21 GenericKey (io.confluent.ksql.GenericKey)12 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)11 QueryContext (io.confluent.ksql.execution.context.QueryContext)9 Test (org.junit.Test)7 Formats (io.confluent.ksql.execution.plan.Formats)6 RuntimeBuildContext (io.confluent.ksql.execution.runtime.RuntimeBuildContext)6 DataSource (io.confluent.ksql.metastore.model.DataSource)6 IntegrationTest (io.confluent.common.utils.IntegrationTest)5 ProcessingLogger (io.confluent.ksql.logging.processing.ProcessingLogger)4 Serde (org.apache.kafka.common.serialization.Serde)4 Windowed (org.apache.kafka.streams.kstream.Windowed)4 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)4 KTableHolder (io.confluent.ksql.execution.plan.KTableHolder)3 SourceBuilderUtils.getPhysicalSchema (io.confluent.ksql.execution.streams.SourceBuilderUtils.getPhysicalSchema)3 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)2 KsqlSchemaAuthorizationException (io.confluent.ksql.exception.KsqlSchemaAuthorizationException)2 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)2 CodeGenRunner (io.confluent.ksql.execution.codegen.CodeGenRunner)2