Search in sources :

Example 1 with DereferenceExpression

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));
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Query(io.confluent.ksql.parser.tree.Query) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) Statement(io.confluent.ksql.parser.tree.Statement) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 2 with DereferenceExpression

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);
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) InListExpression(io.confluent.ksql.parser.tree.InListExpression) NullIfExpression(io.confluent.ksql.parser.tree.NullIfExpression) SimpleCaseExpression(io.confluent.ksql.parser.tree.SimpleCaseExpression) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) LogicalBinaryExpression(io.confluent.ksql.parser.tree.LogicalBinaryExpression) TumblingWindowExpression(io.confluent.ksql.parser.tree.TumblingWindowExpression) ArithmeticBinaryExpression(io.confluent.ksql.parser.tree.ArithmeticBinaryExpression) NotExpression(io.confluent.ksql.parser.tree.NotExpression) HoppingWindowExpression(io.confluent.ksql.parser.tree.HoppingWindowExpression) SubscriptExpression(io.confluent.ksql.parser.tree.SubscriptExpression) SessionWindowExpression(io.confluent.ksql.parser.tree.SessionWindowExpression) SearchedCaseExpression(io.confluent.ksql.parser.tree.SearchedCaseExpression) LambdaExpression(io.confluent.ksql.parser.tree.LambdaExpression) SubqueryExpression(io.confluent.ksql.parser.tree.SubqueryExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) ArithmeticUnaryExpression(io.confluent.ksql.parser.tree.ArithmeticUnaryExpression) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference)

Example 3 with DereferenceExpression

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

Example 4 with DereferenceExpression

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

Example 5 with DereferenceExpression

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

DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)8 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)8 Expression (io.confluent.ksql.parser.tree.Expression)6 Test (org.junit.Test)4 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)3 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)2 GenericRow (io.confluent.ksql.GenericRow)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 NotExpression (io.confluent.ksql.parser.tree.NotExpression)2 NullIfExpression (io.confluent.ksql.parser.tree.NullIfExpression)2 SearchedCaseExpression (io.confluent.ksql.parser.tree.SearchedCaseExpression)2 SessionWindowExpression (io.confluent.ksql.parser.tree.SessionWindowExpression)2 SimpleCaseExpression (io.confluent.ksql.parser.tree.SimpleCaseExpression)2 Statement (io.confluent.ksql.parser.tree.Statement)2 SubqueryExpression (io.confluent.ksql.parser.tree.SubqueryExpression)2