Skip to content

Fix histogram producing wrong results for small unsorted inputs - #105548

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-histogram-unsorted-input
May 22, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-histogram-unsorted-input

Conversation

@Avogar

@Avogar Avogar commented May 21, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix histogram producing wrong results for small unsorted inputs. Closes #103109

`compress` had an early return (`size <= max_bins`) before sorting,
introduced in #85760. This meant `unique`, which assumes sorted data,
operated on unsorted points when the number of values was within the
bin limit, incorrectly merging non-adjacent bins.

Move the sort block before the early return so data is always sorted
before `unique` runs, matching the original behavior.

Closes #103109

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4455091]

Summary: ✅


AI Review

Summary

This PR fixes histogram order-sensitivity for small unsorted inputs by ensuring compress still normalizes sort-tracking state even when size <= max_bins, and adds a focused stateless regression test covering unsorted, descending, and shuffled input orders. I did not find any remaining correctness, safety, or contract issues in the current head diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 21, 2026
`sorted_prefix` was updated before the early return (`size <= max_bins`),
but `last_inserted` was not. This left `last_inserted` stale after sorting,
which could cause a subsequent `add` to incorrectly increment
`sorted_prefix` (because `last_inserted <= value` would hold for a value
that is not actually greater than the new last element of the sorted array).
A later `compress` would then rely on a wrong `sorted_prefix` and skip
sorting or use invalid `inplace_merge` assumptions, producing wrong
histogram bins.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 100.00% (9/9) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this May 22, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 22, 2026
Merged via the queue into master with commit 8b5e643 May 22, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-histogram-unsorted-input branch May 22, 2026 00:29
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — could this be a backport candidate for 26.4, 26.3 (LTS), 26.2? No backport label was found, and the affected code appears to exist in those branches based on a file/function presence check.

Affected code: AggregateFunctionHistogramData::compress early return logic — introduced in 26.1.

Why this might apply: The bug exists in 26.1+ where the sorted_prefix optimization was introduced. Verified by checking upstream/26.2, upstream/26.3, upstream/26.4 - all have the early return before sorting. Branch 25.8 is NOT affected (it calls sort() before the early return check). This is a wrong results bug affecting a commonly used aggregate function.

Other supported branches (25.8) predate 26.1 and do not contain this code.

Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider pr-must-backport or a version label like v26.4-must-backport.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Avogar @alexey-milovidov — a couple of suggestions to make this PR easier for reviewers and the changelog reader.

Description (currently missing):

  • Why: PR description is empty - no explanation of what was wrong or how it was fixed.

  • Suggested addition:

    The compress() function in histogram had an early return if (size <= max_bins) return; BEFORE sorting the data. This caused unique() to operate on unsorted points, incorrectly merging non-adjacent bins based on their position rather than their value proximity. The fix moves the early return AFTER the sorting logic, ensuring data is always sorted before unique() is called.

Ignore this if the current title/body is intentional.

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 22, 2026
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…sorted-input

Fix `histogram` producing wrong results for small unsorted inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

compress early return before sorting breaks unique invariant, producing wrong histogram results for small unsorted inputs

4 participants