Kikyou1997 commented on code in PR #8864:
URL: https://github.com/apache/doris/pull/8864#discussion_r979972528


##########
fe/fe-core/src/main/java/org/apache/doris/statistics/ColumnStats.java:
##########
@@ -72,16 +75,94 @@ public class ColumnStats {
     private float avgSize = -1;  // in bytes
     private long maxSize = -1;  // in bytes
     private long numNulls = -1;
-    private LiteralExpr minValue;
-    private LiteralExpr maxValue;
-
-    public static ColumnStats createDefaultColumnStats() {
-        ColumnStats columnStats = new ColumnStats();
-        columnStats.setAvgSize(1);
-        columnStats.setMaxSize(1);
-        columnStats.setNdv(1);
-        columnStats.setNumNulls(0);
-        return columnStats;
+
+    private LiteralExpr minValue = new NullLiteral();
+    private LiteralExpr maxValue = new NullLiteral();
+
+    /**
+     * Return default column statistic.
+     */
+    public static ColumnStats getDefaultColumnStats() {
+        return new ColumnStats();
+    }
+
+    /**
+     * Merge column statistics(the original statistics should not be modified)
+     *
+     * @param left statistics to be merged
+     * @param right statistics to be merged
+     */
+    public static ColumnStats aggColumnStats(ColumnStats left, ColumnStats 
right) {
+        // merge ndv
+        long leftNdv = left.getNdv();
+        long rightNdv = right.getNdv();
+
+        if (leftNdv == -1) {
+            leftNdv = rightNdv;
+        } else {
+            leftNdv = rightNdv != -1 ? (leftNdv + rightNdv) : leftNdv;
+        }
+
+        // merge avg_size
+        float leftAvgSize = left.getAvgSize();
+        float rightAvgSize = right.getAvgSize();
+        if (leftAvgSize == -1) {
+            leftAvgSize = rightAvgSize;
+        } else {
+            leftAvgSize = rightAvgSize != -1 ? ((leftAvgSize + rightAvgSize) / 
2) : leftAvgSize;
+        }
+
+        // merge max_size
+        long leftMaxSize = left.getMaxSize();
+        long rightMaxSize = right.getMaxSize();
+        if (leftMaxSize == -1) {
+            leftMaxSize = rightMaxSize;
+        } else {
+            leftMaxSize = rightMaxSize != -1 ? (Math.max(leftMaxSize, 
rightMaxSize)) : leftMaxSize;
+        }
+
+        // merge num_nulls
+        long leftNumNulls = left.getNumNulls();
+        long rightNumNulls = right.getNumNulls();
+        if (leftNumNulls == -1) {
+            leftNumNulls = rightNumNulls;
+        } else {
+            leftNumNulls = rightNumNulls != -1 ? (leftNumNulls + 
rightNumNulls) : leftNumNulls;
+        }
+
+        // merge min_value
+        LiteralExpr leftMinValue = left.getMinValue();
+        LiteralExpr rightMinValue = right.getMinValue();
+        if (leftMinValue == null) {
+            leftMinValue = rightMinValue;
+        } else {
+            leftMinValue = leftMinValue.compareTo(rightMinValue) > 0 ? 
rightMinValue : leftMinValue;

Review Comment:
   `Math.min` might be more clear



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/ColumnStats.java:
##########
@@ -72,16 +75,94 @@ public class ColumnStats {
     private float avgSize = -1;  // in bytes
     private long maxSize = -1;  // in bytes
     private long numNulls = -1;
-    private LiteralExpr minValue;
-    private LiteralExpr maxValue;
-
-    public static ColumnStats createDefaultColumnStats() {
-        ColumnStats columnStats = new ColumnStats();
-        columnStats.setAvgSize(1);
-        columnStats.setMaxSize(1);
-        columnStats.setNdv(1);
-        columnStats.setNumNulls(0);
-        return columnStats;
+
+    private LiteralExpr minValue = new NullLiteral();
+    private LiteralExpr maxValue = new NullLiteral();
+
+    /**
+     * Return default column statistic.
+     */
+    public static ColumnStats getDefaultColumnStats() {
+        return new ColumnStats();
+    }
+
+    /**
+     * Merge column statistics(the original statistics should not be modified)
+     *
+     * @param left statistics to be merged
+     * @param right statistics to be merged
+     */
+    public static ColumnStats aggColumnStats(ColumnStats left, ColumnStats 
right) {
+        // merge ndv
+        long leftNdv = left.getNdv();
+        long rightNdv = right.getNdv();
+
+        if (leftNdv == -1) {
+            leftNdv = rightNdv;
+        } else {
+            leftNdv = rightNdv != -1 ? (leftNdv + rightNdv) : leftNdv;
+        }
+
+        // merge avg_size
+        float leftAvgSize = left.getAvgSize();
+        float rightAvgSize = right.getAvgSize();
+        if (leftAvgSize == -1) {
+            leftAvgSize = rightAvgSize;
+        } else {
+            leftAvgSize = rightAvgSize != -1 ? ((leftAvgSize + rightAvgSize) / 
2) : leftAvgSize;
+        }
+
+        // merge max_size
+        long leftMaxSize = left.getMaxSize();
+        long rightMaxSize = right.getMaxSize();
+        if (leftMaxSize == -1) {
+            leftMaxSize = rightMaxSize;
+        } else {
+            leftMaxSize = rightMaxSize != -1 ? (Math.max(leftMaxSize, 
rightMaxSize)) : leftMaxSize;
+        }
+
+        // merge num_nulls
+        long leftNumNulls = left.getNumNulls();
+        long rightNumNulls = right.getNumNulls();
+        if (leftNumNulls == -1) {
+            leftNumNulls = rightNumNulls;
+        } else {
+            leftNumNulls = rightNumNulls != -1 ? (leftNumNulls + 
rightNumNulls) : leftNumNulls;
+        }
+
+        // merge min_value
+        LiteralExpr leftMinValue = left.getMinValue();
+        LiteralExpr rightMinValue = right.getMinValue();
+        if (leftMinValue == null) {
+            leftMinValue = rightMinValue;
+        } else {
+            leftMinValue = leftMinValue.compareTo(rightMinValue) > 0 ? 
rightMinValue : leftMinValue;

Review Comment:
   `Math.min` might looks more clear



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