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