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));
}
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;
}
Aggregations