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