Search in sources :

Example 1 with AlterSourceCommand

use of io.confluent.ksql.execution.ddl.commands.AlterSourceCommand 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 AlterSourceCommand

use of io.confluent.ksql.execution.ddl.commands.AlterSourceCommand 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 3 with AlterSourceCommand

use of io.confluent.ksql.execution.ddl.commands.AlterSourceCommand in project ksql by confluentinc.

the class DdlCommandExecTest method shouldThrowOnAlterMissingSource.

@Test
public void shouldThrowOnAlterMissingSource() {
    // Given:
    alterSource = new AlterSourceCommand(STREAM_NAME, DataSourceType.KSTREAM.getKsqlType(), NEW_COLUMNS);
    // When:
    final KsqlException e = assertThrows(KsqlException.class, () -> cmdExec.execute(SQL_TEXT, alterSource, false, NO_QUERY_SOURCES));
    // Then:
    assertThat(e.getMessage(), is("Source s1 does not exist."));
}
Also used : AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 4 with AlterSourceCommand

use of io.confluent.ksql.execution.ddl.commands.AlterSourceCommand in project ksql by confluentinc.

the class DdlCommandExecTest method shouldThrowOnAddExistingColumn.

@Test
public void shouldThrowOnAddExistingColumn() {
    // Given:
    givenCreateStream();
    cmdExec.execute(SQL_TEXT, createStream, false, NO_QUERY_SOURCES);
    alterSource = new AlterSourceCommand(STREAM_NAME, DataSourceType.KSTREAM.getKsqlType(), SCHEMA2.columns());
    // When:
    final KsqlException e = assertThrows(KsqlException.class, () -> cmdExec.execute(SQL_TEXT, alterSource, false, NO_QUERY_SOURCES));
    // Then:
    assertThat(e.getMessage(), is("Cannot add column `F1` to schema. A column with the same name already exists."));
}
Also used : AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 5 with AlterSourceCommand

use of io.confluent.ksql.execution.ddl.commands.AlterSourceCommand in project ksql by confluentinc.

the class DdlCommandExecTest method shouldThrowOnAlterCAS.

@Test
public void shouldThrowOnAlterCAS() {
    // Given:
    givenCreateStream();
    cmdExec.execute(SQL_TEXT, createStream, true, NO_QUERY_SOURCES);
    alterSource = new AlterSourceCommand(STREAM_NAME, DataSourceType.KSTREAM.getKsqlType(), NEW_COLUMNS);
    // When:
    final KsqlException e = assertThrows(KsqlException.class, () -> cmdExec.execute(SQL_TEXT, alterSource, false, NO_QUERY_SOURCES));
    // Then:
    assertThat(e.getMessage(), is("ALTER command is not supported for CREATE ... AS statements."));
}
Also used : AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Aggregations

AlterSourceCommand (io.confluent.ksql.execution.ddl.commands.AlterSourceCommand)9 Test (org.junit.Test)8 KsqlException (io.confluent.ksql.util.KsqlException)5 AlterSource (io.confluent.ksql.parser.tree.AlterSource)3 DdlCommandResult (io.confluent.ksql.execution.ddl.commands.DdlCommandResult)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)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