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));
}
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"));
}
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"))));
}
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);
}
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(">"));
}
Aggregations