Skip to content

Commit 27dc824

Browse files
authored
Add system columns to schemeshard (#7685)
1 parent 52d9c30 commit 27dc824

8 files changed

+62
-6
lines changed

ydb/core/protos/flat_scheme_op.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ message TTableDescription {
400400
optional TTableReplicationConfig ReplicationConfig = 40;
401401

402402
optional bool Temporary = 41;
403+
404+
// This flag is create-only, and has to be set up
405+
// on table creation to allow system column names (started with __ydb_)
406+
// It won't be present on describes and won't be preserved
407+
optional bool SystemColumnNamesAllowed = 42;
403408
}
404409

405410
message TDictionaryEncodingSettings {

ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void PrepareScheme(NKikimrSchemeOp::TTableDescription* schema, const TString& na
2020
//inherit all from Src except PartitionConfig, PartitionConfig could be altered
2121
completedSchema.MutablePartitionConfig()->CopyFrom(schema->GetPartitionConfig());
2222
schema->Swap(&completedSchema);
23+
schema->SetSystemColumnNamesAllowed(true);
2324
}
2425

2526
class TConfigureParts: public TSubOperationState {

ydb/core/tx/schemeshard/schemeshard__operation_create_table.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,17 @@ class TCreateTable: public TSubOperation {
559559

560560
const NScheme::TTypeRegistry* typeRegistry = AppData()->TypeRegistry;
561561
const TSchemeLimits& limits = domainInfo->GetSchemeLimits();
562-
TTableInfo::TAlterDataPtr alterData = TTableInfo::CreateAlterData(nullptr, schema, *typeRegistry, limits, *domainInfo, context.SS->EnableTablePgTypes, context.SS->EnableTableDatetime64, errStr, LocalSequences);
562+
TTableInfo::TAlterDataPtr alterData = TTableInfo::CreateAlterData(
563+
nullptr,
564+
schema,
565+
*typeRegistry,
566+
limits,
567+
*domainInfo,
568+
context.SS->EnableTablePgTypes,
569+
context.SS->EnableTableDatetime64,
570+
errStr,
571+
LocalSequences);
572+
563573
if (!alterData.Get()) {
564574
result->SetError(NKikimrScheme::StatusSchemeError, errStr);
565575
return result;

ydb/core/tx/schemeshard/schemeshard__operation_part.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ ISubOperation::TPtr CreateAlterUserAttrs(TOperationId id, TTxState::ETxState sta
338338
ISubOperation::TPtr CreateForceDropUnsafe(TOperationId id, const TTxTransaction& tx);
339339
ISubOperation::TPtr CreateForceDropUnsafe(TOperationId id, TTxState::ETxState state);
340340

341-
ISubOperation::TPtr CreateNewTable(TOperationId id, const TTxTransaction& tx, const THashSet<TString>& localSequences = { });
341+
ISubOperation::TPtr CreateNewTable(TOperationId id, const TTxTransaction& tx, const THashSet<TString>& localSequences = {});
342342
ISubOperation::TPtr CreateNewTable(TOperationId id, TTxState::ETxState state);
343343

344344
ISubOperation::TPtr CreateCopyTable(TOperationId id, const TTxTransaction& tx,

ydb/core/tx/schemeshard/schemeshard_info_types.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,12 @@ TTableInfo::TAlterDataPtr TTableInfo::CreateAlterData(
258258
TPtr source,
259259
NKikimrSchemeOp::TTableDescription& op,
260260
const NScheme::TTypeRegistry& typeRegistry,
261-
const TSchemeLimits& limits, const TSubDomainInfo& subDomain,
261+
const TSchemeLimits& limits,
262+
const TSubDomainInfo& subDomain,
262263
bool pgTypesEnabled,
263264
bool datetime64TypesEnabled,
264-
TString& errStr, const THashSet<TString>& localSequences)
265+
TString& errStr,
266+
const THashSet<TString>& localSequences)
265267
{
266268
TAlterDataPtr alterData = new TTableInfo::TAlterTableInfo();
267269
alterData->TableDescriptionFull = NKikimrSchemeOp::TTableDescription();
@@ -290,6 +292,8 @@ TTableInfo::TAlterDataPtr TTableInfo::CreateAlterData(
290292
}
291293
}
292294

295+
bool allowSystemColumns = op.GetSystemColumnNamesAllowed();
296+
293297
for (auto& col : *op.MutableColumns()) {
294298
TString colName = col.GetName();
295299

@@ -300,7 +304,7 @@ TTableInfo::TAlterDataPtr TTableInfo::CreateAlterData(
300304
return nullptr;
301305
}
302306

303-
if (!IsValidColumnName(colName)) {
307+
if (!IsValidColumnName(colName, allowSystemColumns)) {
304308
errStr = Sprintf("Invalid name for column '%s'", colName.data());
305309
return nullptr;
306310
}

ydb/core/tx/schemeshard/schemeshard_utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ auto CalcVectorKmeansTreePostingImplTableDescImpl(
464464
}
465465
implTableDesc.AddKeyColumnNames(NTableVectorKmeansTreeIndex::PostingTable_ParentIdColumn);
466466
FillIndexImplTableColumns(GetColumns(baseTable), implTableColumns, implTableDesc);
467+
468+
implTableDesc.SetSystemColumnNamesAllowed(true);
469+
467470
return implTableDesc;
468471
}
469472

@@ -517,6 +520,8 @@ NKikimrSchemeOp::TTableDescription CalcVectorKmeansTreeLevelImplTableDesc(
517520
implTableDesc.AddKeyColumnNames(NTableVectorKmeansTreeIndex::LevelTable_ParentIdColumn);
518521
implTableDesc.AddKeyColumnNames(NTableVectorKmeansTreeIndex::LevelTable_IdColumn);
519522

523+
implTableDesc.SetSystemColumnNamesAllowed(true);
524+
520525
return implTableDesc;
521526
}
522527

ydb/core/tx/schemeshard/schemeshard_utils.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
namespace NKikimr {
2323
namespace NSchemeShard {
2424

25+
inline constexpr TStringBuf SYSTEM_COLUMN_PREFIX = "__ydb_";
26+
2527
inline bool IsAllowedKeyType(NScheme::TTypeInfo typeInfo) {
2628
switch (typeInfo.GetTypeId()) {
2729
case NScheme::NTypeIds::Json:
@@ -37,12 +39,17 @@ inline bool IsAllowedKeyType(NScheme::TTypeInfo typeInfo) {
3739
}
3840
}
3941

40-
inline bool IsValidColumnName(const TString& name) {
42+
inline bool IsValidColumnName(const TString& name, bool allowSystemColumnNames = false) {
43+
if (!allowSystemColumnNames && name.StartsWith(SYSTEM_COLUMN_PREFIX)) {
44+
return false;
45+
}
46+
4147
for (auto c: name) {
4248
if (!std::isalnum(c) && c != '_' && c != '-') {
4349
return false;
4450
}
4551
}
52+
4653
return true;
4754
}
4855

ydb/core/tx/schemeshard/ut_base/ut_base.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11302,4 +11302,28 @@ Y_UNIT_TEST_SUITE(TSchemeShardTest) {
1130211302
env.TestWaitNotification(runtime, txId);
1130311303
}
1130411304

11305+
Y_UNIT_TEST(CreateSystemColumn) {
11306+
TTestBasicRuntime runtime;
11307+
TTestEnv env(runtime);
11308+
ui64 txId = 100;
11309+
11310+
TestCreateTable(runtime, ++txId, "/MyRoot", R"(
11311+
Name: "SystemColumnForbidden"
11312+
Columns { Name: "__ydb_SomeColumn" Type: "Uint64" }
11313+
Columns { Name: "value" Type: "Uint64" }
11314+
KeyColumnNames: ["__ydb_SomeColumn", "value"]
11315+
)", {NKikimrScheme::StatusSchemeError});
11316+
11317+
TestCreateTable(runtime, ++txId, "/MyRoot", R"(
11318+
Name: "SystemColumnAllowed"
11319+
Columns { Name: "__ydb_SomeColumn" Type: "Uint64" }
11320+
Columns { Name: "value" Type: "Uint64" }
11321+
KeyColumnNames: ["__ydb_SomeColumn", "value"]
11322+
SystemColumnNamesAllowed: true
11323+
)");
11324+
11325+
env.TestWaitNotification(runtime, txId);
11326+
11327+
TestCopyTable(runtime, ++txId, "/MyRoot", "SystemColumnInCopyAllowed", "/MyRoot/SystemColumnAllowed");
11328+
}
1130511329
}

0 commit comments

Comments
 (0)