Search in sources :

Example 6 with TableElements

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));
}
Also used : TableElements(io.confluent.ksql.parser.tree.TableElements) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 7 with TableElements

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)))));
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) TableElements(io.confluent.ksql.parser.tree.TableElements) Matchers.containsString(org.hamcrest.Matchers.containsString) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 8 with TableElements

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))));
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) TableElements(io.confluent.ksql.parser.tree.TableElements) Matchers.containsString(org.hamcrest.Matchers.containsString) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 9 with TableElements

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);
}
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 10 with TableElements

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);
}
Also used : IntStream(java.util.stream.IntStream) FormatFactory(io.confluent.ksql.serde.FormatFactory) ServiceContext(io.confluent.ksql.services.ServiceContext) SerdeFeaturesFactory(io.confluent.ksql.serde.SerdeFeaturesFactory) CommonCreateConfigs(io.confluent.ksql.properties.with.CommonCreateConfigs) TableElement(io.confluent.ksql.parser.tree.TableElement) ArrayList(java.util.ArrayList) CreateStream(io.confluent.ksql.parser.tree.CreateStream) Injector(io.confluent.ksql.statement.Injector) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateSource(io.confluent.ksql.parser.tree.CreateSource) SchemaResult(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaResult) ColumnConstraints(io.confluent.ksql.parser.tree.ColumnConstraints) SimpleColumn(io.confluent.ksql.schema.ksql.SimpleColumn) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateSourceCommand(io.confluent.ksql.execution.ddl.commands.CreateSourceCommand) ImmutableSet(com.google.common.collect.ImmutableSet) Type(io.confluent.ksql.execution.expression.tree.Type) ImmutableMap(com.google.common.collect.ImmutableMap) SerdeFeature(io.confluent.ksql.serde.SerdeFeature) SetView(com.google.common.collect.Sets.SetView) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) SqlFormatter(io.confluent.ksql.parser.SqlFormatter) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) SourcePropertiesUtil(io.confluent.ksql.parser.properties.with.SourcePropertiesUtil) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ErrorMessageUtil(io.confluent.ksql.util.ErrorMessageUtil) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Objects(java.util.Objects) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) List(java.util.List) Stream(java.util.stream.Stream) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Format(io.confluent.ksql.serde.Format) TableElements(io.confluent.ksql.parser.tree.TableElements) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Statement(io.confluent.ksql.parser.tree.Statement) Column(io.confluent.ksql.schema.ksql.Column) FormatInfo(io.confluent.ksql.serde.FormatInfo) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Type(io.confluent.ksql.execution.expression.tree.Type) ColumnConstraints(io.confluent.ksql.parser.tree.ColumnConstraints) ArrayList(java.util.ArrayList) TableElement(io.confluent.ksql.parser.tree.TableElement)

Aggregations

TableElements (io.confluent.ksql.parser.tree.TableElements)15 TableElement (io.confluent.ksql.parser.tree.TableElement)11 Test (org.junit.Test)11 Type (io.confluent.ksql.execution.expression.tree.Type)9 Matchers.containsString (org.hamcrest.Matchers.containsString)8 CreateSource (io.confluent.ksql.parser.tree.CreateSource)3 KsqlException (io.confluent.ksql.util.KsqlException)3 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 SetView (com.google.common.collect.Sets.SetView)1 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)1 CreateSourceCommand (io.confluent.ksql.execution.ddl.commands.CreateSourceCommand)1 TypeRegistry (io.confluent.ksql.metastore.TypeRegistry)1 ColumnName (io.confluent.ksql.name.ColumnName)1