Search in sources :

Example 11 with Query

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

the class KsqlParserTest method testBooleanExpression.

@Test
public void testBooleanExpression() throws Exception {
    String queryStr = "SELECT col0 = 10, col2, col3 > col1 FROM test1;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testBooleanExpression fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testProjection fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    SingleColumn column0 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(0);
    Assert.assertTrue("testBooleanExpression fails", column0.getAlias().get().equalsIgnoreCase("KSQL_COL_0"));
    Assert.assertTrue("testBooleanExpression 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)

Example 12 with Query

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

the class KsqlEngine method terminateQueryForEntity.

@Override
public void terminateQueryForEntity(final String entity) {
    final Optional<PersistentQueryMetadata> query = persistentQueries.values().stream().filter(persistentQueryMetadata -> persistentQueryMetadata.getEntity().equalsIgnoreCase(entity)).findFirst();
    if (query.isPresent()) {
        final PersistentQueryMetadata metadata = query.get();
        log.info("Terminating persistent query {}", metadata.getId());
        metadata.close();
        persistentQueries.remove(metadata.getId());
        livePersistentQueries.remove(metadata);
        allLiveQueries.remove(metadata);
    }
}
Also used : CreateTableCommand(io.confluent.ksql.ddl.commands.CreateTableCommand) Query(io.confluent.ksql.parser.tree.Query) Interval(org.antlr.v4.runtime.misc.Interval) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) LoggerFactory(org.slf4j.LoggerFactory) CommandFactories(io.confluent.ksql.ddl.commands.CommandFactories) KsqlParser(io.confluent.ksql.parser.KsqlParser) KafkaTopicClient(io.confluent.ksql.util.KafkaTopicClient) Pair(io.confluent.ksql.util.Pair) CreateTable(io.confluent.ksql.parser.tree.CreateTable) Map(java.util.Map) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic) QueryId(io.confluent.ksql.query.QueryId) DropTopic(io.confluent.ksql.parser.tree.DropTopic) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) DdlCommandResult(io.confluent.ksql.ddl.commands.DdlCommandResult) QueryMetadata(io.confluent.ksql.util.QueryMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) DropStream(io.confluent.ksql.parser.tree.DropStream) Set(java.util.Set) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Expression(io.confluent.ksql.parser.tree.Expression) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) DataSourceExtractor(io.confluent.ksql.util.DataSourceExtractor) List(java.util.List) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) SetProperty(io.confluent.ksql.parser.tree.SetProperty) DdlCommandExec(io.confluent.ksql.ddl.commands.DdlCommandExec) Optional(java.util.Optional) KsqlEngineMetrics(io.confluent.ksql.internal.KsqlEngineMetrics) KsqlException(io.confluent.ksql.util.KsqlException) Statement(io.confluent.ksql.parser.tree.Statement) StreamsConfig(org.apache.kafka.streams.StreamsConfig) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) DdlConfig(io.confluent.ksql.ddl.DdlConfig) HashMap(java.util.HashMap) QualifiedName(io.confluent.ksql.parser.tree.QualifiedName) RegisterTopicCommand(io.confluent.ksql.ddl.commands.RegisterTopicCommand) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CreateStream(io.confluent.ksql.parser.tree.CreateStream) CharStream(org.antlr.v4.runtime.CharStream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MetaStore(io.confluent.ksql.metastore.MetaStore) DropSourceCommand(io.confluent.ksql.ddl.commands.DropSourceCommand) DropTable(io.confluent.ksql.parser.tree.DropTable) Logger(org.slf4j.Logger) KsqlSchemaRegistryClientFactory(io.confluent.ksql.schema.registry.KsqlSchemaRegistryClientFactory) DropTopicCommand(io.confluent.ksql.ddl.commands.DropTopicCommand) CreateStreamCommand(io.confluent.ksql.ddl.commands.CreateStreamCommand) Table(io.confluent.ksql.parser.tree.Table) DataSource(io.confluent.ksql.serde.DataSource) TimeUnit(java.util.concurrent.TimeUnit) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) Closeable(java.io.Closeable) PlanNode(io.confluent.ksql.planner.plan.PlanNode) QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Collections(java.util.Collections) SqlBaseParser(io.confluent.ksql.parser.SqlBaseParser) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 13 with Query

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

the class QueryEngine method buildLogicalPlans.

List<Pair<String, PlanNode>> buildLogicalPlans(final MetaStore metaStore, final List<Pair<String, Statement>> statementList) {
    List<Pair<String, PlanNode>> logicalPlansList = new ArrayList<>();
    // TODO: the purpose of tempMetaStore here
    MetaStore tempMetaStore = metaStore.clone();
    for (Pair<String, Statement> statementQueryPair : statementList) {
        if (statementQueryPair.getRight() instanceof Query) {
            PlanNode logicalPlan = buildQueryLogicalPlan(statementQueryPair.getLeft(), (Query) statementQueryPair.getRight(), tempMetaStore);
            logicalPlansList.add(new Pair<>(statementQueryPair.getLeft(), logicalPlan));
        } else {
            logicalPlansList.add(new Pair<>(statementQueryPair.getLeft(), null));
        }
        log.info("Build logical plan for {}.", statementQueryPair.getLeft());
    }
    return logicalPlansList;
}
Also used : MetaStore(io.confluent.ksql.metastore.MetaStore) PlanNode(io.confluent.ksql.planner.plan.PlanNode) Query(io.confluent.ksql.parser.tree.Query) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) Statement(io.confluent.ksql.parser.tree.Statement) ArrayList(java.util.ArrayList) Pair(io.confluent.ksql.util.Pair)

Example 14 with Query

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

the class KsqlParserTest method testSelectAll.

@Test
public void testSelectAll() throws Exception {
    String queryStr = "SELECT * FROM test1 t1;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testSelectAll fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testSelectAll fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testSelectAll fails", querySpecification.getSelect().getSelectItems().size() == 6);
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Test(org.junit.Test)

Example 15 with Query

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

the class KsqlParserTest method testSelectHoppingWindow.

@Test
public void testSelectHoppingWindow() throws Exception {
    String queryStr = "select itemid, sum(orderunits) from orders window HOPPING ( size 30 second, advance by 5" + " seconds) " + "where " + "orderunits" + " > 5 group by itemid;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    assertThat(statement, instanceOf(Query.class));
    Query query = (Query) statement;
    assertThat(query.getQueryBody(), instanceOf(QuerySpecification.class));
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    assertThat(querySpecification.getSelect().getSelectItems().size(), equalTo(2));
    assertThat(querySpecification.getWhere().get().toString(), equalTo("(ORDERS.ORDERUNITS > 5)"));
    assertThat(((AliasedRelation) querySpecification.getFrom()).getAlias().toUpperCase(), equalTo("ORDERS"));
    Assert.assertTrue("window expression isn't present", querySpecification.getWindowExpression().isPresent());
    assertThat(querySpecification.getWindowExpression().get().toString().toUpperCase(), equalTo(" WINDOW STREAMWINDOW  HOPPING ( SIZE 30 SECONDS , ADVANCE BY 5 SECONDS ) "));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) Test(org.junit.Test)

Aggregations

Query (io.confluent.ksql.parser.tree.Query)29 Statement (io.confluent.ksql.parser.tree.Statement)25 Test (org.junit.Test)23 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)20 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)7 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)6 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)4 Expression (io.confluent.ksql.parser.tree.Expression)4 CreateTable (io.confluent.ksql.parser.tree.CreateTable)3 DropTable (io.confluent.ksql.parser.tree.DropTable)3 KsqlException (io.confluent.ksql.util.KsqlException)3 Pair (io.confluent.ksql.util.Pair)3 CreateStreamCommand (io.confluent.ksql.ddl.commands.CreateStreamCommand)2 CreateTableCommand (io.confluent.ksql.ddl.commands.CreateTableCommand)2 DropSourceCommand (io.confluent.ksql.ddl.commands.DropSourceCommand)2 DropTopicCommand (io.confluent.ksql.ddl.commands.DropTopicCommand)2 RegisterTopicCommand (io.confluent.ksql.ddl.commands.RegisterTopicCommand)2 MetaStore (io.confluent.ksql.metastore.MetaStore)2 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2