mkql_multimap_ut.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "mkql_computation_node_ut.h"
  2. #include <yql/essentials/minikql/mkql_runtime_version.h>
  3. namespace NKikimr {
  4. namespace NMiniKQL {
  5. Y_UNIT_TEST_SUITE(TMiniKQLMultiMapTest) {
  6. Y_UNIT_TEST_LLVM(TestOverList) {
  7. TSetup<LLVM> setup;
  8. TProgramBuilder& pb = *setup.PgmBuilder;
  9. const auto data1 = pb.NewDataLiteral<ui32>(1);
  10. const auto data2 = pb.NewDataLiteral<ui32>(2);
  11. const auto data3 = pb.NewDataLiteral<ui32>(3);
  12. const auto dataType = pb.NewDataType(NUdf::TDataType<ui32>::Id);
  13. const auto list = pb.NewList(dataType, {data1, data2, data3});
  14. const auto pgmReturn = pb.MultiMap(list,
  15. [&](TRuntimeNode item) {
  16. return TRuntimeNode::TList{pb.Add(item, data1), item, pb.Mul(item, data2)};
  17. });
  18. const auto graph = setup.BuildGraph(pgmReturn);
  19. const auto iterator = graph->GetValue().GetListIterator();
  20. NUdf::TUnboxedValue item;
  21. UNIT_ASSERT(iterator.Next(item));
  22. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  23. UNIT_ASSERT(iterator.Next(item));
  24. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 1);
  25. UNIT_ASSERT(iterator.Next(item));
  26. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  27. UNIT_ASSERT(iterator.Next(item));
  28. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 3);
  29. UNIT_ASSERT(iterator.Next(item));
  30. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  31. UNIT_ASSERT(iterator.Next(item));
  32. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 4);
  33. UNIT_ASSERT(iterator.Next(item));
  34. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 4);
  35. UNIT_ASSERT(iterator.Next(item));
  36. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 3);
  37. UNIT_ASSERT(iterator.Next(item));
  38. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 6);
  39. UNIT_ASSERT(!iterator.Next(item));
  40. UNIT_ASSERT(!iterator.Next(item));
  41. }
  42. Y_UNIT_TEST_LLVM(TestOverLazyList) {
  43. TSetup<LLVM> setup;
  44. TProgramBuilder& pb = *setup.PgmBuilder;
  45. const auto data1 = pb.NewDataLiteral<ui32>(1);
  46. const auto data2 = pb.NewDataLiteral<ui32>(2);
  47. const auto data3 = pb.NewDataLiteral<ui32>(3);
  48. const auto dataType = pb.NewDataType(NUdf::TDataType<ui32>::Id);
  49. const auto list = pb.NewList(dataType, {data1, data2, data3});
  50. const auto pgmReturn = pb.MultiMap(pb.LazyList(list),
  51. [&](TRuntimeNode item) {
  52. return TRuntimeNode::TList{pb.Add(item, data1), item, pb.Mul(item, data2)};
  53. });
  54. const auto graph = setup.BuildGraph(pgmReturn);
  55. const auto iterator = graph->GetValue().GetListIterator();
  56. NUdf::TUnboxedValue item;
  57. UNIT_ASSERT(iterator.Next(item));
  58. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  59. UNIT_ASSERT(iterator.Next(item));
  60. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 1);
  61. UNIT_ASSERT(iterator.Next(item));
  62. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  63. UNIT_ASSERT(iterator.Next(item));
  64. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 3);
  65. UNIT_ASSERT(iterator.Next(item));
  66. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  67. UNIT_ASSERT(iterator.Next(item));
  68. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 4);
  69. UNIT_ASSERT(iterator.Next(item));
  70. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 4);
  71. UNIT_ASSERT(iterator.Next(item));
  72. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 3);
  73. UNIT_ASSERT(iterator.Next(item));
  74. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 6);
  75. UNIT_ASSERT(!iterator.Next(item));
  76. UNIT_ASSERT(!iterator.Next(item));
  77. }
  78. Y_UNIT_TEST_LLVM(TestOverFlow) {
  79. TSetup<LLVM> setup;
  80. TProgramBuilder& pb = *setup.PgmBuilder;
  81. const auto data1 = pb.NewDataLiteral<ui32>(1);
  82. const auto data2 = pb.NewDataLiteral<ui32>(2);
  83. const auto data3 = pb.NewDataLiteral<ui32>(3);
  84. const auto dataType = pb.NewDataType(NUdf::TDataType<ui32>::Id);
  85. const auto list = pb.NewList(dataType, {data1, data2, data3});
  86. const auto pgmReturn = pb.Collect(pb.MultiMap(pb.ToFlow(list),
  87. [&](TRuntimeNode item) {
  88. return TRuntimeNode::TList{pb.Add(item, data1), item, pb.Mul(item, data2)};
  89. }));
  90. const auto graph = setup.BuildGraph(pgmReturn);
  91. const auto iterator = graph->GetValue().GetListIterator();
  92. NUdf::TUnboxedValue item;
  93. UNIT_ASSERT(iterator.Next(item));
  94. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  95. UNIT_ASSERT(iterator.Next(item));
  96. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 1);
  97. UNIT_ASSERT(iterator.Next(item));
  98. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  99. UNIT_ASSERT(iterator.Next(item));
  100. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 3);
  101. UNIT_ASSERT(iterator.Next(item));
  102. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 2);
  103. UNIT_ASSERT(iterator.Next(item));
  104. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 4);
  105. UNIT_ASSERT(iterator.Next(item));
  106. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 4);
  107. UNIT_ASSERT(iterator.Next(item));
  108. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 3);
  109. UNIT_ASSERT(iterator.Next(item));
  110. UNIT_ASSERT_VALUES_EQUAL(item.template Get<ui32>(), 6);
  111. UNIT_ASSERT(!iterator.Next(item));
  112. UNIT_ASSERT(!iterator.Next(item));
  113. }
  114. #if !defined(MKQL_RUNTIME_VERSION) || MKQL_RUNTIME_VERSION >= 18u
  115. Y_UNIT_TEST_LLVM(TestFlattenByNarrow) {
  116. TSetup<LLVM> setup;
  117. TProgramBuilder& pb = *setup.PgmBuilder;
  118. const auto dataType = pb.NewOptionalType(pb.NewDataType(NUdf::TDataType<i32>::Id));
  119. const auto tupleType = pb.NewTupleType({dataType, dataType, dataType});
  120. const auto data1 = pb.NewTuple(tupleType, {pb.NewOptional(pb.NewDataLiteral<i32>(1)), pb.NewEmptyOptional(dataType), pb.NewOptional(pb.NewDataLiteral<i32>(-1))});
  121. const auto data2 = pb.NewTuple(tupleType, {pb.NewEmptyOptional(dataType), pb.NewOptional(pb.NewDataLiteral<i32>(2)), pb.NewOptional(pb.NewDataLiteral<i32>(-2))});
  122. const auto data3 = pb.NewTuple(tupleType, {pb.NewOptional(pb.NewDataLiteral<i32>(3)), pb.NewEmptyOptional(dataType), pb.NewOptional(pb.NewDataLiteral<i32>(-3))});
  123. const auto list = pb.NewList(tupleType, {data1, data2, data3});
  124. const auto pgmReturn = pb.Collect(pb.NarrowMultiMap(pb.ExpandMap(pb.ToFlow(list),
  125. [&](TRuntimeNode item) -> TRuntimeNode::TList { return {pb.Nth(item, 0U), pb.Nth(item, 1U), pb.Nth(item, 2U)}; }),
  126. [&](TRuntimeNode::TList items) -> TRuntimeNode::TList { return {items[2U], items[1U], items[0U] }; }
  127. ));
  128. const auto graph = setup.BuildGraph(pgmReturn);
  129. const auto iterator = graph->GetValue().GetListIterator();
  130. NUdf::TUnboxedValue item;
  131. UNIT_ASSERT(iterator.Next(item));
  132. UNIT_ASSERT_VALUES_EQUAL(item.template Get<i32>(), -1);
  133. UNIT_ASSERT(iterator.Next(item));
  134. UNIT_ASSERT(!item);
  135. UNIT_ASSERT(iterator.Next(item));
  136. UNIT_ASSERT_VALUES_EQUAL(item.template Get<i32>(), 1);
  137. UNIT_ASSERT(iterator.Next(item));
  138. UNIT_ASSERT_VALUES_EQUAL(item.template Get<i32>(), -2);
  139. UNIT_ASSERT(iterator.Next(item));
  140. UNIT_ASSERT_VALUES_EQUAL(item.template Get<i32>(), 2);
  141. UNIT_ASSERT(iterator.Next(item));
  142. UNIT_ASSERT(!item);
  143. UNIT_ASSERT(iterator.Next(item));
  144. UNIT_ASSERT_VALUES_EQUAL(item.template Get<i32>(), -3);
  145. UNIT_ASSERT(iterator.Next(item));
  146. UNIT_ASSERT(!item);
  147. UNIT_ASSERT(iterator.Next(item));
  148. UNIT_ASSERT_VALUES_EQUAL(item.template Get<i32>(), 3);
  149. UNIT_ASSERT(!iterator.Next(item));
  150. UNIT_ASSERT(!iterator.Next(item));
  151. }
  152. #endif
  153. }
  154. }
  155. }