use of com.facebook.presto.sql.tree.Join in project presto by prestodb.
the class AstBuilder method visitQuerySpecification.
@Override
public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) {
Optional<Relation> from = Optional.empty();
List<SelectItem> selectItems = visit(context.selectItem(), SelectItem.class);
List<Relation> relations = visit(context.relation(), Relation.class);
if (!relations.isEmpty()) {
// synthesize implicit join nodes
Iterator<Relation> iterator = relations.iterator();
Relation relation = iterator.next();
while (iterator.hasNext()) {
relation = new Join(getLocation(context), Join.Type.IMPLICIT, relation, iterator.next(), Optional.empty());
}
from = Optional.of(relation);
}
return new QuerySpecification(getLocation(context), new Select(getLocation(context.SELECT()), isDistinct(context.setQuantifier()), selectItems), from, visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), Optional.empty(), Optional.empty());
}
use of com.facebook.presto.sql.tree.Join in project presto by prestodb.
the class TestSqlParser method testUnnest.
@Test
public void testUnnest() throws Exception {
assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a)", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.CROSS, new Table(QualifiedName.of("t")), new Unnest(ImmutableList.of(new Identifier("a")), false), Optional.empty())));
assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a) WITH ORDINALITY", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.CROSS, new Table(QualifiedName.of("t")), new Unnest(ImmutableList.of(new Identifier("a")), true), Optional.empty())));
}
use of com.facebook.presto.sql.tree.Join in project presto by prestodb.
the class TestSqlParser method testJoinPrecedence.
@Test
public void testJoinPrecedence() {
assertStatement("SELECT * FROM a CROSS JOIN b LEFT JOIN c ON true", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.LEFT, new Join(Join.Type.CROSS, new Table(QualifiedName.of("a")), new Table(QualifiedName.of("b")), Optional.empty()), new Table(QualifiedName.of("c")), Optional.of(new JoinOn(BooleanLiteral.TRUE_LITERAL)))));
assertStatement("SELECT * FROM a CROSS JOIN b NATURAL JOIN c CROSS JOIN d NATURAL JOIN e", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.INNER, new Join(Join.Type.CROSS, new Join(Join.Type.INNER, new Join(Join.Type.CROSS, new Table(QualifiedName.of("a")), new Table(QualifiedName.of("b")), Optional.empty()), new Table(QualifiedName.of("c")), Optional.of(new NaturalJoin())), new Table(QualifiedName.of("d")), Optional.empty()), new Table(QualifiedName.of("e")), Optional.of(new NaturalJoin()))));
}
use of com.facebook.presto.sql.tree.Join in project presto by prestodb.
the class AstBuilder method visitJoinRelation.
// *************** from clause *****************
@Override
public Node visitJoinRelation(SqlBaseParser.JoinRelationContext context) {
Relation left = (Relation) visit(context.left);
Relation right;
if (context.CROSS() != null) {
right = (Relation) visit(context.right);
return new Join(getLocation(context), Join.Type.CROSS, left, right, Optional.empty());
}
JoinCriteria criteria;
if (context.NATURAL() != null) {
right = (Relation) visit(context.right);
criteria = new NaturalJoin();
} else {
right = (Relation) visit(context.rightRelation);
if (context.joinCriteria().ON() != null) {
criteria = new JoinOn((Expression) visit(context.joinCriteria().booleanExpression()));
} else if (context.joinCriteria().USING() != null) {
List<String> columns = context.joinCriteria().identifier().stream().map(ParseTree::getText).collect(toList());
criteria = new JoinUsing(columns);
} else {
throw new IllegalArgumentException("Unsupported join criteria");
}
}
Join.Type joinType;
if (context.joinType().LEFT() != null) {
joinType = Join.Type.LEFT;
} else if (context.joinType().RIGHT() != null) {
joinType = Join.Type.RIGHT;
} else if (context.joinType().FULL() != null) {
joinType = Join.Type.FULL;
} else {
joinType = Join.Type.INNER;
}
return new Join(getLocation(context), joinType, left, right, Optional.of(criteria));
}
Aggregations