Repository navigation
Fix LOGICAL_ERROR reading a bucketed Map inside a Dynamic column - #113514
PedroTadim merged 3 commits into
Conversation
SerializationDynamic objects are interned in SerializationObjectPool, keyed only on SerializationDynamic::getHash with no key-equality check on a hit. That hash mixes in SerializationInfoSettings::updateHash, which hashed seven of its eight semantic fields and omitted map_serialization_version, while the defaulted operator== does compare it. So Dynamic(max_types=N) built with map_serialization_version = with_buckets and the same type built with basic hashed identically and shared one pooled object; whichever version constructed it first served every later user in the process. The result is a part that contradicts its own metadata. A table declaring with_buckets that reused a basic-configured object wrote the plain Array(Tuple) streams and still recorded with_buckets in serialization.json. Reading that part once the pooled object is rebuilt from the table's own settings (a restart, a DETACH/ATTACH, or any later process) takes the non-BASIC branch in SerializationMap::deserializeBinaryBulkStatePrefix, asks for the .buckets_info stream the part never wrote, and MergeTreeReaderWide::getStream raises LOGICAL_ERROR because the column is present in the part's column list. In debug and sanitizer builds that aborts the server. Observed as three Stress test failures on 2026-08-05 across three unrelated pull requests, driven by the AST fuzzer, which randomizes map_serialization_version per query. The two versions also coexist by design: map_serialization_version_for_zero_level_parts exists so INSERT parts stay basic while merged parts use with_buckets. Fix by hashing the field. The key can now only become more injective, so one pool entry may split into two and none can merge. The two SharedPartColumns hashes that also call updateHash already compare the full key on a collision and are left alone; the change only makes their sharing more precise. No on-disk format is affected: all three consumers are in-memory caches and serialization.json persists the field rather than a hash of it, so no SettingsChangesHistory entry and no format version bump are required. Two tests are added. A gtest pins the pool key directly, asserting that the two Map versions produce different hashes and different pooled objects, with a positive control (a field that is already hashed does separate them) and a negative control (equal settings still share one object, so the assertions cannot be satisfied by breaking interning). A stateless test reproduces the abort end to end and also asserts the written part carries the bucketed streams its metadata declares.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own
Severity: ❌ blocker / Additional checks I ran on top of the gate, all clean: the hashed field set now equals the Session id: cron:clickhouse-review-slot-48:20260805-144700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-review-slot-48:20260805-144700 |
|
cc @Avogar @antonio2368, could you review this? |
|
Workflow [PR], commit [5ea0dba] Summary: ✅
AI ReviewSummaryThis PR fixes the missing Findings
Missing context / blind spots
Final VerdictLLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
CI finish ledger - d528e52Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
All 174 check-runs completed and Session id: cron:our-pr-ci-monitor:20260805-230000 |
| @@ -0,0 +1,61 @@ | |||
| #include <Core/MergeTreeSerializationEnums.h> | |||
There was a problem hiding this comment.
Removed in 5d6b548. I checked the SQL test does not lose anything first: reverting the one-line fix makes 04753 fail on both sides on its own, printing bucketed streams 0 0 0 for the write side and then aborting on the read with the same .buckets_info ... is not found message as the CI reports.
| @@ -74,6 +74,7 @@ void SerializationInfoSettings::updateHash(SipHash & hash) const | |||
| hash.update(static_cast<int>(version)); | |||
The stateless test 04753_dynamic_map_buckets_pooled_serialization already covers the defect in both directions. Verified by reverting the one-line fix: the .sql alone reddens twice over, printing "bucketed streams 0 0 0" for the write side and then aborting on the read side with the byte-identical message from the CI reports. The pooled-object behaviour the gtest asserted is reachable from SQL too: SerializationObjectPool::getOrCreate accounts every insertion in the SerializationCacheCount metric, so a table declaring with_buckets adds one entry next to an existing basic one, while a further table with identical settings adds none. No assertion is lost with the file.
CI finish ledger - 5d6b548Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Two notes on what is deliberately not listed. Session id: cron:our-pr-ci-monitor:20260810-150000 |
…c-map-serialization-version-pool-key
| hash.update(static_cast<int>(version)); | ||
| hash.update(static_cast<int>(string_serialization_version)); | ||
| hash.update(static_cast<int>(nullable_serialization_version)); | ||
| hash.update(static_cast<int>(map_serialization_version)); |
There was a problem hiding this comment.
This fixes the pool key for future SerializationDynamic objects, but it does not make already-written inconsistent parts readable. If a part already has serialization.json saying map_serialization_version = with_buckets while its streams were written in the basic layout, the fixed build still takes the WITH_BUCKETS branch in SerializationMap::deserializeBinaryBulkStatePrefix and immediately asks the reader for ...buckets_info; MergeTreeReaderWide::getStream still throws the same LOGICAL_ERROR when that stream is absent.
Since this PR closes #113914 as a read-path bug, I think the user-facing fix is still incomplete. Either add a compatibility/recovery path for these legacy bad parts (the reader already has check_stream_exists_callback / actual stream presence to distinguish them) or narrow the PR contract to “prevents writing new inconsistent parts” instead of claiming that the read failure itself is fixed.
There was a problem hiding this comment.
Correct, and I checked it rather than reasoning about it. I built the bad part with a pre-fix binary and read it with the fixed one: it still raises Stream y.Map%28UInt64%2C%20UInt64%29.buckets_info for column y with type Dynamic(max_types=128) is not found. Two things are worse than stated: ALTER TABLE ... MODIFY SETTING map_serialization_version = 'basic' does not help, because the read follows the part's own serialization.json, and OPTIMIZE TABLE ... FINAL aborts too, so the part cannot be rewritten either. It also needs only one table, via the documented map_serialization_version = 'with_buckets' plus map_serialization_version_for_zero_level_parts = 'basic' pair.
I took your first option: the contract is narrowed. The changelog entry and description now say this stops the inconsistent part from being written and that parts written by an earlier version stay unreadable and have to be dropped. I kept Closes #113914, since that issue's own reproducer passes with the fix.
I am not adding the recovery path here. Falling back to the basic layout when .buckets_info is absent would make the reader ignore the part's declared version, which is branched on at 15 sites across the 4 Map serialization classes, and it would also silently accept a genuinely truncated bucketed part, which is the case this error currently catches. That is a read-path compatibility decision worth its own PR, not an addition to a one-line cache-key fix.
CI finish ledger - 5ea0dbaCI is fully finished on this head and there are no failures to own: praktika reports
Session id: cron:our-pr-ci-monitor:20260811-000000 |
Closes #113914
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Stopped writing a part whose
Maplayout contradicts its own metadata, for aMapvalue inside aDynamiccolumn. Tables differing only inmap_serialization_versionshared one cached serialization object, so a part could get thebasicMap layout while its metadata declaredwith_buckets, and reading it later failed withLOGICAL_ERROR: Stream ...buckets_info ... is not found. Parts an earlier version already wrote this way stay unreadable and have to be dropped.Description
Two tables of the same
Dynamictype but differentmap_serialization_versionshared one cached serialization object, because the cache key omitted that setting.SerializationDynamicobjects are interned inSerializationObjectPool, keyed only onSerializationDynamic::getHashwith no key-equality check on a hit. That hash mixes inSerializationInfoSettings::updateHash, which hashed 7 of its 8 semantic fields and omittedmap_serialization_version, while the defaultedoperator==does compare it. Whichever version built the object first therefore served every later user in the process.The result is a part that contradicts its own metadata: a table declaring
with_bucketsreused abasic-configured object, wrote the plainArray(Tuple)streams, and still recordedwith_bucketsinserialization.json. Once the pooled object is rebuilt (restart,DETACH/ATTACH, or a later process) the read asks for the.buckets_infostream the part never wrote andMergeTreeReaderWide::getStreamraisesLOGICAL_ERROR. A single table reaches this, becausemap_serialization_version_for_zero_level_partskeeps INSERT partsbasicwhile merged parts usewith_buckets.The fix hashes the field. The other consumers (
SharedPartColumns) already compare the full key and are unaffected; one pool entry can now split into two. No on-disk format change.Scope: this stops such a part from being written. It does not make one an earlier version already wrote readable, because the read follows the part's own
serialization.json: that part still raises the same error on a fixed build, changing the table'smap_serialization_versiondoes not help, andOPTIMIZEcannot rewrite it, so those parts have to be dropped. Making them readable needs a separate decision on the read path.Validated with a deterministic reproducer (aborts on unpatched master 3/3, passes with the fix), a mutation arm reverting only the added line (which reddens the stateless test on both the write and the read side), and 50/50 randomized runs of that test.
Version info
26.8.1.1156(included in26.8and later)