@@ -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+
303341SIMPLE_UDF (TObfuscate, TOptional<char *>(TAutoMap<char *>)) {
304342 using namespace NSQLFormat ;
305343 try {
@@ -330,38 +368,23 @@ using TRuleFreqResult = TListType<TTuple<char*, char*, ui64>>;
330368SIMPLE_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+
382426SIMPLE_MODULE (TYqlLangModule,
383427 TObfuscate,
384- TRuleFreq
428+ TRuleFreq,
429+ TTestSyntax
385430);
386431
387432REGISTER_MODULES (TYqlLangModule);
0 commit comments