diff options
author | marsaly <marsaly@yandex-team.com> | 2023-03-22 11:24:05 +0300 |
---|---|---|
committer | marsaly <marsaly@yandex-team.com> | 2023-03-22 11:24:05 +0300 |
commit | 144d85a4b3417951a8565c3a55e630cf71ef8d00 (patch) | |
tree | b5ec2191c2e93334cf605a3262419f6baa33d44f | |
parent | 63407f77f8af9298529998c4bec2e3903df5c505 (diff) | |
download | ydb-144d85a4b3417951a8565c3a55e630cf71ef8d00.tar.gz |
YQL-15780: Cleaning up CREATE TABLE AST tree in PG-syntax
Don't add empty 'primarykey and 'notnull options into AST
-rw-r--r-- | ydb/library/yql/sql/pg/pg_sql.cpp | 23 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/pg_sql_ut.cpp | 13 |
2 files changed, 28 insertions, 8 deletions
diff --git a/ydb/library/yql/sql/pg/pg_sql.cpp b/ydb/library/yql/sql/pg/pg_sql.cpp index 368c9ddb8d..f4746f783f 100644 --- a/ydb/library/yql/sql/pg/pg_sql.cpp +++ b/ydb/library/yql/sql/pg/pg_sql.cpp @@ -1075,8 +1075,13 @@ private: for (auto i = 0; i < ListLength(pk->keys); ++i) { auto node = ListNodeNth(pk->keys, i); + auto nodeName = StrVal(node); - AddNonNullColumn(ctx, StrVal(node)); + if (!ctx.ColumnsSet.contains(nodeName)) { + AddError("PK column does not belong to table"); + return false; + } + AddNonNullColumn(ctx, nodeName); ctx.PrimaryKey.push_back(QA(StrVal(node))); } @@ -1166,11 +1171,17 @@ private: } TAstNode* BuildCreateTableOptions(TCreateTableCtx& ctx) { - return QL( - QL(QA("mode"), QA("create")), - QL(QA("columns"), QVL(ctx.Columns.data(), ctx.Columns.size())), - QL(QA("primarykey"), QVL(ctx.PrimaryKey.data(), ctx.PrimaryKey.size())), - QL(QA("notnull"), QVL(ctx.NotNullColumns.data(), ctx.NotNullColumns.size()))); + std::vector<TAstNode*> options; + + options.push_back(QL(QA("mode"), QA("create"))); + options.push_back(QL(QA("columns"), QVL(ctx.Columns.data(), ctx.Columns.size()))); + if (!ctx.PrimaryKey.empty()) { + options.push_back(QL(QA("primarykey"), QVL(ctx.PrimaryKey.data(), ctx.PrimaryKey.size()))); + } + if (!ctx.NotNullColumns.empty()) { + options.push_back(QL(QA("notnull"), QVL(ctx.NotNullColumns.data(), ctx.NotNullColumns.size()))); + } + return QVL(options.data(), options.size()); } public: diff --git a/ydb/library/yql/sql/pg/pg_sql_ut.cpp b/ydb/library/yql/sql/pg/pg_sql_ut.cpp index 728b11b65c..4966050d18 100644 --- a/ydb/library/yql/sql/pg/pg_sql_ut.cpp +++ b/ydb/library/yql/sql/pg/pg_sql_ut.cpp @@ -79,7 +79,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingOnly) { TString program = R"( ( (let world (Configure! world (DataSource 'config) 'OrderedColumns)) - (let world (Write! world (DataSink '"kikimr" '"") (Key '('tablescheme (String '"t"))) (Void) '('('mode 'create) '('columns '('('a (PgType 'int4)) '('b (PgType 'text)))) '('primarykey '()) '('notnull '())))) + (let world (Write! world (DataSink '"kikimr" '"") (Key '('tablescheme (String '"t"))) (Void) '('('mode 'create) '('columns '('('a (PgType 'int4)) '('b (PgType 'text))))))) (let world (CommitAll! world)) (return world) ) @@ -95,7 +95,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingOnly) { TString program = R"( ( (let world (Configure! world (DataSource 'config) 'OrderedColumns)) - (let world (Write! world (DataSink '"kikimr" '"") (Key '('tablescheme (String '"t"))) (Void) '('('mode 'create) '('columns '('('a (PgType 'int4)) '('b (PgType 'text)))) '('primarykey '()) '('notnull '('a))))) + (let world (Write! world (DataSink '"kikimr" '"") (Key '('tablescheme (String '"t"))) (Void) '('('mode 'create) '('columns '('('a (PgType 'int4)) '('b (PgType 'text)))) '('notnull '('a))))) (let world (CommitAll! world)) (return world) ) @@ -176,4 +176,13 @@ Y_UNIT_TEST_SUITE(PgSqlParsingOnly) { auto issue = *(res.Issues.begin()); UNIT_ASSERT(issue.GetMessage().find("duplicate") != TString::npos); } + + Y_UNIT_TEST(CreateTableStmt_PKHasColumnsNotBelongingToTable_Fails) { + auto res = PgSqlToYql("CREATE TABLE t (a int, primary key(b))"); + UNIT_ASSERT(!res.Root); + UNIT_ASSERT_EQUAL(res.Issues.Size(), 1); + + auto issue = *(res.Issues.begin()); + UNIT_ASSERT(issue.GetMessage().find("PK column does not belong to table") != TString::npos); + } } |