diff options
author | qrort <qrort@yandex-team.com> | 2023-03-02 17:47:08 +0300 |
---|---|---|
committer | qrort <qrort@yandex-team.com> | 2023-03-02 17:47:08 +0300 |
commit | 9f2f2091b3bef79e2599a80d4bb0d962d1e20b3a (patch) | |
tree | 9f89866faead2a4154f2fe778b704ca1c665dab1 | |
parent | 46106987bf9ad142884f9f94b1326ded3e96f96c (diff) | |
download | ydb-9f2f2091b3bef79e2599a80d4bb0d962d1e20b3a.tar.gz |
ParseDeleteStmt
-rw-r--r-- | ydb/library/yql/sql/pg/CMakeLists.darwin.txt | 1 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/CMakeLists.linux-aarch64.txt | 1 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/CMakeLists.linux.txt | 1 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/pg_sql.cpp | 94 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/pg_sql_ut.cpp | 73 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/ut/CMakeLists.darwin.txt | 66 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/ut/CMakeLists.linux-aarch64.txt | 70 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/ut/CMakeLists.linux.txt | 72 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/ut/CMakeLists.txt | 15 |
9 files changed, 390 insertions, 3 deletions
diff --git a/ydb/library/yql/sql/pg/CMakeLists.darwin.txt b/ydb/library/yql/sql/pg/CMakeLists.darwin.txt index 3883f3c467b..2337fcbd452 100644 --- a/ydb/library/yql/sql/pg/CMakeLists.darwin.txt +++ b/ydb/library/yql/sql/pg/CMakeLists.darwin.txt @@ -6,6 +6,7 @@ # original buildsystem will not be accepted. +add_subdirectory(ut) add_library(yql-sql-pg) target_compile_options(yql-sql-pg PRIVATE diff --git a/ydb/library/yql/sql/pg/CMakeLists.linux-aarch64.txt b/ydb/library/yql/sql/pg/CMakeLists.linux-aarch64.txt index b5677d41571..477c06a316c 100644 --- a/ydb/library/yql/sql/pg/CMakeLists.linux-aarch64.txt +++ b/ydb/library/yql/sql/pg/CMakeLists.linux-aarch64.txt @@ -6,6 +6,7 @@ # original buildsystem will not be accepted. +add_subdirectory(ut) add_library(yql-sql-pg) target_compile_options(yql-sql-pg PRIVATE diff --git a/ydb/library/yql/sql/pg/CMakeLists.linux.txt b/ydb/library/yql/sql/pg/CMakeLists.linux.txt index b5677d41571..477c06a316c 100644 --- a/ydb/library/yql/sql/pg/CMakeLists.linux.txt +++ b/ydb/library/yql/sql/pg/CMakeLists.linux.txt @@ -6,6 +6,7 @@ # original buildsystem will not be accepted. +add_subdirectory(ut) add_library(yql-sql-pg) target_compile_options(yql-sql-pg PRIVATE diff --git a/ydb/library/yql/sql/pg/pg_sql.cpp b/ydb/library/yql/sql/pg/pg_sql.cpp index 9e17d17d2df..635951202fb 100644 --- a/ydb/library/yql/sql/pg/pg_sql.cpp +++ b/ydb/library/yql/sql/pg/pg_sql.cpp @@ -169,7 +169,7 @@ public: bool InjectRead = false; }; - struct TInsertDesc { + struct TWriteRangeDesc { TAstNode* Sink = nullptr; TAstNode* Key = nullptr; }; @@ -271,6 +271,8 @@ public: return ParseDropStmt(CAST_NODE(DropStmt, node)) != nullptr; case T_VariableSetStmt: return ParseVariableSetStmt(CAST_NODE(VariableSetStmt, node)) != nullptr; + case T_DeleteStmt: + return ParseDeleteStmt(CAST_NODE(DeleteStmt, node)) != nullptr; default: NodeNotImplemented(value, node); return false; @@ -1137,6 +1139,88 @@ public: return Statements.back(); } + [[nodiscard]] + TAstNode* ParseDeleteStmt(const DeleteStmt* value) { + if (value->usingClause) { + AddError("using is not supported"); + return nullptr; + } + if (value->returningList) { + AddError("returning is not supported"); + return nullptr; + } + if (value->withClause) { + AddError("with is not supported"); + return nullptr; + } + + if (!value->relation) { + AddError("DeleteStmt: expected relation"); + return nullptr; + } + + TVector<TAstNode*> fromList; + auto p = ParseRangeVar(value->relation); + if (!p.Source) { + return nullptr; + } + AddFrom(p, fromList); + + TAstNode* whereFilter = nullptr; + if (value->whereClause) { + TExprSettings settings; + settings.AllowColumns = true; + settings.AllowSubLinks = true; + settings.Scope = "WHERE"; + whereFilter = ParseExpr(value->whereClause, settings); + if (!whereFilter) { + return nullptr; + } + } + + TAstNode* starLambda = L(A("lambda"), QL(), L(A("PgStar"))); + TAstNode* resultItem = L(A("PgResultItem"), QAX(""), L(A("Void")), starLambda); + + TVector<TAstNode*> setItemOptions; + + setItemOptions.push_back(QL(QA("result"), QVL(resultItem))); + setItemOptions.push_back(QL(QA("from"), QVL(fromList.data(), fromList.size()))); + setItemOptions.push_back(QL(QA("join_ops"), QVL(QL()))); + + NYql::TAstNode* lambda = nullptr; + if (whereFilter) { + lambda = L(A("lambda"), QL(), whereFilter); + setItemOptions.push_back(QL(QA("where"), L(A("PgWhere"), L(A("Void")), lambda))); + } + + auto setItemNode = L(A("PgSetItem"), QVL(setItemOptions.data(), setItemOptions.size())); + + TVector<TAstNode*> selectOptions; + selectOptions.push_back(QL(QA("set_items"), QVL(setItemNode))); + selectOptions.push_back(QL(QA("set_ops"), QVL(QA("push")))); + + auto select = L(A("PgSelect"), QVL(selectOptions.data(), selectOptions.size())); + + auto [sink, key] = ParseWriteRangeVar(value->relation); + + Statements.push_back(L( + A("let"), + A("world"), + L( + A("Write!"), + A("world"), + sink, + key, + L(A("Void")), + QL( + QL(QA("pg_delete"), select), + QL(QA("mode"), QA("delete")) + ) + ) + )); + return Statements.back(); + } + TFromDesc ParseFromClause(const Node* node) { switch (NodeTag(node)) { case T_RangeVar: @@ -1185,7 +1269,7 @@ public: return true; } - TInsertDesc ParseWriteRangeVar(const RangeVar* value) { + TWriteRangeDesc ParseWriteRangeVar(const RangeVar* value) { AT_LOCATION(value); if (StrLength(value->catalogname) > 0) { AddError("catalogname is not supported"); @@ -1210,7 +1294,7 @@ public: } auto sink = L(A("DataSink"), QAX(*p), QAX(schemaname)); - auto key = L(A("Key"), QL(QA("table"), L(A("String"), QAX(value->relname)))); + auto key = L(A("Key"), QL(QA("table"), L(A("String"), QAX(TablePathPrefix + value->relname)))); return { sink, key }; } @@ -2674,6 +2758,10 @@ public: return Q(VL(nodes, size, pos), pos); } + TAstNode* QVL(TAstNode* node, TPosition pos = {}) { + return QVL(&node, 1, pos); + } + TAstNode* A(const TString& str, TPosition pos = {}, ui32 flags = 0) { return TAstNode::NewAtom(pos.Row ? pos : Positions.back(), str, *AstParseResult.Pool, flags); } diff --git a/ydb/library/yql/sql/pg/pg_sql_ut.cpp b/ydb/library/yql/sql/pg/pg_sql_ut.cpp new file mode 100644 index 00000000000..eb29a07cc85 --- /dev/null +++ b/ydb/library/yql/sql/pg/pg_sql_ut.cpp @@ -0,0 +1,73 @@ +#include <ydb/library/yql/ast/yql_expr.h> +#include <ydb/library/yql/providers/common/provider/yql_provider_names.h> +#include <ydb/library/yql/sql/sql.h> + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NSQLTranslation; + +enum class EDebugOutput { + None, + ToCerr, +}; + +TString Err2Str(NYql::TAstParseResult& res, EDebugOutput debug = EDebugOutput::None) { + TStringStream s; + res.Issues.PrintTo(s); + + if (debug == EDebugOutput::ToCerr) { + Cerr << s.Str() << Endl; + } + return s.Str(); +} + +NYql::TAstParseResult SqlToYqlWithMode(const TString& query, NSQLTranslation::ESqlMode mode = NSQLTranslation::ESqlMode::QUERY, size_t maxErrors = 10, const TString& provider = {}, + EDebugOutput debug = EDebugOutput::None, bool ansiLexer = false, NSQLTranslation::TTranslationSettings settings = {}) +{ + google::protobuf::Arena arena; + const auto service = provider ? provider : TString(NYql::YtProviderName); + const TString cluster = "plato"; + settings.ClusterMapping[cluster] = service; + settings.ClusterMapping["hahn"] = NYql::YtProviderName; + settings.ClusterMapping["mon"] = NYql::SolomonProviderName; + settings.MaxErrors = maxErrors; + settings.Mode = mode; + settings.Arena = &arena; + settings.AnsiLexer = ansiLexer; + settings.SyntaxVersion = 1; + auto q = TStringBuilder() << "--!syntax_pg\n" << query; + auto res = SqlToYql(q, settings); + if (debug == EDebugOutput::ToCerr) { + Err2Str(res, debug); + } + return res; +} + +NYql::TAstParseResult PgSqlToYql(const TString& query, size_t maxErrors = 10, const TString& provider = {}, EDebugOutput debug = EDebugOutput::None) { + return SqlToYqlWithMode(query, NSQLTranslation::ESqlMode::QUERY, maxErrors, provider, debug); +} + +Y_UNIT_TEST_SUITE(PgSqlParsingOnly) { + Y_UNIT_TEST(InsertStmt) { + auto res = PgSqlToYql("INSERT INTO plato.Input VALUES (1, 1)"); + UNIT_ASSERT(res.Root); + res.Root->PrintTo(Cerr); + } + + Y_UNIT_TEST(DeleteStmt) { + auto res = PgSqlToYql("DELETE FROM plato.Input"); + UNIT_ASSERT(res.Root); + TString program = R"( + ( + (let world (Configure! world (DataSource 'config) 'OrderedColumns)) + (let read0 (Read! world (DataSource '"yt" '"plato") (Key '('table (String '"input"))) (Void) '())) + (let world (Left! read0)) + (let world (Write! world (DataSink '"yt" '"plato") (Key '('table (String '"input"))) (Void) '('('pg_delete (PgSelect '('('set_items '((PgSetItem '('('result '((PgResultItem '"" (Void) (lambda '() (PgStar))))) '('from '('((Right! read0) '"input" '()))) '('join_ops '('())))))) '('set_ops '('push))))) '('mode 'delete)))) + (let world (CommitAll! world)) + (return world) + ) + )"; + const auto expectedAst = NYql::ParseAst(program); + UNIT_ASSERT_STRINGS_EQUAL(res.Root->ToString(), expectedAst.Root->ToString()); + } +} diff --git a/ydb/library/yql/sql/pg/ut/CMakeLists.darwin.txt b/ydb/library/yql/sql/pg/ut/CMakeLists.darwin.txt new file mode 100644 index 00000000000..e9d6361703b --- /dev/null +++ b/ydb/library/yql/sql/pg/ut/CMakeLists.darwin.txt @@ -0,0 +1,66 @@ + +# This file was generated by the build system used internally in the Yandex monorepo. +# Only simple modifications are allowed (adding source-files to targets, adding simple properties +# like target_include_directories). These modifications will be ported to original +# ya.make files by maintainers. Any complex modifications which can't be ported back to the +# original buildsystem will not be accepted. + + + +add_executable(ydb-library-yql-sql-pg-ut) +target_include_directories(ydb-library-yql-sql-pg-ut PRIVATE + ${CMAKE_SOURCE_DIR}/ydb/library/yql/sql/pg +) +target_link_libraries(ydb-library-yql-sql-pg-ut PUBLIC + contrib-libs-cxxsupp + yutil + cpp-malloc-system + library-cpp-cpuid_check + cpp-testing-unittest_main + yql-sql-pg + udf-service-exception_policy + library-yql-sql +) +target_link_options(ydb-library-yql-sql-pg-ut PRIVATE + -Wl,-no_deduplicate + -Wl,-sdk_version,10.15 + -fPIC + -fPIC +) +target_sources(ydb-library-yql-sql-pg-ut PRIVATE + ${CMAKE_SOURCE_DIR}/ydb/library/yql/sql/pg/pg_sql_ut.cpp +) +set_property( + TARGET + ydb-library-yql-sql-pg-ut + PROPERTY + SPLIT_FACTOR + 1 +) +add_yunittest( + NAME + ydb-library-yql-sql-pg-ut + TEST_TARGET + ydb-library-yql-sql-pg-ut + TEST_ARG + --print-before-suite + --print-before-test + --fork-tests + --print-times + --show-fails +) +set_yunittest_property( + TEST + ydb-library-yql-sql-pg-ut + PROPERTY + LABELS + MEDIUM +) +set_yunittest_property( + TEST + ydb-library-yql-sql-pg-ut + PROPERTY + PROCESSORS + 1 +) +vcs_info(ydb-library-yql-sql-pg-ut) diff --git a/ydb/library/yql/sql/pg/ut/CMakeLists.linux-aarch64.txt b/ydb/library/yql/sql/pg/ut/CMakeLists.linux-aarch64.txt new file mode 100644 index 00000000000..52db0f08069 --- /dev/null +++ b/ydb/library/yql/sql/pg/ut/CMakeLists.linux-aarch64.txt @@ -0,0 +1,70 @@ + +# This file was generated by the build system used internally in the Yandex monorepo. +# Only simple modifications are allowed (adding source-files to targets, adding simple properties +# like target_include_directories). These modifications will be ported to original +# ya.make files by maintainers. Any complex modifications which can't be ported back to the +# original buildsystem will not be accepted. + + + +add_executable(ydb-library-yql-sql-pg-ut) +target_include_directories(ydb-library-yql-sql-pg-ut PRIVATE + ${CMAKE_SOURCE_DIR}/ydb/library/yql/sql/pg +) +target_link_libraries(ydb-library-yql-sql-pg-ut PUBLIC + contrib-libs-linux-headers + contrib-libs-cxxsupp + yutil + library-cpp-lfalloc + cpp-testing-unittest_main + yql-sql-pg + udf-service-exception_policy + library-yql-sql +) +target_link_options(ydb-library-yql-sql-pg-ut PRIVATE + -ldl + -lrt + -Wl,--no-as-needed + -fPIC + -fPIC + -lpthread + -lrt + -ldl +) +target_sources(ydb-library-yql-sql-pg-ut PRIVATE + ${CMAKE_SOURCE_DIR}/ydb/library/yql/sql/pg/pg_sql_ut.cpp +) +set_property( + TARGET + ydb-library-yql-sql-pg-ut + PROPERTY + SPLIT_FACTOR + 1 +) +add_yunittest( + NAME + ydb-library-yql-sql-pg-ut + TEST_TARGET + ydb-library-yql-sql-pg-ut + TEST_ARG + --print-before-suite + --print-before-test + --fork-tests + --print-times + --show-fails +) +set_yunittest_property( + TEST + ydb-library-yql-sql-pg-ut + PROPERTY + LABELS + MEDIUM +) +set_yunittest_property( + TEST + ydb-library-yql-sql-pg-ut + PROPERTY + PROCESSORS + 1 +) +vcs_info(ydb-library-yql-sql-pg-ut) diff --git a/ydb/library/yql/sql/pg/ut/CMakeLists.linux.txt b/ydb/library/yql/sql/pg/ut/CMakeLists.linux.txt new file mode 100644 index 00000000000..ff508ea7057 --- /dev/null +++ b/ydb/library/yql/sql/pg/ut/CMakeLists.linux.txt @@ -0,0 +1,72 @@ + +# This file was generated by the build system used internally in the Yandex monorepo. +# Only simple modifications are allowed (adding source-files to targets, adding simple properties +# like target_include_directories). These modifications will be ported to original +# ya.make files by maintainers. Any complex modifications which can't be ported back to the +# original buildsystem will not be accepted. + + + +add_executable(ydb-library-yql-sql-pg-ut) +target_include_directories(ydb-library-yql-sql-pg-ut PRIVATE + ${CMAKE_SOURCE_DIR}/ydb/library/yql/sql/pg +) +target_link_libraries(ydb-library-yql-sql-pg-ut PUBLIC + contrib-libs-linux-headers + contrib-libs-cxxsupp + yutil + cpp-malloc-tcmalloc + libs-tcmalloc-no_percpu_cache + library-cpp-cpuid_check + cpp-testing-unittest_main + yql-sql-pg + udf-service-exception_policy + library-yql-sql +) +target_link_options(ydb-library-yql-sql-pg-ut PRIVATE + -ldl + -lrt + -Wl,--no-as-needed + -fPIC + -fPIC + -lpthread + -lrt + -ldl +) +target_sources(ydb-library-yql-sql-pg-ut PRIVATE + ${CMAKE_SOURCE_DIR}/ydb/library/yql/sql/pg/pg_sql_ut.cpp +) +set_property( + TARGET + ydb-library-yql-sql-pg-ut + PROPERTY + SPLIT_FACTOR + 1 +) +add_yunittest( + NAME + ydb-library-yql-sql-pg-ut + TEST_TARGET + ydb-library-yql-sql-pg-ut + TEST_ARG + --print-before-suite + --print-before-test + --fork-tests + --print-times + --show-fails +) +set_yunittest_property( + TEST + ydb-library-yql-sql-pg-ut + PROPERTY + LABELS + MEDIUM +) +set_yunittest_property( + TEST + ydb-library-yql-sql-pg-ut + PROPERTY + PROCESSORS + 1 +) +vcs_info(ydb-library-yql-sql-pg-ut) diff --git a/ydb/library/yql/sql/pg/ut/CMakeLists.txt b/ydb/library/yql/sql/pg/ut/CMakeLists.txt new file mode 100644 index 00000000000..5bb4faffb40 --- /dev/null +++ b/ydb/library/yql/sql/pg/ut/CMakeLists.txt @@ -0,0 +1,15 @@ + +# This file was generated by the build system used internally in the Yandex monorepo. +# Only simple modifications are allowed (adding source-files to targets, adding simple properties +# like target_include_directories). These modifications will be ported to original +# ya.make files by maintainers. Any complex modifications which can't be ported back to the +# original buildsystem will not be accepted. + + +if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" AND UNIX AND NOT APPLE AND NOT ANDROID) + include(CMakeLists.linux-aarch64.txt) +elseif (APPLE AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") + include(CMakeLists.darwin.txt) +elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND UNIX AND NOT APPLE AND NOT ANDROID) + include(CMakeLists.linux.txt) +endif() |