yql_pure_provider_ut.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "yql_pure_provider.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <yql/essentials/core/facade/yql_facade.h>
  4. #include <yql/essentials/minikql/invoke_builtins/mkql_builtins.h>
  5. #include <yql/essentials/minikql/mkql_function_registry.h>
  6. #include <yql/essentials/public/result_format/yql_result_format_response.h>
  7. #include <library/cpp/yson/node/node_io.h>
  8. #include <util/system/user.h>
  9. #include <util/string/strip.h>
  10. namespace NYql {
  11. namespace {
  12. struct TSettings {
  13. bool SExpr = false;
  14. bool Pretty = false;
  15. };
  16. TString Run(const TString& query, TSettings settings = {}) {
  17. auto functionRegistry = NKikimr::NMiniKQL::CreateFunctionRegistry(NKikimr::NMiniKQL::CreateBuiltinRegistry());
  18. TVector<TDataProviderInitializer> dataProvidersInit;
  19. dataProvidersInit.push_back(GetPureDataProviderInitializer());
  20. TProgramFactory factory(true, functionRegistry.Get(), 0ULL, dataProvidersInit, "ut");
  21. TProgramPtr program = factory.Create("-stdin-", query);
  22. program->ConfigureYsonResultFormat(settings.Pretty ? NYson::EYsonFormat::Pretty : NYson::EYsonFormat::Text);
  23. bool parseRes;
  24. if (settings.SExpr) {
  25. parseRes = program->ParseYql();
  26. } else {
  27. parseRes = program->ParseSql();
  28. }
  29. if (!parseRes) {
  30. TStringStream err;
  31. program->PrintErrorsTo(err);
  32. UNIT_FAIL(err.Str());
  33. }
  34. if (!program->Compile(GetUsername())) {
  35. TStringStream err;
  36. program->PrintErrorsTo(err);
  37. UNIT_FAIL(err.Str());
  38. }
  39. TProgram::TStatus status = program->Run(GetUsername());
  40. if (status == TProgram::TStatus::Error) {
  41. TStringStream err;
  42. program->PrintErrorsTo(err);
  43. UNIT_FAIL(err.Str());
  44. }
  45. return program->ResultsAsString();
  46. }
  47. }
  48. Y_UNIT_TEST_SUITE(TPureProviderTests) {
  49. Y_UNIT_TEST(SExpr) {
  50. const auto s = R"(
  51. (
  52. (let result_sink (DataSink 'result))
  53. (let output (Int32 '1))
  54. (let world (Write! world result_sink (Key) output '('('type))))
  55. (return (Commit! world result_sink))
  56. )
  57. )";
  58. const auto expectedRes = R"(
  59. [
  60. {
  61. "Write" = [
  62. {
  63. "Type" = [
  64. "DataType";
  65. "Int32"
  66. ];
  67. "Data" = "1"
  68. }
  69. ]
  70. }
  71. ]
  72. )";
  73. auto res = Run(s, TSettings{.SExpr = true, .Pretty = true});
  74. UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
  75. }
  76. Y_UNIT_TEST(Sql0Rows) {
  77. const auto s = "select * from (select 1 as x) limit 0";
  78. const auto expectedRes = R"(
  79. [
  80. {
  81. "Write" = [
  82. {
  83. "Type" = [
  84. "ListType";
  85. [
  86. "StructType";
  87. [
  88. [
  89. "x";
  90. [
  91. "DataType";
  92. "Int32"
  93. ]
  94. ]
  95. ]
  96. ]
  97. ];
  98. "Data" = []
  99. }
  100. ]
  101. }
  102. ]
  103. )";
  104. auto res = Run(s, TSettings{.Pretty = true});
  105. UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
  106. }
  107. void Sql1RowImpl(const TString& query) {
  108. const auto expectedRes = R"(
  109. [
  110. {
  111. "Write" = [
  112. {
  113. "Type" = [
  114. "ListType";
  115. [
  116. "StructType";
  117. [
  118. [
  119. "x";
  120. [
  121. "DataType";
  122. "Int32"
  123. ]
  124. ]
  125. ]
  126. ]
  127. ];
  128. "Data" = [
  129. [
  130. "1"
  131. ]
  132. ]
  133. }
  134. ]
  135. }
  136. ]
  137. )";
  138. auto res = Run(query, TSettings{.Pretty = true});
  139. UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
  140. }
  141. Y_UNIT_TEST(Sql1Row_LLVM_On) {
  142. const auto s = "pragma config.flags(\"LLVM\",\"--dump-stats\");select 1 as x";
  143. Sql1RowImpl(s);
  144. }
  145. Y_UNIT_TEST(Sql1Row_LLVM_Off) {
  146. const auto s = "pragma config.flags(\"LLVM\",\"OFF\");select 1 as x";
  147. Sql1RowImpl(s);
  148. }
  149. Y_UNIT_TEST(Sql2Rows) {
  150. const auto s = "select 1 as x union all select 2 as x order by x";
  151. const auto expectedRes = R"(
  152. [
  153. {
  154. "Write" = [
  155. {
  156. "Type" = [
  157. "ListType";
  158. [
  159. "StructType";
  160. [
  161. [
  162. "x";
  163. [
  164. "DataType";
  165. "Int32"
  166. ]
  167. ]
  168. ]
  169. ]
  170. ];
  171. "Data" = [
  172. [
  173. "1"
  174. ];
  175. [
  176. "2"
  177. ]
  178. ]
  179. }
  180. ]
  181. }
  182. ]
  183. )";
  184. auto res = Run(s, TSettings{.Pretty = true});
  185. UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
  186. }
  187. Y_UNIT_TEST(TruncateRows) {
  188. const auto s = "select x from (select ListFromRange(1,2000) as x) flatten by x";
  189. auto res = Run(s);
  190. auto respList = NResult::ParseResponse(NYT::NodeFromYsonString(res));
  191. UNIT_ASSERT_VALUES_EQUAL(respList.size(), 1);
  192. UNIT_ASSERT_VALUES_EQUAL(respList[0].Writes.size(), 1);
  193. UNIT_ASSERT(respList[0].Writes[0].IsTruncated);
  194. }
  195. Y_UNIT_TEST(TruncateBytes) {
  196. const auto s = "select '" + TString(1000000, 'a') + "' as x, 1 as y union all select '' as x, 2 as y order by y";
  197. auto res = Run(s);
  198. auto respList = NResult::ParseResponse(NYT::NodeFromYsonString(res));
  199. UNIT_ASSERT_VALUES_EQUAL(respList.size(), 1);
  200. UNIT_ASSERT_VALUES_EQUAL(respList[0].Writes.size(), 1);
  201. UNIT_ASSERT(respList[0].Writes[0].IsTruncated);
  202. }
  203. Y_UNIT_TEST(ColumnOrder) {
  204. const auto s = "pragma OrderedColumns;select 1 as y, 2 as x";
  205. const auto expectedRes = R"(
  206. [
  207. {
  208. "Write" = [
  209. {
  210. "Type" = [
  211. "ListType";
  212. [
  213. "StructType";
  214. [
  215. [
  216. "y";
  217. [
  218. "DataType";
  219. "Int32"
  220. ]
  221. ];
  222. [
  223. "x";
  224. [
  225. "DataType";
  226. "Int32"
  227. ]
  228. ]
  229. ]
  230. ]
  231. ];
  232. "Data" = [
  233. [
  234. "1";
  235. "2"
  236. ]
  237. ]
  238. }
  239. ]
  240. }
  241. ]
  242. )";
  243. auto res = Run(s, TSettings{.Pretty = true});
  244. UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
  245. }
  246. }
  247. } // namespace NYql