Skip to content

Sampling for Hypothetical Indexes - #126991

Merged
nihalzp merged 26 commits into
masterfrom
yarik/whatif-index-sampling
Nov 2, 2026
Merged

nihalzp merged 26 commits into
masterfrom
yarik/whatif-index-sampling

Conversation

@yariks5s

@yariks5s yariks5s commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Related: #121766

EXPLAIN WHATIF estimates a hypothetical skip index by building the index in memory. Until now it read every index window that touches the baseline marks. On a large table this reads a lot of data.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

EXPLAIN WHATIF estimates a hypothetical skip index from a sample of index windows when the scan would read more than index_scan_budget_rows rows (10 million by default). It shows the sampling error as marks_span


Workflow [PR]
Sync PR [sync-upstream/pr/126991]

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bbd6bd0]

Summary: ✅


AI Review

Summary

EXPLAIN WHATIF now samples hypothetical skip-index windows beyond index_scan_budget_rows, reports uncertainty through marks_span, and withholds combined estimates when a contributing candidate is sampled. No remaining actionable findings at bbd6bd0922c1edcdc8853124d6cc1e548fb1bdf0: the previous sampling-bias, uncertainty, and observed-bound issues are addressed in the current code. The three added regression tests pass in the current-head Fast test report. Validation relied on source review and CI results; no local build or tests were run.

Final Verdict

✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Oct 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bbd6bd092 with master be7f34ade (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of d7b58a43e; 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.

Object file sizes

8 object files changed (+57.80 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/WhatIfEmpiricalEstimator.cpp.o 119.41 KiB 156.24 KiB +36.83 KiB (+30.84%)
src/CMakeFiles/dbms.dir/Storages/MergeTree/WhatIfSampling.cpp.o new 19.32 KiB +19.32 KiB

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

12 translation units recompiled, 40 s compile time in total, 11 of them have a recent master baseline.

Job report

…le in proportion to its marks

With primary-key pruning the windows can hold very different numbers of baseline marks. A sample of windows then often missed the few windows with most of the marks, and the span came from the count of windows, not of marks (`marks: 0`, `marks_span: 0 to 5990`, real 30000). Now each run takes one baseline mark and reads its window, which stands for the marks of the runs that took it.
#126991 (comment)
…the time limit of the sanitizer flaky checks
…nd take it as the least sampling error of a skip-index estimate
… and the not skipped marks of the sampled windows, so it is in `marks_span`
@yariks5s
yariks5s marked this pull request as ready for review October 23, 2026 10:40
yariks5s and others added 4 commits October 23, 2026 13:10
…f `05408`, so the test writes half the marks
…arks, so the test fits the time limit of the private sanitizer flaky check
… the test does not run the full-scan oracles and fits the time limit of the private sanitizer flaky check

The private check `Stateless tests (amd_asan_ubsan, flaky check, s3 storage, meta in keeper)` still stopped `05408` at 180 s (runs took 120-180 s). Its query_log shows that the full-scan `EXPLAIN WHATIF` queries read about 70% of the marks of the test. The first case still checks that a full scan is exact against the twin table.

Report: https://s3.amazonaws.com/clickhouse-test-reports-private/praktika.html?PR=83399&sha=2c3d807640cbd89abe6afec01d17278a5bde59c5&name_0=PR&name_1=Stateless%20tests%20(amd_asan_ubsan%2C%20flaky%20check%2C%20s3%20storage%2C%20meta%20in%20keeper)
PR: #126991

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… for all parts, and take 3 heaviest draws as the least error, so many light draws do not hide a heavy one

A run for each part gave a part with dense windows one draw of 64 marks next to 500 draws of 4 marks of the other part. The effective count of draws was 352, so the rule of three gave `marks: 0`, `marks_span: 0 to 26`, real 32. Now all parts use the same run, so a part gets draws in proportion to its baseline marks (here both windows of the dense part, `marks: 32`, `marks_span: 32 to 57`). With draws of unequal weights, a change that no draw found has fewer marks than 3 heaviest draws with a 95% chance, so this is the least error.

#126991 (comment)
#126991 (comment)
…o a part that the sample reads fully, because the draws decide which parts these are and their exact counts biased the estimate
…the weighted distances from the mean, so a heavy draw next to a light draw of a short last run keeps all its variance in `marks_span`
@yariks5s
yariks5s requested a review from nihalzp October 26, 2026 10:49
@nihalzp nihalzp self-assigned this Oct 26, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 2, 2026
Merged via the queue into master with commit 7f036b6 Nov 2, 2026
179 checks passed
@nihalzp
nihalzp deleted the yarik/whatif-index-sampling branch November 2, 2026 17:43
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 2, 2026
middas-0706 pushed a commit to middas-0706/ClickHouse that referenced this pull request Nov 7, 2026
…ColumnsPass

The pass set the type of every reference to an ARRAY JOIN column (the first
argument of `tupleElement`) to the type of the ARRAY JOIN expression, for every
expression, pruned or not. The analyzer can give a reference a different type
than the definition: `join_use_nulls` (ARRAY JOIN on the nullified side of a
RIGHT/FULL JOIN) and `group_by_use_nulls` (the column is a ROLLUP/CUBE key)
make it `Nullable`, and a `USING` key gives it the common supertype. After the
overwrite `tupleElement` was re-resolved to the narrower type while its parent
function still expected the old one, so the query tree validator of debug and
sanitizer builds threw `LOGICAL_ERROR` "Function tupleElement(__array_join_exp_1,
'a') > 0 expects argument 1 to have Nullable(Int64) type but receives Int64 after
running PruneArrayJoinColumns pass". Release builds were not affected: the
planner rebuilds a function whose argument types do not match.

Now `pruned_type` is set only when `pruneNestedFunctionArguments` actually
pruned the expression, so references of other expressions keep their type, and
a reference of a pruned expression keeps its own `Nullable`. A `USING` key or a
matcher column is a direct reference, which marks the expression fully used,
so it is never pruned.

Found by the server-side AST fuzzer in Stress tests (PR ClickHouse#126991, ClickHouse#118582,
ClickHouse#115763).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-what-if What-if analysis: hypothetical indexes and projections that exist only in the catalog, and the es... 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-what-if What-if analysis: hypothetical indexes and projections that exist only in the catalog, and the es... pr-improvement Pull request with some product improvements 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