use of io.trino.sql.planner.optimizations.joins.JoinGraph in project trino by trinodb.
the class EliminateCrossJoins method buildJoinTree.
public static PlanNode buildJoinTree(List<Symbol> expectedOutputSymbols, JoinGraph graph, List<Integer> joinOrder, PlanNodeIdAllocator idAllocator) {
requireNonNull(expectedOutputSymbols, "expectedOutputSymbols is null");
requireNonNull(idAllocator, "idAllocator is null");
requireNonNull(graph, "graph is null");
joinOrder = ImmutableList.copyOf(requireNonNull(joinOrder, "joinOrder is null"));
checkArgument(joinOrder.size() >= 2);
PlanNode result = graph.getNode(joinOrder.get(0));
Set<PlanNodeId> alreadyJoinedNodes = new HashSet<>();
alreadyJoinedNodes.add(result.getId());
for (int i = 1; i < joinOrder.size(); i++) {
PlanNode rightNode = graph.getNode(joinOrder.get(i));
alreadyJoinedNodes.add(rightNode.getId());
ImmutableList.Builder<JoinNode.EquiJoinClause> criteria = ImmutableList.builder();
for (JoinGraph.Edge edge : graph.getEdges(rightNode)) {
PlanNode targetNode = edge.getTargetNode();
if (alreadyJoinedNodes.contains(targetNode.getId())) {
criteria.add(new JoinNode.EquiJoinClause(edge.getTargetSymbol(), edge.getSourceSymbol()));
}
}
result = new JoinNode(idAllocator.getNextId(), JoinNode.Type.INNER, result, rightNode, criteria.build(), result.getOutputSymbols(), rightNode.getOutputSymbols(), false, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of(), Optional.empty());
}
List<Expression> filters = graph.getFilters();
for (Expression filter : filters) {
result = new FilterNode(idAllocator.getNextId(), result, filter);
}
// Some nodes are sensitive to what's produced (e.g., DistinctLimit node)
return restrictOutputs(idAllocator, result, ImmutableSet.copyOf(expectedOutputSymbols)).orElse(result);
}
use of io.trino.sql.planner.optimizations.joins.JoinGraph in project trino by trinodb.
the class TestEliminateCrossJoins method testJoinOrderWithMultipleEdgesBetweenNodes.
@Test
public void testJoinOrderWithMultipleEdgesBetweenNodes() {
Session session = testSessionBuilder().build();
PlanNode plan = joinNode(joinNode(values("a"), values("b1", "b2")), values("c1", "c2"), "a", "c1", "b1", "c1", "b2", "c2");
JoinGraph joinGraph = JoinGraph.buildFrom(tester().getPlannerContext(), plan, noLookup(), new PlanNodeIdAllocator(), session, createTestingTypeAnalyzer(tester().getPlannerContext()), TypeProvider.empty());
assertEquals(getJoinOrder(joinGraph), ImmutableList.of(0, 2, 1));
}
use of io.trino.sql.planner.optimizations.joins.JoinGraph in project trino by trinodb.
the class TestEliminateCrossJoins method testDoesNotChangeOrderWithoutCrossJoin.
@Test
public void testDoesNotChangeOrderWithoutCrossJoin() {
Session session = testSessionBuilder().build();
PlanNode plan = joinNode(joinNode(values("a"), values("b"), "a", "b"), values("c"), "b", "c");
JoinGraph joinGraph = JoinGraph.buildFrom(tester().getPlannerContext(), plan, noLookup(), new PlanNodeIdAllocator(), session, createTestingTypeAnalyzer(tester().getPlannerContext()), TypeProvider.empty());
assertEquals(getJoinOrder(joinGraph), ImmutableList.of(0, 1, 2));
}
Aggregations