From 3d63b20af248716074b967555c2f97b9aea478a6 Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Tue, 19 Nov 2024 14:40:22 +0300 Subject: [PATCH] fix chunks reorder on loading (#11736) --- .../engines/portions/constructor_accessor.cpp | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/ydb/core/tx/columnshard/engines/portions/constructor_accessor.cpp b/ydb/core/tx/columnshard/engines/portions/constructor_accessor.cpp index 34ce1dc41789..b8535712106c 100644 --- a/ydb/core/tx/columnshard/engines/portions/constructor_accessor.cpp +++ b/ydb/core/tx/columnshard/engines/portions/constructor_accessor.cpp @@ -130,12 +130,39 @@ TPortionDataAccessor TPortionAccessorConstructor::BuildForLoading( const TPortionInfo::TConstPtr& portion, std::vector&& records, std::vector&& indexes) { AFL_VERIFY(portion); std::vector recordChunks; - for (auto&& i : records) { - recordChunks.emplace_back(TColumnRecord(i)); + { + const auto pred = [](const TColumnRecord& l, const TColumnRecord& r) -> bool { + return l.GetAddress() < r.GetAddress(); + }; + bool needSort = false; + for (auto&& i : records) { + TColumnRecord chunk(i); + if (recordChunks.size() && !pred(recordChunks.back(), chunk)) { + needSort = true; + } + recordChunks.emplace_back(std::move(chunk)); + } + if (needSort) { + std::sort(recordChunks.begin(), recordChunks.end(), pred); + } } std::vector indexChunks; - for (auto&& i : indexes) { - indexChunks.emplace_back(i.BuildIndexChunk()); + { + + const auto pred = [](const TIndexChunk& l, const TIndexChunk& r) ->bool { + return l.GetAddress() < r.GetAddress(); + }; + bool needSort = false; + for (auto&& i : indexes) { + auto chunk = i.BuildIndexChunk(); + if (indexChunks.size() && !pred(indexChunks.back(), chunk)) { + needSort = true; + } + indexChunks.emplace_back(std::move(chunk)); + } + if (needSort) { + std::sort(indexChunks.begin(), indexChunks.end(), pred); + } } return TPortionDataAccessor(portion, std::move(recordChunks), std::move(indexChunks), true); }