From 2c9998ac9f7f66ddd55e69fbe64ec9ccd434f6a0 Mon Sep 17 00:00:00 2001 From: minghong Date: Mon, 9 Sep 2024 20:28:05 +0800 Subject: [PATCH] disable join reorder if any table row count is not available --- .../apache/doris/nereids/NereidsPlanner.java | 16 ++++++++++ .../doris/nereids/stats/StatsCalculator.java | 30 +++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java index a304fe36062c80b..663b78ec4fc5444 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java @@ -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; @@ -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; @@ -255,6 +257,8 @@ private Plan planWithoutLock( return rewrittenPlan; } } + List scans = getAllOlapScans(cascadesContext.getRewritePlan()); + StatsCalculator.disableJoinReorderIfTableRowCountNotAvailable(scans, cascadesContext); optimize(); if (statementContext.getConnectContext().getExecutor() != null) { @@ -288,6 +292,18 @@ private Plan planWithoutLock( return physicalPlan; } + private List getAllOlapScans(Plan plan) { + List 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); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java index 5946192a27eff9a..7710926c2cda212 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java @@ -182,6 +182,11 @@ public class StatsCalculator extends DefaultPlanVisitor { private CascadesContext cascadesContext; + private StatsCalculator(CascadesContext context) { + this.groupExpression = null; + this.cascadesContext = context; + } + private StatsCalculator(GroupExpression groupExpression, boolean forbidUnknownColStats, Map columnStatisticMap, boolean isPlayNereidsDump, Map cteIdToStats, CascadesContext context) { @@ -205,6 +210,22 @@ public Map getTotalColumnStatisticMap() { return totalColumnStatisticMap; } + /** + * disable join reorder if any table row count is not available. + */ + public static void disableJoinReorderIfTableRowCountNotAvailable( + List 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 */ @@ -217,15 +238,6 @@ public static StatsCalculator estimate(GroupExpression groupExpression, boolean return statsCalculator; } - public static StatsCalculator estimate(GroupExpression groupExpression, boolean forbidUnknownColStats, - Map 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,