Search in sources :

Example 1 with QualifiedNameReference

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

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"))));
}
Also used : ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) 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) LongLiteral(io.confluent.ksql.parser.tree.LongLiteral) Statement(io.confluent.ksql.parser.tree.Statement) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 3 with QualifiedNameReference

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;
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Field(org.apache.kafka.connect.data.Field) DropTable(io.confluent.ksql.parser.tree.DropTable) Table(io.confluent.ksql.parser.tree.Table) CreateTable(io.confluent.ksql.parser.tree.CreateTable) SelectItem(io.confluent.ksql.parser.tree.SelectItem) ArrayList(java.util.ArrayList) Join(io.confluent.ksql.parser.tree.Join) NaturalJoin(io.confluent.ksql.parser.tree.NaturalJoin) AllColumns(io.confluent.ksql.parser.tree.AllColumns) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Example 4 with QualifiedNameReference

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);
}
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 5 with QualifiedNameReference

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

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