Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shnikd committed Sep 16, 2024
1 parent e3c9723 commit 276da56
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
25 changes: 21 additions & 4 deletions ydb/core/grpc_services/rpc_create_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,28 @@ class TCreateTableRPC : public TRpcSchemeRequestActor<TCreateTableRPC, TEvCreate
return;
}

StatusIds::StatusCode code = StatusIds::SUCCESS;
TString error;

bool hasSerial = false;
for (const auto& column : req->columns()) {
switch (column.default_value_case()) {
case Ydb::Table::ColumnMeta::kFromSequence: {
auto* seqDesc = modifyScheme->MutableCreateIndexedTable()->MutableSequenceDescription()->Add();
if (!FillSequenceDescription(*seqDesc, column.from_sequence(), code, error)) {
NYql::TIssues issues;
issues.AddIssue(NYql::TIssue(error));
return Reply(code, issues, ctx);
}
hasSerial = true;
break;
}
default: break;
}
}

NKikimrSchemeOp::TTableDescription* tableDesc = nullptr;
if (req->indexesSize()) {
if (req->indexesSize() || hasSerial) {
modifyScheme->SetOperationType(NKikimrSchemeOp::EOperationType::ESchemeOpCreateIndexedTable);
tableDesc = modifyScheme->MutableCreateIndexedTable()->MutableTableDescription();
} else {
Expand All @@ -192,9 +212,6 @@ class TCreateTableRPC : public TRpcSchemeRequestActor<TCreateTableRPC, TEvCreate

tableDesc->SetName(name);

StatusIds::StatusCode code = StatusIds::SUCCESS;
TString error;

if (!FillColumnDescription(*tableDesc, req->columns(), code, error)) {
NYql::TIssues issues;
issues.AddIssue(NYql::TIssue(error));
Expand Down
1 change: 0 additions & 1 deletion ydb/core/ydb_convert/table_description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,6 @@ bool FillTableDescription(NKikimrSchemeOp::TModifyScheme& out,
const Ydb::Table::CreateTableRequest& in, const TTableProfiles& profiles,
Ydb::StatusIds::StatusCode& status, TString& error, bool indexedTable)
{

NKikimrSchemeOp::TTableDescription* tableDesc = nullptr;
if (indexedTable) {
tableDesc = out.MutableCreateIndexedTable()->MutableTableDescription();
Expand Down
25 changes: 21 additions & 4 deletions ydb/library/backup/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,19 +742,26 @@ void BackupFolder(TDriver driver, const TString& database, const TString& relDbP
// Restore
////////////////////////////////////////////////////////////////////////////////

TString ProcessColumnType(const TString& name, TTypeParser parser, NTable::TTableBuilder *builder) {
TString ProcessColumnType(const TString& name, TTypeParser parser, NTable::TTableBuilder *builder, bool isSerial) {
TStringStream ss;
ss << "name: " << name << "; ";
if (parser.GetKind() == TTypeParser::ETypeKind::Optional) {
ss << " optional; ";
parser.OpenOptional();
}
if (isSerial) {
ss << "serial; ";
}
ss << "kind: " << parser.GetKind() << "; ";
switch (parser.GetKind()) {
case TTypeParser::ETypeKind::Primitive:
ss << " type_id: " << parser.GetPrimitive() << "; ";
if (builder) {
builder->AddNullableColumn(name, parser.GetPrimitive());
if (isSerial) {
builder->AddSerialColumn(name, parser.GetPrimitive());
} else {
builder->AddNullableColumn(name, parser.GetPrimitive());
}
}
break;
case TTypeParser::ETypeKind::Decimal:
Expand All @@ -776,7 +783,17 @@ NTable::TTableDescription TableDescriptionFromProto(const Ydb::Table::CreateTabl
NTable::TTableBuilder builder;

for (const auto &col : proto.Getcolumns()) {
LOG_DEBUG("AddNullableColumn: " << ProcessColumnType(col.Getname(), TType(col.Gettype()), &builder));
bool isSerial = false;
switch (col.default_value_case()) {
case Ydb::Table::ColumnMeta::kFromSequence: {
if (col.from_sequence().name() == "_serial_" + col.Getname()) {
isSerial = true;
}
break;
}
default: break;
}
LOG_DEBUG("AddColumn: " << ProcessColumnType(col.Getname(), TType(col.Gettype()), &builder, isSerial));
}

for (const auto &primary : proto.Getprimary_key()) {
Expand Down Expand Up @@ -805,7 +822,7 @@ TString SerializeColumnsToString(const TVector<TColumn>& columns, TVector<TStrin
if (BinarySearch(primary.cbegin(), primary.cend(), col.Name)) {
ss << "primary; ";
}
ss << ProcessColumnType(col.Name, col.Type, nullptr) << Endl;
ss << ProcessColumnType(col.Name, col.Type, nullptr, false) << Endl;
}
// Cerr << "Parse column to : " << ss.Str() << Endl;
return ss.Str();
Expand Down
45 changes: 34 additions & 11 deletions ydb/public/sdk/cpp/client/ydb_table/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,17 @@ class TTableDescription::TImpl {
if (col.has_not_null()) {
not_null = col.not_null();
}
Columns_.emplace_back(col.name(), col.type(), col.family(), not_null);
std::optional<bool> isSerial;
switch (col.default_value_case()) {
case Ydb::Table::ColumnMeta::kFromSequence: {
if (col.from_sequence().name() == "_serial_column_" + col.name()) {
isSerial = true;
}
break;
}
default: break;
}
Columns_.emplace_back(col.name(), col.type(), col.family(), not_null, isSerial);
}

// indexes
Expand Down Expand Up @@ -453,8 +463,8 @@ class TTableDescription::TImpl {
return Proto_;
}

void AddColumn(const TString& name, const Ydb::Type& type, const TString& family, std::optional<bool> notNull) {
Columns_.emplace_back(name, type, family, notNull);
void AddColumn(const TString& name, const Ydb::Type& type, const TString& family, std::optional<bool> notNull, std::optional<bool> isSerial) {
Columns_.emplace_back(name, type, family, notNull, isSerial);
}

void SetPrimaryKeyColumns(const TVector<TString>& primaryKeyColumns) {
Expand Down Expand Up @@ -737,8 +747,8 @@ const TVector<TKeyRange>& TTableDescription::GetKeyRanges() const {
return Impl_->GetKeyRanges();
}

void TTableDescription::AddColumn(const TString& name, const Ydb::Type& type, const TString& family, std::optional<bool> notNull) {
Impl_->AddColumn(name, type, family, notNull);
void TTableDescription::AddColumn(const TString& name, const Ydb::Type& type, const TString& family, std::optional<bool> notNull, std::optional<bool> isSerial) {
Impl_->AddColumn(name, type, family, notNull, isSerial);
}

void TTableDescription::SetPrimaryKeyColumns(const TVector<TString>& primaryKeyColumns) {
Expand Down Expand Up @@ -914,6 +924,10 @@ void TTableDescription::SerializeTo(Ydb::Table::CreateTableRequest& request) con
if (column.NotNull.has_value()) {
protoColumn.set_not_null(column.NotNull.value());
}
if (column.IsSerial.has_value() && column.IsSerial.value()) {
auto* fromSequence = protoColumn.mutable_from_sequence();
fromSequence->set_name("_serial_column_" + column.Name);
}
}

for (const auto& pk : Impl_->GetPrimaryKeyColumns()) {
Expand Down Expand Up @@ -1121,7 +1135,7 @@ TTableBuilder& TTableBuilder::AddNullableColumn(const TString& name, const EPrim
.EndOptional()
.Build();

TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, false);
TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, false, false);
return *this;
}

Expand All @@ -1131,7 +1145,7 @@ TTableBuilder& TTableBuilder::AddNullableColumn(const TString& name, const TDeci
.Decimal(type)
.EndOptional()
.Build();
TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, false);
TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, false, false);
return *this;
}

Expand All @@ -1140,7 +1154,7 @@ TTableBuilder& TTableBuilder::AddNullableColumn(const TString& name, const TPgTy
.Pg(type)
.Build();

TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, false);
TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, false, false);
return *this;
}

Expand All @@ -1149,7 +1163,7 @@ TTableBuilder& TTableBuilder::AddNonNullableColumn(const TString& name, const EP
.Primitive(type)
.Build();

TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, true);
TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, true, false);
return *this;
}

Expand All @@ -1158,7 +1172,7 @@ TTableBuilder& TTableBuilder::AddNonNullableColumn(const TString& name, const TD
.Decimal(type)
.Build();

TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, true);
TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, true, false);
return *this;
}

Expand All @@ -1167,7 +1181,16 @@ TTableBuilder& TTableBuilder::AddNonNullableColumn(const TString& name, const TP
.Pg(type)
.Build();

TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, true);
TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, true, false);
return *this;
}

TTableBuilder& TTableBuilder::AddSerialColumn(const TString& name, const EPrimitiveType& type, const TString& family) {
auto columnType = TTypeBuilder()
.Primitive(type)
.Build();

TableDescription_.AddColumn(name, TProtoAccessor::GetProto(columnType), family, true, true);
return *this;
}

Expand Down
7 changes: 5 additions & 2 deletions ydb/public/sdk/cpp/client/ydb_table/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ struct TTableColumn {
TType Type;
TString Family;
std::optional<bool> NotNull;
std::optional<bool> IsSerial;

TTableColumn() = default;

TTableColumn(TString name, TType type, TString family = TString(), std::optional<bool> notNull = std::nullopt)
TTableColumn(TString name, TType type, TString family, std::optional<bool> notNull, std::optional<bool> isSerial)
: Name(std::move(name))
, Type(std::move(type))
, Family(std::move(family))
, NotNull(std::move(notNull))
, IsSerial(isSerial)
{ }

// Conversion from TColumn for API compatibility
Expand Down Expand Up @@ -636,7 +638,7 @@ class TTableDescription {
TTableDescription();
explicit TTableDescription(const Ydb::Table::CreateTableRequest& request);

void AddColumn(const TString& name, const Ydb::Type& type, const TString& family, std::optional<bool> notNull);
void AddColumn(const TString& name, const Ydb::Type& type, const TString& family, std::optional<bool> notNull, std::optional<bool> isSerial);
void SetPrimaryKeyColumns(const TVector<TString>& primaryKeyColumns);

// common
Expand Down Expand Up @@ -854,6 +856,7 @@ class TTableBuilder {
TTableBuilder& AddNonNullableColumn(const TString& name, const TPgType& type, const TString& family = TString());
TTableBuilder& SetPrimaryKeyColumns(const TVector<TString>& primaryKeyColumns);
TTableBuilder& SetPrimaryKeyColumn(const TString& primaryKeyColumn);
TTableBuilder& AddSerialColumn(const TString& name, const EPrimitiveType& type, const TString& family = TString());

// common
TTableBuilder& AddSecondaryIndex(const TIndexDescription& indexDescription);
Expand Down

0 comments on commit 276da56

Please sign in to comment.