englefly commented on code in PR #70911:
URL: https://github.com/apache/doris/pull/70911#discussion_r4749001886


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterEstimation.java:
##########
@@ -679,19 +680,13 @@ private Statistics 
estimateColumnEqualToConstant(ComparisonPredicate cp, ColumnS
                     selectivity = 0.0;
                 } else if (ndv >= 1.0) {
                     selectivity = StatsMathUtil.minNonNaN(1.0, 1.0 / ndv);
-                    if (statsForLeft.getHotValues() != null && cp.child(1) 
instanceof Literal) {
-                        Literal constHand = (Literal) cp.child(1);
-                        for (Literal hot : 
statsForLeft.getHotValues().keySet()) {
-                            try {
-                                // stats-derive may be applied before type 
coercion. so we need to try to catch
-                                // comparison exception. for example: boolean 
compare with int
-                                if (((ComparableLiteral) 
hot).compareTo((ComparableLiteral) constHand) == 0) {
-                                    selectivity = 
statsForLeft.getHotValues().get(hot);
-                                    break;
-                                }
-                            } catch (Exception e) {
-                                // ignore
-                            }
+                    Map<Literal, Float> hotValues = 
statsForLeft.getHotValues();
+                    if (hotValues != null && !hotValues.isEmpty() && 
cp.child(1) instanceof Literal) {
+                        Map.Entry<Literal, Float> match = 
findMatchingHotValue(hotValues, (Literal) cp.child(1));
+                        if (match != null && match.getValue() > 0) {
+                            selectivity = 
Statistics.getValidSelectivity(match.getValue());
+                        } else {
+                            selectivity = 
estimateSkewedValueSelectivity(hotValues, ndv, 1, List.of());

Review Comment:
   Fixed in .
   
   Expression estimation now drops inherited hot values for casts and other 
value-changing expressions before those statistics reach filters or projected 
aliases. The new test covers CAST with equality and IN, SQRT, and hot-value 
preservation through a plain column alias.
   



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterEstimation.java:
##########
@@ -729,30 +724,57 @@ private Statistics 
estimateColumnToColumn(ComparisonPredicate cp, EstimationCont
         }
     }
 
-    private Map<Literal, Float> 
computeHotValuesForInPredicates(ColumnStatistic colStats,
-                                                                List<Literal> 
options) {
-        if (colStats.getHotValues() != null) {
-            Map<Literal, Float> hotValues = colStats.getHotValues();
-            Map<Literal, Float> matchedHotValues = new HashMap<>();
-            for (Expression option : options) {
-                try {
-                    for (Map.Entry<Literal, Float> entry : 
hotValues.entrySet()) {
-                        if (((ComparableLiteral) 
entry.getKey()).compareTo((ComparableLiteral) option) == 0) {
-                            matchedHotValues.put(entry.getKey(), 
entry.getValue());
-                            break;
-                        }
-                    }
-                } catch (Exception e) {
-                    // ignore
+    private static Map.Entry<Literal, Float> findMatchingHotValue(Map<Literal, 
Float> hotValues, Literal option) {
+        for (Map.Entry<Literal, Float> entry : hotValues.entrySet()) {
+            try {
+                if (((ComparableLiteral) 
entry.getKey()).compareTo((ComparableLiteral) option) == 0) {
+                    return entry;
                 }
-            }
-            if (!matchedHotValues.isEmpty()) {
-                return matchedHotValues;
+            } catch (Exception e) {
+                // Statistics may be derived before type coercion makes the 
literals comparable.
             }
         }
         return null;
     }
 
+    private Map<Literal, Float> 
computeHotValuesForInPredicates(ColumnStatistic colStats, List<Literal> 
options) {
+        Map<Literal, Float> matchedHotValues = new HashMap<>();
+        for (Literal option : options) {
+            Map.Entry<Literal, Float> entry = 
findMatchingHotValue(colStats.getHotValues(), option);
+            if (entry != null) {
+                matchedHotValues.put(entry.getKey(), entry.getValue());
+            }
+        }
+        return matchedHotValues.isEmpty() ? null : matchedHotValues;
+    }
+
+    private static double estimateSkewedValueSelectivity(Map<Literal, Float> 
hotValues, double inputNdv,
+            double selectedNdv, Collection<Float> matchedHotRatios) {
+        // These frequencies are conditional on the value being non-NULL. 
Legacy statistics
+        // can round rare values to zero, so include those values in the 
remaining distribution.
+        double remainingRatio = 1;
+        double remainingNdv = inputNdv;
+        for (float ratio : hotValues.values()) {
+            if (ratio > 0) {
+                remainingRatio -= ratio;
+                remainingNdv--;
+            }
+        }
+        double selectivity = 0;
+        double selectedColdNdv = selectedNdv;
+        for (float ratio : matchedHotRatios) {
+            if (ratio > 0) {
+                selectivity += ratio;
+                selectedColdNdv--;
+            }
+        }
+        if (remainingNdv > 0) {

Review Comment:
   Fixed in .
   
   When the retained hot map exhausts a reduced NDV and cold candidates remain, 
the shared estimator falls back to uniform selectivity using the current NDV. 
The AND reproducer now estimates one row for equality and two rows for the 
cold-only IN list. The existing test for a fully recorded distribution still 
passes.
   



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterEstimation.java:
##########
@@ -729,30 +724,57 @@ private Statistics 
estimateColumnToColumn(ComparisonPredicate cp, EstimationCont
         }
     }
 
-    private Map<Literal, Float> 
computeHotValuesForInPredicates(ColumnStatistic colStats,
-                                                                List<Literal> 
options) {
-        if (colStats.getHotValues() != null) {
-            Map<Literal, Float> hotValues = colStats.getHotValues();
-            Map<Literal, Float> matchedHotValues = new HashMap<>();
-            for (Expression option : options) {
-                try {
-                    for (Map.Entry<Literal, Float> entry : 
hotValues.entrySet()) {
-                        if (((ComparableLiteral) 
entry.getKey()).compareTo((ComparableLiteral) option) == 0) {
-                            matchedHotValues.put(entry.getKey(), 
entry.getValue());
-                            break;
-                        }
-                    }
-                } catch (Exception e) {
-                    // ignore
+    private static Map.Entry<Literal, Float> findMatchingHotValue(Map<Literal, 
Float> hotValues, Literal option) {
+        for (Map.Entry<Literal, Float> entry : hotValues.entrySet()) {
+            try {
+                if (((ComparableLiteral) 
entry.getKey()).compareTo((ComparableLiteral) option) == 0) {
+                    return entry;
                 }
-            }
-            if (!matchedHotValues.isEmpty()) {
-                return matchedHotValues;
+            } catch (Exception e) {
+                // Statistics may be derived before type coercion makes the 
literals comparable.
             }
         }
         return null;
     }
 
+    private Map<Literal, Float> 
computeHotValuesForInPredicates(ColumnStatistic colStats, List<Literal> 
options) {
+        Map<Literal, Float> matchedHotValues = new HashMap<>();
+        for (Literal option : options) {
+            Map.Entry<Literal, Float> entry = 
findMatchingHotValue(colStats.getHotValues(), option);
+            if (entry != null) {
+                matchedHotValues.put(entry.getKey(), entry.getValue());
+            }
+        }
+        return matchedHotValues.isEmpty() ? null : matchedHotValues;
+    }
+
+    private static double estimateSkewedValueSelectivity(Map<Literal, Float> 
hotValues, double inputNdv,
+            double selectedNdv, Collection<Float> matchedHotRatios) {
+        // These frequencies are conditional on the value being non-NULL. 
Legacy statistics
+        // can round rare values to zero, so include those values in the 
remaining distribution.
+        double remainingRatio = 1;
+        double remainingNdv = inputNdv;
+        for (float ratio : hotValues.values()) {
+            if (ratio > 0) {
+                remainingRatio -= ratio;
+                remainingNdv--;
+            }
+        }
+        double selectivity = 0;
+        double selectedColdNdv = selectedNdv;
+        for (float ratio : matchedHotRatios) {
+            if (ratio > 0) {
+                selectivity += ratio;
+                selectedColdNdv--;
+            }
+        }
+        if (remainingNdv > 0) {
+            double coldFraction = 
Statistics.getValidSelectivity(selectedColdNdv / remainingNdv);
+            selectivity += Math.max(0, remainingRatio) * coldFraction;

Review Comment:
   Fixed in .
   
   When rounding exhausts the frequency mass and cold NDV remains, the shared 
estimator allows one non-NULL row per selected cold value and retains the 
estimated contribution from matching hot values. The new test covers an omitted 
literal, a zero-frequency entry, and both cold-only and mixed IN lists.
   



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/analysis/BaseAnalysisTask.java:
##########
@@ -200,7 +200,7 @@ public enum AnalyzeSampleAlgorithm {
             + "cte3 AS ("
             +     "SELECT IFNULL(GROUP_CONCAT(CONCAT("
             +         "REPLACE(REPLACE(t2.`col_value`, \":\", \"\\\\:\"), 
\";\", \"\\\\;\"), "
-            +         "\" :\", ROUND(t2.`count` / ${rowCount2}, 2)), \" ;\"), 
'') "
+            +         "\" :\", ROUND(t2.`count` / ${rowCount2}, 6)), \" ;\"), 
'') "

Review Comment:
   Fixed in .
   
   Updated the exact DUJ1 SQL expectation in 
`OlapAnalysisTaskTest.testKeyColumnUseLimitAndNot` from `ROUND(..., 2)` to 
`ROUND(..., 6)`. The assertion failed on the previous revision and passes with 
the update.
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to