Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions ydb/library/yql/sql/v1/SQLv1.g.in
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,25 @@ create_table_entry:
| an_id_schema
;

create_backup_collection_stmt: CREATE backup_collection WITH LPAREN backup_collection_settings RPAREN;
alter_backup_collection_stmt: ALTER backup_collection alter_backup_collection_actions;
create_backup_collection_stmt: CREATE backup_collection WITH LPAREN backup_collection_settings RPAREN database_or_table_list?;
alter_backup_collection_stmt: ALTER backup_collection (alter_backup_collection_actions | alter_backup_collection_entries);
drop_backup_collection_stmt: DROP backup_collection;

database_or_table_list: DATABASE | table_list;
table_list: TABLE an_id_table (COMMA TABLE an_id_table)*;

alter_backup_collection_actions: alter_backup_collection_action (COMMA alter_backup_collection_action)*;
alter_backup_collection_action:
alter_table_set_table_setting_compat
| alter_table_reset_table_setting
;
alter_backup_collection_entries: alter_backup_collection_entry (COMMA alter_backup_collection_entry)*;
alter_backup_collection_entry:
ADD DATABASE
| DROP DATABASE
| ADD TABLE an_id_table
| DROP TABLE an_id_table
;
backup_collection: BACKUP COLLECTION object_ref;
backup_collection_settings: backup_collection_settings_entry (COMMA backup_collection_settings_entry)*;
backup_collection_settings_entry: an_id EQUALS table_setting_value;
Expand Down
14 changes: 12 additions & 2 deletions ydb/library/yql/sql/v1/SQLv1Antlr4.g.in
Original file line number Diff line number Diff line change
Expand Up @@ -650,15 +650,25 @@ create_table_entry:
| an_id_schema
;

create_backup_collection_stmt: CREATE backup_collection WITH LPAREN backup_collection_settings RPAREN;
alter_backup_collection_stmt: ALTER backup_collection alter_backup_collection_actions;
create_backup_collection_stmt: CREATE backup_collection WITH LPAREN backup_collection_settings RPAREN database_or_table_list?;
alter_backup_collection_stmt: ALTER backup_collection (alter_backup_collection_actions | alter_backup_collection_entries);
drop_backup_collection_stmt: DROP backup_collection;

database_or_table_list: DATABASE | table_list;
table_list: TABLE an_id_table (COMMA TABLE an_id_table)*;

alter_backup_collection_actions: alter_backup_collection_action (COMMA alter_backup_collection_action)*;
alter_backup_collection_action:
alter_table_set_table_setting_compat
| alter_table_reset_table_setting
;
alter_backup_collection_entries: alter_backup_collection_entry (COMMA alter_backup_collection_entry)*;
alter_backup_collection_entry:
ADD DATABASE
| DROP DATABASE
| ADD TABLE an_id_table
| DROP TABLE an_id_table
;
backup_collection: BACKUP COLLECTION object_ref;
backup_collection_settings: backup_collection_settings_entry (COMMA backup_collection_settings_entry)*;
backup_collection_settings_entry: an_id EQUALS table_setting_value;
Expand Down
26 changes: 21 additions & 5 deletions ydb/library/yql/sql/v1/format/sql_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1549,11 +1549,27 @@ friend struct TStaticData;

NewLine();
PushCurrentIndent();
Visit(msg.GetRule_alter_backup_collection_actions3().GetRule_alter_backup_collection_action1());
for (const auto& action : msg.GetRule_alter_backup_collection_actions3().GetBlock2()) {
Visit(action.GetToken1()); // comma
NewLine();
Visit(action.GetRule_alter_backup_collection_action2());
switch (msg.GetBlock3().Alt_case()) {
case TRule_alter_backup_collection_stmt_TBlock3::kAlt1: {
Visit(msg.GetBlock3().GetAlt1().GetRule_alter_backup_collection_actions1().GetRule_alter_backup_collection_action1());
for (const auto& action : msg.GetBlock3().GetAlt1().GetRule_alter_backup_collection_actions1().GetBlock2()) {
Visit(action.GetToken1()); // comma
NewLine();
Visit(action.GetRule_alter_backup_collection_action2());
}
break;
}
case TRule_alter_backup_collection_stmt_TBlock3::kAlt2: {
Visit(msg.GetBlock3().GetAlt2().GetRule_alter_backup_collection_entries1().GetRule_alter_backup_collection_entry1());
for (const auto& entry : msg.GetBlock3().GetAlt2().GetRule_alter_backup_collection_entries1().GetBlock2()) {
Visit(entry.GetToken1()); // comma
NewLine();
Visit(entry.GetRule_alter_backup_collection_entry2());
}
break;
}
default:
ythrow yexception() << "Alt is not supported";
}

PopCurrentIndent();
Expand Down
14 changes: 12 additions & 2 deletions ydb/library/yql/sql/v1/format/sql_format_ut.h
Original file line number Diff line number Diff line change
Expand Up @@ -1577,8 +1577,18 @@ Y_UNIT_TEST(BackupCollectionOperations) {
TCases cases = {
{"creAte BackuP colLection `-naMe` wIth (a = \"b\")",
"CREATE BACKUP COLLECTION `-naMe` WITH (a = \"b\");\n"},
{"alTer bACKuP coLLECTION naMe resEt (b, c), seT (x=y, z=false)",
{"creAte BackuP colLection `-naMe` wIth (a = \"b\") DATabase",
"CREATE BACKUP COLLECTION `-naMe` WITH (a = \"b\") DATABASE;\n"},
{"creAte BackuP colLection `-naMe` wIth (a = \"b\") tabLe `tbl1` , TablE `tbl2`",
"CREATE BACKUP COLLECTION `-naMe` WITH (a = \"b\") TABLE `tbl1`, TABLE `tbl2`;\n"},
{"alTer bACKuP coLLECTION naMe resEt (b, c), seT (x=y, z=false)",
"ALTER BACKUP COLLECTION naMe\n\tRESET (b, c),\n\tSET (x = y, z = FALSE);\n"},
{"alTer bACKuP coLLECTION naMe aDD DATAbase",
"ALTER BACKUP COLLECTION naMe\n\tADD DATABASE;\n"},
{"alTer bACKuP coLLECTION naMe DRoP \n\n DaTAbase",
"ALTER BACKUP COLLECTION naMe\n\tDROP DATABASE;\n"},
{"alTer bACKuP coLLECTION naMe add \n\n tablE\n\tsometable,drOp TABle `other`",
"ALTER BACKUP COLLECTION naMe\n\tADD TABLE sometable,\n\tDROP TABLE `other`;\n"},
{"DROP backup collectiOn `/some/path`",
"DROP BACKUP COLLECTION `/some/path`;\n"},
};
Expand Down Expand Up @@ -1615,4 +1625,4 @@ Y_UNIT_TEST(ResourcePoolClassifierOperations) {

TSetup setup;
setup.Run(cases);
}
}
41 changes: 41 additions & 0 deletions ydb/library/yql/sql/v1/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ namespace NSQLTranslationV1 {
}
};


struct TCreateTopicParameters {
TVector<TTopicConsumerDescription> Consumers;
TTopicSettings TopicSettings;
Expand All @@ -1353,6 +1354,36 @@ namespace NSQLTranslationV1 {
bool MissingOk;
};

struct TCreateBackupCollectionParameters {
std::map<TString, TDeferredAtom> Settings;

bool Database;
TVector<TDeferredAtom> Tables;

bool ExistingOk;
};

struct TAlterBackupCollectionParameters {
enum class EDatabase {
Unchanged,
Add,
Drop,
};

std::map<TString, TDeferredAtom> Settings;
std::set<TString> SettingsToReset;

EDatabase Database = EDatabase::Unchanged;
TVector<TDeferredAtom> TablesToAdd;
TVector<TDeferredAtom> TablesToDrop;

bool MissingOk;
};

struct TDropBackupCollectionParameters {
bool MissingOk;
};

TString IdContent(TContext& ctx, const TString& str);
TString IdContentFromString(TContext& ctx, const TString& str);
TTableHints GetContextHints(TContext& ctx);
Expand Down Expand Up @@ -1488,6 +1519,16 @@ namespace NSQLTranslationV1 {
TNodePtr BuildDropTopic(TPosition pos, const TTopicRef& topic, const TDropTopicParameters& params,
TScopedStatePtr scoped);

TNodePtr BuildCreateBackupCollection(TPosition pos, const TString& id,
const TCreateBackupCollectionParameters& params,
const TObjectOperatorContext& context);
TNodePtr BuildAlterBackupCollection(TPosition pos, const TString& id,
const TAlterBackupCollectionParameters& params,
const TObjectOperatorContext& context);
TNodePtr BuildDropBackupCollection(TPosition pos, const TString& id,
const TDropBackupCollectionParameters& params,
const TObjectOperatorContext& context);

template<class TContainer>
TMaybe<TString> FindMistypeIn(const TContainer& container, const TString& name) {
for (auto& item: container) {
Expand Down
179 changes: 179 additions & 0 deletions ydb/library/yql/sql/v1/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3259,4 +3259,183 @@ TNodePtr BuildAnalyze(TPosition pos, const TString& service, const TDeferredAtom
return new TAnalyzeNode(pos, service, cluster, params, scoped);
}

class TBaseBackupCollectionNode
: public TAstListNode
, public TObjectOperatorContext
{
using TBase = TAstListNode;
public:
TBaseBackupCollectionNode(
TPosition pos,
const TString& objectId,
const TObjectOperatorContext& context)
: TBase(pos)
, TObjectOperatorContext(context)
, Id(objectId)
{}

bool DoInit(TContext& ctx, ISource* src) final {
auto keys = Y("Key");
keys = L(keys, Q(Y(Q("backupCollection"), Y("String", BuildQuotedAtom(Pos, Id)))));
auto options = this->FillOptions(ctx, Y());

Add("block", Q(Y(
Y("let", "sink", Y("DataSink", BuildQuotedAtom(Pos, ServiceId), Scoped->WrapCluster(Cluster, ctx))),
Y("let", "world", Y(TString(WriteName), "world", "sink", keys, Y("Void"), Q(options))),
Y("return", ctx.PragmaAutoCommit ? Y(TString(CommitName), "world", "sink") : AstNode("world"))
)));

return TAstListNode::DoInit(ctx, src);
}

virtual INode::TPtr FillOptions(TContext& ctx, INode::TPtr options) const = 0;

protected:
TString Id;
};

class TCreateBackupCollectionNode
: public TBaseBackupCollectionNode
{
using TBase = TBaseBackupCollectionNode;
public:
TCreateBackupCollectionNode(
TPosition pos,
const TString& objectId,
const TCreateBackupCollectionParameters& params,
const TObjectOperatorContext& context)
: TBase(pos, objectId, context)
, Params(params)
{}

virtual INode::TPtr FillOptions(TContext& ctx, INode::TPtr options) const final {
options->Add(Q(Y(Q("mode"), Q("create"))));

auto settings = Y();
for (auto& [key, value] : Params.Settings) {
settings->Add(Q(Y(BuildQuotedAtom(Pos, key), value.Build())));
}
options->Add(Q(Y(Q("settings"), Q(settings))));

auto entries = Y();
if (Params.Database) {
entries->Add(Q(Y(Q(Y(Q("type"), Q("database"))))));
}
for (auto& table : Params.Tables) {
auto path = ctx.GetPrefixedPath(ServiceId, Cluster, table);
entries->Add(Q(Y(Q(Y(Q("type"), Q("table"))), Q(Y(Q("path"), path)))));
}
options->Add(Q(Y(Q("entries"), Q(entries))));

return options;
}

TPtr DoClone() const final {
return new TCreateBackupCollectionNode(GetPos(), Id, Params, *this);
}

private:
TCreateBackupCollectionParameters Params;
};

class TAlterBackupCollectionNode
: public TBaseBackupCollectionNode
{
using TBase = TBaseBackupCollectionNode;
public:
TAlterBackupCollectionNode(
TPosition pos,
const TString& objectId,
const TAlterBackupCollectionParameters& params,
const TObjectOperatorContext& context)
: TBase(pos, objectId, context)
, Params(params)
{}

virtual INode::TPtr FillOptions(TContext& ctx, INode::TPtr options) const final {
options->Add(Q(Y(Q("mode"), Q("alter"))));

auto settings = Y();
for (auto& [key, value] : Params.Settings) {
settings->Add(Q(Y(BuildQuotedAtom(Pos, key), value.Build())));
}
options->Add(Q(Y(Q("settings"), Q(settings))));

auto resetSettings = Y();
for (auto& key : Params.SettingsToReset) {
resetSettings->Add(BuildQuotedAtom(Pos, key));
}
options->Add(Q(Y(Q("resetSettings"), Q(resetSettings))));

auto entries = Y();
if (Params.Database != TAlterBackupCollectionParameters::EDatabase::Unchanged) {
entries->Add(Q(Y(Q(Y(Q("type"), Q("database"))), Q(Y(Q("action"), Q(Params.Database == TAlterBackupCollectionParameters::EDatabase::Add ? "add" : "drop"))))));
}
for (auto& table : Params.TablesToAdd) {
auto path = ctx.GetPrefixedPath(ServiceId, Cluster, table);
entries->Add(Q(Y(Q(Y(Q("type"), Q("table"))), Q(Y(Q("path"), path)), Q(Y(Q("action"), Q("add"))))));
}
for (auto& table : Params.TablesToDrop) {
auto path = ctx.GetPrefixedPath(ServiceId, Cluster, table);
entries->Add(Q(Y(Q(Y(Q("type"), Q("table"))), Q(Y(Q("path"), path)), Q(Y(Q("action"), Q("drop"))))));
}
options->Add(Q(Y(Q("alterEntries"), Q(entries))));

return options;
}

TPtr DoClone() const final {
return new TAlterBackupCollectionNode(GetPos(), Id, Params, *this);
}

private:
TAlterBackupCollectionParameters Params;
};

class TDropBackupCollectionNode
: public TBaseBackupCollectionNode
{
using TBase = TBaseBackupCollectionNode;
public:
TDropBackupCollectionNode(
TPosition pos,
const TString& objectId,
const TDropBackupCollectionParameters&,
const TObjectOperatorContext& context)
: TBase(pos, objectId, context)
{}

virtual INode::TPtr FillOptions(TContext&, INode::TPtr options) const final {
options->Add(Q(Y(Q("mode"), Q("drop"))));

return options;
}

TPtr DoClone() const final {
TDropBackupCollectionParameters params;
return new TDropBackupCollectionNode(GetPos(), Id, params, *this);
}
};

TNodePtr BuildCreateBackupCollection(TPosition pos, const TString& id,
const TCreateBackupCollectionParameters& params,
const TObjectOperatorContext& context)
{
return new TCreateBackupCollectionNode(pos, id, params, context);
}

TNodePtr BuildAlterBackupCollection(TPosition pos, const TString& id,
const TAlterBackupCollectionParameters& params,
const TObjectOperatorContext& context)
{
return new TAlterBackupCollectionNode(pos, id, params, context);
}

TNodePtr BuildDropBackupCollection(TPosition pos, const TString& id,
const TDropBackupCollectionParameters& params,
const TObjectOperatorContext& context)
{
return new TDropBackupCollectionNode(pos, id, params, context);
}

} // namespace NSQLTranslationV1
Loading