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."));
}
}
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));
}
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'"));
}
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'"));
}
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;
}
Aggregations