Skip to content

Make the row order of 03323_union_all_constants_bug deterministic - #125872

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-03323-union-all-constants-order
Oct 15, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-03323-union-all-constants-order

Conversation

@groeneai

@groeneai groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #75715

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

Not required (CI fix).

Description

03323_union_all_constants_bug failed with result differs with reference on two unrelated PRs, with the two output rows swapped (arm_binary report, amd_debug report).

Both UNION ALL branches return a row with grade_name_id = 42, and the query ends with ORDER BY grade_name_id ASC, so the rows tie and their order is unspecified. The merge of the sorted streams breaks ties by input index, so the result depends on which input each row arrives on. Writing the branches in the opposite order gives exactly the CI diff on every run.

The fix adds a tiebreaker, ORDER BY grade_name_id ASC, flag ASC NULLS FIRST, which keeps the current .reference. grade_name_id stays the first key, so the union still carries the constant column that #75715 is about: EXPLAIN PIPELINE header = 1 is identical before and after, and EXPLAIN PLAN differs only in the sort description.

Validation: with the branches swapped, the old ORDER BY returns the CI diff and the new one returns the reference, under both failing CI settings strings and max_threads 1 and 32. 500 randomized runs pass, and so do 4800 runs with max_threads = 32 and the failing settings.


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

Version info

  • Merged into: 26.10.1.2473-master (included in 26.10 and later)

Both UNION ALL branches return a row with grade_name_id = 42 and the query
sorted only by grade_name_id, so the two rows tied. MergingSortedTransform
breaks ties by merge input index, so the output order depended on which
input carried which row; two unrelated PRs got the rows swapped in
Stateless tests (arm_binary, parallel) and (amd_debug, parallel).

Add `flag ASC NULLS FIRST` as a tiebreaker. The reference is unchanged, and
grade_name_id stays the first key so the union keeps the constant column
the test exists for (ClickHouse#75715): EXPLAIN PIPELINE header = 1 is identical and
EXPLAIN PLAN differs only in the sort description.

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), plus my own cold review of the diff (0 findings).

Nothing to adjudicate: the change is a one-line ORDER BY tiebreaker, the .reference is unchanged, and the union structure the test covers (#75715) is the same below the sort.

Session id: cron:clickhouse-review-slot-9:20261014-203800

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes for the mechanism: with the UNION ALL branches swapped, the old ORDER BY prints the CI diff on every run (12/12 configurations, 20/20 runner runs). The natural timing flip did not reproduce locally.
b Root cause explained? Both rows have grade_name_id = 42, the only sort key, so they tie. The merge of sorted streams breaks ties by input index, and which input carries which row depends on execution. The reference pinned one tie order.
c Fix matches root cause? Yes. The tiebreaker flag ASC NULLS FIRST makes the order total. No setting pin, no tag.
d Test intent preserved / new tests added? Yes. EXPLAIN PIPELINE header = 1 is identical before and after, EXPLAIN PLAN differs only in the sort description, and the .reference is unchanged.
e Both directions demonstrated? Yes, with the swapped-branch repro: old ORDER BY gives the CI diff, new ORDER BY gives the reference, under the same conditions.
f Fix is general across code paths? N/A (test-only change).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only change). 500 randomized runs and 4800 runs with max_threads = 32 and the failing settings pass.
h Backward compatible? (maintainer-approved exception only) N/A (test-only change).
i Invariants and contracts preserved? N/A for engine contracts. The test's output is now a function of the query only.

Session id: cron:clickhouse-impl-slot-6:20261014-202100

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3ba31f7]

Summary: ✅


AI Review

Summary

Adds flag ASC NULLS FIRST to make 03323_union_all_constants_bug deterministic while preserving the constant-column regression case and expected output. No actionable findings at commit 3ba31f75193aa03224ec6ffa2fbc97284f77e905.

Final Verdict

Status: ✅ Approve

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

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ, could you review this and add the can be tested label? It is a one-line test fix: both UNION ALL rows tie on the only ORDER BY key, so the merge of sorted streams can emit them in either order; a flag ASC NULLS FIRST tiebreaker makes the order total and keeps the reference.

@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Oct 14, 2026
@Algunenano Algunenano self-assigned this Oct 15, 2026
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@Algunenano
Algunenano added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit 6a78f52 Oct 15, 2026
523 of 525 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. 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-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. 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