sql_ut.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <yql/essentials/providers/common/provider/yql_provider_names.h>
  2. #include <yql/essentials/sql/sql.h>
  3. #include <util/generic/map.h>
  4. #include <library/cpp/regex/pcre/pcre.h>
  5. #include <library/cpp/testing/unittest/registar.h>
  6. #include <util/string/split.h>
  7. #include <deque>
  8. #include <unordered_set>
  9. using namespace NSQLTranslation;
  10. enum class EDebugOutput {
  11. None,
  12. ToCerr,
  13. };
  14. const ui32 PRETTY_FLAGS = NYql::TAstPrintFlags::PerLine | NYql::TAstPrintFlags::ShortQuote |
  15. NYql::TAstPrintFlags::AdaptArbitraryContent;
  16. inline TString Err2Str(NYql::TAstParseResult& res, EDebugOutput debug = EDebugOutput::None) {
  17. TStringStream s;
  18. res.Issues.PrintTo(s);
  19. if (debug == EDebugOutput::ToCerr) {
  20. Cerr << s.Str() << Endl;
  21. }
  22. return s.Str();
  23. }
  24. inline NYql::TAstParseResult SqlToYqlWithMode(const TString& query, NSQLTranslation::ESqlMode mode = NSQLTranslation::ESqlMode::QUERY, size_t maxErrors = 10, const TString& provider = {},
  25. EDebugOutput debug = EDebugOutput::None, bool ansiLexer = false, NSQLTranslation::TTranslationSettings settings = {})
  26. {
  27. google::protobuf::Arena arena;
  28. const auto service = provider ? provider : TString(NYql::YtProviderName);
  29. const TString cluster = "plato";
  30. settings.ClusterMapping[cluster] = service;
  31. settings.ClusterMapping["hahn"] = NYql::YtProviderName;
  32. settings.ClusterMapping["mon"] = NYql::SolomonProviderName;
  33. settings.MaxErrors = maxErrors;
  34. settings.Mode = mode;
  35. settings.Arena = &arena;
  36. settings.AnsiLexer = ansiLexer;
  37. settings.Antlr4Parser = false;
  38. settings.SyntaxVersion = 1;
  39. auto res = SqlToYql(query, settings);
  40. if (debug == EDebugOutput::ToCerr) {
  41. Err2Str(res, debug);
  42. }
  43. return res;
  44. }
  45. inline NYql::TAstParseResult SqlToYql(const TString& query, size_t maxErrors = 10, const TString& provider = {}, EDebugOutput debug = EDebugOutput::None) {
  46. return SqlToYqlWithMode(query, NSQLTranslation::ESqlMode::QUERY, maxErrors, provider, debug);
  47. }
  48. inline NYql::TAstParseResult
  49. SqlToYqlWithSettings(const TString& query, const NSQLTranslation::TTranslationSettings& settings) {
  50. return SqlToYqlWithMode(query, NSQLTranslation::ESqlMode::QUERY, 10, {}, EDebugOutput::None, false, settings);
  51. }
  52. inline void ExpectFailWithError(const TString& query, const TString& error) {
  53. NYql::TAstParseResult res = SqlToYql(query);
  54. UNIT_ASSERT(!res.Root);
  55. UNIT_ASSERT_NO_DIFF(Err2Str(res), error);
  56. }
  57. inline void ExpectFailWithFuzzyError(const TString& query, const TString& errorRegex) {
  58. NYql::TAstParseResult res = SqlToYql(query);
  59. UNIT_ASSERT(!res.Root);
  60. UNIT_ASSERT(NPcre::TPcre<char>(errorRegex.c_str()).Matches(Err2Str(res)));
  61. }
  62. inline NYql::TAstParseResult SqlToYqlWithAnsiLexer(const TString& query, size_t maxErrors = 10, const TString& provider = {}, EDebugOutput debug = EDebugOutput::None) {
  63. bool ansiLexer = true;
  64. return SqlToYqlWithMode(query, NSQLTranslation::ESqlMode::QUERY, maxErrors, provider, debug, ansiLexer);
  65. }
  66. inline void ExpectFailWithErrorForAnsiLexer(const TString& query, const TString& error) {
  67. NYql::TAstParseResult res = SqlToYqlWithAnsiLexer(query);
  68. UNIT_ASSERT(!res.Root);
  69. UNIT_ASSERT_NO_DIFF(Err2Str(res), error);
  70. }
  71. inline TString GetPrettyPrint(const NYql::TAstParseResult& res) {
  72. TStringStream yqlProgram;
  73. res.Root->PrettyPrintTo(yqlProgram, NYql::TAstPrintFlags::PerLine | NYql::TAstPrintFlags::ShortQuote);
  74. return yqlProgram.Str();
  75. }
  76. inline TString Quote(const char* str) {
  77. return TStringBuilder() << "'\"" << str << "\"";
  78. }
  79. class TWordCountHive: public TMap<TString, unsigned> {
  80. public:
  81. TWordCountHive(std::initializer_list<TString> strings) {
  82. for (auto& str: strings) {
  83. emplace(str, 0);
  84. }
  85. }
  86. TWordCountHive(std::initializer_list<std::pair<const TString, unsigned>> list)
  87. : TMap(list)
  88. {
  89. }
  90. };
  91. typedef std::function<void (const TString& word, const TString& line)> TVerifyLineFunc;
  92. inline TString VerifyProgram(const NYql::TAstParseResult& res, TWordCountHive& wordCounter, TVerifyLineFunc verifyLine = TVerifyLineFunc()) {
  93. const auto programm = GetPrettyPrint(res);
  94. TVector<TString> yqlProgram;
  95. Split(programm, "\n", yqlProgram);
  96. for (const auto& line: yqlProgram) {
  97. for (auto& counterIter: wordCounter) {
  98. const auto& word = counterIter.first;
  99. auto pos = line.find(word);
  100. while (pos != TString::npos) {
  101. ++counterIter.second;
  102. if (verifyLine) {
  103. verifyLine(word, line);
  104. }
  105. pos = line.find(word, pos + word.length());
  106. }
  107. }
  108. }
  109. return programm;
  110. }
  111. inline void VerifySqlInHints(const TString& query, const THashSet<TString>& expectedHints, TMaybe<bool> ansi) {
  112. TString pragma;
  113. if (ansi.Defined()) {
  114. pragma = *ansi ? "PRAGMA AnsiInForEmptyOrNullableItemsCollections;" :
  115. "PRAGMA DisableAnsiInForEmptyOrNullableItemsCollections;";
  116. }
  117. NYql::TAstParseResult res = SqlToYql(pragma + query);
  118. UNIT_ASSERT(res.Root);
  119. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  120. Y_UNUSED(word);
  121. if (!ansi.Defined()) {
  122. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('warnNoAnsi)"));
  123. } else if (*ansi) {
  124. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('ansi)"));
  125. }
  126. for (auto& hint : expectedHints) {
  127. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(hint));
  128. }
  129. };
  130. TWordCountHive elementStat = {{TString("SqlIn"), 0}};
  131. VerifyProgram(res, elementStat, verifyLine);
  132. }
  133. inline void VerifySqlInHints(const TString& query, const THashSet<TString>& expectedHints) {
  134. VerifySqlInHints(query, expectedHints, false);
  135. VerifySqlInHints(query, expectedHints, true);
  136. }
  137. inline NSQLTranslation::TTranslationSettings GetSettingsWithS3Binding(const TString& name) {
  138. NSQLTranslation::TTranslationSettings settings;
  139. NSQLTranslation::TTableBindingSettings bindSettings;
  140. bindSettings.ClusterType = "s3";
  141. bindSettings.Settings["cluster"] = "cluster";
  142. bindSettings.Settings["path"] = "path";
  143. bindSettings.Settings["format"] = "format";
  144. bindSettings.Settings["compression"] = "ccompression";
  145. bindSettings.Settings["bar"] = "1";
  146. // schema is not validated in this test but should be valid YSON text
  147. bindSettings.Settings["schema"] = R"__("[
  148. "StructType";
  149. [
  150. [
  151. "key";
  152. [
  153. "DataType";
  154. "String"
  155. ]
  156. ];
  157. [
  158. "subkey";
  159. [
  160. "DataType";
  161. "String"
  162. ]
  163. ];
  164. [
  165. "value";
  166. [
  167. "DataType";
  168. "String"
  169. ]
  170. ]
  171. ]])__";
  172. bindSettings.Settings["partitioned_by"] = "[\"key\", \"subkey\"]";
  173. settings.Bindings[name] = bindSettings;
  174. return settings;
  175. }
  176. inline void AstBfs(NYql::TAstNode const* root, std::function<bool (NYql::TAstNode const*)> visitor) {
  177. std::deque<NYql::TAstNode const*> wishList{ root };
  178. std::unordered_set<NYql::TAstNode const*> visited;
  179. while(!wishList.empty()){
  180. auto v = wishList.front();
  181. wishList.pop_front();
  182. if (!visitor(v))
  183. return;
  184. visited.insert(v);
  185. if (v->IsList()) {
  186. for (ui32 i = 0; i != v->GetChildrenCount(); ++i) {
  187. auto child = v->GetChild(i);
  188. if (visited.find(child) == visited.cend()) {
  189. wishList.push_back(child);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. inline const NYql::TAstNode* FindNodeByChildAtomContent(const NYql::TAstNode* root, uint32_t childIndex, TStringBuf name){
  196. const NYql::TAstNode* result = nullptr;
  197. AstBfs(root, [&result, childIndex, name](auto v) {
  198. if (v->IsList() && v->GetChildrenCount() > childIndex &&
  199. v->GetChild(childIndex)->IsAtom() && v->GetChild(childIndex)->GetContent() == name) {
  200. result = v;
  201. return false;
  202. }
  203. return true; });
  204. return result;
  205. }