Search in sources :

Example 1 with Statement

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

the class LogicalPlanBuilder method buildLogicalPlan.

public PlanNode buildLogicalPlan(String queryStr) {
    List<Statement> statements = parser.buildAst(queryStr, metaStore);
    Analysis analysis = new Analysis();
    Analyzer analyzer = new Analyzer(queryStr, analysis, metaStore);
    analyzer.process(statements.get(0), new AnalysisContext(null));
    AggregateAnalysis aggregateAnalysis = new AggregateAnalysis();
    AggregateAnalyzer aggregateAnalyzer = new AggregateAnalyzer(aggregateAnalysis, analysis, functionRegistry);
    AggregateExpressionRewriter aggregateExpressionRewriter = new AggregateExpressionRewriter(functionRegistry);
    for (Expression expression : analysis.getSelectExpressions()) {
        aggregateAnalyzer.process(expression, new AnalysisContext(null));
        if (!aggregateAnalyzer.isHasAggregateFunction()) {
            aggregateAnalysis.addNonAggResultColumns(expression);
        }
        aggregateAnalysis.addFinalSelectExpression(ExpressionTreeRewriter.rewriteWith(aggregateExpressionRewriter, expression));
        aggregateAnalyzer.setHasAggregateFunction(false);
    }
    // Build a logical plan
    return new LogicalPlanner(analysis, aggregateAnalysis, functionRegistry).buildPlan();
}
Also used : LogicalPlanner(io.confluent.ksql.planner.LogicalPlanner) AggregateAnalyzer(io.confluent.ksql.analyzer.AggregateAnalyzer) Expression(io.confluent.ksql.parser.tree.Expression) Statement(io.confluent.ksql.parser.tree.Statement) Analysis(io.confluent.ksql.analyzer.Analysis) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) AnalysisContext(io.confluent.ksql.analyzer.AnalysisContext) AggregateExpressionRewriter(io.confluent.ksql.util.AggregateExpressionRewriter) AggregateAnalyzer(io.confluent.ksql.analyzer.AggregateAnalyzer) Analyzer(io.confluent.ksql.analyzer.Analyzer)

Example 2 with Statement

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

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

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

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

the class ExpressionTypeManagerTest method analyzeQuery.

private Analysis analyzeQuery(String queryStr) {
    List<Statement> statements = KSQL_PARSER.buildAst(queryStr, metaStore);
    // Analyze the query to resolve the references and extract oeprations
    Analysis analysis = new Analysis();
    Analyzer analyzer = new Analyzer("sqlExpression", analysis, metaStore);
    analyzer.process(statements.get(0), new AnalysisContext(null));
    return analysis;
}
Also used : Statement(io.confluent.ksql.parser.tree.Statement) Analysis(io.confluent.ksql.analyzer.Analysis) AnalysisContext(io.confluent.ksql.analyzer.AnalysisContext) Analyzer(io.confluent.ksql.analyzer.Analyzer)

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