Skip to content

Commit b00d849

Browse files
committed
fix column name index
1 parent 2df04c5 commit b00d849

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

ydb/core/tx/columnshard/engines/scheme/index_info.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ std::optional<ui32> TIndexInfo::GetColumnIdOptional(const std::string& name) con
3737

3838
std::optional<ui32> TIndexInfo::GetColumnIndexOptional(const std::string& name) const {
3939
auto it = std::lower_bound(ColumnIdxSortedByName.begin(), ColumnIdxSortedByName.end(), name, [this](const ui32 idx, const std::string name) {
40-
return SchemaWithSpecials->GetFieldByIndexVerified(idx)->name() < name;
40+
AFL_VERIFY(idx < SchemaColumnIdsWithSpecials.size());
41+
return GetColumnName(SchemaColumnIdsWithSpecials[idx]) < name;
4142
});
4243
if (it != ColumnIdxSortedByName.end() && SchemaWithSpecials->GetFieldByIndexVerified(*it)->name() == name) {
4344
return *it;
@@ -244,18 +245,20 @@ bool TIndexInfo::DeserializeFromProto(const NKikimrSchemeOp::TColumnTableSchema&
244245
AFL_VERIFY(PKColumnIds.empty());
245246
{
246247
TMemoryProfileGuard g("TIndexInfo::DeserializeFromProto::Columns");
248+
THashMap<TString, ui32> columnIds;
247249
for (const auto& col : schema.GetColumns()) {
248250
auto tableCol = BuildColumnFromProto(col, cache);
249251
auto id = tableCol.Id;
252+
AFL_VERIFY(columnIds.emplace(tableCol.Name, id).second);
250253
AFL_VERIFY(columns.emplace(id, std::move(tableCol)).second);
251254
}
252-
BuildColumnIndexByName();
253255
for (const auto& keyName : schema.GetKeyColumnNames()) {
254-
const ui32 columnId = GetColumnIdVerified(keyName);
255-
auto it = columns.find(columnId);
256+
const ui32* findColumnId = columnIds.FindPtr(keyName);
257+
AFL_VERIFY(findColumnId);
258+
auto it = columns.find(*findColumnId);
256259
AFL_VERIFY(it != columns.end());
257260
it->second.KeyOrder = PKColumnIds.size();
258-
PKColumnIds.push_back(columnId);
261+
PKColumnIds.push_back(*findColumnId);
259262
}
260263
}
261264
InitializeCaches(operators, columns, cache, false);
@@ -287,6 +290,7 @@ bool TIndexInfo::DeserializeFromProto(const NKikimrSchemeOp::TColumnTableSchema&
287290
}
288291

289292
Version = schema.GetVersion();
293+
BuildColumnIndexByName();
290294
Precalculate();
291295
Validate();
292296
return true;
@@ -562,6 +566,21 @@ void TIndexInfo::Precalculate() {
562566
}
563567
}
564568

569+
void TIndexInfo::BuildColumnIndexByName() {
570+
const ui32 columnCount = SchemaColumnIdsWithSpecials.size() - SpecialColumnsCount;
571+
std::erase_if(ColumnIdxSortedByName, [columnCount](const ui32 idx) {
572+
return idx >= columnCount;
573+
});
574+
ColumnIdxSortedByName.reserve(columnCount);
575+
for (ui32 i = 0; i < columnCount; ++i) {
576+
ColumnIdxSortedByName.push_back(i);
577+
}
578+
579+
std::sort(ColumnIdxSortedByName.begin(), ColumnIdxSortedByName.end(), [this](const ui32 lhs, const ui32 rhs) {
580+
return CompareColumnIdxByName(lhs, rhs);
581+
});
582+
}
583+
565584
void TIndexInfo::Validate() const {
566585
AFL_VERIFY(!!UsedStorageIds);
567586
AFL_VERIFY(ColumnFeatures.size() == SchemaColumnIdsWithSpecials.size());
@@ -575,11 +594,12 @@ void TIndexInfo::Validate() const {
575594
++idx;
576595
}
577596
}
597+
AFL_VERIFY(std::is_sorted(SchemaColumnIdsWithSpecials.begin(), SchemaColumnIdsWithSpecials.end()));
578598

579599
AFL_VERIFY(ColumnFeatures.size() == ColumnIdxSortedByName.size() + SpecialColumnsCount);
580-
std::is_sorted(ColumnIdxSortedByName.begin(), ColumnIdxSortedByName.end(), [this](const ui32 lhs, const ui32 rhs) {
581-
return GetColumnName(lhs) < GetColumnName(rhs);
582-
});
600+
AFL_VERIFY(std::is_sorted(ColumnIdxSortedByName.begin(), ColumnIdxSortedByName.end(), [this](const ui32 lhs, const ui32 rhs) {
601+
return CompareColumnIdxByName(lhs, rhs);
602+
}));
583603

584604
{
585605
ui32 pkIdx = 0;

ydb/core/tx/columnshard/engines/scheme/index_info.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,7 @@ struct TIndexInfo: public IIndexInfo {
111111

112112
void Validate() const;
113113
void Precalculate();
114-
void BuildColumnIndexByName() {
115-
const ui32 columnCount = SchemaColumnIdsWithSpecials.size() - SpecialColumnsCount;
116-
std::erase_if(ColumnIdxSortedByName, [columnCount](const ui32 idx) {
117-
return idx >= columnCount;
118-
});
119-
ColumnIdxSortedByName.reserve(columnCount);
120-
for (ui32 i = 0; i < columnCount; ++i) {
121-
ColumnIdxSortedByName.push_back(i);
122-
}
123-
124-
std::sort(ColumnIdxSortedByName.begin(), ColumnIdxSortedByName.end(), [this](const ui32 lhs, const ui32 rhs) {
125-
return GetColumnName(lhs) < GetColumnName(rhs);
126-
});
127-
}
114+
void BuildColumnIndexByName();
128115

129116
bool DeserializeFromProto(const NKikimrSchemeOp::TColumnTableSchema& schema, const std::shared_ptr<IStoragesManager>& operators,
130117
const std::shared_ptr<TSchemaObjectsCache>& cache);
@@ -159,6 +146,12 @@ struct TIndexInfo: public IIndexInfo {
159146

160147
void SetAllKeys(const std::shared_ptr<IStoragesManager>& operators, const THashMap<ui32, NTable::TColumn>& columns);
161148

149+
bool CompareColumnIdxByName(const ui32 lhs, const ui32 rhs) const {
150+
AFL_VERIFY(lhs < SchemaColumnIdsWithSpecials.size());
151+
AFL_VERIFY(rhs < SchemaColumnIdsWithSpecials.size());
152+
return GetColumnName(SchemaColumnIdsWithSpecials[lhs]) < GetColumnName(SchemaColumnIdsWithSpecials[rhs]);
153+
}
154+
162155
public:
163156
NSplitter::TEntityGroups GetEntityGroupsByStorageId(const TString& specialTier, const IStoragesManager& storages) const;
164157
std::optional<ui32> GetPKColumnIndexByIndexVerified(const ui32 columnIndex) const {

0 commit comments

Comments
 (0)