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); }