EmmyMiao87 commented on code in PR #8947:
URL: https://github.com/apache/incubator-doris/pull/8947#discussion_r855940321


##########
fe/fe-core/src/main/java/org/apache/doris/statistics/BaseStatsDerive.java:
##########
@@ -0,0 +1,158 @@
+// 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.statistics;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.SlotId;
+import org.apache.doris.common.UserException;
+import org.apache.doris.planner.PlanNode;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+
+public class BaseStatsDerive {
+    private static final Logger LOG = 
LogManager.getLogger(BaseStatsDerive.class);
+    // estimate of the output rowCount of this node;
+    // invalid: -1
+    protected long rowCount = -1;
+    protected long limit = -1;
+
+    protected List<Expr> conjuncts = Lists.newArrayList();
+    protected List<StatsDeriveResult> childrenStatsResult = 
Lists.newArrayList();
+
+    protected void init(PlanNode node) throws UserException {
+        limit = node.getLimit();
+        conjuncts.addAll(node.getConjuncts());
+
+        for (PlanNode childNode : node.getChildren()) {
+            StatsDeriveResult result = childNode.getStatsDeriveResult();
+            if (result == null) {
+                throw new UserException("childNode statsDeriveResult is null, 
childNodeType is " + childNode.getNodeType()
+                + "parentNodeType is " + node.getNodeType());
+            }
+            childrenStatsResult.add(result);
+        }
+    }
+
+    public StatsDeriveResult deriveStats() {
+        return new StatsDeriveResult(deriveRowCount(), 
deriveColumnToDataSize(), deriveColumnToNdv());
+    }
+
+    public boolean hasLimit() {
+        return limit > -1;
+    }
+
+    protected void applyConjunctsSelectivity() {
+        if (rowCount == -1) {
+            return;
+        }
+        applySelectivity();
+    }
+
+    private void applySelectivity() {
+        double selectivity = computeSelectivity();
+        Preconditions.checkState(rowCount >= 0);
+        long preConjunctrowCount = rowCount;
+        rowCount = Math.round(rowCount * selectivity);
+        // don't round rowCount down to zero for safety.
+        if (rowCount == 0 && preConjunctrowCount > 0) {
+            rowCount = 1;
+        }
+    }
+
+    protected double computeSelectivity() {
+        for (Expr expr : conjuncts) {
+            expr.setSelectivity();
+        }
+        return computeCombinedSelectivity(conjuncts);
+    }
+
+    /**
+     * Returns the estimated combined selectivity of all conjuncts. Uses 
heuristics to
+     * address the following estimation challenges:
+     * 1. The individual selectivities of conjuncts may be unknown.
+     * 2. Two selectivities, whether known or unknown, could be correlated. 
Assuming
+     * independence can lead to significant underestimation.
+     * <p>
+     * The first issue is addressed by using a single default selectivity that 
is
+     * representative of all conjuncts with unknown selectivities.
+     * The second issue is addressed by an exponential backoff when 
multiplying each
+     * additional selectivity into the final result.
+     */
+    protected double computeCombinedSelectivity(List<Expr> conjuncts) {
+        // Collect all estimated selectivities.
+        List<Double> selectivities = new ArrayList<>();
+        for (Expr e : conjuncts) {
+            if (e.hasSelectivity()) selectivities.add(e.getSelectivity());
+        }
+        if (selectivities.size() != conjuncts.size()) {
+            // Some conjuncts have no estimated selectivity. Use a single 
default
+            // representative selectivity for all those conjuncts.
+            selectivities.add(Expr.DEFAULT_SELECTIVITY);
+        }
+        // Sort the selectivities to get a consistent estimate, regardless of 
the original
+        // conjunct order. Sort in ascending order such that the most 
selective conjunct
+        // is fully applied.
+        Collections.sort(selectivities);
+        double result = 1.0;
+        // selectivity = 1 * (s1)^(1/1) * (s2)^(1/2) * ... * (sn-1)^(1/(n-1)) 
* (sn)^(1/n)
+        for (int i = 0; i < selectivities.size(); ++i) {
+            // Exponential backoff for each selectivity multiplied into the 
final result.
+            result *= Math.pow(selectivities.get(i), 1.0 / (double) (i + 1));
+        }
+        // Bound result in [0, 1]
+        return Math.max(0.0, Math.min(1.0, result));
+    }
+
+    protected void capRowCountAtLimit() {
+        if (hasLimit()) {
+            rowCount = rowCount == -1 ? limit : Math.min(rowCount, limit);
+        }
+    }
+
+
+    // Currently it simply adds the number of rows of children
+    protected long deriveRowCount() {
+        applyConjunctsSelectivity();

Review Comment:
   ```suggestion
           rowcount = children max(rowcount)
           applyConjunctsSelectivity();
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java:
##########
@@ -386,6 +403,18 @@ public void computeStats(Analyzer analyzer) {
         }
         // when node scan has no data, cardinality should be 0 instead of a 
invalid value after computeStats()
         cardinality = cardinality == -1 ? 0 : cardinality;
+
+        // update statsDeriveResult for real statistics
+        // After statistics collection is complete, remove the logic
+        if (analyzer.safeIsEnableJoinReorderBasedCost()) {
+            statsDeriveResult.setRowCount(cardinality);

Review Comment:
   Only keep this line



##########
fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java:
##########
@@ -386,6 +403,18 @@ public void computeStats(Analyzer analyzer) {
         }
         // when node scan has no data, cardinality should be 0 instead of a 
invalid value after computeStats()
         cardinality = cardinality == -1 ? 0 : cardinality;
+
+        // update statsDeriveResult for real statistics
+        // After statistics collection is complete, remove the logic
+        if (analyzer.safeIsEnableJoinReorderBasedCost()) {
+            statsDeriveResult.setRowCount(cardinality);
+            for (Map.Entry<SlotId, Long> entry : 
statsDeriveResult.getColumnToNdv().entrySet()) {

Review Comment:
   remove from 411 ~ 416 



-- 
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]

Reply via email to