mkql_grace_join_ut.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. #include "mkql_computation_node_ut.h"
  2. #include <yql/essentials/minikql/mkql_runtime_version.h>
  3. #include <yql/essentials/minikql/comp_nodes/mkql_grace_join_imp.h>
  4. #include <yql/essentials/minikql/computation/mock_spiller_factory_ut.h>
  5. #include <chrono>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <vector>
  9. #include <cassert>
  10. #include <cstdlib>
  11. #include <stdlib.h>
  12. #include <random>
  13. #include <util/system/compiler.h>
  14. #include <util/stream/null.h>
  15. #include <util/system/mem_info.h>
  16. #include <cstdint>
  17. namespace NKikimr {
  18. namespace NMiniKQL {
  19. constexpr bool IsVerbose = false;
  20. #define CTEST (IsVerbose ? Cerr : Cnull)
  21. Y_UNIT_TEST_SUITE(TMiniKQLGraceJoinMemTest) {
  22. Y_UNIT_TEST(TestMem1) {
  23. const ui64 TupleSize = 1024;
  24. const ui64 NBuckets = 128;
  25. const ui64 NTuples = 100000;
  26. const ui64 BucketSize = (2* NTuples * (TupleSize + 1) ) / NBuckets;
  27. ui64 *bigTuple = (ui64 * ) malloc(TupleSize * sizeof(ui64));
  28. ui64 *buckets[NBuckets];
  29. ui64 tuplesPos[NBuckets];
  30. std::mt19937_64 rng;
  31. std::uniform_int_distribution<ui64> dist(0, 10000 - 1);
  32. for (ui64 i = 0; i < TupleSize; i++)
  33. {
  34. bigTuple[i] = dist(rng);
  35. }
  36. ui64 bucket = 0;
  37. ui64 milliseconds = 0;
  38. const ui64 BitsForData = 30;
  39. char* a = (char * )malloc(1 << BitsForData);
  40. char* b = (char *) malloc(1 << BitsForData);
  41. UNIT_ASSERT(a);
  42. UNIT_ASSERT(b);
  43. memset(a, 1, 1 << BitsForData);
  44. memset(b, 2, 1 << BitsForData);
  45. std::chrono::steady_clock::time_point begin01 = std::chrono::steady_clock::now();
  46. memcpy(b, a, 1 << BitsForData);
  47. std::chrono::steady_clock::time_point end01 = std::chrono::steady_clock::now();
  48. UNIT_ASSERT(*a == 1);
  49. UNIT_ASSERT(*b == 1);
  50. Y_DO_NOT_OPTIMIZE_AWAY(a);
  51. Y_DO_NOT_OPTIMIZE_AWAY(b);
  52. ui64 microseconds = std::chrono::duration_cast<std::chrono::microseconds>(end01 - begin01).count();
  53. CTEST << "Time for memcpy = " << microseconds << "[microseconds]" << Endl;
  54. CTEST << "Data size = " << (1<<BitsForData) / (1024 * 1024) << "[MB]" << Endl;
  55. CTEST << "Memcpy speed = " << ( (1<<BitsForData) ) / (microseconds) << "MB/sec" << Endl;
  56. CTEST << Endl;
  57. std::vector<std::vector<ui64>> vec_buckets;
  58. vec_buckets.resize(NBuckets);
  59. for (ui64 i = 0; i < NBuckets; i++)
  60. {
  61. vec_buckets[i].resize(2 * TupleSize * NTuples / (NBuckets - 1), 0);
  62. vec_buckets[i].clear();
  63. // vec_buckets[i].reserve( 2 * TupleSize * NTuples / (NBuckets - 1));
  64. }
  65. for (ui64 i = 0; i < NBuckets; i++) {
  66. buckets[i] = (ui64 * ) malloc( (BucketSize * sizeof(ui64) * 32) / 32);
  67. memset( buckets[i], 1, (BucketSize * sizeof(ui64) * 32) / 32);
  68. tuplesPos[i] = 0;
  69. }
  70. std::chrono::steady_clock::time_point begin02 = std::chrono::steady_clock::now();
  71. std::uniform_int_distribution<ui64> bucketDist(0, NBuckets - 1);
  72. for (ui64 i = 0; i < NTuples; i++)
  73. {
  74. bucket = i % NBuckets;
  75. // bucket = bucketDist(rng);
  76. std::vector<ui64> &curr_vec = vec_buckets[bucket];
  77. curr_vec.insert(curr_vec.end(), bigTuple, bigTuple + TupleSize);
  78. }
  79. std::chrono::steady_clock::time_point end02 = std::chrono::steady_clock::now();
  80. milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end02 - begin02).count();
  81. CTEST << "Time for std::insert = " << milliseconds << "[ms]" << Endl;
  82. CTEST << "Total MB = " << (TupleSize * NTuples * sizeof(ui64) / (1024 * 1024)) << Endl;
  83. CTEST << "std::insert speed = " << (TupleSize * NTuples * sizeof(ui64) * 1000) / (milliseconds * 1024 * 1024) << "MB/sec" << Endl;
  84. CTEST << Endl;
  85. std::chrono::steady_clock::time_point begin03 = std::chrono::steady_clock::now();
  86. for (ui64 i = 0; i < NTuples; i++)
  87. {
  88. bucket = i % NBuckets;
  89. // bucket = bucketDist(rng);
  90. ui64 * dst = buckets[bucket] + tuplesPos[bucket];
  91. std::memcpy(dst, bigTuple, TupleSize*sizeof(ui64));
  92. tuplesPos[bucket] += TupleSize;
  93. }
  94. std::chrono::steady_clock::time_point end03 = std::chrono::steady_clock::now();
  95. milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  96. CTEST << "Time for std::memcpy = " << milliseconds << "[ms]" << Endl;
  97. CTEST << "Total MB = " << (TupleSize * NTuples * sizeof(ui64) / (1024 * 1024)) << Endl;
  98. CTEST << "std:memcpy speed = " << (TupleSize * NTuples * sizeof(ui64) * 1000) / (milliseconds * 1024 * 1024) << "MB/sec" << Endl;
  99. CTEST << Endl;
  100. for (ui64 i = 0; i < NBuckets; i++) {
  101. tuplesPos[i] = 0;
  102. }
  103. std::chrono::steady_clock::time_point begin04 = std::chrono::steady_clock::now();
  104. for (ui64 i = 0; i < NTuples; i++)
  105. {
  106. bucket = bucketDist(rng);
  107. ui64 * dst = buckets[bucket] + tuplesPos[bucket];
  108. ui64 *dst1 = dst + 1;
  109. ui64 *dst2 = dst + 2;
  110. ui64 *dst3 = dst + 3;
  111. ui64 *src = bigTuple;
  112. ui64 *src1 = bigTuple + 1;
  113. ui64 *src2 = bigTuple + 2;
  114. ui64 *src3 = bigTuple + 3;
  115. for (ui64 i = 0; i < TupleSize; i += 4)
  116. {
  117. *dst++ = *src++;
  118. *dst1++ = *src1++;
  119. *dst2++ = *src2++;
  120. *dst3++ = *src3++;
  121. }
  122. tuplesPos[bucket] += TupleSize;
  123. }
  124. std::chrono::steady_clock::time_point end04 = std::chrono::steady_clock::now();
  125. milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end04 - begin04).count();
  126. CTEST << "Time for loop copy = " << milliseconds << "[ms]" << Endl;
  127. CTEST << "Total MB = " << (TupleSize * NTuples * sizeof(ui64) / (1024 * 1024)) << Endl;
  128. CTEST << "Loop copy speed = " << (TupleSize * NTuples * sizeof(ui64) * 1000) / (milliseconds * 1024 * 1024) << "MB/sec" << Endl;
  129. CTEST << Endl;
  130. for (ui64 i = 0; i < NBuckets; i++) {
  131. free(buckets[i]);
  132. }
  133. free(b);
  134. free(a);
  135. free(bigTuple);
  136. UNIT_ASSERT(true);
  137. }
  138. }
  139. Y_UNIT_TEST_SUITE(TMiniKQLGraceJoinImpTest) {
  140. constexpr ui64 BigTableTuples = 600000;
  141. constexpr ui64 SmallTableTuples = 150000;
  142. constexpr ui64 BigTupleSize = 40;
  143. Y_UNIT_TEST_LLVM(TestImp1) {
  144. TSetup<LLVM> setup;
  145. ui64 tuple[11] = {0,1,2,3,4,5,6,7,8,9,10};
  146. ui32 strSizes[2] = {4, 4};
  147. char * strVals[] = {(char *)"aaaaa", (char *)"bbbb"};
  148. char * bigStrVal[] = {(char *)"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  149. (char *)"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"};
  150. ui32 bigStrSize[2] = {151, 151};
  151. NMemInfo::TMemInfo mi = NMemInfo::GetMemInfo();
  152. CTEST << "Mem usage before tables tuples added (MB): " << mi.RSS / (1024 * 1024) << Endl;
  153. GraceJoin::TTable bigTable(1,1,1,1);
  154. GraceJoin::TTable smallTable(1,1,1,1);
  155. GraceJoin::TTable joinTable(1,1,1,1);
  156. std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
  157. const ui64 TupleSize = 1024;
  158. ui64 bigTuple[TupleSize];
  159. std::mt19937_64 rng; // deterministic PRNG
  160. std::uniform_int_distribution<ui64> dist(0, 10000 - 1);
  161. for (ui64 i = 0; i < TupleSize; i++) {
  162. bigTuple[i] = dist(rng);
  163. }
  164. ui64 milliseconds = 0;
  165. std::uniform_int_distribution<ui64> smallDist(0, SmallTableTuples - 1);
  166. std::chrono::steady_clock::time_point begin03 = std::chrono::steady_clock::now();
  167. smallTable.AddTuple(tuple, bigStrVal, bigStrSize);
  168. for ( ui64 i = 0; i < SmallTableTuples + 1; i++) {
  169. tuple[1] = smallDist(rng);
  170. tuple[2] = tuple[1];
  171. smallTable.AddTuple(tuple, strVals, strSizes);
  172. }
  173. for ( ui64 i = 0; i < BigTableTuples; i++) {
  174. tuple[1] = smallDist(rng);
  175. tuple[2] = tuple[1];
  176. bigTable.AddTuple(tuple, strVals, strSizes);
  177. }
  178. std::chrono::steady_clock::time_point end03 = std::chrono::steady_clock::now();
  179. milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  180. CTEST << "Time for hash = " << milliseconds << "[ms]" << Endl;
  181. CTEST << "Adding tuples speed: " << (BigTupleSize * (BigTableTuples + SmallTableTuples) * 1000) / ( milliseconds * 1024 * 1024) << "MB/sec" << Endl;
  182. CTEST << Endl;
  183. mi = NMemInfo::GetMemInfo();
  184. CTEST << "Mem usage after tables tuples added (MB): " << mi.RSS / (1024 * 1024) << Endl;
  185. bigTable.Clear();
  186. smallTable.Clear();
  187. begin03 = std::chrono::steady_clock::now();
  188. smallTable.AddTuple(tuple, bigStrVal, bigStrSize);
  189. for ( ui64 i = 0; i < SmallTableTuples + 1; i++) {
  190. tuple[1] = smallDist(rng);
  191. tuple[2] = tuple[1];
  192. smallTable.AddTuple(tuple, strVals, strSizes);
  193. }
  194. for ( ui64 i = 0; i < BigTableTuples; i++) {
  195. tuple[1] = smallDist(rng);
  196. tuple[2] = tuple[1];
  197. bigTable.AddTuple(tuple, strVals, strSizes);
  198. }
  199. end03 = std::chrono::steady_clock::now();
  200. milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  201. CTEST << "Time for hash = " << milliseconds << "[ms]" << Endl;
  202. CTEST << "Adding tuples speed: " << (BigTupleSize * (BigTableTuples + SmallTableTuples) * 1000) / ( milliseconds * 1024 * 1024) << "MB/sec" << Endl;
  203. CTEST << Endl;
  204. mi = NMemInfo::GetMemInfo();
  205. CTEST << "Mem usage after tables tuples added (MB): " << mi.RSS / (1024 * 1024) << Endl;
  206. std::vector<ui64> vals1, vals2;
  207. std::vector<char *> strVals1, strVals2;
  208. std::vector<ui32> strSizes1, strSizes2;
  209. GraceJoin::TupleData td1, td2;
  210. vals1.resize(100);
  211. vals2.resize(100);
  212. strVals1.resize(100);
  213. strVals2.resize(100);
  214. strSizes1.resize(100);
  215. strSizes2.resize(100);
  216. td1.IntColumns = vals1.data();
  217. td1.StrColumns = strVals1.data();
  218. td1.StrSizes = strSizes1.data();
  219. td2.IntColumns = vals2.data();
  220. td2.StrColumns = strVals2.data();
  221. td2.StrSizes = strSizes2.data();
  222. ui64 numBigTuples = 0;
  223. bigTable.ResetIterator();
  224. std::chrono::steady_clock::time_point begin04 = std::chrono::steady_clock::now();
  225. while(bigTable.NextTuple(td1)) { numBigTuples++; }
  226. CTEST << "Num of big tuples 1: " << numBigTuples << Endl;
  227. std::chrono::steady_clock::time_point end04 = std::chrono::steady_clock::now();
  228. CTEST << "Time for get 1 = " << std::chrono::duration_cast<std::chrono::milliseconds>(end04 - begin04).count() << "[ms]" << Endl;
  229. CTEST << Endl;
  230. numBigTuples = 0;
  231. bigTable.ResetIterator();
  232. std::chrono::steady_clock::time_point begin041 = std::chrono::steady_clock::now();
  233. while(bigTable.NextTuple(td2)) { numBigTuples++; }
  234. CTEST << "Num of big tuples 2: " << numBigTuples << Endl;
  235. std::chrono::steady_clock::time_point end041 = std::chrono::steady_clock::now();
  236. CTEST << "Time for get 2 = " << std::chrono::duration_cast<std::chrono::milliseconds>(end041 - begin041).count() << "[ms]" << Endl;
  237. CTEST << Endl;
  238. std::chrono::steady_clock::time_point begin05 = std::chrono::steady_clock::now();
  239. joinTable.Join(smallTable,bigTable);
  240. std::chrono::steady_clock::time_point end05 = std::chrono::steady_clock::now();
  241. CTEST << "Time for join = " << std::chrono::duration_cast<std::chrono::milliseconds>(end05 - begin05).count() << "[ms]" << Endl;
  242. CTEST << Endl;
  243. mi = NMemInfo::GetMemInfo();
  244. CTEST << "Mem usage after tables join (MB): " << mi.RSS / (1024 * 1024) << Endl;
  245. joinTable.ResetIterator();
  246. ui64 numJoinedTuples = 0;
  247. std::chrono::steady_clock::time_point begin042 = std::chrono::steady_clock::now();
  248. while(joinTable.NextJoinedData(td1, td2)) { numJoinedTuples++; }
  249. CTEST << "Num of joined tuples : " << numJoinedTuples << Endl;
  250. std::chrono::steady_clock::time_point end042 = std::chrono::steady_clock::now();
  251. CTEST << "Time for get joined tuples: = " << std::chrono::duration_cast<std::chrono::milliseconds>(end042 - begin042).count() << "[ms]" << Endl;
  252. CTEST << Endl;
  253. std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
  254. CTEST << "Time difference = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << Endl;
  255. CTEST << Endl;
  256. }
  257. Y_UNIT_TEST_LLVM(TestImp1Batch) {
  258. TSetup<LLVM> setup;
  259. ui64 tuple[11] = {0,1,2,3,4,5,6,7,8,9,10};
  260. ui32 strSizes[2] = {4, 4};
  261. char * strVals[] = {(char *)"aaaaa", (char *)"bbbb"};
  262. char * bigStrVal[] = {(char *)"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  263. (char *)"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"};
  264. ui32 bigStrSize[2] = {151, 151};
  265. NMemInfo::TMemInfo mi = NMemInfo::GetMemInfo();
  266. CTEST << "Mem usage before tables tuples added (MB): " << mi.RSS / (1024 * 1024) << Endl;
  267. GraceJoin::TTable bigTable(1,1,1,1);
  268. GraceJoin::TTable smallTable(1,1,1,1);
  269. GraceJoin::TTable joinTable(1,1,1,1);
  270. std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
  271. const ui64 TupleSize = 1024;
  272. ui64 bigTuple[TupleSize];
  273. std::mt19937_64 rng; // deterministic PRNG
  274. std::uniform_int_distribution<ui64> dist(0, 10000 - 1);
  275. for (ui64 i = 0; i < TupleSize; i++) {
  276. bigTuple[i] = dist(rng);
  277. }
  278. ui64 millisecondsAdd = 0;
  279. ui64 millisecondsJoin = 0;
  280. ui64 millisecondsNextJoinTuple = 0;
  281. ui64 millisecondsNextTuple = 0;
  282. const ui64 BatchTuples = 100000;
  283. std::uniform_int_distribution<ui64> smallDist(0, SmallTableTuples - 1);
  284. {
  285. std::chrono::steady_clock::time_point begin03 = std::chrono::steady_clock::now();
  286. smallTable.AddTuple(tuple, bigStrVal, bigStrSize);
  287. for ( ui64 i = 0; i < SmallTableTuples + 1; i++) {
  288. tuple[1] = smallDist(rng);
  289. tuple[2] = tuple[1];
  290. smallTable.AddTuple(tuple, strVals, strSizes);
  291. }
  292. std::chrono::steady_clock::time_point end03 = std::chrono::steady_clock::now();
  293. millisecondsAdd += std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  294. }
  295. for ( ui64 pos = 0; pos < BigTableTuples; ) {
  296. std::chrono::steady_clock::time_point begin03 = std::chrono::steady_clock::now();
  297. ui64 limit = std::min(pos + BatchTuples, BigTableTuples);
  298. for (; pos < limit; ++pos) {
  299. tuple[1] = smallDist(rng);
  300. tuple[2] = tuple[1];
  301. bigTable.AddTuple(tuple, strVals, strSizes);
  302. }
  303. bigTable.Clear();
  304. std::chrono::steady_clock::time_point end03 = std::chrono::steady_clock::now();
  305. millisecondsAdd += std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  306. }
  307. CTEST << "Time for hash = " << millisecondsAdd << "[ms]" << Endl;
  308. CTEST << "Adding tuples speed: " << (BigTupleSize * (BigTableTuples + SmallTableTuples) * 1000) / ( millisecondsAdd * 1024 * 1024) << "MB/sec" << Endl;
  309. CTEST << Endl;
  310. mi = NMemInfo::GetMemInfo();
  311. CTEST << "Mem usage after tables tuples added (MB): " << mi.RSS / (1024 * 1024) << Endl;
  312. millisecondsAdd = 0;
  313. smallTable.Clear();
  314. {
  315. auto begin03 = std::chrono::steady_clock::now();
  316. smallTable.AddTuple(tuple, bigStrVal, bigStrSize);
  317. for ( ui64 i = 0; i < SmallTableTuples + 1; i++) {
  318. tuple[1] = smallDist(rng);
  319. tuple[2] = tuple[1];
  320. smallTable.AddTuple(tuple, strVals, strSizes);
  321. }
  322. auto end03 = std::chrono::steady_clock::now();
  323. millisecondsAdd += std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  324. }
  325. std::vector<ui64> vals1, vals2;
  326. std::vector<char *> strVals1, strVals2;
  327. std::vector<ui32> strSizes1, strSizes2;
  328. GraceJoin::TupleData td1, td2;
  329. vals1.resize(100);
  330. vals2.resize(100);
  331. strVals1.resize(100);
  332. strVals2.resize(100);
  333. strSizes1.resize(100);
  334. strSizes2.resize(100);
  335. td1.IntColumns = vals1.data();
  336. td1.StrColumns = strVals1.data();
  337. td1.StrSizes = strSizes1.data();
  338. td2.IntColumns = vals2.data();
  339. td2.StrColumns = strVals2.data();
  340. td2.StrSizes = strSizes2.data();
  341. ui64 numJoinedTuples = 0;
  342. ui64 numBigTuples = 0;
  343. for ( ui64 pos = 0; pos < BigTableTuples; ) {
  344. std::chrono::steady_clock::time_point begin03 = std::chrono::steady_clock::now();
  345. bigTable.Clear();
  346. ui64 limit = std::min(pos + BatchTuples, BigTableTuples);
  347. for (; pos < limit; ++pos) {
  348. tuple[1] = smallDist(rng);
  349. tuple[2] = tuple[1];
  350. bigTable.AddTuple(tuple, strVals, strSizes);
  351. }
  352. auto end03 = std::chrono::steady_clock::now();
  353. millisecondsAdd += std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  354. bigTable.ResetIterator();
  355. std::chrono::steady_clock::time_point begin04 = std::chrono::steady_clock::now();
  356. while(bigTable.NextTuple(td1)) { numBigTuples++; }
  357. std::chrono::steady_clock::time_point end04 = std::chrono::steady_clock::now();
  358. millisecondsNextTuple += std::chrono::duration_cast<std::chrono::milliseconds>(end04 - begin04).count();
  359. std::chrono::steady_clock::time_point begin05 = std::chrono::steady_clock::now();
  360. joinTable.Join(smallTable, bigTable, EJoinKind::Inner, false, pos < BigTableTuples);
  361. std::chrono::steady_clock::time_point end05 = std::chrono::steady_clock::now();
  362. millisecondsJoin += std::chrono::duration_cast<std::chrono::milliseconds>(end05 - begin05).count();
  363. joinTable.ResetIterator();
  364. std::chrono::steady_clock::time_point begin042 = std::chrono::steady_clock::now();
  365. while(joinTable.NextJoinedData(td1, td2)) { numJoinedTuples++; }
  366. std::chrono::steady_clock::time_point end042 = std::chrono::steady_clock::now();
  367. millisecondsNextJoinTuple += std::chrono::duration_cast<std::chrono::milliseconds>(end042 - begin042).count();
  368. }
  369. std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
  370. CTEST << "Num of big tuples 1: " << numBigTuples << Endl;
  371. CTEST << "Time for get 1 = " << millisecondsNextTuple << "[ms]" << Endl;
  372. CTEST << Endl;
  373. CTEST << "Time for join = " << millisecondsJoin << "[ms]" << Endl;
  374. CTEST << Endl;
  375. CTEST << "Time for get joined tuples: = " << millisecondsNextJoinTuple << "[ms]" << Endl;
  376. CTEST << Endl;
  377. mi = NMemInfo::GetMemInfo();
  378. CTEST << "Mem usage after tables add and join (MB): " << mi.RSS / (1024 * 1024) << Endl;
  379. CTEST << "Time for hash = " << millisecondsAdd << "[ms]" << Endl;
  380. CTEST << "Adding tuples speed: " << (BigTupleSize * (BigTableTuples + SmallTableTuples) * 1000) / ( millisecondsAdd * 1024 * 1024) << "MB/sec" << Endl;
  381. CTEST << Endl;
  382. CTEST << "Num of joined tuples : " << numJoinedTuples << Endl;
  383. CTEST << "Time difference = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << Endl;
  384. CTEST << Endl;
  385. }
  386. }
  387. Y_UNIT_TEST_SUITE(TMiniKQLGraceJoinAnyTest) {
  388. Y_UNIT_TEST_LLVM(TestImp2) {
  389. TSetup<LLVM> setup;
  390. ui64 tuple[11] = {0,1,2,3,4,5,6,7,8,9,10};
  391. ui32 strSizes[2] = {4, 4};
  392. char * strVals[] = {(char *)"aaaaa", (char *)"bbbb"};
  393. char * bigStrVal[] = {(char *)"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  394. (char *)"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"};
  395. ui32 bigStrSize[2] = {151, 151};
  396. GraceJoin::TTable bigTable (1,1,1,1,0,0,1, nullptr, true);
  397. GraceJoin::TTable smallTable(1,1,1,1,0,0,1, nullptr, true);
  398. GraceJoin::TTable joinTable (1,1,1,1,0,0,1, nullptr, true);
  399. std::mt19937_64 rng;
  400. std::uniform_int_distribution<ui64> dist(0, 10000 - 1);
  401. std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
  402. const ui64 TupleSize = 1024;
  403. ui64 bigTuple[TupleSize];
  404. for (ui64 i = 0; i < TupleSize; i++) {
  405. bigTuple[i] = dist(rng);
  406. }
  407. ui64 milliseconds = 0;
  408. const ui64 BigTableTuples = 600000;
  409. const ui64 SmallTableTuples = 150000;
  410. const ui64 BigTupleSize = 40;
  411. std::uniform_int_distribution<ui64> smallDist(0, SmallTableTuples - 1);
  412. std::chrono::steady_clock::time_point begin03 = std::chrono::steady_clock::now();
  413. smallTable.AddTuple(tuple, bigStrVal, bigStrSize);
  414. for ( ui64 i = 0; i < SmallTableTuples + 1; i++) {
  415. tuple[1] = i;
  416. tuple[2] = tuple[1];
  417. smallTable.AddTuple(tuple, strVals, strSizes);
  418. }
  419. for ( ui64 i = 0; i < BigTableTuples; i++) {
  420. tuple[1] = i % SmallTableTuples;
  421. tuple[2] = tuple[1];
  422. bigTable.AddTuple(tuple, strVals, strSizes);
  423. }
  424. std::chrono::steady_clock::time_point end03 = std::chrono::steady_clock::now();
  425. milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  426. CTEST << "Time for hash = " << milliseconds << "[ms]" << Endl;
  427. CTEST << "Adding tuples speed: " << (BigTupleSize * (BigTableTuples + SmallTableTuples) * 1000) / ( milliseconds * 1024 * 1024) << "MB/sec" << Endl;
  428. CTEST << Endl;
  429. std::vector<ui64> vals1, vals2;
  430. std::vector<char *> strVals1, strVals2;
  431. std::vector<ui32> strSizes1, strSizes2;
  432. GraceJoin::TupleData td1, td2;
  433. vals1.resize(100);
  434. vals2.resize(100);
  435. strVals1.resize(100);
  436. strVals2.resize(100);
  437. strSizes1.resize(100);
  438. strSizes2.resize(100);
  439. td1.IntColumns = vals1.data();
  440. td1.StrColumns = strVals1.data();
  441. td1.StrSizes = strSizes1.data();
  442. td2.IntColumns = vals2.data();
  443. td2.StrColumns = strVals2.data();
  444. td2.StrSizes = strSizes2.data();
  445. ui64 numBigTuples = 0;
  446. bigTable.ResetIterator();
  447. std::chrono::steady_clock::time_point begin04 = std::chrono::steady_clock::now();
  448. while(bigTable.NextTuple(td1)) { numBigTuples++; }
  449. CTEST << "Num of big tuples 1: " << numBigTuples << Endl;
  450. std::chrono::steady_clock::time_point end04 = std::chrono::steady_clock::now();
  451. CTEST << "Time for get 1 = " << std::chrono::duration_cast<std::chrono::milliseconds>(end04 - begin04).count() << "[ms]" << Endl;
  452. CTEST << Endl;
  453. numBigTuples = 0;
  454. bigTable.ResetIterator();
  455. std::chrono::steady_clock::time_point begin041 = std::chrono::steady_clock::now();
  456. while(bigTable.NextTuple(td2)) { numBigTuples++; }
  457. CTEST << "Num of big tuples 2: " << numBigTuples << Endl;
  458. std::chrono::steady_clock::time_point end041 = std::chrono::steady_clock::now();
  459. CTEST << "Time for get 2 = " << std::chrono::duration_cast<std::chrono::milliseconds>(end041 - begin041).count() << "[ms]" << Endl;
  460. CTEST << Endl;
  461. std::chrono::steady_clock::time_point begin05 = std::chrono::steady_clock::now();
  462. joinTable.Join(smallTable,bigTable);
  463. std::chrono::steady_clock::time_point end05 = std::chrono::steady_clock::now();
  464. CTEST << "Time for join = " << std::chrono::duration_cast<std::chrono::milliseconds>(end05 - begin05).count() << "[ms]" << Endl;
  465. CTEST << Endl;
  466. joinTable.ResetIterator();
  467. ui64 numJoinedTuples = 0;
  468. std::chrono::steady_clock::time_point begin042 = std::chrono::steady_clock::now();
  469. while(joinTable.NextJoinedData(td1, td2)) { numJoinedTuples++; }
  470. CTEST << "Num of joined tuples : " << numJoinedTuples << Endl;
  471. std::chrono::steady_clock::time_point end042 = std::chrono::steady_clock::now();
  472. CTEST << "Time for get joined tuples: = " << std::chrono::duration_cast<std::chrono::milliseconds>(end042 - begin042).count() << "[ms]" << Endl;
  473. CTEST << Endl;
  474. std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
  475. CTEST << "Time difference = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << Endl;
  476. CTEST << Endl;
  477. }
  478. }
  479. Y_UNIT_TEST_SUITE(TMiniKQLGraceSelfJoinTest) {
  480. Y_UNIT_TEST_LLVM(TestImp3) {
  481. TSetup<LLVM> setup;
  482. ui64 tuple[11] = {0,1,2,3,4,5,6,7,8,9,10};
  483. ui32 strSizes[2] = {4, 4};
  484. char * strVals[] = {(char *)"aaaaa", (char *)"bbbb"};
  485. char * bigStrVal[] = {(char *)"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  486. (char *)"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"};
  487. ui32 bigStrSize[2] = {151, 151};
  488. GraceJoin::TTable bigTable (1,1,1,1,0,0,1, nullptr, false);
  489. GraceJoin::TTable smallTable(1,1,1,1,0,0,1, nullptr, false);
  490. GraceJoin::TTable joinTable (1,1,1,1,0,0,1, nullptr, false);
  491. std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
  492. const ui64 TupleSize = 1024;
  493. ui64 bigTuple[TupleSize];
  494. std::mt19937_64 rng;
  495. std::uniform_int_distribution<ui64> dist(0, 10000 - 1);
  496. for (ui64 i = 0; i < TupleSize; i++) {
  497. bigTuple[i] = dist(rng);
  498. }
  499. ui64 milliseconds = 0;
  500. const ui64 BigTableTuples = 600000;
  501. const ui64 SmallTableTuples = 150000;
  502. const ui64 BigTupleSize = 40;
  503. std::chrono::steady_clock::time_point begin03 = std::chrono::steady_clock::now();
  504. smallTable.AddTuple(tuple, bigStrVal, bigStrSize);
  505. for ( ui64 i = 0; i < SmallTableTuples + 1; i++) {
  506. tuple[1] = i;
  507. tuple[2] = tuple[1];
  508. smallTable.AddTuple(tuple, strVals, strSizes);
  509. }
  510. for ( ui64 i = 0; i < BigTableTuples; i++) {
  511. tuple[1] = i % SmallTableTuples;
  512. tuple[2] = tuple[1];
  513. bigTable.AddTuple(tuple, strVals, strSizes);
  514. }
  515. std::chrono::steady_clock::time_point end03 = std::chrono::steady_clock::now();
  516. milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end03 - begin03).count();
  517. CTEST << "Time for hash = " << milliseconds << "[ms]" << Endl;
  518. CTEST << "Adding tuples speed: " << (BigTupleSize * (BigTableTuples + SmallTableTuples) * 1000) / ( milliseconds * 1024 * 1024) << "MB/sec" << Endl;
  519. CTEST << Endl;
  520. std::vector<ui64> vals1, vals2;
  521. std::vector<char *> strVals1, strVals2;
  522. std::vector<ui32> strSizes1, strSizes2;
  523. GraceJoin::TupleData td1, td2;
  524. vals1.resize(100);
  525. vals2.resize(100);
  526. strVals1.resize(100);
  527. strVals2.resize(100);
  528. strSizes1.resize(100);
  529. strSizes2.resize(100);
  530. td1.IntColumns = vals1.data();
  531. td1.StrColumns = strVals1.data();
  532. td1.StrSizes = strSizes1.data();
  533. td2.IntColumns = vals2.data();
  534. td2.StrColumns = strVals2.data();
  535. td2.StrSizes = strSizes2.data();
  536. ui64 numBigTuples = 0;
  537. bigTable.ResetIterator();
  538. std::chrono::steady_clock::time_point begin04 = std::chrono::steady_clock::now();
  539. while(bigTable.NextTuple(td1)) { numBigTuples++; }
  540. CTEST << "Num of big tuples 1: " << numBigTuples << Endl;
  541. std::chrono::steady_clock::time_point end04 = std::chrono::steady_clock::now();
  542. CTEST << "Time for get 1 = " << std::chrono::duration_cast<std::chrono::milliseconds>(end04 - begin04).count() << "[ms]" << Endl;
  543. CTEST << Endl;
  544. numBigTuples = 0;
  545. bigTable.ResetIterator();
  546. std::chrono::steady_clock::time_point begin041 = std::chrono::steady_clock::now();
  547. while(bigTable.NextTuple(td2)) { numBigTuples++; }
  548. CTEST << "Num of big tuples 2: " << numBigTuples << Endl;
  549. std::chrono::steady_clock::time_point end041 = std::chrono::steady_clock::now();
  550. CTEST << "Time for get 2 = " << std::chrono::duration_cast<std::chrono::milliseconds>(end041 - begin041).count() << "[ms]" << Endl;
  551. CTEST << Endl;
  552. std::chrono::steady_clock::time_point begin05 = std::chrono::steady_clock::now();
  553. joinTable.Join(bigTable,bigTable);
  554. std::chrono::steady_clock::time_point end05 = std::chrono::steady_clock::now();
  555. CTEST << "Time for join = " << std::chrono::duration_cast<std::chrono::milliseconds>(end05 - begin05).count() << "[ms]" << Endl;
  556. CTEST << Endl;
  557. joinTable.ResetIterator();
  558. ui64 numJoinedTuples = 0;
  559. std::chrono::steady_clock::time_point begin042 = std::chrono::steady_clock::now();
  560. while(joinTable.NextJoinedData(td1, td2)) { numJoinedTuples++; }
  561. CTEST << "Num of joined tuples : " << numJoinedTuples << Endl;
  562. std::chrono::steady_clock::time_point end042 = std::chrono::steady_clock::now();
  563. CTEST << "Time for get joined tuples: = " << std::chrono::duration_cast<std::chrono::milliseconds>(end042 - begin042).count() << "[ms]" << Endl;
  564. CTEST << Endl;
  565. std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
  566. CTEST << "Time difference = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << Endl;
  567. CTEST << Endl;
  568. }
  569. }
  570. #if !defined(MKQL_RUNTIME_VERSION) || MKQL_RUNTIME_VERSION >= 40u
  571. Y_UNIT_TEST_SUITE(TMiniKQLSelfJoinTest) {
  572. Y_UNIT_TEST_LLVM_SPILLING(TestInner1) {
  573. if (SPILLING && RuntimeVersion < 50) return;
  574. for (ui32 pass = 0; pass < 1; ++pass) {
  575. TSetup<LLVM, SPILLING> setup;
  576. TProgramBuilder& pb = *setup.PgmBuilder;
  577. const auto key1 = pb.NewDataLiteral<ui32>(1);
  578. const auto key2 = pb.NewDataLiteral<ui32>(2);
  579. const auto key3 = pb.NewDataLiteral<ui32>(4);
  580. const auto key4 = pb.NewDataLiteral<ui32>(4);
  581. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  582. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  583. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  584. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  585. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  586. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  587. const auto tupleType = pb.NewTupleType({
  588. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  589. pb.NewDataType(NUdf::TDataType<char*>::Id)
  590. });
  591. const auto list1 = pb.NewList(tupleType, {
  592. pb.NewTuple({key1, payload1}),
  593. pb.NewTuple({key2, payload2}),
  594. pb.NewTuple({key3, payload3}),
  595. pb.NewTuple({key4, payload4})
  596. });
  597. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  598. pb.NewDataType(NUdf::TDataType<char*>::Id),
  599. pb.NewDataType(NUdf::TDataType<char*>::Id)
  600. }));
  601. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceSelfJoin(
  602. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  603. EJoinKind::Inner, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  604. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  605. );
  606. if (SPILLING) {
  607. setup.RenameCallable(pgmReturn, "GraceSelfJoin", "GraceSelfJoinWithSpilling");
  608. }
  609. const auto graph = setup.BuildGraph(pgmReturn);
  610. if (SPILLING) {
  611. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  612. }
  613. const auto iterator = graph->GetValue().GetListIterator();
  614. NUdf::TUnboxedValue tuple;
  615. std::map<std::pair<TString, TString>, ui32> u;
  616. while (iterator.Next(tuple)) {
  617. auto t0 = tuple.GetElement(0);
  618. auto t1 = tuple.GetElement(1);
  619. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  620. }
  621. UNIT_ASSERT(!iterator.Next(tuple));
  622. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("C"))], 1);
  623. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("X"))], 1);
  624. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("X"), TString("C"))], 1);
  625. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("X"), TString("X"))], 1);
  626. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("B"))], 1);
  627. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), TString("A"))], 1);
  628. UNIT_ASSERT_EQUAL(u.size(), 6);
  629. }
  630. }
  631. Y_UNIT_TEST_LLVM_SPILLING(TestDiffKeys) {
  632. if (SPILLING && RuntimeVersion < 50) return;
  633. for (ui32 pass = 0; pass < 1; ++pass) {
  634. TSetup<LLVM, SPILLING> setup;
  635. TProgramBuilder& pb = *setup.PgmBuilder;
  636. const auto key1 = pb.NewDataLiteral<ui32>(1);
  637. const auto key2 = pb.NewDataLiteral<ui32>(2);
  638. const auto key3 = pb.NewDataLiteral<ui32>(4);
  639. const auto key4 = pb.NewDataLiteral<ui32>(4);
  640. const auto key11 = pb.NewDataLiteral<ui32>(1);
  641. const auto key21 = pb.NewDataLiteral<ui32>(1);
  642. const auto key31 = pb.NewDataLiteral<ui32>(2);
  643. const auto key41 = pb.NewDataLiteral<ui32>(3);
  644. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  645. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  646. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  647. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  648. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  649. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  650. const auto tupleType = pb.NewTupleType({
  651. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  652. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  653. pb.NewDataType(NUdf::TDataType<char*>::Id)
  654. });
  655. const auto list1 = pb.NewList(tupleType, {
  656. pb.NewTuple({key1, key11, payload1}),
  657. pb.NewTuple({key2, key21, payload2}),
  658. pb.NewTuple({key3, key31, payload3}),
  659. pb.NewTuple({key4, key41, payload4})
  660. });
  661. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  662. pb.NewDataType(NUdf::TDataType<char*>::Id),
  663. pb.NewDataType(NUdf::TDataType<char*>::Id)
  664. }));
  665. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceSelfJoin(
  666. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U), pb.Nth(item, 2U)}; }),
  667. EJoinKind::Inner, {0U}, {1U}, {2U, 0U}, {2U, 1U}, resultType),
  668. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  669. );
  670. if (SPILLING) {
  671. setup.RenameCallable(pgmReturn, "GraceSelfJoin", "GraceSelfJoinWithSpilling");
  672. }
  673. const auto graph = setup.BuildGraph(pgmReturn);
  674. if (SPILLING) {
  675. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  676. }
  677. const auto iterator = graph->GetValue().GetListIterator();
  678. NUdf::TUnboxedValue tuple;
  679. std::map<std::pair<TString, TString>, ui32> u;
  680. while (iterator.Next(tuple)) {
  681. auto t0 = tuple.GetElement(0);
  682. auto t1 = tuple.GetElement(1);
  683. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  684. }
  685. UNIT_ASSERT(!iterator.Next(tuple));
  686. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), TString("A"))], 1);
  687. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), TString("B"))], 1);
  688. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("C"))], 1);
  689. UNIT_ASSERT_EQUAL(u.size(), 3);
  690. }
  691. }
  692. }
  693. #endif
  694. Y_UNIT_TEST_SUITE(TMiniKQLGraceJoinTest) {
  695. Y_UNIT_TEST_LLVM_SPILLING(TestInner1) {
  696. if (SPILLING && RuntimeVersion < 50) return;
  697. for (ui32 pass = 0; pass < 1; ++pass) {
  698. TSetup<LLVM, SPILLING> setup;
  699. TProgramBuilder& pb = *setup.PgmBuilder;
  700. const auto key1 = pb.NewDataLiteral<ui32>(1);
  701. const auto key2 = pb.NewDataLiteral<ui32>(2);
  702. const auto key3 = pb.NewDataLiteral<ui32>(4);
  703. const auto key4 = pb.NewDataLiteral<ui32>(4);
  704. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  705. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  706. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  707. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  708. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  709. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  710. const auto tupleType = pb.NewTupleType({
  711. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  712. pb.NewDataType(NUdf::TDataType<char*>::Id)
  713. });
  714. const auto list1 = pb.NewList(tupleType, {
  715. pb.NewTuple({key1, payload1}),
  716. pb.NewTuple({key2, payload2}),
  717. pb.NewTuple({key3, payload3})
  718. });
  719. const auto list2 = pb.NewList(tupleType, {
  720. pb.NewTuple({key2, payload4}),
  721. pb.NewTuple({key3, payload5}),
  722. pb.NewTuple({key4, payload6})
  723. });
  724. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  725. pb.NewDataType(NUdf::TDataType<char*>::Id),
  726. pb.NewDataType(NUdf::TDataType<char*>::Id)
  727. }));
  728. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  729. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  730. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  731. EJoinKind::Inner, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  732. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  733. );
  734. if (SPILLING) {
  735. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  736. }
  737. const auto graph = setup.BuildGraph(pgmReturn);
  738. if (SPILLING) {
  739. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  740. }
  741. const auto iterator = graph->GetValue().GetListIterator();
  742. NUdf::TUnboxedValue tuple;
  743. std::map<std::pair<TString, TString>, ui32> u;
  744. while (iterator.Next(tuple)) {
  745. auto t0 = tuple.GetElement(0);
  746. auto t1 = tuple.GetElement(1);
  747. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  748. }
  749. UNIT_ASSERT(!iterator.Next(tuple));
  750. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  751. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Z"))], 1);
  752. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  753. UNIT_ASSERT_EQUAL(u.size(), 3);
  754. }
  755. }
  756. Y_UNIT_TEST_LLVM_SPILLING(TestInnerDoubleCondition1) {
  757. if (SPILLING && RuntimeVersion < 50) return;
  758. for (ui32 pass = 0; pass < 1; ++pass) {
  759. TSetup<LLVM, SPILLING> setup;
  760. TProgramBuilder& pb = *setup.PgmBuilder;
  761. const auto key1 = pb.NewDataLiteral<ui32>(1);
  762. const auto key2 = pb.NewDataLiteral<ui32>(2);
  763. const auto key3 = pb.NewDataLiteral<ui32>(4);
  764. const auto key4 = pb.NewDataLiteral<ui32>(4);
  765. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  766. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  767. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  768. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  769. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  770. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  771. const auto tupleType1 = pb.NewTupleType({
  772. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  773. pb.NewDataType(NUdf::TDataType<char*>::Id)
  774. });
  775. const auto tupleType2 = pb.NewTupleType({
  776. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  777. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  778. pb.NewDataType(NUdf::TDataType<char*>::Id)
  779. });
  780. const auto list1 = pb.NewList(tupleType1, {
  781. pb.NewTuple({key1, payload1}),
  782. pb.NewTuple({key2, payload2}),
  783. pb.NewTuple({key3, payload3})
  784. });
  785. const auto list2 = pb.NewList(tupleType2, {
  786. pb.NewTuple({key2, key2, payload4}),
  787. pb.NewTuple({key3, key2, payload5}),
  788. pb.NewTuple({key4, key1, payload6})
  789. });
  790. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  791. pb.NewDataType(NUdf::TDataType<char*>::Id),
  792. pb.NewDataType(NUdf::TDataType<char*>::Id)
  793. }));
  794. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  795. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  796. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U), pb.Nth(item, 2U)}; }),
  797. EJoinKind::Inner, {0U, 0U}, {0U, 1U}, {1U, 0U}, {2U, 1U}, resultType),
  798. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  799. );
  800. if (SPILLING) {
  801. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  802. }
  803. const auto graph = setup.BuildGraph(pgmReturn);
  804. if (SPILLING) {
  805. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  806. }
  807. const auto iterator = graph->GetValue().GetListIterator();
  808. NUdf::TUnboxedValue tuple;
  809. std::map<std::pair<TString, TString>, ui32> u;
  810. while (iterator.Next(tuple)) {
  811. auto t0 = tuple.GetElement(0);
  812. auto t1 = tuple.GetElement(1);
  813. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  814. }
  815. UNIT_ASSERT(!iterator.Next(tuple));
  816. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  817. UNIT_ASSERT_EQUAL(u.size(), 1);
  818. }
  819. }
  820. Y_UNIT_TEST_LLVM_SPILLING(TestInnerManyKeyStrings) {
  821. if (SPILLING && RuntimeVersion < 50) return;
  822. for (ui32 pass = 0; pass < 1; ++pass) {
  823. TSetup<LLVM, SPILLING> setup;
  824. TProgramBuilder& pb = *setup.PgmBuilder;
  825. const auto key1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A1");
  826. const auto key2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A2");
  827. const auto key3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A3");
  828. const auto key4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B1");
  829. const auto key5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B2");
  830. const auto key6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B3");
  831. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  832. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  833. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  834. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  835. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  836. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  837. const auto tupleType1 = pb.NewTupleType({
  838. pb.NewDataType(NUdf::TDataType<char*>::Id),
  839. pb.NewDataType(NUdf::TDataType<char*>::Id),
  840. pb.NewDataType(NUdf::TDataType<char*>::Id)
  841. });
  842. const auto tupleType2 = pb.NewTupleType({
  843. pb.NewDataType(NUdf::TDataType<char*>::Id),
  844. pb.NewDataType(NUdf::TDataType<char*>::Id),
  845. pb.NewDataType(NUdf::TDataType<char*>::Id)
  846. });
  847. const auto list1 = pb.NewList(tupleType1, {
  848. pb.NewTuple({key1, key4, payload1}),
  849. pb.NewTuple({key2, key5, payload2}),
  850. pb.NewTuple({key3, key6, payload3})
  851. });
  852. const auto list2 = pb.NewList(tupleType2, {
  853. pb.NewTuple({key4, key1, payload4}),
  854. pb.NewTuple({key5, key2, payload5}),
  855. pb.NewTuple({key6, key6, payload6})
  856. });
  857. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  858. pb.NewDataType(NUdf::TDataType<char*>::Id),
  859. pb.NewDataType(NUdf::TDataType<char*>::Id)
  860. }));
  861. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  862. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U), pb.Nth(item, 2U)}; }),
  863. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U), pb.Nth(item, 2U)}; }),
  864. EJoinKind::Inner, {0U, 1U}, {1U, 0U}, {1U, 0U}, {2U, 1U}, resultType),
  865. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  866. );
  867. if (SPILLING) {
  868. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  869. }
  870. const auto graph = setup.BuildGraph(pgmReturn);
  871. if (SPILLING) {
  872. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  873. }
  874. const auto iterator = graph->GetValue().GetListIterator();
  875. NUdf::TUnboxedValue tuple;
  876. std::map<std::pair<TString, TString>, ui32> u;
  877. while (iterator.Next(tuple)) {
  878. auto t0 = tuple.GetElement(0);
  879. auto t1 = tuple.GetElement(1);
  880. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  881. }
  882. UNIT_ASSERT(!iterator.Next(tuple));
  883. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B2"), TString("Y"))], 1);
  884. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B1"), TString("X"))], 1);
  885. UNIT_ASSERT_EQUAL(u.size(), 2);
  886. }
  887. }
  888. Y_UNIT_TEST_LLVM_SPILLING(TestInnerManyKeyUuid) {
  889. if (SPILLING && RuntimeVersion < 50) return;
  890. for (ui32 pass = 0; pass < 1; ++pass) {
  891. TSetup<LLVM, SPILLING> setup;
  892. TProgramBuilder& pb = *setup.PgmBuilder;
  893. const auto key1 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("A1A1A1A1A1A1A1A1");
  894. const auto key2 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("A2A2A2A2A2A2A2A2");
  895. const auto key3 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("A3A3A3A3A3A3A3A3");
  896. const auto key4 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("B1B1B1B1B1B1B1B1");
  897. const auto key5 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("B2B2B2B2B2B2B2B2");
  898. const auto key6 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("B3B3B3B3B3B3B3B3");
  899. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("AAAAAAAAAAAAAAAA");
  900. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("BBBBBBBBBBBBBBBB");
  901. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("CCCCCCCCCCCCCCCC");
  902. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("XXXXXXXXXXXXXXXX");
  903. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("YYYYYYYYYYYYYYYY");
  904. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::Uuid>("ZZZZZZZZZZZZZZZZ");
  905. const auto tupleType1 = pb.NewTupleType({
  906. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id),
  907. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id),
  908. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id)
  909. });
  910. const auto tupleType2 = pb.NewTupleType({
  911. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id),
  912. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id),
  913. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id)
  914. });
  915. const auto list1 = pb.NewList(tupleType1, {
  916. pb.NewTuple({key1, key4, payload1}),
  917. pb.NewTuple({key2, key5, payload2}),
  918. pb.NewTuple({key3, key6, payload3})
  919. });
  920. const auto list2 = pb.NewList(tupleType2, {
  921. pb.NewTuple({key4, key1, payload4}),
  922. pb.NewTuple({key5, key2, payload5}),
  923. pb.NewTuple({key6, key6, payload6})
  924. });
  925. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  926. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id),
  927. pb.NewDataType(NUdf::TDataType<NUdf::TUuid>::Id)
  928. }));
  929. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  930. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U), pb.Nth(item, 2U)}; }),
  931. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U), pb.Nth(item, 2U)}; }),
  932. EJoinKind::Inner, {0U, 1U}, {1U, 0U}, {1U, 0U}, {2U, 1U}, resultType),
  933. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  934. );
  935. if (SPILLING) {
  936. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  937. }
  938. const auto graph = setup.BuildGraph(pgmReturn);
  939. if (SPILLING) {
  940. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  941. }
  942. const auto iterator = graph->GetValue().GetListIterator();
  943. NUdf::TUnboxedValue tuple;
  944. std::map<std::pair<TString, TString>, ui32> u;
  945. while (iterator.Next(tuple)) {
  946. auto t0 = tuple.GetElement(0);
  947. auto t1 = tuple.GetElement(1);
  948. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  949. }
  950. UNIT_ASSERT(!iterator.Next(tuple));
  951. UNIT_ASSERT_EQUAL(u[std::make_pair(
  952. TString("B2B2B2B2B2B2B2B2"),
  953. TString("YYYYYYYYYYYYYYYY")
  954. )], 1);
  955. UNIT_ASSERT_EQUAL(u[std::make_pair(
  956. TString("B1B1B1B1B1B1B1B1"),
  957. TString("XXXXXXXXXXXXXXXX")
  958. )], 1);
  959. UNIT_ASSERT_EQUAL(u.size(), 2);
  960. }
  961. }
  962. Y_UNIT_TEST_LLVM_SPILLING(TestInnerStringKey1) {
  963. if (SPILLING && RuntimeVersion < 50) return;
  964. for (ui32 pass = 0; pass < 1; ++pass) {
  965. TSetup<LLVM, SPILLING> setup;
  966. TProgramBuilder& pb = *setup.PgmBuilder;
  967. const auto key1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("1");
  968. const auto key2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("2");
  969. const auto key3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("4");
  970. const auto key4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("4");
  971. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  972. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  973. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  974. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  975. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  976. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  977. const auto tupleType = pb.NewTupleType({
  978. pb.NewDataType(NUdf::TDataType<char*>::Id),
  979. pb.NewDataType(NUdf::TDataType<char*>::Id)
  980. });
  981. const auto list1 = pb.NewList(tupleType, {
  982. pb.NewTuple({key1, payload1}),
  983. pb.NewTuple({key2, payload2}),
  984. pb.NewTuple({key3, payload3})
  985. });
  986. const auto list2 = pb.NewList(tupleType, {
  987. pb.NewTuple({key2, payload4}),
  988. pb.NewTuple({key3, payload5}),
  989. pb.NewTuple({key4, payload6})
  990. });
  991. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  992. pb.NewDataType(NUdf::TDataType<char*>::Id),
  993. pb.NewDataType(NUdf::TDataType<char*>::Id)
  994. }));
  995. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  996. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  997. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  998. EJoinKind::Inner, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  999. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1000. );
  1001. if (SPILLING) {
  1002. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1003. }
  1004. const auto graph = setup.BuildGraph(pgmReturn);
  1005. if (SPILLING) {
  1006. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1007. }
  1008. const auto iterator = graph->GetValue().GetListIterator();
  1009. NUdf::TUnboxedValue tuple;
  1010. std::map<std::pair<TString, TString>, ui32> u;
  1011. while (iterator.Next(tuple)) {
  1012. auto t0 = tuple.GetElement(0);
  1013. auto t1 = tuple.GetElement(1);
  1014. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  1015. }
  1016. UNIT_ASSERT(!iterator.Next(tuple));
  1017. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  1018. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  1019. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Z"))], 1);
  1020. UNIT_ASSERT_EQUAL(u.size(), 3);
  1021. }
  1022. }
  1023. Y_UNIT_TEST_LLVM_SPILLING(TMiniKQLGraceJoinTestInnerMulti1) {
  1024. if (SPILLING && RuntimeVersion < 50) return;
  1025. for (ui32 pass = 0; pass < 1; ++pass) {
  1026. TSetup<LLVM, SPILLING> setup;
  1027. TProgramBuilder& pb = *setup.PgmBuilder;
  1028. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1029. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1030. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1031. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1032. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1033. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1034. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1035. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1036. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1037. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1038. const auto tupleType = pb.NewTupleType({
  1039. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1040. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1041. });
  1042. const auto list1 = pb.NewList(tupleType, {
  1043. pb.NewTuple({key1, payload1}),
  1044. pb.NewTuple({key2, payload2}),
  1045. pb.NewTuple({key3, payload3})
  1046. });
  1047. const auto list2 = pb.NewList(tupleType, {
  1048. pb.NewTuple({key2, payload4}),
  1049. pb.NewTuple({key3, payload5}),
  1050. pb.NewTuple({key4, payload6})
  1051. });
  1052. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1053. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1054. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1055. }));
  1056. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1057. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1058. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1059. EJoinKind::Inner, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  1060. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1061. );
  1062. if (SPILLING) {
  1063. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1064. }
  1065. const auto graph = setup.BuildGraph(pgmReturn);
  1066. if (SPILLING) {
  1067. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1068. }
  1069. const auto iterator = graph->GetValue().GetListIterator();
  1070. NUdf::TUnboxedValue tuple;
  1071. std::map<std::pair<TString, TString>, ui32> u;
  1072. while (iterator.Next(tuple)) {
  1073. auto t0 = tuple.GetElement(0);
  1074. auto t1 = tuple.GetElement(1);
  1075. ++u[std::make_pair(TString(t0.AsStringRef()), TString(t1.AsStringRef()) )];
  1076. }
  1077. UNIT_ASSERT(!iterator.Next(tuple));
  1078. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  1079. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("Y"))], 1);
  1080. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("X"))], 1);
  1081. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  1082. UNIT_ASSERT_EQUAL(u.size(), 4);
  1083. }
  1084. }
  1085. Y_UNIT_TEST_LLVM_SPILLING(TestLeft1) {
  1086. if (SPILLING && RuntimeVersion < 50) return;
  1087. for (ui32 pass = 0; pass < 1; ++pass) {
  1088. TSetup<LLVM, SPILLING> setup;
  1089. TProgramBuilder& pb = *setup.PgmBuilder;
  1090. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1091. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1092. const auto key3 = pb.NewDataLiteral<ui32>(3);
  1093. const auto key4 = pb.NewDataLiteral<ui32>(4);
  1094. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1095. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1096. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1097. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1098. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1099. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1100. const auto tupleType = pb.NewTupleType({
  1101. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1102. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1103. });
  1104. const auto list1 = pb.NewList(tupleType, {
  1105. pb.NewTuple({key1, payload1}),
  1106. pb.NewTuple({key2, payload2}),
  1107. pb.NewTuple({key3, payload3})
  1108. });
  1109. const auto list2 = pb.NewList(tupleType, {
  1110. pb.NewTuple({key2, payload4}),
  1111. pb.NewTuple({key3, payload5}),
  1112. pb.NewTuple({key4, payload6})
  1113. });
  1114. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1115. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1116. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1117. }));
  1118. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1119. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1120. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1121. EJoinKind::Left, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  1122. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1123. );
  1124. if (SPILLING) {
  1125. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1126. }
  1127. const auto graph = setup.BuildGraph(pgmReturn);
  1128. if (SPILLING) {
  1129. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1130. }
  1131. const auto iterator = graph->GetValue().GetListIterator();
  1132. NUdf::TUnboxedValue tuple;
  1133. std::map<std::pair<TString, TString>, ui32> u;
  1134. // use empty TString as replacement for NULL
  1135. while (iterator.Next(tuple)) {
  1136. auto t0 = tuple.GetElement(0);
  1137. auto t1 = tuple.GetElement(1);
  1138. UNIT_ASSERT(!t0 || !t0.AsStringRef().Empty()); // ensure no empty strings
  1139. UNIT_ASSERT(!t1 || !t1.AsStringRef().Empty());
  1140. ++u[std::make_pair(t0 ? TString(t0.AsStringRef()) : TString(), t1 ? TString(t1.AsStringRef()) : TString())];
  1141. }
  1142. UNIT_ASSERT(!iterator.Next(tuple));
  1143. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), TString())], 1);
  1144. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  1145. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  1146. UNIT_ASSERT_EQUAL(u.size(), 3);
  1147. }
  1148. }
  1149. Y_UNIT_TEST_LLVM_SPILLING(TestLeftMulti1) {
  1150. if (SPILLING && RuntimeVersion < 50) return;
  1151. for (ui32 pass = 0; pass < 1; ++pass) {
  1152. TSetup<LLVM, SPILLING> setup;
  1153. TProgramBuilder& pb = *setup.PgmBuilder;
  1154. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1155. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1156. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1157. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1158. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1159. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1160. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1161. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1162. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1163. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1164. const auto tupleType = pb.NewTupleType({
  1165. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1166. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1167. });
  1168. const auto list1 = pb.NewList(tupleType, {
  1169. pb.NewTuple({key1, payload1}),
  1170. pb.NewTuple({key2, payload2}),
  1171. pb.NewTuple({key3, payload3})
  1172. });
  1173. const auto list2 = pb.NewList(tupleType, {
  1174. pb.NewTuple({key2, payload4}),
  1175. pb.NewTuple({key3, payload5}),
  1176. pb.NewTuple({key4, payload6})
  1177. });
  1178. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1179. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1180. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1181. }));
  1182. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1183. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1184. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1185. EJoinKind::Left, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  1186. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1187. );
  1188. if (SPILLING) {
  1189. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1190. }
  1191. const auto graph = setup.BuildGraph(pgmReturn);
  1192. if (SPILLING) {
  1193. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1194. }
  1195. const auto iterator = graph->GetValue().GetListIterator();
  1196. NUdf::TUnboxedValue tuple;
  1197. std::map<std::pair<TString, TString>, ui32> u;
  1198. while (iterator.Next(tuple)) {
  1199. auto t0 = tuple.GetElement(0);
  1200. auto t1 = tuple.GetElement(1);
  1201. UNIT_ASSERT(!t0 || !t0.AsStringRef().Empty()); // ensure no empty strings
  1202. UNIT_ASSERT(!t1 || !t1.AsStringRef().Empty());
  1203. ++u[std::make_pair(t0 ? TString(t0.AsStringRef()) : TString(), t1 ? TString(t1.AsStringRef()) : TString())];
  1204. }
  1205. UNIT_ASSERT(!iterator.Next(tuple));
  1206. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), TString())], 1);
  1207. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  1208. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("Y"))], 1);
  1209. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("X"))], 1);
  1210. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  1211. UNIT_ASSERT_EQUAL(u.size(), 5);
  1212. }
  1213. }
  1214. Y_UNIT_TEST_LLVM_SPILLING(TestLeftSemi1) {
  1215. if (SPILLING && RuntimeVersion < 50) return;
  1216. for (ui32 pass = 0; pass < 1; ++pass) {
  1217. TSetup<LLVM, SPILLING> setup;
  1218. TProgramBuilder& pb = *setup.PgmBuilder;
  1219. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1220. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1221. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1222. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1223. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1224. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1225. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1226. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1227. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1228. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1229. const auto tupleType = pb.NewTupleType({
  1230. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1231. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1232. });
  1233. const auto list1 = pb.NewList(tupleType, {
  1234. pb.NewTuple({key1, payload1}),
  1235. pb.NewTuple({key2, payload2}),
  1236. pb.NewTuple({key3, payload3})
  1237. });
  1238. const auto list2 = pb.NewList(tupleType, {
  1239. pb.NewTuple({key2, payload4}),
  1240. pb.NewTuple({key3, payload5}),
  1241. pb.NewTuple({key4, payload6})
  1242. });
  1243. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1244. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1245. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1246. }));
  1247. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1248. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1249. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1250. EJoinKind::LeftSemi, {0U}, {0U}, {1U, 0U, 0U, 1U}, {}, resultType),
  1251. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1252. );
  1253. if (SPILLING) {
  1254. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1255. }
  1256. const auto graph = setup.BuildGraph(pgmReturn);
  1257. if (SPILLING) {
  1258. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1259. }
  1260. const auto iterator = graph->GetValue().GetListIterator();
  1261. NUdf::TUnboxedValue tuple;
  1262. std::map<std::pair<TString, ui32>, ui32> u;
  1263. while (iterator.Next(tuple)) {
  1264. auto t0 = tuple.GetElement(0);
  1265. auto t1 = tuple.GetElement(1);
  1266. ++u[std::make_pair(TString(t0.AsStringRef()), t1.Get<ui32>())];
  1267. }
  1268. UNIT_ASSERT(!iterator.Next(tuple));
  1269. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), 2)], 1);
  1270. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), 2)], 1);
  1271. UNIT_ASSERT_EQUAL(u.size(), 2);
  1272. }
  1273. }
  1274. Y_UNIT_TEST_LLVM_SPILLING(TestLeftOnly1) {
  1275. if (SPILLING && RuntimeVersion < 50) return;
  1276. for (ui32 pass = 0; pass < 1; ++pass) {
  1277. TSetup<LLVM, SPILLING> setup;
  1278. TProgramBuilder& pb = *setup.PgmBuilder;
  1279. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1280. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1281. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1282. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1283. const auto key5 = pb.NewDataLiteral<ui32>(4);
  1284. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1285. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1286. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1287. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("D");
  1288. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1289. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1290. const auto payload7 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1291. const auto tupleType = pb.NewTupleType({
  1292. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1293. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1294. });
  1295. const auto list1 = pb.NewList(tupleType, {
  1296. pb.NewTuple({key1, payload1}),
  1297. pb.NewTuple({key2, payload2}),
  1298. pb.NewTuple({key3, payload3}),
  1299. pb.NewTuple({key4, payload4}),
  1300. pb.NewTuple({key5, payload4})
  1301. });
  1302. const auto list2 = pb.NewList(tupleType, {
  1303. pb.NewTuple({key2, payload5}),
  1304. pb.NewTuple({key3, payload6}),
  1305. pb.NewTuple({key4, payload7})
  1306. });
  1307. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1308. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1309. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1310. }));
  1311. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1312. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1313. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1314. EJoinKind::LeftOnly, {0U}, {0U}, {1U, 0U, 0U, 1U}, {}, resultType),
  1315. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1316. );
  1317. if (SPILLING) {
  1318. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1319. }
  1320. const auto graph = setup.BuildGraph(pgmReturn);
  1321. if (SPILLING) {
  1322. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1323. }
  1324. const auto iterator = graph->GetValue().GetListIterator();
  1325. NUdf::TUnboxedValue tuple;
  1326. std::map<std::pair<TString, ui32>, ui32> u;
  1327. while (iterator.Next(tuple)) {
  1328. auto t0 = tuple.GetElement(0);
  1329. auto t1 = tuple.GetElement(1);
  1330. ++u[std::make_pair(TString(t0.AsStringRef()), t1.Get<ui32>())];
  1331. }
  1332. UNIT_ASSERT(!iterator.Next(tuple));
  1333. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("D"), 4)], 1);
  1334. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), 1)], 1);
  1335. UNIT_ASSERT_EQUAL(u.size(), 2);
  1336. }
  1337. }
  1338. Y_UNIT_TEST_LLVM_SPILLING(TestLeftSemiWithNullKey1) {
  1339. if (SPILLING && RuntimeVersion < 50) return;
  1340. for (ui32 pass = 0; pass < 1; ++pass) {
  1341. TSetup<LLVM, SPILLING> setup;
  1342. TProgramBuilder& pb = *setup.PgmBuilder;
  1343. const auto key0 = pb.NewEmptyOptional(pb.NewDataType(NUdf::TDataType<ui32>::Id, true));
  1344. const auto key1 = pb.NewOptional(pb.NewDataLiteral<ui32>(1));
  1345. const auto key2 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1346. const auto key3 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1347. const auto key4 = pb.NewOptional(pb.NewDataLiteral<ui32>(3));
  1348. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1349. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1350. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1351. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1352. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1353. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1354. const auto tupleType = pb.NewTupleType({
  1355. pb.NewDataType(NUdf::TDataType<ui32>::Id, true),
  1356. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1357. });
  1358. const auto list1 = pb.NewList(tupleType, {
  1359. pb.NewTuple({key0, payload4}),
  1360. pb.NewTuple({key1, payload1}),
  1361. pb.NewTuple({key2, payload2}),
  1362. pb.NewTuple({key3, payload3})
  1363. });
  1364. const auto list2 = pb.NewList(tupleType, {
  1365. pb.NewTuple({key0, payload3}),
  1366. pb.NewTuple({key2, payload4}),
  1367. pb.NewTuple({key3, payload5}),
  1368. pb.NewTuple({key4, payload6})
  1369. });
  1370. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1371. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1372. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1373. }));
  1374. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1375. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1376. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1377. EJoinKind::LeftSemi, {0U}, {0U}, {1U, 0U, 0U, 1U}, {}, resultType),
  1378. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1379. );
  1380. if (SPILLING) {
  1381. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1382. }
  1383. const auto graph = setup.BuildGraph(pgmReturn);
  1384. if (SPILLING) {
  1385. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1386. }
  1387. const auto iterator = graph->GetValue().GetListIterator();
  1388. NUdf::TUnboxedValue tuple;
  1389. std::map<std::pair<TString, ui32>, ui32> u;
  1390. while (iterator.Next(tuple)) {
  1391. auto t0 = tuple.GetElement(0);
  1392. auto t1 = tuple.GetElement(1);
  1393. ++u[std::make_pair(TString(t0.AsStringRef()), t1.Get<ui32>())];
  1394. }
  1395. UNIT_ASSERT(!iterator.Next(tuple));
  1396. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), 2)], 1);
  1397. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), 2)], 1);
  1398. UNIT_ASSERT_EQUAL(u.size(), 2);
  1399. }
  1400. }
  1401. Y_UNIT_TEST_LLVM_SPILLING(TestLeftOnlyWithNullKey1) {
  1402. if (SPILLING && RuntimeVersion < 50) return;
  1403. for (ui32 pass = 0; pass < 1; ++pass) {
  1404. TSetup<LLVM, SPILLING> setup;
  1405. TProgramBuilder& pb = *setup.PgmBuilder;
  1406. const auto key0 = pb.NewEmptyOptional(pb.NewDataType(NUdf::TDataType<ui32>::Id, true));
  1407. const auto key1 = pb.NewOptional(pb.NewDataLiteral<ui32>(1));
  1408. const auto key2 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1409. const auto key3 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1410. const auto key4 = pb.NewOptional(pb.NewDataLiteral<ui32>(3));
  1411. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1412. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1413. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1414. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1415. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1416. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1417. const auto tupleType = pb.NewTupleType({
  1418. pb.NewDataType(NUdf::TDataType<ui32>::Id, true),
  1419. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1420. });
  1421. const auto list1 = pb.NewList(tupleType, {
  1422. pb.NewTuple({key0, payload4}),
  1423. pb.NewTuple({key1, payload1}),
  1424. pb.NewTuple({key2, payload2}),
  1425. pb.NewTuple({key3, payload3})
  1426. });
  1427. const auto list2 = pb.NewList(tupleType, {
  1428. pb.NewTuple({key0, payload3}),
  1429. pb.NewTuple({key2, payload4}),
  1430. pb.NewTuple({key3, payload5}),
  1431. pb.NewTuple({key4, payload6})
  1432. });
  1433. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1434. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1435. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1436. }));
  1437. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1438. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1439. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1440. EJoinKind::LeftOnly, {0U}, {0U}, {1U, 0U, 0U, 1U}, {}, resultType),
  1441. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1442. );
  1443. if (SPILLING) {
  1444. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1445. }
  1446. const auto graph = setup.BuildGraph(pgmReturn);
  1447. if (SPILLING) {
  1448. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1449. }
  1450. const auto iterator = graph->GetValue().GetListIterator();
  1451. NUdf::TUnboxedValue tuple;
  1452. std::map<std::pair<TString, ui64>, ui32> u;
  1453. while (iterator.Next(tuple)) {
  1454. auto t0 = tuple.GetElement(0);
  1455. auto t1 = tuple.GetElement(1);
  1456. ++u[std::make_pair(TString(t0.AsStringRef()), t1 ? t1.Get<ui32>() : std::numeric_limits<ui64>::max())];
  1457. // replace NULL with <ui64>::max()
  1458. }
  1459. UNIT_ASSERT(!iterator.Next(tuple));
  1460. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), 1)], 1);
  1461. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("X"), std::numeric_limits<ui64>::max())], 1);
  1462. UNIT_ASSERT_EQUAL(u.size(), 2);
  1463. }
  1464. }
  1465. Y_UNIT_TEST_LLVM_SPILLING(TestRight1) {
  1466. if (SPILLING && RuntimeVersion < 50) return;
  1467. for (ui32 pass = 0; pass < 1; ++pass) {
  1468. TSetup<LLVM, SPILLING> setup;
  1469. TProgramBuilder& pb = *setup.PgmBuilder;
  1470. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1471. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1472. const auto key3 = pb.NewDataLiteral<ui32>(3);
  1473. const auto key4 = pb.NewDataLiteral<ui32>(4);
  1474. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1475. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1476. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1477. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1478. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1479. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1480. const auto tupleType = pb.NewTupleType({
  1481. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1482. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1483. });
  1484. const auto list1 = pb.NewList(tupleType, {
  1485. pb.NewTuple({key1, payload1}),
  1486. pb.NewTuple({key2, payload2}),
  1487. pb.NewTuple({key3, payload3})
  1488. });
  1489. const auto list2 = pb.NewList(tupleType, {
  1490. pb.NewTuple({key2, payload4}),
  1491. pb.NewTuple({key3, payload5}),
  1492. pb.NewTuple({key4, payload6})
  1493. });
  1494. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1495. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1496. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1497. }));
  1498. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1499. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1500. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1501. EJoinKind::Right, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  1502. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1503. );
  1504. if (SPILLING) {
  1505. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1506. }
  1507. const auto graph = setup.BuildGraph(pgmReturn);
  1508. if (SPILLING) {
  1509. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1510. }
  1511. const auto iterator = graph->GetValue().GetListIterator();
  1512. NUdf::TUnboxedValue tuple;
  1513. std::map<std::pair<TString, TString>, ui32> u;
  1514. while (iterator.Next(tuple)) {
  1515. auto t0 = tuple.GetElement(0);
  1516. auto t1 = tuple.GetElement(1);
  1517. UNIT_ASSERT(!t0 || !t0.AsStringRef().Empty()); // ensure no empty strings
  1518. UNIT_ASSERT(!t1 || !t1.AsStringRef().Empty());
  1519. ++u[std::make_pair(t0 ? TString(t0.AsStringRef()) : TString(), t1 ? TString(t1.AsStringRef()) : TString())];
  1520. }
  1521. UNIT_ASSERT(!iterator.Next(tuple));
  1522. UNIT_ASSERT_EQUAL(u[std::make_pair(TString(), TString("Z"))], 1);
  1523. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  1524. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  1525. UNIT_ASSERT_EQUAL(u.size(), 3);
  1526. }
  1527. }
  1528. Y_UNIT_TEST_LLVM_SPILLING(TestRightOnly1) {
  1529. if (SPILLING && RuntimeVersion < 50) return;
  1530. for (ui32 pass = 0; pass < 1; ++pass) {
  1531. TSetup<LLVM, SPILLING> setup;
  1532. TProgramBuilder& pb = *setup.PgmBuilder;
  1533. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1534. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1535. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1536. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1537. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1538. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1539. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1540. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1541. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1542. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1543. const auto tupleType = pb.NewTupleType({
  1544. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1545. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1546. });
  1547. const auto list1 = pb.NewList(tupleType, {
  1548. pb.NewTuple({key1, payload1}),
  1549. pb.NewTuple({key2, payload2}),
  1550. pb.NewTuple({key3, payload3})
  1551. });
  1552. const auto list2 = pb.NewList(tupleType, {
  1553. pb.NewTuple({key2, payload4}),
  1554. pb.NewTuple({key3, payload5}),
  1555. pb.NewTuple({key4, payload6})
  1556. });
  1557. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1558. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1559. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1560. }));
  1561. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1562. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1563. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1564. EJoinKind::RightOnly, {0U}, {0U}, {}, {1U, 0U, 0U, 1U}, resultType),
  1565. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1566. );
  1567. if (SPILLING) {
  1568. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1569. }
  1570. const auto graph = setup.BuildGraph(pgmReturn);
  1571. if (SPILLING) {
  1572. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1573. }
  1574. const auto iterator = graph->GetValue().GetListIterator();
  1575. NUdf::TUnboxedValue tuple;
  1576. std::map<std::pair<TString, ui32>, ui32> u;
  1577. while (iterator.Next(tuple)) {
  1578. auto t0 = tuple.GetElement(0);
  1579. auto t1 = tuple.GetElement(1);
  1580. ++u[std::make_pair(TString(t0.AsStringRef()), t1.Get<ui32>())];
  1581. }
  1582. UNIT_ASSERT(!iterator.Next(tuple));
  1583. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("Z"), 3)], 1);
  1584. UNIT_ASSERT_EQUAL(u.size(), 1);
  1585. }
  1586. }
  1587. Y_UNIT_TEST_LLVM_SPILLING(TestRightSemi1) {
  1588. if (SPILLING && RuntimeVersion < 50) return;
  1589. for (ui32 pass = 0; pass < 1; ++pass) {
  1590. TSetup<LLVM, SPILLING> setup;
  1591. TProgramBuilder& pb = *setup.PgmBuilder;
  1592. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1593. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1594. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1595. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1596. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1597. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1598. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1599. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1600. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1601. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1602. const auto tupleType = pb.NewTupleType({
  1603. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1604. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1605. });
  1606. const auto list1 = pb.NewList(tupleType, {
  1607. pb.NewTuple({key1, payload1}),
  1608. pb.NewTuple({key2, payload2}),
  1609. pb.NewTuple({key3, payload3})
  1610. });
  1611. const auto list2 = pb.NewList(tupleType, {
  1612. pb.NewTuple({key2, payload4}),
  1613. pb.NewTuple({key3, payload5}),
  1614. pb.NewTuple({key4, payload6})
  1615. });
  1616. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1617. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1618. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1619. }));
  1620. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1621. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1622. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1623. EJoinKind::RightSemi, {0U}, {0U}, {}, {1U, 0U, 0U, 1U}, resultType),
  1624. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1625. );
  1626. if (SPILLING) {
  1627. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1628. }
  1629. const auto graph = setup.BuildGraph(pgmReturn);
  1630. if (SPILLING) {
  1631. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1632. }
  1633. const auto iterator = graph->GetValue().GetListIterator();
  1634. NUdf::TUnboxedValue tuple;
  1635. std::map<std::pair<TString, ui32>, ui32> u;
  1636. while (iterator.Next(tuple)) {
  1637. auto t0 = tuple.GetElement(0);
  1638. auto t1 = tuple.GetElement(1);
  1639. ++u[std::make_pair(TString(t0.AsStringRef()), t1.Get<ui32>())];
  1640. }
  1641. UNIT_ASSERT(!iterator.Next(tuple));
  1642. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("X"), 2)], 1);
  1643. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("Y"), 2)], 1);
  1644. UNIT_ASSERT_EQUAL(u.size(), 2);
  1645. }
  1646. }
  1647. Y_UNIT_TEST_LLVM_SPILLING(TestRightMulti1) {
  1648. if (SPILLING && RuntimeVersion < 50) return;
  1649. for (ui32 pass = 0; pass < 1; ++pass) {
  1650. TSetup<LLVM, SPILLING> setup;
  1651. TProgramBuilder& pb = *setup.PgmBuilder;
  1652. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1653. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1654. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1655. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1656. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1657. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1658. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1659. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1660. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1661. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1662. const auto tupleType = pb.NewTupleType({
  1663. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1664. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1665. });
  1666. const auto list1 = pb.NewList(tupleType, {
  1667. pb.NewTuple({key1, payload1}),
  1668. pb.NewTuple({key2, payload2}),
  1669. pb.NewTuple({key3, payload3})
  1670. });
  1671. const auto list2 = pb.NewList(tupleType, {
  1672. pb.NewTuple({key2, payload4}),
  1673. pb.NewTuple({key3, payload5}),
  1674. pb.NewTuple({key4, payload6})
  1675. });
  1676. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1677. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1678. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1679. }));
  1680. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1681. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1682. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1683. EJoinKind::Right, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  1684. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1685. );
  1686. if (SPILLING) {
  1687. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1688. }
  1689. const auto graph = setup.BuildGraph(pgmReturn);
  1690. if (SPILLING) {
  1691. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1692. }
  1693. const auto iterator = graph->GetValue().GetListIterator();
  1694. NUdf::TUnboxedValue tuple;
  1695. std::map<std::pair<TString, TString>, ui32> u;
  1696. while (iterator.Next(tuple)) {
  1697. auto t0 = tuple.GetElement(0);
  1698. auto t1 = tuple.GetElement(1);
  1699. UNIT_ASSERT(!t0 || !t0.AsStringRef().Empty()); // ensure no empty strings
  1700. UNIT_ASSERT(!t1 || !t1.AsStringRef().Empty());
  1701. ++u[std::make_pair(t0 ? TString(t0.AsStringRef()) : TString(), t1 ? TString(t1.AsStringRef()) : TString())];
  1702. }
  1703. UNIT_ASSERT(!iterator.Next(tuple));
  1704. UNIT_ASSERT_EQUAL(u[std::make_pair(TString(), TString("Z"))], 1);
  1705. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  1706. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("Y"))], 1);
  1707. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("X"))], 1);
  1708. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  1709. UNIT_ASSERT_EQUAL(u.size(), 5);
  1710. }
  1711. }
  1712. Y_UNIT_TEST_LLVM_SPILLING(TestRightSemiWithNullKey1) {
  1713. if (SPILLING && RuntimeVersion < 50) return;
  1714. for (ui32 pass = 0; pass < 1; ++pass) {
  1715. TSetup<LLVM, SPILLING> setup;
  1716. TProgramBuilder& pb = *setup.PgmBuilder;
  1717. const auto key0 = pb.NewEmptyOptional(pb.NewDataType(NUdf::TDataType<ui32>::Id, true));
  1718. const auto key1 = pb.NewOptional(pb.NewDataLiteral<ui32>(1));
  1719. const auto key2 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1720. const auto key3 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1721. const auto key4 = pb.NewOptional(pb.NewDataLiteral<ui32>(3));
  1722. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1723. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1724. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1725. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1726. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1727. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1728. const auto tupleType = pb.NewTupleType({
  1729. pb.NewDataType(NUdf::TDataType<ui32>::Id, true),
  1730. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1731. });
  1732. const auto list1 = pb.NewList(tupleType, {
  1733. pb.NewTuple({key0, payload4}),
  1734. pb.NewTuple({key1, payload1}),
  1735. pb.NewTuple({key2, payload2}),
  1736. pb.NewTuple({key3, payload3})
  1737. });
  1738. const auto list2 = pb.NewList(tupleType, {
  1739. pb.NewTuple({key0, payload3}),
  1740. pb.NewTuple({key2, payload4}),
  1741. pb.NewTuple({key3, payload5}),
  1742. pb.NewTuple({key4, payload6})
  1743. });
  1744. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1745. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1746. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1747. }));
  1748. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1749. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1750. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1751. EJoinKind::RightSemi, {0U}, {0U}, {}, {1U, 0U, 0U, 1U}, resultType),
  1752. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1753. );
  1754. if (SPILLING) {
  1755. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1756. }
  1757. const auto graph = setup.BuildGraph(pgmReturn);
  1758. if (SPILLING) {
  1759. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1760. }
  1761. const auto iterator = graph->GetValue().GetListIterator();
  1762. NUdf::TUnboxedValue tuple;
  1763. std::map<std::pair<TString, ui32>, ui32> u;
  1764. while (iterator.Next(tuple)) {
  1765. auto t0 = tuple.GetElement(0);
  1766. auto t1 = tuple.GetElement(1);
  1767. ++u[std::make_pair(TString(t0.AsStringRef()), t1.Get<ui32>())];
  1768. }
  1769. UNIT_ASSERT(!iterator.Next(tuple));
  1770. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("X"), 2)], 1);
  1771. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("Y"), 2)], 1);
  1772. UNIT_ASSERT_EQUAL(u.size(), 2);
  1773. }
  1774. }
  1775. Y_UNIT_TEST_LLVM_SPILLING(TestRightOnlyWithNullKey1) {
  1776. if (SPILLING && RuntimeVersion < 50) return;
  1777. for (ui32 pass = 0; pass < 1; ++pass) {
  1778. TSetup<LLVM, SPILLING> setup;
  1779. TProgramBuilder& pb = *setup.PgmBuilder;
  1780. const auto key0 = pb.NewEmptyOptional(pb.NewDataType(NUdf::TDataType<ui32>::Id, true));
  1781. const auto key1 = pb.NewOptional(pb.NewDataLiteral<ui32>(1));
  1782. const auto key2 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1783. const auto key3 = pb.NewOptional(pb.NewDataLiteral<ui32>(2));
  1784. const auto key4 = pb.NewOptional(pb.NewDataLiteral<ui32>(3));
  1785. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1786. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1787. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1788. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1789. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1790. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1791. const auto tupleType = pb.NewTupleType({
  1792. pb.NewDataType(NUdf::TDataType<ui32>::Id, true),
  1793. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1794. });
  1795. const auto list1 = pb.NewList(tupleType, {
  1796. pb.NewTuple({key0, payload4}),
  1797. pb.NewTuple({key1, payload1}),
  1798. pb.NewTuple({key2, payload2}),
  1799. pb.NewTuple({key3, payload3})
  1800. });
  1801. const auto list2 = pb.NewList(tupleType, {
  1802. pb.NewTuple({key0, payload3}),
  1803. pb.NewTuple({key2, payload4}),
  1804. pb.NewTuple({key3, payload5}),
  1805. pb.NewTuple({key4, payload6})
  1806. });
  1807. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1808. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1809. pb.NewDataType(NUdf::TDataType<ui32>::Id)
  1810. }));
  1811. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1812. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1813. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1814. EJoinKind::RightOnly, {0U}, {0U}, {}, {1U, 0U, 0U, 1U}, resultType),
  1815. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1816. );
  1817. if (SPILLING) {
  1818. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1819. }
  1820. const auto graph = setup.BuildGraph(pgmReturn);
  1821. if (SPILLING) {
  1822. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1823. }
  1824. const auto iterator = graph->GetValue().GetListIterator();
  1825. NUdf::TUnboxedValue tuple;
  1826. std::map<std::pair<TString, ui64>, ui32> u;
  1827. while (iterator.Next(tuple)) {
  1828. auto t0 = tuple.GetElement(0);
  1829. auto t1 = tuple.GetElement(1);
  1830. ++u[std::make_pair(TString(t0.AsStringRef()), t1 ? t1.Get<ui32>() : std::numeric_limits<ui64>::max())];
  1831. // replace NULL with <ui64>::max()
  1832. }
  1833. UNIT_ASSERT(!iterator.Next(tuple));
  1834. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("Z"), 3)], 1);
  1835. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), std::numeric_limits<ui64>::max())], 1);
  1836. UNIT_ASSERT_EQUAL(u.size(), 2);
  1837. }
  1838. }
  1839. Y_UNIT_TEST_LLVM_SPILLING(TestFull1) {
  1840. if (SPILLING && RuntimeVersion < 50) return;
  1841. for (ui32 pass = 0; pass < 1; ++pass) {
  1842. TSetup<LLVM, SPILLING> setup;
  1843. TProgramBuilder& pb = *setup.PgmBuilder;
  1844. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1845. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1846. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1847. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1848. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1849. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1850. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1851. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1852. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1853. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1854. const auto tupleType = pb.NewTupleType({
  1855. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1856. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1857. });
  1858. const auto list1 = pb.NewList(tupleType, {
  1859. pb.NewTuple({key1, payload1}),
  1860. pb.NewTuple({key2, payload2}),
  1861. pb.NewTuple({key3, payload3})
  1862. });
  1863. const auto list2 = pb.NewList(tupleType, {
  1864. pb.NewTuple({key2, payload4}),
  1865. pb.NewTuple({key3, payload5}),
  1866. pb.NewTuple({key4, payload6})
  1867. });
  1868. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1869. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1870. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1871. }));
  1872. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1873. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1874. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1875. EJoinKind::Full, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  1876. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1877. );
  1878. if (SPILLING) {
  1879. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1880. }
  1881. const auto graph = setup.BuildGraph(pgmReturn);
  1882. if (SPILLING) {
  1883. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1884. }
  1885. const auto iterator = graph->GetValue().GetListIterator();
  1886. NUdf::TUnboxedValue tuple;
  1887. std::map<std::pair<TString, TString>, ui32> u;
  1888. while (iterator.Next(tuple)) {
  1889. auto t0 = tuple.GetElement(0);
  1890. auto t1 = tuple.GetElement(1);
  1891. UNIT_ASSERT(!t0 || !t0.AsStringRef().Empty()); // ensure no empty strings
  1892. UNIT_ASSERT(!t1 || !t1.AsStringRef().Empty());
  1893. ++u[std::make_pair(t0 ? TString(t0.AsStringRef()) : TString(), t1 ? TString(t1.AsStringRef()) : TString())];
  1894. }
  1895. UNIT_ASSERT(!iterator.Next(tuple));
  1896. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("X"))], 1);
  1897. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("B"), TString("Y"))], 1);
  1898. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("X"))], 1);
  1899. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("C"), TString("Y"))], 1);
  1900. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), TString())], 1);
  1901. UNIT_ASSERT_EQUAL(u[std::make_pair(TString(), TString("Z"))], 1);
  1902. UNIT_ASSERT_EQUAL(u.size(), 6);
  1903. }
  1904. }
  1905. Y_UNIT_TEST_LLVM_SPILLING(TestExclusion1) {
  1906. if (SPILLING && RuntimeVersion < 50) return;
  1907. for (ui32 pass = 0; pass < 1; ++pass) {
  1908. TSetup<LLVM, SPILLING> setup;
  1909. TProgramBuilder& pb = *setup.PgmBuilder;
  1910. const auto key1 = pb.NewDataLiteral<ui32>(1);
  1911. const auto key2 = pb.NewDataLiteral<ui32>(2);
  1912. const auto key3 = pb.NewDataLiteral<ui32>(2);
  1913. const auto key4 = pb.NewDataLiteral<ui32>(3);
  1914. const auto payload1 = pb.NewDataLiteral<NUdf::EDataSlot::String>("A");
  1915. const auto payload2 = pb.NewDataLiteral<NUdf::EDataSlot::String>("B");
  1916. const auto payload3 = pb.NewDataLiteral<NUdf::EDataSlot::String>("C");
  1917. const auto payload4 = pb.NewDataLiteral<NUdf::EDataSlot::String>("X");
  1918. const auto payload5 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Y");
  1919. const auto payload6 = pb.NewDataLiteral<NUdf::EDataSlot::String>("Z");
  1920. const auto tupleType = pb.NewTupleType({
  1921. pb.NewDataType(NUdf::TDataType<ui32>::Id),
  1922. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1923. });
  1924. const auto list1 = pb.NewList(tupleType, {
  1925. pb.NewTuple({key1, payload1}),
  1926. pb.NewTuple({key2, payload2}),
  1927. pb.NewTuple({key3, payload3})
  1928. });
  1929. const auto list2 = pb.NewList(tupleType, {
  1930. pb.NewTuple({key2, payload4}),
  1931. pb.NewTuple({key3, payload5}),
  1932. pb.NewTuple({key4, payload6})
  1933. });
  1934. const auto resultType = pb.NewFlowType(pb.NewMultiType({
  1935. pb.NewDataType(NUdf::TDataType<char*>::Id),
  1936. pb.NewDataType(NUdf::TDataType<char*>::Id)
  1937. }));
  1938. const auto pgmReturn = pb.Collect(pb.NarrowMap(pb.GraceJoin(
  1939. pb.ExpandMap(pb.ToFlow(list1), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1940. pb.ExpandMap(pb.ToFlow(list2), [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U)}; }),
  1941. EJoinKind::Exclusion, {0U}, {0U}, {1U, 0U}, {1U, 1U}, resultType),
  1942. [&](TRuntimeNode::TList items) -> TRuntimeNode { return pb.NewTuple(items); })
  1943. );
  1944. if (SPILLING) {
  1945. setup.RenameCallable(pgmReturn, "GraceJoin", "GraceJoinWithSpilling");
  1946. }
  1947. const auto graph = setup.BuildGraph(pgmReturn);
  1948. if (SPILLING) {
  1949. graph->GetContext().SpillerFactory = std::make_shared<TMockSpillerFactory>();
  1950. }
  1951. const auto iterator = graph->GetValue().GetListIterator();
  1952. NUdf::TUnboxedValue tuple;
  1953. std::map<std::pair<TString, TString>, ui32> u;
  1954. while (iterator.Next(tuple)) {
  1955. auto t0 = tuple.GetElement(0);
  1956. auto t1 = tuple.GetElement(1);
  1957. UNIT_ASSERT(!t0 || !t0.AsStringRef().Empty()); // ensure no empty strings
  1958. UNIT_ASSERT(!t1 || !t1.AsStringRef().Empty());
  1959. ++u[std::make_pair(t0 ? TString(t0.AsStringRef()) : TString(), t1 ? TString(t1.AsStringRef()) : TString())];
  1960. }
  1961. UNIT_ASSERT(!iterator.Next(tuple));
  1962. UNIT_ASSERT_EQUAL(u[std::make_pair(TString("A"), TString())], 1);
  1963. UNIT_ASSERT_EQUAL(u[std::make_pair(TString(), TString("Z"))], 1);
  1964. UNIT_ASSERT_EQUAL(u.size(), 2);
  1965. }
  1966. }
  1967. }
  1968. }
  1969. }