Skip to content

Ship a LIMIT without ORDER BY to replicas in plan-based parallel replicas - #129811

Merged
devcrafter merged 7 commits into
masterfrom
plan-based-ship-limit
Nov 10, 2026
Merged

devcrafter merged 7 commits into
masterfrom
plan-based-ship-limit

Conversation

@devcrafter

@devcrafter devcrafter commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Related: #128413

Plan-based parallel replicas (parallel_replicas_plan_based) did not ship a LIMIT without ORDER BY to the replicas. The split marker is lifted through expressions, filters, aggregation and sorting (an ORDER BY ... LIMIT already ships a local top-N), but not through a LimitStep, so the LIMIT stayed on the initiator. Every replica then read its whole share of the table until the initiator cancelled it.

A cancelled replica never reports its progress. With a large interactive_delay this is what makes 02841_parallel_replicas_summary fail in the plan-based job added in #128413: the CI query_log of the failing run shows the replica cancelled after reading 100000 rows, and the initial query with read_rows = 0.

This adds a copy of the LIMIT, with limit + offset rows, to the fragment (Limit (local LIMIT)), and keeps the original step on the initiator, which applies the offset and the final limit. Classic parallel replicas gets the same effect by sending the LIMIT in the query. The copy is not shipped with WITH TIES, when a LIMIT must read till the end (exact_rows_before_limit), for LIMIT 0, or when limit + offset overflows.

The limits that optimizations attach to the read itself do not stop it: ReadFromMergeTree does not use SourceStepWithFilterBase::limit, and trivial_limit only tunes the read (row estimate, read pool). Shipping trivial_limit alone was tried, and the replicas still read until cancelled.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


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

Version info

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

…replicas

The split marker of plan-based parallel replicas was lifted through expressions, filters, aggregation and
sorting, but not through a `LimitStep`, so a `LIMIT` without `ORDER BY` stayed on the initiator. Every
replica then read its whole share of the table and was cancelled by the initiator once it had enough rows.
A cancelled replica never reports its progress, which made `02841_parallel_replicas_summary` fail in the
plan-based job.

Add a copy of the `LIMIT` with `limit + offset` rows to the fragment, as classic parallel replicas does by
sending the `LIMIT` in the query, and keep the original step on the initiator. It is not shipped with
`WITH TIES`, when a `LIMIT` must read till the end (`exact_rows_before_limit`), or when `limit + offset`
overflows.

The limits that optimizations attach to the read itself are not enough: `ReadFromMergeTree` does not use
`SourceStepWithFilterBase::limit`, and `trivial_limit` only tunes the read (row estimate, read pool), so
only a `LimitStep` in the replica's pipeline stops its read early.

Related: #128413

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d93dbe4]

Summary: ✅


AI Review

Summary

Reviewed d93dbe4da446f4899b568b28b03866d16e5dd91c. The change ships a per-replica limit + offset bound while retaining the final LIMIT and offset on the initiator. The previous blocker is fixed: the copied LimitStep uses only the matched limit's alwaysReadTillEnd flag, and the revised plan assertion covers the regression deterministically. No remaining actionable findings.

Missing context / blind spots
  • ⚠️ No local ClickHouse executable was available for rerunning tests. The current workflow's full replica and sanitizer suites remain pending; the fast test passed. Earlier review discussion records execution checks for exact row counts and both local-plan modes on the unchanged C++ revision. Full CI should confirm the final test assertion change.
Final Verdict

✅ Approve — no unresolved review findings. No new inline comments are needed.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 6, 2026
devcrafter added a commit that referenced this pull request Nov 6, 2026
… slowed local replica

Instead of switching off `parallel_replicas_local_plan`, slow the local replica down with the
`slowdown_parallel_replicas_local_plan_read` failpoint, so the remote replicas always take part in the query.

Pin `parallel_replicas_plan_based = 0` for now: plan-based parallel replicas does not ship a `LIMIT` without
`ORDER BY` to the replicas yet, so a remote replica reads until the initiator cancels it, and a cancelled
replica never reports its progress. The `interactive_delay = 99999999999` query then reports `read_rows = 0`
(3 of 40 local runs failed that way with the plan-based profile). Remove the pin once
#129811 is merged; with it the test passed 30 of 30.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128413&sha=4190a1cf64c1c5b703453654c52118dcab70ffe0&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20plan-based%2C%20s3%20storage%2C%20parallel%29
Related: #129811

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Nov 6, 2026
@devcrafter
devcrafter requested a review from groeneai November 6, 2026 20:30
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing d93dbe4da with master 39da336ee (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Compile time of recompiled translation units

7 translation units recompiled, 17 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flaky check is red in amd_msan and amd_tsan on 550a394, and the cost is one arm: SELECT 1 AS x FROM t_pr_ship_limit LIMIT 5 FORMAT JSON SETTINGS exact_rows_before_limit = 1. The LIMIT is not shipped there, so each replica streams its whole share back to the initiator, and that is slow when the random MergeTree settings make the part Wide (min_bytes_for_wide_part = 0). From the jobs' query_log.tsv: under tsan that statement took 235 s median in all 17 runs with a Wide part, and at most 4.4 s in the other 33; under msan 352 s median in all 11 such runs, at most 2.2 s in the other 22. Locally (debug, nothing else running) it is 2.5 s with a Wide part and 32 ms with a Compact one.

I think the fix belongs in the code rather than the test: ship the copy in that case too, with always_read_till_end, and mark it as a shard limit, like the local-shard preliminary LIMIT (Planner.cpp#L1777-L1778). The replica still reads everything but returns only limit + offset rows, and rows_before_limit_at_least stays exact. Suggestion inline. I checked it on a debug build of 550a394 with the 3-replica localhost cluster:

  • rows_before_limit_at_least is exact with parallel_replicas_local_plan 0 and 1, Wide and Compact parts: 1000000 for the test's query, 333334 for WHERE n % 3 = 0 LIMIT 10 OFFSET 20, 100 for SELECT * FROM (... LIMIT 100) LIMIT 5. Each replica returns 5 rows instead of its share.
  • The shard mark is needed: without it, with the local plan the count is 606789 (131102 for the filtered query), because the initiator's counter does not pass a non-shard LIMIT (QueryPipeline.cpp#L253).
  • 05546 then differs only in the expected exact_rows_before_limit 1 line (and the section title above it). 02841, 03174, 04504, 04509, 04510, 04836, 05212, 05256 pass.
  • 05546 at 18 workers with random settings: max 1.69 s over 48 runs, against 5.45 s over 36 runs on 550a394.

If you prefer to keep the exclusion, that arm needs a smaller table (or a pinned part type) to stay under 180 s in the flaky check.

… the plan fragment

An outer `WITH TOTALS` or exact `LIMIT` made the `Limit` under an inner `LIMIT` read till the end,
so the local replica drained its whole share.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@devcrafter
devcrafter requested a review from groeneai November 8, 2026 17:47

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked 0c84e22 on a local debug build; the change is right. limitAlwaysReadsTillEnd and addPreliminaryLimitStep take the flag from exact_rows_before_limit at each query level, so the matched step still carries it when it is needed:

  • rows_before_limit_at_least with exact_rows_before_limit = 1 equals the non-parallel value at parallel_replicas_local_plan 0 and 1, Wide and Compact parts (1000000 plain, 333334 with a filter and OFFSET, 100 over an inner LIMIT 100).
  • The new arm's query reads 128 rows; with || mustReadTillEnd() restored a replica read all 100000. An inner LIMIT with SETTINGS exact_rows_before_limit = 0 under an outer exact_rows_before_limit = 1 also stops at 128.
  • 02841 and 05546 pass 10/10 each with plan-based parallel replicas forced at local plan 0 and 1; 05546 passes 50/50 with 18 workers and random settings (max 0.60 s).

One suggestion for the new arm, inline: as written it does not reliably catch the old behaviour.

A cancelled replica does not report its progress, so the read rows did not catch the old behaviour reliably.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@devcrafter
devcrafter added this pull request to the merge queue Nov 10, 2026
Merged via the queue into master with commit 5b43a39 Nov 10, 2026
368 of 369 checks passed
@devcrafter
devcrafter deleted the plan-based-ship-limit branch November 10, 2026 15:01
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 10, 2026
devcrafter added a commit that referenced this pull request Nov 10, 2026
Remove the `parallel_replicas_plan_based=0` pins: the `LIMIT` is shipped to the replicas since #129811.
Wait for the replicas' `query_log` entries, not only the initiator's. Regenerate the workflows after the merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
devcrafter added a commit that referenced this pull request Nov 10, 2026
Add the subquery query result cache tests: the cache is not used with plan-based parallel replicas since #130376.
Add `03707_autopr_unsupported_cases` (the `LIMIT` is shipped since #129811) and `03401_..._no_sorting` (#128537).
Remove the tests that pass now: the temporary table ones (#129223), the `LIMIT` ones (#129811), and those fixed on master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
devcrafter added a commit that referenced this pull request Nov 11, 2026
…for the replicas' `query_log` entries

The failpoint slowed the local replica by 20 ms per request, which is not enough under load: the local
replica read the whole `LIMIT` alone, and no remote replica ran. With `parallel_replicas_local_plan = 0`
every read is a remote query. Plan-based parallel replicas passes it since #129811 ships the `LIMIT`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants