Search in sources :

Example 1 with NaturalJoin

use of io.prestosql.sql.tree.NaturalJoin 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));
}
Also used : SampledRelation(io.prestosql.sql.tree.SampledRelation) Relation(io.prestosql.sql.tree.Relation) AliasedRelation(io.prestosql.sql.tree.AliasedRelation) Identifier(io.prestosql.sql.tree.Identifier) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) TryExpression(io.prestosql.sql.tree.TryExpression) JoinCriteria(io.prestosql.sql.tree.JoinCriteria) Join(io.prestosql.sql.tree.Join) NaturalJoin(io.prestosql.sql.tree.NaturalJoin) JoinUsing(io.prestosql.sql.tree.JoinUsing) NaturalJoin(io.prestosql.sql.tree.NaturalJoin) JoinOn(io.prestosql.sql.tree.JoinOn)

Example 2 with NaturalJoin

use of io.prestosql.sql.tree.NaturalJoin 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()))));
}
Also used : Table(io.prestosql.sql.tree.Table) CreateTable(io.prestosql.sql.tree.CreateTable) VacuumTable(io.prestosql.sql.tree.VacuumTable) DropTable(io.prestosql.sql.tree.DropTable) RenameTable(io.prestosql.sql.tree.RenameTable) Join(io.prestosql.sql.tree.Join) NaturalJoin(io.prestosql.sql.tree.NaturalJoin) AllColumns(io.prestosql.sql.tree.AllColumns) NaturalJoin(io.prestosql.sql.tree.NaturalJoin) JoinOn(io.prestosql.sql.tree.JoinOn) Test(org.testng.annotations.Test)

Example 3 with NaturalJoin

use of io.prestosql.sql.tree.NaturalJoin in project hetu-core by openlookeng.

the class RelationPlanner method planLateralJoin.

private RelationPlan planLateralJoin(Join join, RelationPlan leftPlan, Lateral lateral) {
    RelationPlan rightPlan = process(lateral.getQuery(), null);
    PlanBuilder leftPlanBuilder = initializePlanBuilder(leftPlan);
    PlanBuilder rightPlanBuilder = initializePlanBuilder(rightPlan);
    Expression filterExpression;
    if (!join.getCriteria().isPresent()) {
        filterExpression = TRUE_LITERAL;
    } else {
        JoinCriteria criteria = join.getCriteria().get();
        if (criteria instanceof JoinUsing || criteria instanceof NaturalJoin) {
            throw notSupportedException(join, "Lateral join with criteria other than ON");
        }
        filterExpression = (Expression) getOnlyElement(criteria.getNodes());
    }
    List<Symbol> rewriterOutputSymbols = ImmutableList.<Symbol>builder().addAll(leftPlan.getFieldMappings()).addAll(rightPlan.getFieldMappings()).build();
    // this node is not used in the plan. It is only used for creating the TranslationMap.
    PlanNode dummy = new ValuesNode(idAllocator.getNextId(), ImmutableList.<Symbol>builder().addAll(leftPlanBuilder.getRoot().getOutputSymbols()).addAll(rightPlanBuilder.getRoot().getOutputSymbols()).build(), ImmutableList.of());
    RelationPlan intermediateRelationPlan = new RelationPlan(dummy, analysis.getScope(join), rewriterOutputSymbols);
    TranslationMap translationMap = new TranslationMap(intermediateRelationPlan, analysis, lambdaDeclarationToSymbolMap);
    translationMap.setFieldMappings(rewriterOutputSymbols);
    translationMap.putExpressionMappingsFrom(leftPlanBuilder.getTranslations());
    translationMap.putExpressionMappingsFrom(rightPlanBuilder.getTranslations());
    Expression rewrittenFilterCondition = translationMap.rewrite(filterExpression);
    PlanBuilder planBuilder = subqueryPlanner.appendLateralJoin(leftPlanBuilder, rightPlanBuilder, lateral.getQuery(), true, LateralJoinNode.Type.typeConvert(join.getType()), rewrittenFilterCondition);
    List<Symbol> outputSymbols = ImmutableList.<Symbol>builder().addAll(leftPlan.getRoot().getOutputSymbols()).addAll(rightPlan.getRoot().getOutputSymbols()).build();
    return new RelationPlan(planBuilder.getRoot(), analysis.getScope(join), outputSymbols);
}
Also used : ValuesNode(io.prestosql.spi.plan.ValuesNode) PlanNode(io.prestosql.spi.plan.PlanNode) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Symbol(io.prestosql.spi.plan.Symbol) JoinCriteria(io.prestosql.sql.tree.JoinCriteria) JoinUsing(io.prestosql.sql.tree.JoinUsing) NaturalJoin(io.prestosql.sql.tree.NaturalJoin)

Aggregations

NaturalJoin (io.prestosql.sql.tree.NaturalJoin)3 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)2 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)2 Expression (io.prestosql.sql.tree.Expression)2 Join (io.prestosql.sql.tree.Join)2 JoinCriteria (io.prestosql.sql.tree.JoinCriteria)2 JoinOn (io.prestosql.sql.tree.JoinOn)2 JoinUsing (io.prestosql.sql.tree.JoinUsing)2 PlanNode (io.prestosql.spi.plan.PlanNode)1 Symbol (io.prestosql.spi.plan.Symbol)1 ValuesNode (io.prestosql.spi.plan.ValuesNode)1 RowExpression (io.prestosql.spi.relation.RowExpression)1 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)1 AliasedRelation (io.prestosql.sql.tree.AliasedRelation)1 AllColumns (io.prestosql.sql.tree.AllColumns)1 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)1 BindExpression (io.prestosql.sql.tree.BindExpression)1 CreateTable (io.prestosql.sql.tree.CreateTable)1 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)1