sql.cpp 9.4 KB

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