Skip to content

Commit 633c9e4

Browse files
committed
YQL-19747: Refactor SQL completion engine
Some refactorings to compact the code: - YQL-19747: Pass TParsedInput to visitor - YQL-19747: Add TParsedInput - YQL-19747: Cosmetics - YQL-19747: Refactor configuration - YQL-19747: Refactor name to candidate mapping commit_hash:44dfe7dc7bcc627ef9c20696077f2d962a3014f6
1 parent e99c378 commit 633c9e4

24 files changed

+336
-297
lines changed

yql/essentials/sql/v1/complete/analysis/global/column.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ namespace NSQLComplete {
220220

221221
class TVisitor: public TSQLv1NarrowingVisitor {
222222
public:
223-
TVisitor(antlr4::TokenStream* tokens, size_t cursorPosition)
224-
: TSQLv1NarrowingVisitor(tokens, cursorPosition)
223+
TVisitor(const TParsedInput& input)
224+
: TSQLv1NarrowingVisitor(input)
225225
{
226226
}
227227

@@ -247,12 +247,9 @@ namespace NSQLComplete {
247247

248248
} // namespace
249249

250-
TMaybe<TColumnContext> InferColumnContext(
251-
SQLv1::Sql_queryContext* ctx,
252-
antlr4::TokenStream* tokens,
253-
size_t cursorPosition) {
250+
TMaybe<TColumnContext> InferColumnContext(TParsedInput input) {
254251
// TODO: add utility `auto ToMaybe<T>(std::any any) -> TMaybe<T>`
255-
std::any result = TVisitor(tokens, cursorPosition).visit(ctx);
252+
std::any result = TVisitor(input).visit(input.SqlQuery);
256253
if (!result.has_value()) {
257254
return Nothing();
258255
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#pragma once
22

33
#include "global.h"
4-
#include "parse_tree.h"
4+
#include "input.h"
55

66
namespace NSQLComplete {
77

8-
TMaybe<TColumnContext> InferColumnContext(
9-
SQLv1::Sql_queryContext* ctx,
10-
antlr4::TokenStream* tokens,
11-
size_t cursorPosition);
8+
TMaybe<TColumnContext> InferColumnContext(TParsedInput input);
129

1310
} // namespace NSQLComplete

yql/essentials/sql/v1/complete/analysis/global/function.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace NSQLComplete {
88

99
class TVisitor: public TSQLv1NarrowingVisitor {
1010
public:
11-
TVisitor(antlr4::TokenStream* tokens, size_t cursorPosition)
12-
: TSQLv1NarrowingVisitor(tokens, cursorPosition)
11+
TVisitor(const TParsedInput& input)
12+
: TSQLv1NarrowingVisitor(input)
1313
{
1414
}
1515

@@ -37,11 +37,8 @@ namespace NSQLComplete {
3737

3838
} // namespace
3939

40-
TMaybe<TString> EnclosingFunction(
41-
SQLv1::Sql_queryContext* ctx,
42-
antlr4::TokenStream* tokens,
43-
size_t cursorPosition) {
44-
std::any result = TVisitor(tokens, cursorPosition).visit(ctx);
40+
TMaybe<TString> EnclosingFunction(TParsedInput input) {
41+
std::any result = TVisitor(input).visit(input.SqlQuery);
4542
if (!result.has_value()) {
4643
return Nothing();
4744
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#pragma once
22

3-
#include "parse_tree.h"
3+
#include "input.h"
44

55
#include <util/generic/maybe.h>
66
#include <util/generic/string.h>
77

88
namespace NSQLComplete {
99

10-
TMaybe<TString> EnclosingFunction(
11-
SQLv1::Sql_queryContext* ctx,
12-
antlr4::TokenStream* tokens,
13-
size_t cursorPosition);
10+
TMaybe<TString> EnclosingFunction(TParsedInput input);
1411

1512
} // namespace NSQLComplete

yql/essentials/sql/v1/complete/analysis/global/global.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "column.h"
44
#include "function.h"
5+
#include "input.h"
56
#include "named_node.h"
67
#include "parse_tree.h"
78
#include "use.h"
@@ -87,18 +88,10 @@ namespace NSQLComplete {
8788
template <bool IsAnsiLexer>
8889
class TSpecializedGlobalAnalysis: public IGlobalAnalysis {
8990
public:
90-
using TDefaultYQLGrammar = TAntlrGrammar<
91-
NALADefaultAntlr4::SQLv1Antlr4Lexer,
92-
NALADefaultAntlr4::SQLv1Antlr4Parser>;
93-
94-
using TAnsiYQLGrammar = TAntlrGrammar<
95-
NALAAnsiAntlr4::SQLv1Antlr4Lexer,
96-
NALAAnsiAntlr4::SQLv1Antlr4Parser>;
97-
98-
using G = std::conditional_t<
91+
using TLexer = std::conditional_t<
9992
IsAnsiLexer,
100-
TAnsiYQLGrammar,
101-
TDefaultYQLGrammar>;
93+
NALAAnsiAntlr4::SQLv1Antlr4Lexer,
94+
NALADefaultAntlr4::SQLv1Antlr4Lexer>;
10295

10396
TSpecializedGlobalAnalysis()
10497
: Chars_()
@@ -128,11 +121,17 @@ namespace NSQLComplete {
128121

129122
TGlobalContext ctx;
130123

131-
// TODO(YQL-19747): Add ~ParseContext(Tokens, ParseTree, CursorPosition)
132-
ctx.Use = FindUseStatement(sqlQuery, &Tokens_, input.CursorPosition, env);
133-
ctx.Names = CollectNamedNodes(sqlQuery, &Tokens_, input.CursorPosition);
134-
ctx.EnclosingFunction = EnclosingFunction(sqlQuery, &Tokens_, input.CursorPosition);
135-
ctx.Column = InferColumnContext(sqlQuery, &Tokens_, input.CursorPosition);
124+
TParsedInput parsed = {
125+
.Original = input,
126+
.Tokens = &Tokens_,
127+
.Parser = &Parser_,
128+
.SqlQuery = sqlQuery,
129+
};
130+
131+
ctx.Use = FindUseStatement(parsed, env);
132+
ctx.Names = CollectNamedNodes(parsed);
133+
ctx.EnclosingFunction = EnclosingFunction(parsed);
134+
ctx.Column = InferColumnContext(parsed);
136135

137136
if (ctx.Use && ctx.Column) {
138137
EnrichTableClusters(*ctx.Column, *ctx.Use);
@@ -176,9 +175,9 @@ namespace NSQLComplete {
176175
}
177176

178177
antlr4::ANTLRInputStream Chars_;
179-
G::TLexer Lexer_;
178+
TLexer Lexer_;
180179
antlr4::CommonTokenStream Tokens_;
181-
TDefaultYQLGrammar::TParser Parser_;
180+
SQLv1 Parser_;
182181
};
183182

184183
class TGlobalAnalysis: public IGlobalAnalysis {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "input.h"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "parse_tree.h"
4+
5+
#include <yql/essentials/sql/v1/complete/core/input.h>
6+
7+
namespace NSQLComplete {
8+
9+
struct TParsedInput {
10+
TCompletionInput Original;
11+
antlr4::CommonTokenStream* Tokens;
12+
SQLv1* Parser;
13+
SQLv1::Sql_queryContext* SqlQuery;
14+
};
15+
16+
} // namespace NSQLComplete

yql/essentials/sql/v1/complete/analysis/global/named_node.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ namespace NSQLComplete {
1414

1515
class TVisitor: public TSQLv1NarrowingVisitor {
1616
public:
17-
TVisitor(
18-
antlr4::TokenStream* tokens,
19-
size_t cursorPosition,
20-
THashSet<TString>* names)
21-
: TSQLv1NarrowingVisitor(tokens, cursorPosition)
17+
TVisitor(const TParsedInput& input, THashSet<TString>* names)
18+
: TSQLv1NarrowingVisitor(input)
2219
, Names_(names)
2320
{
2421
}
@@ -110,12 +107,9 @@ namespace NSQLComplete {
110107

111108
} // namespace
112109

113-
TVector<TString> CollectNamedNodes(
114-
SQLv1::Sql_queryContext* ctx,
115-
antlr4::TokenStream* tokens,
116-
size_t cursorPosition) {
110+
TVector<TString> CollectNamedNodes(TParsedInput input) {
117111
THashSet<TString> names;
118-
TVisitor(tokens, cursorPosition, &names).visit(ctx);
112+
TVisitor(input, &names).visit(input.SqlQuery);
119113
return TVector<TString>(begin(names), end(names));
120114
}
121115

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#pragma once
22

3-
#include "parse_tree.h"
3+
#include "input.h"
44

55
#include <util/generic/string.h>
66
#include <util/generic/vector.h>
77

88
namespace NSQLComplete {
99

10-
TVector<TString> CollectNamedNodes(
11-
SQLv1::Sql_queryContext* ctx,
12-
antlr4::TokenStream* tokens,
13-
size_t cursorPosition);
10+
TVector<TString> CollectNamedNodes(TParsedInput input);
1411

1512
} // namespace NSQLComplete

yql/essentials/sql/v1/complete/analysis/global/narrowing_visitor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace NSQLComplete {
44

5-
TSQLv1NarrowingVisitor::TSQLv1NarrowingVisitor(antlr4::TokenStream* tokens, size_t cursorPosition)
6-
: Tokens_(tokens)
7-
, CursorPosition_(cursorPosition)
5+
TSQLv1NarrowingVisitor::TSQLv1NarrowingVisitor(const TParsedInput& input)
6+
: Tokens_(input.Tokens)
7+
, CursorPosition_(input.Original.CursorPosition)
88
{
99
}
1010

0 commit comments

Comments
 (0)