translation_settings.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "translation_settings.h"
  2. #include <yql/essentials/core/issue/yql_issue.h>
  3. #include <yql/essentials/utils/utf8.h>
  4. #include <library/cpp/deprecated/split/split_iterator.h>
  5. #include <util/string/split.h>
  6. #include <util/system/env.h>
  7. namespace {
  8. bool InTestEnvironment() {
  9. return GetEnv("YQL_LOCAL_ENVIRONMENT") || GetEnv("YA_TEST_RUNNER");
  10. }
  11. using namespace NSQLTranslation;
  12. class TAlwaysDiallowPolicy : public ISqlFeaturePolicy {
  13. public:
  14. TAlwaysDiallowPolicy() = default;
  15. bool Allow() const override {
  16. return false;
  17. }
  18. };
  19. class TAlwaysAllowPolicy : public ISqlFeaturePolicy {
  20. public:
  21. TAlwaysAllowPolicy() = default;
  22. bool Allow() const override {
  23. return true;
  24. }
  25. };
  26. }
  27. namespace NSQLTranslation {
  28. ISqlFeaturePolicy::TPtr ISqlFeaturePolicy::MakeAlwaysDisallow() {
  29. return new TAlwaysDiallowPolicy;
  30. }
  31. ISqlFeaturePolicy::TPtr ISqlFeaturePolicy::MakeAlwaysAllow() {
  32. return new TAlwaysAllowPolicy;
  33. }
  34. ISqlFeaturePolicy::TPtr ISqlFeaturePolicy::Make(bool allow) {
  35. return allow ? MakeAlwaysAllow() : MakeAlwaysDisallow();
  36. }
  37. TTranslationSettings::TTranslationSettings()
  38. : ModuleMapping({{"core", "/lib/yql/core.yqls"}})
  39. , BindingsMode(EBindingsMode::ENABLED)
  40. , Mode(ESqlMode::QUERY)
  41. , MaxErrors(SQL_MAX_PARSER_ERRORS)
  42. , EndOfQueryCommit(true)
  43. , EnableGenericUdfs(true)
  44. , SyntaxVersion(1)
  45. , AnsiLexer(false)
  46. , Antlr4Parser(false)
  47. , PgParser(false)
  48. , InferSyntaxVersion(false)
  49. , V0Behavior(EV0Behavior::Disable)
  50. , V0ForceDisable(InTestEnvironment())
  51. , PGDisable(false)
  52. , WarnOnV0(true)
  53. , TestAntlr4(false)
  54. , V0WarnAsError(ISqlFeaturePolicy::MakeAlwaysDisallow())
  55. , DqDefaultAuto(ISqlFeaturePolicy::MakeAlwaysDisallow())
  56. , BlockDefaultAuto(ISqlFeaturePolicy::MakeAlwaysDisallow())
  57. , AssumeYdbOnClusterWithSlash(false)
  58. {}
  59. bool ParseTranslationSettings(const TString& query, TTranslationSettings& settings, NYql::TIssues& issues) {
  60. if (!NYql::IsUtf8(query)) {
  61. issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, 0), NYql::TIssuesIds::DEFAULT_ERROR, "Invalid UTF8 input"));
  62. return false;
  63. }
  64. if (settings.Flags.contains("Antlr4")) {
  65. settings.Antlr4Parser = true;
  66. }
  67. TSplitDelimiters lineDelimiters("\n\r");
  68. TDelimitersSplit linesSplit(query, lineDelimiters);
  69. auto lineIterator = linesSplit.Iterator();
  70. auto lineNumber = 0;
  71. while (!lineIterator.Eof()) {
  72. const TString& line = lineIterator.NextString();
  73. ++lineNumber;
  74. TVector<TStringBuf> parts;
  75. TSetDelimiter<const char> partsDelimiters(" \t");
  76. Split(line, partsDelimiters, parts);
  77. if (parts.empty()) {
  78. continue;
  79. }
  80. if (!parts[0].StartsWith("--!")) {
  81. break;
  82. }
  83. if (parts.size() > 1) {
  84. issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
  85. "Bad translation settings format"));
  86. return false;
  87. }
  88. auto value = TString(parts[0].SubString(3, TString::npos));
  89. if (value.empty()) {
  90. continue;
  91. }
  92. if ((value == "syntax_v0" || value == "syntax_v1") && !settings.InferSyntaxVersion) {
  93. issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
  94. "Explicit syntax version is not allowed"));
  95. return false;
  96. }
  97. if (value == "syntax_v0") {
  98. if (settings.V0ForceDisable || settings.V0Behavior == NSQLTranslation::EV0Behavior::Disable) {
  99. issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
  100. "V0 syntax is disabled"));
  101. return false;
  102. }
  103. settings.SyntaxVersion = 0;
  104. } else if (value == "syntax_v1") {
  105. settings.SyntaxVersion = 1;
  106. } else if (value == "ansi_lexer") {
  107. settings.AnsiLexer = true;
  108. } else if (value == "antlr4_parser") {
  109. settings.Antlr4Parser = true;
  110. } else if (value == "antlr3_parser") {
  111. settings.Antlr4Parser = false;
  112. } else if (value == "syntax_pg") {
  113. settings.PgParser = true;
  114. } else {
  115. issues.AddIssue(NYql::YqlIssue(NYql::TPosition(0, lineNumber), NYql::TIssuesIds::DEFAULT_ERROR,
  116. TStringBuilder() << "Unknown SQL translation setting: " << value));
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. } // namespace NSQLTranslation