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