morrySnow commented on code in PR #64559: URL: https://github.com/apache/doris/pull/64559#discussion_r3504202210
########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/MemoStatsAndCostRecomputer.java: ########## @@ -0,0 +1,839 @@ +// 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.common.Pair; +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.cost.Cost; +import org.apache.doris.nereids.cost.CostCalculator; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.properties.PhysicalProperties; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.Join; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEProducer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.statistics.Statistics; + +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Re-estimate memo logical row counts and rebuild physical costs. + * and rebuild physical cost state. + */ +public final class MemoStatsAndCostRecomputer { + private static final double CHOSEN_PROJECT_STATS_DIVERGENCE_RATIO_THRESHOLD = 1_000D; + protected final Map<Group, Integer> groupTrustJoinCountCache = new HashMap<>(); Review Comment: `groupTrustJoinCountCache` is declared `protected` but the enclosing class `MemoStatsAndCostRecomputer` is `final`. Consider changing to `private` since the class cannot be subclassed. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/MemoStatsAndCostRecomputer.java: ########## @@ -0,0 +1,839 @@ +// 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.common.Pair; +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.cost.Cost; +import org.apache.doris.nereids.cost.CostCalculator; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.properties.PhysicalProperties; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.Join; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEProducer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.statistics.Statistics; + +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Re-estimate memo logical row counts and rebuild physical costs. + * and rebuild physical cost state. + */ +public final class MemoStatsAndCostRecomputer { + private static final double CHOSEN_PROJECT_STATS_DIVERGENCE_RATIO_THRESHOLD = 1_000D; + protected final Map<Group, Integer> groupTrustJoinCountCache = new HashMap<>(); + private final CascadesContext cascadesContext; + private final Map<CTEId, Statistics> cteIdToStats = new HashMap<>(); + private final LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy; + + MemoStatsAndCostRecomputer(CascadesContext cascadesContext, + LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy) { + this.cascadesContext = cascadesContext; + this.logicalExpressionRowCountSyncPolicy = logicalExpressionRowCountSyncPolicy; + } + + /** + * recompute + */ + public static void recompute(Group rootGroup, PhysicalProperties physicalProperties, + CascadesContext cascadesContext) { + recompute(rootGroup, physicalProperties, cascadesContext, + LogicalExpressionRowCountSyncPolicy.KEEP_INDIVIDUAL_EXPRESSION_ROW_COUNT); + } + + /** + * recompute with configurable logical expression row count sync behavior. + */ + public static void recompute(Group rootGroup, PhysicalProperties physicalProperties, + CascadesContext cascadesContext, + LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy) { + MemoStatsAndCostRecomputer recomputer = new MemoStatsAndCostRecomputer(cascadesContext, + logicalExpressionRowCountSyncPolicy); + recomputer.seedProducerStats(rootGroup, new HashSet<>()); + recomputer.groupTrustJoinCountCache.clear(); + recomputer.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); + // Run a second pass so CTE consumers and their ancestors can settle on producer stats refreshed above. + // Skip when no CTEs exist — the bottom-up first pass already produces settled stats for non-CTE queries. + if (!recomputer.cteIdToStats.isEmpty()) { + recomputer.groupTrustJoinCountCache.clear(); + recomputer.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); + } + recomputer.recomputePhysicalCostsBottomUp(rootGroup, new HashSet<>()); + } + + private void seedProducerStats(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + Statistics statistics = group.getStatistics(); + if (statistics != null) { + recordProducerStats(group, statistics); + } + for (Group child : getTraversalChildren(group)) { + seedProducerStats(child, visited); + } + } + + private void reestimateLogicalStatsBottomUp(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + for (Group child : getTraversalChildren(group)) { + reestimateLogicalStatsBottomUp(child, visited); + } + reestimateCurrentGroup(group); + refreshEnforcerRowCount(group); + } + + private void reestimateCurrentGroup(Group group) { + List<GroupExpression> estimableExpressions = getEstimableLogicalExpressions(group); + if (estimableExpressions.isEmpty()) { + if (group.getLogicalExpressions().isEmpty()) { + reestimatePhysicalOnlyGroup(group); + } + return; + } + Statistics originalStatistics = group.getStatistics(); + boolean originalStatsReliable = group.isStatsReliable(); + Map<GroupExpression, Statistics> candidateStatisticsByExpression = new LinkedHashMap<>(); + Map<GroupExpression, Boolean> candidateStatsReliableByExpression = new LinkedHashMap<>(); + for (GroupExpression logicalExpression : estimableExpressions) { + group.setStatistics(null); + estimateStats(logicalExpression); + Statistics estimatedStatistics = group.getStatistics(); + if (estimatedStatistics == null || !isValidCandidateStatistics(estimatedStatistics)) { + continue; + } + logicalExpression.setEstOutputRowCount(estimatedStatistics.getRowCount()); + // Safe to store the reference: group.setStatistics(null) above releases the group's + // hold, and the next iteration will overwrite with a new object from estimateStats(). + candidateStatisticsByExpression.put(logicalExpression, estimatedStatistics); + candidateStatsReliableByExpression.put(logicalExpression, group.isStatsReliable()); + } + if (candidateStatisticsByExpression.isEmpty()) { + group.setStatistics(originalStatistics); + group.setStatsReliable(originalStatsReliable); + return; + } + LogicalRowCountAggregationPolicy aggregationPolicy = getLogicalRowCountAggregationPolicy(); + Map<GroupExpression, Statistics> selectedCandidateStatisticsByExpression = filterCandidateStatisticsByPolicy( + aggregationPolicy, candidateStatisticsByExpression); + List<Statistics> candidateStatistics = new ArrayList<>(selectedCandidateStatisticsByExpression.values()); + double aggregatedRowCount = aggregationPolicy.aggregate(candidateStatistics); + Statistics updatedStatistics = resolveUpdatedGroupStatistics(group, selectedCandidateStatisticsByExpression, + candidateStatistics, aggregatedRowCount, originalStatistics); + boolean resolvedStatsReliable = resolveUpdatedGroupStatsReliability(group, + selectedCandidateStatisticsByExpression, candidateStatsReliableByExpression, + aggregatedRowCount); + group.setStatsReliable(resolvedStatsReliable); + group.setStatistics(updatedStatistics); + repairInvalidLogicalExpressionRowCounts(group, aggregatedRowCount); + refreshPhysicalExpressionRowCount(group, updatedStatistics.getRowCount()); + recordProducerStats(group, updatedStatistics); + if (shouldSyncLogicalExpressionRowCount()) { + syncLogicalExpressionRowCount(group, updatedStatistics.getRowCount()); + } + } + + private void reestimatePhysicalOnlyGroup(Group group) { + List<GroupExpression> estimableExpressions = getEstimablePhysicalExpressions(group); + if (estimableExpressions.isEmpty()) { + return; + } + Statistics originalStatistics = group.getStatistics(); + boolean originalStatsReliable = group.isStatsReliable(); + Map<GroupExpression, Statistics> candidateStatisticsByExpression = new LinkedHashMap<>(); + Map<GroupExpression, Boolean> candidateStatsReliableByExpression = new LinkedHashMap<>(); + for (GroupExpression physicalExpression : estimableExpressions) { + group.setStatistics(null); + estimateStats(physicalExpression); + Statistics estimatedStatistics = group.getStatistics(); + if (estimatedStatistics == null || !isValidCandidateStatistics(estimatedStatistics)) { + continue; + } + physicalExpression.setEstOutputRowCount(estimatedStatistics.getRowCount()); + candidateStatisticsByExpression.put(physicalExpression, new Statistics(estimatedStatistics)); + candidateStatsReliableByExpression.put(physicalExpression, group.isStatsReliable()); + } + if (candidateStatisticsByExpression.isEmpty()) { + group.setStatistics(originalStatistics); + group.setStatsReliable(originalStatsReliable); + return; + } + Statistics updatedStatistics = choosePhysicalOnlyGroupStatistics(group, candidateStatisticsByExpression, + originalStatistics); + boolean resolvedStatsReliable = resolvePhysicalOnlyGroupStatsReliability(group, + candidateStatisticsByExpression, candidateStatsReliableByExpression, + originalStatistics); + group.setStatsReliable(resolvedStatsReliable); + group.setStatistics(updatedStatistics); + refreshPhysicalExpressionRowCount(group, updatedStatistics.getRowCount()); + recordProducerStats(group, updatedStatistics); + } + + private boolean isValidCandidateStatistics(Statistics statistics) { + return Double.isFinite(statistics.getRowCount()) && statistics.getRowCount() >= 0; + } + + private void estimateStats(GroupExpression groupExpression) { + ConnectContext connectContext = cascadesContext.getConnectContext(); + StatsCalculator statsCalculator = new StatsCalculator( + groupExpression, + connectContext.getSessionVariable().getForbidUnknownColStats(), + connectContext.getTotalColumnStatisticMap(), + connectContext.getSessionVariable().isPlayNereidsDump(), + cteIdToStats, + cascadesContext); + statsCalculator.estimate(); + } + + private void recomputePhysicalCostsBottomUp(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + for (Group child : getTraversalChildren(group)) { + recomputePhysicalCostsBottomUp(child, visited); + } + if (group.getStatistics() == null + || (group.getPhysicalExpressions().isEmpty() && group.getEnforcers().isEmpty())) { + refreshEnforcerRowCount(group); + return; + } + Map<PhysicalProperties, Pair<Cost, GroupExpression>> originalLowestCostPlans = + snapshotLowestCostPlans(group); + group.clearLowestCostPlans(); + for (GroupExpression physicalExpression : group.getPhysicalExpressions()) { + recomputeGroupExpressionCost(group, physicalExpression); + } + refreshEnforcerRowCount(group); + for (GroupExpression enforcer : group.getEnforcers().values()) { + recomputeGroupExpressionCost(group, enforcer); + } + restoreMissingLowestCostPlans(group, originalLowestCostPlans); + } + + private void recomputeGroupExpressionCost(Group ownerGroup, GroupExpression groupExpression) { + if (ownerGroup.getStatistics() == null || !hasCompleteChildStatistics(groupExpression)) { + return; + } + Cost originalCost = groupExpression.getCost(); + Map<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> originalLowestCostTable + = new LinkedHashMap<>(groupExpression.getLowestCostTable()); + Map<PhysicalProperties, PhysicalProperties> originalRequestPropertiesMap + = new LinkedHashMap<>(groupExpression.getRequestPropertiesMap()); + groupExpression.clearCostState(); + + Cost bestNodeCost = null; + for (Map.Entry<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> entry + : originalLowestCostTable.entrySet()) { + PhysicalProperties outputProperties = entry.getKey(); + List<PhysicalProperties> childInputProperties = entry.getValue().second; + if (!hasAvailableChildBestPlan(groupExpression, childInputProperties)) { + continue; + } + Cost nodeCost = CostCalculator.calculateCost(cascadesContext.getConnectContext(), + groupExpression, childInputProperties); + Cost totalCost = nodeCost; + for (int i = 0; i < childInputProperties.size(); i++) { + Optional<Pair<Cost, GroupExpression>> childBestPlan = groupExpression.child(i) + .getLowestCostPlan(childInputProperties.get(i)); + if (!childBestPlan.isPresent()) { + totalCost = null; + break; + } + totalCost = CostCalculator.addChildCost(cascadesContext.getConnectContext(), + groupExpression.getPlan(), totalCost, childBestPlan.get().first, i); + } + if (totalCost == null) { + continue; + } + groupExpression.updateLowestCostTable( + outputProperties, childInputProperties, totalCost); + ownerGroup.setBestPlan(groupExpression, totalCost, outputProperties); + if (bestNodeCost == null || nodeCost.getValue() < bestNodeCost.getValue()) { + bestNodeCost = nodeCost; + } + } + restoreMissingExpressionCostState(groupExpression, originalLowestCostTable, originalRequestPropertiesMap); + if (bestNodeCost != null) { + groupExpression.setCost(bestNodeCost); + } else { + groupExpression.setCost(originalCost); + } + for (Map.Entry<PhysicalProperties, PhysicalProperties> entry + : originalRequestPropertiesMap.entrySet()) { + if (groupExpression.getLowestCostTable().containsKey(entry.getKey())) { + groupExpression.putOutputPropertiesMap(entry.getValue(), entry.getKey()); + } + } + } + + private void restoreMissingExpressionCostState(GroupExpression groupExpression, + Map<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> originalLowestCostTable, + Map<PhysicalProperties, PhysicalProperties> originalRequestPropertiesMap) { + for (Map.Entry<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> entry + : originalLowestCostTable.entrySet()) { + if (!groupExpression.getLowestCostTable().containsKey(entry.getKey())) { + groupExpression.updateLowestCostTable(entry.getKey(), entry.getValue().second, entry.getValue().first); + } + } + for (Map.Entry<PhysicalProperties, PhysicalProperties> entry : originalRequestPropertiesMap.entrySet()) { + if (groupExpression.getLowestCostTable().containsKey(entry.getKey())) { + groupExpression.putOutputPropertiesMap(entry.getValue(), entry.getKey()); + } + } + } + + private boolean hasAvailableChildBestPlan(GroupExpression groupExpression, + List<PhysicalProperties> childInputProperties) { + if (childInputProperties.size() != groupExpression.arity()) { + return false; + } + for (int i = 0; i < childInputProperties.size(); i++) { + if (!groupExpression.child(i) + .getLowestCostPlan(childInputProperties.get(i)).isPresent()) { + return false; + } + } + return true; + } + + private void syncLogicalExpressionRowCount(Group group, double rowCount) { + for (GroupExpression logicalExpression : group.getLogicalExpressions()) { + if (logicalExpression.getEstOutputRowCount() > 0 + || !Double.isFinite(logicalExpression.getEstOutputRowCount())) { + logicalExpression.setEstOutputRowCount(rowCount); + } + } + } + + private void refreshPhysicalExpressionRowCount(Group group, double rowCount) { + for (GroupExpression physicalExpression : group.getPhysicalExpressions()) { + physicalExpression.setEstOutputRowCount(getPhysicalExpressionRowCount(physicalExpression, rowCount)); + } + } + + private double getPhysicalExpressionRowCount(GroupExpression physicalExpression, double rowCount) { + if (physicalExpression.getPlan() instanceof PhysicalProject && physicalExpression.arity() == 1) { + Statistics childStatistics = physicalExpression.child(0).getStatistics(); + if (childStatistics != null && Double.isFinite(childStatistics.getRowCount()) + && childStatistics.getRowCount() >= 0) { + return childStatistics.getRowCount(); + } + } + return rowCount; + } + + private boolean shouldSyncLogicalExpressionRowCount() { + return logicalExpressionRowCountSyncPolicy + == LogicalExpressionRowCountSyncPolicy.SYNC_WITH_GROUP_ROW_COUNT; + } + + private Map<GroupExpression, Statistics> filterCandidateStatisticsByPolicy( + LogicalRowCountAggregationPolicy aggregationPolicy, + Map<GroupExpression, Statistics> candidateStatisticsByExpression) { + if (aggregationPolicy != LogicalRowCountAggregationPolicy.TRUST_JOIN_COUNT + || candidateStatisticsByExpression.size() < 2) { + return candidateStatisticsByExpression; + } + int maxTrustJoinCount = Integer.MIN_VALUE; + Map<GroupExpression, Statistics> selectedCandidateStatisticsByExpression = new LinkedHashMap<>(); + for (Map.Entry<GroupExpression, Statistics> entry : candidateStatisticsByExpression.entrySet()) { + int trustJoinCount = countTrustJoins(entry.getKey()); + if (trustJoinCount > maxTrustJoinCount) { + selectedCandidateStatisticsByExpression.clear(); + maxTrustJoinCount = trustJoinCount; + } + if (trustJoinCount == maxTrustJoinCount) { + selectedCandidateStatisticsByExpression.put(entry.getKey(), entry.getValue()); + } + } + return selectedCandidateStatisticsByExpression; + } + + int countTrustJoins(GroupExpression groupExpression) { + int trustJoinCount = isTrustJoin(groupExpression) ? 1 : 0; + for (Group child : groupExpression.children()) { + if (!child.getLogicalExpressions().isEmpty()) { + trustJoinCount += getGroupTrustJoinCount(child); + } + } + return trustJoinCount; + } + + int getGroupTrustJoinCount(Group group) { + Integer cached = groupTrustJoinCountCache.get(group); + if (cached != null) { + return cached; + } + // Placeholder to break recursion cycles through the memo group DAG. + groupTrustJoinCountCache.put(group, 0); + int maxCount = 0; + for (GroupExpression logicalExpression : group.getLogicalExpressions()) { + int count = countTrustJoins(logicalExpression); + maxCount = Math.max(maxCount, count); + } + groupTrustJoinCountCache.put(group, maxCount); + return maxCount; + } + + boolean isTrustJoin(GroupExpression groupExpression) { + if (groupExpression.arity() != 2 || !(groupExpression.getPlan() instanceof Join)) { + return false; + } + Statistics leftStats = groupExpression.child(0).getStatistics(); + Statistics rightStats = groupExpression.child(1).getStatistics(); + if (leftStats == null || rightStats == null) { + return false; + } + return JoinEstimation.hasTrustableEqualCondition(leftStats, rightStats, + (Join) groupExpression.getPlan()); + } + + private LogicalRowCountAggregationPolicy getLogicalRowCountAggregationPolicy() { + ConnectContext connectContext = cascadesContext == null ? null : cascadesContext.getConnectContext(); + if (connectContext == null || connectContext.getSessionVariable() == null) { + return LogicalRowCountAggregationPolicy.AVERAGE; + } + return LogicalRowCountAggregationPolicy.fromSessionValue( + connectContext.getSessionVariable().getMemoLogicalRowCountAggregationPolicy()); + } + + private void refreshEnforcerRowCount(Group group) { + Statistics statistics = group.getStatistics(); + if (statistics == null) { + return; + } + for (GroupExpression enforcer : group.getEnforcers().values()) { + enforcer.setEstOutputRowCount(statistics.getRowCount()); + } + } + + private void recordProducerStats(Group group, Statistics statistics) { + if (cascadesContext == null || statistics == null) { + return; + } + for (GroupExpression logicalExpression : group.getLogicalExpressions()) { + Plan plan = logicalExpression.getPlan(); + if (plan instanceof LogicalCTEProducer) { + cteIdToStats.put(((LogicalCTEProducer<?>) plan).getCteId(), new Statistics(statistics)); + } + } + for (GroupExpression physicalExpression : group.getPhysicalExpressions()) { + Plan plan = physicalExpression.getPlan(); + if (plan instanceof PhysicalCTEProducer) { + cteIdToStats.put(((PhysicalCTEProducer<?>) plan).getCteId(), new Statistics(statistics)); + } + } + } + + private Map<PhysicalProperties, Pair<Cost, GroupExpression>> snapshotLowestCostPlans(Group group) { + Map<PhysicalProperties, Pair<Cost, GroupExpression>> snapshot = new LinkedHashMap<>(); + for (PhysicalProperties properties : group.getAllProperties()) { + group.getLowestCostPlan(properties).ifPresent(plan -> snapshot.put(properties, plan)); + } + return snapshot; + } + + private void restoreMissingLowestCostPlans(Group group, + Map<PhysicalProperties, Pair<Cost, GroupExpression>> lowestCostPlans) { + for (Map.Entry<PhysicalProperties, Pair<Cost, GroupExpression>> entry : lowestCostPlans.entrySet()) { + if (!group.getLowestCostPlan(entry.getKey()).isPresent()) { + group.putBestPlan(entry.getValue().second, entry.getValue().first, entry.getKey()); + } + } + } + + private Statistics chooseRepresentativeStatistics(List<Statistics> candidateStatistics, + double aggregatedRowCount, Statistics originalStatistics) { + Statistics bestMatch = null; + double bestDistance = Double.POSITIVE_INFINITY; + for (Statistics candidate : candidateStatistics) { + double distance = Math.abs(candidate.getRowCount() - aggregatedRowCount); + if (distance < bestDistance) { + bestDistance = distance; + bestMatch = candidate; + } + } + if (bestMatch != null) { + return bestMatch; + } + if (originalStatistics != null) { + return new Statistics(originalStatistics); + } + return new Statistics(aggregatedRowCount, new HashMap<>()); + } + + private Statistics resolveUpdatedGroupStatistics(Group group, + Map<GroupExpression, Statistics> candidateStatisticsByExpression, + List<Statistics> candidateStatistics, double aggregatedRowCount, + Statistics originalStatistics) { + Statistics chosenProjectStatistics = resolveChosenProjectStatistics(group, candidateStatisticsByExpression, + aggregatedRowCount); + if (chosenProjectStatistics != null) { + return chosenProjectStatistics; + } + Statistics representativeStatistics = chooseRepresentativeStatistics( + candidateStatistics, aggregatedRowCount, originalStatistics); + return representativeStatistics.withRowCountAndEnforceValid(aggregatedRowCount); + } + + private Statistics resolveChosenProjectStatistics(Group group, + Map<GroupExpression, Statistics> candidateStatisticsByExpression, + double aggregatedRowCount) { + if (!shouldPreserveChosenProjectStatistics(group, candidateStatisticsByExpression, aggregatedRowCount)) { + return null; + } + Optional<Pair<Cost, GroupExpression>> lowestCostPlan = group.getLowestCostPlan(PhysicalProperties.ANY); + if (!lowestCostPlan.isPresent()) { + return null; + } + GroupExpression chosenPhysicalExpression = lowestCostPlan.get().second; + for (Map.Entry<GroupExpression, Statistics> entry : candidateStatisticsByExpression.entrySet()) { + if (entry.getKey().children().equals(chosenPhysicalExpression.children())) { + // The candidate map owns the only reference; returning directly avoids a deep copy. + return entry.getValue(); + } + } + return null; + } + + private boolean resolveUpdatedGroupStatsReliability(Group group, + Map<GroupExpression, Statistics> selectedCandidateStatisticsByExpression, + Map<GroupExpression, Boolean> candidateStatsReliableByExpression, + double aggregatedRowCount) { + // If resolveChosenProjectStatistics would return non-null, use that specific candidate's reliability + if (shouldPreserveChosenProjectStatistics(group, selectedCandidateStatisticsByExpression, aggregatedRowCount)) { + Optional<Pair<Cost, GroupExpression>> lowestCostPlan = group.getLowestCostPlan(PhysicalProperties.ANY); + if (lowestCostPlan.isPresent()) { + GroupExpression chosenPhysicalExpression = lowestCostPlan.get().second; + for (Map.Entry<GroupExpression, Statistics> entry + : selectedCandidateStatisticsByExpression.entrySet()) { + if (entry.getKey().children().equals(chosenPhysicalExpression.children())) { + Boolean reliable = candidateStatsReliableByExpression.get(entry.getKey()); + if (reliable != null) { + return reliable; + } + } + } + } + } + // Conservative: false if any selected candidate is unreliable + for (Map.Entry<GroupExpression, Boolean> entry : candidateStatsReliableByExpression.entrySet()) { + if (selectedCandidateStatisticsByExpression.containsKey(entry.getKey()) && !entry.getValue()) { + return false; + } + } + return true; + } + + private boolean resolvePhysicalOnlyGroupStatsReliability(Group group, + Map<GroupExpression, Statistics> candidateStatisticsByExpression, + Map<GroupExpression, Boolean> candidateStatsReliableByExpression, + Statistics originalStatistics) { + // Mirror the selection logic of choosePhysicalOnlyGroupStatistics: + // prefer the lowest-cost plan's expression, then closest to original row count, then first candidate + Optional<Pair<Cost, GroupExpression>> lowestCostPlan = group.getLowestCostPlan(PhysicalProperties.ANY); + if (lowestCostPlan.isPresent()) { + Boolean reliable = candidateStatsReliableByExpression.get(lowestCostPlan.get().second); + if (reliable != null) { + return reliable; + } + } + if (originalStatistics != null && Double.isFinite(originalStatistics.getRowCount())) { + double bestDistance = Double.POSITIVE_INFINITY; + GroupExpression bestExpression = null; + for (Map.Entry<GroupExpression, Statistics> entry : candidateStatisticsByExpression.entrySet()) { + double distance = Math.abs(entry.getValue().getRowCount() - originalStatistics.getRowCount()); + if (distance < bestDistance) { + bestDistance = distance; + bestExpression = entry.getKey(); + } + } + if (bestExpression != null) { + Boolean reliable = candidateStatsReliableByExpression.get(bestExpression); + return reliable != null ? reliable : false; + } + } + Boolean reliable = candidateStatsReliableByExpression.values().iterator().next(); + return reliable != null ? reliable : false; + } + + private boolean shouldPreserveChosenProjectStatistics(Group group, + Map<GroupExpression, Statistics> candidateStatisticsByExpression, + double aggregatedRowCount) { + if (candidateStatisticsByExpression.size() < 2 || !(aggregatedRowCount > 0)) { + return false; + } + if (!group.getLowestCostPlan(PhysicalProperties.ANY).isPresent()) { + return false; + } + GroupExpression chosenPhysicalExpression = group.getLowestCostPlan(PhysicalProperties.ANY).get().second; + if (!(chosenPhysicalExpression.getPlan() instanceof PhysicalProject)) { + return false; + } + for (GroupExpression logicalExpression : candidateStatisticsByExpression.keySet()) { + if (!(logicalExpression.getPlan() instanceof LogicalProject)) { + return false; + } + } + double minRowCount = Double.POSITIVE_INFINITY; + double maxRowCount = 0; + double chosenRowCount = Double.NaN; + for (Map.Entry<GroupExpression, Statistics> entry : candidateStatisticsByExpression.entrySet()) { + double rowCount = entry.getValue().getRowCount(); + if (!(rowCount > 0)) { + return false; + } + minRowCount = Math.min(minRowCount, rowCount); + maxRowCount = Math.max(maxRowCount, rowCount); + if (entry.getKey().children().equals(chosenPhysicalExpression.children())) { + chosenRowCount = rowCount; + } + } + if (!Double.isFinite(chosenRowCount) || chosenRowCount >= aggregatedRowCount) { + return false; + } + return maxRowCount / minRowCount >= CHOSEN_PROJECT_STATS_DIVERGENCE_RATIO_THRESHOLD; + } + + private List<GroupExpression> getEstimableLogicalExpressions(Group group) { + List<GroupExpression> logicalExpressions = group.getLogicalExpressions(); + if (logicalExpressions.isEmpty()) { + return Collections.emptyList(); + } + List<GroupExpression> estimableExpressions = new ArrayList<>(); + for (GroupExpression logicalExpression : logicalExpressions) { + if (hasEstimableLogicalExpressionRowCount(group, logicalExpression) + && hasCompleteChildStatistics(logicalExpression) + && hasAvailableCteStatistics(logicalExpression)) { + estimableExpressions.add(logicalExpression); + } + } + return estimableExpressions; + } + + private List<GroupExpression> getEstimablePhysicalExpressions(Group group) { + List<GroupExpression> estimableExpressions = new ArrayList<>(); + for (GroupExpression physicalExpression : group.getPhysicalExpressions()) { + if (physicalExpression.arity() > 0 + && !dependsOnOwnerGroupStatistics(group, physicalExpression) + && hasCompleteChildStatistics(physicalExpression) + && hasAvailableCteStatistics(physicalExpression)) { + estimableExpressions.add(physicalExpression); + } + } + for (GroupExpression enforcer : group.getEnforcers().values()) { + if (enforcer.arity() > 0 + && !dependsOnOwnerGroupStatistics(group, enforcer) + && hasCompleteChildStatistics(enforcer) + && hasAvailableCteStatistics(enforcer)) { + estimableExpressions.add(enforcer); + } + } + return estimableExpressions; + } + + private Statistics choosePhysicalOnlyGroupStatistics(Group group, + Map<GroupExpression, Statistics> candidateStatisticsByExpression, + Statistics originalStatistics) { + Optional<Pair<Cost, GroupExpression>> lowestCostPlan = group.getLowestCostPlan(PhysicalProperties.ANY); + if (lowestCostPlan.isPresent()) { + Statistics chosenStatistics = candidateStatisticsByExpression.get(lowestCostPlan.get().second); + if (chosenStatistics != null) { + return new Statistics(chosenStatistics); + } + } + if (originalStatistics != null && Double.isFinite(originalStatistics.getRowCount())) { + Statistics bestMatch = null; + double bestDistance = Double.POSITIVE_INFINITY; + for (Statistics candidateStatistics : candidateStatisticsByExpression.values()) { + double distance = Math.abs(candidateStatistics.getRowCount() - originalStatistics.getRowCount()); + if (distance < bestDistance) { + bestDistance = distance; + bestMatch = candidateStatistics; + } + } + if (bestMatch != null) { + return new Statistics(bestMatch); + } + } + return new Statistics(candidateStatisticsByExpression.values().iterator().next()); + } + + private void repairInvalidLogicalExpressionRowCounts(Group group, double rowCount) { + for (GroupExpression logicalExpression : group.getLogicalExpressions()) { + double expressionRowCount = logicalExpression.getEstOutputRowCount(); + if (!Double.isFinite(expressionRowCount) || expressionRowCount <= 0) { + logicalExpression.setEstOutputRowCount(rowCount); + } + } + } + + private boolean hasEstimableLogicalExpressionRowCount(Group group, GroupExpression logicalExpression) { + return logicalExpression.getEstOutputRowCount() > 0 + || (!shouldSyncLogicalExpressionRowCount() && group.getStatistics() != null); + } + + private boolean hasAvailableCteStatistics(GroupExpression groupExpression) { + Plan plan = groupExpression.getPlan(); + if (plan instanceof LogicalCTEConsumer) { + return cteIdToStats.containsKey(((LogicalCTEConsumer) plan).getCteId()); + } + if (plan instanceof PhysicalCTEConsumer) { + return cteIdToStats.containsKey(((PhysicalCTEConsumer) plan).getCteId()); + } + return true; + } + + private List<Group> getTraversalChildren(Group group) { + List<Group> children = Lists.newArrayList(); + addChildren(children, group.getLogicalExpressions(), group); + addChildren(children, group.getPhysicalExpressions(), group); + addChildren(children, group.getEnforcers().values(), group); + return children; + } + + private void addChildren(List<Group> children, Iterable<GroupExpression> groupExpressions, + Group ownerGroup) { + for (GroupExpression groupExpression : groupExpressions) { + for (Group child : groupExpression.children()) { + if (child != ownerGroup) { + children.add(child); + } + } + } + } + + private boolean hasCompleteChildStatistics(GroupExpression groupExpression) { + for (Group child : groupExpression.children()) { + if (child.getStatistics() == null) { + return false; + } + } + return true; + } + + private boolean dependsOnOwnerGroupStatistics(Group ownerGroup, GroupExpression groupExpression) { + for (Group child : groupExpression.children()) { + if (child == ownerGroup) { + return true; + } + } + return false; + } + + /** + * LogicalExpressionRowCountSyncPolicy + */ + public enum LogicalExpressionRowCountSyncPolicy { + SYNC_WITH_GROUP_ROW_COUNT, + KEEP_INDIVIDUAL_EXPRESSION_ROW_COUNT + } + + private enum LogicalRowCountAggregationPolicy { + AVERAGE { + @Override + double aggregate(List<Statistics> candidateStatistics) { + return candidateStatistics.stream() + .mapToDouble(Statistics::getRowCount) + .average() + .orElse(Double.NaN); + } + }, + MEDIAN { + @Override + double aggregate(List<Statistics> candidateStatistics) { + double[] rowCounts = candidateStatistics.stream() + .mapToDouble(Statistics::getRowCount) + .sorted() + .toArray(); + if (rowCounts.length == 0) { + return Double.NaN; + } + int middle = rowCounts.length / 2; + if ((rowCounts.length & 1) == 1) { + return rowCounts[middle]; + } + return (rowCounts[middle - 1] + rowCounts[middle]) / 2; + } + }, + MIN { + @Override + double aggregate(List<Statistics> candidateStatistics) { + return candidateStatistics.stream() + .mapToDouble(Statistics::getRowCount) + .min() + .orElse(Double.NaN); + } + }, + TRUST_JOIN_COUNT { + @Override + double aggregate(List<Statistics> candidateStatistics) { + return MEDIAN.aggregate(candidateStatistics); + } + }, + MAX { Review Comment: `MAX` is defined as an enum value in `LogicalRowCountAggregationPolicy` but it is not included in the session variable `options` list (SessionVariable.java line 2357) and `fromSessionValue()` does not handle `"max"`, which will throw an `IllegalArgumentException`. This appears to be unreachable dead code. Consider either adding `"max"` to the session variable options list, or removing the `MAX` enum value. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/MemoStatsAndCostRecomputer.java: ########## @@ -0,0 +1,839 @@ +// 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.common.Pair; +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.cost.Cost; +import org.apache.doris.nereids.cost.CostCalculator; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.properties.PhysicalProperties; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.Join; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEProducer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.statistics.Statistics; + +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Re-estimate memo logical row counts and rebuild physical costs. + * and rebuild physical cost state. + */ +public final class MemoStatsAndCostRecomputer { + private static final double CHOSEN_PROJECT_STATS_DIVERGENCE_RATIO_THRESHOLD = 1_000D; + protected final Map<Group, Integer> groupTrustJoinCountCache = new HashMap<>(); + private final CascadesContext cascadesContext; + private final Map<CTEId, Statistics> cteIdToStats = new HashMap<>(); + private final LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy; + + MemoStatsAndCostRecomputer(CascadesContext cascadesContext, + LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy) { + this.cascadesContext = cascadesContext; + this.logicalExpressionRowCountSyncPolicy = logicalExpressionRowCountSyncPolicy; + } + + /** + * recompute + */ + public static void recompute(Group rootGroup, PhysicalProperties physicalProperties, + CascadesContext cascadesContext) { + recompute(rootGroup, physicalProperties, cascadesContext, + LogicalExpressionRowCountSyncPolicy.KEEP_INDIVIDUAL_EXPRESSION_ROW_COUNT); + } + + /** + * recompute with configurable logical expression row count sync behavior. + */ + public static void recompute(Group rootGroup, PhysicalProperties physicalProperties, + CascadesContext cascadesContext, + LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy) { + MemoStatsAndCostRecomputer recomputer = new MemoStatsAndCostRecomputer(cascadesContext, + logicalExpressionRowCountSyncPolicy); + recomputer.seedProducerStats(rootGroup, new HashSet<>()); + recomputer.groupTrustJoinCountCache.clear(); + recomputer.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); + // Run a second pass so CTE consumers and their ancestors can settle on producer stats refreshed above. + // Skip when no CTEs exist — the bottom-up first pass already produces settled stats for non-CTE queries. + if (!recomputer.cteIdToStats.isEmpty()) { + recomputer.groupTrustJoinCountCache.clear(); + recomputer.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); + } + recomputer.recomputePhysicalCostsBottomUp(rootGroup, new HashSet<>()); + } + + private void seedProducerStats(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + Statistics statistics = group.getStatistics(); + if (statistics != null) { + recordProducerStats(group, statistics); + } + for (Group child : getTraversalChildren(group)) { + seedProducerStats(child, visited); + } + } + + private void reestimateLogicalStatsBottomUp(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + for (Group child : getTraversalChildren(group)) { + reestimateLogicalStatsBottomUp(child, visited); + } + reestimateCurrentGroup(group); + refreshEnforcerRowCount(group); + } + + private void reestimateCurrentGroup(Group group) { + List<GroupExpression> estimableExpressions = getEstimableLogicalExpressions(group); + if (estimableExpressions.isEmpty()) { + if (group.getLogicalExpressions().isEmpty()) { + reestimatePhysicalOnlyGroup(group); + } + return; + } + Statistics originalStatistics = group.getStatistics(); + boolean originalStatsReliable = group.isStatsReliable(); + Map<GroupExpression, Statistics> candidateStatisticsByExpression = new LinkedHashMap<>(); + Map<GroupExpression, Boolean> candidateStatsReliableByExpression = new LinkedHashMap<>(); + for (GroupExpression logicalExpression : estimableExpressions) { + group.setStatistics(null); + estimateStats(logicalExpression); + Statistics estimatedStatistics = group.getStatistics(); + if (estimatedStatistics == null || !isValidCandidateStatistics(estimatedStatistics)) { + continue; + } + logicalExpression.setEstOutputRowCount(estimatedStatistics.getRowCount()); + // Safe to store the reference: group.setStatistics(null) above releases the group's + // hold, and the next iteration will overwrite with a new object from estimateStats(). + candidateStatisticsByExpression.put(logicalExpression, estimatedStatistics); + candidateStatsReliableByExpression.put(logicalExpression, group.isStatsReliable()); + } + if (candidateStatisticsByExpression.isEmpty()) { + group.setStatistics(originalStatistics); + group.setStatsReliable(originalStatsReliable); + return; + } + LogicalRowCountAggregationPolicy aggregationPolicy = getLogicalRowCountAggregationPolicy(); + Map<GroupExpression, Statistics> selectedCandidateStatisticsByExpression = filterCandidateStatisticsByPolicy( + aggregationPolicy, candidateStatisticsByExpression); + List<Statistics> candidateStatistics = new ArrayList<>(selectedCandidateStatisticsByExpression.values()); + double aggregatedRowCount = aggregationPolicy.aggregate(candidateStatistics); + Statistics updatedStatistics = resolveUpdatedGroupStatistics(group, selectedCandidateStatisticsByExpression, + candidateStatistics, aggregatedRowCount, originalStatistics); + boolean resolvedStatsReliable = resolveUpdatedGroupStatsReliability(group, + selectedCandidateStatisticsByExpression, candidateStatsReliableByExpression, + aggregatedRowCount); + group.setStatsReliable(resolvedStatsReliable); + group.setStatistics(updatedStatistics); + repairInvalidLogicalExpressionRowCounts(group, aggregatedRowCount); + refreshPhysicalExpressionRowCount(group, updatedStatistics.getRowCount()); + recordProducerStats(group, updatedStatistics); + if (shouldSyncLogicalExpressionRowCount()) { + syncLogicalExpressionRowCount(group, updatedStatistics.getRowCount()); + } + } + + private void reestimatePhysicalOnlyGroup(Group group) { + List<GroupExpression> estimableExpressions = getEstimablePhysicalExpressions(group); + if (estimableExpressions.isEmpty()) { + return; + } + Statistics originalStatistics = group.getStatistics(); + boolean originalStatsReliable = group.isStatsReliable(); + Map<GroupExpression, Statistics> candidateStatisticsByExpression = new LinkedHashMap<>(); + Map<GroupExpression, Boolean> candidateStatsReliableByExpression = new LinkedHashMap<>(); + for (GroupExpression physicalExpression : estimableExpressions) { + group.setStatistics(null); + estimateStats(physicalExpression); + Statistics estimatedStatistics = group.getStatistics(); + if (estimatedStatistics == null || !isValidCandidateStatistics(estimatedStatistics)) { + continue; + } + physicalExpression.setEstOutputRowCount(estimatedStatistics.getRowCount()); + candidateStatisticsByExpression.put(physicalExpression, new Statistics(estimatedStatistics)); + candidateStatsReliableByExpression.put(physicalExpression, group.isStatsReliable()); + } + if (candidateStatisticsByExpression.isEmpty()) { + group.setStatistics(originalStatistics); + group.setStatsReliable(originalStatsReliable); + return; + } + Statistics updatedStatistics = choosePhysicalOnlyGroupStatistics(group, candidateStatisticsByExpression, + originalStatistics); + boolean resolvedStatsReliable = resolvePhysicalOnlyGroupStatsReliability(group, + candidateStatisticsByExpression, candidateStatsReliableByExpression, + originalStatistics); + group.setStatsReliable(resolvedStatsReliable); + group.setStatistics(updatedStatistics); + refreshPhysicalExpressionRowCount(group, updatedStatistics.getRowCount()); + recordProducerStats(group, updatedStatistics); + } + + private boolean isValidCandidateStatistics(Statistics statistics) { + return Double.isFinite(statistics.getRowCount()) && statistics.getRowCount() >= 0; + } + + private void estimateStats(GroupExpression groupExpression) { + ConnectContext connectContext = cascadesContext.getConnectContext(); + StatsCalculator statsCalculator = new StatsCalculator( + groupExpression, + connectContext.getSessionVariable().getForbidUnknownColStats(), + connectContext.getTotalColumnStatisticMap(), + connectContext.getSessionVariable().isPlayNereidsDump(), + cteIdToStats, + cascadesContext); + statsCalculator.estimate(); + } + + private void recomputePhysicalCostsBottomUp(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + for (Group child : getTraversalChildren(group)) { + recomputePhysicalCostsBottomUp(child, visited); + } + if (group.getStatistics() == null + || (group.getPhysicalExpressions().isEmpty() && group.getEnforcers().isEmpty())) { + refreshEnforcerRowCount(group); + return; + } + Map<PhysicalProperties, Pair<Cost, GroupExpression>> originalLowestCostPlans = + snapshotLowestCostPlans(group); + group.clearLowestCostPlans(); + for (GroupExpression physicalExpression : group.getPhysicalExpressions()) { + recomputeGroupExpressionCost(group, physicalExpression); + } + refreshEnforcerRowCount(group); + for (GroupExpression enforcer : group.getEnforcers().values()) { + recomputeGroupExpressionCost(group, enforcer); + } + restoreMissingLowestCostPlans(group, originalLowestCostPlans); + } + + private void recomputeGroupExpressionCost(Group ownerGroup, GroupExpression groupExpression) { + if (ownerGroup.getStatistics() == null || !hasCompleteChildStatistics(groupExpression)) { + return; + } + Cost originalCost = groupExpression.getCost(); + Map<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> originalLowestCostTable + = new LinkedHashMap<>(groupExpression.getLowestCostTable()); + Map<PhysicalProperties, PhysicalProperties> originalRequestPropertiesMap + = new LinkedHashMap<>(groupExpression.getRequestPropertiesMap()); + groupExpression.clearCostState(); + + Cost bestNodeCost = null; + for (Map.Entry<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> entry + : originalLowestCostTable.entrySet()) { + PhysicalProperties outputProperties = entry.getKey(); + List<PhysicalProperties> childInputProperties = entry.getValue().second; + if (!hasAvailableChildBestPlan(groupExpression, childInputProperties)) { + continue; + } + Cost nodeCost = CostCalculator.calculateCost(cascadesContext.getConnectContext(), + groupExpression, childInputProperties); + Cost totalCost = nodeCost; + for (int i = 0; i < childInputProperties.size(); i++) { + Optional<Pair<Cost, GroupExpression>> childBestPlan = groupExpression.child(i) + .getLowestCostPlan(childInputProperties.get(i)); + if (!childBestPlan.isPresent()) { + totalCost = null; + break; + } + totalCost = CostCalculator.addChildCost(cascadesContext.getConnectContext(), + groupExpression.getPlan(), totalCost, childBestPlan.get().first, i); + } + if (totalCost == null) { + continue; + } + groupExpression.updateLowestCostTable( + outputProperties, childInputProperties, totalCost); + ownerGroup.setBestPlan(groupExpression, totalCost, outputProperties); + if (bestNodeCost == null || nodeCost.getValue() < bestNodeCost.getValue()) { + bestNodeCost = nodeCost; + } + } + restoreMissingExpressionCostState(groupExpression, originalLowestCostTable, originalRequestPropertiesMap); + if (bestNodeCost != null) { + groupExpression.setCost(bestNodeCost); + } else { + groupExpression.setCost(originalCost); Review Comment: `originalCost` captured at line 252 via `groupExpression.getCost()` could be `null` if the cost was never previously set on this expression. If `bestNodeCost` is null and `originalCost` is also null, calling `groupExpression.setCost(null)` could cause an NPE downstream when the cost is later accessed. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/MemoStatsAndCostRecomputer.java: ########## @@ -0,0 +1,839 @@ +// 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.common.Pair; +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.cost.Cost; +import org.apache.doris.nereids.cost.CostCalculator; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.properties.PhysicalProperties; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.Join; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEProducer; +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.statistics.Statistics; + +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Re-estimate memo logical row counts and rebuild physical costs. + * and rebuild physical cost state. + */ +public final class MemoStatsAndCostRecomputer { + private static final double CHOSEN_PROJECT_STATS_DIVERGENCE_RATIO_THRESHOLD = 1_000D; + protected final Map<Group, Integer> groupTrustJoinCountCache = new HashMap<>(); + private final CascadesContext cascadesContext; + private final Map<CTEId, Statistics> cteIdToStats = new HashMap<>(); + private final LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy; + + MemoStatsAndCostRecomputer(CascadesContext cascadesContext, + LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy) { + this.cascadesContext = cascadesContext; + this.logicalExpressionRowCountSyncPolicy = logicalExpressionRowCountSyncPolicy; + } + + /** + * recompute + */ + public static void recompute(Group rootGroup, PhysicalProperties physicalProperties, + CascadesContext cascadesContext) { + recompute(rootGroup, physicalProperties, cascadesContext, + LogicalExpressionRowCountSyncPolicy.KEEP_INDIVIDUAL_EXPRESSION_ROW_COUNT); + } + + /** + * recompute with configurable logical expression row count sync behavior. + */ + public static void recompute(Group rootGroup, PhysicalProperties physicalProperties, + CascadesContext cascadesContext, + LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy) { + MemoStatsAndCostRecomputer recomputer = new MemoStatsAndCostRecomputer(cascadesContext, + logicalExpressionRowCountSyncPolicy); + recomputer.seedProducerStats(rootGroup, new HashSet<>()); + recomputer.groupTrustJoinCountCache.clear(); + recomputer.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); + // Run a second pass so CTE consumers and their ancestors can settle on producer stats refreshed above. + // Skip when no CTEs exist — the bottom-up first pass already produces settled stats for non-CTE queries. + if (!recomputer.cteIdToStats.isEmpty()) { + recomputer.groupTrustJoinCountCache.clear(); + recomputer.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); + } + recomputer.recomputePhysicalCostsBottomUp(rootGroup, new HashSet<>()); + } + + private void seedProducerStats(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + Statistics statistics = group.getStatistics(); + if (statistics != null) { + recordProducerStats(group, statistics); + } + for (Group child : getTraversalChildren(group)) { + seedProducerStats(child, visited); + } + } + + private void reestimateLogicalStatsBottomUp(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + for (Group child : getTraversalChildren(group)) { + reestimateLogicalStatsBottomUp(child, visited); + } + reestimateCurrentGroup(group); + refreshEnforcerRowCount(group); + } + + private void reestimateCurrentGroup(Group group) { + List<GroupExpression> estimableExpressions = getEstimableLogicalExpressions(group); + if (estimableExpressions.isEmpty()) { + if (group.getLogicalExpressions().isEmpty()) { + reestimatePhysicalOnlyGroup(group); + } + return; + } + Statistics originalStatistics = group.getStatistics(); + boolean originalStatsReliable = group.isStatsReliable(); + Map<GroupExpression, Statistics> candidateStatisticsByExpression = new LinkedHashMap<>(); + Map<GroupExpression, Boolean> candidateStatsReliableByExpression = new LinkedHashMap<>(); + for (GroupExpression logicalExpression : estimableExpressions) { + group.setStatistics(null); + estimateStats(logicalExpression); + Statistics estimatedStatistics = group.getStatistics(); + if (estimatedStatistics == null || !isValidCandidateStatistics(estimatedStatistics)) { + continue; + } + logicalExpression.setEstOutputRowCount(estimatedStatistics.getRowCount()); + // Safe to store the reference: group.setStatistics(null) above releases the group's + // hold, and the next iteration will overwrite with a new object from estimateStats(). + candidateStatisticsByExpression.put(logicalExpression, estimatedStatistics); + candidateStatsReliableByExpression.put(logicalExpression, group.isStatsReliable()); + } + if (candidateStatisticsByExpression.isEmpty()) { + group.setStatistics(originalStatistics); + group.setStatsReliable(originalStatsReliable); + return; + } + LogicalRowCountAggregationPolicy aggregationPolicy = getLogicalRowCountAggregationPolicy(); + Map<GroupExpression, Statistics> selectedCandidateStatisticsByExpression = filterCandidateStatisticsByPolicy( + aggregationPolicy, candidateStatisticsByExpression); + List<Statistics> candidateStatistics = new ArrayList<>(selectedCandidateStatisticsByExpression.values()); + double aggregatedRowCount = aggregationPolicy.aggregate(candidateStatistics); + Statistics updatedStatistics = resolveUpdatedGroupStatistics(group, selectedCandidateStatisticsByExpression, + candidateStatistics, aggregatedRowCount, originalStatistics); + boolean resolvedStatsReliable = resolveUpdatedGroupStatsReliability(group, + selectedCandidateStatisticsByExpression, candidateStatsReliableByExpression, + aggregatedRowCount); + group.setStatsReliable(resolvedStatsReliable); + group.setStatistics(updatedStatistics); + repairInvalidLogicalExpressionRowCounts(group, aggregatedRowCount); + refreshPhysicalExpressionRowCount(group, updatedStatistics.getRowCount()); + recordProducerStats(group, updatedStatistics); + if (shouldSyncLogicalExpressionRowCount()) { + syncLogicalExpressionRowCount(group, updatedStatistics.getRowCount()); + } + } + + private void reestimatePhysicalOnlyGroup(Group group) { + List<GroupExpression> estimableExpressions = getEstimablePhysicalExpressions(group); + if (estimableExpressions.isEmpty()) { + return; + } + Statistics originalStatistics = group.getStatistics(); + boolean originalStatsReliable = group.isStatsReliable(); + Map<GroupExpression, Statistics> candidateStatisticsByExpression = new LinkedHashMap<>(); + Map<GroupExpression, Boolean> candidateStatsReliableByExpression = new LinkedHashMap<>(); + for (GroupExpression physicalExpression : estimableExpressions) { + group.setStatistics(null); + estimateStats(physicalExpression); + Statistics estimatedStatistics = group.getStatistics(); + if (estimatedStatistics == null || !isValidCandidateStatistics(estimatedStatistics)) { + continue; + } + physicalExpression.setEstOutputRowCount(estimatedStatistics.getRowCount()); + candidateStatisticsByExpression.put(physicalExpression, new Statistics(estimatedStatistics)); + candidateStatsReliableByExpression.put(physicalExpression, group.isStatsReliable()); + } + if (candidateStatisticsByExpression.isEmpty()) { + group.setStatistics(originalStatistics); + group.setStatsReliable(originalStatsReliable); + return; + } + Statistics updatedStatistics = choosePhysicalOnlyGroupStatistics(group, candidateStatisticsByExpression, + originalStatistics); + boolean resolvedStatsReliable = resolvePhysicalOnlyGroupStatsReliability(group, + candidateStatisticsByExpression, candidateStatsReliableByExpression, + originalStatistics); + group.setStatsReliable(resolvedStatsReliable); + group.setStatistics(updatedStatistics); + refreshPhysicalExpressionRowCount(group, updatedStatistics.getRowCount()); + recordProducerStats(group, updatedStatistics); + } + + private boolean isValidCandidateStatistics(Statistics statistics) { + return Double.isFinite(statistics.getRowCount()) && statistics.getRowCount() >= 0; + } + + private void estimateStats(GroupExpression groupExpression) { + ConnectContext connectContext = cascadesContext.getConnectContext(); + StatsCalculator statsCalculator = new StatsCalculator( + groupExpression, + connectContext.getSessionVariable().getForbidUnknownColStats(), + connectContext.getTotalColumnStatisticMap(), + connectContext.getSessionVariable().isPlayNereidsDump(), + cteIdToStats, + cascadesContext); + statsCalculator.estimate(); + } + + private void recomputePhysicalCostsBottomUp(Group group, Set<Group> visited) { + if (!visited.add(group)) { + return; + } + for (Group child : getTraversalChildren(group)) { + recomputePhysicalCostsBottomUp(child, visited); + } + if (group.getStatistics() == null + || (group.getPhysicalExpressions().isEmpty() && group.getEnforcers().isEmpty())) { + refreshEnforcerRowCount(group); + return; + } + Map<PhysicalProperties, Pair<Cost, GroupExpression>> originalLowestCostPlans = + snapshotLowestCostPlans(group); + group.clearLowestCostPlans(); + for (GroupExpression physicalExpression : group.getPhysicalExpressions()) { + recomputeGroupExpressionCost(group, physicalExpression); + } + refreshEnforcerRowCount(group); + for (GroupExpression enforcer : group.getEnforcers().values()) { + recomputeGroupExpressionCost(group, enforcer); + } + restoreMissingLowestCostPlans(group, originalLowestCostPlans); + } + + private void recomputeGroupExpressionCost(Group ownerGroup, GroupExpression groupExpression) { + if (ownerGroup.getStatistics() == null || !hasCompleteChildStatistics(groupExpression)) { + return; + } + Cost originalCost = groupExpression.getCost(); + Map<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> originalLowestCostTable + = new LinkedHashMap<>(groupExpression.getLowestCostTable()); + Map<PhysicalProperties, PhysicalProperties> originalRequestPropertiesMap + = new LinkedHashMap<>(groupExpression.getRequestPropertiesMap()); + groupExpression.clearCostState(); + + Cost bestNodeCost = null; + for (Map.Entry<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> entry + : originalLowestCostTable.entrySet()) { + PhysicalProperties outputProperties = entry.getKey(); + List<PhysicalProperties> childInputProperties = entry.getValue().second; + if (!hasAvailableChildBestPlan(groupExpression, childInputProperties)) { + continue; + } + Cost nodeCost = CostCalculator.calculateCost(cascadesContext.getConnectContext(), + groupExpression, childInputProperties); + Cost totalCost = nodeCost; + for (int i = 0; i < childInputProperties.size(); i++) { + Optional<Pair<Cost, GroupExpression>> childBestPlan = groupExpression.child(i) + .getLowestCostPlan(childInputProperties.get(i)); + if (!childBestPlan.isPresent()) { + totalCost = null; + break; + } + totalCost = CostCalculator.addChildCost(cascadesContext.getConnectContext(), + groupExpression.getPlan(), totalCost, childBestPlan.get().first, i); + } + if (totalCost == null) { + continue; + } + groupExpression.updateLowestCostTable( + outputProperties, childInputProperties, totalCost); + ownerGroup.setBestPlan(groupExpression, totalCost, outputProperties); + if (bestNodeCost == null || nodeCost.getValue() < bestNodeCost.getValue()) { + bestNodeCost = nodeCost; + } + } + restoreMissingExpressionCostState(groupExpression, originalLowestCostTable, originalRequestPropertiesMap); + if (bestNodeCost != null) { + groupExpression.setCost(bestNodeCost); + } else { + groupExpression.setCost(originalCost); + } + for (Map.Entry<PhysicalProperties, PhysicalProperties> entry + : originalRequestPropertiesMap.entrySet()) { Review Comment: The `putOutputPropertiesMap` restoration logic on lines 297-302 is redundant with the same logic inside `restoreMissingExpressionCostState()` (lines 314-318), which is called at line 290. Both loops iterate over `originalRequestPropertiesMap` and call `groupExpression.putOutputPropertiesMap(entry.getValue(), entry.getKey())` when `getLowestCostTable().containsKey(entry.getKey())`. Consider removing the duplicate loop at lines 297-302. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/OptimizeGroupExpressionJob.java: ########## @@ -93,9 +93,7 @@ private List<Rule> getExplorationRules(CascadesContext cascadesContext) { private List<Rule> getJoinRules() { boolean isDisableJoinReorder = context.getCascadesContext().getConnectContext().getSessionVariable() .isDisableJoinReorder() - || context.getCascadesContext().isLeadingDisableJoinReorder() - || context.getCascadesContext().getMemo().getGroupExpressionsSize() > context.getCascadesContext() - .getConnectContext().getSessionVariable().memoMaxGroupExpressionSize; + || context.getCascadesContext().isLeadingDisableJoinReorder(); Review Comment: The memo size guard condition `|| context.getCascadesContext().getMemo().getGroupExpressionsSize() > ...memoMaxGroupExpressionSize` has been removed from the join reorder disable check. This means join reorder will no longer be automatically disabled when the memo grows beyond `memoMaxGroupExpressionSize` (default 10000). Combined with the `dphyperLimit` increase from 1000 to 2600 (SessionVariable.java line 2369), this could lead to significantly longer optimization times for very complex queries. Is this intentional? The `memoMaxGroupExpressionSize` variable is still defined as a session variable but is no longer used to guard join reorder. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/memo/GroupExpression.java: ########## @@ -63,6 +63,10 @@ public class GroupExpression { private double estOutputRowCount = -1; + private Statistics statistics; Review Comment: The new fields `statistics` (line 66) and `isStatsReliable` (line 68) appear to be unused in this PR — they are declared but never read or written anywhere in the changeset. Are these intended for a follow-up change? If not, consider removing them to avoid dead code. ########## fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java: ########## @@ -2341,11 +2352,21 @@ public boolean isEnableHboNonStrictMatchingMode() { @VarAttrDef.VarAttr(name = DUMP_NEREIDS_MEMO) public boolean dumpNereidsMemo = false; + @VarAttrDef.VarAttr(name = MEMO_LOGICAL_ROW_COUNT_AGGREGATION_POLICY, needForward = true, + checker = "checkMemoLogicalRowCountAggregationPolicy", setter = "setMemoLogicalRowCountAggregationPolicy", + options = {"trust_join_count", "average", "median", "min" }, description = { Review Comment: The `options` list includes `{"trust_join_count", "average", "median", "min"}` but is missing `"max"`, even though the `LogicalRowCountAggregationPolicy` enum in `MemoStatsAndCostRecomputer.java` defines a `MAX` value (line 809). Either add `"max"` here and in the `checkMemoLogicalRowCountAggregationPolicy` switch statement, or remove the `MAX` enum value to keep things consistent. ########## fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java: ########## @@ -2341,11 +2352,21 @@ public boolean isEnableHboNonStrictMatchingMode() { @VarAttrDef.VarAttr(name = DUMP_NEREIDS_MEMO) public boolean dumpNereidsMemo = false; + @VarAttrDef.VarAttr(name = MEMO_LOGICAL_ROW_COUNT_AGGREGATION_POLICY, needForward = true, + checker = "checkMemoLogicalRowCountAggregationPolicy", setter = "setMemoLogicalRowCountAggregationPolicy", + options = {"trust_join_count", "average", "median", "min" }, description = { + "控制 MemoStatsAndCostRecomputer 在多个逻辑候选统计之间如何聚合 group row count。" + + "支持 trust_join_count, average、median、min。", + "Controls how MemoStatsAndCostRecomputer aggregates group row count across multiple logical " + + "statistics candidates. Supported values: trust_join_count, average, median, min." }, + affectQueryResultInPlan = true) + public String memoLogicalRowCountAggregationPolicy = "median"; + @VarAttrDef.VarAttr(name = "memo_max_group_expression_size") public int memoMaxGroupExpressionSize = 10000; @VarAttrDef.VarAttr(name = DPHYPER_LIMIT) - public int dphyperLimit = 1000; + public int dphyperLimit = 2600; Review Comment: `dphyperLimit` is increased from 1000 → 2600 (2.6x). This enables better plan exploration for complex queries but may also significantly increase optimization time. Was there performance testing (e.g., TPC-DS benchmarks) to confirm this doesn't cause regressions on common workloads? Consider adding a brief comment documenting the rationale for the new default. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
