englefly commented on code in PR #18784: URL: https://github.com/apache/doris/pull/18784#discussion_r1196233623
########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java: ########## @@ -464,6 +478,32 @@ private Statistics computeTopN(TopN topN) { return stats.withRowCount(Math.min(stats.getRowCount(), topN.getLimit())); } + private Statistics computePartitionTopN(PartitionTopN partitionTopN) { + Statistics stats = groupExpression.childStatistics(0); + double rowCount = stats.getRowCount(); + List<Expression> partitionKeys = partitionTopN.getPartitionKeys(); + if (!partitionTopN.hasGlobalLimit() && !partitionKeys.isEmpty()) { + // If there is no global limit. So result for the cardinality estimation is: + // NDV(partition key) * partitionLimit + Map<Expression, ColumnStatistic> childSlotToColumnStats = stats.columnStatistics(); + List<ColumnStatistic> partitionByKeyStats = partitionKeys.stream() + .filter(childSlotToColumnStats::containsKey) + .map(childSlotToColumnStats::get) + .filter(s -> !s.isUnKnown) + .collect(Collectors.toList()); + if (partitionByKeyStats.isEmpty()) { + // all column stats are unknown, use default ratio + rowCount = rowCount * DEFAULT_COLUMN_NDV_RATION; + } else { + rowCount = Math.min(rowCount, partitionByKeyStats.stream().map(s -> s.ndv) + .max(Double::compare).get()); + } + } else { + rowCount = Math.min(rowCount, partitionTopN.getPartitionLimit()); + } + return stats.withRowCount(rowCount); Review Comment: yes, we need to update column stats. if we do not know how to update col stats, just use Statisitcs.updateRowCountOnly() -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org