Search in sources :

Example 6 with JoinGraph

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);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) JoinNode(io.trino.sql.planner.plan.JoinNode) FilterNode(io.trino.sql.planner.plan.FilterNode) JoinGraph(io.trino.sql.planner.optimizations.joins.JoinGraph) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) PlanNode(io.trino.sql.planner.plan.PlanNode) Expression(io.trino.sql.tree.Expression) HashSet(java.util.HashSet)

Example 7 with JoinGraph

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));
}
Also used : PlanNode(io.trino.sql.planner.plan.PlanNode) PlanNodeIdAllocator(io.trino.sql.planner.PlanNodeIdAllocator) Session(io.trino.Session) JoinGraph(io.trino.sql.planner.optimizations.joins.JoinGraph) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 8 with JoinGraph

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));
}
Also used : PlanNode(io.trino.sql.planner.plan.PlanNode) PlanNodeIdAllocator(io.trino.sql.planner.PlanNodeIdAllocator) Session(io.trino.Session) JoinGraph(io.trino.sql.planner.optimizations.joins.JoinGraph) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Aggregations

JoinGraph (io.trino.sql.planner.optimizations.joins.JoinGraph)8 PlanNode (io.trino.sql.planner.plan.PlanNode)8 Session (io.trino.Session)6 PlanNodeIdAllocator (io.trino.sql.planner.PlanNodeIdAllocator)6 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)5 Test (org.testng.annotations.Test)5 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 FilterNode (io.trino.sql.planner.plan.FilterNode)2 JoinNode (io.trino.sql.planner.plan.JoinNode)2 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)2 Expression (io.trino.sql.tree.Expression)2 HashSet (java.util.HashSet)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SystemSessionProperties.getJoinReorderingStrategy (io.trino.SystemSessionProperties.getJoinReorderingStrategy)1 Captures (io.trino.matching.Captures)1 Pattern (io.trino.matching.Pattern)1