Search in sources :

Example 6 with QueryContext

use of io.confluent.ksql.execution.context.QueryContext in project ksql by confluentinc.

the class MaterializationUtil method buildMaterialized.

static <K> Materialized<K, GenericRow, KeyValueStore<Bytes, byte[]>> buildMaterialized(final ExecutionStep<?> step, final LogicalSchema aggregateSchema, final Formats formats, final RuntimeBuildContext buildContext, final MaterializedFactory materializedFactory, final ExecutionKeyFactory<K> executionKeyFactory) {
    final PhysicalSchema physicalAggregationSchema = PhysicalSchema.from(aggregateSchema, formats.getKeyFeatures(), formats.getValueFeatures());
    final QueryContext queryContext = MaterializationUtil.materializeContext(step);
    final Serde<K> keySerde = buildKeySerde(formats, physicalAggregationSchema, queryContext, executionKeyFactory);
    final Serde<GenericRow> valueSerde = buildValueSerde(formats, buildContext, physicalAggregationSchema, queryContext);
    return materializedFactory.create(keySerde, valueSerde, StreamsUtil.buildOpName(queryContext));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) QueryContext(io.confluent.ksql.execution.context.QueryContext)

Example 7 with QueryContext

use of io.confluent.ksql.execution.context.QueryContext 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)

Example 8 with QueryContext

use of io.confluent.ksql.execution.context.QueryContext in project ksql by confluentinc.

the class StreamSelectBuilder method build.

public static <K> KStreamHolder<K> build(final KStreamHolder<K> stream, final StreamSelect<K> step, final RuntimeBuildContext buildContext) {
    final QueryContext queryContext = step.getProperties().getQueryContext();
    final LogicalSchema sourceSchema = stream.getSchema();
    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));
    return stream.withStream(stream.getStream().transformValues(() -> new KsTransformer<>(selectMapper.getTransformer(logger)), selectName), selection.getSchema());
}
Also used : ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) Named(org.apache.kafka.streams.kstream.Named) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsTransformer(io.confluent.ksql.execution.streams.transform.KsTransformer) QueryContext(io.confluent.ksql.execution.context.QueryContext)

Example 9 with QueryContext

use of io.confluent.ksql.execution.context.QueryContext in project ksql by confluentinc.

the class TableSelectKeyBuilder method build.

@VisibleForTesting
static <K> KTableHolder<K> build(final KTableHolder<K> table, final TableSelectKey<K> selectKey, final RuntimeBuildContext buildContext, final MaterializedFactory materializedFactory, final PartitionByParamsBuilder paramsBuilder) {
    final LogicalSchema sourceSchema = table.getSchema();
    final QueryContext queryContext = selectKey.getProperties().getQueryContext();
    final ProcessingLogger logger = buildContext.getProcessingLogger(queryContext);
    final PartitionByParams<K> params = paramsBuilder.build(sourceSchema, table.getExecutionKeyFactory(), selectKey.getKeyExpressions(), buildContext.getKsqlConfig(), buildContext.getFunctionRegistry(), logger);
    final Mapper<K> mapper = params.getMapper();
    final KTable<K, GenericRow> kTable = table.getTable();
    final Materialized<K, GenericRow, KeyValueStore<Bytes, byte[]>> materialized = MaterializationUtil.buildMaterialized(selectKey, params.getSchema(), selectKey.getInternalFormats(), buildContext, materializedFactory, table.getExecutionKeyFactory());
    final KTable<K, GenericRow> reKeyed = kTable.toStream().map(mapper, Named.as(queryContext.formatContext() + "-SelectKey-Mapper")).toTable(Named.as(queryContext.formatContext() + "-SelectKey"), materialized);
    final MaterializationInfo.Builder materializationBuilder = MaterializationInfo.builder(StreamsUtil.buildOpName(MaterializationUtil.materializeContext(selectKey)), params.getSchema());
    return KTableHolder.materialized(reKeyed, params.getSchema(), table.getExecutionKeyFactory().withQueryBuilder(buildContext), materializationBuilder);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) MaterializationInfo(io.confluent.ksql.execution.materialization.MaterializationInfo) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) QueryContext(io.confluent.ksql.execution.context.QueryContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with QueryContext

use of io.confluent.ksql.execution.context.QueryContext in project ksql by confluentinc.

the class StreamFlatMapBuilder method build.

public static <K> KStreamHolder<K> build(final KStreamHolder<K> stream, final StreamFlatMap<K> step, final RuntimeBuildContext buildContext) {
    final List<FunctionCall> tableFunctions = step.getTableFunctions();
    final LogicalSchema schema = stream.getSchema();
    final Builder<TableFunctionApplier> tableFunctionAppliersBuilder = ImmutableList.builder();
    final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, buildContext.getKsqlConfig(), buildContext.getFunctionRegistry());
    for (final FunctionCall functionCall : tableFunctions) {
        final List<CompiledExpression> compiledExpressionList = new ArrayList<>(functionCall.getArguments().size());
        for (final Expression expression : functionCall.getArguments()) {
            final CompiledExpression compiledExpression = codeGenRunner.buildCodeGenFromParseTree(expression, "Table function");
            compiledExpressionList.add(compiledExpression);
        }
        final KsqlTableFunction tableFunction = UdtfUtil.resolveTableFunction(buildContext.getFunctionRegistry(), functionCall, schema);
        final TableFunctionApplier tableFunctionApplier = new TableFunctionApplier(tableFunction, compiledExpressionList);
        tableFunctionAppliersBuilder.add(tableFunctionApplier);
    }
    final QueryContext queryContext = step.getProperties().getQueryContext();
    final ProcessingLogger processingLogger = buildContext.getProcessingLogger(queryContext);
    final ImmutableList<TableFunctionApplier> tableFunctionAppliers = tableFunctionAppliersBuilder.build();
    final KStream<K, GenericRow> mapped = stream.getStream().flatTransformValues(() -> new KsTransformer<>(new KudtfFlatMapper<>(tableFunctionAppliers, processingLogger)), Named.as(StreamsUtil.buildOpName(queryContext)));
    return stream.withStream(mapped, buildSchema(stream.getSchema(), step.getTableFunctions(), buildContext.getFunctionRegistry()));
}
Also used : ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) ArrayList(java.util.ArrayList) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) QueryContext(io.confluent.ksql.execution.context.QueryContext) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) GenericRow(io.confluent.ksql.GenericRow) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) KsqlTableFunction(io.confluent.ksql.function.KsqlTableFunction) TableFunctionApplier(io.confluent.ksql.execution.function.udtf.TableFunctionApplier) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) KudtfFlatMapper(io.confluent.ksql.execution.function.udtf.KudtfFlatMapper)

Aggregations

QueryContext (io.confluent.ksql.execution.context.QueryContext)22 GenericRow (io.confluent.ksql.GenericRow)13 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)12 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)10 ProcessingLogger (io.confluent.ksql.logging.processing.ProcessingLogger)8 Formats (io.confluent.ksql.execution.plan.Formats)7 Test (org.junit.Test)7 RuntimeBuildContext (io.confluent.ksql.execution.runtime.RuntimeBuildContext)5 Serde (org.apache.kafka.common.serialization.Serde)5 GenericKey (io.confluent.ksql.GenericKey)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 CodeGenRunner (io.confluent.ksql.execution.codegen.CodeGenRunner)3 CompiledExpression (io.confluent.ksql.execution.codegen.CompiledExpression)3 Expression (io.confluent.ksql.execution.expression.tree.Expression)3 KTableHolder (io.confluent.ksql.execution.plan.KTableHolder)3 KsTransformer (io.confluent.ksql.execution.streams.transform.KsTransformer)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Named (org.apache.kafka.streams.kstream.Named)3 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)3 MaterializationInfo (io.confluent.ksql.execution.materialization.MaterializationInfo)2