Skip to content

Fix flaky test 05829_read_by_layers_reads_only_key_prefix - #133653

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-05829-statistics-cache-clear
Nov 23, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-05829-statistics-cache-clear

Conversation

@groeneai

@groeneai groeneai commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #132720
Related: #119664

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

05829_read_by_layers_reads_only_key_prefix (added in #132720) printed 05829_join 0 in Stateless tests (arm_asan_ubsan, targeted) on #119664: the measured merge join had ReadCompressedBytes 426184, while the 41 other runs of the test in that job had 17100 or 11924.

The excess was the column statistics, not the read: SelectedMarks, read_rows and read_bytes equal the passing runs, and the join had StatisticsCacheMisses 2. The EXPLAIN PIPELINE before it normally loads the statistics, so the measured join hits the statistics cache. Here the concurrent 05081_ill_typed_skip_index_expression_attach.sh dropped an ENGINE = Ordinary database in between; for a table without a UUID MergeTreeData::dropAllData calls Context::clearCaches, which empties the server-wide statistics and mark caches, so the join loaded the statistics again. The randomized auto_statistics_types included uniq (about 408 KB of statistics), and the randomized server codec ZSTD(1) puts the threshold at about 326 KB (about 597 KB with LZ4).

The table is now created with auto_statistics_types = '', so neither measured query has statistics to load. Test-only change, the reference is unchanged.

Validation (master debug build, CI test configs with ZSTD(1), the failing run's statistics types): with SYSTEM DROP STATISTICS CACHE or an Ordinary database drop between the EXPLAIN and the join, the unfixed test prints 0 in 5/5 runs and the fixed one 1 in 5/5. Randomized runs while a loop empties the statistics, mark and primary index caches about every 40 ms: 9/100 failed before, 0/250 after.


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

Version info

  • Merged into: 26.12.1.250-master (included in 26.12 and later)

The measured merge join loads the column statistics of the table on a
statistics cache miss, and the compressed bytes of that load count in
`ReadCompressedBytes`, which the test compares with a tenth of the
compressed size of `s`. The `EXPLAIN PIPELINE` before the join normally
warms the statistics cache. A concurrent test that drops a table in an
`Ordinary` database empties the server-wide caches in between
(`MergeTreeData::dropAllData` calls `Context::clearCaches` for a table
without a UUID), so the join loads the statistics again. With the
randomized `uniq` statistic (about 408 KB here) and the randomized server
codec `ZSTD(1)` (threshold about 326 KB) the oracle prints 0. The read
itself is unchanged.

Create the table without statistics, so the measured queries count only
their column reads.

Related: ClickHouse#132720, ClickHouse#119664

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 of the diff (1 finding).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test does not assert that the table has no statistics, so dropping the pin would still pass DISAGREE The flake needs a concurrent server-wide cache clear, which a parallel-safe test cannot force, so no in-test check fails on it deterministically. Measured instead: 9/100 failures before and 0/250 after under a cache-clear loop. An explicit table setting always beats the randomized one (0 statistics cache events in 300 randomized runs). 00804, 00961 and 02381 use the same pin without asserting it.
2 💡 The PR description gave the wrong period for the cache-clear loop AGREE, fixed in the description none

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

Session id: cron:clickhouse-review-slot-8:20261123-023200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano, could you review this? It sets auto_statistics_types = '' on the table of 05829_read_by_layers_reads_only_key_prefix (added in #132720): a concurrent Ordinary database drop cleared the statistics cache between the EXPLAIN and the measured join, so the join reloaded the randomized column statistics and their compressed bytes broke the ReadCompressedBytes threshold. The read itself is unchanged.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The first half of the test with the failing run's auto_statistics_types (uniq,basic,uniq_v2), the ZSTD(1) codec and SYSTEM DROP STATISTICS CACHE (or an Ordinary database drop) between the EXPLAIN and the measured join prints 05829_join 0 in 5/5 runs (ReadCompressedBytes 420384, threshold 325770). The real test fails 9/100 randomized runs while a loop empties the statistics, mark and primary index caches.
b Root cause explained? Yes. The measured join loads the column statistics on a statistics cache miss, and their compressed bytes count in ReadCompressedBytes. A concurrent Ordinary database drop calls Context::clearCaches between the EXPLAIN (which normally warms the cache) and the join; with the randomized uniq statistic (~408 KB) and the ZSTD(1) server codec the load exceeds data_compressed_bytes(s) / 10. The read itself is unchanged.
c Fix matches root cause? Yes. auto_statistics_types = '' on the measured table removes the statistics, so no cache state can add bytes to the measured queries. No bound widened, no blanket no-random-* tag.
d Test intent preserved / new tests added? Yes. The oracle still checks that only the key prefix columns are read; the EXPLAIN checks and the reference are unchanged. Test-only fix.
e Both directions demonstrated? Yes. Repro: 5/5 0 before, 5/5 1 with the pin (ReadCompressedBytes 12294, no statistics cache events). Real test under the cache-clear loop: 9/100 failed before, 0/250 after; 50/50 without the loop.
f Fix is general across code paths? N/A (test-only). Both byte oracles of the test (05829_join, 05829_uniq_exact) read the same table, so the pin covers both.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only). A table setting beats every randomized auto_statistics_types draw and every server codec: 0 statistics cache events in 300 randomized runs.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change.
i Invariants and contracts preserved? Yes. ReadCompressedBytes of each measured query now counts only the column reads, whatever the caches hold.

Session id: cron:clickhouse-impl-slot-4:20261123-021400

@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8b37792]

Summary: ✅


AI Review

Summary

The PR disables automatic column statistics on t_layers_prefix so statistics cache misses cannot inflate ReadCompressedBytes and cause false failures. The existing pipeline checks, byte thresholds, and reference output remain intact. The documented deterministic and randomized before/after reproductions support this focused test fix. No actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 23, 2026
@Algunenano Algunenano self-assigned this Nov 23, 2026
@Algunenano
Algunenano added this pull request to the merge queue Nov 23, 2026
Merged via the queue into ClickHouse:master with commit d374f96 Nov 23, 2026
388 of 389 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 23, 2026
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-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-ci 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