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


##########
fe/fe-core/src/main/java/org/apache/doris/statistics/BaseStatsDerive.java:
##########
@@ -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.
+
+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.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();
+        capRowCountAtLimit();
+        return rowCount;
+    }
+
+
+    protected HashMap<Long, Long> deriveColumnToDataSize() {
+        HashMap<Long, Long> columnToDataSize = new HashMap<>();

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatsDeriveResult.java:
##########
@@ -0,0 +1,57 @@
+// 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.collect.Maps;
+
+import java.util.Map;
+
+// This structure is maintained in each operator to store the statistical 
information results obtained by the operator.
+public class StatsDeriveResult {
+    private long rowCount = -1;
+    // The data size of the corresponding column in the operator
+    // The actual key is slotId
+    private final Map<Long, Long> columnToDataSize = Maps.newHashMap();

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/OlapScanStatsDerive.java:
##########
@@ -0,0 +1,107 @@
+// 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 org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.catalog.Catalog;
+import org.apache.doris.common.UserException;
+import org.apache.doris.planner.OlapScanNode;
+import org.apache.doris.planner.PlanNode;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class OlapScanStatsDerive extends BaseStatsDerive {
+    // Currently, due to the structure of doris,
+    // the selected materialized view is not determined when calculating the 
statistical information of scan,
+    // so baseIndex is used for calculation when generating Planner.
+
+    // The rowCount here is the number of rows.
+    private long inputRowCount = -1;
+    private Map<Long, Long> slotIdToDataSize;
+    private Map<Long, Long> slotIdToNdv;
+
+    @Override
+    public void init(PlanNode node) throws UserException {
+        Preconditions.checkState(node instanceof OlapScanNode);
+        super.init(node);
+        buildColumnToStats((OlapScanNode)node);
+    }
+
+    @Override
+    public StatsDeriveResult deriveStats() {
+        /**
+         * Compute InAccurate cardinality before mv selector and tablet 
pruning.
+         * - Accurate statistical information relies on the selector of 
materialized views and bucket reduction.
+         * - However, Those both processes occur after the reorder algorithm 
is completed.
+         * - When Join reorder is turned on, the cardinality must be 
calculated before the reorder algorithm.
+         * - So only an inaccurate cardinality can be calculated here.
+         */
+        rowCount = inputRowCount;
+        return super.deriveStats();
+    }
+
+    public void buildColumnToStats(OlapScanNode node) {

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to