Search in sources :

Example 61 with LogicalSchema

use of io.confluent.ksql.schema.ksql.LogicalSchema in project ksql by confluentinc.

the class PartitionByParamsFactoryTest method shouldBuildResultSchemaWhenPartitioningByStructField.

@Test
public void shouldBuildResultSchemaWhenPartitioningByStructField() {
    // Given:
    final List<Expression> partitionBy = ImmutableList.of(new DereferenceExpression(Optional.empty(), new UnqualifiedColumnReferenceExp(COL3), "someField"));
    // When:
    final LogicalSchema resultSchema = PartitionByParamsFactory.buildSchema(SCHEMA, partitionBy, functionRegistry);
    // Then:
    assertThat(resultSchema, is(LogicalSchema.builder().keyColumn(ColumnName.of("someField"), SqlTypes.BIGINT).valueColumn(COL1, SqlTypes.INTEGER).valueColumn(COL2, SqlTypes.INTEGER).valueColumn(COL3, COL3_TYPE).valueColumn(SystemColumns.ROWTIME_NAME, SqlTypes.BIGINT).valueColumn(COL0, SqlTypes.STRING).valueColumn(ColumnName.of("someField"), SqlTypes.BIGINT).build()));
}
Also used : DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Example 62 with LogicalSchema

use of io.confluent.ksql.schema.ksql.LogicalSchema in project ksql by confluentinc.

the class PartitionByParamsFactoryTest method shouldBuildResultSchemaWhenPartitioningByNull.

@Test
public void shouldBuildResultSchemaWhenPartitioningByNull() {
    // Given:
    final List<Expression> partitionBy = ImmutableList.of(new NullLiteral());
    // When:
    final LogicalSchema resultSchema = PartitionByParamsFactory.buildSchema(SCHEMA, partitionBy, functionRegistry);
    // Then:
    assertThat(resultSchema, is(LogicalSchema.builder().valueColumn(COL1, SqlTypes.INTEGER).valueColumn(COL2, SqlTypes.INTEGER).valueColumn(COL3, COL3_TYPE).valueColumn(SystemColumns.ROWTIME_NAME, SqlTypes.BIGINT).valueColumn(COL0, SqlTypes.STRING).build()));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) Test(org.junit.Test)

Example 63 with LogicalSchema

use of io.confluent.ksql.schema.ksql.LogicalSchema 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 64 with LogicalSchema

use of io.confluent.ksql.schema.ksql.LogicalSchema 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 65 with LogicalSchema

use of io.confluent.ksql.schema.ksql.LogicalSchema in project ksql by confluentinc.

the class GroupByParamsFactoryTest method shouldSetKeyNameFromFieldName.

@Test
public void shouldSetKeyNameFromFieldName() {
    // Given:
    when(groupBy0.getExpression()).thenReturn(new DereferenceExpression(Optional.empty(), new UnqualifiedColumnReferenceExp(COL3), "someField"));
    // When:
    final LogicalSchema schema = GroupByParamsFactory.buildSchema(SOURCE_SCHEMA, ImmutableList.of(groupBy0, groupBy1));
    // Then:
    assertThat(schema, is(LogicalSchema.builder().keyColumn(ColumnName.of("someField"), SqlTypes.INTEGER).keyColumn(ColumnName.of("K1"), SqlTypes.INTEGER).valueColumns(SOURCE_SCHEMA.value()).build()));
}
Also used : DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Aggregations

LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)223 Test (org.junit.Test)152 Expression (io.confluent.ksql.execution.expression.tree.Expression)44 ColumnName (io.confluent.ksql.name.ColumnName)31 GenericRow (io.confluent.ksql.GenericRow)30 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)29 KsqlException (io.confluent.ksql.util.KsqlException)27 GenericKey (io.confluent.ksql.GenericKey)20 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)19 List (java.util.List)16 TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)14 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)14 Optional (java.util.Optional)14 Collectors (java.util.stream.Collectors)14 QueryContext (io.confluent.ksql.execution.context.QueryContext)13 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)12 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)12 SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)12 Column (io.confluent.ksql.schema.ksql.Column)12 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)11