diff options
author | zverevgeny <zverevgeny@ydb.tech> | 2023-07-24 19:36:20 +0300 |
---|---|---|
committer | zverevgeny <zverevgeny@ydb.tech> | 2023-07-24 19:36:20 +0300 |
commit | f2b5a3362738b5cc60ea626eac83022856851412 (patch) | |
tree | 0b15c0dc1a618161cb78c658d8b3e5a9c3eae83a | |
parent | bbcb255d548e74015d75cfdae2f7433ba94360eb (diff) | |
download | ydb-f2b5a3362738b5cc60ea626eac83022856851412.tar.gz |
YQL-16222 fix in tests the list of tokens currently forbidden as column names
-rw-r--r-- | ydb/library/yql/sql/v1/format/sql_format.cpp | 62 | ||||
-rw-r--r-- | ydb/library/yql/sql/v1/format/sql_format.h | 2 | ||||
-rw-r--r-- | ydb/library/yql/sql/v1/sql_ut.cpp | 30 | ||||
-rw-r--r-- | ydb/library/yql/sql/v1/ut/ya.make | 1 |
4 files changed, 61 insertions, 34 deletions
diff --git a/ydb/library/yql/sql/v1/format/sql_format.cpp b/ydb/library/yql/sql/v1/format/sql_format.cpp index 05d04e846a..e88073d249 100644 --- a/ydb/library/yql/sql/v1/format/sql_format.cpp +++ b/ydb/library/yql/sql/v1/format/sql_format.cpp @@ -211,37 +211,6 @@ enum class EScope { DoubleQuestion }; -THashSet<TString> GetKeywords() { - TString grammar; - Y_ENSURE(NResource::FindExact("SQLv1.g.in", &grammar)); - THashSet<TString> res; - TVector<TString> lines; - Split(grammar, "\n", lines); - for (auto s : lines) { - s = StripString(s); - if (s.StartsWith("//")) { - continue; - } - - auto pos1 = s.find(':'); - auto pos2 = s.find(';'); - if (pos1 == TString::npos || pos2 == TString::npos || pos2 < pos1 + 2) { - continue; - } - - auto before = s.substr(0, pos1); - auto after = s.substr(pos1 + 1, pos2 - pos1 - 1); - SubstGlobal(after, " ", ""); - SubstGlobal(after, "'", ""); - if (after == before) { - //Cerr << before << "\n"; - res.insert(before); - } - } - - return res; -} - class TVisitor; using TFunctor = std::function<void(TVisitor&, const NProtoBuf::Message& msg)>; @@ -2326,4 +2295,35 @@ bool SqlFormatSimple(const TString& query, TString& formattedQuery, TString& err } } +THashSet<TString> GetKeywords() { + TString grammar; + Y_ENSURE(NResource::FindExact("SQLv1.g.in", &grammar)); + THashSet<TString> res; + TVector<TString> lines; + Split(grammar, "\n", lines); + for (auto s : lines) { + s = StripString(s); + if (s.StartsWith("//")) { + continue; + } + + auto pos1 = s.find(':'); + auto pos2 = s.find(';'); + if (pos1 == TString::npos || pos2 == TString::npos || pos2 < pos1 + 2) { + continue; + } + + auto before = s.substr(0, pos1); + auto after = s.substr(pos1 + 1, pos2 - pos1 - 1); + SubstGlobal(after, " ", ""); + SubstGlobal(after, "'", ""); + if (after == before) { + //Cerr << before << "\n"; + res.insert(before); + } + } + + return res; +} + } // namespace NSQLFormat diff --git a/ydb/library/yql/sql/v1/format/sql_format.h b/ydb/library/yql/sql/v1/format/sql_format.h index 5c6f3d23b8..3170efe348 100644 --- a/ydb/library/yql/sql/v1/format/sql_format.h +++ b/ydb/library/yql/sql/v1/format/sql_format.h @@ -24,4 +24,6 @@ TString MutateQuery(const TString& query, const NSQLTranslation::TTranslationSet bool SqlFormatSimple(const TString& query, TString& formattedQuery, TString& error); +THashSet<TString> GetKeywords(); + } diff --git a/ydb/library/yql/sql/v1/sql_ut.cpp b/ydb/library/yql/sql/v1/sql_ut.cpp index 0718c3c64c..ed8988c5b8 100644 --- a/ydb/library/yql/sql/v1/sql_ut.cpp +++ b/ydb/library/yql/sql/v1/sql_ut.cpp @@ -1,4 +1,4 @@ - +#include "format/sql_format.h" #include <ydb/library/yql/providers/common/provider/yql_provider_names.h> #include <ydb/library/yql/sql/sql.h> #include <util/generic/map.h> @@ -198,8 +198,32 @@ Y_UNIT_TEST_SUITE(AnsiMode) { } Y_UNIT_TEST_SUITE(SqlParsingOnly) { - Y_UNIT_TEST(CoverColumnName) { - UNIT_ASSERT(SqlToYql("SELECT cover FROM plato.Input").IsOk()); + Y_UNIT_TEST(TokensAsColumnName) { + const auto& forbidden = THashSet<TString>{ + "ALL", "ANY", "AS", "ASSUME", "AUTOMAP", "BETWEEN", "BITCAST", + "CALLABLE", "CASE", "CAST", "CUBE", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", + "DICT", "DISTINCT", "ENUM", "ERASE", "EXCEPT", "EXISTS", "FLOW", "FROM", "FULL", "GLOBAL", + "HAVING", "HOP", "INTERSECT", "JSON_EXISTS", "JSON_QUERY", "JSON_VALUE", "LIMIT", "LIST", "LOCAL", + "NOT", "OPTIONAL", "PROCESS", "REDUCE", "REPEATABLE", "RESOURCE", "RETURN", "ROLLUP", + "SELECT", "SET", "STREAM", "STRUCT", "TAGGED", "TUPLE", "UNBOUNDED", "UNION", "VARIANT", + "WHEN", "WHERE", "WINDOW", "WITHOUT" + }; + + THashMap<TString, bool> tokens; + for (const auto& t: NSQLFormat::GetKeywords()) { + tokens[t] = !forbidden.contains((t)); + } + for (const auto& f: forbidden) { + UNIT_ASSERT(tokens.contains(f)); //check that forbidden list contains tokens only(self check) + } + TStringBuilder failed; + for (const auto& [token, allowed]: tokens) { + TStringBuilder req; + req << "SELECT " << token << " FROM plato.Input"; + if (SqlToYql(req).IsOk() != allowed) + failed << token << " "; + } + UNIT_ASSERT_EQUAL_C(TString{}, failed, failed); } Y_UNIT_TEST(TableHints) { diff --git a/ydb/library/yql/sql/v1/ut/ya.make b/ydb/library/yql/sql/v1/ut/ya.make index 9b30b19e71..bb048bb4b2 100644 --- a/ydb/library/yql/sql/v1/ut/ya.make +++ b/ydb/library/yql/sql/v1/ut/ya.make @@ -8,6 +8,7 @@ PEERDIR( ydb/library/yql/public/udf/service/exception_policy ydb/library/yql/sql ydb/library/yql/sql/pg_dummy + ydb/library/yql/sql/v1/format ) TIMEOUT(300) |