diff options
author | vvvv <vvvv@ydb.tech> | 2023-07-22 22:39:24 +0300 |
---|---|---|
committer | vvvv <vvvv@ydb.tech> | 2023-07-22 22:39:24 +0300 |
commit | c402475a0a16ee3bb780ef7849a19bac8956e21f (patch) | |
tree | ef60b8dec21d1a6b8254375d5957d1c4967f3815 | |
parent | d353941db5dbb5e834f9422c4a372f0c634643d9 (diff) | |
download | ydb-c402475a0a16ee3bb780ef7849a19bac8956e21f.tar.gz |
Find balanced brackets inside expressions only + check for double format for all tests
-rw-r--r-- | ydb/library/yql/sql/v1/format/sql_format.cpp | 36 | ||||
-rw-r--r-- | ydb/library/yql/sql/v1/format/sql_format_ut.cpp | 12 |
2 files changed, 36 insertions, 12 deletions
diff --git a/ydb/library/yql/sql/v1/format/sql_format.cpp b/ydb/library/yql/sql/v1/format/sql_format.cpp index 9bdf71f73d1..05d04e846a7 100644 --- a/ydb/library/yql/sql/v1/format/sql_format.cpp +++ b/ydb/library/yql/sql/v1/format/sql_format.cpp @@ -354,6 +354,7 @@ private: Scopes.push_back(*scopePtr); } + bool suppressExpr = false; if (descr == TToken::GetDescriptor()) { const auto& token = dynamic_cast<const TToken&>(msg); MarkToken(token); @@ -366,11 +367,30 @@ private: } else if (descr == TRule_in_atom_expr::GetDescriptor()) { const auto& value = dynamic_cast<const TRule_in_atom_expr&>(msg); if (value.Alt_case() == TRule_in_atom_expr::kAltInAtomExpr7) { - AfterInAtom = true; + suppressExpr = true; } + } else if (descr == TRule_select_kind_parenthesis::GetDescriptor()) { + const auto& value = dynamic_cast<const TRule_select_kind_parenthesis&>(msg); + if (value.Alt_case() == TRule_select_kind_parenthesis::kAltSelectKindParenthesis2) { + suppressExpr = true; + } + } + + const bool expr = (descr == TRule_expr::GetDescriptor() || descr == TRule_in_expr::GetDescriptor()); + if (expr) { + ++InsideExpr; + } + + ui64 prevInsideExpr = InsideExpr; + if (suppressExpr) { + InsideExpr = 0; } VisitAllFieldsImpl<&TVisitor::MarkTokens>(descr, msg); + if (suppressExpr) { + InsideExpr = prevInsideExpr; + } + if (scopePtr) { if (*scopePtr == EScope::TypeName) { --InsideType; @@ -378,6 +398,10 @@ private: Scopes.pop_back(); } + + if (expr) { + --InsideExpr; + } } void MarkToken(const TToken& token) { @@ -390,7 +414,7 @@ private: if (str == "(" || str == "[" || str == "{" || str == "<|" || (InsideType && str == "<")) { MarkTokenStack.push_back(TokenIndex); auto& info = MarkedTokens[TokenIndex]; - info.OpeningBracket = true; + info.OpeningBracket = (InsideExpr > 0); } else if (str == ")") { PopBracket("("); } else if (str == "]") { @@ -403,12 +427,6 @@ private: PopBracket("<"); } - if (AfterInAtom) { - auto& info = MarkedTokens[TokenIndex]; - info.OpeningBracket = false; - AfterInAtom = false; - } - TokenIndex++; } @@ -2019,7 +2037,7 @@ private: ui32 TokenIndex = 0; TMarkTokenStack MarkTokenStack; TVector<TTokenInfo> MarkedTokens; - bool AfterInAtom = false; + ui64 InsideExpr = 0; }; template <typename T> diff --git a/ydb/library/yql/sql/v1/format/sql_format_ut.cpp b/ydb/library/yql/sql/v1/format/sql_format_ut.cpp index 8c7daab35f6..5b651390766 100644 --- a/ydb/library/yql/sql/v1/format/sql_format_ut.cpp +++ b/ydb/library/yql/sql/v1/format/sql_format_ut.cpp @@ -26,9 +26,15 @@ struct TSetup { auto expected = c.second; SubstGlobal(expected, "\t", TString(NSQLFormat::OneIndent, ' ')); UNIT_ASSERT_NO_DIFF(formatted, expected); - auto mutatedQuery = NSQLFormat::MutateQuery(c.first); - auto res2 = Formatter->Format(mutatedQuery, formatted, issues); + + TString formatted2; + auto res2 = Formatter->Format(formatted, formatted2, issues); UNIT_ASSERT_C(res2, issues.ToString()); + UNIT_ASSERT_NO_DIFF(formatted, formatted2); + + auto mutatedQuery = NSQLFormat::MutateQuery(c.first); + auto res3 = Formatter->Format(mutatedQuery, formatted, issues); + UNIT_ASSERT_C(res3, issues.ToString()); } } @@ -207,7 +213,7 @@ Y_UNIT_TEST_SUITE(CheckSqlFormatter) { TCases cases = { {"values (1);","VALUES\n\t(1);\n\n"}, {"values (1,2),(3,4);","VALUES\n\t(1, 2),\n\t(3, 4);\n\n"}, - {"values ('a\nb');","VALUES\n\t(\n\t\t'a\nb'\n\t);\n\n"}, + {"values ('a\nb');","VALUES\n\t('a\nb');\n\n"}, }; TSetup setup; |