Search in sources :

Example 1 with AlterSource

use of io.confluent.ksql.parser.tree.AlterSource in project ksql by confluentinc.

the class AlterSourceFactory method create.

public AlterSourceCommand create(final AlterSource statement) {
    final DataSource dataSource = metaStore.getSource(statement.getName());
    final String dataSourceType = statement.getDataSourceType().getKsqlType();
    if (dataSource != null && dataSource.isSource()) {
        throw new KsqlException(String.format("Cannot alter %s '%s': ALTER operations are not supported on source %s.", dataSourceType.toLowerCase(), statement.getName().text(), dataSourceType.toLowerCase() + "s"));
    }
    final List<Column> newColumns = statement.getAlterOptions().stream().map(alterOption -> Column.of(ColumnName.of(alterOption.getColumnName()), alterOption.getType().getSqlType(), Namespace.VALUE, 0)).collect(Collectors.toList());
    return new AlterSourceCommand(statement.getName(), dataSourceType, newColumns);
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) AlterSource(io.confluent.ksql.parser.tree.AlterSource) List(java.util.List) ColumnName(io.confluent.ksql.name.ColumnName) Objects.requireNonNull(java.util.Objects.requireNonNull) MetaStore(io.confluent.ksql.metastore.MetaStore) AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) KsqlException(io.confluent.ksql.util.KsqlException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Column(io.confluent.ksql.schema.ksql.Column) Collectors(java.util.stream.Collectors) Namespace(io.confluent.ksql.schema.ksql.Column.Namespace) AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) Column(io.confluent.ksql.schema.ksql.Column) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 2 with AlterSource

use of io.confluent.ksql.parser.tree.AlterSource in project ksql by confluentinc.

the class AlterSourceFactoryTest method shouldThrowInAlterOnSourceStream.

@Test
public void shouldThrowInAlterOnSourceStream() {
    // Given:
    final AlterSource alterSource = new AlterSource(STREAM_NAME, DataSourceType.KSTREAM, NEW_COLUMNS);
    when(ksqlStream.isSource()).thenReturn(true);
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> alterSourceFactory.create(alterSource));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot alter stream 'streamname': ALTER operations are not supported on " + "source streams."));
}
Also used : AlterSource(io.confluent.ksql.parser.tree.AlterSource) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 3 with AlterSource

use of io.confluent.ksql.parser.tree.AlterSource in project ksql by confluentinc.

the class AlterSourceFactoryTest method shouldCreateCommandForAlterTable.

@Test
public void shouldCreateCommandForAlterTable() {
    // Given:
    final AlterSource alterSource = new AlterSource(TABLE_NAME, DataSourceType.KTABLE, NEW_COLUMNS);
    // When:
    final AlterSourceCommand result = alterSourceFactory.create(alterSource);
    // Then:
    assertEquals(result.getKsqlType(), DataSourceType.KTABLE.getKsqlType());
    assertEquals(result.getSourceName(), TABLE_NAME);
    assertEquals(result.getNewColumns().size(), 1);
}
Also used : AlterSource(io.confluent.ksql.parser.tree.AlterSource) AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) Test(org.junit.Test)

Example 4 with AlterSource

use of io.confluent.ksql.parser.tree.AlterSource in project ksql by confluentinc.

the class CommandFactoriesTest method shouldCreateCommandForAlterSource.

@Test
public void shouldCreateCommandForAlterSource() {
    // Given:
    final AlterSource ddlStatement = new AlterSource(SOME_NAME, DataSourceType.KSTREAM, new ArrayList<>());
    // When:
    final DdlCommand result = commandFactories.create(sqlExpression, ddlStatement, SessionConfig.of(ksqlConfig, emptyMap()));
    // Then:
    assertThat(result, is(alterSourceCommand));
    verify(alterSourceFactory).create(ddlStatement);
}
Also used : AlterSource(io.confluent.ksql.parser.tree.AlterSource) DdlCommand(io.confluent.ksql.execution.ddl.commands.DdlCommand) Test(org.junit.Test)

Example 5 with AlterSource

use of io.confluent.ksql.parser.tree.AlterSource in project ksql by confluentinc.

the class AlterSourceFactoryTest method shouldThrowInAlterOnSourceTable.

@Test
public void shouldThrowInAlterOnSourceTable() {
    // Given:
    final AlterSource alterSource = new AlterSource(TABLE_NAME, DataSourceType.KTABLE, NEW_COLUMNS);
    when(ksqlTable.isSource()).thenReturn(true);
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> alterSourceFactory.create(alterSource));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot alter table 'tablename': ALTER operations are not supported on " + "source tables."));
}
Also used : AlterSource(io.confluent.ksql.parser.tree.AlterSource) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Aggregations

AlterSource (io.confluent.ksql.parser.tree.AlterSource)6 Test (org.junit.Test)5 AlterSourceCommand (io.confluent.ksql.execution.ddl.commands.AlterSourceCommand)3 KsqlException (io.confluent.ksql.util.KsqlException)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 DdlCommand (io.confluent.ksql.execution.ddl.commands.DdlCommand)1 MetaStore (io.confluent.ksql.metastore.MetaStore)1 DataSource (io.confluent.ksql.metastore.model.DataSource)1 ColumnName (io.confluent.ksql.name.ColumnName)1 Column (io.confluent.ksql.schema.ksql.Column)1 Namespace (io.confluent.ksql.schema.ksql.Column.Namespace)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Collectors (java.util.stream.Collectors)1