Search in sources :

Example 16 with QueryContext

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

the class StreamSelectKeyBuilder method build.

@VisibleForTesting
static <K> KStreamHolder<K> build(final KStreamHolder<K> stream, final StreamSelectKey<K> selectKey, final RuntimeBuildContext buildContext, final PartitionByParamsBuilder paramsBuilder) {
    final LogicalSchema sourceSchema = stream.getSchema();
    final QueryContext queryContext = selectKey.getProperties().getQueryContext();
    final ProcessingLogger logger = buildContext.getProcessingLogger(queryContext);
    final PartitionByParams<K> params = paramsBuilder.build(sourceSchema, stream.getExecutionKeyFactory(), selectKey.getKeyExpressions(), buildContext.getKsqlConfig(), buildContext.getFunctionRegistry(), logger);
    final Mapper<K> mapper = params.getMapper();
    final KStream<K, GenericRow> kStream = stream.getStream();
    final KStream<K, GenericRow> reKeyed = kStream.map(mapper, Named.as(queryContext.formatContext() + "-SelectKey"));
    return new KStreamHolder<>(reKeyed, params.getSchema(), stream.getExecutionKeyFactory().withQueryBuilder(buildContext));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) KStreamHolder(io.confluent.ksql.execution.plan.KStreamHolder) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) QueryContext(io.confluent.ksql.execution.context.QueryContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 17 with QueryContext

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

the class StreamTableJoinBuilder method build.

public static <K> KStreamHolder<K> build(final KStreamHolder<K> left, final KTableHolder<K> right, final StreamTableJoin<K> join, final RuntimeBuildContext buildContext, final JoinedFactory joinedFactory) {
    final Formats leftFormats = join.getInternalFormats();
    final QueryContext queryContext = join.getProperties().getQueryContext();
    final QueryContext.Stacker stacker = QueryContext.Stacker.of(queryContext);
    final LogicalSchema leftSchema = left.getSchema();
    final PhysicalSchema leftPhysicalSchema = PhysicalSchema.from(leftSchema, leftFormats.getKeyFeatures(), leftFormats.getValueFeatures());
    final Serde<GenericRow> leftSerde = buildContext.buildValueSerde(leftFormats.getValueFormat(), leftPhysicalSchema, stacker.push(SERDE_CTX).getQueryContext());
    final Serde<K> keySerde = left.getExecutionKeyFactory().buildKeySerde(leftFormats.getKeyFormat(), leftPhysicalSchema, queryContext);
    final Joined<K, GenericRow, GenericRow> joined = joinedFactory.create(keySerde, leftSerde, null, StreamsUtil.buildOpName(queryContext));
    final LogicalSchema rightSchema = right.getSchema();
    final JoinParams joinParams = JoinParamsFactory.create(join.getKeyColName(), leftSchema, rightSchema);
    final KStream<K, GenericRow> result;
    switch(join.getJoinType()) {
        case LEFT:
            result = left.getStream().leftJoin(right.getTable(), joinParams.getJoiner(), joined);
            break;
        case INNER:
            result = left.getStream().join(right.getTable(), joinParams.getJoiner(), joined);
            break;
        default:
            throw new IllegalStateException("invalid join type");
    }
    return left.withStream(result, joinParams.getSchema());
}
Also used : LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Formats(io.confluent.ksql.execution.plan.Formats) QueryContext(io.confluent.ksql.execution.context.QueryContext) GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema)

Example 18 with QueryContext

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

the class StreamGroupByBuilderBase method build.

public KGroupedStreamHolder build(final KStreamHolder<GenericKey> stream, final StreamGroupByKey step) {
    final LogicalSchema sourceSchema = stream.getSchema();
    final QueryContext queryContext = step.getProperties().getQueryContext();
    final Formats formats = step.getInternalFormats();
    final Grouped<GenericKey, GenericRow> grouped = buildGrouped(formats, sourceSchema, queryContext, buildContext, groupedFactory);
    return KGroupedStreamHolder.of(stream.getStream().groupByKey(grouped), stream.getSchema());
}
Also used : GenericRow(io.confluent.ksql.GenericRow) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) QueryContext(io.confluent.ksql.execution.context.QueryContext) Formats(io.confluent.ksql.execution.plan.Formats) GenericKey(io.confluent.ksql.GenericKey)

Example 19 with QueryContext

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

the class StreamGroupByBuilderBase method build.

public <K> KGroupedStreamHolder build(final KStreamHolder<K> stream, final QueryContext queryContext, final Formats formats, final List<Expression> groupByExpressions) {
    final LogicalSchema sourceSchema = stream.getSchema();
    final List<CompiledExpression> groupBy = CodeGenRunner.compileExpressions(groupByExpressions.stream(), "Group By", sourceSchema, buildContext.getKsqlConfig(), buildContext.getFunctionRegistry());
    final ProcessingLogger logger = buildContext.getProcessingLogger(queryContext);
    final GroupByParams params = paramsFactory.build(sourceSchema, groupBy, logger);
    final Grouped<GenericKey, GenericRow> grouped = buildGrouped(formats, params.getSchema(), queryContext, buildContext, groupedFactory);
    final KGroupedStream<GenericKey, GenericRow> groupedStream = stream.getStream().filter((k, v) -> v != null).groupBy((k, v) -> params.getMapper().apply(v), grouped);
    return KGroupedStreamHolder.of(groupedStream, params.getSchema());
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) KGroupedStream(org.apache.kafka.streams.kstream.KGroupedStream) Expression(io.confluent.ksql.execution.expression.tree.Expression) QueryContext(io.confluent.ksql.execution.context.QueryContext) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) Formats(io.confluent.ksql.execution.plan.Formats) StreamGroupByKey(io.confluent.ksql.execution.plan.StreamGroupByKey) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) Grouped(org.apache.kafka.streams.kstream.Grouped) KGroupedStreamHolder(io.confluent.ksql.execution.plan.KGroupedStreamHolder) KStreamHolder(io.confluent.ksql.execution.plan.KStreamHolder) List(java.util.List) GenericRow(io.confluent.ksql.GenericRow) Serde(org.apache.kafka.common.serialization.Serde) Objects.requireNonNull(java.util.Objects.requireNonNull) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) GenericKey(io.confluent.ksql.GenericKey) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) GenericKey(io.confluent.ksql.GenericKey) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression)

Example 20 with QueryContext

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

the class StreamStreamJoinBuilder method build.

// deprecation can be fixed after GRACE clause is mandatory
// (cf. `WithinExpression`)
@SuppressWarnings("deprecation")
public static <K> KStreamHolder<K> build(final KStreamHolder<K> left, final KStreamHolder<K> right, final StreamStreamJoin<K> join, final RuntimeBuildContext buildContext, final StreamJoinedFactory streamJoinedFactory) {
    final Formats leftFormats = join.getLeftInternalFormats();
    final QueryContext queryContext = join.getProperties().getQueryContext();
    final QueryContext.Stacker stacker = QueryContext.Stacker.of(queryContext);
    final LogicalSchema leftSchema = left.getSchema();
    final PhysicalSchema leftPhysicalSchema = PhysicalSchema.from(leftSchema, leftFormats.getKeyFeatures(), leftFormats.getValueFeatures());
    final Serde<GenericRow> leftSerde = buildContext.buildValueSerde(leftFormats.getValueFormat(), leftPhysicalSchema, stacker.push(LEFT_SERDE_CTX).getQueryContext());
    final Formats rightFormats = join.getRightInternalFormats();
    final LogicalSchema rightSchema = right.getSchema();
    final PhysicalSchema rightPhysicalSchema = PhysicalSchema.from(rightSchema, rightFormats.getKeyFeatures(), rightFormats.getValueFeatures());
    final Serde<GenericRow> rightSerde = buildContext.buildValueSerde(rightFormats.getValueFormat(), rightPhysicalSchema, stacker.push(RIGHT_SERDE_CTX).getQueryContext());
    final Serde<K> keySerde = left.getExecutionKeyFactory().buildKeySerde(leftFormats.getKeyFormat(), leftPhysicalSchema, queryContext);
    final StreamJoined<K, GenericRow, GenericRow> joined = streamJoinedFactory.create(keySerde, leftSerde, rightSerde, StreamsUtil.buildOpName(queryContext), StreamsUtil.buildOpName(queryContext));
    final JoinParams joinParams = JoinParamsFactory.create(join.getKeyColName(), leftSchema, rightSchema);
    JoinWindows joinWindows;
    // which enables the "spurious" results bugfix with left/outer joins (see KAFKA-10847).
    if (join.getGraceMillis().isPresent()) {
        joinWindows = JoinWindows.ofTimeDifferenceAndGrace(join.getBeforeMillis(), join.getGraceMillis().get());
    } else {
        joinWindows = JoinWindows.of(join.getBeforeMillis());
    }
    joinWindows = joinWindows.after(join.getAfterMillis());
    final KStream<K, GenericRow> result;
    switch(join.getJoinType()) {
        case LEFT:
            result = left.getStream().leftJoin(right.getStream(), joinParams.getJoiner(), joinWindows, joined);
            break;
        case OUTER:
            result = left.getStream().outerJoin(right.getStream(), joinParams.getJoiner(), joinWindows, joined);
            break;
        case INNER:
            result = left.getStream().join(right.getStream(), joinParams.getJoiner(), joinWindows, joined);
            break;
        default:
            throw new IllegalStateException("invalid join type");
    }
    return left.withStream(result, joinParams.getSchema());
}
Also used : LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Formats(io.confluent.ksql.execution.plan.Formats) QueryContext(io.confluent.ksql.execution.context.QueryContext) GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) JoinWindows(org.apache.kafka.streams.kstream.JoinWindows)

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