Skip to content

Fix flaky test 02465_limit_trivial_max_rows_to_read - #112199

Merged
PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-02465-limit-trivial-parallel-replicas
Jul 30, 2026
Merged

PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-02465-limit-trivial-parallel-replicas

Conversation

@groeneai

@groeneai groeneai commented Jul 27, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #111968

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

Fixed flaky test 02465_limit_trivial_max_rows_to_read.

Description

The statement SELECT a FROM t_max_rows_to_read LIMIT 1 has no ORDER BY, so its result is
unspecified: any row of the table is a legal answer. On a single reader one stream reads the
first granule first, so the answer happened to be 0 and the .reference file has asserted
that one particular unspecified answer since the test was written. Under parallel replicas the
read is split into per-replica mark ranges and whichever replica answers first supplies the row
that LIMIT 1 keeps, so the assertion is not stable.

The test therefore fails intermittently with a one-line reference diff, always the same wrong
value:

@@ -1,7 +1,7 @@
 0 / 0 / 1 / 2 / 3 / 4
-0
+32

32 is not a random row. On this lane all parts are on object storage, so
PartRangesReadInfo takes the remote branch where both
merge_tree_min_rows_for_concurrent_read_for_remote_filesystem and
merge_tree_min_bytes_for_concurrent_read_for_remote_filesystem default to 0, and
minMarksForConcurrentRead collapses to merge_tree_min_read_task_size = 8 marks per task.
With index_granularity = 4 the second task begins at mark 8, whose first row is
8 * 4 = 32. On a local disk the same formula yields one task covering the whole part, which
is why the failure only ever appears on the object-storage lanes.

The fix makes the asserted output invariant instead of pinning the reader: the statement now
projects a constant, so every legal row selection produces the reference value. The
.reference file is unchanged, because it already asserts 0.

What the line measures is unchanged, and I verified that rather than assuming it:

  • EXPLAIN still shows ReadFromMergeTree and ReadFromRemoteParallelReplicas, and
    EXPLAIN PIPELINE still shows MergeTreeSelect(pool: ReadPoolParallelReplicas, ...). The
    read is not eliminated: with no column referenced, chooseSmallestColumnToReadFromStorage
    injects the table's single column.
  • read_rows and ProfileEvents['SelectedMarks'] from system.query_log are identical
    before and after the change (4 and 25).
  • max_rows_to_read accounting is still live on the new form: lowering it to 3, 2 or 1 still
    raises TOO_MANY_ROWS, at exactly the same thresholds as the old form. This also holds with
    enable_analyzer = 0.

The assertion is also self-protecting, which is why a constant projection does not make it
vacuous: the line prints exactly one row only if a row was really read, so if the read were
ever eliminated the output would lose a line and the test would fail, and if the original
trivial-LIMIT accounting regressed the statement would raise TOO_MANY_ROWS and the test
would fail too. Only the identity of the returned row stopped being asserted.

max_rows_to_read = 20 and index_granularity = 4, which are what the test actually
measures, are untouched, and no setting is pinned, so nothing leaks into the following
statements. The two TOO_MANY_ROWS statements on this table keep exercising parallel-replica
limit enforcement on this lane.

I did not add -- Tags: no-parallel-replicas and did not add the test to
tests/parallel_replicas_blacklist.txt: either would remove the whole test from the
ParallelReplicas lane, where it otherwise runs green thousands of times, and only one of its
seven statements is affected. A blacklist entry would additionally report the test as
NOT_FAILED ("test succeeded but is listed in the blacklist") on every passing run.

The same shape is already used in the tree for the same reason, in
tests/queries/0_stateless/01913_exact_rows_before_limit.sql.

CI report. The failure this fixes, on the lane it appears on:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111968&sha=3084113c10b2726a333dc26732034c039ac441ab&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29
There is no open issue for this test; I searched and found none, so there is nothing to close
here. Over the last 30 days the test failed this way on four unrelated pull requests and never
on master, always on the object-storage ParallelReplicas lanes.

Reproduction. An object-storage server with a three-replica parallel-replicas cluster.
With the test's own settings the broken form returned 32 on 1 of 2000 runs, and the fixed
form returned 0 on 2000 of 2000. With parallel_replicas_local_plan = 0, which the test
runner randomizes, the rate rises: the broken form returned 32 on 7 of 200 runs and the
fixed form on 0 of 200. clickhouse-test --s3-storage --test-runs 50 is green, as is a run
with enable_analyzer = 0.

Not fixed here. The same statement has a second, unrelated failure mode on the
distributed plan variant of this lane: TOO_MANY_ROWS max rows: 20.00, current rows:
followed by 24.00, 25.00 or 100.00. That is per-replica limit accounting rather than row
identity, so an output-invariance change cannot address it, and all three occurrences over the
last 90 days are confined to one open pull request that changes exactly that machinery.

Version info

  • Merged into: 26.8.1.433 (included in 26.8 and later)

groeneai added 3 commits July 27, 2026 19:20
The statement `SELECT a FROM t_max_rows_to_read LIMIT 1` has no ORDER BY, so
its result is unspecified: any row of the table is a legal answer. On a single
reader the first granule is read first, so the answer happened to be 0 and the
reference file has asserted that one particular unspecified answer since the
test was written. Under parallel replicas the read is split into per-replica
mark ranges and whichever replica answers first supplies the row that LIMIT 1
keeps, so the assertion is not stable and the test fails intermittently with a
one-line reference diff of 32 instead of 0.

32 is not a random row. On the failing lane all parts are on object storage, so
PartRangesReadInfo takes the remote branch where both
merge_tree_min_rows_for_concurrent_read_for_remote_filesystem and its bytes
counterpart default to 0, and minMarksForConcurrentRead collapses to
merge_tree_min_read_task_size = 8 marks per task. With index_granularity = 4
the second task begins at mark 8, whose first row is 8 * 4 = 32. On a local
disk the same formula yields a single task covering the whole part, which is
why the failure only appears on the object-storage lanes and why earlier
local-disk reproduction attempts could only ever observe 0.

Project a constant instead, so every legal row selection produces the reference
value. The reference file does not change, because it already asserts 0.

What the line measures is unchanged, verified rather than assumed: EXPLAIN
still shows ReadFromMergeTree and ReadFromRemoteParallelReplicas, read_rows and
ProfileEvents['SelectedMarks'] are identical before and after (4 and 25), and
lowering max_rows_to_read to 3, 2 or 1 still raises TOO_MANY_ROWS at exactly
the old thresholds, on both analyzers. The read is not eliminated because
chooseSmallestColumnToReadFromStorage injects the table's single column.

max_rows_to_read = 20 and index_granularity = 4, which are what the test
actually measures, are untouched, and no setting is pinned, so nothing leaks
into the following statements. A no-parallel-replicas tag or a
parallel_replicas_blacklist entry was deliberately avoided: either would remove
the whole test from that lane, where it otherwise runs green thousands of
times, while only one of its seven statements is affected.
The comment above the changed statement carried the concrete value observed in
CI and the mark arithmetic behind it. That context depends on the default of
merge_tree_min_read_task_size, so it would become wrong while the test stays
correct, and it duplicates what the commit message of the previous commit and
the pull request description already explain in full.

Keep only what a reader cannot infer from the code: the result is unspecified
without ORDER BY because parallel replicas split the read into per-replica mark
ranges, a constant is projected for that reason, and max_rows_to_read is what
the line measures.

No behaviour change: the statement, the settings and the reference file are
untouched.
The failure was reported with a CI report link, so record it here as well as in
the pull request description.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111968&sha=3084113c10b2726a333dc26732034c039ac441ab&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29
Pull request the report belongs to:
ClickHouse#111968

No change to the tree: the fix and its test edit are in the previous commits.
@groeneai

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

An independent reviewer and a second model reviewed this change over three rounds before it was
opened. Two rounds sent it back for changes and the third found nothing. Eight findings were
raised, every one was agreed with and none was overridden; the seven that affected the shipped
change or this description are below (the eighth corrected an internal note of my own).

# Finding Verdict
⚠️ The in-test comment carried incident context (the observed value and its task position) that duplicates the commit message and depends on the current default of merge_tree_min_read_task_size, so it would go stale while the test stayed correct. Agreed and fixed: the comment now states only the durable invariant, that LIMIT 1 without ORDER BY may return any row under parallel replicas and that a constant is projected for that reason. The narrative stays in the commit message and in the description.
⚠️ The maintainer-supplied CI report link was recorded only in the description, not in the commit history. Agreed and fixed: a separate commit records the report URL and the pull request it belongs to. The tree is unchanged by that commit.
⚠️ The description's only provenance link sat inside the template's HTML comment block, so it did not render and produced no cross-reference. Agreed and fixed: the Related: line is now rendered text.
⚠️ The description did not carry the CI report URL, and did not say whether an open issue exists for this test. Agreed and fixed: the description now links the canonical report and states that no open issue exists.
⚠️ The description said "the three TOO_MANY_ROWS statements", but the file has four, and only two of them are on the table where a replica choice exists. Agreed and fixed: the sentence now says two, and scopes them to this table.
💡 The description's paragraph about the unrelated failure mode quoted one of the three current rows values that mode reports. Agreed and fixed: the description now lists all three.
💡 The description argued that the assertion is not vacuous only from measurements, and omitted the structural argument. Agreed and fixed: the description now also notes that the line prints a row only if a row was read, so read elimination or a regression of the trivial-LIMIT accounting would each fail the test.

The substance of the change was verified independently rather than taken on trust: that the
trivial-LIMIT path is projection-agnostic on both analyzers, that the read cannot be
eliminated because the smallest physical column is injected when the projection references
none, that max_rows_to_read accounting is driven by the LIMIT rather than by the select list,
that parallel replicas require a MergeTree table so the two numbers statements can never be
affected, and that the mark arithmetic behind the observed value follows from the remote-disk
branch of the read-size calculation. All four recorded occurrences of the failure were read line
by line: each moves only the last reference line and each reports the same value.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, with the exact CI value 32. Object-storage server plus a three-replica parallel-replicas cluster. At the test's own settings the broken statement returned 32 on 1 of 2000 runs (12-way concurrent), matching the ~0.17% rate measured on the failing lane. With parallel_replicas_local_plan = 0, a state the test runner randomizes, the rate rises to 7 of 200 runs (3.5%).
b Root cause explained? Yes. SELECT a FROM t_max_rows_to_read LIMIT 1 has no ORDER BY, so its result is unspecified and any row is a legal answer; under parallel replicas the read is split into per-replica mark ranges and whichever replica answers first supplies the row, while the .reference asserts one particular unspecified answer. 32 is not random: on object storage merge_tree_min_rows/bytes_for_concurrent_read_for_remote_filesystem both default to 0, so minMarksForConcurrentRead returns merge_tree_min_read_task_size = 8 marks per task, and with index_granularity = 4 the second task starts at row 8 * 4 = 32. On a local disk the same formula yields a single task, which is why only the object-storage lanes fail.
c Fix matches root cause? Yes. The defect is that the test asserts an unspecified result, so the fix removes the unspecifiedness from the assertion (project a constant) rather than removing the parallel-replica read from the run. No assertion bound is widened, no dataset reduced, no no-random-* tag added and no setting pinned. max_rows_to_read = 20 and index_granularity = 4 are byte-identical, and the .reference file does not change.
d Test intent preserved / new tests added? Preserved, and measured rather than assumed: read_rows and ProfileEvents['SelectedMarks'] are identical before and after (4 and 25), EXPLAIN still shows ReadFromMergeTree and ReadFromRemoteParallelReplicas, the trivial-LIMIT path is still taken, and lowering max_rows_to_read to 3, 2 or 1 still raises TOO_MANY_ROWS on the new form at exactly the old thresholds. Six of the seven statements are untouched. The changed test is itself the regression test.
e Both directions demonstrated? Yes, in two arms. At the shipped test settings: broken 1 of 2000 runs returned 32, fixed 0 of 2000. At the higher-rate randomized state: broken 7 of 200, fixed 0 of 200. Additionally clickhouse-test --s3-storage --test-runs 50 is 50/50 green and a run with enable_analyzer = 0 is 20/20 green.
f Fix is general across code paths? N/A for production code, since this is a test-only change. The carrier enumeration was done anyway: the two numbers(...) printing statements plan as ReadFromSystemNumbers with no distributed read, so no replica choice exists for them; the four serverError TOO_MANY_ROWS statements assert an error code rather than a row; and all six sibling max_rows_to_read tests were run, none of which needs the same change.
g Fix generalizes across inputs (params/datatypes/wrappers)? Output invariance is a property of the query, not of any setting, so it holds for every combination of the relevant knobs. Verified by sweeping parallel_replicas_local_plan against merge_tree_min_read_task_size (10 cells) and parallel_replicas_mark_segment_size over {1, 2, 4, 8, 16, 128}: the fixed form returned the reference value in every cell. Also verified on both analyzers.
h Backward compatible? (maintainer-approved exception only) N/A. Test-only change: no production code, no setting default, no serialization format, so no SettingsChangesHistory.cpp entry and no experimental gate.
i Invariants and contracts preserved? Yes. The invariant is that a statement whose result is unspecified without ORDER BY must not be asserted against a fixed reference while parallel replicas may serve it from several replicas. No setting is pinned, so nothing leaks into the following statements, and the .reference file is unchanged, so no other assertion moves.

@groeneai groeneai mentioned this pull request Jul 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai — could you review this? Test-only: SELECT a FROM t_max_rows_to_read LIMIT 1 has no ORDER BY, so under parallel replicas on object storage the second read task starts at row 32 and the .reference was asserting one particular unspecified answer; the statement now projects a constant so the assertion is invariant, with max_rows_to_read accounting and the read itself verified unchanged.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jul 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d6474e8]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, parallel) FAIL
04357_distributed_alias_same_expression FAIL cidb IGNORED

AI Review

Summary

This PR removes a flaky assertion in 02465_limit_trivial_max_rows_to_read by changing the unordered LIMIT 1 query on a MergeTree table to project a constant instead of a table column. I did not find a correctness or coverage regression in the current code: with an empty projection, both the planner path (PlannerJoinTree::chooseSmallestColumnToReadFromStorage) and the old-analyzer path (TreeRewriter::collectUsedColumns) still force one physical column read to determine row count, so the query continues to exercise MergeTree reading and max_rows_to_read enforcement while no longer asserting an unspecified row identity.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - d6474e8

CI fully finished on this head: 174 check-runs, 0 queued or in progress, Finish Workflow pass,
Config Workflow pass. 1 failure. Every failure below has an owner.

Check / test Reason Owner / fixing PR
Stateless tests (amd_debug, parallel) / 04357_distributed_alias_same_expression un-merged GROUP BY groups on a duplicate-ALIAS Distributed query under two-level aggregation (-300 300 2 becomes 300 300 1 twice); runner rerun 18/75 failed fixing PR #108855 (mine, open)

The failing hunk is @@ -61,17 +61,18 @@ and the only changed lines are the three 300 300 * ones,
with the drawn settings including group_by_two_level_threshold_bytes 1 and
prefer_localhost_replica 0. That is exactly the mechanism #108855 fixes: shards collapse the
duplicate GROUP BY keys and compute two-level bucket numbers from one key while the initiator merges
on two, so equal groups land in different buckets and never merge. Not caused by this PR, whose diff
is a single unrelated stateless .sql file.

Session id: cron:our-pr-ci-monitor:20260728-130000

@PedroTadim PedroTadim self-assigned this Jul 30, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Jul 30, 2026
Merged via the queue into ClickHouse:master with commit b719314 Jul 30, 2026
176 of 178 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 Jul 30, 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 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