Skip to content

Fix flaky test 03366_bfloat16_sorting - #106684

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/03366-bfloat16-equal-key-sort-stability
Jun 7, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/03366-bfloat16-equal-key-sort-stability

Conversation

@groeneai

@groeneai groeneai commented Jun 7, 2026 •

Copy link
Copy Markdown
Collaborator

The test sorts a hand-built BFloat16 array that contains five values which all round to the same BFloat16 representation (+0.0, +0.0, 0.0, -0.0, 0.0). Equal sort keys have an implementation-defined relative order, and under the right combination of randomized query settings the relative order of -0 against the surrounding 0s changes, so the byte-exact .reference no longer matches.

Diagnosis is from @ alexey-milovidov on #93830 (comment): "ORDER BY x over BFloat16 values where many distinct literals (including +0.0/-0.0/0.0) round to the same value; equal keys sort nondeterministically (the diff was exactly -0 vs 0 reordering)."

Pinning max_threads = 1 in the query's SETTINGS clause makes MergeSortingTransform produce a deterministic order and matches the committed reference. The test still exercises sorting of BFloat16 values across the full input.

CI history: 4 hits across 4 unrelated PRs in 90 days (#93830, #100983, #100649, #96130), 0 master failures, 78,957 OK / 1 FAIL on the most recent 30 days.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=93830&sha=1ae1135cb1b82dd3952036c711434be2c391a12a&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
Related: #93830

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.477

The test sorts a hand-built `BFloat16` array that contains five values
which all round to the same `BFloat16` representation (`+0.0`, `+0.0`,
`0.0`, `-0.0`, `0.0`). Equal sort keys have an implementation-defined
relative order; under enough query-plan randomization the relative
order of `-0` against the surrounding `0`s changes, and the result no
longer matches the byte-exact `.reference` file. Pinning
`max_threads = 1` makes the order produced by `MergeSortingTransform`
deterministic and matches the committed reference. The test still
exercises sorting of `BFloat16` values across the full input.

Diagnosed by @alexey-milovidov on
ClickHouse#93830 (comment)

CI history: 4 hits across 4 unrelated PRs in 90 days (ClickHouse#93830, ClickHouse#100983,
ClickHouse#100649, ClickHouse#96130), 0 master failures, 78957 OK runs / 1 FAIL in 30 days.
@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Partial. The mechanism is reproduced locally: a format(Values, ...) source with parallel MergeSortingTransform produces variable relative order of equal BFloat16 zero-equivalents (-0 and 0 swap positions across runs when max_threads > 1 with multi-chunk sources). The exact CI failure was 1 / 78,957 runs in 30 days; the underlying mechanism is solid even though the CI rate is too low to deterministically reproduce.
b Root cause explained? Yes. The test sorts five values that all round to the same BFloat16 representation (+0.0, +0.0, 0.0, -0.0, 0.0). They compare equal as BFloat16, so sorting only constrains them to be contiguous, not their relative order. clickhouse-test randomization can route the query through a parallel sort path that happens to put -0 somewhere other than the position the .reference was committed with. Diagnosed by @ alexey-milovidov on PR #93830.
c Fix matches root cause? Yes. SETTINGS max_threads = 1 forces MergeSortingTransform to run single-threaded, which uses a deterministic comparator-based sort whose equal-key order matches the committed reference. Targets the parallelism that introduces the variability.
d Test intent preserved / new tests added? Yes. The test still exercises ORDER BY over a BFloat16 column with the full input (negatives, NaNs, infinities, large integer-rounded values, sub-unit values). Only the parallelism is constrained. No widening of bounds, no no-random-* tags, no data reduction.
e Both directions demonstrated? The fix passes 50/50 with --max_threads 16 --prefer_external_sort_block_bytes 1 --max_bytes_before_external_sort 1 --max_block_size 64. Without the fix the equal-key order varies under multi-chunk parallel sort (demonstrated locally on a UNION ALL probe with the same five zero-equivalent values).
f Fix is general, not a narrow patch? N/A — this is a flaky-test setting fix, not a code bug fix.

Session id: cron:clickhouse-ci-task-worker:20260607-161200

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov — could you review this? Pins max_threads = 1 per your diagnosis on #93830 to make the equal-key BFloat16 zero-equivalent ordering deterministic.

@groeneai groeneai mentioned this pull request Jun 7, 2026
1 task done
@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [46f87d4]

Summary: ✅


AI Review

Summary
  • The PR stabilizes 03366_bfloat16_sorting by pinning the query to max_threads = 1, so equal BFloat16 zero values do not depend on parallel sort merge ordering. The change is narrow, matches the stated flaky-test mechanism, and keeps the existing BFloat16 sorting coverage intact.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.50% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 356 line(s), 20 function(s) across 133 file(s) · Details

Top files
  • src/Storages/StorageReplicatedMergeTree.cpp: 17 line(s), 2 function(s)
  • src/Functions/FunctionsBitmap.h: 12 line(s)
  • src/Functions/FunctionsExternalDictionaries.h: 12 line(s)
  • src/Processors/Formats/Impl/Parquet/Decoding.cpp: 11 line(s)
  • src/Storages/ObjectStorageQueue/ObjectStorageQueueIFileMetadata.cpp: 11 line(s)

Full report

@alexey-milovidov alexey-milovidov self-assigned this Jun 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 7, 2026
Merged via the queue into ClickHouse:master with commit 96b8a53 Jun 7, 2026
166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 8, 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 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.

4 participants