use of io.confluent.ksql.parser.tree.QualifiedNameReference 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.QualifiedNameReference in project ksql by confluentinc.
the class Analyzer method visitSelect.
@Override
protected Node visitSelect(final Select node, final AnalysisContext context) {
for (SelectItem selectItem : node.getSelectItems()) {
if (selectItem instanceof AllColumns) {
// expand * and T.*
AllColumns allColumns = (AllColumns) selectItem;
if ((this.analysis.getFromDataSources() == null) || (this.analysis.getFromDataSources().isEmpty())) {
throw new KsqlException("FROM clause was not resolved!");
}
if (analysis.getJoin() != null) {
JoinNode joinNode = analysis.getJoin();
for (Field field : joinNode.getLeft().getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(joinNode.getLeftAlias() + "." + field.name()));
analysis.addSelectItem(qualifiedNameReference, joinNode.getLeftAlias() + "_" + field.name());
}
for (Field field : joinNode.getRight().getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(joinNode.getRightAlias() + "." + field.name()));
analysis.addSelectItem(qualifiedNameReference, joinNode.getRightAlias() + "_" + field.name());
}
} else {
for (Field field : this.analysis.getFromDataSources().get(0).getLeft().getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(this.analysis.getFromDataSources().get(0).getRight() + "." + field.name()));
analysis.addSelectItem(qualifiedNameReference, field.name());
}
}
} else if (selectItem instanceof SingleColumn) {
SingleColumn column = (SingleColumn) selectItem;
analysis.addSelectItem(column.getExpression(), column.getAlias().get());
} else {
throw new IllegalArgumentException("Unsupported SelectItem type: " + selectItem.getClass().getName());
}
}
return null;
}
use of io.confluent.ksql.parser.tree.QualifiedNameReference in project ksql by confluentinc.
the class AggregateExpressionRewriter method rewriteFunctionCall.
@Override
public Expression rewriteFunctionCall(FunctionCall node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
String functionName = node.getName().getSuffix();
if (functionRegistry.isAnAggregateFunction(functionName)) {
String aggVarName = AGGREGATE_FUNCTION_VARIABLE_PREFIX + aggVariableIndex;
aggVariableIndex++;
return new QualifiedNameReference(QualifiedName.of(aggVarName));
} else {
List<Expression> arguments = new ArrayList<>();
for (Expression argExpression : node.getArguments()) {
arguments.add(treeRewriter.rewrite(argExpression, context));
}
return new FunctionCall(node.getName(), arguments);
}
}
use of io.confluent.ksql.parser.tree.QualifiedNameReference 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");
}
use of io.confluent.ksql.parser.tree.QualifiedNameReference in project ksql by confluentinc.
the class SchemaKStreamTest method testGroupByMultipleColumns.
@Test
public void testGroupByMultipleColumns() {
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 col0Expression = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("TEST1")), "COL0");
Expression col1Expression = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("TEST1")), "COL1");
KsqlTopicSerDe ksqlTopicSerDe = new KsqlJsonTopicSerDe();
Serde<GenericRow> rowSerde = ksqlTopicSerDe.getGenericRowSerde(initialSchemaKStream.getSchema(), null, false, null);
List<Expression> groupByExpressions = Arrays.asList(col1Expression, col0Expression);
SchemaKGroupedStream groupedSchemaKStream = initialSchemaKStream.groupBy(Serdes.String(), rowSerde, groupByExpressions);
Assert.assertEquals(groupedSchemaKStream.getKeyField().name(), "TEST1.COL1|+|TEST1.COL0");
}
Aggregations