Search in sources :

Example 1 with JoinCriteria

use of io.crate.sql.tree.JoinCriteria in project crate by crate.

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(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 = identsToStrings(context.joinCriteria().ident());
            criteria = new JoinUsing(columns);
        } else {
            throw new IllegalArgumentException("Unsupported join criteria");
        }
    }
    return new Join(getJoinType(context.joinType()), left, right, Optional.of(criteria));
}
Also used : AliasedRelation(io.crate.sql.tree.AliasedRelation) Relation(io.crate.sql.tree.Relation) SubscriptExpression(io.crate.sql.tree.SubscriptExpression) IfExpression(io.crate.sql.tree.IfExpression) NotExpression(io.crate.sql.tree.NotExpression) SearchedCaseExpression(io.crate.sql.tree.SearchedCaseExpression) LogicalBinaryExpression(io.crate.sql.tree.LogicalBinaryExpression) ArraySliceExpression(io.crate.sql.tree.ArraySliceExpression) ArraySubQueryExpression(io.crate.sql.tree.ArraySubQueryExpression) SimpleCaseExpression(io.crate.sql.tree.SimpleCaseExpression) SubqueryExpression(io.crate.sql.tree.SubqueryExpression) InListExpression(io.crate.sql.tree.InListExpression) ParameterExpression(io.crate.sql.tree.ParameterExpression) ArrayComparisonExpression(io.crate.sql.tree.ArrayComparisonExpression) Expression(io.crate.sql.tree.Expression) ComparisonExpression(io.crate.sql.tree.ComparisonExpression) ArithmeticExpression(io.crate.sql.tree.ArithmeticExpression) NegativeExpression(io.crate.sql.tree.NegativeExpression) JoinCriteria(io.crate.sql.tree.JoinCriteria) Join(io.crate.sql.tree.Join) NaturalJoin(io.crate.sql.tree.NaturalJoin) ValuesList(io.crate.sql.tree.ValuesList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) JoinUsing(io.crate.sql.tree.JoinUsing) NaturalJoin(io.crate.sql.tree.NaturalJoin) JoinOn(io.crate.sql.tree.JoinOn)

Example 2 with JoinCriteria

use of io.crate.sql.tree.JoinCriteria in project crate by crate.

the class RelationAnalyzer method visitJoin.

@Override
protected AnalyzedRelation visitJoin(Join node, StatementAnalysisContext statementContext) {
    AnalyzedRelation leftRel = node.getLeft().accept(this, statementContext);
    AnalyzedRelation rightRel = node.getRight().accept(this, statementContext);
    RelationAnalysisContext relationContext = statementContext.currentRelationContext();
    Optional<JoinCriteria> optCriteria = node.getCriteria();
    Symbol joinCondition = null;
    if (optCriteria.isPresent()) {
        JoinCriteria joinCriteria = optCriteria.get();
        if (joinCriteria instanceof JoinOn || joinCriteria instanceof JoinUsing) {
            final CoordinatorTxnCtx coordinatorTxnCtx = statementContext.transactionContext();
            ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, statementContext.paramTyeHints(), new FullQualifiedNameFieldProvider(relationContext.sources(), relationContext.parentSources(), coordinatorTxnCtx.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(this, statementContext));
            Expression expr;
            if (joinCriteria instanceof JoinOn) {
                expr = ((JoinOn) joinCriteria).getExpression();
            } else {
                expr = JoinUsing.toExpression(leftRel.relationName().toQualifiedName(), rightRel.relationName().toQualifiedName(), ((JoinUsing) joinCriteria).getColumns());
            }
            try {
                joinCondition = expressionAnalyzer.convert(expr, relationContext.expressionAnalysisContext());
            } catch (RelationUnknown e) {
                throw new RelationValidationException(e.getTableIdents(), String.format(Locale.ENGLISH, "missing FROM-clause entry for relation '%s'", e.getTableIdents()));
            }
        } else {
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "join criteria %s not supported", joinCriteria.getClass().getSimpleName()));
        }
    }
    relationContext.addJoinType(JoinType.values()[node.getType().ordinal()], joinCondition);
    return null;
}
Also used : CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) RelationUnknown(io.crate.exceptions.RelationUnknown) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) RelationValidationException(io.crate.exceptions.RelationValidationException) JoinUsing(io.crate.sql.tree.JoinUsing) Expression(io.crate.sql.tree.Expression) JoinCriteria(io.crate.sql.tree.JoinCriteria) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) JoinOn(io.crate.sql.tree.JoinOn)

Aggregations

Expression (io.crate.sql.tree.Expression)2 JoinCriteria (io.crate.sql.tree.JoinCriteria)2 JoinOn (io.crate.sql.tree.JoinOn)2 JoinUsing (io.crate.sql.tree.JoinUsing)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)1 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)1 RelationUnknown (io.crate.exceptions.RelationUnknown)1 RelationValidationException (io.crate.exceptions.RelationValidationException)1 Symbol (io.crate.expression.symbol.Symbol)1 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)1 AliasedRelation (io.crate.sql.tree.AliasedRelation)1 ArithmeticExpression (io.crate.sql.tree.ArithmeticExpression)1 ArrayComparisonExpression (io.crate.sql.tree.ArrayComparisonExpression)1 ArraySliceExpression (io.crate.sql.tree.ArraySliceExpression)1 ArraySubQueryExpression (io.crate.sql.tree.ArraySubQueryExpression)1 ComparisonExpression (io.crate.sql.tree.ComparisonExpression)1 IfExpression (io.crate.sql.tree.IfExpression)1 InListExpression (io.crate.sql.tree.InListExpression)1 Join (io.crate.sql.tree.Join)1 LogicalBinaryExpression (io.crate.sql.tree.LogicalBinaryExpression)1