github-actions[bot] commented on code in PR #64559: URL: https://github.com/apache/doris/pull/64559#discussion_r3434095962
########## regression-test/suites/shape_check/tpcds_sf1000/dphyper/query10.groovy: ########## @@ -0,0 +1,157 @@ +/* + * 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. + */ + +suite("query10") { + String db = context.config.getDbNameByFile(new File(context.file.parent)) + if (isCloudMode()) { + return + } + sql "use ${db}" + sql 'set enable_nereids_planner=true' + sql 'set enable_nereids_distribute_planner=false' + sql 'set enable_fallback_to_original_planner=false' + sql 'set exec_mem_limit=21G' + sql 'set be_number_for_test=3' + sql 'set parallel_pipeline_task_num=8; ' + sql 'set forbid_unknown_col_stats=true' + sql 'set enable_nereids_timeout = false' + sql 'set enable_runtime_filter_prune=false' + sql 'set runtime_filter_type=8' + sql 'set dump_nereids_memo=false' + sql "set disable_nereids_rules=PRUNE_EMPTY_PARTITION" + sql "SET enable_dphyp_optimizer = true" + sql "set max_table_count_use_cascades_join_reorder = 1" + sql "set dphyper_limit=2600" + + def ds = """select Review Comment: This added line has trailing whitespace, and the same whitespace issue appears throughout many of the new TPC-DS/TPC-H DPHyp shape suites plus blank lines at EOF in the generated `.out` files. Please regenerate or format these files without whitespace errors before merge so the PR satisfies the repository style gate and does not leave noisy generated diffs. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/MemoStatsAndCostRecomputer.java: ########## @@ -0,0 +1,836 @@ +// 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 LogicalExpressionRowCountSyncPolicy logicalExpressionRowCountSyncPolicy; + + private 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<>()); + 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) { + List<Statistics> originalChildStatistics = replaceChildStatisticsForLogicalEstimation(logicalExpression); + group.setStatistics(null); + try { + estimateStats(logicalExpression); + } finally { + restoreChildStatistics(logicalExpression, originalChildStatistics); + } + Statistics estimatedStatistics = group.getStatistics(); + if (estimatedStatistics == null || !isValidCandidateStatistics(estimatedStatistics)) { + continue; + } + logicalExpression.setEstOutputRowCount(estimatedStatistics.getRowCount()); + candidateStatisticsByExpression.put(logicalExpression, new Statistics(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( Review Comment: This recompute path bypasses HBO statistics even when `enable_hbo_optimization` is enabled. Normal `DeriveStatsJob` selects `HboStatsCalculator` for that session setting, and `HboStatsCalculator` replaces filter/join/aggregate row counts from matched historical plan stats. In a DPHyp query with matched HBO stats, DPHyp initially derives HBO-adjusted row counts, but this method clears group statistics and re-estimates with plain `StatsCalculator` before `copyOutBestLogicalPlan()`, so the copied plan is chosen using legacy estimates instead of the effective session behavior. Please use the same calculator selection as `DeriveStatsJob` here and preserve the HBO-adjusted row counts/flags before rebuilding costs. -- 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]
