use of io.prestosql.sql.tree.Join in project hetu-core by openlookeng.
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(), Optional.empty());
}
use of io.prestosql.sql.tree.Join in project hetu-core by openlookeng.
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) {
criteria = new JoinUsing(visit(context.joinCriteria().identifier(), Identifier.class));
} 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));
}
use of io.prestosql.sql.tree.Join in project hetu-core by openlookeng.
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 io.prestosql.sql.tree.Join in project hetu-core by openlookeng.
the class TestSqlParser method testUnnest.
@Test
public void testUnnest() {
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, b) WITH ORDINALITY", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.CROSS, new Table(QualifiedName.of("t")), new Unnest(ImmutableList.of(new Identifier("a"), new Identifier("b")), true), Optional.empty())));
assertStatement("SELECT * FROM t FULL JOIN UNNEST(a) AS tmp (c) ON true", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.FULL, new Table(QualifiedName.of("t")), new AliasedRelation(new Unnest(ImmutableList.of(new Identifier("a")), false), new Identifier("tmp"), ImmutableList.of(new Identifier("c"))), Optional.of(new JoinOn(BooleanLiteral.TRUE_LITERAL)))));
}
use of io.prestosql.sql.tree.Join in project hetu-core by openlookeng.
the class HiveAstBuilder method visitJoinRelation.
// *************** from clause *****************
@Override
public Node visitJoinRelation(HiveSqlParser.JoinRelationContext context) {
Relation leftRelation = (Relation) visit(context.left);
Relation rightRelation;
Optional<JoinCriteria> criteria = Optional.empty();
// Semi Join
if (context.semiJoin() != null) {
addDiff(DiffType.UNSUPPORTED, context.semiJoin().LEFT().getText(), "[LEFT SEMI JOIN] is not supported");
addDiff(DiffType.UNSUPPORTED, context.semiJoin().SEMI().getText(), null);
addDiff(DiffType.UNSUPPORTED, context.semiJoin().JOIN().getText(), null);
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: Left semi join", context.semiJoin());
}
// Cross join
if (context.crossJoin() != null) {
if (context.crossJoin().joinCriteria() != null) {
addDiff(DiffType.UNSUPPORTED, context.crossJoin().CROSS().getText(), "[CROSS JOIN] must not contain join condition");
addDiff(DiffType.UNSUPPORTED, context.crossJoin().JOIN().getText(), null);
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: Cross join must not contain join condition", context.crossJoin().joinCriteria());
}
rightRelation = (Relation) visit(context.crossJoin().right);
return new Join(getLocation(context), Join.Type.CROSS, leftRelation, rightRelation, criteria);
}
// Inner join
if (context.innerJoin() != null) {
if (context.innerJoin().joinCriteria() == null) {
if (context.innerJoin().INNER() != null) {
addDiff(DiffType.UNSUPPORTED, context.innerJoin().INNER().getText(), null);
}
addDiff(DiffType.UNSUPPORTED, context.innerJoin().JOIN().getText(), "[INNER JOIN] must contain join condition");
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: Inner join must contain join condition", context.innerJoin());
}
rightRelation = (Relation) visit(context.innerJoin().right);
criteria = Optional.of(new JoinOn((Expression) visit(context.innerJoin().joinCriteria().booleanExpression())));
return new Join(getLocation(context), Join.Type.INNER, leftRelation, rightRelation, criteria);
}
// Outer join
HiveSqlParser.OuterJoinContext outerJoinContext = context.outerJoin();
// Get join type
Join.Type joinType;
if (outerJoinContext.joinType().LEFT() != null) {
joinType = Join.Type.LEFT;
} else if (outerJoinContext.joinType().RIGHT() != null) {
joinType = Join.Type.RIGHT;
} else {
joinType = Join.Type.FULL;
}
// Get right relation
rightRelation = (Relation) visit(outerJoinContext.rightRelation);
// Get join criteria
criteria = Optional.of(new JoinOn((Expression) visit(outerJoinContext.joinCriteria().booleanExpression())));
return new Join(getLocation(context), joinType, leftRelation, rightRelation, criteria);
}
Aggregations