Skip to content

Commit

Permalink
choose join order by connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Nov 30, 2023
1 parent ce70353 commit 377a7be
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ public Statistics getChildStatistics(int index) {
public List<Statistics> getChildrenStatistics() {
return childrenStats;
}

public StatementContext getStatementContext() {
return connectContext.getStatementContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -90,6 +92,8 @@ public class StatementContext {
private final Map<String, Hint> hintMap = Maps.newLinkedHashMap();
private final Set<String> viewDdlSqlSet = Sets.newHashSet();

private final List<Expression> joinFilters = new ArrayList<>();

public StatementContext() {
this.connectContext = ConnectContext.get();
}
Expand Down Expand Up @@ -246,4 +250,12 @@ public void addViewDdlSql(String ddlSql) {
public List<String> getViewDdlSqls() {
return ImmutableList.copyOf(viewDdlSqlSet);
}

public List<Expression> getJoinFilters() {
return joinFilters;
}

public void addJoinFilters(Collection<Expression> newJoinFilters) {
this.joinFilters.addAll(newJoinFilters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.doris.nereids.properties.DistributionSpecGather;
import org.apache.doris.nereids.properties.DistributionSpecHash;
import org.apache.doris.nereids.properties.DistributionSpecReplicated;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.physical.PhysicalAssertNumRows;
import org.apache.doris.nereids.trees.plans.physical.PhysicalDeferMaterializeOlapScan;
Expand All @@ -48,6 +49,8 @@

import com.google.common.base.Preconditions;

import java.util.Collections;

class CostModelV1 extends PlanVisitor<Cost, PlanContext> {

// for a join, skew = leftRowCount/rightRowCount
Expand Down Expand Up @@ -262,6 +265,15 @@ public Cost visitPhysicalHashJoin(

double leftRowCount = probeStats.getRowCount();
double rightRowCount = buildStats.getRowCount();
if (Math.abs(leftRowCount - rightRowCount) < 10) {
int leftConnectivity = computeConnectivity(physicalHashJoin.left(), context);
int rightConnectivity = computeConnectivity(physicalHashJoin.right(), context);
if (rightConnectivity < leftConnectivity) {
leftRowCount += 10;
}

}

/*
pattern1: L join1 (Agg1() join2 Agg2())
result number of join2 may much less than Agg1.
Expand Down Expand Up @@ -310,6 +322,15 @@ public Cost visitPhysicalHashJoin(
);
}

private int computeConnectivity(
Plan plan, PlanContext context) {
int connectCount = 0;
for (Expression expr : context.getStatementContext().getJoinFilters()) {
connectCount += Collections.disjoint(expr.getInputSlots(), plan.getOutputSet()) ? 0 : 1;
}
return connectCount;
}

@Override
public Cost visitPhysicalNestedLoopJoin(
PhysicalNestedLoopJoin<? extends Plan, ? extends Plan> nestedLoopJoin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static boolean check(SwapType swapType, LogicalJoin<GroupPlan, GroupPlan>
if (swapType == SwapType.LEFT_ZIG_ZAG) {
double leftRows = join.left().getGroup().getStatistics().getRowCount();
double rightRows = join.right().getGroup().getStatistics().getRowCount();
return leftRows < rightRows && isZigZagJoin(join);
return leftRows <= rightRows && isZigZagJoin(join);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public Rule build() {
Plan plan = joinToMultiJoin(filter, planToHintType);
Preconditions.checkState(plan instanceof MultiJoin);
MultiJoin multiJoin = (MultiJoin) plan;
ctx.statementContext.addJoinFilters(multiJoin.getJoinFilter());
ctx.statementContext.setMaxNAryInnerJoin(multiJoin.children().size());
Plan after = multiJoinToJoin(multiJoin, planToHintType);
return after;
Expand Down

0 comments on commit 377a7be

Please sign in to comment.