Search in sources :

Example 1 with CreateTable

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

the class CommandFactoriesTest method shouldFailCreateTableIfKeyNameIsIncorrect.

@Test
public void shouldFailCreateTableIfKeyNameIsIncorrect() {
    HashMap<String, Expression> tableProperties = new HashMap<>();
    tableProperties.putAll(properties);
    tableProperties.put(DdlConfig.KEY_NAME_PROPERTY, new StringLiteral("COL3"));
    try {
        final DdlCommand result = commandFactories.create(sqlExpression, new CreateTable(QualifiedName.of("foo"), Arrays.asList(new TableElement("COL1", "BIGINT"), new TableElement("COL2", "VARCHAR")), true, tableProperties), Collections.emptyMap());
    } catch (KsqlException e) {
        assertThat(e.getMessage(), equalTo("No column with the provided key column name in the " + "WITH clause, COL3, exists in the defined schema."));
    }
}
Also used : StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) CreateTable(io.confluent.ksql.parser.tree.CreateTable) EasyMock.anyString(org.easymock.EasyMock.anyString) KsqlException(io.confluent.ksql.util.KsqlException) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 2 with CreateTable

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

the class CommandFactoriesTest method shouldCreateCommandForCreateTable.

@Test
public void shouldCreateCommandForCreateTable() {
    HashMap<String, Expression> tableProperties = new HashMap<>();
    tableProperties.putAll(properties);
    tableProperties.put(DdlConfig.KEY_NAME_PROPERTY, new StringLiteral("COL1"));
    final DdlCommand result = commandFactories.create(sqlExpression, new CreateTable(QualifiedName.of("foo"), Arrays.asList(new TableElement("COL1", "BIGINT"), new TableElement("COL2", "VARCHAR")), true, tableProperties), Collections.emptyMap());
    assertThat(result, instanceOf(CreateTableCommand.class));
}
Also used : StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) CreateTable(io.confluent.ksql.parser.tree.CreateTable) EasyMock.anyString(org.easymock.EasyMock.anyString) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 3 with CreateTable

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

the class KsqlParserTest method testCreateTable.

@Test
public void testCreateTable() throws Exception {
    String queryStr = "CREATE TABLE users (usertime bigint, userid varchar, regionid varchar, gender varchar) " + "WITH (kafka_topic = 'users_topic', value_format='json', key = 'userid');";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testRegisterTopic failed.", statement instanceof CreateTable);
    CreateTable createTable = (CreateTable) statement;
    Assert.assertTrue("testCreateTable failed.", createTable.getName().toString().equalsIgnoreCase("USERS"));
    Assert.assertTrue("testCreateTable failed.", createTable.getElements().size() == 4);
    Assert.assertTrue("testCreateTable failed.", createTable.getElements().get(0).getName().toString().equalsIgnoreCase("usertime"));
    Assert.assertTrue("testCreateTable failed.", createTable.getProperties().get(DdlConfig.KAFKA_TOPIC_NAME_PROPERTY).toString().equalsIgnoreCase("'users_topic'"));
    Assert.assertTrue("testCreateTable failed.", createTable.getProperties().get(DdlConfig.VALUE_FORMAT_PROPERTY).toString().equalsIgnoreCase("'json'"));
}
Also used : Statement(io.confluent.ksql.parser.tree.Statement) CreateTable(io.confluent.ksql.parser.tree.CreateTable) Test(org.junit.Test)

Example 4 with CreateTable

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

the class KsqlParserTest method testCreateTableWithTopic.

@Test
public void testCreateTableWithTopic() throws Exception {
    String queryStr = "CREATE TABLE users (usertime bigint, userid varchar, regionid varchar, gender varchar) WITH (registered_topic = 'users_topic', key='userid', statestore='user_statestore');";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testRegisterTopic failed.", statement instanceof CreateTable);
    CreateTable createTable = (CreateTable) statement;
    Assert.assertTrue("testCreateTable failed.", createTable.getName().toString().equalsIgnoreCase("USERS"));
    Assert.assertTrue("testCreateTable failed.", createTable.getElements().size() == 4);
    Assert.assertTrue("testCreateTable failed.", createTable.getElements().get(0).getName().toString().equalsIgnoreCase("usertime"));
    Assert.assertTrue("testCreateTable failed.", createTable.getProperties().get(DdlConfig.TOPIC_NAME_PROPERTY).toString().equalsIgnoreCase("'users_topic'"));
}
Also used : Statement(io.confluent.ksql.parser.tree.Statement) CreateTable(io.confluent.ksql.parser.tree.CreateTable) Test(org.junit.Test)

Example 5 with CreateTable

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

the class KsqlEngine method buildSingleQueryAst.

private Pair<String, Statement> buildSingleQueryAst(final Statement statement, final String statementString, final MetaStore tempMetaStore, final MetaStore tempMetaStoreForParser, final Map<String, Object> overriddenProperties) {
    log.info("Building AST for {}.", statementString);
    if (statement instanceof Query) {
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateAsSelect) {
        CreateAsSelect createAsSelect = (CreateAsSelect) statement;
        QuerySpecification querySpecification = (QuerySpecification) createAsSelect.getQuery().getQueryBody();
        Query query = addInto(createAsSelect.getQuery(), querySpecification, createAsSelect.getName().getSuffix(), createAsSelect.getProperties(), createAsSelect.getPartitionByColumn());
        tempMetaStoreForParser.putSource(queryEngine.getResultDatasource(querySpecification.getSelect(), createAsSelect.getName().getSuffix()).cloneWithTimeKeyColumns());
        return new Pair<>(statementString, query);
    } else if (statement instanceof RegisterTopic) {
        ddlCommandExec.tryExecute(new RegisterTopicCommand((RegisterTopic) statement), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new RegisterTopicCommand((RegisterTopic) statement), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateStream) {
        ddlCommandExec.tryExecute(new CreateStreamCommand(statementString, (CreateStream) statement, overriddenProperties, topicClient, false), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new CreateStreamCommand(statementString, (CreateStream) statement, overriddenProperties, topicClient, false), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof CreateTable) {
        ddlCommandExec.tryExecute(new CreateTableCommand(statementString, (CreateTable) statement, overriddenProperties, topicClient, false), tempMetaStoreForParser);
        ddlCommandExec.tryExecute(new CreateTableCommand(statementString, (CreateTable) statement, overriddenProperties, topicClient, false), tempMetaStore);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropStream) {
        ddlCommandExec.tryExecute(new DropSourceCommand((DropStream) statement, DataSource.DataSourceType.KSTREAM, this), tempMetaStore);
        ddlCommandExec.tryExecute(new DropSourceCommand((DropStream) statement, DataSource.DataSourceType.KSTREAM, this), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropTable) {
        ddlCommandExec.tryExecute(new DropSourceCommand((DropTable) statement, DataSource.DataSourceType.KTABLE, this), tempMetaStore);
        ddlCommandExec.tryExecute(new DropSourceCommand((DropTable) statement, DataSource.DataSourceType.KTABLE, this), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof DropTopic) {
        ddlCommandExec.tryExecute(new DropTopicCommand((DropTopic) statement), tempMetaStore);
        ddlCommandExec.tryExecute(new DropTopicCommand((DropTopic) statement), tempMetaStoreForParser);
        return new Pair<>(statementString, statement);
    } else if (statement instanceof SetProperty) {
        return new Pair<>(statementString, statement);
    }
    return null;
}
Also used : DropTopicCommand(io.confluent.ksql.ddl.commands.DropTopicCommand) Query(io.confluent.ksql.parser.tree.Query) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic) DropSourceCommand(io.confluent.ksql.ddl.commands.DropSourceCommand) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateStream(io.confluent.ksql.parser.tree.CreateStream) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) RegisterTopicCommand(io.confluent.ksql.ddl.commands.RegisterTopicCommand) DropTable(io.confluent.ksql.parser.tree.DropTable) CreateTableCommand(io.confluent.ksql.ddl.commands.CreateTableCommand) QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) CreateStreamCommand(io.confluent.ksql.ddl.commands.CreateStreamCommand) DropTopic(io.confluent.ksql.parser.tree.DropTopic) DropStream(io.confluent.ksql.parser.tree.DropStream) SetProperty(io.confluent.ksql.parser.tree.SetProperty) Pair(io.confluent.ksql.util.Pair)

Aggregations

CreateTable (io.confluent.ksql.parser.tree.CreateTable)7 Test (org.junit.Test)5 Expression (io.confluent.ksql.parser.tree.Expression)4 StringLiteral (io.confluent.ksql.parser.tree.StringLiteral)4 HashMap (java.util.HashMap)4 TableElement (io.confluent.ksql.parser.tree.TableElement)3 KsqlException (io.confluent.ksql.util.KsqlException)3 EasyMock.anyString (org.easymock.EasyMock.anyString)3 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 Statement (io.confluent.ksql.parser.tree.Statement)2 Pair (io.confluent.ksql.util.Pair)2 CreateStreamCommand (io.confluent.ksql.ddl.commands.CreateStreamCommand)1 CreateTableCommand (io.confluent.ksql.ddl.commands.CreateTableCommand)1 DropSourceCommand (io.confluent.ksql.ddl.commands.DropSourceCommand)1 DropTopicCommand (io.confluent.ksql.ddl.commands.DropTopicCommand)1 RegisterTopicCommand (io.confluent.ksql.ddl.commands.RegisterTopicCommand)1 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)1 AbstractStreamCreateStatement (io.confluent.ksql.parser.tree.AbstractStreamCreateStatement)1 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)1 DropStream (io.confluent.ksql.parser.tree.DropStream)1