codegen_ut.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "../pg_compat.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <library/cpp/resource/resource.h>
  4. #include <yql/essentials/minikql/codegen/codegen.h>
  5. #include <yql/essentials/minikql/arrow/arrow_defs.h>
  6. #include <arrow/compute/kernel.h>
  7. #include <arrow/array/builder_primitive.h>
  8. #include <arrow/array/builder_binary.h>
  9. #include <llvm/IR/Module.h>
  10. #include <yql/essentials/parser/pg_wrapper/arrow.h>
  11. extern "C" {
  12. #include <yql/essentials/parser/pg_wrapper/postgresql/src/backend/catalog/pg_collation_d.h>
  13. #include <yql/essentials/parser/pg_wrapper/postgresql/src/backend/utils/fmgrprotos.h>
  14. }
  15. #include <yql/essentials/parser/pg_catalog/catalog.h>
  16. #include <yql/essentials/minikql/arrow/arrow_util.h>
  17. #include <util/datetime/cputimer.h>
  18. using namespace NYql;
  19. using namespace NYql::NCodegen;
  20. extern "C" {
  21. Y_PRAGMA_DIAGNOSTIC_PUSH
  22. Y_PRAGMA("GCC diagnostic ignored \"-Wreturn-type-c-linkage\"")
  23. #include <yql/essentials/parser/pg_wrapper/pg_kernels_fwd.inc>
  24. Y_PRAGMA_DIAGNOSTIC_POP
  25. }
  26. enum class EKernelFlavor {
  27. Indirect,
  28. DefArg,
  29. Cpp,
  30. BitCode,
  31. Ideal
  32. };
  33. Y_UNIT_TEST_SUITE(TPgCodegen) {
  34. void PgFuncImpl(EKernelFlavor flavor, bool constArg, bool fixed) {
  35. const TString& name = fixed ? "date_eq" : "textout";
  36. ICodegen::TPtr codegen;
  37. TExecFunc execFunc;
  38. switch (flavor) {
  39. case EKernelFlavor::Indirect: {
  40. if (fixed) {
  41. execFunc = MakeIndirectExec<true, true>(&date_eq);
  42. } else {
  43. execFunc = MakeIndirectExec<true, false>(&textout);
  44. }
  45. break;
  46. }
  47. case EKernelFlavor::DefArg: {
  48. if (fixed) {
  49. execFunc = TGenericExec<TPgDirectFunc<&date_eq>, true, true, TDefaultArgsPolicy>({});
  50. } else {
  51. execFunc = TGenericExec<TPgDirectFunc<&textout>, true, false, TDefaultArgsPolicy>({});
  52. }
  53. break;
  54. }
  55. case EKernelFlavor::Cpp: {
  56. execFunc = fixed ? arrow_date_eq() : arrow_textout();
  57. break;
  58. }
  59. case EKernelFlavor::BitCode: {
  60. codegen = ICodegen::Make(ETarget::Native);
  61. auto bitcode = NResource::Find(fixed ? "/llvm_bc/PgFuncs1" : "/llvm_bc/PgFuncs17");
  62. codegen->LoadBitCode(bitcode, "Funcs");
  63. auto func = codegen->GetModule().getFunction(std::string("arrow_" + name));
  64. Y_ENSURE(func);
  65. codegen->AddGlobalMapping("GetPGKernelState", (const void*)&GetPGKernelState);
  66. codegen->Verify();
  67. codegen->ExportSymbol(func);
  68. codegen->Compile();
  69. //codegen->ShowGeneratedFunctions(&Cerr);
  70. typedef TExecFunc (*TFunc)();
  71. auto funcPtr = (TFunc)codegen->GetPointerToFunction(func);
  72. execFunc = funcPtr();
  73. break;
  74. }
  75. case EKernelFlavor::Ideal: {
  76. if (fixed) {
  77. execFunc = [](arrow::compute::KernelContext* ctx, const arrow::compute::ExecBatch& batch, arrow::Datum* res) {
  78. size_t length = batch.values[0].length();
  79. //NUdf::TFixedSizeArrayBuilder<ui64, true> builder(NKikimr::NMiniKQL::TTypeInfoHelper(), arrow::uint64(), *arrow::default_memory_pool(), length);
  80. NUdf::TTypedBufferBuilder<ui64> dataBuilder(arrow::default_memory_pool());
  81. NUdf::TTypedBufferBuilder<ui8> nullBuilder(arrow::default_memory_pool());
  82. dataBuilder.Reserve(length);
  83. nullBuilder.Reserve(length);
  84. auto out = dataBuilder.MutableData();
  85. auto outNulls = nullBuilder.MutableData();
  86. NUdf::TFixedSizeBlockReader<ui64, false> reader1;
  87. NUdf::TFixedSizeBlockReader<ui64, false> reader2;
  88. const auto& array1 = *batch.values[0].array();
  89. const auto ptr1 = array1.GetValues<ui64>(1);
  90. if (batch.values[1].is_array()) {
  91. const auto& array2 = *batch.values[1].array();
  92. const auto ptr2 = array2.GetValues<ui64>(1);
  93. for (size_t i = 0; i < length; ++i) {
  94. //auto x = reader1.GetItem(array1, i).As<ui64>();
  95. //auto y = reader2.GetItem(array2, i).As<ui64>();
  96. auto x = ptr1[i];
  97. auto y = ptr2[i];
  98. out[i] = x == y ? 1 : 0;
  99. outNulls[i] = false;
  100. }
  101. } else {
  102. ui64 yConst = reader2.GetScalarItem(*batch.values[1].scalar()).As<ui64>();
  103. for (size_t i = 0; i < length; ++i) {
  104. auto x = ptr1[i];
  105. out[i] = x == yConst ? 1 : 0;
  106. outNulls[i] = false;
  107. }
  108. }
  109. std::shared_ptr<arrow::Buffer> nulls;
  110. nulls = nullBuilder.Finish();
  111. nulls = NUdf::MakeDenseBitmap(nulls->data(), length, arrow::default_memory_pool());
  112. std::shared_ptr<arrow::Buffer> data = dataBuilder.Finish();
  113. *res = arrow::ArrayData::Make(arrow::uint64(), length ,{ data, nulls});
  114. return arrow::Status::OK();
  115. };
  116. } else {
  117. execFunc = [](arrow::compute::KernelContext* ctx, const arrow::compute::ExecBatch& batch, arrow::Datum* res) {
  118. size_t length = batch.values[0].length();
  119. NUdf::TStringArrayBuilder<arrow::BinaryType, true, NUdf::EPgStringType::None> builder(NKikimr::NMiniKQL::TTypeInfoHelper(), arrow::binary(), *ctx->memory_pool(), length);
  120. NUdf::TStringBlockReader<arrow::BinaryType, true> reader;
  121. const auto& array = *batch.values[0].array();
  122. for (size_t i = 0; i < length; ++i) {
  123. auto item = reader.GetItem(array, i);
  124. if (!item) {
  125. builder.Add(NUdf::TBlockItem{});
  126. } else {
  127. auto s = item.AsStringRef();
  128. size_t len = s.Size() - VARHDRSZ - sizeof(void*);
  129. const char* ptr = s.Data() + VARHDRSZ + sizeof(void*);
  130. builder.Add(NUdf::TBlockItem{NUdf::TStringRef(ptr, len)});
  131. }
  132. }
  133. *res = builder.Build(true);
  134. return arrow::Status::OK();
  135. };
  136. };
  137. break;
  138. }
  139. }
  140. Y_ENSURE(execFunc);
  141. arrow::compute::ExecContext execContent;
  142. arrow::compute::KernelContext kernelCtx(&execContent);
  143. TPgKernelState state;
  144. kernelCtx.SetState(&state);
  145. FmgrInfo finfo;
  146. Zero(state.flinfo);
  147. state.ProcDesc = fixed ? &NPg::LookupProc("date_eq", { 0, 0 }) : &NPg::LookupProc("textout", { 0 });
  148. fmgr_info(state.ProcDesc->ProcId, &state.flinfo);
  149. state.context = nullptr;
  150. state.resultinfo = nullptr;
  151. state.fncollation = DEFAULT_COLLATION_OID;
  152. state.Name = name;
  153. if (fixed) {
  154. state.TypeLen = 1;
  155. state.IsFixedResult = true;
  156. state.IsFixedArg.push_back(true);
  157. state.IsFixedArg.push_back(true);
  158. } else {
  159. state.TypeLen = -2;
  160. state.IsFixedResult = false;
  161. state.IsFixedArg.push_back(false);
  162. }
  163. #ifdef NDEBUG
  164. const size_t N = 10000;
  165. #else
  166. const size_t N = 1000;
  167. #endif
  168. std::vector<arrow::Datum> batchArgs;
  169. if (fixed) {
  170. arrow::UInt64Builder builder;
  171. ARROW_OK(builder.Reserve(N));
  172. for (size_t i = 0; i < N; ++i) {
  173. builder.UnsafeAppend(i);
  174. }
  175. std::shared_ptr<arrow::ArrayData> out;
  176. ARROW_OK(builder.FinishInternal(&out));
  177. arrow::Datum arg1(out), arg2;
  178. if (constArg) {
  179. Cout << "with const arg\n";
  180. arg2 = NKikimr::NMiniKQL::MakeScalarDatum<ui64>(0);
  181. } else {
  182. arg2 = out;
  183. }
  184. batchArgs.push_back(arg1);
  185. batchArgs.push_back(arg2);
  186. } else {
  187. arrow::BinaryBuilder builder;
  188. ARROW_OK(builder.Reserve(N));
  189. for (size_t i = 0; i < N; ++i) {
  190. std::string s(sizeof(void*) + VARHDRSZ + 500, 'A' + i % 26);
  191. NUdf::ZeroMemoryContext(s.data() + sizeof(void*));
  192. auto t = (text*)(s.data() + sizeof(void*));
  193. SET_VARSIZE(t, VARHDRSZ + 500);
  194. ARROW_OK(builder.Append(s));
  195. }
  196. std::shared_ptr<arrow::ArrayData> out;
  197. ARROW_OK(builder.FinishInternal(&out));
  198. arrow::Datum arg1(out);
  199. batchArgs.push_back(arg1);
  200. }
  201. arrow::compute::ExecBatch batch(std::move(batchArgs), N);
  202. {
  203. Cout << "begin...\n";
  204. TSimpleTimer timer;
  205. for (size_t count = 0; count < (fixed ? 10000 : 1000); ++count) {
  206. arrow::Datum res;
  207. ARROW_OK(execFunc(&kernelCtx, batch, &res));
  208. Y_ENSURE(res.length() == N);
  209. }
  210. Cout << "done, elapsed: " << timer.Get() << "\n";
  211. }
  212. }
  213. Y_UNIT_TEST(PgFixedFuncIdeal) {
  214. PgFuncImpl(EKernelFlavor::Ideal, false, true);
  215. PgFuncImpl(EKernelFlavor::Ideal, true, true);
  216. }
  217. Y_UNIT_TEST(PgFixedFuncDefArg) {
  218. PgFuncImpl(EKernelFlavor::DefArg, false, true);
  219. PgFuncImpl(EKernelFlavor::DefArg, true, true);
  220. }
  221. Y_UNIT_TEST(PgFixedFuncIndirect) {
  222. PgFuncImpl(EKernelFlavor::Indirect, false, true);
  223. PgFuncImpl(EKernelFlavor::Indirect, true, true);
  224. }
  225. #if !defined(USE_SLOW_PG_KERNELS)
  226. Y_UNIT_TEST(PgFixedFuncCpp) {
  227. PgFuncImpl(EKernelFlavor::Cpp, false, true);
  228. PgFuncImpl(EKernelFlavor::Cpp, true, true);
  229. }
  230. #if defined(YQL_USE_PG_BC)
  231. Y_UNIT_TEST(PgFixedFuncBC) {
  232. PgFuncImpl(EKernelFlavor::BitCode, false, true);
  233. PgFuncImpl(EKernelFlavor::BitCode, true, true);
  234. }
  235. #endif
  236. #endif
  237. Y_UNIT_TEST(PgStrFuncIdeal) {
  238. PgFuncImpl(EKernelFlavor::Ideal, false, false);
  239. }
  240. Y_UNIT_TEST(PgStrFuncDefArg) {
  241. PgFuncImpl(EKernelFlavor::DefArg, false, false);
  242. }
  243. Y_UNIT_TEST(PgStrFuncIndirect) {
  244. PgFuncImpl(EKernelFlavor::Indirect, false, false);
  245. }
  246. #if !defined(USE_SLOW_PG_KERNELS)
  247. Y_UNIT_TEST(PgStrFuncCpp) {
  248. PgFuncImpl(EKernelFlavor::Cpp, false, false);
  249. }
  250. #if defined(YQL_USE_PG_BC)
  251. Y_UNIT_TEST(PgStrFuncBC) {
  252. PgFuncImpl(EKernelFlavor::BitCode, false, false);
  253. }
  254. #endif
  255. #endif
  256. }