Skip to content

Commit 3149bc4

Browse files
committed
Intermediate changes
commit_hash:dadff7d2bad302fad87b0104b2ea3fdd0243aa27
1 parent 100be08 commit 3149bc4

File tree

4 files changed

+118
-22
lines changed

4 files changed

+118
-22
lines changed

yql/essentials/udfs/language/yql/test/canondata/result.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@
5353
{
5454
"uri": "file://test.test_ObfuscateTable_/results.txt"
5555
}
56+
],
57+
"test.test[TestSyntax]": [
58+
{
59+
"uri": "file://test.test_TestSyntax_/results.txt"
60+
}
5661
]
5762
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[
2+
{
3+
"Write" = [
4+
{
5+
"Type" = [
6+
"ListType";
7+
[
8+
"StructType";
9+
[
10+
[
11+
"column0";
12+
[
13+
"OptionalType";
14+
[
15+
"DataType";
16+
"String"
17+
]
18+
]
19+
];
20+
[
21+
"column1";
22+
[
23+
"DataType";
24+
"Int32"
25+
]
26+
]
27+
]
28+
]
29+
];
30+
"Data" = [
31+
[
32+
#;
33+
"0"
34+
]
35+
]
36+
}
37+
]
38+
}
39+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$t1 = YqlLang::TestSyntax("SELECT 1;");
2+
$t2 = YqlLang::TestSyntax("SELECT");
3+
4+
SELECT
5+
$t1,
6+
Ensure(0, Find($t2 ?? "", "mismatched input '<EOF>'") IS NOT NULL, $t2 ?? "");
7+
;

yql/essentials/udfs/language/yql/yql_language_udf.cpp

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,44 @@ class TRuleFreqVisitor {
300300
THashSet<TString> KeywordNames_;
301301
};
302302

303+
bool GetParseTree(
304+
const TString& query,
305+
NSQLTranslation::TTranslationSettings& settings,
306+
NYql::TIssues& issues,
307+
NSQLTranslationV1::TLexers& lexers,
308+
NSQLTranslationV1::TParsers& parsers,
309+
google::protobuf::Message*& message)
310+
{
311+
if (!ParseTranslationSettings(query, settings, issues)) {
312+
return false;
313+
}
314+
315+
lexers.Antlr4 = NSQLTranslationV1::MakeAntlr4LexerFactory();
316+
lexers.Antlr4Ansi = NSQLTranslationV1::MakeAntlr4AnsiLexerFactory();
317+
auto lexer = NSQLTranslationV1::MakeLexer(lexers, settings.AnsiLexer, true);
318+
auto onNextToken = [&](NSQLTranslation::TParsedToken&& token) {
319+
Y_UNUSED(token);
320+
};
321+
322+
if (!lexer->Tokenize(query, "", onNextToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
323+
return false;
324+
}
325+
326+
parsers.Antlr4 = NSQLTranslationV1::MakeAntlr4ParserFactory();
327+
parsers.Antlr4Ansi = NSQLTranslationV1::MakeAntlr4AnsiParserFactory();
328+
message = NSQLTranslationV1::SqlAST(
329+
parsers,
330+
query,
331+
/* queryName = */ "",
332+
issues,
333+
NSQLTranslation::SQL_MAX_PARSER_ERRORS,
334+
settings.AnsiLexer,
335+
/* antlr4Parser = */ true,
336+
settings.Arena);
337+
338+
return static_cast<bool>(message);
339+
}
340+
303341
SIMPLE_UDF(TObfuscate, TOptional<char*>(TAutoMap<char*>)) {
304342
using namespace NSQLFormat;
305343
try {
@@ -330,38 +368,23 @@ using TRuleFreqResult = TListType<TTuple<char*, char*, ui64>>;
330368
SIMPLE_UDF(TRuleFreq, TOptional<TRuleFreqResult>(TAutoMap<char*>)) {
331369
try {
332370
const TString query(args[0].AsStringRef());
333-
NYql::TIssues issues;
371+
334372
google::protobuf::Arena arena;
335373
NSQLTranslation::TTranslationSettings settings;
336374
settings.Arena = &arena;
337-
if (!ParseTranslationSettings(query, settings, issues)) {
338-
return {};
339-
}
340375

376+
NYql::TIssues issues;
341377
NSQLTranslationV1::TLexers lexers;
342-
lexers.Antlr4 = NSQLTranslationV1::MakeAntlr4LexerFactory();
343-
lexers.Antlr4Ansi = NSQLTranslationV1::MakeAntlr4AnsiLexerFactory();
344-
auto lexer = NSQLTranslationV1::MakeLexer(lexers, settings.AnsiLexer, true);
345-
auto onNextToken = [&](NSQLTranslation::TParsedToken&& token) {
346-
Y_UNUSED(token);
347-
};
348-
349-
if (!lexer->Tokenize(query, "", onNextToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
350-
return {};
351-
}
352-
353378
NSQLTranslationV1::TParsers parsers;
354-
parsers.Antlr4 = NSQLTranslationV1::MakeAntlr4ParserFactory();
355-
parsers.Antlr4Ansi = NSQLTranslationV1::MakeAntlr4AnsiParserFactory();
356-
auto msg = NSQLTranslationV1::SqlAST(parsers, query, "", issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS,
357-
settings.AnsiLexer, true, &arena);
358-
if (!msg) {
379+
380+
google::protobuf::Message* tree;
381+
if (!GetParseTree(query, settings, issues, lexers, parsers, tree)) {
359382
return {};
360383
}
361384

362385
TContext ctx(lexers, parsers, settings, {}, issues, query);
363386
TRuleFreqVisitor visitor(ctx);
364-
visitor.Visit(*msg);
387+
visitor.Visit(*tree);
365388

366389
auto listBuilder = valueBuilder->NewListBuilder();
367390
for (const auto& [key, f] : visitor.GetFreqs()) {
@@ -379,9 +402,31 @@ SIMPLE_UDF(TRuleFreq, TOptional<TRuleFreqResult>(TAutoMap<char*>)) {
379402
}
380403
}
381404

405+
SIMPLE_UDF(TTestSyntax, TOptional<char*>(TAutoMap<char*>)) try {
406+
const TString query(args[0].AsStringRef());
407+
408+
google::protobuf::Arena arena;
409+
NSQLTranslation::TTranslationSettings settings;
410+
settings.Arena = &arena;
411+
412+
NYql::TIssues issues;
413+
NSQLTranslationV1::TLexers lexers;
414+
NSQLTranslationV1::TParsers parsers;
415+
416+
google::protobuf::Message* tree;
417+
if (!GetParseTree(query, settings, issues, lexers, parsers, tree)) {
418+
return valueBuilder->NewString(issues.ToString());
419+
}
420+
421+
return {};
422+
} catch (const yexception& e) {
423+
return valueBuilder->NewString(TString(e.what()));
424+
}
425+
382426
SIMPLE_MODULE(TYqlLangModule,
383427
TObfuscate,
384-
TRuleFreq
428+
TRuleFreq,
429+
TTestSyntax
385430
);
386431

387432
REGISTER_MODULES(TYqlLangModule);

0 commit comments

Comments
 (0)