use of io.confluent.ksql.util.AggregateExpressionRewriter 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.util.AggregateExpressionRewriter 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;
}
use of io.confluent.ksql.util.AggregateExpressionRewriter in project ksql by confluentinc.
the class QueryAnalyzer method analyzeAggregate.
public AggregateAnalysis analyzeAggregate(final Query query, final Analysis analysis) {
AggregateAnalysis aggregateAnalysis = new AggregateAnalysis();
AggregateAnalyzer aggregateAnalyzer = new AggregateAnalyzer(aggregateAnalysis, analysis, functionRegistry);
AggregateExpressionRewriter aggregateExpressionRewriter = new AggregateExpressionRewriter(functionRegistry);
processSelectExpressions(analysis, aggregateAnalysis, aggregateAnalyzer, aggregateExpressionRewriter);
if (!aggregateAnalysis.getAggregateFunctionArguments().isEmpty() && analysis.getGroupByExpressions().isEmpty()) {
throw new KsqlException("Aggregate query needs GROUP BY clause. query:" + query);
}
// TODO: make sure only aggregates are in the expression. For now we assume this is the case.
if (analysis.getHavingExpression() != null) {
processHavingExpression(analysis, aggregateAnalysis, aggregateAnalyzer, aggregateExpressionRewriter);
}
enforceAggregateRules(query, aggregateAnalysis);
return aggregateAnalysis;
}
Aggregations