Search in sources :

Example 46 with Statement

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

the class AstBuilder method visitStatements.

@Override
public Node visitStatements(SqlBaseParser.StatementsContext context) {
    List<Statement> statementList = new ArrayList<>();
    for (SqlBaseParser.SingleStatementContext singleStatementContext : context.singleStatement()) {
        Statement statement = (Statement) visitSingleStatement(singleStatementContext);
        statementList.add(statement);
    }
    return new Statements(statementList);
}
Also used : Statement(io.confluent.ksql.parser.tree.Statement) ArrayList(java.util.ArrayList) Statements(io.confluent.ksql.parser.tree.Statements)

Example 47 with Statement

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

the class QueryAnalyzerTest method shouldCreateAnalysisForSimpleQuery.

@Test
public void shouldCreateAnalysisForSimpleQuery() {
    final List<Statement> statements = ksqlParser.buildAst("select orderid from orders;", metaStore);
    final Analysis analysis = queryAnalyzer.analyze("sqlExpression", (Query) statements.get(0));
    final Pair<StructuredDataSource, String> fromDataSource = analysis.getFromDataSource(0);
    assertThat(analysis.getSelectExpressions(), equalTo(Collections.singletonList(new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("ORDERS")), "ORDERID"))));
    assertThat(analysis.getFromDataSources().size(), equalTo(1));
    assertThat(fromDataSource.left, instanceOf(KsqlStream.class));
    assertThat(fromDataSource.right, equalTo("ORDERS"));
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlStream(io.confluent.ksql.metastore.KsqlStream) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Statement(io.confluent.ksql.parser.tree.Statement) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 48 with Statement

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

the class QueryAnalyzerTest method shouldThrowExceptionIfNonAggregateSelectsDontMatchGroupBySize.

@Test
public void shouldThrowExceptionIfNonAggregateSelectsDontMatchGroupBySize() {
    final List<Statement> statements = ksqlParser.buildAst("select itemid, orderid, 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);
    try {
        queryAnalyzer.analyzeAggregate(query, analysis);
        fail("should have thrown KsqlException as aggregate query doesn't have a groupby clause");
    } catch (KsqlException e) {
    // ok
    }
}
Also used : Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 49 with Statement

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

the class QueryAnalyzerTest method shouldFailWithIncorrectJoinCriteria.

@Test
public void shouldFailWithIncorrectJoinCriteria() {
    final List<Statement> statements = ksqlParser.buildAst("select * from test1 join test2 on test1.col1 = test2.coll;", metaStore);
    final Query query = (Query) statements.get(0);
    try {
        queryAnalyzer.analyze("sqlExpression", query);
    } catch (KsqlException ex) {
        assertThat(ex.getMessage().trim(), equalTo("Line: 1, Col: 46 : Invalid join criteria (TEST1.COL1 = TEST2.COLL). Key for TEST2 is not set correctly."));
    }
}
Also used : Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 50 with Statement

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

the class QueryAnalyzerTest method shouldThrowExceptionIfAggregateAnalysisDoesntHaveGroupBy.

@Test
public void shouldThrowExceptionIfAggregateAnalysisDoesntHaveGroupBy() {
    final List<Statement> statements = ksqlParser.buildAst("select itemid, sum(orderunits) from orders window TUMBLING ( size 30 second) " + "where orderunits > 5;", metaStore);
    final Query query = (Query) statements.get(0);
    final Analysis analysis = queryAnalyzer.analyze("sqlExpression", query);
    try {
        queryAnalyzer.analyzeAggregate(query, analysis);
        fail("should have thrown KsqlException as aggregate query doesn't have a groupby clause");
    } catch (KsqlException e) {
    // ok
    }
}
Also used : Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) KsqlException(io.confluent.ksql.util.KsqlException) 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