github-actions[bot] commented on code in PR #70911:
URL: https://github.com/apache/doris/pull/70911#discussion_r4675244069
##########
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:
[P2] Check the hot-map value domain before inferring a cold miss
For `Filter(CAST(d AS INT) = 1) -> Scan(d DOUBLE)` with 1,000 rows split
equally between `0.1` and `1.1`, full hot statistics contain `{0.1: 0.5, 1.1:
0.5}`. `ExpressionEstimation.visitCast/castMinMax` returns the original numeric
statistics, including those uncast hot keys. Neither key matches integer `1`,
so this new fallback exhausts the two recorded NDVs and returns **0 rows**,
although 500 rows match; the previous uniform fallback estimated 500.
`SimplifyComparisonPredicate` retains this narrowing cast. `visitSqrt` also
retains untransformed hot keys, so the issue extends to functions.
Transform/invalidate maps when their expression domain changes, or establish
that the map describes the compared values before applying residual inference;
otherwise populated filters acquire zero cardinality and distort plan costing.
##########
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:
[P2] Preserve a fallback when earlier filters have capped NDV
Consider `Filter(AND(a = 1, b = 19)) -> Scan(a, b)` over 10,000 non-NULL
rows, with `a.ndv = 1,000`, `b.ndv = 20`, range `[0,19]`, and ten positive hot
entries for `b` (keys `0..9`, total frequency `0.9`). Estimating `a = 1` leaves
ten rows; `rescaleNumNulls`/`Statistics.buildValidColumnStatistic` caps `b.ndv`
to ten while `ColumnStatisticBuilder` retains its entire hot map. `visitAnd`
then estimates `b = 19` using that derived NDV. This loop subtracts all ten hot
entries, and `remainingNdv == 0` makes the helper return **0 rows**. Previously
the unmatched equality estimated one row, which is realizable by putting one `b
= 19` row among the ten `a = 1` rows. A capped NDV does not prove the retained
hot map exhausts the value domain; preserve a valid residual/fallback for
derived statistics.
##########
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:
[P2] Update the existing DUJ1 SQL expectation with the template
Changing this template to `ROUND(..., 6)` leaves
`OlapAnalysisTaskTest.testKeyColumnUseLimitAndNot` expecting the full query
with `ROUND(t2.`count` / ${rowCount2}, 2)` at line 194. Its second `doSample()`
invocation explicitly forces `AnalyzeSampleAlgorithm.DUJ1`, and the mocked
`runQuery` asserts exact string equality. The generated SQL now contains `6`,
so this existing FE unit test will fail deterministically. Update that
expectation together with the template. This failure is established by static
inspection; the test was not executed.
##########
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:
[P2] Handle exhausted probability mass in legacy rounded statistics
A supported legacy map can have positive cold NDV but no residual mass after
two-decimal rounding. For 100,000 rows with `v=0` on 99,980 rows and `v=1..20`
once each, the old top-ten collector stores `0:1.00` and nine tail entries at
`0.00`; at least eleven tail values are unrecorded. For `Filter(v = 19) ->
Scan(v)` when `19` is omitted, the parser retains those frequencies, and this
helper computes `remainingNdv=20` but `remainingRatio=0`, producing **0 rows**
for a real match. Cold-only IN lists have the same regression. Previously an
omitted literal took the positive uniform fallback. Increasing collection
precision does not update already persisted maps, and the added zero-frequency
test retains nonzero residual mass. Please handle this lossy legacy case
without treating rounded exhausted mass as proof that all remaining values are
absent.
--
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]