Repository navigation
Conversation
ReadFromMerge::initializePipeline narrows the united child pipeline with narrowPipe, which concatenates streams via ConcatProcessor and does not preserve per-stream order. When a child is read at a partial stage that emits already-sorted streams (e.g. a Distributed child produces one sorted stream per shard), the outer plan adds a merge-only sorting step that assumes each input stream is individually sorted. With max_threads = 1 the sorted shard streams were concatenated into one, the merge-only sort had a single unsorted input and became a no-op, and LIMIT returned one shard's local top rows instead of the global top rows. Extend the existing should_not_narrow guard (which already covers read-in-order and memory-efficient distributed aggregation) with the global-ORDER-BY case: a child read above FetchColumns for an ORDER BY without aggregation, or any after-aggregation stage. This mirrors when the planner/interpreter choose a merge-only sort. Closes ClickHouse#111211. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-6:20260722-160600 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds — full pass + delta recheck; bounded: 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-6:20260722-160600 |
|
cc @devcrafter @davenger — could you review this? It fixes wrong results for a global |
|
Workflow [PR], commit [d58cf12] Summary: ❌
AI ReviewSummaryThis PR makes Final Verdict✅ No new findings. |
The existing assertions all read through a LIMIT, so the merge-only SortingStep always carried a sort limit. An ORDER BY without LIMIT keeps that step unbounded, which is a distinct code path (SortingStep treats a zero sort limit differently). Verified locally that this unbounded case returns per-shard-local order before the fix and global order after it, so add two no-LIMIT monotonicity assertions (new and old analyzer) on the existing tables. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch, addressed in 70afc67. Confirmed locally that the no-
Added two no- DESC is used deliberately: shard 0 (max 297) concatenates before shard 1 (min 1000), so a broken global sort is non-monotonic and the check fails. 50/50 runs pass with randomized settings. |
…R BY The regression test only matched a single child (^dist$), so getQueryProcessingStage returned the child's after-aggregation stage and every assertion exercised the should_not_narrow after-aggregation arm. When a Merge matches two or more partial-stage children the stage is capped to WithMergeableState, where the guard instead relies on the WithMergeableState && !need_aggregate && !has_window arm. With the default distributed_aggregation_memory_efficient = 1 the pre-existing memory-efficient clause already covers that shape, so the arm is only load-bearing when the setting is 0. Add a second Distributed table (disjoint higher range) and a Merge matching both children, asserting a globally monotonic no-LIMIT ORDER BY under distributed_aggregation_memory_efficient = 0 (new and old analyzer). Verified the assertion fails when the WithMergeableState arm is removed. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Added in ab68ff2. Confirmed the second arm (
|
CI finish ledger — ab68ff2Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Not PR-caused: this PR's diff is Session id: cron:our-pr-ci-monitor:20260723-063000 |
CI finish ledger — ab68ff2Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Not PR-caused: this PR's diff is a 10-line StorageMerge.cpp Session id: cron:our-pr-ci-monitor:20260723-073000 |
Same root cause as the ORDER BY case (issue ClickHouse#111211): narrowPipe concatenates the per-shard-sorted streams, so DISTINCT + optimize_distinct_in_order dedups only adjacent rows and the second shard's run survives, doubling the result multiset. The existing should_not_narrow guard already covers this query shape (WithMergeableState without aggregation/window), so this adds a regression case only. Count the bare query's output rows rather than SELECT count() over it: count() lets the optimizer drop the inner ORDER BY, which makes narrowing harmless and hides the bug. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Internal second-model review — adjudication log (DISTINCT regression case, click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds; bounded: 1 full pass + 1 fix round + 1 delta recheck). Scope: the added
Severity: ❌ blocker / Session id: cron:clickhouse-premium-slot-26:20260724-082500 |
|
Added a |
CI finish ledger - 3bcaddfEvery failure below has an owner (an open fixing PR). Neither is caused by this PR's diff (
Reports:
Session id: cron:our-pr-ci-monitor:20260724-160000 |
CI finish ledger — 3bcaddfEvery failure below has an owner. Only
No PR-caused failure: #111464's diff is a 10-line StorageMerge |
…-order-by-merge-over-distributed
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code |
CI finish ledger — d58cf12Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
This PR's diff is a 10-line Session id: cron:our-pr-ci-monitor:20260725-033000 |
|
Superseded by #112931 (merged 2026-08-12), which fixes the same defect in the same lines of Its predicate is a strict superset of mine. With
Over every Verified on master #112931 carries tests for both arms ( Note that #111211 is still open. It reproduced independently of #79949, which #112931 closed, but the verification above shows the same commit fixes it. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results for a global
ORDER BY(andDISTINCT) when reading aDistributedtable through aMergeengine: the per-shard sorted streams were concatenated instead of merge-sorted, soLIMITcould return rows local to one shard instead of the global top rows, andDISTINCTcould return a doubled result multiset.Description
Closes: #111211
ReadFromMerge::initializePipelinenarrows the united child pipeline withnarrowPipe, which concatenates streams viaConcatProcessorand does not preserve per-stream order. When a child is read at a partial stage that emits already-sorted streams (e.g. aDistributedchild produces one sorted stream per shard), the outer plan adds a merge-only sorting step that assumes each input stream is individually sorted. Withmax_threads = 1the two sorted shard streams were concatenated into one, the merge-only sort then had a single unsorted input and became a no-op, andLIMITreturned one shard's local top rows.The
should_not_narrowguard already suppresses narrowing for the two other order-sensitive cases (read-in-order and memory-efficient distributed aggregation). This adds the third: a globalORDER BY(no aggregation, or any after-aggregation stage) over a child read aboveFetchColumns. The condition mirrors when the planner/interpreter choose a merge-only sort (Planner.cppisFromAggregationState/InterpreterSelectQuery.cppfrom_aggregation_stage).Reproducer (before:
297, 294, 291; after:1297, 1294, 1291), see #111211.The same
narrowPipeconcatenation also corrupts the result multiset underDISTINCT(withoptimize_distinct_in_order, the second shard's sorted run survives adjacent-only dedup, doubling the rows). That query shape is already covered by the guard added here, so no extra code change was needed; the regression test now also asserts theDISTINCT/DISTINCT ONrow counts (reported by @ zlareb1 on #111211).