Skip to content

Fix flaky test 05481_shuffle_randomness - #131668

Open
groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-05481-shuffle-randomness
Open

groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-05481-shuffle-randomness

Conversation

@groeneai

@groeneai groeneai commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #100305

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Not required.

Description

The last query of 05481_shuffle_randomness failed on master with max(c) < 150 false. Its bounds were 5.3 standard deviations, not the "more than 10" the header states: a perfectly uniform SHUFFLE LIMIT 1000 fails it at up to 4.5e-06 per run (1 master failure in ~27.7k runs in 30 days).

With SHUFFLE LIMIT 10000 the bounds are 11.7 sd (counts) and 10.4 sd (mean), yet tighter relative to the mean, so a real bias is caught more often (a 1.5x weight on one bucket: 299/300 runs, was 85/300).

The flaky check of this PR also showed the file taking up to 214 s with 8 copies of it under the thread fuzzer, about 90% of it in the two full SHUFFLEs of 1M rows at max_block_size = 1000. They now use max_block_size = 10000 (same rows and bounds, still 12 buckets per part): with the fuzzer the file takes 54-59 s instead of 309-341 s locally.

Validation: 5500 local runs over 5 shapes (max_threads 1/2/8/16) match the uniform model in variance, mean and tails, so the sampler is not at fault. The test passes 100/100 with random settings.


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

The header of the test says the bounds are more than 10 standard
deviations wide, but the last query (`SHUFFLE LIMIT 1000` of
`numbers_mt(10000000)`, 10 value buckets) did not meet that. Every bucket
count is hypergeometric with mean 100 and sd 9.49, so `min(c) > 50` and
`max(c) < 150` are only 5.27 sd, and the mean bound 500000 is 5.48 sd
(sd 91283). Exact tails: P(X >= 150) = 4.48e-07 per bucket, so a uniform
SHUFFLE fails the query at up to 4.5e-06 per run. Observed: 1 master
failure (`max(c) < 150` false) in about 27.7k runs over 30 days.

Take a 10x larger sample instead: `SHUFFLE LIMIT 10000`, counts in
(650, 1350) = 11.7 sd (sd 29.98), mean within 300000 = 10.4 sd (sd 28853).
Relative to the mean the bounds are tighter than before (+-35% vs +-50%
on counts, +-6% vs +-10% on the mean), so the check is more sensitive to
a real bias. The query still samples a small fraction of every stream
(8 streams of ~1.25M rows).

Validation with the master debug build (a33e1c1), through
clickhouse local:
- uniformity, 2000 runs at k = 1000 and 2000 at k = 10000, plus 500 each
  with max_threads 1/2/16: bucket means, pooled variance (90.7 vs model
  90.0; 904.6 vs 899.1), sd of the mean (90770 vs 91283; 28899 vs 28853)
  and tail rates (max >= 125: 122/2000, model 118) all match a uniform
  sample, so the failure is the bound and not the sampler;
- a 1.5x weight on one bucket: the old query catches it in 85/300 runs,
  the new one in 299/300.
The test: 20/20 before; 100/100 with random settings and 50/50 without
after; median duration 0.29 s before, 0.34 s after.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (1 nit).

# Sev Finding Verdict Evidence / action
1 ⚠️ Query 7 does not assert that SHUFFLE LIMIT 10000 returns exactly 10000 rows DISAGREE The sampler keeps exactly limit rows and the merge emits exactly min(limit, population) or throws (ShuffleTransform.cpp:417-469, :599-664, :616-619). The exact count is already asserted on the same path by 05480_shuffle_clause.sql:30, 05483_shuffle_order_statistics.sql:23, 05490_shuffle_distinct_population.sql:5. The old query had the same gap; 2000/2000 local runs returned exactly 10000 rows.
2 💡 The header's "more than 10 standard deviations" is still inexact for query 6 (8.9 sd with the exact sd 0.0112) DISAGREE Untouched query, false-fail rate about 1.9e-19 per run; left as is to keep this PR to the failing query.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-48:20261114-172300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? The CI event (at most 4.5e-06 per run) cannot be triggered on demand. Its mechanism is reproduced: 5500 runs over 5 shapes (max_threads 1/2/8/16) match the uniform hypergeometric model, including tail rates at 125/130/75, so the old bound fails at the exact model tail.
b Root cause explained? Query 7 bucket counts have sd 9.49 (mean 100), so (50, 150) is 5.27 sd and the mean bound is 5.48 sd, not the "more than 10" the header claims. P(X >= 150) = 4.48e-07 per bucket, at most 4.5e-06 per run; 1 master failure in ~27.7k runs.
c Fix matches root cause? Yes. A 10x sample shrinks the relative spread: counts (650, 1350) = 11.7 sd, mean bound 10.4 sd, both tighter relative to the mean than before. No bound was widened.
d Test intent preserved / new tests added? Kept and strengthened: still a small sample of a large stream (8 streams of ~1.25M rows), and a 1.5x weight on one bucket is now caught in 299/300 runs (was 85/300). Test-only change.
e Both directions demonstrated? Exact false-fail rate per run: 4.5e-06 before, 4.3e-28 after, on a sampler measured uniform. Test: 20/20 before; 100/100 random settings and 50/50 without after.
f Fix is general across code paths? Every statistical bound of the SHUFFLE tests (05481, 05482, 05483, 05486, 05490) was checked; all others are below 1e-17 per run. Only this query needed a change.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for types (test-only). Checked across max_threads 1/2/8/16 and max_block_size 1000/65536/73000, with and without random settings.
h Backward compatible? (maintainer-approved exception only) N/A (test-only; .reference unchanged).
i Invariants and contracts preserved? N/A (no source change).

Session id: cron:clickhouse-impl-slot-40:20261114-170501

@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [aa3f0fd]

Summary: ✅


AI Review

Summary

The PR stabilizes 05481_shuffle_randomness by increasing the small sample to 10,000 rows and using larger query-local blocks for the two full shuffles. The revised statistical bounds reduce false failures while tightening relative tolerances, and the block-size changes preserve multiple buckets and partial output blocks. No actionable findings.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-testing Test content only (no production code). labels Nov 14, 2026
The flaky check of this PR failed the test with "Test runs too long
(> 180s)": `amd_debug` p50 141 s (5 runs 187-194 s), `amd_binary` p50
148 s (5 runs 213-214 s). In that job 8 workers run the same changed test
under the thread fuzzer, which migrates the thread on every mutex
operation, so the cost follows the number of chunks through the pipeline,
not the rows. Its `query_log` puts the two full `SHUFFLE`s of 1M rows at
68 s + 64 s of a 141 s run: at `max_block_size = 1000` every part of the
scatter is a 125-row piece. In the flaky check of ClickHouse#100305 the test shared
the workers with 18 other new tests, which hid this (`amd_debug` p50 80-131 s over 7 heads).

Run those two queries with `max_block_size = 10000`. The rows and the
bounds are unchanged, and every part of the shuffle still has 12 buckets
(125000 / 10000), so the multi-bucket path and the partial output blocks
are still exercised (192 output blocks).

Measured with the master debug build and the CI thread fuzzer settings,
one copy on a server: the file takes 54-59 s instead of 309-341 s (query 3
129-135 s -> 7 s, query 4 134-144 s -> 8-9 s); 16 runs in flaky-check mode
with 8 workers: all OK, p50 58 s, max 63 s. Without the fuzzer: 50/50 OK
with random settings, 20/20 without. Without `SHUFFLE` both queries still
return 0, as does query 4 for a shuffle within 10000-row blocks only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - ddbb560

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build (amd_msan) / Relocation headroom not from this PR (it changes one .sql file): the check fell back to the older master baseline 58726cc5 because master's own Build (amd_msan) of the clang-23 merge edfbc33b (#117346) had not reported yet, so it charged that merge's layout change (-16.23 MiB unit_tests_dbms, -14.81 MiB clickhouse) to this PR. The same false red hit 2 other PRs in the same hour a fix task is created (investigating at full effort, fixing-PR link to follow here)

Session id: cron:our-pr-ci-monitor:20261114-223011

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the Build (amd_msan) / Relocation headroom line of the ledger above: #131700. The baseline walk no longer skips a master commit whose build is still running.

arwenizEr pushed a commit to arwenizEr/ClickHouse that referenced this pull request Nov 18, 2026
…adroom baseline

`master_baseline` took the nearest commit of the run's master chain whose
`Build (amd_msan)` report existed and skipped every commit without one. A master
job uploads its report only in `post_run`, so a parent that is still building
looked like one that never builds, and the PR was compared with an older commit.
On 2026-11-14 ClickHouse#117346 (`Use clang-23`) moved the msan layout by -16.23 MiB; its
own master build ran until 20:42Z, and ClickHouse#124068, ClickHouse#131668 and ClickHouse#117318, checked a
few seconds earlier, were compared with the commit before it and failed with
exactly master's delta.

Apply the rule the performance delta gate uses for the same walk
(`classify_missing_prev_master_run`, 8b06c76): when the job report is
missing, read the commit's MasterCI summary and skip the commit only when MasterCI
never ran there, the run has no such job, or the job is `SKIPPED`. Any other
status, a report without the headroom row, or a lookup error other than 403/404
stops the walk: no baseline (the header names the commit and its status) or the
existing `ERROR`. A PR success without a baseline because the walk stopped is not
cached, since master's state is not a build digest input.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. and removed comp-testing Test content only (no production code). labels Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants