Search in sources :

Example 6 with AliasedRelation

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

the class DataSourceExtractor method visitAliasedRelation.

@Override
public Node visitAliasedRelation(final SqlBaseParser.AliasedRelationContext context) {
    Table table = (Table) visit(context.relationPrimary());
    String alias = null;
    if (context.children.size() == 1) {
        alias = table.getName().getSuffix().toUpperCase();
    } else if (context.children.size() == 2) {
        alias = context.children.get(1).getText().toUpperCase();
    }
    if (!isJoin) {
        this.fromAlias = alias;
        StructuredDataSource fromDataSource = metaStore.getSource(table.getName().getSuffix());
        if (fromDataSource == null) {
            throw new KsqlException(table.getName().getSuffix() + " does not exist.");
        }
        return null;
    }
    // TODO: Figure out if the call to toUpperCase() here is really necessary
    return new AliasedRelation(getLocation(context), table, alias.toUpperCase(), getColumnAliases(context.columnAliases()));
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Table(io.confluent.ksql.parser.tree.Table) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Example 7 with AliasedRelation

use of io.confluent.ksql.parser.tree.AliasedRelation 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)

Example 8 with AliasedRelation

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

the class KsqlParserTest method testSelectHoppingWindow.

@Test
public void testSelectHoppingWindow() throws Exception {
    String queryStr = "select itemid, sum(orderunits) from orders window HOPPING ( size 30 second, advance by 5" + " seconds) " + "where " + "orderunits" + " > 5 group by itemid;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    assertThat(statement, instanceOf(Query.class));
    Query query = (Query) statement;
    assertThat(query.getQueryBody(), instanceOf(QuerySpecification.class));
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    assertThat(querySpecification.getSelect().getSelectItems().size(), equalTo(2));
    assertThat(querySpecification.getWhere().get().toString(), equalTo("(ORDERS.ORDERUNITS > 5)"));
    assertThat(((AliasedRelation) querySpecification.getFrom()).getAlias().toUpperCase(), equalTo("ORDERS"));
    Assert.assertTrue("window expression isn't present", querySpecification.getWindowExpression().isPresent());
    assertThat(querySpecification.getWindowExpression().get().toString().toUpperCase(), equalTo(" WINDOW STREAMWINDOW  HOPPING ( SIZE 30 SECONDS , ADVANCE BY 5 SECONDS ) "));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) Test(org.junit.Test)

Example 9 with AliasedRelation

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

the class KsqlParserTest method testSelectAllJoin.

@Test
public void testSelectAllJoin() throws Exception {
    String queryStr = "SELECT * FROM test1 t1 LEFT JOIN test2 t2 ON t1.col1 = t2.col1 WHERE t2.col2 = 'test';";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testLeftJoinWithFilter fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testSelectAllJoin fails", querySpecification.getFrom() instanceof Join);
    Join join = (Join) querySpecification.getFrom();
    Assert.assertTrue("testSelectAllJoin fails", querySpecification.getSelect().getSelectItems().size() == 11);
    Assert.assertTrue("testLeftJoinWithFilter fails", ((AliasedRelation) join.getLeft()).getAlias().equalsIgnoreCase("T1"));
    Assert.assertTrue("testLeftJoinWithFilter fails", ((AliasedRelation) join.getRight()).getAlias().equalsIgnoreCase("T2"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Join(io.confluent.ksql.parser.tree.Join) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) Test(org.junit.Test)

Example 10 with AliasedRelation

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

the class KsqlParserTest method testLeftJoinWithFilter.

@Test
public void testLeftJoinWithFilter() throws Exception {
    String queryStr = "SELECT t1.col1, t2.col1, t2.col4, t2.col2 FROM test1 t1 LEFT JOIN test2 t2 ON t1.col1 = " + "t2.col1 WHERE t2.col2 = 'test';";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testSimpleQuery fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testLeftJoinWithFilter fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testLeftJoinWithFilter fails", querySpecification.getFrom() instanceof Join);
    Join join = (Join) querySpecification.getFrom();
    Assert.assertTrue("testLeftJoinWithFilter fails", join.getType().toString().equalsIgnoreCase("LEFT"));
    Assert.assertTrue("testLeftJoinWithFilter fails", ((AliasedRelation) join.getLeft()).getAlias().equalsIgnoreCase("T1"));
    Assert.assertTrue("testLeftJoinWithFilter fails", ((AliasedRelation) join.getRight()).getAlias().equalsIgnoreCase("T2"));
    Assert.assertTrue("testLeftJoinWithFilter fails", querySpecification.getWhere().get().toString().equalsIgnoreCase("(T2.COL2 = 'test')"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) Join(io.confluent.ksql.parser.tree.Join) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) Test(org.junit.Test)

Aggregations

AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)12 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)7 Statement (io.confluent.ksql.parser.tree.Statement)7 Test (org.junit.Test)7 Query (io.confluent.ksql.parser.tree.Query)6 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)4 Join (io.confluent.ksql.parser.tree.Join)4 Table (io.confluent.ksql.parser.tree.Table)4 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 DropTable (io.confluent.ksql.parser.tree.DropTable)2 AllColumns (io.confluent.ksql.parser.tree.AllColumns)1 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)1 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)1 JoinOn (io.confluent.ksql.parser.tree.JoinOn)1 NaturalJoin (io.confluent.ksql.parser.tree.NaturalJoin)1 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)1 Relation (io.confluent.ksql.parser.tree.Relation)1 SelectItem (io.confluent.ksql.parser.tree.SelectItem)1 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)1 JoinNode (io.confluent.ksql.planner.plan.JoinNode)1