Repository navigation
Keep the Map bucket_indexes stream in sync when rows are skipped - #113047
Conversation
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
|
Workflow [PR], commit [3a355ccf] Summary: ✅
AI ReviewSummaryThis PR fixes the bucketed Final VerdictStatus: ✅ Approve LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 113/114 (99.12%) · Uncovered code |
Build profile diff (arm_release)Comparing Binary sizes
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
|
| 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%) |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Thanks. Looks like a good follow-up.
Cherry pick #113047 to 26.3: Keep the Map `bucket_indexes` stream in sync when rows are skipped
…c when rows are skipped
Cherry pick #113047 to 26.5: Keep the Map `bucket_indexes` stream in sync when rows are skipped
…c when rows are skipped
Backport #113047 to 26.7: Keep the Map `bucket_indexes` stream in sync when rows are skipped
Backport #113047 to 26.5: Keep the Map `bucket_indexes` stream in sync when rows are skipped
Backport #113047 to 26.3: Keep the Map `bucket_indexes` stream in sync when rows are skipped
Backport #113047 to 26.6: Keep the Map `bucket_indexes` stream in sync when rows are skipped
…eam in sync when rows are skipped
The bucketed
Mapserialization (map_serialization_version = 'with_buckets') writes a flatbucket_indexesstream 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::deserializeBinaryBulkWithMultipleStreamsandSerializationMapKeysOrValues::deserializeBinaryBulkWithMultipleStreams— forwardedrows_offsetto 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
Mapand itskeyssubcolumn through in-memory substreams for everyrows_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_offsettoday (MergeTreeRangeReaderreads 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
Mapdocumentation, which still claimed thatwith_bucketsdoes 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 orOPTIMIZE FINALfreezes 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_indexesstream is the only discriminator between the old and the newwith_bucketslayout, 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):
Version info
26.8.1.705(included in26.8and later)26.7.4.3,26.6.3.24,26.5.7.25,26.3.18.14