Search in sources :

Example 6 with QualifiedNameReference

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;
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Pair(io.confluent.ksql.util.Pair)

Example 7 with QualifiedNameReference

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;
}
Also used : Field(org.apache.kafka.connect.data.Field) SelectItem(io.confluent.ksql.parser.tree.SelectItem) JoinNode(io.confluent.ksql.planner.plan.JoinNode) AllColumns(io.confluent.ksql.parser.tree.AllColumns) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) KsqlException(io.confluent.ksql.util.KsqlException) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference)

Example 8 with QualifiedNameReference

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);
    }
}
Also used : Expression(io.confluent.ksql.parser.tree.Expression) ArrayList(java.util.ArrayList) FunctionCall(io.confluent.ksql.parser.tree.FunctionCall) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference)

Example 9 with QualifiedNameReference

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");
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) ArrayList(java.util.ArrayList) GenericRow(io.confluent.ksql.GenericRow) PlanNode(io.confluent.ksql.planner.plan.PlanNode) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) KsqlJsonTopicSerDe(io.confluent.ksql.serde.json.KsqlJsonTopicSerDe) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 10 with QualifiedNameReference

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");
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) ArrayList(java.util.ArrayList) GenericRow(io.confluent.ksql.GenericRow) PlanNode(io.confluent.ksql.planner.plan.PlanNode) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) KsqlJsonTopicSerDe(io.confluent.ksql.serde.json.KsqlJsonTopicSerDe) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Aggregations

QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)13 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)9 Expression (io.confluent.ksql.parser.tree.Expression)8 Test (org.junit.Test)6 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)4 ArrayList (java.util.ArrayList)4 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)3 Statement (io.confluent.ksql.parser.tree.Statement)3 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)2 GenericRow (io.confluent.ksql.GenericRow)2 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)2 AllColumns (io.confluent.ksql.parser.tree.AllColumns)2 ArithmeticBinaryExpression (io.confluent.ksql.parser.tree.ArithmeticBinaryExpression)2 ArithmeticUnaryExpression (io.confluent.ksql.parser.tree.ArithmeticUnaryExpression)2 HoppingWindowExpression (io.confluent.ksql.parser.tree.HoppingWindowExpression)2 InListExpression (io.confluent.ksql.parser.tree.InListExpression)2 LambdaExpression (io.confluent.ksql.parser.tree.LambdaExpression)2 LogicalBinaryExpression (io.confluent.ksql.parser.tree.LogicalBinaryExpression)2 LongLiteral (io.confluent.ksql.parser.tree.LongLiteral)2 NotExpression (io.confluent.ksql.parser.tree.NotExpression)2