Search in sources :

Example 16 with QuerySpecification

use of io.confluent.ksql.parser.tree.QuerySpecification 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 17 with QuerySpecification

use of io.confluent.ksql.parser.tree.QuerySpecification 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)

Example 18 with QuerySpecification

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

the class KsqlParserTest method testSelectSinkProperties.

@Test
public void testSelectSinkProperties() throws Exception {
    String simpleQuery = "create stream s1 with (timestamp='orderid', partitions = 3) as select " + "col1, col2" + " from orders where col2 is null and col3 is not null or (col3*col2 = " + "12);";
    Statement statement = KSQL_PARSER.buildAst(simpleQuery, metaStore).get(0);
    Assert.assertTrue("testSelectTumblingWindow failed.", statement instanceof CreateStreamAsSelect);
    CreateStreamAsSelect createStreamAsSelect = (CreateStreamAsSelect) statement;
    Assert.assertTrue("testSelectTumblingWindow failed.", createStreamAsSelect.getQuery().getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) createStreamAsSelect.getQuery().getQueryBody();
    Assert.assertTrue(querySpecification.getWhere().toString().equalsIgnoreCase("Optional[(((ORDERS.COL2 IS NULL) AND (ORDERS.COL3 IS NOT NULL)) OR ((ORDERS.COL3 * ORDERS.COL2) = 12))]"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Statement(io.confluent.ksql.parser.tree.Statement) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) Test(org.junit.Test)

Example 19 with QuerySpecification

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

the class KsqlParserTest method testLiterals.

@Test
public void testLiterals() throws Exception {
    String queryStr = "SELECT 10, col2, 'test', 2.5, true, -5 FROM test1;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testLiterals fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testLiterals fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    SingleColumn column0 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(0);
    Assert.assertTrue("testLiterals fails", column0.getAlias().get().equalsIgnoreCase("KSQL_COL_0"));
    Assert.assertTrue("testLiterals fails", column0.getExpression().toString().equalsIgnoreCase("10"));
    SingleColumn column1 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(1);
    Assert.assertTrue("testLiterals fails", column1.getAlias().get().equalsIgnoreCase("COL2"));
    Assert.assertTrue("testLiterals fails", column1.getExpression().toString().equalsIgnoreCase("TEST1.COL2"));
    SingleColumn column2 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(2);
    Assert.assertTrue("testLiterals fails", column2.getAlias().get().equalsIgnoreCase("KSQL_COL_2"));
    Assert.assertTrue("testLiterals fails", column2.getExpression().toString().equalsIgnoreCase("'test'"));
    SingleColumn column3 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(3);
    Assert.assertTrue("testLiterals fails", column3.getAlias().get().equalsIgnoreCase("KSQL_COL_3"));
    Assert.assertTrue("testLiterals fails", column3.getExpression().toString().equalsIgnoreCase("2.5"));
    SingleColumn column4 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(4);
    Assert.assertTrue("testLiterals fails", column4.getAlias().get().equalsIgnoreCase("KSQL_COL_4"));
    Assert.assertTrue("testLiterals fails", column4.getExpression().toString().equalsIgnoreCase("true"));
    SingleColumn column5 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(5);
    Assert.assertTrue("testLiterals fails", column5.getAlias().get().equalsIgnoreCase("KSQL_COL_5"));
    Assert.assertTrue("testLiterals fails", column5.getExpression().toString().equalsIgnoreCase("-5"));
}
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 20 with QuerySpecification

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

the class AstBuilder method visitQuerySpecification.

@Override
public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) {
    Table into;
    if (context.into != null) {
        into = (Table) visit(context.into);
    } else {
        // TODO: Generate a unique name
        String intoName = "KSQL_Stream_" + System.currentTimeMillis();
        into = new Table(QualifiedName.of(intoName), true);
    }
    Relation from = (Relation) visit(context.from);
    Select select = new Select(getLocation(context.SELECT()), false, visit(context.selectItem(), SelectItem.class));
    select = new Select(getLocation(context.SELECT()), select.isDistinct(), extractSelectItems(select, from));
    getResultDatasource(select, into);
    return new QuerySpecification(getLocation(context), select, into, from, visitIfPresent(context.windowExpression(), WindowExpression.class), visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), Optional.<String>empty());
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Relation(io.confluent.ksql.parser.tree.Relation) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) DropTable(io.confluent.ksql.parser.tree.DropTable) Table(io.confluent.ksql.parser.tree.Table) CreateTable(io.confluent.ksql.parser.tree.CreateTable) SimpleGroupBy(io.confluent.ksql.parser.tree.SimpleGroupBy) GroupBy(io.confluent.ksql.parser.tree.GroupBy) InListExpression(io.confluent.ksql.parser.tree.InListExpression) NullIfExpression(io.confluent.ksql.parser.tree.NullIfExpression) SimpleCaseExpression(io.confluent.ksql.parser.tree.SimpleCaseExpression) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) LogicalBinaryExpression(io.confluent.ksql.parser.tree.LogicalBinaryExpression) TumblingWindowExpression(io.confluent.ksql.parser.tree.TumblingWindowExpression) ArithmeticBinaryExpression(io.confluent.ksql.parser.tree.ArithmeticBinaryExpression) NotExpression(io.confluent.ksql.parser.tree.NotExpression) HoppingWindowExpression(io.confluent.ksql.parser.tree.HoppingWindowExpression) SubscriptExpression(io.confluent.ksql.parser.tree.SubscriptExpression) SessionWindowExpression(io.confluent.ksql.parser.tree.SessionWindowExpression) SearchedCaseExpression(io.confluent.ksql.parser.tree.SearchedCaseExpression) LambdaExpression(io.confluent.ksql.parser.tree.LambdaExpression) SubqueryExpression(io.confluent.ksql.parser.tree.SubqueryExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) ArithmeticUnaryExpression(io.confluent.ksql.parser.tree.ArithmeticUnaryExpression) SelectItem(io.confluent.ksql.parser.tree.SelectItem) TumblingWindowExpression(io.confluent.ksql.parser.tree.TumblingWindowExpression) HoppingWindowExpression(io.confluent.ksql.parser.tree.HoppingWindowExpression) SessionWindowExpression(io.confluent.ksql.parser.tree.SessionWindowExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) CreateTableAsSelect(io.confluent.ksql.parser.tree.CreateTableAsSelect) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) Select(io.confluent.ksql.parser.tree.Select)

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