Search in sources :

Example 1 with CompiledExpression

use of io.confluent.ksql.execution.codegen.CompiledExpression in project ksql by confluentinc.

the class TableGroupByBuilderBase method build.

public <K> KGroupedTableHolder build(final KTableHolder<K> table, final QueryContext queryContext, final Formats formats, final List<Expression> groupByExpressions) {
    final LogicalSchema sourceSchema = table.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 PhysicalSchema physicalSchema = PhysicalSchema.from(params.getSchema(), formats.getKeyFeatures(), formats.getValueFeatures());
    final Serde<GenericKey> keySerde = buildContext.buildKeySerde(formats.getKeyFormat(), physicalSchema, queryContext);
    final Serde<GenericRow> valSerde = buildContext.buildValueSerde(formats.getValueFormat(), physicalSchema, queryContext);
    final Grouped<GenericKey, GenericRow> grouped = groupedFactory.create(StreamsUtil.buildOpName(queryContext), keySerde, valSerde);
    final KGroupedTable<GenericKey, GenericRow> groupedTable = table.getTable().filter((k, v) -> v != null).groupBy(new TableKeyValueMapper<>(params.getMapper()), grouped);
    return KGroupedTableHolder.of(groupedTable, params.getSchema());
}
Also used : PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) Expression(io.confluent.ksql.execution.expression.tree.Expression) QueryContext(io.confluent.ksql.execution.context.QueryContext) KeyValue(org.apache.kafka.streams.KeyValue) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) Formats(io.confluent.ksql.execution.plan.Formats) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Function(java.util.function.Function) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) Grouped(org.apache.kafka.streams.kstream.Grouped) KGroupedTableHolder(io.confluent.ksql.execution.plan.KGroupedTableHolder) KTableHolder(io.confluent.ksql.execution.plan.KTableHolder) List(java.util.List) KGroupedTable(org.apache.kafka.streams.kstream.KGroupedTable) 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) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) GenericKey(io.confluent.ksql.GenericKey)

Example 2 with CompiledExpression

use of io.confluent.ksql.execution.codegen.CompiledExpression in project ksql by confluentinc.

the class StreamSelectKeyBuilderV1 method build.

public static KStreamHolder<GenericKey> build(final KStreamHolder<?> stream, final StreamSelectKeyV1 selectKey, final RuntimeBuildContext buildContext) {
    final LogicalSchema sourceSchema = stream.getSchema();
    final CompiledExpression expression = buildExpressionEvaluator(selectKey, buildContext, sourceSchema);
    final ProcessingLogger processingLogger = buildContext.getProcessingLogger(selectKey.getProperties().getQueryContext());
    final String errorMsg = "Error extracting new key using expression " + selectKey.getKeyExpression();
    final Function<GenericRow, Object> evaluator = val -> expression.evaluate(val, null, processingLogger, () -> errorMsg);
    final LogicalSchema resultSchema = new StepSchemaResolver(buildContext.getKsqlConfig(), buildContext.getFunctionRegistry()).resolve(selectKey, sourceSchema);
    final KStream<?, GenericRow> kstream = stream.getStream();
    final KStream<GenericKey, GenericRow> rekeyed = kstream.filter((key, val) -> val != null && evaluator.apply(val) != null).selectKey((key, val) -> GenericKey.genericKey(evaluator.apply(val)));
    return new KStreamHolder<>(rekeyed, resultSchema, ExecutionKeyFactory.unwindowed(buildContext));
}
Also used : KStreamHolder(io.confluent.ksql.execution.plan.KStreamHolder) StreamSelectKeyV1(io.confluent.ksql.execution.plan.StreamSelectKeyV1) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) GenericRow(io.confluent.ksql.GenericRow) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) GenericKey(io.confluent.ksql.GenericKey) KStream(org.apache.kafka.streams.kstream.KStream) ExecutionKeyFactory(io.confluent.ksql.execution.plan.ExecutionKeyFactory) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Function(java.util.function.Function) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) KStreamHolder(io.confluent.ksql.execution.plan.KStreamHolder) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) GenericRow(io.confluent.ksql.GenericRow) GenericKey(io.confluent.ksql.GenericKey)

Example 3 with CompiledExpression

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

Example 4 with CompiledExpression

use of io.confluent.ksql.execution.codegen.CompiledExpression in project ksql by confluentinc.

the class GroupByParamsFactory method expressionSchema.

private static LogicalSchema expressionSchema(final LogicalSchema sourceSchema, final List<CompiledExpression> groupBys) {
    final ColumnAliasGenerator columnAliasGenerator = ColumnNames.columnAliasGenerator(Stream.of(sourceSchema));
    final LogicalSchema.Builder schemaBuilder = LogicalSchema.builder();
    for (final CompiledExpression groupBy : groupBys) {
        final Expression groupByExp = groupBy.getExpression();
        final ColumnName columnName = groupByExp instanceof ColumnReferenceExp ? ((ColumnReferenceExp) groupByExp).getColumnName() : columnAliasGenerator.uniqueAliasFor(groupByExp);
        schemaBuilder.keyColumn(columnName, groupBy.getExpressionType());
    }
    schemaBuilder.valueColumns(sourceSchema.value());
    return schemaBuilder.build();
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) Expression(io.confluent.ksql.execution.expression.tree.Expression) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) ColumnAliasGenerator(io.confluent.ksql.schema.ksql.ColumnAliasGenerator) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression)

Example 5 with CompiledExpression

use of io.confluent.ksql.execution.codegen.CompiledExpression in project ksql by confluentinc.

the class PartitionByParamsFactory method buildExpressionEvaluator.

private static PartitionByExpressionEvaluator buildExpressionEvaluator(final LogicalSchema schema, final Expression partitionBy, final KsqlConfig ksqlConfig, final FunctionRegistry functionRegistry, final ProcessingLogger logger, final boolean partitionByInvolvesKeyColsOnly) {
    final CodeGenRunner codeGen = new CodeGenRunner(partitionByInvolvesKeyColsOnly ? schema.withKeyColsOnly() : schema, ksqlConfig, functionRegistry);
    final CompiledExpression compiledExpression = codeGen.buildCodeGenFromParseTree(partitionBy, "SelectKey");
    final String errorMsg = "Error computing new key from expression " + compiledExpression.getExpression();
    return new PartitionByExpressionEvaluator(compiledExpression, logger, () -> errorMsg, partitionByInvolvesKeyColsOnly);
}
Also used : CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression)

Aggregations

CompiledExpression (io.confluent.ksql.execution.codegen.CompiledExpression)6 CodeGenRunner (io.confluent.ksql.execution.codegen.CodeGenRunner)5 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)5 GenericRow (io.confluent.ksql.GenericRow)4 Expression (io.confluent.ksql.execution.expression.tree.Expression)4 ProcessingLogger (io.confluent.ksql.logging.processing.ProcessingLogger)4 GenericKey (io.confluent.ksql.GenericKey)3 QueryContext (io.confluent.ksql.execution.context.QueryContext)3 RuntimeBuildContext (io.confluent.ksql.execution.runtime.RuntimeBuildContext)3 Formats (io.confluent.ksql.execution.plan.Formats)2 KStreamHolder (io.confluent.ksql.execution.plan.KStreamHolder)2 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Function (java.util.function.Function)2 Serde (org.apache.kafka.common.serialization.Serde)2 Grouped (org.apache.kafka.streams.kstream.Grouped)2 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)1 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)1 KudtfFlatMapper (io.confluent.ksql.execution.function.udtf.KudtfFlatMapper)1