Search in sources :

Example 1 with ComparisonExpression

use of io.confluent.ksql.parser.tree.ComparisonExpression 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 2 with ComparisonExpression

use of io.confluent.ksql.parser.tree.ComparisonExpression in project ksql by confluentinc.

the class KsqlParserTest method testProjectFilter.

@Test
public void testProjectFilter() throws Exception {
    String queryStr = "SELECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testProjectFilter fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testProjectFilter fails", querySpecification.getWhere().get() instanceof ComparisonExpression);
    ComparisonExpression comparisonExpression = (ComparisonExpression) querySpecification.getWhere().get();
    Assert.assertTrue("testProjectFilter fails", comparisonExpression.toString().equalsIgnoreCase("(TEST1.COL0 > 100)"));
    Assert.assertTrue("testProjectFilter fails", querySpecification.getSelect().getSelectItems().size() == 3);
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Test(org.junit.Test)

Example 3 with ComparisonExpression

use of io.confluent.ksql.parser.tree.ComparisonExpression in project ksql by confluentinc.

the class KsqlParserTest method testSimpleQuery.

@Test
public void testSimpleQuery() throws Exception {
    String simpleQuery = "SELECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
    Statement statement = KSQL_PARSER.buildAst(simpleQuery, metaStore).get(0);
    Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testSimpleQuery fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testSimpleQuery fails", querySpecification.getSelect().getSelectItems().size() == 3);
    assertThat(querySpecification.getFrom(), not(nullValue()));
    Assert.assertTrue("testSimpleQuery fails", querySpecification.getWhere().isPresent());
    Assert.assertTrue("testSimpleQuery fails", querySpecification.getWhere().get() instanceof ComparisonExpression);
    ComparisonExpression comparisonExpression = (ComparisonExpression) querySpecification.getWhere().get();
    Assert.assertTrue("testSimpleQuery fails", comparisonExpression.getType().getValue().equalsIgnoreCase(">"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Test(org.junit.Test)

Example 4 with ComparisonExpression

use of io.confluent.ksql.parser.tree.ComparisonExpression in project ksql by confluentinc.

the class Analyzer method visitJoin.

@Override
protected Node visitJoin(final Join node, final AnalysisContext context) {
    AliasedRelation left = (AliasedRelation) process(node.getLeft(), context);
    AliasedRelation right = (AliasedRelation) process(node.getRight(), context);
    String leftSideName = ((Table) left.getRelation()).getName().getSuffix();
    StructuredDataSource leftDataSource = metaStore.getSource(leftSideName);
    if (leftDataSource == null) {
        throw new KsqlException(format("Resource %s does not exist.", leftSideName));
    }
    leftDataSource = timestampColumn(left, leftDataSource);
    String rightSideName = ((Table) right.getRelation()).getName().getSuffix();
    StructuredDataSource rightDataSource = metaStore.getSource(rightSideName);
    if (rightDataSource == null) {
        throw new KsqlException(format("Resource %s does not exist.", rightSideName));
    }
    rightDataSource = timestampColumn(right, rightDataSource);
    String leftAlias = left.getAlias();
    String rightAlias = right.getAlias();
    JoinNode.Type joinType = getJoinType(node);
    if (!node.getCriteria().isPresent()) {
        throw new KsqlException(String.format("%s Join criteria is not set.", node.getLocation().isPresent() ? node.getLocation().get().toString() : ""));
    }
    JoinOn joinOn = (JoinOn) (node.getCriteria().get());
    ComparisonExpression comparisonExpression = (ComparisonExpression) joinOn.getExpression();
    Pair<String, String> leftSide = fetchKeyFieldName(comparisonExpression, leftAlias, leftDataSource.getSchema());
    Pair<String, String> rightSide = fetchKeyFieldName(comparisonExpression, rightAlias, rightDataSource.getSchema());
    String leftKeyFieldName = leftSide.getRight();
    String rightKeyFieldName = rightSide.getRight();
    if (comparisonExpression.getType() != ComparisonExpression.Type.EQUAL) {
        throw new KsqlException("Only equality join criteria is supported.");
    }
    StructuredDataSourceNode leftSourceKafkaTopicNode = new StructuredDataSourceNode(new PlanNodeId("KafkaTopic_Left"), leftDataSource, leftDataSource.getSchema());
    StructuredDataSourceNode rightSourceKafkaTopicNode = new StructuredDataSourceNode(new PlanNodeId("KafkaTopic_Right"), rightDataSource, rightDataSource.getSchema());
    JoinNode joinNode = new JoinNode(new PlanNodeId("Join"), joinType, leftSourceKafkaTopicNode, rightSourceKafkaTopicNode, leftKeyFieldName, rightKeyFieldName, leftAlias, rightAlias);
    analysis.setJoin(joinNode);
    return null;
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) JoinNode(io.confluent.ksql.planner.plan.JoinNode) StructuredDataSourceNode(io.confluent.ksql.planner.plan.StructuredDataSourceNode) KsqlException(io.confluent.ksql.util.KsqlException) JoinOn(io.confluent.ksql.parser.tree.JoinOn) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Aggregations

ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)4 Query (io.confluent.ksql.parser.tree.Query)3 Statement (io.confluent.ksql.parser.tree.Statement)3 Test (org.junit.Test)3 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)2 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)1 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)1 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)1 Expression (io.confluent.ksql.parser.tree.Expression)1 JoinOn (io.confluent.ksql.parser.tree.JoinOn)1 LongLiteral (io.confluent.ksql.parser.tree.LongLiteral)1 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)1 JoinNode (io.confluent.ksql.planner.plan.JoinNode)1 PlanNodeId (io.confluent.ksql.planner.plan.PlanNodeId)1 StructuredDataSourceNode (io.confluent.ksql.planner.plan.StructuredDataSourceNode)1 KsqlException (io.confluent.ksql.util.KsqlException)1