Search in sources :

Example 1 with ExecutionKeyFactory

use of io.confluent.ksql.execution.plan.ExecutionKeyFactory 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 2 with ExecutionKeyFactory

use of io.confluent.ksql.execution.plan.ExecutionKeyFactory in project ksql by confluentinc.

the class PartitionByParamsFactory method build.

public static <K> PartitionByParams<K> build(final LogicalSchema sourceSchema, final ExecutionKeyFactory<K> serdeFactory, final List<Expression> partitionBys, final KsqlConfig ksqlConfig, final FunctionRegistry functionRegistry, final ProcessingLogger logger) {
    final List<PartitionByColumn> partitionByCols = getPartitionByColumnName(sourceSchema, partitionBys);
    final LogicalSchema resultSchema = buildSchema(sourceSchema, partitionBys, functionRegistry, partitionByCols);
    final Mapper<K> mapper;
    if (isPartitionByNull(partitionBys)) {
        // In case of PARTITION BY NULL, it is sufficient to set the new key to null as the old key
        // is already present in the current value
        mapper = (k, v) -> new KeyValue<>(null, v);
    } else {
        final List<PartitionByExpressionEvaluator> evaluators = partitionBys.stream().map(pby -> {
            final Set<? extends ColumnReferenceExp> sourceColsInPartitionBy = ColumnExtractor.extractColumns(pby);
            final boolean partitionByInvolvesKeyColsOnly = sourceColsInPartitionBy.stream().map(ColumnReferenceExp::getColumnName).allMatch(sourceSchema::isKeyColumn);
            return buildExpressionEvaluator(sourceSchema, pby, ksqlConfig, functionRegistry, logger, partitionByInvolvesKeyColsOnly);
        }).collect(Collectors.toList());
        mapper = buildMapper(partitionByCols, evaluators, serdeFactory);
    }
    return new PartitionByParams<>(resultSchema, mapper);
}
Also used : ColumnExtractor(io.confluent.ksql.execution.util.ColumnExtractor) ColumnName(io.confluent.ksql.name.ColumnName) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) KeyUtil(io.confluent.ksql.execution.util.KeyUtil) Supplier(java.util.function.Supplier) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) Mapper(io.confluent.ksql.execution.streams.PartitionByParams.Mapper) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) ColumnNames(io.confluent.ksql.schema.ksql.ColumnNames) Expression(io.confluent.ksql.execution.expression.tree.Expression) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) KeyValue(org.apache.kafka.streams.KeyValue) Set(java.util.Set) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ExecutionKeyFactory(io.confluent.ksql.execution.plan.ExecutionKeyFactory) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) Objects(java.util.Objects) List(java.util.List) ColumnAliasGenerator(io.confluent.ksql.schema.ksql.ColumnAliasGenerator) Stream(java.util.stream.Stream) GenericRow(io.confluent.ksql.GenericRow) KsqlException(io.confluent.ksql.util.KsqlException) GenericKey(io.confluent.ksql.GenericKey) Column(io.confluent.ksql.schema.ksql.Column) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) Set(java.util.Set) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema)

Example 3 with ExecutionKeyFactory

use of io.confluent.ksql.execution.plan.ExecutionKeyFactory 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

GenericRow (io.confluent.ksql.GenericRow)3 ExecutionKeyFactory (io.confluent.ksql.execution.plan.ExecutionKeyFactory)3 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)3 QueryContext (io.confluent.ksql.execution.context.QueryContext)2 RuntimeBuildContext (io.confluent.ksql.execution.runtime.RuntimeBuildContext)2 ProcessingLogger (io.confluent.ksql.logging.processing.ProcessingLogger)2 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)2 KsqlConfig (io.confluent.ksql.util.KsqlConfig)2 Serde (org.apache.kafka.common.serialization.Serde)2 KeyValue (org.apache.kafka.streams.KeyValue)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 GenericKey (io.confluent.ksql.GenericKey)1 CodeGenRunner (io.confluent.ksql.execution.codegen.CodeGenRunner)1 CompiledExpression (io.confluent.ksql.execution.codegen.CompiledExpression)1 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)1 Expression (io.confluent.ksql.execution.expression.tree.Expression)1 NullLiteral (io.confluent.ksql.execution.expression.tree.NullLiteral)1 Formats (io.confluent.ksql.execution.plan.Formats)1 KTableHolder (io.confluent.ksql.execution.plan.KTableHolder)1 TableSuppress (io.confluent.ksql.execution.plan.TableSuppress)1