Skip to content

Commit 3cd8da3

Browse files
authored
[opt](nereids) tabe row count priority: user injected > BE report > analyzed (3.0) (#41347)
## Proposed changes pick #40529 Issue Number: close #xxx <!--Describe your changes.-->
1 parent 39520b4 commit 3cd8da3

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,28 @@ private void checkIfUnknownStatsUsedAsKey(StatisticsBuilder builder) {
397397
}
398398
}
399399

400+
private double getOlapTableRowCount(OlapScan olapScan) {
401+
OlapTable olapTable = olapScan.getTable();
402+
AnalysisManager analysisManager = Env.getCurrentEnv().getAnalysisManager();
403+
TableStatsMeta tableMeta = analysisManager.findTableStatsStatus(olapScan.getTable().getId());
404+
double rowCount = -1;
405+
if (tableMeta != null && tableMeta.userInjected) {
406+
rowCount = tableMeta.getRowCount(olapScan.getSelectedIndexId());
407+
} else {
408+
rowCount = olapTable.getRowCountForIndex(olapScan.getSelectedIndexId(), true);
409+
if (rowCount == -1) {
410+
if (tableMeta != null) {
411+
rowCount = tableMeta.getRowCount(olapScan.getSelectedIndexId());
412+
}
413+
}
414+
}
415+
return rowCount;
416+
}
417+
400418
private Statistics computeOlapScan(OlapScan olapScan) {
401419
OlapTable olapTable = olapScan.getTable();
402-
double tableRowCount = olapTable.getRowCountForIndex(olapScan.getSelectedIndexId());
420+
double tableRowCount = getOlapTableRowCount(olapScan);
421+
tableRowCount = Math.max(1, tableRowCount);
403422
if (tableRowCount <= 0) {
404423
AnalysisManager analysisManager = Env.getCurrentEnv().getAnalysisManager();
405424
TableStatsMeta tableMeta = analysisManager.findTableStatsStatus(olapScan.getTable().getId());
@@ -471,11 +490,13 @@ private Statistics computeOlapScan(OlapScan olapScan) {
471490
builder.putColumnStatistics(slot, ColumnStatistic.UNKNOWN);
472491
}
473492
}
474-
493+
boolean useTableLevelStats = true;
475494
if (olapScan.getSelectedPartitionIds().size() < olapScan.getTable().getPartitionNum()) {
476495
// partition pruned
496+
// try to use selected partition stats, if failed, fall back to table stats
477497
double selectedPartitionsRowCount = getSelectedPartitionRowCount(olapScan);
478-
if (selectedPartitionsRowCount > 0) {
498+
if (selectedPartitionsRowCount >= 0) {
499+
useTableLevelStats = false;
479500
List<String> selectedPartitionNames = new ArrayList<>(olapScan.getSelectedPartitionIds().size());
480501
olapScan.getSelectedPartitionIds().forEach(id -> {
481502
selectedPartitionNames.add(olapScan.getTable().getPartition(id).getName());
@@ -489,19 +510,11 @@ private Statistics computeOlapScan(OlapScan olapScan) {
489510
}
490511
checkIfUnknownStatsUsedAsKey(builder);
491512
builder.setRowCount(selectedPartitionsRowCount);
492-
} else {
493-
// if partition row count is invalid (-1), fallback to table stats
494-
for (SlotReference slot : visibleOutputSlots) {
495-
ColumnStatistic cache = getColumnStatsFromTableCache((CatalogRelation) olapScan, slot);
496-
ColumnStatisticBuilder colStatsBuilder = new ColumnStatisticBuilder(cache);
497-
colStatsBuilder.setCount(tableRowCount);
498-
adjustColStats(olapScan, slot, colStatsBuilder);
499-
builder.putColumnStatistics(slot, colStatsBuilder.build());
500-
}
501-
checkIfUnknownStatsUsedAsKey(builder);
502-
builder.setRowCount(tableRowCount);
503513
}
504-
} else {
514+
}
515+
// 1. no partition is pruned, or
516+
// 2. fall back to table stats
517+
if (useTableLevelStats) {
505518
// get table level stats
506519
for (SlotReference slot : visibleOutputSlots) {
507520
ColumnStatistic cache = getColumnStatsFromTableCache((CatalogRelation) olapScan, slot);

0 commit comments

Comments
 (0)