use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.
the class SchemaParserTest method shouldParseEmptySchema.
@Test
public void shouldParseEmptySchema() {
// Given:
final String schema = " \t\n\r";
// When:
final TableElements elements = parser.parse(schema);
// Then:
assertThat(Iterables.isEmpty(elements), is(true));
}
use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.
the class SchemaParserTest method shouldParseValidSchema.
@Test
public void shouldParseValidSchema() {
// Given:
final String schema = "foo INTEGER, bar MAP<VARCHAR, VARCHAR>";
// When:
final TableElements elements = parser.parse(schema);
// Then:
assertThat(elements, contains(new TableElement(FOO, new Type(SqlTypes.INTEGER)), new TableElement(BAR, new Type(SqlTypes.map(SqlTypes.STRING, SqlTypes.STRING)))));
}
use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.
the class SchemaParserTest method shouldParseValidSchemaWithSingleHeaderKeyField.
@Test
public void shouldParseValidSchemaWithSingleHeaderKeyField() {
// Given:
final String schema = "K STRING HEADER('k1'), bar INT";
// When:
final TableElements elements = parser.parse(schema);
// Then:
assertThat(elements, contains(new TableElement(ColumnName.of("K"), new Type(SqlTypes.STRING), HEADER_KEY1_CONSTRAINT), new TableElement(BAR, new Type(SqlTypes.INTEGER))));
}
use of io.confluent.ksql.parser.tree.TableElements 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);
}
use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.
the class DefaultSchemaInjector method buildElements.
private static TableElements buildElements(final ConfiguredStatement<CreateSource> preparedStatement, final Optional<SchemaAndId> keySchema, final Optional<SchemaAndId> valueSchema) {
final List<TableElement> elements = new ArrayList<>();
if (keySchema.isPresent()) {
final ColumnConstraints constraints = getKeyConstraints(preparedStatement.getStatement());
keySchema.get().columns.stream().map(col -> new TableElement(col.name(), new Type(col.type()), constraints)).forEach(elements::add);
} else {
getKeyColumns(preparedStatement).forEach(elements::add);
}
if (valueSchema.isPresent()) {
valueSchema.get().columns.stream().map(col -> new TableElement(col.name(), new Type(col.type()))).forEach(elements::add);
} else {
getValueColumns(preparedStatement).forEach(elements::add);
}
return TableElements.of(elements);
}
Aggregations