Search in sources :

Example 6 with CreateSourceProperties

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

the class DefaultSchemaInjector method addSchemaFields.

private static CreateSource addSchemaFields(final ConfiguredStatement<CreateSource> preparedStatement, final Optional<SchemaAndId> keySchema, final Optional<SchemaAndId> valueSchema) {
    final TableElements elements = buildElements(preparedStatement, keySchema, valueSchema);
    final CreateSource statement = preparedStatement.getStatement();
    final CreateSourceProperties 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 CreateSourceProperties newProperties = statement.getProperties().withKeyValueSchemaName(keySchemaName, valueSchemaName);
    return statement.copyWith(elements, newProperties);
}
Also used : TableElements(io.confluent.ksql.parser.tree.TableElements) CreateSource(io.confluent.ksql.parser.tree.CreateSource) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties)

Example 7 with CreateSourceProperties

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

the class DefaultSchemaInjector method getValueSchema.

private Optional<SchemaAndId> getValueSchema(final ConfiguredStatement<CreateSource> statement) {
    final CreateSourceProperties props = statement.getStatement().getProperties();
    final FormatInfo valueFormat = SourcePropertiesUtil.getValueFormat(props);
    if (!shouldInferSchema(props.getValueSchemaId(), statement, valueFormat, false)) {
        return Optional.empty();
    }
    return Optional.of(getSchema(Optional.of(props.getKafkaTopic()), props.getValueSchemaId(), valueFormat, props.getValueSerdeFeatures(), statement.getStatementText(), false));
}
Also used : FormatInfo(io.confluent.ksql.serde.FormatInfo) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties)

Example 8 with CreateSourceProperties

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

the class TopicCreateInjectorTest method shouldPassThroughWithClauseToBuilderForCreate.

@Test
public void shouldPassThroughWithClauseToBuilderForCreate() {
    // Given:
    givenStatement("CREATE STREAM x (FOO VARCHAR) WITH(value_format='avro', kafka_topic='topic', partitions=2);");
    final CreateSourceProperties props = ((CreateSource) statement.getStatement()).getProperties();
    // When:
    injector.inject(statement, builder);
    // Then:
    verify(builder).withWithClause(Optional.of(props.getKafkaTopic()), props.getPartitions(), props.getReplicas());
}
Also used : CreateSource(io.confluent.ksql.parser.tree.CreateSource) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) Test(org.junit.Test)

Example 9 with CreateSourceProperties

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

the class SqlFormatterTest method shouldFormatCreateTableStatementWithExplicitTimestamp.

@Test
public void shouldFormatCreateTableStatementWithExplicitTimestamp() {
    // Given:
    final CreateSourceProperties props = CreateSourceProperties.from(new ImmutableMap.Builder<String, Literal>().putAll(SOME_WITH_PROPS.copyOfOriginalLiterals()).put(CommonCreateConfigs.TIMESTAMP_NAME_PROPERTY, new StringLiteral("Foo")).put(CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY, new StringLiteral("%s")).build());
    final CreateTable createTable = new CreateTable(TEST, ELEMENTS_WITH_PRIMARY_KEY, false, false, props, false);
    // When:
    final String sql = SqlFormatter.formatSql(createTable);
    // Then:
    assertThat(sql, is("CREATE TABLE TEST (`k3` STRING PRIMARY KEY, `Foo` STRING) " + "WITH (KAFKA_TOPIC='topic_test', " + "TIMESTAMP='Foo', TIMESTAMP_FORMAT='%s', VALUE_FORMAT='JSON');"));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) CreateTable(io.confluent.ksql.parser.tree.CreateTable) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ImmutableMap(com.google.common.collect.ImmutableMap) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) Test(org.junit.Test)

Example 10 with CreateSourceProperties

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

the class SqlFormatterTest method shouldFormatCreateSourceTableStatement.

@Test
public void shouldFormatCreateSourceTableStatement() {
    // Given:
    final CreateSourceProperties props = CreateSourceProperties.from(new ImmutableMap.Builder<String, Literal>().putAll(SOME_WITH_PROPS.copyOfOriginalLiterals()).build());
    final CreateTable createTable = new CreateTable(TEST, ELEMENTS_WITH_PRIMARY_KEY, false, false, props, true);
    // When:
    final String sql = SqlFormatter.formatSql(createTable);
    // Then:
    assertThat(sql, is("CREATE SOURCE TABLE TEST (`k3` STRING PRIMARY KEY, `Foo` STRING) " + "WITH (KAFKA_TOPIC='topic_test', VALUE_FORMAT='JSON');"));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) CreateTable(io.confluent.ksql.parser.tree.CreateTable) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ImmutableMap(com.google.common.collect.ImmutableMap) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) Test(org.junit.Test)

Aggregations

CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)15 Literal (io.confluent.ksql.execution.expression.tree.Literal)6 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)6 CreateSource (io.confluent.ksql.parser.tree.CreateSource)6 Test (org.junit.Test)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 StringContains.containsString (org.hamcrest.core.StringContains.containsString)5 CreateTable (io.confluent.ksql.parser.tree.CreateTable)4 FormatInfo (io.confluent.ksql.serde.FormatInfo)4 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)3 KsqlException (io.confluent.ksql.util.KsqlException)3 TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)2 DataSource (io.confluent.ksql.metastore.model.DataSource)2 SourceName (io.confluent.ksql.name.SourceName)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 CreateStreamCommand (io.confluent.ksql.execution.ddl.commands.CreateStreamCommand)1 CreateTableCommand (io.confluent.ksql.execution.ddl.commands.CreateTableCommand)1 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)1 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)1 DefaultFormatInjector (io.confluent.ksql.format.DefaultFormatInjector)1