Search in sources :

Example 1 with Join

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());
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) Relation(com.facebook.presto.sql.tree.Relation) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) GroupBy(com.facebook.presto.sql.tree.GroupBy) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) TryExpression(com.facebook.presto.sql.tree.TryExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) SelectItem(com.facebook.presto.sql.tree.SelectItem) Select(com.facebook.presto.sql.tree.Select) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) NaturalJoin(com.facebook.presto.sql.tree.NaturalJoin) Join(com.facebook.presto.sql.tree.Join)

Example 2 with Join

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())));
}
Also used : CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) Identifier(com.facebook.presto.sql.tree.Identifier) NaturalJoin(com.facebook.presto.sql.tree.NaturalJoin) Join(com.facebook.presto.sql.tree.Join) AllColumns(com.facebook.presto.sql.tree.AllColumns) Unnest(com.facebook.presto.sql.tree.Unnest) Test(org.testng.annotations.Test)

Example 3 with Join

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()))));
}
Also used : CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) NaturalJoin(com.facebook.presto.sql.tree.NaturalJoin) Join(com.facebook.presto.sql.tree.Join) AllColumns(com.facebook.presto.sql.tree.AllColumns) NaturalJoin(com.facebook.presto.sql.tree.NaturalJoin) JoinOn(com.facebook.presto.sql.tree.JoinOn) Test(org.testng.annotations.Test)

Example 4 with Join

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));
}
Also used : AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) Relation(com.facebook.presto.sql.tree.Relation) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) TryExpression(com.facebook.presto.sql.tree.TryExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) JoinCriteria(com.facebook.presto.sql.tree.JoinCriteria) NaturalJoin(com.facebook.presto.sql.tree.NaturalJoin) Join(com.facebook.presto.sql.tree.Join) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) JoinUsing(com.facebook.presto.sql.tree.JoinUsing) NaturalJoin(com.facebook.presto.sql.tree.NaturalJoin) JoinOn(com.facebook.presto.sql.tree.JoinOn) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

Join (com.facebook.presto.sql.tree.Join)4 NaturalJoin (com.facebook.presto.sql.tree.NaturalJoin)4 AliasedRelation (com.facebook.presto.sql.tree.AliasedRelation)2 AllColumns (com.facebook.presto.sql.tree.AllColumns)2 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)2 ArithmeticUnaryExpression (com.facebook.presto.sql.tree.ArithmeticUnaryExpression)2 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)2 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)2 CreateTable (com.facebook.presto.sql.tree.CreateTable)2 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)2 DropTable (com.facebook.presto.sql.tree.DropTable)2 Expression (com.facebook.presto.sql.tree.Expression)2 IfExpression (com.facebook.presto.sql.tree.IfExpression)2 InListExpression (com.facebook.presto.sql.tree.InListExpression)2 JoinOn (com.facebook.presto.sql.tree.JoinOn)2 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)2 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)2 NotExpression (com.facebook.presto.sql.tree.NotExpression)2 NullIfExpression (com.facebook.presto.sql.tree.NullIfExpression)2 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)2