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 cf1803705c..2709d7a7f2 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 @@ -44,6 +44,7 @@ import org.apache.doris.nereids.trees.plans.algebra.EmptyRelation; import org.apache.doris.nereids.trees.plans.algebra.Filter; import org.apache.doris.nereids.trees.plans.algebra.Generate; import org.apache.doris.nereids.trees.plans.algebra.Limit; +import org.apache.doris.nereids.trees.plans.algebra.OlapScan; import org.apache.doris.nereids.trees.plans.algebra.PartitionTopN; import org.apache.doris.nereids.trees.plans.algebra.Project; import org.apache.doris.nereids.trees.plans.algebra.Repeat; @@ -608,7 +609,7 @@ public class StatsCalculator extends DefaultPlanVisitor { return new FilterEstimation().estimate(filter.getPredicate(), stats); } - private ColumnStatistic getColumnStatistic(TableIf table, String colName) { + private ColumnStatistic getColumnStatistic(TableIf table, String colName, long idxId) { ConnectContext connectContext = ConnectContext.get(); if (connectContext != null && connectContext.getSessionVariable().internalSession) { return ColumnStatistic.UNKNOWN; @@ -633,9 +634,8 @@ public class StatsCalculator extends DefaultPlanVisitor { return ColumnStatistic.UNKNOWN; } } else { - // TODO. Get index id for materialized view. return Env.getCurrentEnv().getStatisticsCache().getColumnStatistics( - catalogId, dbId, table.getId(), -1, colName); + catalogId, dbId, table.getId(), idxId, colName); } } @@ -649,8 +649,17 @@ public class StatsCalculator extends DefaultPlanVisitor { TableIf table = catalogRelation.getTable(); double rowCount = catalogRelation.getTable().estimatedRowCount(); boolean hasUnknownCol = false; + long idxId = -1; + if (catalogRelation instanceof OlapScan) { + OlapScan olapScan = (OlapScan) catalogRelation; + if (olapScan.getTable().getBaseIndexId() != olapScan.getSelectedIndexId()) { + idxId = olapScan.getSelectedIndexId(); + } + } for (SlotReference slotReference : slotSet) { - String colName = slotReference.getName(); + String colName = slotReference.getColumn().isPresent() + ? slotReference.getColumn().get().getName() + : slotReference.getName(); boolean shouldIgnoreThisCol = StatisticConstants.shouldIgnoreCol(table, slotReference.getColumn().get()); if (colName == null) { @@ -661,7 +670,7 @@ public class StatsCalculator extends DefaultPlanVisitor { || shouldIgnoreThisCol) { cache = ColumnStatistic.UNKNOWN; } else { - cache = getColumnStatistic(table, colName); + cache = getColumnStatistic(table, colName, idxId); } if (cache.avgSizeByte <= 0) { cache = new ColumnStatisticBuilder(cache)