Skip to content

Commit

Permalink
AlterColumnTable (ydb-platform#10672)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-gogov authored Oct 23, 2024
1 parent 2038471 commit 9f0b790
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 34 deletions.
5 changes: 2 additions & 3 deletions ydb/core/kqp/gateway/kqp_ic_gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,10 +1071,9 @@ class TKikimrIcGateway : public IKqpGateway {
return NotImplemented<TGenericResult>();
}

TFuture<TGenericResult> AlterColumnTable(const TString& cluster,
const NYql::TAlterColumnTableSettings& settings) override {
TFuture<TGenericResult> AlterColumnTable(const TString& cluster, Ydb::Table::AlterTableRequest&& req) override {
Y_UNUSED(cluster);
Y_UNUSED(settings);
Y_UNUSED(req);
return NotImplemented<TGenericResult>();
}

Expand Down
4 changes: 4 additions & 0 deletions ydb/core/kqp/gateway/kqp_metadata_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ TTableMetadataResult GetTableMetadataResult(const NSchemeCache::TSchemeCacheNavi
switch (entry.Kind) {
case EKind::KindTable:
tableMeta->Kind = NYql::EKikimrTableKind::Datashard;
tableMeta->TableType = NYql::ETableType::Table;
tableMeta->StoreType = NYql::EStoreType::Row;
break;

case EKind::KindColumnTable:
tableMeta->Kind = NYql::EKikimrTableKind::Olap;
tableMeta->TableType = NYql::ETableType::Table;
tableMeta->StoreType = NYql::EStoreType::Column;
break;

default:
Expand Down
24 changes: 9 additions & 15 deletions ydb/core/kqp/host/kqp_gateway_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,30 +1524,24 @@ class TKqpGatewayProxy : public IKikimrGateway {
}
}

TFuture<TGenericResult> AlterColumnTable(const TString& cluster,
const TAlterColumnTableSettings& settings) override
{
TFuture<TGenericResult> AlterColumnTable(const TString& cluster, Ydb::Table::AlterTableRequest&& req) override {
CHECK_PREPARED_DDL(AlterColumnTable);

try {
if (cluster != SessionCtx->GetCluster()) {
return MakeFuture(ResultFromError<TGenericResult>("Invalid cluster: " + cluster));
}

std::pair<TString, TString> pathPair;
{
TString error;
if (!NSchemeHelpers::SplitTablePath(settings.Table, GetDatabase(), pathPair, error, false)) {
return MakeFuture(ResultFromError<TGenericResult>(error));
}
}

NKikimrSchemeOp::TModifyScheme schemeTx;
schemeTx.SetWorkingDir(pathPair.first);

schemeTx.SetOperationType(NKikimrSchemeOp::ESchemeOpAlterColumnTable);
NKikimrSchemeOp::TAlterColumnTable* alter = schemeTx.MutableAlterColumnTable();
alter->SetName(settings.Table);
Ydb::StatusIds::StatusCode code;
TString error;
if (!BuildAlterColumnTableModifyScheme(&req, &schemeTx, code, error)) {
IKqpGateway::TGenericResult errResult;
errResult.AddIssue(NYql::TIssue(error));
errResult.SetStatus(NYql::YqlStatusFromYdbStatus(code));
return MakeFuture(errResult);
}

if (IsPrepare()) {
auto& phyQuery = *SessionCtx->Query().PreparingQuery->MutablePhysicalQuery();
Expand Down
11 changes: 3 additions & 8 deletions ydb/core/kqp/provider/yql_kikimr_exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,6 @@ namespace {
};
}

TAlterColumnTableSettings ParseAlterColumnTableSettings(TKiAlterTable alter) {
return TAlterColumnTableSettings{
.Table = TString(alter.Table())
};
}

TSequenceSettings ParseSequenceSettings(const TCoNameValueTupleList& sequenceSettings) {
TSequenceSettings result;
for (const auto& setting: sequenceSettings) {
Expand Down Expand Up @@ -1979,18 +1973,19 @@ class TKiSinkCallableExecutionTransformer : public TAsyncCallbackTransformer<TKi
}

NThreading::TFuture<IKikimrGateway::TGenericResult> future;
bool isTableStore = (table.Metadata->TableType == ETableType::TableStore);
bool isTableStore = (table.Metadata->TableType == ETableType::TableStore); // Doesn't set, so always false
bool isColumn = (table.Metadata->StoreType == EStoreType::Column);

if (isTableStore) {
AFL_VERIFY(false);
if (!isColumn) {
ctx.AddError(TIssue(ctx.GetPosition(input->Pos()),
TStringBuilder() << "TABLESTORE with not COLUMN store"));
return SyncError();
}
future = Gateway->AlterTableStore(cluster, ParseAlterTableStoreSettings(maybeAlter.Cast()));
} else if (isColumn) {
future = Gateway->AlterColumnTable(cluster, ParseAlterColumnTableSettings(maybeAlter.Cast()));
future = Gateway->AlterColumnTable(cluster, std::move(alterTableRequest));
} else {
TMaybe<TString> requestType;
if (!SessionCtx->Query().DocumentApiRestricted) {
Expand Down
2 changes: 1 addition & 1 deletion ydb/core/kqp/provider/yql_kikimr_gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ class IKikimrGateway : public TThrRefBase {
virtual NThreading::TFuture<TGenericResult> CreateColumnTable(
TKikimrTableMetadataPtr metadata, bool createDir, bool existingOk = false) = 0;

virtual NThreading::TFuture<TGenericResult> AlterColumnTable(const TString& cluster, const TAlterColumnTableSettings& settings) = 0;
virtual NThreading::TFuture<TGenericResult> AlterColumnTable(const TString& cluster, Ydb::Table::AlterTableRequest&& req) = 0;

virtual NThreading::TFuture<TGenericResult> CreateTableStore(const TString& cluster,
const TCreateTableStoreSettings& settings, bool existingOk = false) = 0;
Expand Down
1 change: 1 addition & 0 deletions ydb/core/kqp/ut/scheme/kqp_scheme_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5116,6 +5116,7 @@ Y_UNIT_TEST_SUITE(KqpScheme) {
Y_UNIT_TEST(AlterColumnTableTiering) {
TKikimrSettings runnerSettings;
runnerSettings.WithSampleTables = false;
runnerSettings.SetEnableTieringInColumnShard(true);
TKikimrRunner kikimr(runnerSettings);
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
Expand Down
92 changes: 87 additions & 5 deletions ydb/core/ydb_convert/table_description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,18 +733,25 @@ bool FillColumnDescription(NKikimrSchemeOp::TTableDescription& out,
return true;
}

bool FillColumnDescription(NKikimrSchemeOp::TColumnTableDescription& out,
const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in, Ydb::StatusIds::StatusCode& status, TString& error) {
auto* schema = out.MutableSchema();
NKikimrSchemeOp::TOlapColumnDescription* GetAddColumn(NKikimrSchemeOp::TColumnTableDescription& out) {
return out.MutableSchema()->AddColumns();
}

NKikimrSchemeOp::TOlapColumnDescription* GetAddColumn(NKikimrSchemeOp::TAlterColumnTable& out) {
return out.MutableAlterSchema()->AddAddColumns();
}

template <typename TColumnTable>
bool FillColumnDescriptionImpl(TColumnTable& out, const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in,
Ydb::StatusIds::StatusCode& status, TString& error) {
for (const auto& column : in) {
if (column.type().has_pg_type()) {
status = Ydb::StatusIds::BAD_REQUEST;
error = "Unsupported column type for column: " + column.name();
return false;
}

auto* columnDesc = schema->AddColumns();
auto* columnDesc = GetAddColumn(out);
columnDesc->SetName(column.name());

NScheme::TTypeInfo typeInfo;
Expand All @@ -763,6 +770,81 @@ bool FillColumnDescription(NKikimrSchemeOp::TColumnTableDescription& out,
return true;
}

bool FillColumnDescription(NKikimrSchemeOp::TColumnTableDescription& out, const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in,
Ydb::StatusIds::StatusCode& status, TString& error) {
return FillColumnDescriptionImpl(out, in, status, error);
}

bool FillColumnDescription(NKikimrSchemeOp::TAlterColumnTable& out, const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in,
Ydb::StatusIds::StatusCode& status, TString& error) {
return FillColumnDescriptionImpl(out, in, status, error);
}

bool BuildAlterColumnTableModifyScheme(const TString& path, const Ydb::Table::AlterTableRequest* req,
NKikimrSchemeOp::TModifyScheme* modifyScheme, Ydb::StatusIds::StatusCode& status, TString& error) {
const auto ops = GetAlterOperationKinds(req);
if (ops.empty()) {
status = Ydb::StatusIds::BAD_REQUEST;
error = "Empty alter";
return false;
}

if (ops.size() > 1) {
status = Ydb::StatusIds::UNSUPPORTED;
error = "Mixed alter is unsupported";
return false;
}

const auto OpType = *ops.begin();

std::pair<TString, TString> pathPair;
try {
pathPair = SplitPathIntoWorkingDirAndName(path);
} catch (const std::exception&) {
status = Ydb::StatusIds::BAD_REQUEST;
return false;
}

const auto& workingDir = pathPair.first;
const auto& name = pathPair.second;
modifyScheme->SetWorkingDir(workingDir);

if (OpType == EAlterOperationKind::Common) {
auto alterColumnTable = modifyScheme->MutableAlterColumnTable();
alterColumnTable->SetName(name);
modifyScheme->SetOperationType(NKikimrSchemeOp::EOperationType::ESchemeOpAlterColumnTable);

for (const auto& drop : req->drop_columns()) {
alterColumnTable->MutableAlterSchema()->AddDropColumns()->SetName(drop);
}

if (!FillColumnDescription(*alterColumnTable, req->add_columns(), status, error)) {
return false;
}

if (req->has_set_ttl_settings()) {
if (!FillTtlSettings(*alterColumnTable->MutableAlterTtlSettings()->MutableEnabled(), req->Getset_ttl_settings(), status, error)) {
return false;
}
} else if (req->has_drop_ttl_settings()) {
alterColumnTable->MutableAlterTtlSettings()->MutableDisabled();
}

if (req->has_set_tiering()) {
alterColumnTable->MutableAlterTtlSettings()->SetUseTiering(req->set_tiering());
} else if (req->has_drop_tiering()) {
alterColumnTable->MutableAlterTtlSettings()->SetUseTiering("");
}
}

return true;
}

bool BuildAlterColumnTableModifyScheme(
const Ydb::Table::AlterTableRequest* req, NKikimrSchemeOp::TModifyScheme* modifyScheme, Ydb::StatusIds::StatusCode& code, TString& error) {
return BuildAlterColumnTableModifyScheme(req->path(), req, modifyScheme, code, error);
}

template <typename TYdbProto>
void FillTableBoundaryImpl(TYdbProto& out,
const NKikimrSchemeOp::TTableDescription& in, const NKikimrMiniKQL::TType& splitKeyType) {
Expand Down Expand Up @@ -1667,4 +1749,4 @@ bool FillSequenceDescription(NKikimrSchemeOp::TSequenceDescription& out, const Y
return true;
}

} // namespace NKikimr
} // namespace NKikimr
10 changes: 8 additions & 2 deletions ydb/core/ydb_convert/table_description.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ bool BuildAlterTableModifyScheme(const TString& path, const Ydb::Table::AlterTab
bool BuildAlterTableModifyScheme(const Ydb::Table::AlterTableRequest* req, NKikimrSchemeOp::TModifyScheme* modifyScheme,
const TTableProfiles& profiles, const TPathId& resolvedPathId,
Ydb::StatusIds::StatusCode& status, TString& error);
bool BuildAlterColumnTableModifyScheme(const TString& path, const Ydb::Table::AlterTableRequest* req,
NKikimrSchemeOp::TModifyScheme* modifyScheme, Ydb::StatusIds::StatusCode& status, TString& error);
bool BuildAlterColumnTableModifyScheme(
const Ydb::Table::AlterTableRequest* req, NKikimrSchemeOp::TModifyScheme* modifyScheme, Ydb::StatusIds::StatusCode& status, TString& error);

bool FillAlterTableSettingsDesc(NKikimrSchemeOp::TTableDescription& out,
const Ydb::Table::AlterTableRequest& in, const TTableProfiles& profiles,
Expand All @@ -55,8 +59,10 @@ void FillColumnDescription(Ydb::Table::DescribeTableResult& out, const NKikimrSc
// in
bool FillColumnDescription(NKikimrSchemeOp::TTableDescription& out,
const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in, Ydb::StatusIds::StatusCode& status, TString& error);
bool FillColumnDescription(NKikimrSchemeOp::TColumnTableDescription& out,
const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in, Ydb::StatusIds::StatusCode& status, TString& error);
bool FillColumnDescription(NKikimrSchemeOp::TColumnTableDescription& out, const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in,
Ydb::StatusIds::StatusCode& status, TString& error);
bool FillColumnDescription(NKikimrSchemeOp::TAlterColumnTable& out, const google::protobuf::RepeatedPtrField<Ydb::Table::ColumnMeta>& in,
Ydb::StatusIds::StatusCode& status, TString& error);
bool ExtractColumnTypeInfo(NScheme::TTypeInfo& outTypeInfo, TString& outTypeMod,
const Ydb::Type& inType, Ydb::StatusIds::StatusCode& status, TString& error);

Expand Down

0 comments on commit 9f0b790

Please sign in to comment.