Search in sources :

Example 1 with Query

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

the class QueryAnalyzerTest method shouldAnalyseWindowedAggregate.

@Test
public void shouldAnalyseWindowedAggregate() {
    final List<Statement> statements = ksqlParser.buildAst("select itemid, sum(orderunits) from orders window TUMBLING ( size 30 second) " + "where orderunits > 5 group by itemid;", metaStore);
    final Query query = (Query) statements.get(0);
    final Analysis analysis = queryAnalyzer.analyze("sqlExpression", query);
    final AggregateAnalysis aggregateAnalysis = queryAnalyzer.analyzeAggregate(query, analysis);
    final DereferenceExpression itemId = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("ORDERS")), "ITEMID");
    final DereferenceExpression orderUnits = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("ORDERS")), "ORDERUNITS");
    final Map<String, Expression> expectedRequiredColumns = new HashMap<>();
    expectedRequiredColumns.put("ORDERS.ITEMID", itemId);
    expectedRequiredColumns.put("ORDERS.ORDERUNITS", orderUnits);
    assertThat(aggregateAnalysis.getNonAggResultColumns(), equalTo(Collections.singletonList(itemId)));
    assertThat(aggregateAnalysis.getFinalSelectExpressions(), equalTo(Arrays.asList(itemId, new QualifiedNameReference(QualifiedName.of("KSQL_AGG_VARIABLE_0")))));
    assertThat(aggregateAnalysis.getAggregateFunctionArguments(), equalTo(Collections.singletonList(orderUnits)));
    assertThat(aggregateAnalysis.getRequiredColumnsMap(), equalTo(expectedRequiredColumns));
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Query(io.confluent.ksql.parser.tree.Query) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) Statement(io.confluent.ksql.parser.tree.Statement) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 2 with Query

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

the class QueryAnalyzerTest method shouldPassJoinWithAnyCriteriaOrder.

@Test
public void shouldPassJoinWithAnyCriteriaOrder() {
    final List<Statement> statements = ksqlParser.buildAst("select * from test1 left join test2 on test2.col2 = test1.col1;", metaStore);
    final Query query = (Query) statements.get(0);
    final Analysis analysis = queryAnalyzer.analyze("sqlExpression", query);
    assertTrue(analysis.getJoin().isLeftJoin());
    assertThat(analysis.getJoin().getLeftKeyFieldName(), equalTo("COL1"));
    assertThat(analysis.getJoin().getRightKeyFieldName(), equalTo("COL2"));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Test(org.junit.Test)

Example 3 with Query

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

the class QueryAnalyzerTest method shouldProcessHavingExpression.

@Test
public void shouldProcessHavingExpression() {
    final List<Statement> statements = ksqlParser.buildAst("select itemid, sum(orderunits) from orders window TUMBLING ( size 30 second) " + "where orderunits > 5 group by itemid having count(itemid) > 10;", metaStore);
    final Query query = (Query) statements.get(0);
    final Analysis analysis = queryAnalyzer.analyze("sqlExpression", query);
    final AggregateAnalysis aggregateAnalysis = queryAnalyzer.analyzeAggregate(query, analysis);
    final Expression havingExpression = aggregateAnalysis.getHavingExpression();
    assertThat(havingExpression, equalTo(new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, new QualifiedNameReference(QualifiedName.of("KSQL_AGG_VARIABLE_1")), new LongLiteral("10"))));
}
Also used : ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) LongLiteral(io.confluent.ksql.parser.tree.LongLiteral) Statement(io.confluent.ksql.parser.tree.Statement) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 4 with Query

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

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

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