Repository navigation
Fix distributed TopK planning with prefer_localhost_replica - #110136
alexey-milovidov merged 2 commits into
Conversation
|
cc @KochetovNicolai -- Hi! I see you're assigned to the issue this PR hopefully resolves. Would you be able to recommend a reviewer for this? |
|
Workflow [PR], commit [6e2b0b5] Summary: ✅
AI ReviewSummaryThis PR keeps preliminary shard Tests
Final VerdictStatus: Minimum required actions:
|
Keep shard limits in local plans so lazy materialization and dynamic TopK match remote shards. Mark shard limits so rows-before-limit accounting can traverse them without crossing limits written in the query. Disable TopK source skipping when exact rows-before-limit accounting is requested. Related: ClickHouse#89769
43bfbf6 to
35cff6a
Compare
…-lazy-materialization
| { | ||
| auto limit = std::make_unique<LimitStep>( | ||
| query_plan.getCurrentHeader(), lim_info.limit_length, lim_info.limit_offset, settings[Setting::exact_rows_before_limit]); | ||
| if (options.is_local_shard_plan) |
There was a problem hiding this comment.
This legacy path is part of the fix, but the only new regression file is tagged no-old-analyzer and runs with enable_analyzer = 1, so we never prove that the non-analyzer Distributed path still reports rows_before_limit_at_least correctly here. Since this PR is a Bug Fix for stable user-visible behavior, please add one small legacy case as well (it can just assert the statistics; it does not need lazy materialization / TopK) or explain why this branch is unreachable for the reported bug.
| /// how constant node names are generated (using source expression instead of _CAST wrapper), | ||
| /// leading to column name mismatches with the expected header. | ||
| auto select_query_options = SelectQueryOptions(processed_stage); | ||
| select_query_options.is_local_shard_plan |
There was a problem hiding this comment.
This adds the same shard-limit accounting contract to the local parallel-replicas path, but the new test only checks EXPLAIN for this branch. Because ReadFromLocalParallelReplicaStep extracts and re-optimizes the local plan before execution, plan shape alone does not prove that rows_before_limit_at_least is still counted through the hidden shard limit at runtime. Please add a small exact_rows_before_limit = 1 result/statistics check for the parallel-replicas case too.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 197/211 (93.36%) · Uncovered code |
|
@clickgapai, complete the test according to the review comments. |
|
Both comments point to the same gap: the regression file only covered the analyzer path (and only via
Expected counts: two-shard → Note: I wasn't able to run the cluster/parallel-replica paths end-to-end locally, so CI will exercise those; the Happy to fold this into the existing regression file instead of a standalone test if you'd prefer. |
|
Done. Both review gaps are now covered at runtime (asserting the statistic, not just plan shape), following @clickhouse-gh's two comments: - Legacy non-analyzer Distributed path (comment 1): (trimmed for brevity) |
|
Completed. Here's the runtime regression test covering both review comments, plus how I verified it. Verification (on this dev server, current master = pre-fix): the standard CI clusters aren't configured here (only
So current master silently reports the wrong statistic on the local shard on both analyzer paths (stable across 2 runs). This test asserts Comment 2 (parallel-replicas local plan): I couldn't execute Test uses |
|
@alexey-milovidov done — opened a separate PR from master with the runtime |
… the statistic The runtime `rows_before_limit_at_least` assertion for parallel replicas came back as 0 instead of 30. The local plan never marks its preliminary LIMIT as a shard limit -- the marking added in ClickHouse#110136 is gated on a processing stage that parallel replicas never use -- so `initRowsBeforeLimit` treats it as a query-level LIMIT, stops the walk there, and counts only the remote replicas. Reported as ClickHouse#113279. Assert the statistic with `parallel_replicas_local_plan = 0` until that is fixed, and say in the test that the local-plan path is left uncovered. Also tag the test `no-parallel-replicas`: the expected counts depend on the read topology, which the ParallelReplicas job overrides globally. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…110136-runtime-rows-before-limit Add runtime rows_before_limit coverage for distributed TopK planning (follow-up to ClickHouse#110136)
…local-lazy-materialization Fix distributed TopK planning with prefer_localhost_replica
…terialization Conflict resolution notes: - `LimitStep`: master moved plan-serialization changes from the global version to per-step versions, so the shard-limit and distributed Top-K candidate markers are now version 1 of the `Limit` step (`DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_SHARD_LIMIT` = 21, the value of release 26.11). Serializing a candidate limit for an older peer throws instead of silently dropping the marker. Master's new column pruning keeps the candidate sort columns. - Protocol: master is at revision 54495, so query coordination moves to 54496. - `RemoteQueryExecutor`: master restructured `read`, `readAsync`, `sendQueryAsync` and the parallel-replicas packet path with `SCOPE_FAIL` fragment spans. The PR's coordinator cancellation on exceptions is kept in thin wrappers around `*Impl` bodies, and the coordination-request handling is re-applied on master's bodies. - `ReadFromRemote`: both the unavailable-shard tracker (master) and the Top-K participant (PR) are passed to `addPipe` / `addLazyPipe`; Top-K coordination keeps `GET_ONE`. - `optimizeTree`: master moved join conversion after `applyParallelReplicas`; the `planReadsFromRemote` guard now feeds both the join settings and master's new `make_distributed_plan` computation. - `getThrottler` moved from `ClusterProxy` to `RemoteQueryExecutor` on master; the PR's exported copy is dropped. - The old analyzer was removed on master, so the `allow_experimental_analyzer` gates added by the PR are dropped too. - Changes taken from ClickHouse#110136 (now merged to master) resolve to master's version. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…replicas With parallel replicas and the default `parallel_replicas_local_plan = 1`, `rows_before_limit_at_least` dropped every row read by the initiator's own replica (0 instead of 30 in the repro of ClickHouse#113279), with or without `exact_rows_before_limit`. `initRowsBeforeLimit` counts through a nested `LimitTransform` only if it is a shard limit. The local plan builders mark their preliminary LIMIT as a shard limit (`is_local_shard_plan`, ClickHouse#110136) only at `WithMergeableStateAfterAggregationAndLimit`, but parallel replicas always build the local replica plan at `WithMergeableState`, so the local preliminary LIMIT was a counting boundary and only the remote replicas were counted. `createLocalPlan` (Distributed with `prefer_localhost_replica = 1`) has the same gate and loses the local shard whenever the shard query runs at `WithMergeableState`, e.g. with `extremes = 1` (30 instead of 60). At both stages the initiator re-applies the query's LIMIT over all replicas or shards, so mark the local preliminary LIMIT at `WithMergeableState` too. A plan built to `Complete` stays unmarked, and subqueries still reset the flag, so a LIMIT inside the query remains a boundary. Plan-based parallel replicas build their local fragment elsewhere and are not changed. Extend 04510 with the local-plan and the `extremes = 1` Distributed cases. Closes ClickHouse#113279 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #89769
A
Distributedquery may execute one shard inside the initiator whenprefer_localhost_replica=1. ClickHouse currently removes the preliminaryLimitStepfrom this local plan to keeprows_before_limit_at_leastcorrect. Remote shards keep the limit and report their row counts separately.Without the
LimitStep, the local shard cannot use lazy materialization or dynamic TopK. AnORDER BY ... LIMITquery can therefore read and calculate columns for many more rows on the local shard than on remote shards.This change keeps the preliminary limit in normal local shard plans. It marks limits added for shard processing so
rows_before_limit_at_leastincludes rows discarded by those limits. Limits from the query remain accounting boundaries. Positive and negative limits and offsets are covered.TopK source filtering is also disabled when exact row counting is requested, since skipped rows cannot be counted.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed slow
ORDER BY ... LIMITqueries onDistributedtables whenprefer_localhost_replicaselects a local replica.Version info
26.7.1.916(included in26.7and later)