Skip to content

Commit

Permalink
disable join reorder if any table row count is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Sep 9, 2024
1 parent 0d3374b commit 2c9998a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.doris.nereids.processor.pre.PlanPreprocessors;
import org.apache.doris.nereids.properties.PhysicalProperties;
import org.apache.doris.nereids.rules.exploration.mv.MaterializationContext;
import org.apache.doris.nereids.stats.StatsCalculator;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.plans.ComputeResultSet;
Expand All @@ -56,6 +57,7 @@
import org.apache.doris.nereids.trees.plans.distribute.DistributePlanner;
import org.apache.doris.nereids.trees.plans.distribute.DistributedPlan;
import org.apache.doris.nereids.trees.plans.distribute.FragmentIdMapping;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.logical.LogicalSqlCache;
import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
Expand Down Expand Up @@ -255,6 +257,8 @@ private Plan planWithoutLock(
return rewrittenPlan;
}
}
List<LogicalOlapScan> scans = getAllOlapScans(cascadesContext.getRewritePlan());
StatsCalculator.disableJoinReorderIfTableRowCountNotAvailable(scans, cascadesContext);

optimize();
if (statementContext.getConnectContext().getExecutor() != null) {
Expand Down Expand Up @@ -288,6 +292,18 @@ private Plan planWithoutLock(
return physicalPlan;
}

private List<LogicalOlapScan> getAllOlapScans(Plan plan) {
List<LogicalOlapScan> scans = Lists.newArrayList();
if (plan instanceof LogicalOlapScan) {
scans.add((LogicalOlapScan) plan);
} else {
for (Plan child : plan.children()) {
scans.addAll(getAllOlapScans(child));
}
}
return scans;
}

private LogicalPlan preprocess(LogicalPlan logicalPlan) {
return new PlanPreprocessors(statementContext).process(logicalPlan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public class StatsCalculator extends DefaultPlanVisitor<Statistics, Void> {

private CascadesContext cascadesContext;

private StatsCalculator(CascadesContext context) {
this.groupExpression = null;
this.cascadesContext = context;
}

private StatsCalculator(GroupExpression groupExpression, boolean forbidUnknownColStats,
Map<String, ColumnStatistic> columnStatisticMap, boolean isPlayNereidsDump,
Map<CTEId, Statistics> cteIdToStats, CascadesContext context) {
Expand All @@ -205,6 +210,22 @@ public Map<String, ColumnStatistic> getTotalColumnStatisticMap() {
return totalColumnStatisticMap;
}

/**
* disable join reorder if any table row count is not available.
*/
public static void disableJoinReorderIfTableRowCountNotAvailable(
List<LogicalOlapScan> scans, CascadesContext context) {
StatsCalculator calculator = new StatsCalculator(context);
for (LogicalOlapScan scan : scans) {
double rowCount = calculator.getOlapTableRowCount(scan);
if (rowCount == -1 && ConnectContext.get() != null) {
LOG.info("disable join reorder since row count not available: "
+ scan.getTable().getNameWithFullQualifiers());
ConnectContext.get().getSessionVariable().setDisableJoinReorder(true);
}
}
}

/**
* estimate stats
*/
Expand All @@ -217,15 +238,6 @@ public static StatsCalculator estimate(GroupExpression groupExpression, boolean
return statsCalculator;
}

public static StatsCalculator estimate(GroupExpression groupExpression, boolean forbidUnknownColStats,
Map<String, ColumnStatistic> columnStatisticMap, boolean isPlayNereidsDump, CascadesContext context) {
return StatsCalculator.estimate(groupExpression,
forbidUnknownColStats,
columnStatisticMap,
isPlayNereidsDump,
new HashMap<>(), context);
}

// For unit test only
public static void estimate(GroupExpression groupExpression, CascadesContext context) {
StatsCalculator statsCalculator = new StatsCalculator(groupExpression, false,
Expand Down

0 comments on commit 2c9998a

Please sign in to comment.