Skip to content

Fix single dictionary detection for multiple dicts in one mark interval - #105605

Merged
Avogar merged 3 commits into
ClickHouse:masterfrom
bobrik:ivan/no-single-dict-reload-fixed-again
May 27, 2026
Merged

Avogar merged 3 commits into
ClickHouse:masterfrom
bobrik:ivan/no-single-dict-reload-fixed-again

Conversation

@bobrik

@bobrik bobrik commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixing up #103662 for good.

With this change the heuristic for single dictionary parts is fully validated, removing any ambiguity and any chance of false positives that previously resulted in rare (but unacceptable) occurrences of incorrect dictionaries being used.

The added test fails before the changes and passes with the changes in place.

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

...

Fixed single dictionary detection for multiple dicts in one mark interval (not present in any released version).


Note

Medium Risk
Changes LowCardinality deserialization behavior in MergeTree wide parts by adding stream reset/seek logic and stricter validation, which is correctness-critical and could affect reads if callbacks/stream positioning are wrong.

Overview
Fixes false positives in LowCardinality single-dictionary-per-part detection when multiple dictionaries are written within a single mark interval.

SerializationLowCardinality::deserializeBinaryBulkStatePrefix now treats has_uniform_marks_callback as a prefilter only, then verifies single-dictionary parts by reading the first dictionary and requiring DictionaryKeys to be at EOF; if not at EOF it seeks the stream back to start (via a new seek_to_start_callback) so normal per-granule dictionary updates are read correctly.

Plumbs the new seek_to_start_callback through ISerialization::DeserializeBinaryBulkSettings, makes SerializationObject’s parallel prefix reading wrap the new callbacks safely, updates MergeTreeReaderWide to implement the seek-to-start behavior, and adds a regression test 04201_low_cardinality_multiple_dicts_single_mark covering the multi-dictionary-in-one-mark case.

Reviewed by Cursor Bugbot for commit e595e6f. Bugbot is set up for automated code reviews on this repo. Configure here.

Version info

  • Merged into: 26.6.1.196

@Avogar Avogar self-assigned this May 22, 2026
@Avogar Avogar added the can be tested Allows running workflows for external contributors label May 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e595e6f]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL IGNORED

AI Review

Summary

This PR hardens LowCardinality single-dictionary detection by treating mark-uniformity as a prefilter and then verifying against DictionaryKeys EOF, with explicit stream reset support and a focused regression test. I found no remaining correctness, safety, or concurrency issues in the current diff, and the existing discussion points appear addressed in the current code.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels May 22, 2026
@Avogar Avogar added v26.5-must-backport and removed pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels May 22, 2026
With this change the heuristic for single dictionary parts is fully validated,
removing any ambiguity and any chance of false positives that previously resulted
in rare (but unacceptable) occurrences of incorrect dictionaries being used.

The added test fails before the changes and passes with the changes in place.
@bobrik

bobrik commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

I rebased (for #105636) and added two commits:

I saw that you added v26.5-must-backport, but I'm not sure if that's necessary, since cd05938 is not in v26.5.

@bobrik
bobrik force-pushed the ivan/no-single-dict-reload-fixed-again branch from 0886f70 to 2662529 Compare May 22, 2026 16:49
@Avogar

Avogar commented May 22, 2026

Copy link
Copy Markdown
Member

I saw that you added v26.5-must-backport, but I'm not sure if that's necessary, since cd05938 is not in v26.5.

That's good, for some reason I thought we created 25.6 branch yesterday, thanks for noticing!

@Avogar Avogar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing!

bobrik added 2 commits May 22, 2026 15:41
…ream

Avorgar's words:

> It's not a good idea to rely on settings.getter to reset the stream. This callback
> is used to just get the buffer for the stream, it doesn't guarantee the reset.

Here we switch to using a callback for this purpose.
…m_marks_callback

These are present for other callbacks, so they should be present for the new ones too.
@bobrik
bobrik force-pushed the ivan/no-single-dict-reload-fixed-again branch from 2662529 to e595e6f Compare May 22, 2026 22:41
@clickhouse-gh

clickhouse-gh Bot commented May 23, 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.50% +0.00%

Changed lines: 84.13% (53/63) · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue May 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks May 26, 2026
@Avogar
Avogar added this pull request to the merge queue May 27, 2026
Merged via the queue into ClickHouse:master with commit da674ac May 27, 2026
165 of 167 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 27, 2026
Avogar added a commit to groeneai/ClickHouse that referenced this pull request Aug 24, 2026
`MergeTreeReaderStream::hasAtMostNDistinctMarks` prefilters the single-dictionary
decision for `LowCardinality` columns. It is documented as cheap, but it walks
`marks_count` marks, which is the whole part's mark count rather than the range
being read, once per `LowCardinality` column per read task. Its early exit only
fires on a multi-dictionary part, so a genuine single-dictionary part, the case
the optimization exists for, always paid the full scan.

Marks are non-decreasing positions in the file, so equal marks form contiguous
runs and the end of a run is one `std::upper_bound` away. Walk runs instead of
marks: at most `max_transitions` binary searches, exact for any bound, with no
precomputed state to store or invalidate. `getRightOffset` already did this
search inline for the same `LowCardinality` dictionary stream, so both now share
`findNextDifferentMark`.

The check is only a necessary condition either way: `SerializationLowCardinality`
still verifies it by reading the first dictionary and checking the stream reaches
EOF, so the returned boolean and everything downstream are unchanged.

Closes: ClickHouse#115998
Related: ClickHouse#105605

Co-Authored-By: Claude Opus 5 (1M context) <[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-critical-bugfix 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