Skip to content

Commit

Permalink
Support drop sequence in pg parser (ydb-platform#3733)
Browse files Browse the repository at this point in the history
  • Loading branch information
shnikd authored Apr 16, 2024
1 parent d1ae2d7 commit b234137
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
43 changes: 43 additions & 0 deletions ydb/library/yql/sql/pg/pg_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,9 @@ class TConverter : public IPGParseEvents {
case OBJECT_INDEX: {
return ParseDropIndexStmt(value, nameListNodes);
}
case OBJECT_SEQUENCE: {
return ParseDropSequenceStmt(value, nameListNodes);
}
default: {
AddError("Not supported object type for DROP");
return nullptr;
Expand Down Expand Up @@ -2207,6 +2210,46 @@ class TConverter : public IPGParseEvents {
return State.Statements.back();
}

TAstNode* ParseDropSequenceStmt(const DropStmt* value, const TVector<const List*>& names) {
if (value->behavior == DROP_CASCADE) {
AddError("CASCADE is not implemented");
return nullptr;
}

if (names.size() != 1) {
AddError("DROP SEQUENCE requires exactly one sequence");
return nullptr;
}

for (const auto& nameList : names) {
const auto [clusterName, indexName] = getSchemaAndObjectName(nameList);
const auto [sink, key] = ParseQualifiedPgObjectName(
/* catalogName */ "",
clusterName,
indexName,
"pgSequence"
);

TString mode = (value->missing_ok) ? "drop_if_exists" : "drop";
State.Statements.push_back(L(
A("let"),
A("world"),
L(
A("Write!"),
A("world"),
sink,
key,
L(A("Void")),
QL(
QL(QA("mode"), QA(mode))
)
)
));
}

return State.Statements.back();
}

[[nodiscard]]
TAstNode* ParseVariableSetStmt(const VariableSetStmt* value, bool isSetConfig = false) {
if (value->kind != VAR_SET_VALUE) {
Expand Down
14 changes: 14 additions & 0 deletions ydb/library/yql/sql/pg/pg_sql_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@ Y_UNIT_TEST_SUITE(PgSqlParsingOnly) {
UNIT_ASSERT_STRINGS_EQUAL(res.Root->ToString(), expectedAst.Root->ToString());
}

Y_UNIT_TEST(DropSequenceStmt) {
auto res = PgSqlToYql("DROP SEQUENCE IF EXISTS seq;");
UNIT_ASSERT_C(res.Root, res.Issues.ToString());
TString program = R"(
(
(let world (Configure! world (DataSource 'config) 'OrderedColumns)) (let world (Write! world (DataSink '"kikimr" '"") (Key '('pgObject (String '"seq") (String 'pgSequence))) (Void) '('('mode 'drop_if_exists))))
(let world (CommitAll! world))
(return world)
)
)";
const auto expectedAst = NYql::ParseAst(program);
UNIT_ASSERT_STRINGS_EQUAL(res.Root->ToString(), expectedAst.Root->ToString());
}

Y_UNIT_TEST(VariableShowStmt) {
auto res = PgSqlToYql("Show server_version_num");
UNIT_ASSERT(res.Root);
Expand Down

0 comments on commit b234137

Please sign in to comment.