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 24550f4f7246e7..00ddae718ca9fe 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 @@ -50,6 +50,7 @@ import org.apache.doris.nereids.trees.expressions.literal.Literal; import org.apache.doris.nereids.trees.plans.AbstractPlan; import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.CatalogRelation; import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel; import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; @@ -266,8 +267,8 @@ private Plan planWithoutLock( // 1. user set leading hint // 2. ut test. In ut test, FeConstants.enableInternalSchemaDb is false or FeConstants.runningUnitTest is true if (FeConstants.enableInternalSchemaDb && !FeConstants.runningUnitTest && cascadesContext.isLeadingJoin()) { - List scans = cascadesContext.getRewritePlan() - .collectToList(LogicalOlapScan.class::isInstance); + List scans = cascadesContext.getRewritePlan() + .collectToList(CatalogRelation.class::isInstance); Optional reason = StatsCalculator.disableJoinReorderIfStatsInvalid(scans, cascadesContext); reason.ifPresent(LOG::info); } 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 1fc3708545e690..b088ed0dccfa46 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 @@ -655,10 +655,6 @@ private Statistics computeCatalogRelation(CatalogRelation catalogRelation) { idxId = olapScan.getSelectedIndexId(); } } - // if (deltaRowCount > 0 && LOG.isDebugEnabled()) { - // LOG.debug("{} is partially analyzed, clear min/max values in column stats", - // catalogRelation.getTable().getName()); - // } for (SlotReference slotReference : slotSet) { String colName = slotReference.getColumn().isPresent() ? slotReference.getColumn().get().getName() @@ -692,14 +688,6 @@ private Statistics computeCatalogRelation(CatalogRelation catalogRelation) { hasUnknownCol = true; } if (ConnectContext.get() != null && ConnectContext.get().getSessionVariable().enableStats) { - // if (deltaRowCount > 0) { - // // clear min-max to avoid error estimation - // // for example, after yesterday data loaded, user send query about yesterday immediately. - // // since yesterday data are not analyzed, the max date is before yesterday, and hence optimizer - // // estimates the filter result is zero - // colStatsBuilder.setMinExpr(null).setMinValue(Double.NEGATIVE_INFINITY) - // .setMaxExpr(null).setMaxValue(Double.POSITIVE_INFINITY); - // } columnStatisticBuilderMap.put(slotReference, colStatsBuilder); } else { columnStatisticBuilderMap.put(slotReference, new ColumnStatisticBuilder(ColumnStatistic.UNKNOWN)); @@ -1201,34 +1189,44 @@ private Optional checkNdvValidation(OlapScan olapScan, double rowCount) * 2. col stats ndv=0 but minExpr or maxExpr is not null * 3. ndv > 10 * rowCount */ - public static Optional disableJoinReorderIfStatsInvalid(List scans, + public static Optional disableJoinReorderIfStatsInvalid(List scans, CascadesContext context) { StatsCalculator calculator = new StatsCalculator(context); if (ConnectContext.get() == null) { // ut case return Optional.empty(); } - for (LogicalOlapScan scan : scans) { - double rowCount = calculator.getOlapTableRowCount(scan); + for (CatalogRelation scan : scans) { + double rowCount = calculator.getTableRowCount(scan); // row count not available if (rowCount == -1) { LOG.info("disable join reorder since row count not available: " + scan.getTable().getNameWithFullQualifiers()); return Optional.of("table[" + scan.getTable().getName() + "] row count is invalid"); } - // ndv abnormal - Optional reason = calculator.checkNdvValidation(scan, rowCount); - if (reason.isPresent()) { - try { - ConnectContext.get().getSessionVariable().disableNereidsJoinReorderOnce(); - LOG.info("disable join reorder since col stats invalid: " - + reason.get()); - } catch (Exception e) { - LOG.info("disableNereidsJoinReorderOnce failed"); + if (scan instanceof OlapScan) { + // ndv abnormal + Optional reason = calculator.checkNdvValidation((OlapScan) scan, rowCount); + if (reason.isPresent()) { + try { + ConnectContext.get().getSessionVariable().disableNereidsJoinReorderOnce(); + LOG.info("disable join reorder since col stats invalid: " + + reason.get()); + } catch (Exception e) { + LOG.info("disableNereidsJoinReorderOnce failed"); + } + return reason; } - return reason; } } return Optional.empty(); } + + private double getTableRowCount(CatalogRelation scan) { + if (scan instanceof OlapScan) { + return getOlapTableRowCount((OlapScan) scan); + } else { + return scan.getTable().getRowCount(); + } + } }