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


##########
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:
   done



##########
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:
   done



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