Skip to content

Add a DISTINCT regression test for Merge over Distributed - #114677

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/test-distinct-merge-over-distributed
Aug 17, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/test-distinct-merge-over-distributed

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

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

...

Description

Test only. Related: #112931, already merged, which carries the fix.

#112931 stopped ReadFromMerge from narrowing the per-shard-sorted streams of a Distributed
child. Its tests (04666, 04813) assert the resulting row order. 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 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-only
because the shards already aggregated and sorted. 04813 asserts a pipeline shape for that stage,
not a result.

Both directions, default settings apart from max_threads, on the pre-fix release binary
26.7.4.27 (its should_not_narrow lacks the guard) and on master 068ec6d2ae62 (which has it):

Queries pre-fix master
DISTINCT / DISTINCT ON over the Merge table 42 21
GROUP BY ... ORDER BY ... LIMIT 3 over the Merge table 9 8 7 9 9 8
the same DISTINCT straight through the Distributed table (control) 21 21

50 of 50 runs pass under clickhouse-test with setting randomization on, and corrupting any
asserted line to its pre-fix value fails the test.

optimize_distinct_in_order = 1 and max_threads = 1 are pinned because the runner randomizes
them and either one hides the bug. The row count comes from a subquery carrying the LIMIT: a
bare SELECT count() lets the optimizer drop the ORDER BY and returns 21 even before the fix.

Closes: #111211

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
@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b8c2e2b]

Summary: ✅


AI Review

Summary

This PR adds a shard regression test for the already-merged ReadFromMerge fix from #112931, covering the wrong-multiset manifestation under DISTINCT / DISTINCT ON and the higher-stage GROUP BY ... ORDER BY ... LIMIT case over Merge on Distributed. I did not find new correctness or test-quality issues in the current patch.

Missing context / blind spots
  • ⚠️ The live PR run still has the full PR / shard-heavy checks pending, so I could not validate this new -- Tags: shard test in the complete distributed suite from CI yet. Green stateless/shard results on the current head would close this gap.
Final Verdict

No new review findings. The previous inline comment about the DISTINCT ON arm no longer applies and was resolved; merge once the remaining CI is green.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 13, 2026
@zlareb1 zlareb1 self-assigned this Aug 14, 2026
@zlareb1

zlareb1 commented Aug 14, 2026

Copy link
Copy Markdown
Member

@groeneai fix CI failure and rerun

@groeneai

Copy link
Copy Markdown
Collaborator Author

The red is not a test failure: 174 of the 177 jobs are DROPPED, so nothing ran.

Config Workflow failed in its can_be_tested pre-hook ("'can be tested' label is required", ci/jobs/scripts/workflow_hooks/trusted.py). This PR carries only pr-ci, so the matrix was dropped before any job started. Build profile diff is collateral of the same failure: it is the only job with run_on_upstream_failure=True, so it started anyway and then died on KeyError: 'clickhouse/stateless-test' because the docker-digest step is gated behind the pre-hook and never filled digest_dockers. It is set_allow_failure(), so it is not blocking either.

A bare re-run will not clear it: attempt 2 of this run was already triggered by github-actions[bot] at 19:06:39Z and failed identically, and the infra-retry workflow only picks up attempt == 1, so it will not try again. The label has to come first. The hook reads labels live at run time rather than from the cached run config, so a re-run after the label lands does pick it up.

@zlareb1 could you apply can be tested? I cannot add labels or re-run jobs myself. Once it is on, re-running all jobs on this run is enough, or say so and I will push a fresh head to re-arm the workflow.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Correction to my last comment: I said I cannot add labels. That is wrong, and I checked rather than assuming. My token has triage on this repository, so I can apply an existing label (verified against a spare label on my own closed #111464, reverted immediately).

I am still not applying can be tested myself. Its description is "Allows running workflows for external contributors", so it is a trust gate whose point is that an org member vouches for a fork's code before it runs on the runners. Self-applying it would defeat that, and being technically able to is not authorization. So the ask stands, just for the right reason: please apply it, or tell me to and I will.

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.

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

clickhouse-gh Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b8c2e2b79 with master 9e0ded100 (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
Binary Master PR Δ
programs/clickhouse-stripped 694.56 MiB 691.55 MiB -3.01 MiB (-0.43%)

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 units

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

Job report

@zlareb1

zlareb1 commented Aug 16, 2026

Copy link
Copy Markdown
Member

@groeneai lets merge latest master and retry CI

@groeneai

Copy link
Copy Markdown
Collaborator Author

Merged master (now at d8392cfb, the branch was 1735 commits behind) and pushed bd72f75d. A fresh PR run is configured and running, and the can be tested label is on the PR this time, so the 174-job drop from the last attempt is gone.

The only real red on the previous head was Stress test (arm_msan): Logical error: Sizes of nested column and null map of Nullable column are not equal after deserialization (STID 6726 family). That is a trunk bug, not something this PR can cause (the diff is two new test files, +75/-0). It is fixed by #112484, which merged 2026-08-15 18:27Z, about 33 hours after that failure. git merge-base --is-ancestor 2409fdca HEAD confirms the fix is now on this branch and was absent from the old head, so this run should be the first one to carry it.

`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]>
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

5 participants