Skip to content

Do not estimate a non-empty filtered relation as 0 rows from statistics - #130202

Merged
alexey-milovidov merged 3 commits into
masterfrom
statistics-no-zero-row-estimates
Nov 10, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
statistics-no-zero-row-estimates

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

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.1660 with 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 unfiltered cast_info (36M rows) and name (4M rows) first. Greedy got the same wrong estimates but happened to pick safer local orders.

Two bugs in ConditionSelectivityEstimator produced the zeros:

  • The row estimate was truncated. For an equality on a column whose uniq_v2 NDV is slightly above the row count, rows / ndv is a hair below 1 and became 0. It is now rounded, and kept at 1 or more while the selectivity is positive.
  • A constant conjunct was treated as an unknown condition with selectivity 0.33. In the versions benchmark, NULLs are loaded as defaults, so the JOB predicate x IS NOT NULL folds to 1, and that constant is pushed into the filter of every table. Every filtered relation was therefore underestimated about 3x. A constant conjunct is now ALWAYS_TRUE / ALWAYS_FALSE, as in KeyCondition.

Result with this change (local, aarch64, same data):

greedy DPsub
master 10.0 s 42.0 s
this PR 10.7 s 11.1 s

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 IN on frequent values (ci.note, mi.info) is estimated as rows / NDV. That estimate is 1000x+ too low, because basic, uniq_v2 statistics carry no frequencies. With auto_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. Adding countmin to the default auto statistics is left for a follow-up.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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 NULL on a non-Nullable column). 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

  • Merged into: 26.11.1.1812-master (included in 26.11 and later)

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]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4334d5c]

Summary: ✅


AI Review

Summary

ConditionSelectivityEstimator now rounds positive row estimates with a minimum of one and recognizes constant boolean conditions, addressing the two reported causes of poor join orders. The focused regression covers both causes, and the Iceberg reference changes follow the new rounding behavior. No actionable findings at PR head 4334d5c7a17eba2b586e2a3997854f21d3570a52.

Missing context / blind spots
  • ⚠️ The broader CI suites are still pending. The current-head Fast test report confirms that 05569_statistics_equality_estimate_not_zero passed; the updated Iceberg references still await full-suite validation. CI report.
Final Verdict

Status: ✅ Approve. No code changes requested and no new inline findings.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 8, 2026
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review November 8, 2026 19:38

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Nov 8, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4334d5c7a with master 28cd3f355 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline.

Job report

@fkastrati fkastrati left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Both fixes look correct to me.

alexey-milovidov and others added 2 commits November 9, 2026 14:40
`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]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 9, 2026
Merged via the queue into master with commit 8afe699 Nov 10, 2026
188 checks passed
@alexey-milovidov
alexey-milovidov deleted the statistics-no-zero-row-estimates branch November 10, 2026 04:25
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 10, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 10, 2026
…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]>
@clickhouse-gh clickhouse-gh Bot added comp-statistics Column statistics: statistics kept per part on a column (tdigest, uniq, count-min sketch, minmax)... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-statistics Column statistics: statistics kept per part on a column (tdigest, uniq, count-min sketch, minmax)... pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants