You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/10/20 02:21:32 UTC

[GitHub] [doris] jackwener commented on a diff in pull request #13449: [WIP-feature](Nereids): join reorder greedy and Graph for join cluster.

jackwener commented on code in PR #13449:
URL: https://github.com/apache/doris/pull/13449#discussion_r1000079412


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/join/JoinReorderGreedy.java:
##########
@@ -0,0 +1,136 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.join;
+
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * Greedy Heuristics Join Reorder.
+ */
+public class JoinReorderGreedy {
+
+    HyperGraph graph;
+
+    public JoinReorderGreedy(HyperGraph graph) {
+        this.graph = graph;
+    }
+
+    /**
+     * A Group Plan which contains multiple plans.
+     */
+    public class PlanGroupSet {
+        List<Integer> nodes = Lists.newArrayList();
+        BitSet nodesBitSet = new BitSet(32);
+        List<List<Integer>> edges = Lists.newArrayList();
+        double lowestCost = Double.MAX_VALUE;
+        long rowCount = -1;
+
+        public PlanGroupSet(Integer firstInput) {
+            this.nodes.add(firstInput);
+            this.nodesBitSet.set(firstInput);
+        }
+
+        public void addPlan(int newPlan, double lowestCost) {
+            this.edges.add(graph.findEdges(nodesBitSet, newPlan));
+            this.nodes.add(newPlan);
+            this.nodesBitSet.set(newPlan);
+            this.lowestCost = lowestCost;
+        }
+
+        /**
+         * Convert to Plan tree.
+         */
+        public Plan toPlan() {
+            Plan left;
+            left = graph.plan(nodes.get(0));
+            for (int i = 1; i < nodes.size(); i++) {
+                int rightIndex = nodes.get(i);
+                List<Integer> edgeIndex = edges.get(i - 1);
+                left = new LogicalJoin<>(
+                        graph.joinTypes(edgeIndex.get(0)),
+                        graph.conditions(edgeIndex),
+                        left,
+                        graph.plan(rightIndex)
+                );
+            }
+
+            return left;
+        }
+    }
+
+    /**
+     * do greedy.
+     */
+    public PlanGroupSet greedy() {
+        List<PlanGroupSet> result = new ArrayList<>(graph.nodes().size());
+        for (int i = 0; i < graph.nodes().size(); i++) {
+            // i as starting left plannode.
+            // For each starting left plannode, do greedy selection。
+            PlanGroupSet leftPlanGroupSet = new PlanGroupSet(i);
+            result.add(leftPlanGroupSet);
+
+            findLowestCostRight(leftPlanGroupSet);
+        }
+
+        return result.stream()
+                .min(Comparator.comparingDouble(a -> a.lowestCost))
+                .orElseThrow(() -> new RuntimeException("don't exist best plan."));
+    }
+
+    private void findLowestCostRight(PlanGroupSet leftPlans) {
+        if (leftPlans.nodes.size() == graph.nodes().size()) {
+            return;
+        }
+        BitSet rightCandidates = leftPlans.nodes.stream()
+                .map(left -> graph.nodes().get(left).simpleNeighborhood)
+                .reduce(new BitSet(), (lhs, rhs) -> {
+                    lhs.or(rhs);
+                    return lhs;
+                });
+        rightCandidates.andNot(leftPlans.nodesBitSet);
+
+        int right = -1;
+        double lowestCost = Double.MAX_VALUE;
+
+        for (int rightCandidate = rightCandidates.nextSetBit(0); rightCandidate != -1;
+                rightCandidate = rightCandidates.nextSetBit(rightCandidate + 1)) {
+            double cost = getCost(leftPlans, right);
+            if (cost <= lowestCost) {
+                right = rightCandidate;
+            }

Review Comment:
   It's just children compare children, no means that whole plans.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org