jackwener commented on code in PR #13902: URL: https://github.com/apache/doris/pull/13902#discussion_r1012764424
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/joinreorder/hypergraph/Edge.java: ########## @@ -22,20 +22,19 @@ import java.util.BitSet; class Edge { - final int index; + int index; Review Comment: should be final. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/joinreorder/hypergraph/GraphSimplifier.java: ########## @@ -0,0 +1,383 @@ +// 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.joinreorder.hypergraph; + +import org.apache.doris.common.Pair; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.properties.PhysicalProperties; +import org.apache.doris.nereids.stats.StatsCalculator; +import org.apache.doris.nereids.trees.plans.GroupPlan; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; + +import com.google.common.base.Preconditions; + +import java.util.ArrayList; +import java.util.BitSet; +import java.util.List; +import java.util.Optional; +import java.util.PriorityQueue; + +/** + * GraphSimplifier is used to simplify HyperGraph {@link HyperGraph} + */ +public class GraphSimplifier { + class SimplificationStep { + double benefit; + int beforeIndex; + int afterIndex; + BitSet newLeft; + BitSet newRight; + Plan leftPlan; + Plan rightPlan; + + SimplificationStep(double benefit, int beforeIndex, int afterIndex, Plan leftPlan, Plan rightPlan, + BitSet newLeft, BitSet newRight) { + this.afterIndex = afterIndex; + this.beforeIndex = beforeIndex; + this.benefit = benefit; + this.leftPlan = leftPlan; + this.rightPlan = rightPlan; + this.newLeft = newLeft; + this.newRight = newRight; + } + + public double getBenefit() { + return benefit; + } + } + + class BestSimplification implements Comparable<BestSimplification> { + int bestNeighbor = -1; + Optional<SimplificationStep> step = Optional.empty(); + // This data whether to be added to the queue + boolean isInQueue = false; + + @Override + public int compareTo(GraphSimplifier.BestSimplification o) { + Preconditions.checkArgument(step.isPresent()); + return Double.compare(getBenefit(), o.getBenefit()); + } + + public double getBenefit() { + return step.get().getBenefit(); + } + + public void setStep(SimplificationStep step) { + this.step = Optional.of(step); + } + + public SimplificationStep getStep() { + return step.get(); + } + } + + // The graph we are simplifying + HyperGraph graph; + + // It stores the children plan of one edge. + // we don't store it in hyper graph, because it's just used for simulating join. + // In fact, the graph simplifier just generate the partial order of join operator. + List<Pair<Plan, Plan>> joinPlans = new ArrayList<>(); + + // This is used for cache the intermediate results when calculate the benefit + // Note that we store it for the after. Because if we put B after A (t1 Join_A t2 Join_B t3), + // B is changed. Therefore, Any step that involves B need to be recalculated. + List<BestSimplification> simplifications = new ArrayList<>(); + PriorityQueue<BestSimplification> priorityQueue = new PriorityQueue<>(); + int edgeSize; + + GraphSimplifier(HyperGraph graph) { + this.graph = graph; + edgeSize = graph.edges.size(); + for (int i = 0; i < edgeSize; i++) { + BestSimplification bestSimplification = new BestSimplification(); + Plan leftPlan = graph.getPlan(graph.getEdge(i).getLeft()); + Plan rightPlan = graph.getPlan(graph.getEdge(i).getRight()); + joinPlans.add(Pair.of(leftPlan, rightPlan)); + simplifications.add(bestSimplification); + } Review Comment: ```suggestion for (Edge edge : graph.edges) { BestSimplification bestSimplification = new BestSimplification(); Plan leftPlan = graph.getPlan(edge.getLeft()); Plan rightPlan = graph.getPlan(edge.getRight()); joinPlans.add(Pair.of(leftPlan, rightPlan)); simplifications.add(bestSimplification); } ``` -- 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-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org