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


##########
fe/fe-core/src/main/java/org/apache/doris/statistics/BaseStatsDerive.java:
##########
@@ -0,0 +1,122 @@
+// 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.planner.PlanNode;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+public abstract class BaseStatsDerive {
+    // estimate of the output cardinality of this node;
+    // invalid: -1
+    protected long cardinality = -1;
+    protected long limit = -1;
+
+    protected List<Expr> conjuncts = Lists.newArrayList();
+    protected List<Optional<StatsDeriveResult>> childrenStatsResult = 
Lists.newArrayList();
+
+    protected BaseStatsDerive init(PlanNode node) {
+        limit = node.getLimit();
+        conjuncts.addAll(node.getConjuncts());
+
+        for (PlanNode childNode : node.getChildren()) {
+            childrenStatsResult.add(childNode.getStatsDeriveResult());
+        }
+        return this;
+    }
+
+    public abstract StatsDeriveResult deriveStats();

Review Comment:
   For those nodes that do not implement their own custom derivation. I think 
you can have a very general calculation put in this function.
   for example
   ````
   applySelectivity();
   applyLimit();
   ````



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/BaseStatsDerive.java:
##########
@@ -0,0 +1,122 @@
+// 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.planner.PlanNode;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+public abstract class BaseStatsDerive {
+    // estimate of the output cardinality of this node;
+    // invalid: -1
+    protected long cardinality = -1;
+    protected long limit = -1;
+
+    protected List<Expr> conjuncts = Lists.newArrayList();
+    protected List<Optional<StatsDeriveResult>> childrenStatsResult = 
Lists.newArrayList();

Review Comment:
   ```suggestion
       protected List<StatsDeriveResult> childrenStatsResult = 
Lists.newArrayList();
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/DeriveFactory.java:
##########
@@ -0,0 +1,41 @@
+// 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 org.apache.doris.planner.PlanNode;
+
+public class DeriveFactory {
+
+    public BaseStatsDerive getStatsDerive(PlanNode.NodeType nodeType) {
+        switch (nodeType) {
+            case AGG_NODE:
+            case HASH_JOIN_NODE:
+            case MERGE_NODE:
+                break;
+            case OLAP_SCAN_NODE:
+                return new ScanStatsDerive();
+            case DEFAULT:
+        }
+        return new BaseStatsDerive() {

Review Comment:
   Implement generic deriveStats() instead of here



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatsRecursiveDerive.java:
##########
@@ -0,0 +1,52 @@
+// 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 org.apache.doris.planner.PlanNode;
+
+
+public class StatsRecursiveDerive {
+    private StatsRecursiveDerive() {}
+
+    public static StatsRecursiveDerive getStatsRecursiveDerive() {
+        return Inner.INSTANCE;
+    }
+
+    private static class Inner {
+        private static final StatsRecursiveDerive INSTANCE = new 
StatsRecursiveDerive();
+    }
+
+    /**
+     * Recursively complete the derivation of statistics for this node and all 
its children
+     * @param node
+     * This parameter is an input and output parameter,
+     * which will store the derivation result of statistical information in 
the corresponding node
+     */
+    public void statsRecursiveDerive(PlanNode node) {
+        if (node.getStatsDeriveResult().get().isStatsDerived()) {
+            return;
+        }
+        for (PlanNode childNode : node.getChildren()) {
+            if (!childNode.getStatsDeriveResult().get().isStatsDerived()) {
+                statsRecursiveDerive(childNode);
+            }
+        }
+        DeriveFactory deriveFactory = new DeriveFactory();
+        
node.setStatsDeriveResult(deriveFactory.getStatsDerive(node.getNodeType()).init(node).deriveStats());

Review Comment:
   ```suggestion
           BaseDeriveStats deriveStats = 
deriveFactory.getStatsDerive(node.getNodeType());
           deriveStats.init(node);
           StatsDeriveResult result = deriveStats.deriveStats();
           node.setStatsDeriveResult(result);
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatsDeriveResult.java:
##########
@@ -0,0 +1,71 @@
+// 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 cardinality = -1;
+    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();
+    // The ndv of the corresponding column in the operator
+    // The actual key is slotId
+    private final Map<Long, Long> columnToNdv = Maps.newHashMap();
+
+    public StatsDeriveResult() {}
+
+    public StatsDeriveResult(long cardinality, long rowCount, Map<Long, Long> 
columnToDataSize, Map<Long, Long> columnToNdv) {
+        this.cardinality = cardinality;
+        this.rowCount = rowCount;
+        this.columnToDataSize.putAll(columnToDataSize);
+        this.columnToNdv.putAll(columnToNdv);
+    }
+
+    public void setRowCount(long rowCount) {
+        this.rowCount = rowCount;
+    }
+
+    public void setCardinality(long cardinality) {
+        this.cardinality = cardinality;
+    }
+
+    public boolean isStatsDerived() {

Review Comment:
   You can directly determine whether the Plan Node has derived statistics by 
whether there is a Result.



##########
fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java:
##########
@@ -133,7 +134,7 @@ abstract public class PlanNode extends TreeNode<PlanNode> {
 
     protected List<SlotId> outputSlotIds;
 
-    private NodeType nodeType = NodeType.DEFAULT;
+    protected NodeType nodeType = NodeType.DEFAULT;
     protected StatsDeriveResult statsDeriveResult = new StatsDeriveResult();

Review Comment:
   Why does it affect UT?



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/ScanStatsDerive.java:
##########
@@ -0,0 +1,94 @@
+// 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.planner.OlapScanNode;
+import org.apache.doris.planner.PlanNode;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ScanStatsDerive 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 rowCount = -1;
+    private Map<Long, Long> slotIdToDataSize;
+    private Map<Long, Long> slotIdToNdv;
+
+    @Override
+    public ScanStatsDerive init(PlanNode node) {
+        Preconditions.checkState(node instanceof OlapScanNode);
+        super.init(node);
+        buildColumnToStats((OlapScanNode)node);
+        return this;
+    }
+
+    @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.
+         */
+        cardinality = rowCount;
+        applyConjunctsSelectivity();
+        capCardinalityAtLimit();
+        return new StatsDeriveResult(cardinality, rowCount, slotIdToDataSize, 
slotIdToNdv);
+    }
+
+    public void buildColumnToStats(OlapScanNode node) {
+        slotIdToDataSize = new HashMap<>();
+        slotIdToNdv = new HashMap<>();
+        if (node.getTupleDesc() != null
+            && node.getTupleDesc().getTable() != null) {
+            long tableId = node.getTupleDesc().getTable().getId();
+            rowCount = Catalog.getCurrentCatalog().getStatisticsManager()
+                    .getStatistics().getTableStats(tableId).getRowCount();
+        }
+        for (SlotDescriptor slot : node.getTupleDesc().getSlots()) {
+            if (slot.getParent() != null
+                    && slot.getParent().getTable() != null
+                    && slot.getColumn() != null) {
+                long tableId = slot.getParent().getTable().getId();
+                String columnName = slot.getColumn().getName();
+                /*TODO:Implement the getStatistics interface

Review Comment:
   It is best to separate the steps of initializing the member variables of the 
class and obtaining statistics.



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/ScanStatsDerive.java:
##########
@@ -0,0 +1,94 @@
+// 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.planner.OlapScanNode;
+import org.apache.doris.planner.PlanNode;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ScanStatsDerive extends BaseStatsDerive {

Review Comment:
   ```suggestion
   public class OlapScanStatsDerive extends BaseStatsDerive {
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatsDeriveResult.java:
##########
@@ -0,0 +1,71 @@
+// 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 cardinality = -1;
+    private long rowCount = -1;

Review Comment:
   What the different between rowCount and cardinality ?



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatsRecursiveDerive.java:
##########
@@ -0,0 +1,52 @@
+// 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 org.apache.doris.planner.PlanNode;
+
+
+public class StatsRecursiveDerive {
+    private StatsRecursiveDerive() {}
+
+    public static StatsRecursiveDerive getStatsRecursiveDerive() {
+        return Inner.INSTANCE;
+    }
+
+    private static class Inner {
+        private static final StatsRecursiveDerive INSTANCE = new 
StatsRecursiveDerive();
+    }
+
+    /**
+     * Recursively complete the derivation of statistics for this node and all 
its children
+     * @param node
+     * This parameter is an input and output parameter,
+     * which will store the derivation result of statistical information in 
the corresponding node
+     */
+    public void statsRecursiveDerive(PlanNode node) {
+        if (node.getStatsDeriveResult().get().isStatsDerived()) {

Review Comment:
   You can directly determine whether the Plan Node has derived statistics by 
whether there is a Result.



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/Statistics.java:
##########
@@ -70,4 +70,16 @@ public Map<String, ColumnStats> getColumnStats(long tableId) 
{
         }
         return tableStats.getNameToColumnStats();
     }
+

Review Comment:
   And comment ```TODO: mock statistics need to be removed in the future ```



##########
fe/fe-core/src/main/java/org/apache/doris/planner/BrokerScanNode.java:
##########
@@ -137,8 +137,9 @@ private static class ParamCreateContext {
     private List<ParamCreateContext> paramCreateContexts;
 
     public BrokerScanNode(PlanNodeId id, TupleDescriptor destTupleDesc, String 
planNodeName,
-                          List<List<TBrokerFileStatus>> fileStatusesList, int 
filesAdded) {
-        super(id, destTupleDesc, planNodeName);
+                          List<List<TBrokerFileStatus>> fileStatusesList, int 
filesAdded,
+                          NodeType nodeType) {
+        super(id, destTupleDesc, planNodeName, nodeType);

Review Comment:
   ```suggestion
           super(id, destTupleDesc, planNodeName, 
PlanNode.NodeType.BROKER_SCAN_NODE);
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/BaseStatsDerive.java:
##########
@@ -0,0 +1,122 @@
+// 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.planner.PlanNode;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+public abstract class BaseStatsDerive {
+    // estimate of the output cardinality of this node;
+    // invalid: -1
+    protected long cardinality = -1;
+    protected long limit = -1;
+
+    protected List<Expr> conjuncts = Lists.newArrayList();
+    protected List<Optional<StatsDeriveResult>> childrenStatsResult = 
Lists.newArrayList();
+
+    protected BaseStatsDerive init(PlanNode node) {
+        limit = node.getLimit();
+        conjuncts.addAll(node.getConjuncts());
+
+        for (PlanNode childNode : node.getChildren()) {
+            childrenStatsResult.add(childNode.getStatsDeriveResult());
+        }
+        return this;
+    }
+
+    public abstract StatsDeriveResult deriveStats();
+
+    public boolean hasLimit() {
+        return limit > -1;
+    }
+
+    protected void applyConjunctsSelectivity() {
+        if (cardinality == -1) {
+            return;
+        }
+        applySelectivity();
+    }
+
+    private void applySelectivity() {
+        double selectivity = computeSelectivity();
+        Preconditions.checkState(cardinality >= 0);
+        long preConjunctCardinality = cardinality;
+        cardinality = Math.round(cardinality * selectivity);
+        // don't round cardinality down to zero for safety.
+        if (cardinality == 0 && preConjunctCardinality > 0) {
+            cardinality = 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.
+     */
+    static protected double computeCombinedSelectivity(List<Expr> conjuncts) {

Review Comment:
   ```suggestion
       protected double computeCombinedSelectivity(List<Expr> conjuncts) {
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/ScanStatsDerive.java:
##########
@@ -0,0 +1,94 @@
+// 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.planner.OlapScanNode;
+import org.apache.doris.planner.PlanNode;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ScanStatsDerive 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 rowCount = -1;
+    private Map<Long, Long> slotIdToDataSize;
+    private Map<Long, Long> slotIdToNdv;
+
+    @Override
+    public ScanStatsDerive init(PlanNode node) {
+        Preconditions.checkState(node instanceof OlapScanNode);
+        super.init(node);
+        buildColumnToStats((OlapScanNode)node);
+        return this;
+    }
+
+    @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.
+         */
+        cardinality = rowCount;
+        applyConjunctsSelectivity();
+        capCardinalityAtLimit();
+        return new StatsDeriveResult(cardinality, rowCount, slotIdToDataSize, 
slotIdToNdv);
+    }
+
+    public void buildColumnToStats(OlapScanNode node) {
+        slotIdToDataSize = new HashMap<>();
+        slotIdToNdv = new HashMap<>();
+        if (node.getTupleDesc() != null
+            && node.getTupleDesc().getTable() != null) {
+            long tableId = node.getTupleDesc().getTable().getId();
+            rowCount = Catalog.getCurrentCatalog().getStatisticsManager()
+                    .getStatistics().getTableStats(tableId).getRowCount();
+        }
+        for (SlotDescriptor slot : node.getTupleDesc().getSlots()) {
+            if (slot.getParent() != null

Review Comment:
   If (!slot.isMaterialized()) {
       continue;
   }



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/BaseStatsDerive.java:
##########
@@ -0,0 +1,122 @@
+// 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.planner.PlanNode;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+public abstract class BaseStatsDerive {
+    // estimate of the output cardinality of this node;
+    // invalid: -1
+    protected long cardinality = -1;
+    protected long limit = -1;
+
+    protected List<Expr> conjuncts = Lists.newArrayList();
+    protected List<Optional<StatsDeriveResult>> childrenStatsResult = 
Lists.newArrayList();
+
+    protected BaseStatsDerive init(PlanNode node) {

Review Comment:
   ```suggestion
       protected void init(PlanNode node) {
   ```



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