zhengshiJ commented on code in PR #11179: URL: https://github.com/apache/doris/pull/11179#discussion_r929593583
########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculator.java: ########## @@ -0,0 +1,249 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.stats; + +import org.apache.doris.catalog.Catalog; +import org.apache.doris.catalog.Table; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.trees.Filter; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.plans.Aggregate; +import org.apache.doris.nereids.trees.plans.GroupPlan; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.Project; +import org.apache.doris.nereids.trees.plans.Scan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalSort; +import org.apache.doris.nereids.trees.plans.physical.PhysicalAggregate; +import org.apache.doris.nereids.trees.plans.physical.PhysicalDistribution; +import org.apache.doris.nereids.trees.plans.physical.PhysicalFilter; +import org.apache.doris.nereids.trees.plans.physical.PhysicalHashJoin; +import org.apache.doris.nereids.trees.plans.physical.PhysicalHeapSort; +import org.apache.doris.nereids.trees.plans.physical.PhysicalOlapScan; +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; +import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanVisitor; +import org.apache.doris.nereids.util.Utils; +import org.apache.doris.statistics.ColumnStats; +import org.apache.doris.statistics.StatsDeriveResult; +import org.apache.doris.statistics.TableStats; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Used to calculate the stats for each operator + */ +public class StatsCalculator extends DefaultPlanVisitor<StatsDeriveResult, Void> { + + private final GroupExpression groupExpression; + + public StatsCalculator(GroupExpression groupExpression) { + this.groupExpression = groupExpression; + } + + /** + * Do estimate. + */ + public void estimate() { + StatsDeriveResult stats = groupExpression.getPlan().accept(this, null); + groupExpression.getOwnerGroup().setStatistics(stats); + Plan plan = groupExpression.getPlan(); + long limit = plan.getLimit(); + if (limit != -1) { + stats.setRowCount(Math.min(limit, stats.getRowCount())); + } + groupExpression.setStatDerived(true); + } + + @Override + public StatsDeriveResult visitLogicalAggregate(LogicalAggregate<Plan> agg, Void context) { + return computeAggregate(agg); + } + + @Override + public StatsDeriveResult visitLogicalFilter(LogicalFilter<Plan> filter, Void context) { + return computeFilter(filter); + } + + @Override + public StatsDeriveResult visitLogicalOlapScan(LogicalOlapScan olapScan, Void context) { + olapScan.getExpressions(); + return computeScan(olapScan); + } + + @Override + public StatsDeriveResult visitLogicalProject(LogicalProject<Plan> project, Void context) { + return computeProject(project); + } + + @Override + public StatsDeriveResult visitLogicalSort(LogicalSort<Plan> sort, Void context) { + return groupExpression.getCopyOfChildStats(0); + } + + @Override + public StatsDeriveResult visitLogicalJoin(LogicalJoin<Plan, Plan> join, Void context) { + return HashJoinEstimation.estimate(groupExpression.getCopyOfChildStats(0), + groupExpression.getCopyOfChildStats(1), + join.getCondition().get(), join.getJoinType()); + } + + @Override + public StatsDeriveResult visitGroupPlan(GroupPlan groupPlan, Void context) { + return super.visitGroupPlan(groupPlan, context); + } + + @Override + public StatsDeriveResult visitPhysicalAggregate(PhysicalAggregate<Plan> agg, Void context) { + return computeAggregate(agg); + } + + @Override + public StatsDeriveResult visitPhysicalOlapScan(PhysicalOlapScan olapScan, Void context) { + return computeScan(olapScan); + } + + @Override + public StatsDeriveResult visitPhysicalHeapSort(PhysicalHeapSort<Plan> sort, Void context) { + return groupExpression.getCopyOfChildStats(0); + } + + @Override + public StatsDeriveResult visitPhysicalHashJoin(PhysicalHashJoin<Plan, Plan> hashJoin, Void context) { + return HashJoinEstimation.estimate(groupExpression.getCopyOfChildStats(0), + groupExpression.getCopyOfChildStats(1), + hashJoin.getCondition().get(), hashJoin.getJoinType()); + } + + // TODO: We should subtract those pruned column, and consider the expression transformations in the node. + @Override + public StatsDeriveResult visitPhysicalProject(PhysicalProject<Plan> project, Void context) { + return computeProject(project); + } + + @Override + public StatsDeriveResult visitPhysicalFilter(PhysicalFilter<Plan> filter, Void context) { + return computeFilter(filter); + } + + @Override + public StatsDeriveResult visitPhysicalDistribution(PhysicalDistribution<Plan> distribution, + Void context) { + return groupExpression.getCopyOfChildStats(0); + } + + private StatsDeriveResult computeFilter(Filter filter) { + StatsDeriveResult stats = groupExpression.getCopyOfChildStats(0); + FilterSelectivityCalculator selectivityCalculator = + new FilterSelectivityCalculator(stats.getSlotRefToColumnStatsMap()); + double selectivity = selectivityCalculator.estimate(filter.getPredicates()); + stats.multiplyDouble(selectivity); + return stats; + } + + // TODO: 1. Subtract the pruned partition + // 2. Consider the influence of runtime filter + // 3. Get NDV and column data size from StatisticManger, StatisticManager doesn't support it now. + private StatsDeriveResult computeScan(Scan scan) { + Table table = scan.getTable(); + TableStats tableStats = Utils.execWithReturnVal(() -> + Catalog.getCurrentCatalog().getStatisticsManager().getStatistics().getTableStats(table.getId()) + ); + Map<Slot, ColumnStats> slotToColumnStats = new HashMap<>(); + Set<SlotReference> slotSet = scan.getOutput().stream().filter(SlotReference.class::isInstance) + .map(s -> (SlotReference) s).collect(Collectors.toSet()); + for (SlotReference slotReference : slotSet) { + String colName = slotReference.getName(); + if (colName == null) { + // TODO: should we throw an exception here? Review Comment: I think an error needs to be reported, because the scan node will generate the column name. -- 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