Search in sources :

Example 6 with FormatInfo

use of io.confluent.ksql.serde.FormatInfo in project ksql by confluentinc.

the class LogicalPlanner method getSinkTopic.

private KsqlTopic getSinkTopic(final Into into, final LogicalSchema schema) {
    if (into.getExistingTopic().isPresent()) {
        return into.getExistingTopic().get();
    }
    final NewTopic newTopic = into.getNewTopic().orElseThrow(IllegalStateException::new);
    final FormatInfo keyFormat = getSinkKeyFormat(schema, newTopic);
    final SerdeFeatures keyFeatures = SerdeFeaturesFactory.buildKeyFeatures(schema, FormatFactory.of(keyFormat));
    final SerdeFeatures valFeatures = SerdeFeaturesFactory.buildValueFeatures(schema, FormatFactory.of(newTopic.getValueFormat()), analysis.getProperties().getValueSerdeFeatures(), ksqlConfig);
    return new KsqlTopic(newTopic.getTopicName(), KeyFormat.of(keyFormat, keyFeatures, newTopic.getWindowInfo()), ValueFormat.of(newTopic.getValueFormat(), valFeatures));
}
Also used : NewTopic(io.confluent.ksql.analyzer.Analysis.Into.NewTopic) FormatInfo(io.confluent.ksql.serde.FormatInfo) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic)

Example 7 with FormatInfo

use of io.confluent.ksql.serde.FormatInfo in project ksql by confluentinc.

the class CreateSourcePropertiesTest method shouldSetValueFullSchemaName.

@Test
public void shouldSetValueFullSchemaName() {
    // When:
    final CreateSourceProperties properties = CreateSourceProperties.from(ImmutableMap.<String, Literal>builder().putAll(MINIMUM_VALID_PROPS).put(CommonCreateConfigs.VALUE_FORMAT_PROPERTY, new StringLiteral("Protobuf")).put(CommonCreateConfigs.VALUE_SCHEMA_FULL_NAME, new StringLiteral("schema")).build());
    // Then:
    assertThat(properties.getValueFormat().map(FormatInfo::getProperties).map(props -> props.get(ConnectProperties.FULL_SCHEMA_NAME)), is(Optional.of("schema")));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) KAFKA_TOPIC_NAME_PROPERTY(io.confluent.ksql.properties.with.CommonCreateConfigs.KAFKA_TOPIC_NAME_PROPERTY) FORMAT_PROPERTY(io.confluent.ksql.properties.with.CommonCreateConfigs.FORMAT_PROPERTY) CreateSourceAsProperties.from(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties.from) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CommonCreateConfigs(io.confluent.ksql.properties.with.CommonCreateConfigs) Function(java.util.function.Function) VALUE_SCHEMA_ID(io.confluent.ksql.properties.with.CommonCreateConfigs.VALUE_SCHEMA_ID) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ConnectProperties(io.confluent.ksql.serde.connect.ConnectProperties) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Duration(java.time.Duration) Map(java.util.Map) WindowType(io.confluent.ksql.model.WindowType) CreateConfigs(io.confluent.ksql.properties.with.CreateConfigs) WINDOW_SIZE_PROPERTY(io.confluent.ksql.properties.with.CreateConfigs.WINDOW_SIZE_PROPERTY) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) TIMESTAMP_FORMAT_PROPERTY(io.confluent.ksql.properties.with.CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) KEY_FORMAT_PROPERTY(io.confluent.ksql.properties.with.CommonCreateConfigs.KEY_FORMAT_PROPERTY) VALUE_FORMAT_PROPERTY(io.confluent.ksql.properties.with.CommonCreateConfigs.VALUE_FORMAT_PROPERTY) VALUE_AVRO_SCHEMA_FULL_NAME(io.confluent.ksql.properties.with.CommonCreateConfigs.VALUE_AVRO_SCHEMA_FULL_NAME) ImmutableMap(com.google.common.collect.ImmutableMap) SerdeFeature(io.confluent.ksql.serde.SerdeFeature) Literal(io.confluent.ksql.execution.expression.tree.Literal) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ImmutableMap.of(com.google.common.collect.ImmutableMap.of) EqualsTester(com.google.common.testing.EqualsTester) KEY_SCHEMA_FULL_NAME(io.confluent.ksql.properties.with.CommonCreateConfigs.KEY_SCHEMA_FULL_NAME) VALUE_SCHEMA_FULL_NAME(io.confluent.ksql.properties.with.CommonCreateConfigs.VALUE_SCHEMA_FULL_NAME) WINDOW_TYPE_PROPERTY(io.confluent.ksql.properties.with.CreateConfigs.WINDOW_TYPE_PROPERTY) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) KEY_SCHEMA_ID(io.confluent.ksql.properties.with.CommonCreateConfigs.KEY_SCHEMA_ID) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) FormatInfo(io.confluent.ksql.serde.FormatInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) FormatInfo(io.confluent.ksql.serde.FormatInfo) Test(org.junit.Test)

Example 8 with FormatInfo

use of io.confluent.ksql.serde.FormatInfo in project ksql by confluentinc.

the class StreamAggregateBuilderTest method shouldReturnCorrectSerdeForWindowedAggregate.

@Test
public void shouldReturnCorrectSerdeForWindowedAggregate() {
    for (final Runnable given : given()) {
        // Given:
        clearInvocations(groupedStream, timeWindowedStream, sessionWindowedStream, aggregated, buildContext);
        given.run();
        // When:
        final KTableHolder<Windowed<GenericKey>> tableHolder = windowedAggregate.build(planBuilder, planInfo);
        // Then:
        final ExecutionKeyFactory<Windowed<GenericKey>> serdeFactory = tableHolder.getExecutionKeyFactory();
        final FormatInfo mockFormat = mock(FormatInfo.class);
        final PhysicalSchema mockSchema = mock(PhysicalSchema.class);
        final QueryContext mockCtx = mock(QueryContext.class);
        serdeFactory.buildKeySerde(mockFormat, mockSchema, mockCtx);
        verify(buildContext).buildKeySerde(same(mockFormat), eq(windowedAggregate.getWindowExpression().getWindowInfo()), same(mockSchema), same(mockCtx));
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) QueryContext(io.confluent.ksql.execution.context.QueryContext) FormatInfo(io.confluent.ksql.serde.FormatInfo) Test(org.junit.Test)

Example 9 with FormatInfo

use of io.confluent.ksql.serde.FormatInfo 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)

Example 10 with FormatInfo

use of io.confluent.ksql.serde.FormatInfo in project ksql by confluentinc.

the class SchemaKStream method groupBy.

public SchemaKGroupedStream groupBy(final FormatInfo valueFormat, final List<Expression> groupByExpressions, final Stacker contextStacker) {
    if (!repartitionNeeded(groupByExpressions)) {
        return groupByKey(keyFormat, valueFormat, contextStacker);
    }
    final KeyFormat rekeyedFormat;
    if (keyFormat.getFormatInfo().getFormat().equals(NoneFormat.NAME)) {
        final FormatInfo defaultFormat = FormatInfo.of(ksqlConfig.getString(KsqlConfig.KSQL_DEFAULT_KEY_FORMAT_CONFIG));
        rekeyedFormat = KeyFormat.nonWindowed(defaultFormat, SerdeFeatures.of());
    } else {
        rekeyedFormat = keyFormat;
    }
    final KeyFormat sanitizedKeyFormat = SerdeFeaturesFactory.sanitizeKeyFormat(rekeyedFormat, toSqlTypes(groupByExpressions), true);
    final StreamGroupBy<K> source = ExecutionStepFactory.streamGroupBy(contextStacker, sourceStep, InternalFormats.of(sanitizedKeyFormat, valueFormat), groupByExpressions);
    return new SchemaKGroupedStream(source, resolveSchema(source), sanitizedKeyFormat, ksqlConfig, functionRegistry);
}
Also used : KeyFormat(io.confluent.ksql.serde.KeyFormat) FormatInfo(io.confluent.ksql.serde.FormatInfo)

Aggregations

FormatInfo (io.confluent.ksql.serde.FormatInfo)16 SerdeFeatures (io.confluent.ksql.serde.SerdeFeatures)8 Optional (java.util.Optional)5 ColumnName (io.confluent.ksql.name.ColumnName)4 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)4 CreateSource (io.confluent.ksql.parser.tree.CreateSource)4 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)4 KeyFormat (io.confluent.ksql.serde.KeyFormat)4 KsqlException (io.confluent.ksql.util.KsqlException)4 WindowType (io.confluent.ksql.model.WindowType)3 SchemaAndId (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)3 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.of (com.google.common.collect.ImmutableMap.of)2 EqualsTester (com.google.common.testing.EqualsTester)2 QueryContext (io.confluent.ksql.execution.context.QueryContext)2 Stacker (io.confluent.ksql.execution.context.QueryContext.Stacker)2 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)2 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)2 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)2