Repository navigation
Add a DISTINCT regression test for Merge over Distributed - #114677
alexey-milovidov merged 3 commits into
Conversation
ClickHouse#112931 stopped `ReadFromMerge` from narrowing the per-shard-sorted streams of a `Distributed` child. Its tests assert the resulting row order, but the same concatenation also produced a wrong result *multiset* under `DISTINCT`: `DistinctSortedStreamTransform` removes only adjacent duplicates, so the second shard's sorted run survived and every value was returned twice. An order assertion cannot express that, so it is covered here. The aggregating queries cover the stage above `WithMergeableState`, reached with `distributed_group_by_no_merge = 2`, where the sort is merge-only because the shards already aggregated and sorted. Neither existing test asserts a value there: 04813 checks a pipeline shape for that stage, not a result. The last one also clears `distributed_push_down_limit`, which otherwise sends the `LIMIT` to the shards and selects a different stage. A `DISTINCT` query cannot cover this stage at all, because the setting legitimately skips the initiator's dedup and returns 42 on both binaries, including straight through the `Distributed` table. Measured with default settings apart from `max_threads`, on the pre-fix release binary 26.7.4.27, whose `should_not_narrow` lacks the guard, and on master 068ec6d, which has it: the `DISTINCT` queries return 42 before and 21 after, the aggregating ones 9 8 7 before and 9 9 8 after, while the direct-`Distributed` control returns 21 on both. Two settings are pinned because the runner randomizes them and either one hides the bug: `optimize_distinct_in_order = 1`, since the hash-dedup plan returns the right count even when the order is corrupt, and `max_threads = 1`, which is what makes the narrowing collapse the shard streams. The row count is taken from a subquery carrying the `LIMIT`; a bare `SELECT count()` over the query lets the optimizer drop the `ORDER BY` and returns 21 even before the fix. Closes: ClickHouse#111211
|
Workflow [PR], commit [b8c2e2b] Summary: ✅ AI ReviewSummaryThis PR adds a shard regression test for the already-merged Missing context / blind spots
Final VerdictNo new review findings. The previous inline comment about the |
|
@groeneai fix CI failure and rerun |
|
The red is not a test failure: 174 of the 177 jobs are DROPPED, so nothing ran. Config Workflow failed in its A bare re-run will not clear it: attempt 2 of this run was already triggered by @zlareb1 could you apply |
|
Correction to my last comment: I said I cannot add labels. That is wrong, and I checked rather than assuming. My token has I am still not applying Everything else in that comment holds. I did not verify whether I can re-run jobs, so treat that part as unchecked; pushing a fresh head is something I can definitely do if you prefer that over a re-run. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units6 translation units recompiled, 3 s compile time in total, 6 of them have a recent master baseline. |
|
@groeneai lets merge latest master and retry CI |
…/test-distinct-merge-over-distributed
|
Merged master (now at The only real red on the previous head was |
`ParserSelectQuery` rewrites `DISTINCT ON (s)` into `LIMIT 1 BY s` before planning (src/Parsers/ParserSelectQuery.cpp:737-745), so that arm runs on `LimitBySortedStreamTransform`, not on `DistinctSortedStreamTransform` like the two arms above it. `EXPLAIN SYNTAX` prints `LIMIT 1 BY s`, and `EXPLAIN PIPELINE` prints `LimitBySortedStreamTransform` over the `MergingSortedTransform 2 -> 1`. The old comment claimed the same transform was reached twice, which was a false coverage signal. The arm is kept rather than replaced, because it does discriminate: measured against a pre-fix binary (26.7.4.47, whose `should_not_narrow` lacks the guard) it returns 42 where master returns 21. Its value is covering a second consumer of the merge-sorted input, which also assumes equal keys are adjacent. The `optimize_distinct_in_order = 1` pin is dropped from this arm only. The two plain `DISTINCT` arms need it, because with the setting at 0 they return 21 even pre-fix: the plan hash-dedups the whole stream. `LimitBySortedStreamTransform` is instead selected on the sort prefix alone (SortingStep::addPerStreamLimitByIfNeeded, src/Processors/QueryPlan/SortingStep.cpp:290-304) and the setting reaches only the distinct path (QueryPlanOptimizationSettings.cpp:212), so this arm returns 42 pre-fix at either value. Dropping the pin lets the runner randomize the setting here. Validation: 30/30 runs pass with setting randomization on; three negative controls (corrupting each of the three assertion groups in the reference) each fail. Co-Authored-By: Claude Fable 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Test only. Related: #112931, already merged, which carries the fix.
#112931 stopped
ReadFromMergefrom narrowing the per-shard-sorted streams of aDistributedchild. Its tests (
04666,04813) assert the resulting row order. The same concatenation alsoproduced a wrong result multiset under
DISTINCT-DistinctSortedStreamTransformremoves onlyadjacent duplicates, so the second shard's sorted run survived and every value came back twice -
and an order assertion cannot express that. @ zlareb1 reported this manifestation on #111211.
The aggregating queries cover the stage above
WithMergeableState, where the sort is merge-onlybecause the shards already aggregated and sorted.
04813asserts a pipeline shape for that stage,not a result.
Both directions, default settings apart from
max_threads, on the pre-fix release binary26.7.4.27(itsshould_not_narrowlacks the guard) and on master068ec6d2ae62(which has it):DISTINCT/DISTINCT ONover theMergetableGROUP BY ... ORDER BY ... LIMIT 3over theMergetableDISTINCTstraight through theDistributedtable (control)50 of 50 runs pass under
clickhouse-testwith setting randomization on, and corrupting anyasserted line to its pre-fix value fails the test.
optimize_distinct_in_order = 1andmax_threads = 1are pinned because the runner randomizesthem and either one hides the bug. The row count comes from a subquery carrying the
LIMIT: abare
SELECT count()lets the optimizer drop theORDER BYand returns 21 even before the fix.Closes: #111211