Repository navigation
Do not estimate a non-empty filtered relation as 0 rows from statistics - #130202
Conversation
Two bugs in `ConditionSelectivityEstimator` made DPsub lose to greedy on the Join Order Benchmark (https://benchmark.clickhouse.com/versions/): - The row estimate was truncated, so `rows * (1 / ndv)` a hair below 1 became 0. The join order optimizer takes 0 for an empty relation, every join with it costs nothing, and DPsub (which minimizes the sum of intermediate rows exactly) picks among ties, joining unfiltered large tables first. - A constant conjunct, e.g. `x IS NOT NULL` folded to `1` for a non-`Nullable` column and pushed into the filter of every table, was an unknown condition with selectivity 0.33. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [4334d5c] Summary: ✅
AI ReviewSummary
Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested and no new inline findings. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
fkastrati
left a comment
There was a problem hiding this comment.
LGTM. Both fixes look correct to me.
`ConditionSelectivityEstimator` now rounds the estimated rows instead of truncating them, so `05299_iceberg_manifest_stats_min_max` and `05428_iceberg_manifest_stats_conditions` print 501 instead of 500, 8000 instead of 7999, and `dim[2]` instead of `dim[1]` (about 1.6 rows) where the fraction is at least one half. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=130202&sha=latest&name_0=PR PR: #130202 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ounts Since ClickHouse#130202 the statistics estimate rounds the row count instead of truncating it, and CI tests the merge with master. 3000 * 0.33 * 0.33 = 326.7 rows for `x0` and `x3` is now shown as 327; the join order is the same. Co-Authored-By: Claude Opus 5.5 <[email protected]>
After the default join order algorithm became DPsub with the conflict detector, the Join Order Benchmark got about 5x slower on https://benchmark.clickhouse.com/versions/ (26.10: 8.6 s; master: 48 s; query 26 went from 0.44 s to 32 s). I reproduced this locally on master
26.11.1.1660with the versions-benchmark schema: greedy 10.0 s, DPsub 42.0 s.The algorithm itself is not the problem. In every slow query, statistics estimated small dimension tables (
info_type,kind_type,comp_cast_type) at 0 rows. The optimizer takes 0 to mean an empty relation, so every join with such a table is estimated at 1 row and costs nothing. DPsub minimizes the sum of intermediate rows exactly, so it is left choosing among ties. In practice it joins the unfilteredcast_info(36M rows) andname(4M rows) first. Greedy got the same wrong estimates but happened to pick safer local orders.Two bugs in
ConditionSelectivityEstimatorproduced the zeros:uniq_v2NDV is slightly above the row count,rows / ndvis a hair below 1 and became 0. It is now rounded, and kept at 1 or more while the selectivity is positive.x IS NOT NULLfolds to1, and that constant is pushed into the filter of every table. Every filtered relation was therefore underestimated about 3x. A constant conjunct is nowALWAYS_TRUE/ALWAYS_FALSE, as inKeyCondition.Result with this change (local, aarch64, same data):
Per query, DPsub: Q93 20.6 s → 0.07 s, Q26 11.2 s → 0.12 s (greedy: 0.31 s), Q75 0.34 s → 0.07 s, Q91 0.22 s → 0.08 s.
DPsub still loses on Q72 and Q108, where equality or
INon frequent values (ci.note,mi.info) is estimated asrows / NDV. That estimate is 1000x+ too low, becausebasic, uniq_v2statistics carry no frequencies. Withauto_statistics_types = 'basic, uniq_v2, countmin', those estimates are exact. A separate 2x slowdown of the JOB 17x/23x queries, seen with both algorithms on the versions page, also disappears that way. Addingcountminto the default auto statistics is left for a follow-up.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed row estimates from column statistics that could be 0 for a non-empty filtered table (an equality matching about one row, or a filter with a constant condition such as
x IS NOT NULLon a non-Nullablecolumn). The join order optimizer took such a table for empty and could choose very slow join orders, for example in the Join Order Benchmark.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/130202]
Version info
26.11.1.1812-master(included in26.11and later)