use of io.confluent.ksql.parser.tree.DereferenceExpression 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.DereferenceExpression 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.DereferenceExpression 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;
}
use of io.confluent.ksql.parser.tree.DereferenceExpression in project ksql by confluentinc.
the class Analyzer method fetchKeyFieldNameFromExpr.
/**
* Given an expression and the source alias detects if the expression type is
* DereferenceExpression
* or QualifiedNameReference and if the variable prefix matches the source Alias.
*/
private Pair<String, String> fetchKeyFieldNameFromExpr(Expression expression, String sourceAlias, Schema sourceSchema) {
if (expression instanceof DereferenceExpression) {
DereferenceExpression dereferenceExpression = (DereferenceExpression) expression;
String sourceAliasVal = dereferenceExpression.getBase().toString();
if (sourceAliasVal.equalsIgnoreCase(sourceAlias)) {
String keyFieldName = dereferenceExpression.getFieldName();
if (SchemaUtil.getFieldByName(sourceSchema, keyFieldName).isPresent()) {
return new Pair<>(sourceAliasVal, keyFieldName);
}
}
} else if (expression instanceof QualifiedNameReference) {
QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) expression;
String keyFieldName = qualifiedNameReference.getName().getSuffix();
if (SchemaUtil.getFieldByName(sourceSchema, keyFieldName).isPresent()) {
return new Pair<>(sourceAlias, keyFieldName);
}
}
return null;
}
use of io.confluent.ksql.parser.tree.DereferenceExpression in project ksql by confluentinc.
the class SchemaKStreamTest method testGroupByKey.
@Test
public void testGroupByKey() {
String selectQuery = "SELECT col0, col1 FROM test1 WHERE col0 > 100;";
PlanNode logicalPlan = planBuilder.buildLogicalPlan(selectQuery);
initialSchemaKStream = new SchemaKStream(logicalPlan.getTheSourceNode().getSchema(), kStream, ksqlStream.getKeyField(), new ArrayList<>(), SchemaKStream.Type.SOURCE, functionRegistry, new MockSchemaRegistryClient());
Expression keyExpression = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("TEST1")), "COL0");
KsqlTopicSerDe ksqlTopicSerDe = new KsqlJsonTopicSerDe();
Serde<GenericRow> rowSerde = ksqlTopicSerDe.getGenericRowSerde(initialSchemaKStream.getSchema(), null, false, null);
List<Expression> groupByExpressions = Arrays.asList(keyExpression);
SchemaKGroupedStream groupedSchemaKStream = initialSchemaKStream.groupBy(Serdes.String(), rowSerde, groupByExpressions);
Assert.assertEquals(groupedSchemaKStream.getKeyField().name(), "COL0");
}
Aggregations