Search in sources :

Example 1 with Expression

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

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

the class AggregateAnalyzerTest method analyzeAggregates.

private AggregateAnalysis analyzeAggregates(final String queryStr) {
    System.out.println("Test query:" + queryStr);
    Analysis analysis = analyze(queryStr);
    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);
    }
    if (analysis.getHavingExpression() != null) {
        aggregateAnalyzer.process(analysis.getHavingExpression(), new AnalysisContext(null));
        if (!aggregateAnalyzer.isHasAggregateFunction()) {
            aggregateAnalysis.addNonAggResultColumns(analysis.getHavingExpression());
        }
        aggregateAnalysis.setHavingExpression(ExpressionTreeRewriter.rewriteWith(aggregateExpressionRewriter, analysis.getHavingExpression()));
        aggregateAnalyzer.setHasAggregateFunction(false);
    }
    return aggregateAnalysis;
}
Also used : Expression(io.confluent.ksql.parser.tree.Expression) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) AggregateExpressionRewriter(io.confluent.ksql.util.AggregateExpressionRewriter)

Example 3 with Expression

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

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

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

the class CommandFactoriesTest method shouldFailCreateTableIfKeyNameIsIncorrect.

@Test
public void shouldFailCreateTableIfKeyNameIsIncorrect() {
    HashMap<String, Expression> tableProperties = new HashMap<>();
    tableProperties.putAll(properties);
    tableProperties.put(DdlConfig.KEY_NAME_PROPERTY, new StringLiteral("COL3"));
    try {
        final DdlCommand result = commandFactories.create(sqlExpression, new CreateTable(QualifiedName.of("foo"), Arrays.asList(new TableElement("COL1", "BIGINT"), new TableElement("COL2", "VARCHAR")), true, tableProperties), Collections.emptyMap());
    } catch (KsqlException e) {
        assertThat(e.getMessage(), equalTo("No column with the provided key column name in the " + "WITH clause, COL3, exists in the defined schema."));
    }
}
Also used : StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) CreateTable(io.confluent.ksql.parser.tree.CreateTable) EasyMock.anyString(org.easymock.EasyMock.anyString) KsqlException(io.confluent.ksql.util.KsqlException) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Aggregations

Expression (io.confluent.ksql.parser.tree.Expression)36 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)17 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)13 WindowExpression (io.confluent.ksql.parser.tree.WindowExpression)10 HashMap (java.util.HashMap)10 ArithmeticBinaryExpression (io.confluent.ksql.parser.tree.ArithmeticBinaryExpression)8 ArithmeticUnaryExpression (io.confluent.ksql.parser.tree.ArithmeticUnaryExpression)8 InListExpression (io.confluent.ksql.parser.tree.InListExpression)8 LogicalBinaryExpression (io.confluent.ksql.parser.tree.LogicalBinaryExpression)8 NotExpression (io.confluent.ksql.parser.tree.NotExpression)8 NullIfExpression (io.confluent.ksql.parser.tree.NullIfExpression)8 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)8 SearchedCaseExpression (io.confluent.ksql.parser.tree.SearchedCaseExpression)8 SimpleCaseExpression (io.confluent.ksql.parser.tree.SimpleCaseExpression)8 StringLiteral (io.confluent.ksql.parser.tree.StringLiteral)8 SubqueryExpression (io.confluent.ksql.parser.tree.SubqueryExpression)8 SubscriptExpression (io.confluent.ksql.parser.tree.SubscriptExpression)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 HoppingWindowExpression (io.confluent.ksql.parser.tree.HoppingWindowExpression)7