use of io.confluent.ksql.parser.tree.QuerySpecification in project ksql by confluentinc.
the class StatementExecutor method startQuery.
private boolean startQuery(String queryString, Query query, CommandId commandId, Map<QueryId, CommandId> terminatedQueries, Command command, boolean wasDropped) throws Exception {
if (query.getQueryBody() instanceof QuerySpecification) {
QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
Relation into = querySpecification.getInto();
if (into instanceof Table) {
Table table = (Table) into;
if (ksqlEngine.getMetaStore().getSource(table.getName().getSuffix()) != null) {
throw new Exception(String.format("Sink specified in INTO clause already exists: %s", table.getName().getSuffix().toUpperCase()));
}
}
}
QueryMetadata queryMetadata = ksqlEngine.buildMultipleQueries(queryString, command.getKsqlProperties()).get(0);
if (queryMetadata instanceof PersistentQueryMetadata) {
PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
final QueryId queryId = persistentQueryMetadata.getId();
if (terminatedQueries != null && terminatedQueries.containsKey(queryId)) {
CommandId terminateId = terminatedQueries.get(queryId);
statusStore.put(terminateId, new CommandStatus(CommandStatus.Status.SUCCESS, "Termination request granted"));
statusStore.put(commandId, new CommandStatus(CommandStatus.Status.TERMINATED, "Query terminated"));
ksqlEngine.terminateQuery(queryId, false);
return false;
} else if (wasDropped) {
ksqlEngine.terminateQuery(queryId, false);
return false;
} else {
persistentQueryMetadata.getKafkaStreams().start();
return true;
}
} else {
throw new Exception(String.format("Unexpected query metadata type: %s; was expecting %s", queryMetadata.getClass().getCanonicalName(), PersistentQueryMetadata.class.getCanonicalName()));
}
}
use of io.confluent.ksql.parser.tree.QuerySpecification in project ksql by confluentinc.
the class KsqlParserTest method testProjectFilter.
@Test
public void testProjectFilter() throws Exception {
String queryStr = "SELECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
Query query = (Query) statement;
Assert.assertTrue("testProjectFilter fails", query.getQueryBody() instanceof QuerySpecification);
QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
Assert.assertTrue("testProjectFilter fails", querySpecification.getWhere().get() instanceof ComparisonExpression);
ComparisonExpression comparisonExpression = (ComparisonExpression) querySpecification.getWhere().get();
Assert.assertTrue("testProjectFilter fails", comparisonExpression.toString().equalsIgnoreCase("(TEST1.COL0 > 100)"));
Assert.assertTrue("testProjectFilter fails", querySpecification.getSelect().getSelectItems().size() == 3);
}
use of io.confluent.ksql.parser.tree.QuerySpecification in project ksql by confluentinc.
the class KsqlParserTest method testSimpleQuery.
@Test
public void testSimpleQuery() throws Exception {
String simpleQuery = "SELECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
Statement statement = KSQL_PARSER.buildAst(simpleQuery, metaStore).get(0);
Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
Query query = (Query) statement;
Assert.assertTrue("testSimpleQuery fails", query.getQueryBody() instanceof QuerySpecification);
QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
Assert.assertTrue("testSimpleQuery fails", querySpecification.getSelect().getSelectItems().size() == 3);
assertThat(querySpecification.getFrom(), not(nullValue()));
Assert.assertTrue("testSimpleQuery fails", querySpecification.getWhere().isPresent());
Assert.assertTrue("testSimpleQuery fails", querySpecification.getWhere().get() instanceof ComparisonExpression);
ComparisonExpression comparisonExpression = (ComparisonExpression) querySpecification.getWhere().get();
Assert.assertTrue("testSimpleQuery fails", comparisonExpression.getType().getValue().equalsIgnoreCase(">"));
}
use of io.confluent.ksql.parser.tree.QuerySpecification in project ksql by confluentinc.
the class KsqlParserTest method testSimpleLeftJoin.
@Test
public void testSimpleLeftJoin() throws Exception {
String queryStr = "SELECT t1.col1, t2.col1, t2.col4, col5, t2.col2 FROM test1 t1 LEFT JOIN test2 t2 ON " + "t1.col1 = t2.col1;";
Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
Query query = (Query) statement;
Assert.assertTrue("testSimpleLeftJoin fails", query.getQueryBody() instanceof QuerySpecification);
QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
Assert.assertTrue("testSimpleLeftJoin fails", querySpecification.getFrom() instanceof Join);
Join join = (Join) querySpecification.getFrom();
Assert.assertTrue("testSimpleLeftJoin fails", join.getType().toString().equalsIgnoreCase("LEFT"));
Assert.assertTrue("testSimpleLeftJoin fails", ((AliasedRelation) join.getLeft()).getAlias().equalsIgnoreCase("T1"));
Assert.assertTrue("testSimpleLeftJoin fails", ((AliasedRelation) join.getRight()).getAlias().equalsIgnoreCase("T2"));
}
use of io.confluent.ksql.parser.tree.QuerySpecification in project ksql by confluentinc.
the class KsqlParserTest method testBinaryExpression.
@Test
public void testBinaryExpression() throws Exception {
String queryStr = "SELECT col0+10, col2, col3-col1 FROM test1;";
Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
Assert.assertTrue("testBinaryExpression fails", statement instanceof Query);
Query query = (Query) statement;
Assert.assertTrue("testBinaryExpression fails", query.getQueryBody() instanceof QuerySpecification);
QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
SingleColumn column0 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(0);
Assert.assertTrue("testBinaryExpression fails", column0.getAlias().get().equalsIgnoreCase("KSQL_COL_0"));
Assert.assertTrue("testBinaryExpression fails", column0.getExpression().toString().equalsIgnoreCase("(TEST1.COL0 + 10)"));
}
Aggregations