Search in sources :

Example 1 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties in project ksql by confluentinc.

the class KsqlAuthorizationValidatorImpl method getCreateAsSelectSinkTopic.

private KsqlTopic getCreateAsSelectSinkTopic(final MetaStore metaStore, final CreateAsSelect createAsSelect) {
    final CreateSourceAsProperties properties = createAsSelect.getProperties();
    final String sinkTopicName;
    final KeyFormat sinkKeyFormat;
    final ValueFormat sinkValueFormat;
    if (!properties.getKafkaTopic().isPresent()) {
        final DataSource dataSource = metaStore.getSource(createAsSelect.getName());
        if (dataSource != null) {
            sinkTopicName = dataSource.getKafkaTopicName();
            sinkKeyFormat = dataSource.getKsqlTopic().getKeyFormat();
            sinkValueFormat = dataSource.getKsqlTopic().getValueFormat();
        } else {
            throw new KsqlException("Cannot validate for topic access from an unknown stream/table: " + createAsSelect.getName());
        }
    } else {
        sinkTopicName = properties.getKafkaTopic().get();
        // If no format is specified for the sink topic, then use the format from the primary
        // source topic.
        final SourceTopicsExtractor extractor = new SourceTopicsExtractor(metaStore);
        extractor.process(createAsSelect.getQuery(), null);
        final KsqlTopic primaryKsqlTopic = extractor.getPrimarySourceTopic();
        final Optional<Format> keyFormat = properties.getKeyFormat().map(formatName -> FormatFactory.fromName(formatName));
        final Optional<Format> valueFormat = properties.getValueFormat().map(formatName -> FormatFactory.fromName(formatName));
        sinkKeyFormat = keyFormat.map(format -> KeyFormat.of(FormatInfo.of(format.name()), format.supportsFeature(SerdeFeature.SCHEMA_INFERENCE) ? SerdeFeatures.of(SerdeFeature.SCHEMA_INFERENCE) : SerdeFeatures.of(), Optional.empty())).orElse(primaryKsqlTopic.getKeyFormat());
        sinkValueFormat = valueFormat.map(format -> ValueFormat.of(FormatInfo.of(format.name()), format.supportsFeature(SerdeFeature.SCHEMA_INFERENCE) ? SerdeFeatures.of(SerdeFeature.SCHEMA_INFERENCE) : SerdeFeatures.of())).orElse(primaryKsqlTopic.getValueFormat());
    }
    return new KsqlTopic(sinkTopicName, sinkKeyFormat, sinkValueFormat);
}
Also used : ValueFormat(io.confluent.ksql.serde.ValueFormat) KeyFormat(io.confluent.ksql.serde.KeyFormat) ValueFormat(io.confluent.ksql.serde.ValueFormat) Format(io.confluent.ksql.serde.Format) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) SourceTopicsExtractor(io.confluent.ksql.topic.SourceTopicsExtractor) KeyFormat(io.confluent.ksql.serde.KeyFormat) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic)

Example 2 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties in project ksql by confluentinc.

the class TopicCreateInjectorTest method shouldPassThroughWithClauseToBuilderForCreateAs.

@Test
public void shouldPassThroughWithClauseToBuilderForCreateAs() {
    // Given:
    givenStatement("CREATE STREAM x WITH (kafka_topic='topic') AS SELECT * FROM SOURCE;");
    final CreateSourceAsProperties props = ((CreateAsSelect) statement.getStatement()).getProperties();
    // When:
    injector.inject(statement, builder);
    // Then:
    verify(builder).withWithClause(props.getKafkaTopic(), props.getPartitions(), props.getReplicas());
}
Also used : CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) Test(org.junit.Test)

Example 3 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties in project ksql by confluentinc.

the class TopicCreateInjectorTest method shouldBuildWithClauseWithTopicProperties.

@SuppressWarnings("unchecked")
@Test
public void shouldBuildWithClauseWithTopicProperties() {
    // Given:
    givenStatement("CREATE STREAM x WITH (kafka_topic='topic') AS SELECT * FROM SOURCE;");
    when(builder.build()).thenReturn(new TopicProperties("expectedName", 10, (short) 10));
    // When:
    final ConfiguredStatement<CreateAsSelect> result = (ConfiguredStatement<CreateAsSelect>) injector.inject(statement, builder);
    // Then:
    final CreateSourceAsProperties props = result.getStatement().getProperties();
    assertThat(props.getKafkaTopic(), is(Optional.of("expectedName")));
    assertThat(props.getPartitions(), is(Optional.of(10)));
    assertThat(props.getReplicas(), is(Optional.of((short) 10)));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) Test(org.junit.Test)

Example 4 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties in project ksql by confluentinc.

the class TopicCreateInjector method injectForCreateAsSelect.

@SuppressWarnings("unchecked")
private <T extends CreateAsSelect> ConfiguredStatement<?> injectForCreateAsSelect(final ConfiguredStatement<T> statement, final TopicProperties.Builder topicPropertiesBuilder) {
    final String prefix = statement.getSessionConfig().getConfig(true).getString(KsqlConfig.KSQL_OUTPUT_TOPIC_NAME_PREFIX_CONFIG);
    final T createAsSelect = statement.getStatement();
    final CreateSourceAsProperties properties = createAsSelect.getProperties();
    final SourceTopicsExtractor extractor = new SourceTopicsExtractor(metaStore);
    extractor.process(statement.getStatement().getQuery(), null);
    final String sourceTopicName = extractor.getPrimarySourceTopic().getKafkaTopicName();
    topicPropertiesBuilder.withName(prefix + createAsSelect.getName().text()).withSource(() -> topicClient.describeTopic(sourceTopicName)).withWithClause(properties.getKafkaTopic(), properties.getPartitions(), properties.getReplicas());
    final String topicCleanUpPolicy;
    final Map<String, Object> additionalTopicConfigs = new HashMap<>();
    if (createAsSelect instanceof CreateStreamAsSelect) {
        topicCleanUpPolicy = TopicConfig.CLEANUP_POLICY_DELETE;
    } else {
        if (createAsSelect.getQuery().getWindow().isPresent()) {
            topicCleanUpPolicy = TopicConfig.CLEANUP_POLICY_COMPACT + "," + TopicConfig.CLEANUP_POLICY_DELETE;
            createAsSelect.getQuery().getWindow().get().getKsqlWindowExpression().getRetention().ifPresent(retention -> additionalTopicConfigs.put(TopicConfig.RETENTION_MS_CONFIG, retention.toDuration().toMillis()));
        } else {
            topicCleanUpPolicy = TopicConfig.CLEANUP_POLICY_COMPACT;
        }
    }
    final TopicProperties info = createTopic(topicPropertiesBuilder, topicCleanUpPolicy, additionalTopicConfigs);
    final T withTopic = (T) createAsSelect.copyWith(properties.withTopic(info.getTopicName(), info.getPartitions(), info.getReplicas()));
    final String withTopicText = SqlFormatter.formatSql(withTopic) + ";";
    return statement.withStatement(withTopicText, withTopic);
}
Also used : HashMap(java.util.HashMap) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect)

Example 5 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties in project ksql by confluentinc.

the class DefaultSchemaInjector method addSchemaFieldsCas.

private static CreateAsSelect addSchemaFieldsCas(final ConfiguredStatement<CreateAsSelect> preparedStatement, final Optional<SchemaAndId> keySchema, final Optional<SchemaAndId> valueSchema) {
    final CreateAsSelect statement = preparedStatement.getStatement();
    final CreateSourceAsProperties properties = statement.getProperties();
    final Optional<String> keySchemaName;
    final Optional<String> valueSchemaName;
    // Only populate key and value schema names when schema ids are explicitly provided
    if (properties.getKeySchemaId().isPresent() && keySchema.isPresent()) {
        keySchemaName = Optional.ofNullable(keySchema.get().rawSchema.name());
    } else {
        keySchemaName = Optional.empty();
    }
    if (properties.getValueSchemaId().isPresent() && valueSchema.isPresent()) {
        valueSchemaName = Optional.ofNullable(valueSchema.get().rawSchema.name());
    } else {
        valueSchemaName = Optional.empty();
    }
    final CreateSourceAsProperties newProperties = statement.getProperties().withKeyValueSchemaName(keySchemaName, valueSchemaName);
    return statement.copyWith(newProperties);
}
Also used : CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect)

Aggregations

CreateSourceAsProperties (io.confluent.ksql.parser.properties.with.CreateSourceAsProperties)9 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)6 SchemaAndId (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)3 Column (io.confluent.ksql.schema.ksql.Column)2 SimpleColumn (io.confluent.ksql.schema.ksql.SimpleColumn)2 FormatInfo (io.confluent.ksql.serde.FormatInfo)2 KsqlException (io.confluent.ksql.util.KsqlException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 CreateSourceCommand (io.confluent.ksql.execution.ddl.commands.CreateSourceCommand)1 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)1 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)1 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)1 Literal (io.confluent.ksql.execution.expression.tree.Literal)1 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)1 DataSource (io.confluent.ksql.metastore.model.DataSource)1 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)1 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)1 Format (io.confluent.ksql.serde.Format)1