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');"));
}
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');"));
}
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()));
}
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()));
}
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);
}
Aggregations