github-actions[bot] commented on code in PR #64559: URL: https://github.com/apache/doris/pull/64559#discussion_r3472358977
########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/MemoStatsAndCostRecomputer.java: ########## @@ -0,0 +1,849 @@ +// 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; + private final CascadesContext cascadesContext; + private final Map<CTEId, Statistics> cteIdToStats = new HashMap<>(); + private final Map<Group, Integer> groupTrustJoinCountCache = 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.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); + // Run a second pass so CTE consumers and their ancestors can settle on producer stats refreshed above. + recomputer.reestimateLogicalStatsBottomUp(rootGroup, new HashSet<>()); Review Comment: The `TRUST_JOIN_COUNT` policy can still score parent candidates with stale child trust counts. `recompute()` intentionally runs `reestimateLogicalStatsBottomUp()` twice because CTE consumers and their ancestors may get different producer stats after the first pass, but `groupTrustJoinCountCache` is kept for the whole recompute. Once `getGroupTrustJoinCount(child)` caches a score during the first pass, the second pass reuses that score even if the child's statistics changed, while `isTrustJoin()` is defined from the current child row counts/NDVs. A CTE-backed child join that becomes trustable only after refreshed producer stats are recorded will still contribute its old count to parent candidates, so `filterCandidateStatisticsByPolicy(TRUST_JOIN_COUNT, ...)` can keep or discard candidates based on first-pass stats rather than the settled stats. Please clear this cache before each logical re-estimation pass, or key/invalidate it by the statistics version used for scoring. -- 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]
