Search in sources :

Example 16 with Statement

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

the class KsqlParserTest method testUDF.

@Test
public void testUDF() throws Exception {
    String queryStr = "SELECT lcase(col1), concat(col2,'hello'), floor(abs(col3)) 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();
    SingleColumn column0 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(0);
    Assert.assertTrue("testProjection fails", column0.getAlias().get().equalsIgnoreCase("KSQL_COL_0"));
    Assert.assertTrue("testProjection fails", column0.getExpression().toString().equalsIgnoreCase("LCASE(T1.COL1)"));
    SingleColumn column1 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(1);
    Assert.assertTrue("testProjection fails", column1.getAlias().get().equalsIgnoreCase("KSQL_COL_1"));
    Assert.assertTrue("testProjection fails", column1.getExpression().toString().equalsIgnoreCase("CONCAT(T1.COL2, 'hello')"));
    SingleColumn column2 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(2);
    Assert.assertTrue("testProjection fails", column2.getAlias().get().equalsIgnoreCase("KSQL_COL_2"));
    Assert.assertTrue("testProjection fails", column2.getExpression().toString().equalsIgnoreCase("FLOOR(ABS(T1.COL3))"));
}
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 Statement

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

the class KsqlParserTest method testSelectSessionWindow.

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

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

the class KsqlParserTest method testCreateStreamAsSelect.

@Test
public void testCreateStreamAsSelect() throws Exception {
    String queryStr = "CREATE STREAM bigorders_json WITH (value_format = 'json', " + "kafka_topic='bigorders_topic') AS SELECT * FROM orders WHERE orderunits > 5 ;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testCreateStreamAsSelect failed.", statement instanceof CreateStreamAsSelect);
    CreateStreamAsSelect createStreamAsSelect = (CreateStreamAsSelect) statement;
    Assert.assertTrue("testCreateTable failed.", createStreamAsSelect.getName().toString().equalsIgnoreCase("bigorders_json"));
    Assert.assertTrue("testCreateTable failed.", createStreamAsSelect.getQuery().getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) createStreamAsSelect.getQuery().getQueryBody();
    Assert.assertTrue("testCreateTable failed.", querySpecification.getSelect().getSelectItems().size() == 4);
    Assert.assertTrue("testCreateTable failed.", querySpecification.getWhere().get().toString().equalsIgnoreCase("(ORDERS.ORDERUNITS > 5)"));
    Assert.assertTrue("testCreateTable failed.", ((AliasedRelation) querySpecification.getFrom()).getAlias().equalsIgnoreCase("ORDERS"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Statement(io.confluent.ksql.parser.tree.Statement) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) Test(org.junit.Test)

Example 19 with Statement

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

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

the class KsqlParserTest method testShouldFailIfWrongKeyword.

@Test
public void testShouldFailIfWrongKeyword() throws Exception {
    try {
        String simpleQuery = "SELLECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
        Statement statement = KSQL_PARSER.buildAst(simpleQuery, metaStore).get(0);
        Assert.fail();
    } catch (ParseFailedException e) {
        String errorMessage = e.getMessage();
        Assert.assertTrue(errorMessage.toLowerCase().contains(("line 1:1: mismatched input 'SELLECT'" + " expecting").toLowerCase()));
    }
}
Also used : Statement(io.confluent.ksql.parser.tree.Statement) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) Test(org.junit.Test)

Aggregations

Statement (io.confluent.ksql.parser.tree.Statement)54 Test (org.junit.Test)38 Query (io.confluent.ksql.parser.tree.Query)24 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)18 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)7 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)7 KsqlException (io.confluent.ksql.util.KsqlException)7 Analysis (io.confluent.ksql.analyzer.Analysis)6 AnalysisContext (io.confluent.ksql.analyzer.AnalysisContext)6 Analyzer (io.confluent.ksql.analyzer.Analyzer)6 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)5 Expression (io.confluent.ksql.parser.tree.Expression)5 ArrayList (java.util.ArrayList)5 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)4 PlanNode (io.confluent.ksql.planner.plan.PlanNode)4 AggregateAnalysis (io.confluent.ksql.analyzer.AggregateAnalysis)3 AggregateAnalyzer (io.confluent.ksql.analyzer.AggregateAnalyzer)3 ParseFailedException (io.confluent.ksql.parser.exception.ParseFailedException)3 AbstractStreamCreateStatement (io.confluent.ksql.parser.tree.AbstractStreamCreateStatement)3 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)3