Search in sources :

Example 1 with CreateSourceProperties

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

the class SqlFormatterTest method shouldFormatCreateSourceStreamStatement.

@Test
public void shouldFormatCreateSourceStreamStatement() {
    // Given:
    final CreateSourceProperties props = CreateSourceProperties.from(new ImmutableMap.Builder<String, Literal>().putAll(SOME_WITH_PROPS.copyOfOriginalLiterals()).build());
    final CreateStream createStream = new CreateStream(TEST, ELEMENTS_WITH_KEY, false, false, props, true);
    // When:
    final String sql = SqlFormatter.formatSql(createStream);
    // Then:
    assertThat(sql, is("CREATE SOURCE STREAM TEST (`k3` STRING 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) CreateStream(io.confluent.ksql.parser.tree.CreateStream) 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 2 with CreateSourceProperties

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

the class SqlFormatterTest method shouldFormatCreateOrReplaceTableStatement.

@Test
public void shouldFormatCreateOrReplaceTableStatement() {
    // 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, true, false, props, false);
    // When:
    final String sql = SqlFormatter.formatSql(createTable);
    // Then:
    assertThat(sql, is("CREATE OR REPLACE 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)

Example 3 with CreateSourceProperties

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

the class CreateSourceFactory method createStreamCommand.

// This method is called by simple CREATE statements
public CreateStreamCommand createStreamCommand(final CreateStream statement, final KsqlConfig ksqlConfig) {
    final SourceName sourceName = statement.getName();
    final CreateSourceProperties props = statement.getProperties();
    final String topicName = ensureTopicExists(props, serviceContext);
    final LogicalSchema schema = buildSchema(statement.getElements(), ksqlConfig);
    final Optional<TimestampColumn> timestampColumn = buildTimestampColumn(ksqlConfig, props, schema);
    final DataSource dataSource = metaStore.getSource(sourceName);
    if (dataSource != null && !statement.isOrReplace() && !statement.isNotExists()) {
        final String sourceType = dataSource.getDataSourceType().getKsqlType();
        throw new KsqlException(String.format("Cannot add stream '%s': A %s with the same name already exists", sourceName.text(), sourceType.toLowerCase()));
    }
    throwIfCreateOrReplaceOnSourceStreamOrTable(statement, dataSource);
    return new CreateStreamCommand(sourceName, schema, timestampColumn, topicName, buildFormats(statement.getName(), schema, props, ksqlConfig), getWindowInfo(props), Optional.of(statement.isOrReplace()), Optional.of(statement.isSource()));
}
Also used : CreateStreamCommand(io.confluent.ksql.execution.ddl.commands.CreateStreamCommand) SourceName(io.confluent.ksql.name.SourceName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsqlException(io.confluent.ksql.util.KsqlException) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 4 with CreateSourceProperties

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

the class CreateSourceFactory method createTableCommand.

// This method is called by simple CREATE statements
public CreateTableCommand createTableCommand(final CreateTable statement, final KsqlConfig ksqlConfig) {
    final SourceName sourceName = statement.getName();
    final CreateSourceProperties props = statement.getProperties();
    final String topicName = ensureTopicExists(props, serviceContext);
    final LogicalSchema schema = buildSchema(statement.getElements(), ksqlConfig);
    final DataSource dataSource = metaStore.getSource(sourceName);
    if (dataSource != null && !statement.isOrReplace() && !statement.isNotExists()) {
        final String sourceType = dataSource.getDataSourceType().getKsqlType();
        throw new KsqlException(String.format("Cannot add table '%s': A %s with the same name already exists", sourceName.text(), sourceType.toLowerCase()));
    }
    if (schema.key().isEmpty()) {
        final boolean usingSchemaInference = props.getValueSchemaId().isPresent();
        final String additional = usingSchemaInference ? System.lineSeparator() + "Use a partial schema to define the primary key and still load the value columns from " + "the Schema Registry, for example:" + System.lineSeparator() + "\tCREATE TABLE " + sourceName.text() + " (ID INT PRIMARY KEY) WITH (...);" : "";
        throw new KsqlException("Tables require a PRIMARY KEY. Please define the PRIMARY KEY." + additional);
    }
    throwIfCreateOrReplaceOnSourceStreamOrTable(statement, dataSource);
    final Optional<TimestampColumn> timestampColumn = buildTimestampColumn(ksqlConfig, props, schema);
    return new CreateTableCommand(sourceName, schema, timestampColumn, topicName, buildFormats(statement.getName(), schema, props, ksqlConfig), getWindowInfo(props), Optional.of(statement.isOrReplace()), Optional.of(statement.isSource()));
}
Also used : SourceName(io.confluent.ksql.name.SourceName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsqlException(io.confluent.ksql.util.KsqlException) CreateTableCommand(io.confluent.ksql.execution.ddl.commands.CreateTableCommand) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 5 with CreateSourceProperties

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

the class DefaultFormatInjector method injectForCreateStatement.

private Optional<ConfiguredStatement<CreateSource>> injectForCreateStatement(final ConfiguredStatement<CreateSource> original) {
    final CreateSource statement = original.getStatement();
    final CreateSourceProperties properties = statement.getProperties();
    final Optional<FormatInfo> keyFormat = properties.getKeyFormat(statement.getName());
    final Optional<FormatInfo> valueFormat = properties.getValueFormat();
    if (keyFormat.isPresent() && valueFormat.isPresent()) {
        return Optional.empty();
    }
    final KsqlConfig config = getConfig(original);
    final CreateSourceProperties injectedProps = properties.withFormats(keyFormat.map(FormatInfo::getFormat).orElseGet(() -> getDefaultKeyFormat(config)), valueFormat.map(FormatInfo::getFormat).orElseGet(() -> getDefaultValueFormat(config)));
    final CreateSource withFormats = statement.copyWith(original.getStatement().getElements(), injectedProps);
    final PreparedStatement<CreateSource> prepared = buildPreparedStatement(withFormats);
    final ConfiguredStatement<CreateSource> configured = ConfiguredStatement.of(prepared, original.getSessionConfig());
    return Optional.of(configured);
}
Also used : CreateSource(io.confluent.ksql.parser.tree.CreateSource) KsqlConfig(io.confluent.ksql.util.KsqlConfig) FormatInfo(io.confluent.ksql.serde.FormatInfo) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties)

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