Search in sources :

Example 16 with Query

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

the class KsqlParserTest method testProjection.

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

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

the class KsqlParserTest method testProjectionWithArrayMap.

@Test
public void testProjectionWithArrayMap() throws Exception {
    String queryStr = "SELECT col0, col2, col3, col4[0], col5['key1'] FROM test1;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testProjectionWithArrayMap fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testProjectionWithArrayMap fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testProjectionWithArrayMap fails", querySpecification.getSelect().getSelectItems().size() == 5);
    Assert.assertTrue("testProjectionWithArrayMap fails", querySpecification.getSelect().getSelectItems().get(0) instanceof SingleColumn);
    SingleColumn column0 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(0);
    Assert.assertTrue("testProjectionWithArrayMap fails", column0.getAlias().get().equalsIgnoreCase("COL0"));
    Assert.assertTrue("testProjectionWithArrayMap fails", column0.getExpression().toString().equalsIgnoreCase("TEST1.COL0"));
    SingleColumn column3 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(3);
    SingleColumn column4 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(4);
    Assert.assertTrue("testProjectionWithArrayMap fails", column3.getExpression().toString().equalsIgnoreCase("TEST1.COL4[0]"));
    Assert.assertTrue("testProjectionWithArrayMap fails", column4.getExpression().toString().equalsIgnoreCase("TEST1.COL5['key1']"));
}
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 18 with Query

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

the class KsqlParserTest method testSelectAllJoin.

@Test
public void testSelectAllJoin() throws Exception {
    String queryStr = "SELECT * FROM test1 t1 LEFT JOIN test2 t2 ON t1.col1 = t2.col1 WHERE t2.col2 = 'test';";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testLeftJoinWithFilter fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testSelectAllJoin fails", querySpecification.getFrom() instanceof Join);
    Join join = (Join) querySpecification.getFrom();
    Assert.assertTrue("testSelectAllJoin fails", querySpecification.getSelect().getSelectItems().size() == 11);
    Assert.assertTrue("testLeftJoinWithFilter fails", ((AliasedRelation) join.getLeft()).getAlias().equalsIgnoreCase("T1"));
    Assert.assertTrue("testLeftJoinWithFilter 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 19 with Query

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

the class KsqlParserTest method testLeftJoinWithFilter.

@Test
public void testLeftJoinWithFilter() throws Exception {
    String queryStr = "SELECT t1.col1, t2.col1, t2.col4, t2.col2 FROM test1 t1 LEFT JOIN test2 t2 ON t1.col1 = " + "t2.col1 WHERE t2.col2 = 'test';";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testLeftJoinWithFilter fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testLeftJoinWithFilter fails", querySpecification.getFrom() instanceof Join);
    Join join = (Join) querySpecification.getFrom();
    Assert.assertTrue("testLeftJoinWithFilter fails", join.getType().toString().equalsIgnoreCase("LEFT"));
    Assert.assertTrue("testLeftJoinWithFilter fails", ((AliasedRelation) join.getLeft()).getAlias().equalsIgnoreCase("T1"));
    Assert.assertTrue("testLeftJoinWithFilter fails", ((AliasedRelation) join.getRight()).getAlias().equalsIgnoreCase("T2"));
    Assert.assertTrue("testLeftJoinWithFilter fails", querySpecification.getWhere().get().toString().equalsIgnoreCase("(T2.COL2 = 'test')"));
}
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 20 with Query

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

the class KsqlParserTest method testSelectTumblingWindow.

@Test
public void testSelectTumblingWindow() throws Exception {
    String queryStr = "select itemid, sum(orderunits) from orders window TUMBLING ( size 30 second) where orderunits > 5 group by itemid;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testSelectTumblingWindow failed.", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testSelectTumblingWindow failed.", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testCreateTable failed.", querySpecification.getSelect().getSelectItems().size() == 2);
    Assert.assertTrue("testSelectTumblingWindow failed.", querySpecification.getWhere().get().toString().equalsIgnoreCase("(ORDERS.ORDERUNITS > 5)"));
    Assert.assertTrue("testSelectTumblingWindow failed.", ((AliasedRelation) querySpecification.getFrom()).getAlias().equalsIgnoreCase("ORDERS"));
    Assert.assertTrue("testSelectTumblingWindow failed.", querySpecification.getWindowExpression().isPresent());
    Assert.assertTrue("testSelectTumblingWindow failed.", querySpecification.getWindowExpression().get().toString().equalsIgnoreCase(" WINDOW STREAMWINDOW  TUMBLING ( SIZE 30 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