Skip to content

Fix flaky tests 02317_distinct_in_order_optimization and 02733_fix_distinct_in_order_bug_49622 under parallel replicas - #127002

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-02317-distinct-in-order-parallel-replicas
Oct 21, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-02317-distinct-in-order-parallel-replicas

Conversation

@groeneai

@groeneai groeneai commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #114550

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

Since #114550 was merged, two tests fail in the ParallelReplicas stateless jobs with the right rows in another order:

  • 02317_distinct_in_order_optimization: select distinct a from sorting_key_empty_tuple prints 0, 1 instead of 1, 0. 16 failures in 433 ParallelReplicas runs after the merge, 0 in 4152 in the 10 days before (job).
  • 02733_fix_distinct_in_order_bug_49622: SELECT DISTINCT c2, c1 FROM test_string prints ('', '2') before ('', '1'). CI's rerun reproduced it 19 of 446 times; 0 failures in 4149 ParallelReplicas runs in the 10 days before the merge (job).

The final DISTINCT now hash-partitions its input when it has more than one input stream. Under parallel replicas the initiator has several, so the rows of one chunk land in different partitions that are output in scheduling order. The set is unchanged; as allow_parallel_distinct documents, a DISTINCT without ORDER BY has no defined row order.

The change adds ORDER BY to both queries, as #114550 did for 02733_distinct and 01213_optimize_skip_unused_shards_DISTINCT. Both tests keep their bug coverage: 42185, the preliminary DISTINCT above the in-order read with an empty sort description, and 49622, the in-order DISTINCT with an empty-string non-sorted column.

On a parallel-replicas server, EXPLAIN PIPELINE shows ScatterByPartitionTransform for both old queries, and none with allow_parallel_distinct = 0 or for the new queries. With parallel_replicas_local_plan = 0 the old queries returned the other order in 332 (02317) and 3113 (02733) of 12000 runs, never in the allow_parallel_distinct = 0 controls; the new ones never did. Both tests passed 50 of 50 randomized runs with and without the parallel-replicas profile.

🤖 Generated with Claude Code


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

Version info

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

…plicas

Since ClickHouse#114550 the final DISTINCT hash-partitions its input
(DistinctStep::tryScatterStreams) when it has more than one input stream
and max_threads > 1. Under parallel replicas the initiator's final DISTINCT
always has several input streams (local plan plus remote replicas, or only
remote replicas with parallel_replicas_local_plan = 0), so the single chunk
of `sorting_key_empty_tuple` is split into partitions whose outputs reach
the client in scheduling order: `0, 1` instead of `1, 0` in 4 of 198
ParallelReplicas runs since the merge, 0 of 2846 in the week before.

The returned set is unchanged and a DISTINCT without ORDER BY has no
defined row order (allow_parallel_distinct documents it), so pin the order
with ORDER BY, as ClickHouse#114550 did for 02733_distinct and
01213_optimize_skip_unused_shards_DISTINCT. The bug 42185 path stays
covered: the preliminary DISTINCT is still planned directly above the
in-order read with an empty sort description; ORDER BY only adds a sort
above it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings on the test change) plus my own cold review (2 nits, both in the PR description, fixed before publication).

# Sev Finding Verdict Evidence / action
1 💡 The Related: link sat inside the template's HTML comment, so it did not render AGREE, fixed in the PR description link moved below the comment
2 💡 Post-merge run count in the description was a stale snapshot AGREE, fixed in the PR description CIDB re-queried: 4 of about 200 ParallelReplicas runs

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-11:20261020-173400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes for the mechanism: EXPLAIN PIPELINE on a parallel-replicas server shows ScatterByPartitionTransform before the final DISTINCT every time, and none with allow_parallel_distinct = 0 or enable_parallel_replicas = 0. The reorder itself: 78 of 12000 runs (332 of 12000 with parallel_replicas_local_plan = 0), 0 of 12000 with allow_parallel_distinct = 0.
b Root cause explained? Since #114550 the final DISTINCT hash-partitions its input when it has more than one stream; under parallel replicas the initiator always has several, so the values of one chunk land in different partitions and reach the output in scheduling order.
c Fix matches root cause? Yes. The row order of a DISTINCT without ORDER BY is undefined and the set is correct, so the test asserts a defined order (ORDER BY a), as #114550 did for 02733_distinct and 01213.
d Test intent preserved / new tests added? Yes. The bug 42185 path is still planned: preliminary DISTINCT directly above the in-order read with an empty sort description (EXPLAIN PLAN before and after). Test-only change, no new test.
e Both directions demonstrated? Old query: 78 and 332 reorders in 12000 runs each. New query: 24000 of 24000 in order. Test: 50 of 50 randomized runs with and without the parallel-replicas profile.
f Fix is general across code paths? Every other DISTINCT without ORDER BY in the file returns one row, or runs with max_threads = 1, which is not scattered (checked with EXPLAIN PIPELINE).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only order fix); both parallel_replicas_local_plan values checked.
h Backward compatible? (maintainer-approved exception only) N/A (test-only).
i Invariants and contracts preserved? N/A (test-only); the asserted result set is unchanged.

Session id: cron:clickhouse-impl-slot-40:20261020-165800

@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b44146b]

Summary: ✅


AI Review

Summary

This PR adds explicit ORDER BY clauses to two DISTINCT regression tests whose unordered results can be reordered by parallel replicas, and updates the affected reference output. The changes preserve the asserted result sets and the regression coverage for the empty sorting key and empty-string cases. No actionable findings in the diff at b44146bee1f0ba1b942d6547654c251611d9eafa.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Oct 20, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @devcrafter, could you review this and add the can be tested label so CI runs? Since #114550 the final DISTINCT hash-partitions a multi-stream input, so under parallel replicas the one unordered select distinct a in this test returns its two rows in scheduling order; the test now orders that query.

@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Oct 20, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 21, 2026
…eplicas

Same mechanism as 02317_distinct_in_order_optimization. Since ClickHouse#114550 the
final DISTINCT hash-partitions its input (DistinctStep::tryScatterStreams)
when it has more than one input stream and max_threads > 1. Under parallel
replicas the initiator's final DISTINCT has several input streams (local
plan plus remote replicas), so the two rows of `test_string` can land in
different partitions and reach the client in either order. CI's own rerun
with the failing run's settings reproduced it 19 of 446 times on PR ClickHouse#127095;
the test had 0 failures in 33352 runs before the merge.

The returned set is unchanged and a DISTINCT without ORDER BY has no
defined row order, so pin it with ORDER BY c1. The reference is unchanged.
With ORDER BY the final DISTINCT runs after the sort and keeps the input
order, which forbids the scatter.

The 49622 path stays covered (in-order DISTINCT with an empty-string
non-sorted column, preliminary and final): the preliminary DISTINCT is
still planned directly above the in-order read with the `c1` prefix, and
the final DISTINCT now also runs in order over `c1`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Fix flaky test 02317_distinct_in_order_optimization under parallel replicas Fix flaky tests 02317_distinct_in_order_optimization and 02733_fix_distinct_in_order_bug_49622 under parallel replicas Oct 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (02733 commit) (click to expand)

Pre-publication review of the added 02733_fix_distinct_in_order_bug_49622 change by an independent model (engine: codex; 2 findings) plus my own cold review (2 nits in the PR description, fixed before publication).

# Sev Finding Verdict Evidence / action
1 ⚠️ 02733 asserts only rows, so a future planner change that skips DistinctSortedStreamTransform would leave it passing DISAGREE On this tree both DISTINCT steps take the sorted path (EXPLAIN PLAN: Sorting: c1 ASC on the preliminary and the final step), and the test fails if DistinctSortedStreamTransform stops clearing its set per range. The plan shape differs per CI lane, so an EXPLAIN assertion here would need lane pins; plan coverage lives in 02317_distinct_in_order_optimization_explain.sh.
2 ⚠️ 02317's bug 42185 query would also pass if distinct-in-order stopped applying DISAGREE Not applying is the fixed behaviour for an empty sort description (bug 42185 was a LOGICAL_ERROR on that read). The preliminary DISTINCT stays directly above the tuple()-keyed read in the old and the new query; ORDER BY a changes only the final step.
3 💡 PR description: stale failure counts, mixed configurations in one comparison AGREE, fixed Counts and the comparison updated in the description.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-10:20261021-070100

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes at mechanism level: on a parallel-replicas server EXPLAIN PIPELINE of the old 02733 query shows ScatterByPartitionTransform before the final DISTINCT (max_threads 3 and 32, parallel_replicas_local_plan 1 and 0), and none with allow_parallel_distinct = 0 or without parallel replicas. The reorder itself: 3113 of 12000 runs (4 clients, max_threads = 32, parallel_replicas_local_plan = 0), 0 in the controls.
b Root cause explained? Since #114550 the final DISTINCT hash-partitions its input when it has more than one input stream; under parallel replicas the initiator always has several, so the two rows can land in different partitions that are output in scheduling order. The set is unchanged.
c Fix matches root cause? Yes. A DISTINCT without ORDER BY has no defined row order; ORDER BY c1 asserts one, and the final DISTINCT then keeps the input order, which also disables the scatter (0 in EXPLAIN).
d Test intent preserved / new tests added? Yes. The preliminary in-order DISTINCT over the c1 prefix (bug 49622: empty-string c2, set cleared per range) is still planned, and the final DISTINCT now takes the same path. Removing the per-range clear in DistinctSortedStreamTransform makes the new test fail. Test-only change, no new test.
e Both directions demonstrated? Old query: 3113 of 12000 and 2 of 12000 reorders; new query: 0 of 36000. Fixed test: 50 of 50 randomized runs with and without the parallel-replicas profile.
f Fix is general across code paths? The file has one SELECT. The only other test that turned red after #114550 in the DISTINCT family is 02317_distinct_in_order_optimization, fixed in this PR (20 of 20 on the same binary).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only order fix); both parallel_replicas_local_plan values and max_threads 3 and 32 checked.
h Backward compatible? (maintainer-approved exception only) N/A (test-only).
i Invariants and contracts preserved? N/A (test-only); the test asserts the same result set.

Session id: cron:clickhouse-impl-slot-5:20261021-063300

@alexey-milovidov alexey-milovidov self-assigned this Oct 21, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2026
Merged via the queue into ClickHouse:master with commit 378e522 Oct 21, 2026
178 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 21, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... and removed comp-testing Test content only (no production code). labels Nov 22, 2026
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 comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-ci 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.

3 participants