diff options
author | Innokentii Mokin <innokentii@ydb.tech> | 2024-08-15 15:53:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-15 15:53:52 +0300 |
commit | 27dc824d786f956481cfa24e0b200afa786f7b97 (patch) | |
tree | d86b2c838c274bd3e3906a8b75bd1fed5b89fd47 | |
parent | 52d9c3054b56c4998f7196bf582559495433125c (diff) | |
download | ydb-27dc824d786f956481cfa24e0b200afa786f7b97.tar.gz |
Add system columns to schemeshard (#7685)
8 files changed, 62 insertions, 6 deletions
diff --git a/ydb/core/protos/flat_scheme_op.proto b/ydb/core/protos/flat_scheme_op.proto index e2b59b83444..9ec80ddb2e3 100644 --- a/ydb/core/protos/flat_scheme_op.proto +++ b/ydb/core/protos/flat_scheme_op.proto @@ -400,6 +400,11 @@ message TTableDescription { optional TTableReplicationConfig ReplicationConfig = 40; optional bool Temporary = 41; + + // This flag is create-only, and has to be set up + // on table creation to allow system column names (started with __ydb_) + // It won't be present on describes and won't be preserved + optional bool SystemColumnNamesAllowed = 42; } message TDictionaryEncodingSettings { diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp index 514f6db3092..5ca1d6b449a 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp @@ -20,6 +20,7 @@ void PrepareScheme(NKikimrSchemeOp::TTableDescription* schema, const TString& na //inherit all from Src except PartitionConfig, PartitionConfig could be altered completedSchema.MutablePartitionConfig()->CopyFrom(schema->GetPartitionConfig()); schema->Swap(&completedSchema); + schema->SetSystemColumnNamesAllowed(true); } class TConfigureParts: public TSubOperationState { diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_create_table.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_create_table.cpp index 029e72fe4c3..1631b0fd4e5 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_create_table.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_create_table.cpp @@ -559,7 +559,17 @@ public: const NScheme::TTypeRegistry* typeRegistry = AppData()->TypeRegistry; const TSchemeLimits& limits = domainInfo->GetSchemeLimits(); - TTableInfo::TAlterDataPtr alterData = TTableInfo::CreateAlterData(nullptr, schema, *typeRegistry, limits, *domainInfo, context.SS->EnableTablePgTypes, context.SS->EnableTableDatetime64, errStr, LocalSequences); + TTableInfo::TAlterDataPtr alterData = TTableInfo::CreateAlterData( + nullptr, + schema, + *typeRegistry, + limits, + *domainInfo, + context.SS->EnableTablePgTypes, + context.SS->EnableTableDatetime64, + errStr, + LocalSequences); + if (!alterData.Get()) { result->SetError(NKikimrScheme::StatusSchemeError, errStr); return result; diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_part.h b/ydb/core/tx/schemeshard/schemeshard__operation_part.h index c18f9982ba3..4be2780c44d 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_part.h +++ b/ydb/core/tx/schemeshard/schemeshard__operation_part.h @@ -338,7 +338,7 @@ ISubOperation::TPtr CreateAlterUserAttrs(TOperationId id, TTxState::ETxState sta ISubOperation::TPtr CreateForceDropUnsafe(TOperationId id, const TTxTransaction& tx); ISubOperation::TPtr CreateForceDropUnsafe(TOperationId id, TTxState::ETxState state); -ISubOperation::TPtr CreateNewTable(TOperationId id, const TTxTransaction& tx, const THashSet<TString>& localSequences = { }); +ISubOperation::TPtr CreateNewTable(TOperationId id, const TTxTransaction& tx, const THashSet<TString>& localSequences = {}); ISubOperation::TPtr CreateNewTable(TOperationId id, TTxState::ETxState state); ISubOperation::TPtr CreateCopyTable(TOperationId id, const TTxTransaction& tx, diff --git a/ydb/core/tx/schemeshard/schemeshard_info_types.cpp b/ydb/core/tx/schemeshard/schemeshard_info_types.cpp index 509d555d7e7..852f506dab9 100644 --- a/ydb/core/tx/schemeshard/schemeshard_info_types.cpp +++ b/ydb/core/tx/schemeshard/schemeshard_info_types.cpp @@ -258,10 +258,12 @@ TTableInfo::TAlterDataPtr TTableInfo::CreateAlterData( TPtr source, NKikimrSchemeOp::TTableDescription& op, const NScheme::TTypeRegistry& typeRegistry, - const TSchemeLimits& limits, const TSubDomainInfo& subDomain, + const TSchemeLimits& limits, + const TSubDomainInfo& subDomain, bool pgTypesEnabled, bool datetime64TypesEnabled, - TString& errStr, const THashSet<TString>& localSequences) + TString& errStr, + const THashSet<TString>& localSequences) { TAlterDataPtr alterData = new TTableInfo::TAlterTableInfo(); alterData->TableDescriptionFull = NKikimrSchemeOp::TTableDescription(); @@ -290,6 +292,8 @@ TTableInfo::TAlterDataPtr TTableInfo::CreateAlterData( } } + bool allowSystemColumns = op.GetSystemColumnNamesAllowed(); + for (auto& col : *op.MutableColumns()) { TString colName = col.GetName(); @@ -300,7 +304,7 @@ TTableInfo::TAlterDataPtr TTableInfo::CreateAlterData( return nullptr; } - if (!IsValidColumnName(colName)) { + if (!IsValidColumnName(colName, allowSystemColumns)) { errStr = Sprintf("Invalid name for column '%s'", colName.data()); return nullptr; } diff --git a/ydb/core/tx/schemeshard/schemeshard_utils.cpp b/ydb/core/tx/schemeshard/schemeshard_utils.cpp index 40499af343c..5ba564b6f70 100644 --- a/ydb/core/tx/schemeshard/schemeshard_utils.cpp +++ b/ydb/core/tx/schemeshard/schemeshard_utils.cpp @@ -464,6 +464,9 @@ auto CalcVectorKmeansTreePostingImplTableDescImpl( } implTableDesc.AddKeyColumnNames(NTableVectorKmeansTreeIndex::PostingTable_ParentIdColumn); FillIndexImplTableColumns(GetColumns(baseTable), implTableColumns, implTableDesc); + + implTableDesc.SetSystemColumnNamesAllowed(true); + return implTableDesc; } @@ -517,6 +520,8 @@ NKikimrSchemeOp::TTableDescription CalcVectorKmeansTreeLevelImplTableDesc( implTableDesc.AddKeyColumnNames(NTableVectorKmeansTreeIndex::LevelTable_ParentIdColumn); implTableDesc.AddKeyColumnNames(NTableVectorKmeansTreeIndex::LevelTable_IdColumn); + implTableDesc.SetSystemColumnNamesAllowed(true); + return implTableDesc; } diff --git a/ydb/core/tx/schemeshard/schemeshard_utils.h b/ydb/core/tx/schemeshard/schemeshard_utils.h index 6c3a110cfd0..513fc5c221e 100644 --- a/ydb/core/tx/schemeshard/schemeshard_utils.h +++ b/ydb/core/tx/schemeshard/schemeshard_utils.h @@ -22,6 +22,8 @@ namespace NKikimr { namespace NSchemeShard { +inline constexpr TStringBuf SYSTEM_COLUMN_PREFIX = "__ydb_"; + inline bool IsAllowedKeyType(NScheme::TTypeInfo typeInfo) { switch (typeInfo.GetTypeId()) { case NScheme::NTypeIds::Json: @@ -37,12 +39,17 @@ inline bool IsAllowedKeyType(NScheme::TTypeInfo typeInfo) { } } -inline bool IsValidColumnName(const TString& name) { +inline bool IsValidColumnName(const TString& name, bool allowSystemColumnNames = false) { + if (!allowSystemColumnNames && name.StartsWith(SYSTEM_COLUMN_PREFIX)) { + return false; + } + for (auto c: name) { if (!std::isalnum(c) && c != '_' && c != '-') { return false; } } + return true; } diff --git a/ydb/core/tx/schemeshard/ut_base/ut_base.cpp b/ydb/core/tx/schemeshard/ut_base/ut_base.cpp index 5ce0598b434..ed922cfdf25 100644 --- a/ydb/core/tx/schemeshard/ut_base/ut_base.cpp +++ b/ydb/core/tx/schemeshard/ut_base/ut_base.cpp @@ -11302,4 +11302,28 @@ Y_UNIT_TEST_SUITE(TSchemeShardTest) { env.TestWaitNotification(runtime, txId); } + Y_UNIT_TEST(CreateSystemColumn) { + TTestBasicRuntime runtime; + TTestEnv env(runtime); + ui64 txId = 100; + + TestCreateTable(runtime, ++txId, "/MyRoot", R"( + Name: "SystemColumnForbidden" + Columns { Name: "__ydb_SomeColumn" Type: "Uint64" } + Columns { Name: "value" Type: "Uint64" } + KeyColumnNames: ["__ydb_SomeColumn", "value"] + )", {NKikimrScheme::StatusSchemeError}); + + TestCreateTable(runtime, ++txId, "/MyRoot", R"( + Name: "SystemColumnAllowed" + Columns { Name: "__ydb_SomeColumn" Type: "Uint64" } + Columns { Name: "value" Type: "Uint64" } + KeyColumnNames: ["__ydb_SomeColumn", "value"] + SystemColumnNamesAllowed: true + )"); + + env.TestWaitNotification(runtime, txId); + + TestCopyTable(runtime, ++txId, "/MyRoot", "SystemColumnInCopyAllowed", "/MyRoot/SystemColumnAllowed"); + } } |