diff options
author | vityaman <vityaman.dev@yandex.ru> | 2025-04-07 14:33:48 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2025-04-07 15:00:33 +0300 |
commit | c33aace83a03e01fbf260c3a28c614a58cbce812 (patch) | |
tree | ee4b771468122bad5bbcfe5a5ba5b842dfc4f561 /yql/essentials/sql/v1/complete/syntax | |
parent | a24849a5e16381ec0c969cd30331c8da1de23aba (diff) | |
download | ydb-c33aace83a03e01fbf260c3a28c614a58cbce812.tar.gz |
YQL-19747 Complete type name as a function argument
As I understand, type name should not be completed at `SELECT |`, so I added a check that we are at `invoke_expr` context.
Currently composite type keywords are suggested at `SELECT |` and also are uppercased. I will fix it separately when this merged during
- https://github.com/users/vityaman/projects/5?pane=issue&itemId=105056723&issue=vityaman%7Cydb%7C8
---
- Related to https://github.com/ydb-platform/ydb/issues/9056
- Related to https://github.com/users/vityaman/projects/5/views/1?pane=issue&itemId=105056423&issue=vityaman%7Cydb%7C7
---
Pull Request resolved: https://github.com/ytsaurus/ytsaurus/pull/1182
commit_hash:e87565867cf9fa82d9ac49a88d59b293d6686fe7
Diffstat (limited to 'yql/essentials/sql/v1/complete/syntax')
-rw-r--r-- | yql/essentials/sql/v1/complete/syntax/grammar.cpp | 14 | ||||
-rw-r--r-- | yql/essentials/sql/v1/complete/syntax/grammar.h | 2 | ||||
-rw-r--r-- | yql/essentials/sql/v1/complete/syntax/parser_call_stack.cpp | 26 |
3 files changed, 36 insertions, 6 deletions
diff --git a/yql/essentials/sql/v1/complete/syntax/grammar.cpp b/yql/essentials/sql/v1/complete/syntax/grammar.cpp index b4f64630f77..252deaf682c 100644 --- a/yql/essentials/sql/v1/complete/syntax/grammar.cpp +++ b/yql/essentials/sql/v1/complete/syntax/grammar.cpp @@ -7,7 +7,7 @@ namespace NSQLComplete { class TSqlGrammar: public ISqlGrammar { public: TSqlGrammar(const NSQLReflect::TLexerGrammar& grammar) - : Vocabulary(GetVocabularyP()) + : Parser(MakeDummyParser()) , AllTokens(ComputeAllTokens()) , KeywordTokens(ComputeKeywordTokens(grammar)) , PunctuationTokens(ComputePunctuationTokens(grammar)) @@ -15,7 +15,7 @@ namespace NSQLComplete { } const antlr4::dfa::Vocabulary& GetVocabulary() const override { - return *Vocabulary; + return Parser->getVocabulary(); } const std::unordered_set<TTokenId>& GetAllTokens() const override { @@ -30,9 +30,13 @@ namespace NSQLComplete { return PunctuationTokens; } + const std::string& SymbolizedRule(TRuleId rule) const override { + return Parser->getRuleNames().at(rule); + } + private: - static const antlr4::dfa::Vocabulary* GetVocabularyP() { - return &NALADefaultAntlr4::SQLv1Antlr4Parser(nullptr).getVocabulary(); + static THolder<antlr4::Parser> MakeDummyParser() { + return MakeHolder<NALADefaultAntlr4::SQLv1Antlr4Parser>(nullptr); } std::unordered_set<TTokenId> ComputeAllTokens() { @@ -72,7 +76,7 @@ namespace NSQLComplete { return punctuationTokens; } - const antlr4::dfa::Vocabulary* Vocabulary; + const THolder<antlr4::Parser> Parser; const std::unordered_set<TTokenId> AllTokens; const std::unordered_set<TTokenId> KeywordTokens; const std::unordered_set<TTokenId> PunctuationTokens; diff --git a/yql/essentials/sql/v1/complete/syntax/grammar.h b/yql/essentials/sql/v1/complete/syntax/grammar.h index a349bd4a3de..b128129e95f 100644 --- a/yql/essentials/sql/v1/complete/syntax/grammar.h +++ b/yql/essentials/sql/v1/complete/syntax/grammar.h @@ -5,6 +5,7 @@ #include <contrib/libs/antlr4_cpp_runtime/src/Vocabulary.h> #include <unordered_set> +#include <string> #ifdef TOKEN_QUERY // Conflict with the winnt.h #undef TOKEN_QUERY @@ -19,6 +20,7 @@ namespace NSQLComplete { class ISqlGrammar { public: virtual const antlr4::dfa::Vocabulary& GetVocabulary() const = 0; + virtual const std::string& SymbolizedRule(TRuleId rule) const = 0; virtual const std::unordered_set<TTokenId>& GetAllTokens() const = 0; virtual const std::unordered_set<TTokenId>& GetKeywordTokens() const = 0; virtual const std::unordered_set<TTokenId>& GetPunctuationTokens() const = 0; diff --git a/yql/essentials/sql/v1/complete/syntax/parser_call_stack.cpp b/yql/essentials/sql/v1/complete/syntax/parser_call_stack.cpp index 855d9af1601..57e058fa900 100644 --- a/yql/essentials/sql/v1/complete/syntax/parser_call_stack.cpp +++ b/yql/essentials/sql/v1/complete/syntax/parser_call_stack.cpp @@ -6,6 +6,9 @@ #include <util/generic/algorithm.h> #include <util/generic/yexception.h> +#define DEBUG_SYMBOLIZE_STACK(stack) \ + auto debug_symbolized_##stack = Symbolized(stack) + namespace NSQLComplete { const TVector<TRuleId> KeywordRules = { @@ -23,6 +26,7 @@ namespace NSQLComplete { const TVector<TRuleId> TypeNameRules = { RULE(Type_name_simple), + RULE(An_id_or_type), }; const TVector<TRuleId> FunctionNameRules = { @@ -31,6 +35,17 @@ namespace NSQLComplete { RULE(Id_or_type), }; + TVector<std::string> Symbolized(const TParserCallStack& stack) { + const ISqlGrammar& grammar = GetSqlGrammar(); + + TVector<std::string> symbolized; + symbolized.reserve(stack.size()); + for (const TRuleId& rule : stack) { + symbolized.emplace_back(grammar.SymbolizedRule(rule)); + } + return symbolized; + } + bool EndsWith(const TParserCallStack& suffix, const TParserCallStack& stack) { if (stack.size() < suffix.size()) { return false; @@ -39,12 +54,21 @@ namespace NSQLComplete { return Equal(std::begin(stack) + prefixSize, std::end(stack), std::begin(suffix)); } + bool Contains(const TParserCallStack& sequence, const TParserCallStack& stack) { + return !std::ranges::search(stack, sequence).empty(); + } + bool ContainsRule(TRuleId rule, const TParserCallStack& stack) { return Find(stack, rule) != std::end(stack); } bool IsLikelyTypeStack(const TParserCallStack& stack) { - return EndsWith({RULE(Type_name_simple)}, stack); + return EndsWith({RULE(Type_name_simple)}, stack) || + (Contains({RULE(Invoke_expr), + RULE(Named_expr_list), + RULE(Named_expr), + RULE(Expr)}, stack) && + EndsWith({RULE(Atom_expr), RULE(An_id_or_type)}, stack)); } bool IsLikelyFunctionStack(const TParserCallStack& stack) { |