Search in sources :

Example 1 with QuerySpecification

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()));
    }
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Relation(io.confluent.ksql.parser.tree.Relation) Table(io.confluent.ksql.parser.tree.Table) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) QueryId(io.confluent.ksql.query.QueryId) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) WakeupException(org.apache.kafka.common.errors.WakeupException) KsqlException(io.confluent.ksql.util.KsqlException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 2 with QuerySpecification

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);
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Test(org.junit.Test)

Example 3 with QuerySpecification

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(">"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Test(org.junit.Test)

Example 4 with QuerySpecification

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"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Join(io.confluent.ksql.parser.tree.Join) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) Test(org.junit.Test)

Example 5 with QuerySpecification

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)"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) Test(org.junit.Test)

Aggregations

QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)23 Query (io.confluent.ksql.parser.tree.Query)19 Statement (io.confluent.ksql.parser.tree.Statement)18 Test (org.junit.Test)18 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)8 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)7 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)3 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)3 CreateTable (io.confluent.ksql.parser.tree.CreateTable)3 DropTable (io.confluent.ksql.parser.tree.DropTable)3 Join (io.confluent.ksql.parser.tree.Join)3 Table (io.confluent.ksql.parser.tree.Table)3 CreateTableAsSelect (io.confluent.ksql.parser.tree.CreateTableAsSelect)2 Expression (io.confluent.ksql.parser.tree.Expression)2 Relation (io.confluent.ksql.parser.tree.Relation)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