Search in sources :

Example 1 with NaturalJoin

use of io.trino.sql.tree.NaturalJoin in project trino by trinodb.

the class RelationPlanner method planJoinUnnest.

private RelationPlan planJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    Optional<Expression> filterExpression = Optional.empty();
    if (joinNode.getCriteria().isPresent()) {
        JoinCriteria criteria = joinNode.getCriteria().get();
        if (criteria instanceof NaturalJoin) {
            throw semanticException(NOT_SUPPORTED, joinNode, "Natural join involving UNNEST is not supported");
        }
        if (criteria instanceof JoinUsing) {
            throw semanticException(NOT_SUPPORTED, joinNode, "USING for join involving UNNEST is not supported");
        }
        Expression filter = (Expression) getOnlyElement(criteria.getNodes());
        if (filter.equals(TRUE_LITERAL)) {
            filterExpression = Optional.of(filter);
        } else {
            // TODO rewrite filter to support non-trivial join criteria
            throw semanticException(NOT_SUPPORTED, joinNode, "JOIN involving UNNEST on condition other than TRUE is not supported");
        }
    }
    return planUnnest(newPlanBuilder(leftPlan, analysis, lambdaDeclarationToSymbolMap), node, leftPlan.getFieldMappings(), filterExpression, joinNode.getType(), analysis.getScope(joinNode));
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) JoinCriteria(io.trino.sql.tree.JoinCriteria) JoinUsing(io.trino.sql.tree.JoinUsing) NaturalJoin(io.trino.sql.tree.NaturalJoin)

Example 2 with NaturalJoin

use of io.trino.sql.tree.NaturalJoin in project trino by trinodb.

the class RelationPlanner method planCorrelatedJoin.

private RelationPlan planCorrelatedJoin(Join join, RelationPlan leftPlan, Lateral lateral) {
    PlanBuilder leftPlanBuilder = newPlanBuilder(leftPlan, analysis, lambdaDeclarationToSymbolMap);
    RelationPlan rightPlan = new RelationPlanner(analysis, symbolAllocator, idAllocator, lambdaDeclarationToSymbolMap, plannerContext, Optional.of(leftPlanBuilder.getTranslations()), session, recursiveSubqueries).process(lateral.getQuery(), null);
    PlanBuilder rightPlanBuilder = newPlanBuilder(rightPlan, analysis, lambdaDeclarationToSymbolMap);
    Expression filterExpression;
    if (join.getCriteria().isEmpty()) {
        filterExpression = TRUE_LITERAL;
    } else {
        JoinCriteria criteria = join.getCriteria().get();
        if (criteria instanceof JoinUsing || criteria instanceof NaturalJoin) {
            throw semanticException(NOT_SUPPORTED, join, "Correlated join with criteria other than ON is not supported");
        }
        filterExpression = (Expression) getOnlyElement(criteria.getNodes());
    }
    List<Symbol> outputSymbols = ImmutableList.<Symbol>builder().addAll(leftPlan.getFieldMappings()).addAll(rightPlan.getFieldMappings()).build();
    TranslationMap translationMap = new TranslationMap(outerContext, analysis.getScope(join), analysis, lambdaDeclarationToSymbolMap, outputSymbols).withAdditionalMappings(leftPlanBuilder.getTranslations().getMappings()).withAdditionalMappings(rightPlanBuilder.getTranslations().getMappings());
    Expression rewrittenFilterCondition = translationMap.rewrite(filterExpression);
    PlanBuilder planBuilder = subqueryPlanner.appendCorrelatedJoin(leftPlanBuilder, rightPlanBuilder.getRoot(), lateral.getQuery(), CorrelatedJoinNode.Type.typeConvert(join.getType()), rewrittenFilterCondition, ImmutableMap.of());
    return new RelationPlan(planBuilder.getRoot(), analysis.getScope(join), outputSymbols, outerContext);
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) JoinCriteria(io.trino.sql.tree.JoinCriteria) JoinUsing(io.trino.sql.tree.JoinUsing) NaturalJoin(io.trino.sql.tree.NaturalJoin) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder)

Example 3 with NaturalJoin

use of io.trino.sql.tree.NaturalJoin in project trino by trinodb.

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(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) NaturalJoin(io.trino.sql.tree.NaturalJoin) Join(io.trino.sql.tree.Join) AllColumns(io.trino.sql.tree.AllColumns) NaturalJoin(io.trino.sql.tree.NaturalJoin) JoinOn(io.trino.sql.tree.JoinOn) Test(org.junit.jupiter.api.Test)

Example 4 with NaturalJoin

use of io.trino.sql.tree.NaturalJoin in project trino by trinodb.

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 : AliasedRelation(io.trino.sql.tree.AliasedRelation) SampledRelation(io.trino.sql.tree.SampledRelation) Relation(io.trino.sql.tree.Relation) PatternRecognitionRelation(io.trino.sql.tree.PatternRecognitionRelation) Identifier(io.trino.sql.tree.Identifier) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TryExpression(io.trino.sql.tree.TryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) JoinCriteria(io.trino.sql.tree.JoinCriteria) NaturalJoin(io.trino.sql.tree.NaturalJoin) Join(io.trino.sql.tree.Join) JoinUsing(io.trino.sql.tree.JoinUsing) NaturalJoin(io.trino.sql.tree.NaturalJoin) JoinOn(io.trino.sql.tree.JoinOn)

Aggregations

NaturalJoin (io.trino.sql.tree.NaturalJoin)4 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)3 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)3 Expression (io.trino.sql.tree.Expression)3 JoinCriteria (io.trino.sql.tree.JoinCriteria)3 JoinUsing (io.trino.sql.tree.JoinUsing)3 SubqueryExpression (io.trino.sql.tree.SubqueryExpression)3 Join (io.trino.sql.tree.Join)2 JoinOn (io.trino.sql.tree.JoinOn)2 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)1 AliasedRelation (io.trino.sql.tree.AliasedRelation)1 AllColumns (io.trino.sql.tree.AllColumns)1 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)1 BindExpression (io.trino.sql.tree.BindExpression)1 CreateTable (io.trino.sql.tree.CreateTable)1 DereferenceExpression (io.trino.sql.tree.DereferenceExpression)1 DropTable (io.trino.sql.tree.DropTable)1 Identifier (io.trino.sql.tree.Identifier)1 IfExpression (io.trino.sql.tree.IfExpression)1