use of io.confluent.ksql.parser.tree.QualifiedNameReference 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.QualifiedNameReference 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.QualifiedNameReference in project ksql by confluentinc.
the class AstBuilder method getSelectStartItems.
private List<SelectItem> getSelectStartItems(final SelectItem selectItem, final Relation from) {
List<SelectItem> selectItems = new ArrayList<>();
AllColumns allColumns = (AllColumns) selectItem;
if (from instanceof Join) {
Join join = (Join) from;
AliasedRelation left = (AliasedRelation) join.getLeft();
StructuredDataSource leftDataSource = dataSourceExtractor.getMetaStore().getSource(left.getRelation().toString());
if (leftDataSource == null) {
throw new InvalidColumnReferenceException(left.getRelation().toString() + " does not exist.");
}
AliasedRelation right = (AliasedRelation) join.getRight();
StructuredDataSource rightDataSource = dataSourceExtractor.getMetaStore().getSource(right.getRelation().toString());
if (rightDataSource == null) {
throw new InvalidColumnReferenceException(right.getRelation().toString() + " does not exist.");
}
for (Field field : leftDataSource.getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(left.getAlias() + "." + field.name()));
SingleColumn newSelectItem = new SingleColumn(qualifiedNameReference, left.getAlias() + "_" + field.name());
selectItems.add(newSelectItem);
}
for (Field field : rightDataSource.getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(right.getAlias() + "." + field.name()));
SingleColumn newSelectItem = new SingleColumn(qualifiedNameReference, right.getAlias() + "_" + field.name());
selectItems.add(newSelectItem);
}
} else {
AliasedRelation fromRel = (AliasedRelation) from;
StructuredDataSource fromDataSource = dataSourceExtractor.getMetaStore().getSource(((Table) fromRel.getRelation()).getName().getSuffix());
if (fromDataSource == null) {
throw new InvalidColumnReferenceException(((Table) fromRel.getRelation()).getName().getSuffix() + " does not exist.");
}
for (Field field : fromDataSource.getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(fromDataSource.getName() + "." + field.name()));
SingleColumn newSelectItem = new SingleColumn(qualifiedNameReference, field.name());
selectItems.add(newSelectItem);
}
}
return selectItems;
}
use of io.confluent.ksql.parser.tree.QualifiedNameReference in project ksql by confluentinc.
the class AstBuilder method visitSelectSingle.
@Override
public Node visitSelectSingle(SqlBaseParser.SelectSingleContext context) {
Expression selectItemExpression = (Expression) visit(context.expression());
Optional<String> alias = Optional.ofNullable(context.identifier()).map(AstBuilder::getIdentifierText);
if (!alias.isPresent()) {
if (selectItemExpression instanceof QualifiedNameReference) {
QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) selectItemExpression;
alias = Optional.of(qualifiedNameReference.getName().getSuffix());
} else if (selectItemExpression instanceof DereferenceExpression) {
DereferenceExpression dereferenceExpression = (DereferenceExpression) selectItemExpression;
if ((dataSourceExtractor.getJoinLeftSchema() != null) && (dataSourceExtractor.getCommonFieldNames().contains(dereferenceExpression.getFieldName()))) {
alias = Optional.of(dereferenceExpression.getBase().toString() + "_" + dereferenceExpression.getFieldName());
} else {
alias = Optional.of(dereferenceExpression.getFieldName());
}
} else {
alias = Optional.of("KSQL_COL_" + selectItemIndex);
}
} else {
alias = Optional.of(alias.get());
}
selectItemIndex++;
return new SingleColumn(getLocation(context), selectItemExpression, alias);
}
use of io.confluent.ksql.parser.tree.QualifiedNameReference in project ksql by confluentinc.
the class AggregateAnalyzer method visitFunctionCall.
@Override
protected Node visitFunctionCall(final FunctionCall node, final AnalysisContext context) {
String functionName = node.getName().getSuffix();
if (functionRegistry.isAnAggregateFunction(functionName)) {
if (node.getArguments().isEmpty()) {
Expression argExpression;
if (analysis.getJoin() != null) {
Expression baseExpression = new QualifiedNameReference(QualifiedName.of(analysis.getJoin().getLeftAlias()));
argExpression = new DereferenceExpression(baseExpression, SchemaUtil.ROWTIME_NAME);
} else {
Expression baseExpression = new QualifiedNameReference(QualifiedName.of(analysis.getFromDataSources().get(0).getRight()));
argExpression = new DereferenceExpression(baseExpression, SchemaUtil.ROWTIME_NAME);
}
aggregateAnalysis.addAggregateFunctionArgument(argExpression);
node.getArguments().add(argExpression);
} else {
aggregateAnalysis.addAggregateFunctionArgument(node.getArguments().get(0));
}
aggregateAnalysis.addFunction(node);
hasAggregateFunction = true;
}
for (Expression argExp : node.getArguments()) {
process(argExp, context);
}
return null;
}
Aggregations