morrySnow commented on code in PR #12987:
URL: https://github.com/apache/doris/pull/12987#discussion_r983029637


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java:
##########
@@ -48,6 +48,8 @@ public abstract class Expression extends 
AbstractTreeNode<Expression> implements
 
     private static final String INPUT_CHECK_ERROR_MESSAGE = "argument %d 
requires %s type, however '%s' is of %s type";
 
+    private ExprId exprId;

Review Comment:
   expression should not have this attribute



##########
fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java:
##########
@@ -225,6 +225,8 @@ public class SessionVariable implements Serializable, 
Writable {
 
     public static final String ENABLE_CBO_STATISTICS = "enable_cbo_statistics";
 
+    public static final String USE_V2_STAT_CALC = "use_v2_stat_calc";

Review Comment:
   enable_nereids_stats_derive_v2



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/TableStats.java:
##########
@@ -214,12 +186,126 @@ private PartitionStats getNotNullPartitionStats(String 
partitionName) {
      * @param columnName column name
      * @return @ColumnStats
      */
-    private ColumnStats getNotNullColumnStats(String columnName) {
-        ColumnStats columnStats = nameToColumnStats.get(columnName);
-        if (columnStats == null) {
-            columnStats = new ColumnStats();
-            nameToColumnStats.put(columnName, columnStats);
+    private ColumnStat getNotNullColumnStats(String columnName) {
+        ColumnStat columnStat = nameToColumnStats.get(columnName);
+        if (columnStat == null) {
+            columnStat = new ColumnStat();
+            nameToColumnStats.put(columnName, columnStat);
         }
-        return columnStats;
+        return columnStat;
+    }
+
+    public ColumnStat getColumnStats(String columnName) {
+        ColumnStat columnStat = nameToColumnStats.get(columnName);
+        if (columnStat == null) {
+            columnStat = new ColumnStat();
+            nameToColumnStats.put(columnName, columnStat);
+        }
+        return columnStat;
+    }
+
+    public ColumnStat getColumnStatCopy(String columnName) {
+        ColumnStat columnStat = getColumnStats(columnName);
+        return columnStat.copy();
+    }
+
+    public List<String> getShowInfo() {
+        List<String> result = Lists.newArrayList();
+        result.add(Double.toString(getRowCount()));
+        result.add(Long.toString(getDataSize()));
+        return result;
+    }
+
+    public List<String> getShowInfo(String partitionName) {
+        PartitionStats partitionStats = 
nameToPartitionStats.get(partitionName);
+        return partitionStats.getShowInfo();
+    }
+
+    private Map<String, ColumnStat> getAggPartitionColStats() {
+        Map<String, ColumnStat> aggColumnStats = Maps.newConcurrentMap();

Review Comment:
   why need ConcurrentMap?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/CostAndEnforcerJob.java:
##########
@@ -189,8 +191,11 @@ public void execute() {
                     // if we come here, mean that we have some error in stats 
calculator and should fix it.
                     return;
                 }
-                StatsCalculator.estimate(groupExpression);
-
+                if (ConnectContext.get() != null && 
ConnectContext.get().getSessionVariable().useV2StatCalc) {
+                    StatsCalculatorV2.estimate(groupExpression);

Review Comment:
   choose stats derive algorithm inside StasCalculator maybe a better way



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatsDeriveResult.java:
##########
@@ -141,4 +142,26 @@ public StatsDeriveResult merge(StatsDeriveResult other) {
     public StatsDeriveResult copy() {
         return new StatsDeriveResult(this);
     }
+
+    public StatsDeriveResult updateRowCountOnCopy(double selectivity) {
+        StatsDeriveResult copy = new StatsDeriveResult(this);
+        copy.setRowCount(rowCount * selectivity);
+        for (Entry<Slot, ColumnStat> entry : 
copy.slotToColumnStats.entrySet()) {
+            entry.getValue().updateBySelectivity(selectivity, rowCount);
+        }
+        return copy;
+    }
+
+    public static StatsDeriveResult build(StatsDeriveResult another) {

Review Comment:
   why need a static function to wrap copy ctor?



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/TableStats.java:
##########
@@ -214,12 +186,126 @@ private PartitionStats getNotNullPartitionStats(String 
partitionName) {
      * @param columnName column name
      * @return @ColumnStats
      */
-    private ColumnStats getNotNullColumnStats(String columnName) {
-        ColumnStats columnStats = nameToColumnStats.get(columnName);
-        if (columnStats == null) {
-            columnStats = new ColumnStats();
-            nameToColumnStats.put(columnName, columnStats);
+    private ColumnStat getNotNullColumnStats(String columnName) {
+        ColumnStat columnStat = nameToColumnStats.get(columnName);
+        if (columnStat == null) {
+            columnStat = new ColumnStat();
+            nameToColumnStats.put(columnName, columnStat);
         }
-        return columnStats;
+        return columnStat;
+    }
+
+    public ColumnStat getColumnStats(String columnName) {
+        ColumnStat columnStat = nameToColumnStats.get(columnName);
+        if (columnStat == null) {
+            columnStat = new ColumnStat();
+            nameToColumnStats.put(columnName, columnStat);
+        }
+        return columnStat;
+    }
+
+    public ColumnStat getColumnStatCopy(String columnName) {
+        ColumnStat columnStat = getColumnStats(columnName);
+        return columnStat.copy();
+    }
+
+    public List<String> getShowInfo() {
+        List<String> result = Lists.newArrayList();
+        result.add(Double.toString(getRowCount()));
+        result.add(Long.toString(getDataSize()));
+        return result;
+    }
+
+    public List<String> getShowInfo(String partitionName) {
+        PartitionStats partitionStats = 
nameToPartitionStats.get(partitionName);
+        return partitionStats.getShowInfo();
+    }
+
+    private Map<String, ColumnStat> getAggPartitionColStats() {
+        Map<String, ColumnStat> aggColumnStats = Maps.newConcurrentMap();
+        for (PartitionStats partitionStats : nameToPartitionStats.values()) {
+            partitionStats.getNameToColumnStats().forEach((colName, 
columnStats) -> {
+                if (!aggColumnStats.containsKey(colName)) {
+                    aggColumnStats.put(colName, columnStats);
+                } else {
+                    ColumnStat tblColStats = aggColumnStats.get(colName);
+                    aggPartitionColumnStats(tblColStats, columnStats);
+                }
+            });
+        }
+
+        return aggColumnStats;
+    }
+
+    private void aggPartitionColumnStats(ColumnStat leftStats, ColumnStat 
rightStats) {

Review Comment:
   agg -> merge?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/StatsCalculatorV2.java:
##########
@@ -0,0 +1,393 @@
+// 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.catalog.MaterializedIndex;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Partition;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.algebra.Aggregate;
+import org.apache.doris.nereids.trees.plans.algebra.EmptyRelation;
+import org.apache.doris.nereids.trees.plans.algebra.Filter;
+import org.apache.doris.nereids.trees.plans.algebra.Limit;
+import org.apache.doris.nereids.trees.plans.algebra.OneRowRelation;
+import org.apache.doris.nereids.trees.plans.algebra.Project;
+import org.apache.doris.nereids.trees.plans.algebra.Scan;
+import org.apache.doris.nereids.trees.plans.algebra.TopN;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAssertNumRows;
+import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
+import org.apache.doris.nereids.trees.plans.logical.LogicalLimit;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
+import org.apache.doris.nereids.trees.plans.logical.LogicalTopN;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalAggregate;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalAssertNumRows;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalDistribute;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalEmptyRelation;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalFilter;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalHashJoin;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalLimit;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalLocalQuickSort;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalNestedLoopJoin;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalOlapScan;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalOneRowRelation;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalProject;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalQuickSort;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalTopN;
+import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanVisitor;
+import org.apache.doris.nereids.util.Utils;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.statistics.ColumnStat;
+import org.apache.doris.statistics.Statistics;
+import org.apache.doris.statistics.StatsDeriveResult;
+import org.apache.doris.statistics.TableStats;
+
+import com.google.common.collect.Maps;
+
+import java.util.AbstractMap.SimpleEntry;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Used to calculate the stats for each plan
+ */
+public class StatsCalculatorV2 extends DefaultPlanVisitor<StatsDeriveResult, 
Void> {
+
+    private static final int DEFAULT_AGGREGATE_RATIO = 1000;

Review Comment:
   why still need this?



##########
fe/fe-core/src/main/java/org/apache/doris/common/CheckedMath.java:
##########
@@ -38,6 +40,16 @@ public static long checkedMultiply(long a, long b) {
         }
     }
 
+    public static double checkedMultiply(double a, double b) {
+        BigDecimal d1 = new BigDecimal(a);
+        BigDecimal d2 = new BigDecimal(a);

Review Comment:
   add a ut



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/ColumnStat.java:
##########
@@ -0,0 +1,363 @@
+// 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.catalog.PrimitiveType;
+import org.apache.doris.catalog.ScalarType;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.util.Util;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Predicate;
+
+/**
+ * There are the statistics of column.
+ * The column stats are mainly used to provide input for the Optimizer's cost 
model.
+ * <p>
+ * The description of column stats are following:
+ * 1. @ndv: The number distinct values of column.
+ * 2. @avgSize: The average size of column. The unit is bytes.
+ * 3. @maxSize: The max size of column. The unit is bytes.
+ * 4. @numNulls: The number of nulls.
+ * 5. @minValue: The min value of column.
+ * 6. @maxValue: The max value of column.
+ * <p>
+ * The granularity of the statistics is whole table.
+ * For example:
+ * "@ndv = 10" means that the number distinct values is 10 in the whole table.
+ */
+public class ColumnStat {
+
+    public static final StatsType NDV = StatsType.NDV;
+    public static final StatsType AVG_SIZE = StatsType.AVG_SIZE;
+    public static final StatsType MAX_SIZE = StatsType.MAX_SIZE;
+    public static final StatsType NUM_NULLS = StatsType.NUM_NULLS;
+    public static final StatsType MIN_VALUE = StatsType.MIN_VALUE;
+    public static final StatsType MAX_VALUE = StatsType.MAX_VALUE;
+
+    public static final ColumnStat UNKNOWN = new ColumnStat();
+
+    private static final Predicate<Double> DESIRED_NDV_PRED = (v) -> v >= -1L;
+    private static final Predicate<Double> DESIRED_AVG_SIZE_PRED = (v) -> (v 
== -1) || (v >= 0);
+    private static final Predicate<Double> DESIRED_MAX_SIZE_PRED = (v) -> v >= 
-1L;
+    private static final Predicate<Double> DESIRED_NUM_NULLS_PRED = (v) -> v 
>= -1L;
+
+    private static final Set<Type> MAX_MIN_UNSUPPORTED_TYPE = new HashSet<>();
+
+    static {
+        MAX_MIN_UNSUPPORTED_TYPE.add(Type.VARCHAR);
+        MAX_MIN_UNSUPPORTED_TYPE.add(Type.CHAR);
+        MAX_MIN_UNSUPPORTED_TYPE.add(Type.HLL);
+        MAX_MIN_UNSUPPORTED_TYPE.add(Type.BITMAP);
+        MAX_MIN_UNSUPPORTED_TYPE.add(Type.ARRAY);
+        MAX_MIN_UNSUPPORTED_TYPE.add(Type.STRUCT);
+        MAX_MIN_UNSUPPORTED_TYPE.add(Type.MAP);
+    }
+
+    private double ndv = -1;
+    private double avgSize = -1;  // in bytes
+    private double maxSize = -1;  // in bytes

Review Comment:
   put bytes into attribute name



##########
fe/fe-core/src/main/java/org/apache/doris/common/CheckedMath.java:
##########
@@ -38,6 +40,16 @@ public static long checkedMultiply(long a, long b) {
         }
     }
 
+    public static double checkedMultiply(double a, double b) {
+        BigDecimal d1 = new BigDecimal(a);
+        BigDecimal d2 = new BigDecimal(a);

Review Comment:
   ```suggestion
           BigDecimal d2 = new BigDecimal(b);
   ```



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