Search in sources :

Example 1 with JoinGraph

use of com.facebook.presto.sql.planner.optimizations.joins.JoinGraph in project presto by prestodb.

the class TestEliminateCrossJoins method testDoNotReorderCrossJoins.

@Test
public void testDoNotReorderCrossJoins() {
    PlanNode plan = join(join(values(symbol("a")), values(symbol("b"))), values(symbol("c")), symbol("c"), symbol("b"));
    JoinGraph joinGraph = getOnlyElement(JoinGraph.buildFrom(plan));
    assertEquals(EliminateCrossJoins.getJoinOrder(joinGraph), ImmutableList.of(0, 1, 2));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) JoinGraph(com.facebook.presto.sql.planner.optimizations.joins.JoinGraph) Test(org.testng.annotations.Test)

Example 2 with JoinGraph

use of com.facebook.presto.sql.planner.optimizations.joins.JoinGraph in project presto by prestodb.

the class TestEliminateCrossJoins method testDonNotChangeOrderWithoutCrossJoin.

@Test
public void testDonNotChangeOrderWithoutCrossJoin() {
    PlanNode plan = join(join(values(symbol("a")), values(symbol("b")), symbol("a"), symbol("b")), values(symbol("c")), symbol("c"), symbol("b"));
    JoinGraph joinGraph = getOnlyElement(JoinGraph.buildFrom(plan));
    assertEquals(EliminateCrossJoins.getJoinOrder(joinGraph), ImmutableList.of(0, 1, 2));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) JoinGraph(com.facebook.presto.sql.planner.optimizations.joins.JoinGraph) Test(org.testng.annotations.Test)

Example 3 with JoinGraph

use of com.facebook.presto.sql.planner.optimizations.joins.JoinGraph in project presto by prestodb.

the class EliminateCrossJoins method getJoinOrder.

/**
     * Given JoinGraph determine the order of joins between graph nodes
     * by traversing JoinGraph. Any graph traversal algorithm could be used
     * here (like BFS or DFS), but we use PriorityQueue to preserve
     * original JoinOrder as mush as it is possible. PriorityQueue returns
     * next nodes to join in order of their occurrence in original Plan.
     */
public static List<Integer> getJoinOrder(JoinGraph graph) {
    ImmutableList.Builder<PlanNode> joinOrder = ImmutableList.builder();
    Map<PlanNodeId, Integer> priorities = new HashMap<>();
    for (int i = 0; i < graph.size(); i++) {
        priorities.put(graph.getNode(i).getId(), i);
    }
    PriorityQueue<PlanNode> nodesToVisit = new PriorityQueue<>(graph.size(), (Comparator<PlanNode>) (node1, node2) -> priorities.get(node1.getId()).compareTo(priorities.get(node2.getId())));
    Set<PlanNode> visited = new HashSet<>();
    nodesToVisit.add(graph.getNode(0));
    while (!nodesToVisit.isEmpty()) {
        PlanNode node = nodesToVisit.poll();
        if (!visited.contains(node)) {
            visited.add(node);
            joinOrder.add(node);
            for (JoinGraph.Edge edge : graph.getEdges(node)) {
                nodesToVisit.add(edge.getTargetNode());
            }
        }
        if (nodesToVisit.isEmpty() && visited.size() < graph.size()) {
            // disconnected graph, find new starting point
            Optional<PlanNode> firstNotVisitedNode = graph.getNodes().stream().filter(graphNode -> !visited.contains(graphNode)).findFirst();
            if (firstNotVisitedNode.isPresent()) {
                nodesToVisit.add(firstNotVisitedNode.get());
            }
        }
    }
    checkState(visited.size() == graph.size());
    return joinOrder.build().stream().map(node -> priorities.get(node.getId())).collect(toImmutableList());
}
Also used : PriorityQueue(java.util.PriorityQueue) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) SimplePlanRewriter(com.facebook.presto.sql.planner.plan.SimplePlanRewriter) Type(com.facebook.presto.spi.type.Type) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Assignments(com.facebook.presto.sql.planner.plan.Assignments) PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Session(com.facebook.presto.Session) Set(java.util.Set) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) List(java.util.List) Expression(com.facebook.presto.sql.tree.Expression) JoinGraph(com.facebook.presto.sql.planner.optimizations.joins.JoinGraph) Optional(java.util.Optional) FilterNode(com.facebook.presto.sql.planner.plan.FilterNode) Comparator(java.util.Comparator) SimplePlanRewriter.rewriteWith(com.facebook.presto.sql.planner.plan.SimplePlanRewriter.rewriteWith) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) PriorityQueue(java.util.PriorityQueue) JoinGraph(com.facebook.presto.sql.planner.optimizations.joins.JoinGraph) PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) HashSet(java.util.HashSet)

Example 4 with JoinGraph

use of com.facebook.presto.sql.planner.optimizations.joins.JoinGraph in project presto by prestodb.

the class EliminateCrossJoins method optimize.

@Override
public PlanNode optimize(PlanNode plan, Session session, Map<Symbol, Type> types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator) {
    if (!SystemSessionProperties.isJoinReorderingEnabled(session)) {
        return plan;
    }
    List<JoinGraph> joinGraphs = JoinGraph.buildFrom(plan);
    for (int i = joinGraphs.size() - 1; i >= 0; i--) {
        JoinGraph graph = joinGraphs.get(i);
        List<Integer> joinOrder = getJoinOrder(graph);
        if (isOriginalOrder(joinOrder)) {
            continue;
        }
        plan = rewriteWith(new Rewriter(idAllocator, graph, joinOrder), plan);
    }
    return plan;
}
Also used : SimplePlanRewriter(com.facebook.presto.sql.planner.plan.SimplePlanRewriter) JoinGraph(com.facebook.presto.sql.planner.optimizations.joins.JoinGraph)

Example 5 with JoinGraph

use of com.facebook.presto.sql.planner.optimizations.joins.JoinGraph in project presto by prestodb.

the class TestEliminateCrossJoins method testJoinOrder.

@Test
public void testJoinOrder() {
    PlanNode plan = join(join(values(symbol("a")), values(symbol("b"))), values(symbol("c")), symbol("a"), symbol("c"), symbol("c"), symbol("b"));
    JoinGraph joinGraph = getOnlyElement(JoinGraph.buildFrom(plan));
    assertEquals(EliminateCrossJoins.getJoinOrder(joinGraph), ImmutableList.of(0, 2, 1));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) JoinGraph(com.facebook.presto.sql.planner.optimizations.joins.JoinGraph) Test(org.testng.annotations.Test)

Aggregations

JoinGraph (com.facebook.presto.sql.planner.optimizations.joins.JoinGraph)7 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)6 Test (org.testng.annotations.Test)5 SimplePlanRewriter (com.facebook.presto.sql.planner.plan.SimplePlanRewriter)2 Session (com.facebook.presto.Session)1 SystemSessionProperties (com.facebook.presto.SystemSessionProperties)1 Type (com.facebook.presto.spi.type.Type)1 PlanNodeIdAllocator (com.facebook.presto.sql.planner.PlanNodeIdAllocator)1 Symbol (com.facebook.presto.sql.planner.Symbol)1 SymbolAllocator (com.facebook.presto.sql.planner.SymbolAllocator)1 Assignments (com.facebook.presto.sql.planner.plan.Assignments)1 FilterNode (com.facebook.presto.sql.planner.plan.FilterNode)1 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)1 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)1 ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)1 SimplePlanRewriter.rewriteWith (com.facebook.presto.sql.planner.plan.SimplePlanRewriter.rewriteWith)1 Expression (com.facebook.presto.sql.tree.Expression)1 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList (com.google.common.collect.ImmutableList)1