private Relation render(List<ForeignKey> keys) { if (keys.isEmpty()) { return QueryUtil.table(new QualifiedName(baseTable)); } ForeignKey key = keys.get(0); if (keys.size() == 1) { return new Join(Join.Type.INNER, QueryUtil.table(new QualifiedName(key.getSourceTable())), QueryUtil.table(new QualifiedName(key.getDestinationTable())), Optional.of(new JoinOn(new ComparisonExpression( ComparisonExpression.Type.EQUAL, new QualifiedNameReference(QualifiedName.of( key.getSourceTable(), key.getSourceColumn())), new QualifiedNameReference(QualifiedName.of( key.getDestinationTable(), key.getDestinationColumn())))))); } return new Join(Join.Type.INNER, render(keys.subList(1, keys.size())), QueryUtil.table(new QualifiedName(key.getDestinationTable())), Optional.of(new JoinOn(new ComparisonExpression( ComparisonExpression.Type.EQUAL, new QualifiedNameReference(QualifiedName.of( key.getSourceTable(), key.getSourceColumn())), new QualifiedNameReference(QualifiedName.of( key.getDestinationTable(), key.getDestinationColumn())))))); }
@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())))); }
@Override protected CatalogSchemaContext visitJoin(Join node, CatalogSchemaContext context) { process(node.getLeft(), context); process(node.getRight(), context); if (node.getCriteria().isPresent()) { if (node.getCriteria().get() instanceof JoinOn) { process(((JoinOn) node.getCriteria().get()).getExpression(), context); } } return context; }