Search in sources :

Example 1 with RuntimeBuildContext

use of io.confluent.ksql.execution.runtime.RuntimeBuildContext in project ksql by confluentinc.

the class TableSelectBuilder method build.

@SuppressWarnings("unchecked")
public static <K> KTableHolder<K> build(final KTableHolder<K> table, final TableSelect<K> step, final RuntimeBuildContext buildContext, final Optional<Formats> formats, final MaterializedFactory materializedFactory) {
    final LogicalSchema sourceSchema = table.getSchema();
    final QueryContext queryContext = step.getProperties().getQueryContext();
    final Selection<K> selection = Selection.of(sourceSchema, step.getKeyColumnNames(), step.getSelectExpressions(), buildContext.getKsqlConfig(), buildContext.getFunctionRegistry());
    final SelectValueMapper<K> selectMapper = selection.getMapper();
    final ProcessingLogger logger = buildContext.getProcessingLogger(queryContext);
    final Named selectName = Named.as(StreamsUtil.buildOpName(queryContext));
    final Optional<MaterializationInfo.Builder> matBuilder = table.getMaterializationBuilder();
    final boolean forceMaterialize = !matBuilder.isPresent();
    final Serde<K> keySerde;
    final Serde<GenericRow> valSerde;
    if (formats.isPresent()) {
        final Formats materializationFormat = formats.get();
        final PhysicalSchema physicalSchema = PhysicalSchema.from(selection.getSchema(), materializationFormat.getKeyFeatures(), materializationFormat.getValueFeatures());
        keySerde = (Serde<K>) buildContext.buildKeySerde(materializationFormat.getKeyFormat(), physicalSchema, queryContext);
        valSerde = buildContext.buildValueSerde(materializationFormat.getValueFormat(), physicalSchema, queryContext);
        if (forceMaterialize) {
            final Stacker stacker = Stacker.of(step.getProperties().getQueryContext());
            final String stateStoreName = StreamsUtil.buildOpName(stacker.push(PROJECT_OP).getQueryContext());
            final Materialized<K, GenericRow, KeyValueStore<Bytes, byte[]>> materialized = materializedFactory.create(keySerde, valSerde, stateStoreName);
            final KTable<K, GenericRow> transFormedTable = table.getTable().transformValues(() -> new KsTransformer<>(selectMapper.getTransformer(logger)), materialized);
            return KTableHolder.materialized(transFormedTable, selection.getSchema(), table.getExecutionKeyFactory(), MaterializationInfo.builder(stateStoreName, selection.getSchema()));
        }
    } else {
        keySerde = null;
        valSerde = null;
    }
    final KTable<K, GenericRow> transFormedTable = table.getTable().transformValues(() -> new KsTransformer<>(selectMapper.getTransformer(logger)), Materialized.with(keySerde, valSerde), selectName);
    final Optional<MaterializationInfo.Builder> materialization = matBuilder.map(b -> b.map(pl -> (KsqlTransformer<Object, GenericRow>) selectMapper.getTransformer(pl), selection.getSchema(), queryContext));
    return table.withTable(transFormedTable, selection.getSchema()).withMaterialization(materialization);
}
Also used : TableSelect(io.confluent.ksql.execution.plan.TableSelect) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) KTable(org.apache.kafka.streams.kstream.KTable) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) KsqlTransformer(io.confluent.ksql.execution.transform.KsqlTransformer) QueryContext(io.confluent.ksql.execution.context.QueryContext) MaterializationInfo(io.confluent.ksql.execution.materialization.MaterializationInfo) Formats(io.confluent.ksql.execution.plan.Formats) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Bytes(org.apache.kafka.common.utils.Bytes) KTableHolder(io.confluent.ksql.execution.plan.KTableHolder) SelectValueMapper(io.confluent.ksql.execution.transform.select.SelectValueMapper) KsTransformer(io.confluent.ksql.execution.streams.transform.KsTransformer) GenericRow(io.confluent.ksql.GenericRow) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) Serde(org.apache.kafka.common.serialization.Serde) Named(org.apache.kafka.streams.kstream.Named) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) Materialized(org.apache.kafka.streams.kstream.Materialized) Optional(java.util.Optional) Selection(io.confluent.ksql.execution.transform.select.Selection) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) Named(org.apache.kafka.streams.kstream.Named) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) QueryContext(io.confluent.ksql.execution.context.QueryContext) Formats(io.confluent.ksql.execution.plan.Formats) GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) KsqlTransformer(io.confluent.ksql.execution.transform.KsqlTransformer)

Example 2 with RuntimeBuildContext

use of io.confluent.ksql.execution.runtime.RuntimeBuildContext in project ksql by confluentinc.

the class TableSuppressBuilder method build.

@VisibleForTesting
@SuppressWarnings("unchecked")
<K> KTableHolder<K> build(final KTableHolder<K> table, final TableSuppress<K> step, final RuntimeBuildContext buildContext, final ExecutionKeyFactory<K> executionKeyFactory, final PhysicalSchemaFactory physicalSchemaFactory, final BiFunction<Serde<K>, Serde<GenericRow>, Materialized> materializedFactory) {
    final PhysicalSchema physicalSchema = physicalSchemaFactory.create(table.getSchema(), step.getInternalFormats().getKeyFeatures(), step.getInternalFormats().getValueFeatures());
    final QueryContext queryContext = QueryContext.Stacker.of(step.getProperties().getQueryContext()).push(SUPPRESS_OP_NAME).getQueryContext();
    final Serde<K> keySerde = executionKeyFactory.buildKeySerde(step.getInternalFormats().getKeyFormat(), physicalSchema, queryContext);
    final Serde<GenericRow> valueSerde = buildContext.buildValueSerde(step.getInternalFormats().getValueFormat(), physicalSchema, queryContext);
    final Materialized<K, GenericRow, KeyValueStore<Bytes, byte[]>> materialized = materializedFactory.apply(keySerde, valueSerde);
    final Suppressed.StrictBufferConfig strictBufferConfig;
    final long maxBytes = buildContext.getKsqlConfig().getLong(KsqlConfig.KSQL_SUPPRESS_BUFFER_SIZE_BYTES);
    if (maxBytes < 0) {
        strictBufferConfig = Suppressed.BufferConfig.unbounded();
    } else {
        strictBufferConfig = Suppressed.BufferConfig.maxBytes(maxBytes).shutDownWhenFull();
    }
    /* This is a dummy transformValues() call, we do this to ensure that the correct materialized
    with the correct key and val serdes is passed on when we call suppress
     */
    final KTable<K, GenericRow> suppressed = table.getTable().transformValues((() -> new KsTransformer<>((k, v, ctx) -> v)), materialized).suppress((Suppressed<? super K>) Suppressed.untilWindowCloses(strictBufferConfig).withName(SUPPRESS_OP_NAME));
    return table.withTable(suppressed, table.getSchema());
}
Also used : PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) KTable(org.apache.kafka.streams.kstream.KTable) TableSuppress(io.confluent.ksql.execution.plan.TableSuppress) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) QueryContext(io.confluent.ksql.execution.context.QueryContext) BiFunction(java.util.function.BiFunction) Suppressed(org.apache.kafka.streams.kstream.Suppressed) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ExecutionKeyFactory(io.confluent.ksql.execution.plan.ExecutionKeyFactory) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Bytes(org.apache.kafka.common.utils.Bytes) KTableHolder(io.confluent.ksql.execution.plan.KTableHolder) KsTransformer(io.confluent.ksql.execution.streams.transform.KsTransformer) GenericRow(io.confluent.ksql.GenericRow) Serde(org.apache.kafka.common.serialization.Serde) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Materialized(org.apache.kafka.streams.kstream.Materialized) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) QueryContext(io.confluent.ksql.execution.context.QueryContext) GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) Suppressed(org.apache.kafka.streams.kstream.Suppressed) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with RuntimeBuildContext

use of io.confluent.ksql.execution.runtime.RuntimeBuildContext in project ksql by confluentinc.

the class TableFilterBuilder method build.

static <K> KTableHolder<K> build(final KTableHolder<K> table, final TableFilter<K> step, final RuntimeBuildContext buildContext, final SqlPredicateFactory sqlPredicateFactory) {
    final SqlPredicate predicate = sqlPredicateFactory.create(step.getFilterExpression(), table.getSchema(), buildContext.getKsqlConfig(), buildContext.getFunctionRegistry());
    final ProcessingLogger processingLogger = buildContext.getProcessingLogger(step.getProperties().getQueryContext());
    final Stacker stacker = Stacker.of(step.getProperties().getQueryContext());
    final KTable<K, GenericRow> filtered = table.getTable().transformValues(() -> new KsTransformer<>(predicate.getTransformer(processingLogger)), Named.as(StreamsUtil.buildOpName(stacker.push(PRE_PROCESS_OP).getQueryContext()))).filter((k, v) -> v.isPresent(), Named.as(StreamsUtil.buildOpName(stacker.push(FILTER_OP).getQueryContext()))).mapValues(Optional::get, Named.as(StreamsUtil.buildOpName(stacker.push(POST_PROCESS_OP).getQueryContext())));
    return table.withTable(filtered, table.getSchema()).withMaterialization(table.getMaterializationBuilder().map(b -> b.filter(predicate::getTransformer, step.getProperties().getQueryContext())));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) SqlPredicate(io.confluent.ksql.execution.transform.sqlpredicate.SqlPredicate) KTableHolder(io.confluent.ksql.execution.plan.KTableHolder) TableFilter(io.confluent.ksql.execution.plan.TableFilter) KTable(org.apache.kafka.streams.kstream.KTable) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) KsTransformer(io.confluent.ksql.execution.streams.transform.KsTransformer) GenericRow(io.confluent.ksql.GenericRow) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) Named(org.apache.kafka.streams.kstream.Named) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) Optional(java.util.Optional) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) Optional(java.util.Optional) SqlPredicate(io.confluent.ksql.execution.transform.sqlpredicate.SqlPredicate) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker)

Example 4 with RuntimeBuildContext

use of io.confluent.ksql.execution.runtime.RuntimeBuildContext in project ksql by confluentinc.

the class SourceBuilderV1 method buildKTable.

@Override
<K> KTable<K, GenericRow> buildKTable(final SourceStep<?> streamSource, final RuntimeBuildContext buildContext, final Consumed<K, GenericRow> consumed, final Function<K, Collection<?>> keyGenerator, final Materialized<K, GenericRow, KeyValueStore<Bytes, byte[]>> materialized, final Serde<GenericRow> valueSerde, final String stateStoreName, final PlanInfo planInfo) {
    validateNotUsingOldExecutionStepWithNewQueries(streamSource);
    final boolean forceChangelog = streamSource instanceof TableSourceV1 && ((TableSourceV1) streamSource).isForceChangelog();
    final KTable<K, GenericRow> table;
    if (!forceChangelog) {
        final String changelogTopic = changelogTopic(buildContext, stateStoreName);
        final Callback onFailure = getRegisterCallback(buildContext, streamSource.getFormats().getValueFormat());
        table = buildContext.getStreamsBuilder().table(streamSource.getTopicName(), consumed.withValueSerde(StaticTopicSerde.wrap(changelogTopic, valueSerde, onFailure)), materialized);
    } else {
        final KTable<K, GenericRow> source = buildContext.getStreamsBuilder().table(streamSource.getTopicName(), consumed);
        final boolean forceMaterialization = !planInfo.isRepartitionedInPlan(streamSource);
        if (forceMaterialization) {
            // add this identity mapValues call to prevent the source-changelog
            // optimization in kafka streams - we don't want this optimization to
            // be enabled because we cannot require symmetric serialization between
            // producer and KSQL (see https://issues.apache.org/jira/browse/KAFKA-10179
            // and https://github.com/confluentinc/ksql/issues/5673 for more details)
            table = source.mapValues(row -> row, materialized);
        } else {
            // if we know this table source is repartitioned later in the topology,
            // we do not need to force a materialization at this source step since the
            // re-partitioned topic will be used for any subsequent state stores, in lieu
            // of the original source topic, thus avoiding the issues above.
            // See https://github.com/confluentinc/ksql/issues/6650
            table = source.mapValues(row -> row);
        }
    }
    return table.transformValues(new AddKeyAndPseudoColumns<>(keyGenerator, streamSource.getPseudoColumnVersion(), streamSource.getSourceSchema().headers()));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) SourceBuilderUtils.getRegisterCallback(io.confluent.ksql.execution.streams.SourceBuilderUtils.getRegisterCallback) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) PlanInfo(io.confluent.ksql.execution.plan.PlanInfo) SourceBuilderUtils.getKeySerde(io.confluent.ksql.execution.streams.SourceBuilderUtils.getKeySerde) SourceBuilderUtils.getWindowedKeySerde(io.confluent.ksql.execution.streams.SourceBuilderUtils.getWindowedKeySerde) SourceBuilderUtils.buildSourceConsumed(io.confluent.ksql.execution.streams.SourceBuilderUtils.buildSourceConsumed) SourceBuilderUtils.getPhysicalSchema(io.confluent.ksql.execution.streams.SourceBuilderUtils.getPhysicalSchema) MaterializationInfo(io.confluent.ksql.execution.materialization.MaterializationInfo) AutoOffsetReset(org.apache.kafka.streams.Topology.AutoOffsetReset) KStream(org.apache.kafka.streams.kstream.KStream) Function(java.util.function.Function) WindowInfo(io.confluent.ksql.serde.WindowInfo) AddKeyAndPseudoColumns(io.confluent.ksql.execution.streams.SourceBuilderUtils.AddKeyAndPseudoColumns) KTableHolder(io.confluent.ksql.execution.plan.KTableHolder) SourceStep(io.confluent.ksql.execution.plan.SourceStep) SourceBuilderUtils.getValueSerde(io.confluent.ksql.execution.streams.SourceBuilderUtils.getValueSerde) Windowed(org.apache.kafka.streams.kstream.Windowed) Serde(org.apache.kafka.common.serialization.Serde) SourceBuilderUtils.changelogTopic(io.confluent.ksql.execution.streams.SourceBuilderUtils.changelogTopic) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) StaticTopicSerde(io.confluent.ksql.serde.StaticTopicSerde) KTable(org.apache.kafka.streams.kstream.KTable) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) SourceBuilderUtils.buildSchema(io.confluent.ksql.execution.streams.SourceBuilderUtils.buildSchema) SourceBuilderUtils.windowedKeyGenerator(io.confluent.ksql.execution.streams.SourceBuilderUtils.windowedKeyGenerator) Consumed(org.apache.kafka.streams.kstream.Consumed) Collection(java.util.Collection) SourceBuilderUtils.tableChangeLogOpName(io.confluent.ksql.execution.streams.SourceBuilderUtils.tableChangeLogOpName) ExecutionKeyFactory(io.confluent.ksql.execution.plan.ExecutionKeyFactory) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) StreamSource(io.confluent.ksql.execution.plan.StreamSource) Bytes(org.apache.kafka.common.utils.Bytes) KStreamHolder(io.confluent.ksql.execution.plan.KStreamHolder) WindowedStreamSource(io.confluent.ksql.execution.plan.WindowedStreamSource) TableSourceV1(io.confluent.ksql.execution.plan.TableSourceV1) WindowedTableSource(io.confluent.ksql.execution.plan.WindowedTableSource) GenericRow(io.confluent.ksql.GenericRow) Callback(io.confluent.ksql.serde.StaticTopicSerde.Callback) Materialized(org.apache.kafka.streams.kstream.Materialized) KsqlException(io.confluent.ksql.util.KsqlException) GenericKey(io.confluent.ksql.GenericKey) TableSourceV1(io.confluent.ksql.execution.plan.TableSourceV1) SourceBuilderUtils.getRegisterCallback(io.confluent.ksql.execution.streams.SourceBuilderUtils.getRegisterCallback) Callback(io.confluent.ksql.serde.StaticTopicSerde.Callback)

Example 5 with RuntimeBuildContext

use of io.confluent.ksql.execution.runtime.RuntimeBuildContext in project ksql by confluentinc.

the class SinkBuilder method build.

public static <K> void build(final LogicalSchema schema, final Formats formats, final Optional<TimestampColumn> timestampColumn, final String topicName, final KStream<K, GenericRow> stream, final ExecutionKeyFactory<K> executionKeyFactory, final QueryContext queryContext, final RuntimeBuildContext buildContext) {
    final PhysicalSchema physicalSchema = PhysicalSchema.from(schema, formats.getKeyFeatures(), formats.getValueFeatures());
    final Serde<K> keySerde = executionKeyFactory.buildKeySerde(formats.getKeyFormat(), physicalSchema, queryContext);
    final Serde<GenericRow> valueSerde = buildContext.buildValueSerde(formats.getValueFormat(), physicalSchema, queryContext);
    final Optional<TransformTimestamp<K>> tsTransformer = timestampTransformer(buildContext, queryContext, schema, timestampColumn);
    final KStream<K, GenericRow> transformed = tsTransformer.map(t -> stream.transform(t, Named.as(TIMESTAMP_TRANSFORM_NAME + StreamsUtil.buildOpName(queryContext)))).orElse(stream);
    transformed.to(topicName, Produced.with(keySerde, valueSerde));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) Produced(org.apache.kafka.streams.kstream.Produced) Transformer(org.apache.kafka.streams.kstream.Transformer) QueryContext(io.confluent.ksql.execution.context.QueryContext) KeyValue(org.apache.kafka.streams.KeyValue) KStream(org.apache.kafka.streams.kstream.KStream) ExecutionKeyFactory(io.confluent.ksql.execution.plan.ExecutionKeyFactory) Formats(io.confluent.ksql.execution.plan.Formats) KsqlTimestampExtractor(io.confluent.ksql.execution.streams.timestamp.KsqlTimestampExtractor) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) To(org.apache.kafka.streams.processor.To) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) TimestampExtractionPolicy(io.confluent.ksql.execution.streams.timestamp.TimestampExtractionPolicy) TransformerSupplier(org.apache.kafka.streams.kstream.TransformerSupplier) GenericRow(io.confluent.ksql.GenericRow) TimestampExtractionPolicyFactory(io.confluent.ksql.execution.streams.timestamp.TimestampExtractionPolicyFactory) Serde(org.apache.kafka.common.serialization.Serde) Objects.requireNonNull(java.util.Objects.requireNonNull) Named(org.apache.kafka.streams.kstream.Named) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) Optional(java.util.Optional) RecordProcessingError(io.confluent.ksql.logging.processing.RecordProcessingError) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema)

Aggregations

RuntimeBuildContext (io.confluent.ksql.execution.runtime.RuntimeBuildContext)11 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)7 GenericRow (io.confluent.ksql.GenericRow)6 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)6 KTableHolder (io.confluent.ksql.execution.plan.KTableHolder)5 KTable (org.apache.kafka.streams.kstream.KTable)5 ExecutionKeyFactory (io.confluent.ksql.execution.plan.ExecutionKeyFactory)4 ProcessingLogger (io.confluent.ksql.logging.processing.ProcessingLogger)4 Serde (org.apache.kafka.common.serialization.Serde)4 Materialized (org.apache.kafka.streams.kstream.Materialized)4 QueryContext (io.confluent.ksql.execution.context.QueryContext)3 KsTransformer (io.confluent.ksql.execution.streams.transform.KsTransformer)3 Optional (java.util.Optional)3 Bytes (org.apache.kafka.common.utils.Bytes)3 KStream (org.apache.kafka.streams.kstream.KStream)3 Named (org.apache.kafka.streams.kstream.Named)3 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)3 GenericKey (io.confluent.ksql.GenericKey)2 Stacker (io.confluent.ksql.execution.context.QueryContext.Stacker)2 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)2