Skip to content

Keep the Map bucket_indexes stream in sync when rows are skipped - #113047

Merged
alexey-milovidov merged 1 commit into
masterfrom
followup-109178-bucketed-map-rows-offset
Aug 3, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
followup-109178-bucketed-map-rows-offset

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

The bucketed Map serialization (map_serialization_version = 'with_buckets') writes a flat bucket_indexes stream with one entry per key-value pair and uses it on read to restore the original key order. The two readers of that stream — SerializationMap::deserializeBinaryBulkWithMultipleStreams and SerializationMapKeysOrValues::deserializeBinaryBulkWithMultipleStreams — forwarded rows_offset to the per-bucket streams, but always read the index stream from its current position, i.e. with an implicit offset of zero.

The number of index entries that belong to the skipped rows is not rows_offset, it is the number of key-value pairs contained in those rows, so for a partial-granule read the index stream desynchronizes from the bucket streams: the kept rows get reassembled from the bucket ids of earlier rows, and when the skipped and the kept rows have different per-bucket counts the reconstruction can step past the selected bucket slice.

The fix reads the skipped rows together with the requested ones, reassembles the whole range in the original order and drops the prefix afterwards, so that both streams advance by the same amount. A new gtest round-trips a bucketed Map and its keys subcolumn through in-memory substreams for every rows_offset; without the fix it reconstructs the wrong rows and trips a huge allocation.

Note that no caller in this repository passes a non-zero rows_offset today (MergeTreeRangeReader reads and discards the skipped rows instead), so this is latent rather than user-visible — which is also why it cannot be covered by a stateless test.

The same commit updates the embedded Map documentation, which still claimed that with_buckets does not preserve the key order. It now states that the order is preserved since 26.8, and that parts written by earlier versions keep their bucket order permanently because the original order was never stored on disk — rewriting such a part by a merge or OPTIMIZE FINAL freezes its current bucket order instead of recovering the insertion order.

One review point of the original pull request is deliberately not addressed here, because it is a format decision: presence of the bucket_indexes stream is the only discriminator between the old and the new with_buckets layout, so an older binary silently ignores the stream and can merge such a part back into the pre-fix layout. Making rolling upgrades monotonic would need a real format/version bit or a compatibility knob.

Related: #109178

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Version info

  • Merged into: 26.8.1.705 (included in 26.8 and later)
  • Backported to: 26.7.4.3, 26.6.3.24, 26.5.7.25, 26.3.18.14

The `with_buckets` `Map` serialization writes a flat `bucket_indexes` stream with
one entry per key-value pair, and uses it on read to restore the original key
order. `SerializationMap::deserializeBinaryBulkWithMultipleStreams` and
`SerializationMapKeysOrValues::deserializeBinaryBulkWithMultipleStreams` forwarded
`rows_offset` to the per-bucket streams but always read the index stream from its
current position with `rows_offset` implicitly zero.

The number of index entries that belong to the skipped rows is not `rows_offset` -
it is the number of key-value pairs in those rows - so for a partial-granule read
the index stream would desynchronize from the bucket streams: the kept rows would
be reassembled from the bucket ids of earlier rows, and, when the skipped and kept
rows have different per-bucket counts, the reconstruction could step past the
selected bucket slice.

Read the skipped rows together with the requested ones, reassemble the whole range
in the original order and drop the prefix afterwards, so both streams advance by
the same amount. Add a gtest that round-trips a bucketed `Map` and its `keys`
subcolumn through in-memory substreams for every `rows_offset`; without the fix it
reconstructs wrong rows and trips a huge allocation.

No caller in this repository passes a non-zero `rows_offset` today, so this is a
latent bug rather than a user-visible one.

Follow-up to #109178
@clickhouse-gh

clickhouse-gh Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3a355ccf]

Summary: ✅


AI Review

Summary

This PR fixes the bucketed Map deserializers so a non-zero rows_offset keeps the flat bucket_indexes stream aligned with the per-bucket streams, and it adds focused regression coverage for the full column plus the keys subcolumn. I did not find any review findings in the current patch; the skip-and-reassemble approach is consistent with the existing bulk-deserialization contract, and the Praktika CI report for commit 3a355ccf is fully green.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.40% -0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.60% -0.10%

Changed lines: Changed C/C++ lines covered: 113/114 (99.12%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3a355ccf9 with master 3f0b0fce6 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 508dbed54; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes, symbol sizes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 685.30 MiB 682.31 MiB -2.99 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes ⚠️

5 object files changed (-40.18 MiB total), 0 added, 685 removed.

Object file Master PR Δ
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/ext/transport/chttp2/transport/cht… 960.56 KiB removed -960.56 KiB (-100.00%)
src/Common/ZooKeeper/CMakeFiles/clickhouse_common_zookeeper_no_log.dir/ZooKeeperImpl.cpp.o 804.97 KiB removed -804.97 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/server/server.cc.o 612.22 KiB removed -612.22 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/load_balancing/rls/rls.cc.o 611.36 KiB removed -611.36 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/client_channel/client_channel_filt… 548.04 KiB removed -548.04 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/client_channel/client_channel.cc.o 525.46 KiB removed -525.46 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/client_channel/retry_interceptor.c… 518.85 KiB removed -518.85 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/lib/channel/promise_based_filter.c… 482.91 KiB removed -482.91 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/load_balancing/grpclb/grpclb.cc.o 434.97 KiB removed -434.97 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/call/client_call.cc.o 433.89 KiB removed -433.89 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/call/server_call.cc.o 391.29 KiB removed -391.29 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc++_unsecure.dir/__/grpc/src/cpp/server/server_cc.cc.o 384.88 KiB removed -384.88 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/client_channel/retry_filter_legacy… 369.55 KiB removed -369.55 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/ext/transport/chttp2/transport/wri… 349.81 KiB removed -349.81 KiB (-100.00%)
contrib/google-cloud-cpp-cmake/CMakeFiles/google_cloud_cpp_grpc_utils.dir/__/google-cloud-cpp/googl… 346.93 KiB removed -346.93 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/ext/transport/inproc/legacy_inproc… 345.35 KiB removed -345.35 KiB (-100.00%)
contrib/google-cloud-cpp-cmake/CMakeFiles/google_cloud_cpp_longrunning_operations_protos.dir/google… 339.33 KiB removed -339.33 KiB (-100.00%)
contrib/google-cloud-cpp-cmake/CMakeFiles/google_cloud_cpp_iam_credentials_v1_iamcredentials_protos… 334.24 KiB removed -334.24 KiB (-100.00%)
contrib/google-protobuf-cmake/CMakeFiles/_libprotobuf-lite.dir/__/google-protobuf/src/google/protob… 316.58 KiB removed -316.58 KiB (-100.00%)
contrib/grpc-cmake/CMakeFiles/grpc_unsecure.dir/__/grpc/src/core/call/call_spine.cc.o 312.88 KiB removed -312.88 KiB (-100.00%)
Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline is ×1.06 (different machine and build flags); deltas below are relative to that ratio.

Binary Function Master PR Δ vs median
programs/clickhouse-keeper DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.11069338206770680048] new 14.4 s +14.4 s
programs/clickhouse DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.12487241783113815285] 8.6 s gone -9.1 s (-100%)
programs/clickhouse-keeper DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.12487241783113815285] 8.1 s gone -8.6 s (-100%)
programs/clickhouse-keeper DB::registerFunctionConversion(DB::FunctionFactory&) 6.9 s 14.0 s +6.7 s (+92%)
programs/clickhouse DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.11069338206770680048] new 6.3 s +6.3 s
programs/clickhouse-keeper _GLOBAL__sub_I_TargetLibraryInfo.cpp 5.7 s 11.2 s +5.1 s (+84%)
programs/clickhouse DB::Parquet::writeColumnChunkBody(DB::Parquet::ColumnChunkWriteState&, DB::Parquet::WriteOptions co… 5.7 s 3.2 s -2.8 s (-46%)
programs/clickhouse-keeper DB::ServerSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.54623650351436… new 2.6 s +2.6 s
programs/clickhouse DB::getSettingsChangesHistory()::$_0::operator()() const 4.7 s 2.6 s -2.3 s (-47%)
programs/clickhouse-keeper DB::MergeTreeSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.56540966842… new 2.2 s +2.2 s
programs/clickhouse-keeper DB::registerFunctionVectorFunctions(DB::FunctionFactory&) 2.3 s 4.5 s +2.1 s (+88%)
programs/clickhouse-keeper _ZN2DB18FunctionArrayIndexINS_16CountEqualActionENS_14NameCountEqualEE15executeIntegralIJDutjmDB8_s… 1.9 s 4.1 s +2.1 s (+101%)
programs/clickhouse-keeper _GLOBAL__sub_I_sqid.cpp 1.7 s 3.8 s +2.0 s (+113%)
Compile time of recompiled translation units

9 translation units recompiled, 26 s compile time in total, 6 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/DataTypes/Serializations/SerializationMap.cpp: 18.0 s
Symbol sizes ⚠️
Binary Symbol Master PR Δ
programs/clickhouse-keeper DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.12487241783113815285] 439.45 KiB removed -439.45 KiB (-100.00%)
programs/clickhouse-keeper DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.11069338206770680048] new 439.45 KiB +439.45 KiB
programs/clickhouse DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.11069338206770680048] new 439.45 KiB +439.45 KiB
programs/clickhouse DB::SettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.12487241783113815285] 439.45 KiB removed -439.45 KiB (-100.00%)
programs/clickhouse DB::ServerSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.54623650351436… new 114.16 KiB +114.16 KiB
programs/clickhouse-keeper DB::ServerSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.17566710545920… 114.16 KiB removed -114.16 KiB (-100.00%)
programs/clickhouse DB::ServerSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.17566710545920… 114.16 KiB removed -114.16 KiB (-100.00%)
programs/clickhouse-keeper DB::ServerSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.54623650351436… new 114.16 KiB +114.16 KiB
programs/clickhouse-keeper DB::MergeTreeSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.16712493492… 102.51 KiB removed -102.51 KiB (-100.00%)
programs/clickhouse-keeper DB::MergeTreeSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.56540966842… new 102.51 KiB +102.51 KiB
programs/clickhouse DB::MergeTreeSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.56540966842… new 102.51 KiB +102.51 KiB
programs/clickhouse DB::MergeTreeSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.16712493492… 102.51 KiB removed -102.51 KiB (-100.00%)
programs/clickhouse DB::DatabaseDataLakeSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.1111… new 97.41 KiB +97.41 KiB
programs/clickhouse DB::DatabaseDataLakeSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.9031… 97.41 KiB removed -97.41 KiB (-100.00%)
programs/clickhouse DB::ObjectStorageQueueSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.26… 92.15 KiB removed -92.15 KiB (-100.00%)
programs/clickhouse DB::ObjectStorageQueueSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.38… new 92.15 KiB +92.15 KiB
programs/clickhouse DB::KafkaSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.176316896530569… 89.47 KiB removed -89.47 KiB (-100.00%)
programs/clickhouse DB::KafkaSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.872715978862839… new 89.47 KiB +89.47 KiB
programs/clickhouse DB::DataLakeStorageSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.13841… new 88.38 KiB +88.38 KiB
programs/clickhouse DB::DataLakeStorageSettingsTraits::Accessor::instance()::$_0::operator()() const [clone .llvm.41967… 88.38 KiB removed -88.38 KiB (-100.00%)

Job report

@alexey-milovidov
alexey-milovidov requested a review from Avogar August 3, 2026 19:35
@alexey-milovidov alexey-milovidov self-assigned this Aug 3, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks. Looks like a good follow-up.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 3, 2026
Merged via the queue into master with commit c79f964 Aug 3, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the followup-109178-bucketed-map-rows-offset branch August 3, 2026 20:08
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 3, 2026
@Avogar Avogar self-assigned this Aug 6, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 6, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Aug 7, 2026
Cherry pick #113047 to 26.3: Keep the Map `bucket_indexes` stream in sync when rows are skipped
robot-clickhouse added a commit that referenced this pull request Aug 7, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Aug 7, 2026
Cherry pick #113047 to 26.5: Keep the Map `bucket_indexes` stream in sync when rows are skipped
robot-clickhouse added a commit that referenced this pull request Aug 7, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 7, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 7, 2026
Backport #113047 to 26.7: Keep the Map `bucket_indexes` stream in sync when rows are skipped
alexey-milovidov added a commit that referenced this pull request Aug 9, 2026
Backport #113047 to 26.5: Keep the Map `bucket_indexes` stream in sync when rows are skipped
alexey-milovidov added a commit that referenced this pull request Aug 9, 2026
Backport #113047 to 26.3: Keep the Map `bucket_indexes` stream in sync when rows are skipped
Avogar added a commit that referenced this pull request Aug 10, 2026
Backport #113047 to 26.6: Keep the Map `bucket_indexes` stream in sync when rows are skipped
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport v26.4-must-backport v26.5-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants