Search in sources :

Example 1 with Stacker

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

the class KsqlMaterializationFactoryTest method shouldUseCorrectLoggerForSelectMapper.

@Test
public void shouldUseCorrectLoggerForSelectMapper() {
    // When:
    factory.create(materialization, info, queryId, new Stacker().push("project"));
    // Then:
    verify(mapperInfo).getMapper(loggerCaptor.capture());
    assertThat(loggerCaptor.getValue().apply(new Stacker().getQueryContext()), is(mapProcessingLogger));
}
Also used : Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) Test(org.junit.Test)

Example 2 with Stacker

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

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

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

the class SourceBuilderUtils method tableChangeLogOpName.

static String tableChangeLogOpName(final ExecutionStepPropertiesV1 props) {
    final List<String> parts = props.getQueryContext().getContext();
    Stacker stacker = new Stacker();
    for (final String part : parts.subList(0, parts.size() - 1)) {
        stacker = stacker.push(part);
    }
    return StreamsUtil.buildOpName(stacker.push("Reduce").getQueryContext());
}
Also used : Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker)

Example 5 with Stacker

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

the class SchemaKGroupedTable method aggregate.

@Override
public SchemaKTable<GenericKey> aggregate(final List<ColumnName> nonAggregateColumns, final List<FunctionCall> aggregations, final Optional<WindowExpression> windowExpression, final FormatInfo valueFormat, final Stacker contextStacker) {
    if (windowExpression.isPresent()) {
        throw new KsqlException("Windowing not supported for table aggregations.");
    }
    final List<String> unsupportedFunctionNames = aggregations.stream().map(call -> UdafUtil.resolveAggregateFunction(functionRegistry, call, schema, ksqlConfig)).filter(function -> !(function instanceof TableAggregationFunction)).map(KsqlAggregateFunction::name).map(FunctionName::text).distinct().collect(Collectors.toList());
    if (!unsupportedFunctionNames.isEmpty()) {
        final String postfix = unsupportedFunctionNames.size() == 1 ? "" : "s";
        throw new KsqlException("The aggregation function" + postfix + " " + GrammaticalJoiner.and().join(unsupportedFunctionNames) + " cannot be applied to a table source, only to a stream source.");
    }
    final TableAggregate step = ExecutionStepFactory.tableAggregate(contextStacker, sourceTableStep, InternalFormats.of(keyFormat, valueFormat), nonAggregateColumns, aggregations);
    return new SchemaKTable<>(step, resolveSchema(step), keyFormat, ksqlConfig, functionRegistry);
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) GrammaticalJoiner(io.confluent.ksql.util.GrammaticalJoiner) KeyFormat(io.confluent.ksql.serde.KeyFormat) KGroupedTableHolder(io.confluent.ksql.execution.plan.KGroupedTableHolder) KsqlAggregateFunction(io.confluent.ksql.function.KsqlAggregateFunction) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) ExecutionStepFactory(io.confluent.ksql.execution.streams.ExecutionStepFactory) ExecutionStep(io.confluent.ksql.execution.plan.ExecutionStep) FunctionName(io.confluent.ksql.name.FunctionName) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) KsqlConfig(io.confluent.ksql.util.KsqlConfig) InternalFormats(io.confluent.ksql.serde.InternalFormats) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) UdafUtil(io.confluent.ksql.execution.function.UdafUtil) Objects(java.util.Objects) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) List(java.util.List) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) TableAggregationFunction(io.confluent.ksql.execution.function.TableAggregationFunction) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) FormatInfo(io.confluent.ksql.serde.FormatInfo) FunctionName(io.confluent.ksql.name.FunctionName) TableAggregate(io.confluent.ksql.execution.plan.TableAggregate) TableAggregationFunction(io.confluent.ksql.execution.function.TableAggregationFunction) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

Stacker (io.confluent.ksql.execution.context.QueryContext.Stacker)9 KTableHolder (io.confluent.ksql.execution.plan.KTableHolder)3 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)3 Optional (java.util.Optional)3 Test (org.junit.Test)3 GenericKey (io.confluent.ksql.GenericKey)2 GenericRow (io.confluent.ksql.GenericRow)2 QueryContext (io.confluent.ksql.execution.context.QueryContext)2 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)2 ExecutionStep (io.confluent.ksql.execution.plan.ExecutionStep)2 Formats (io.confluent.ksql.execution.plan.Formats)2 RuntimeBuildContext (io.confluent.ksql.execution.runtime.RuntimeBuildContext)2 ExecutionStepFactory (io.confluent.ksql.execution.streams.ExecutionStepFactory)2 KsTransformer (io.confluent.ksql.execution.streams.transform.KsTransformer)2 ProcessingLogger (io.confluent.ksql.logging.processing.ProcessingLogger)2 ColumnName (io.confluent.ksql.name.ColumnName)2 InternalFormats (io.confluent.ksql.serde.InternalFormats)2 KeyFormat (io.confluent.ksql.serde.KeyFormat)2 KsqlConfig (io.confluent.ksql.util.KsqlConfig)2 KTable (org.apache.kafka.streams.kstream.KTable)2