Skip to content

Fix flaky test 05059_top_k_dynamic_filter_shares_prewhere on object storage - #123123

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-05059-topk-s3-streams
Oct 1, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-05059-topk-s3-streams

Conversation

@groeneai

@groeneai groeneai commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #117968
Related: #122671

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):

Description

The third on/off comparison of 05059_top_k_dynamic_filter_shares_prewhere failed on master in Stateless tests (amd_tsan, s3 storage, parallel, 3/3) (report): reference line 9 expected 1, got 0.

That check compares ORDER BY grp, rowNumberInBlock() LIMIT 20 with use_top_k_dynamic_filtering off and on. The sort key is not unique: rowNumberInBlock() repeats across blocks, and with the failing run's settings two grp = 0 rows tie exactly at the LIMIT cut. On object storage the part is read in two streams (the remote concurrency thresholds default to 0), on local disk in one. The two tied rows are in different streams' shares, and a stream with nothing left takes the other stream's untaken share, so which row is kept depends on timing, in each arm separately. The filter is not involved: it is refused for this query and EXPLAIN shows no __topKFilter.

The fix adds max_threads = 1 to both arms of that one check, so it is read in one stream on every storage, as local disk already does. The other on/off checks sort by a unique key and are unchanged.

Validation, on an S3 table (MinIO) in clickhouse local with the failing run's settings and ThreadFuzzer lock delays: the old check returned 0 14 times in 150, and 18 times in 150 with the filter off in both arms. The fixed check returned 1 300/300 under the same delays and 500/500 without them. With the planner's refusal for this sort expression deleted, the fixed check returns 0 at max_block_size 8000, 29226 and 50000 on both disks, so it still catches the filter. The test passes 50/50 with randomized settings on a local-disk server and 50/50 on an S3-default server.


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

Version info

  • Merged into: 26.10.1.1157-master (included in 26.10 and later)

…torage

The third on/off comparison orders by `grp, rowNumberInBlock()`, which is not
a total order: `rowNumberInBlock()` repeats across blocks, and with some block
sizes two `grp = 0` rows tie at the LIMIT cut. On object storage the 13-mark
part is read in two streams (the remote concurrency thresholds default to 0,
so `min_marks_per_task = 8` splits it into marks 0-7 and 8-12), while on local
disk it is read in one. Each stream takes its whole share as one task, and a
stream with nothing left takes the other stream's untaken share, so which of
the tied rows survives depends on timing, independently in each arm. The top-K
filter is not involved: it is refused because the expression below the sort
computes the stateful `rowNumberInBlock()`.

Failed on master in `Stateless tests (amd_tsan, s3 storage, parallel, 3/3)` at
852f246. Pin `max_threads = 1` on both arms of that comparison so it is
read in one stream on every storage. A build without the refusal for a
block-dependent expression still makes the pinned comparison return 0 at
`max_block_size` 8000, 29226 and 50000.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Sep 30, 2026
@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; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ⚠️ With one stream and max_block_size = 100000 the part is one chunk, so the pinned check cannot catch a wrongly installed filter DISAGREE The pin changes the stream count only on object storage, where the check with the refusal deleted returns 0 at max_block_size 8000, 29226, 50000, 70000 and 99999; local disk was already one stream.

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

Session id: cron:clickhouse-review-slot-8:20260930-180700

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? On an S3 table with the failing run's settings the read uses 2 streams and grp = 0 ties at the LIMIT cut; with ThreadFuzzer lock delays the old check returns 0 14/150 (18/150 with the filter off in both arms).
b Root cause explained? ORDER BY grp, rowNumberInBlock() is not a total order, and with two streams which tied row survives LIMIT 20 depends on timing; the filter is refused for this query (no __topKFilter in EXPLAIN).
c Fix matches root cause? max_threads = 1 on both arms of that check gives one stream, as on local disk. No tag, no widened bound.
d Test intent preserved / new tests added? Yes: with the stateful-sort-expression refusal deleted from optimizeTopK.cpp, the fixed check returns 0 at max_block_size 8000, 29226, 50000 and 70000 on S3 and local disk.
e Both directions demonstrated? Same harness: old 14/150 0, fixed 300/300 1; without delays fixed 500/500; full test 50/50 on local-disk and S3-default servers.
f Fix is general across code paths? The other result lines sort by unique keys or are EXPLAIN checks. N/A for product code (test-only).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only).
h Backward compatible? (maintainer-approved exception only) N/A (no settings, format or behavior change).
i Invariants and contracts preserved? N/A for product code; two executions of an arm now return the same rows on every storage.

Session id: cron:clickhouse-impl-slot-6:20260930-161100

@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [39bd869]

Summary: ✅


AI Review

Summary

This PR makes the flaky 05059_top_k_dynamic_filter_shares_prewhere comparison deterministic on object storage by pinning max_threads = 1 for the one equality check whose ORDER BY grp, rowNumberInBlock() ties were previously sensitive to multi-stream read timing. I reviewed the current diff, the surrounding test, the PR discussion, and prior inline threads, and I did not find a remaining correctness or coverage issue in the change.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci comp-testing Test content only (no production code). labels Sep 30, 2026
@Algunenano Algunenano self-assigned this Oct 1, 2026
@Algunenano
Algunenano added this pull request to the merge queue Oct 1, 2026
Merged via the queue into ClickHouse:master with commit 37c7e09 Oct 1, 2026
149 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 1, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... and removed comp-testing Test content only (no production code). labels Nov 22, 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-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding 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