Repository navigation
Cherry pick #109178 to 26.3: Preserve original key order in bucketed Map serialization - #112960
Merged
Merged
Conversation
Bucketed Map serialization (PR #99200) splits map keys into buckets by hash, which reorders them. This breaks `ColumnMap::compareAt` (positional comparison) and operations that depend on it: ORDER BY, DISTINCT, GROUP BY, min/max, and equality filters on Map columns. Fix by writing an additional `MapBucketIndexes` substream during serialization that records each key-value pair's bucket index in original insertion order. During deserialization, this stream is used to reconstruct the original key order from per-bucket data. Key changes: - Add `SubstreamType::MapBucketIndexes` for the new stream - Add `CheckStreamExistsCallback` to `DeserializeBinaryBulkSettings` for backward compatibility with old parts lacking the stream - Update `SerializationMap`: write/read bucket indexes, implement `collectMapFromBucketsWithOrder` - Update `SerializationMapKeysOrValues`: same for `m.keys`/`m.values` subcolumn reads - Set `check_stream_exists_callback` in `MergeTreeReaderWide` and `MergeTreeReaderCompact` - Update existing test reference files for correct key ordering - Add stateless tests for key order preservation and comparison correctness - Add integration test for backward compatibility with old parts Co-Authored-By: Claude Opus 4.6 <[email protected]>
…:ClickHouse/ClickHouse into fix-bucketed-map-key-order
This reverts commit 1bb7196.
…x bounds check 1. Add `check_stream_exists_callback` to `EnumerateStreamsSettings` so that `enumerateStreams` can skip substreams that do not exist in the current data part. Both `SerializationMap` and `SerializationMapKeysOrValues` now check this callback before enumerating the `MapBucketIndexes` stream — old parts written before the bucket index fix lack this stream and would cause "Unexpected substream" errors in compact parts when `initSubcolumnsDeserializationOrder` builds the read order. 2. Add bounds check for `bucket_idx` in `collectMapFromBucketsWithOrderImpl` and `collectMapKeysOrValuesFromBucketsWithOrderImpl`. A corrupted `bucket_indexes` stream now throws `INCORRECT_DATA` instead of causing out-of-bounds access. 3. Update the backward compatibility integration test to cover subcolumn access (`m.keys`, `m.values`, `m.size0`) on old compact parts. Co-Authored-By: Claude Opus 4.6 <[email protected]>
This test is picked up by the flaky check because this pull request changes its reference file, and the flaky check rejects any test that runs longer than 180 seconds unless it is tagged `long`. The test body is unchanged by this pull request, but it is genuinely slow in heavy configurations: it forces wide parts (`min_bytes_for_wide_part = 1`) with up to 8 `Map` buckets across six sections, so every part writes a large number of small streams. With `s3 storage` and `meta in keeper` each of those streams costs an S3 object plus a Keeper metadata operation. In the internal `amd_asan_ubsan, flaky check, s3 storage, meta in keeper` job the 38 runs of the test took 68 to 182 seconds, and one of them crossed the threshold. In public CI, which has neither S3 nor Keeper metadata, the same test tops out at 63 seconds. Related: #109178 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Preserve original key order in bucketed Map serialization
Resolve conflicts in `SerializationMap` by keeping the 26.3 release-branch `Columns` type (the branch does not carry the `VectorWithMemoryTracking` migration) and re-applying the PR's own changes on top: the `bucket_index_column` output parameter of `splitMapToBuckets` and the new `collectMapFromBucketsWithOrder`. Same adaptation applied to the merged helpers in `SerializationMapKeysOrValues.cpp`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #109178
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job