Skip to content

Cherry pick #109178 to 26.3: Preserve original key order in bucketed Map serialization - #112960

Merged
robot-clickhouse merged 12 commits into
backport/26.3/109178from
cherrypick/26.3/109178
Aug 3, 2026
Merged

robot-clickhouse merged 12 commits into
backport/26.3/109178from
cherrypick/26.3/109178

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

Avogar and others added 11 commits July 2, 2026 12:45
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
…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
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Aug 1, 2026
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants