1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#include "sql.h"
#include <yql/essentials/core/issue/yql_issue.h>
#include <yql/essentials/sql/v0/sql.h>
#include <yql/essentials/sql/v0/lexer/lexer.h>
#include <yql/essentials/sql/v1/sql.h>
#include <yql/essentials/sql/v1/lexer/lexer.h>
#include <yql/essentials/sql/v1/proto_parser/proto_parser.h>
#include <yql/essentials/parser/pg_wrapper/interface/parser.h>
#include <google/protobuf/arena.h>
#include <util/string/builder.h>
namespace NSQLTranslation {
NYql::TAstParseResult SqlToYql(const TString& query, const TTranslationSettings& settings,
NYql::TWarningRules* warningRules, NYql::TStmtParseInfo* stmtParseInfo, TTranslationSettings* effectiveSettings)
{
NYql::TAstParseResult result;
TTranslationSettings parsedSettings(settings);
if (!ParseTranslationSettings(query, parsedSettings, result.Issues)) {
return result;
}
if (effectiveSettings) {
*effectiveSettings = parsedSettings;
}
google::protobuf::Arena arena;
if (!parsedSettings.Arena) {
parsedSettings.Arena = &arena;
}
if (!parsedSettings.DeclaredNamedExprs.empty() && !parsedSettings.PgParser && parsedSettings.SyntaxVersion != 1) {
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"Externally declared named expressions not supported in V0 syntax"));
return result;
}
if (parsedSettings.PgParser && parsedSettings.PGDisable) {
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"PG syntax is disabled"));
return result;
}
if (parsedSettings.PgParser) {
return NSQLTranslationPG::PGToYql(query, parsedSettings, stmtParseInfo);
}
switch (parsedSettings.SyntaxVersion) {
case 0:
if (settings.V0ForceDisable || parsedSettings.V0Behavior == EV0Behavior::Disable) {
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"V0 syntax is disabled"));
return result;
}
if (parsedSettings.AnsiLexer) {
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"Ansi lexer is not supported in V0 syntax"));
return result;
}
return NSQLTranslationV0::SqlToYql(query, parsedSettings, warningRules);
case 1:
return NSQLTranslationV1::SqlToYql(query, parsedSettings, warningRules);
default:
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
TStringBuilder() << "Unknown SQL syntax version: " << parsedSettings.SyntaxVersion));
return result;
}
}
google::protobuf::Message* SqlAST(const TString& query, const TString& queryName, NYql::TIssues& issues,
size_t maxErrors, const TTranslationSettings& settings, ui16* actualSyntaxVersion)
{
TTranslationSettings parsedSettings(settings);
if (!ParseTranslationSettings(query, parsedSettings, issues)) {
return nullptr;
}
if (actualSyntaxVersion) {
*actualSyntaxVersion = parsedSettings.SyntaxVersion;
}
switch (parsedSettings.SyntaxVersion) {
case 0:
if (settings.V0ForceDisable || settings.V0Behavior == EV0Behavior::Disable) {
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"V0 syntax is disabled"));
return nullptr;
}
if (parsedSettings.AnsiLexer) {
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"Ansi lexer is not supported in V0 syntax"));
return nullptr;
}
return NSQLTranslationV0::SqlAST(query, queryName, issues, maxErrors, settings.Arena);
case 1:
return NSQLTranslationV1::SqlAST(query, queryName, issues, maxErrors, parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser, parsedSettings.TestAntlr4, settings.Arena);
default:
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
TStringBuilder() << "Unknown SQL syntax version: " << parsedSettings.SyntaxVersion));
return nullptr;
}
}
ILexer::TPtr SqlLexer(const TString& query, NYql::TIssues& issues, const TTranslationSettings& settings, ui16* actualSyntaxVersion)
{
TTranslationSettings parsedSettings(settings);
if (!ParseTranslationSettings(query, parsedSettings, issues)) {
return {};
}
if (actualSyntaxVersion) {
*actualSyntaxVersion = parsedSettings.SyntaxVersion;
}
switch (parsedSettings.SyntaxVersion) {
case 0:
if (settings.V0ForceDisable || settings.V0Behavior == EV0Behavior::Disable) {
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"V0 syntax is disabled"));
return {};
}
if (parsedSettings.AnsiLexer) {
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"Ansi lexer is not supported in V0 syntax"));
return {};
}
return NSQLTranslationV0::MakeLexer();
case 1:
return NSQLTranslationV1::MakeLexer(parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser);
default:
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
TStringBuilder() << "Unknown SQL syntax version: " << parsedSettings.SyntaxVersion));
return {};
}
}
NYql::TAstParseResult SqlASTToYql(const google::protobuf::Message& protoAst, const TSQLHints& hints, const TTranslationSettings& settings) {
NYql::TAstParseResult result;
switch (settings.SyntaxVersion) {
case 0:
if (settings.V0ForceDisable || settings.V0Behavior == EV0Behavior::Disable) {
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"V0 syntax is disabled"));
return result;
}
if (settings.AnsiLexer) {
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"Ansi lexer is not supported in V0 syntax"));
return result;
}
return NSQLTranslationV0::SqlASTToYql(protoAst, settings);
case 1:
return NSQLTranslationV1::SqlASTToYql(protoAst, hints, settings);
default:
result.Issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
TStringBuilder() << "Unknown SQL syntax version: " << settings.SyntaxVersion));
return result;
}
}
TVector<NYql::TAstParseResult> SqlToAstStatements(const TString& query, const TTranslationSettings& settings,
NYql::TWarningRules* warningRules, ui16* actualSyntaxVersion, TVector<NYql::TStmtParseInfo>* stmtParseInfo)
{
TVector<NYql::TAstParseResult> result;
NYql::TIssues issues;
TTranslationSettings parsedSettings(settings);
google::protobuf::Arena arena;
if (!parsedSettings.Arena) {
parsedSettings.Arena = &arena;
}
if (!ParseTranslationSettings(query, parsedSettings, issues)) {
return {};
}
if (actualSyntaxVersion) {
*actualSyntaxVersion = parsedSettings.SyntaxVersion;
}
if (!parsedSettings.DeclaredNamedExprs.empty() && !parsedSettings.PgParser && parsedSettings.SyntaxVersion != 1) {
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"Externally declared named expressions not supported in V0 syntax"));
return {};
}
if (parsedSettings.PgParser && parsedSettings.PGDisable) {
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"PG syntax is disabled"));
return result;
}
if (parsedSettings.PgParser) {
return NSQLTranslationPG::PGToYqlStatements(query, parsedSettings, stmtParseInfo);
}
switch (parsedSettings.SyntaxVersion) {
case 0:
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
"V0 syntax is disabled"));
return {};
case 1:
return NSQLTranslationV1::SqlToAstStatements(query, parsedSettings, warningRules, stmtParseInfo);
default:
issues.AddIssue(NYql::YqlIssue(NYql::TPosition(), NYql::TIssuesIds::DEFAULT_ERROR,
TStringBuilder() << "Unknown SQL syntax version: " << parsedSettings.SyntaxVersion));
return {};
}
}
} // namespace NSQLTranslation
|