mkql_computation_node_holders_ut.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  2. #include <yql/essentials/minikql/mkql_string_util.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. namespace NKikimr::NMiniKQL {
  5. Y_UNIT_TEST_SUITE(TKeyTypeContanerHelper) {
  6. struct TSetup {
  7. TSetup()
  8. : Alloc(__LOCATION__)
  9. , TypeEnv(Alloc)
  10. , TypeBuilder(TypeEnv)
  11. , MemUsage("TKeyTypeContanerHelperTest")
  12. , HolderFactory(Alloc.Ref(), MemUsage)
  13. {}
  14. TScopedAlloc Alloc;
  15. TTypeEnvironment TypeEnv;
  16. TTypeBuilder TypeBuilder;
  17. TMemoryUsageInfo MemUsage;
  18. THolderFactory HolderFactory;
  19. };
  20. struct TTestedSets {
  21. TTestedSets() = default;
  22. TTestedSets(const TType* type)
  23. : HashSetHelper(type)
  24. , HashSet(0, HashSetHelper.GetValueHash(), HashSetHelper.GetValueEqual())
  25. , CmpSetHelper(type)
  26. , CmpSet(CmpSetHelper.GetValueLess())
  27. {}
  28. void MoveFromHashToCmpSet() {
  29. const auto size = HashSet.size();
  30. while(!HashSet.empty()) {
  31. CmpSet.insert(std::move(*HashSet.begin()));
  32. HashSet.erase(HashSet.begin());
  33. }
  34. UNIT_ASSERT_EQUAL(size, CmpSet.size());
  35. }
  36. void Reset() {
  37. HashSetHelper = TKeyTypeContanerHelper<true, true, false>{};
  38. HashSet = std::unordered_set<NUdf::TUnboxedValue, TValueHasher, TValueEqual, TMKQLAllocator<NUdf::TUnboxedValue>>{
  39. 0,
  40. HashSetHelper.GetValueHash(),
  41. HashSetHelper.GetValueEqual(),
  42. };
  43. CmpSetHelper = TKeyTypeContanerHelper<false, false, true>{};
  44. CmpSet = std::set<NUdf::TUnboxedValue, TValueLess, TMKQLAllocator<NUdf::TUnboxedValue>>{CmpSetHelper.GetValueLess()};
  45. }
  46. TKeyTypeContanerHelper<true, true, false> HashSetHelper;
  47. std::unordered_set<NUdf::TUnboxedValue, TValueHasher, TValueEqual, TMKQLAllocator<NUdf::TUnboxedValue>> HashSet;
  48. TKeyTypeContanerHelper<false, false, true> CmpSetHelper;
  49. std::set<NUdf::TUnboxedValue, TValueLess, TMKQLAllocator<NUdf::TUnboxedValue>> CmpSet;
  50. };
  51. Y_UNIT_TEST(StoreInts) {
  52. TSetup setup;
  53. TTestedSets sets{setup.TypeBuilder.NewDataType(NUdf::EDataSlot::Int32)};
  54. const size_t N = 100;
  55. for (size_t i = 0; i != N; ++i) {
  56. sets.HashSet.insert(NUdf::TUnboxedValuePod{(ui32)i});
  57. }
  58. UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
  59. sets.MoveFromHashToCmpSet();
  60. for (size_t i = 0; i != N; ++i) {
  61. UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->Get<ui32>());
  62. sets.CmpSet.erase(sets.CmpSet.begin());
  63. }
  64. }
  65. Y_UNIT_TEST(StoreStrings) {
  66. TSetup setup;
  67. TTestedSets sets{setup.TypeBuilder.NewDataType(NUdf::EDataSlot::String)};
  68. const size_t N = 100; //be aware of O(n^2) complexity in data generation
  69. for (size_t i = 0; i != N; ++i) {
  70. sets.HashSet.insert(NMiniKQL::MakeString(std::string(i, 'x')));
  71. }
  72. UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
  73. sets.MoveFromHashToCmpSet();
  74. for (size_t i = 0; i != N; ++i) {
  75. UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->AsStringRef().Size());
  76. sets.CmpSet.erase(sets.CmpSet.begin());
  77. }
  78. }
  79. Y_UNIT_TEST(StoreTuples) {
  80. TSetup setup;
  81. TType* elems[] = {
  82. setup.TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32),
  83. setup.TypeBuilder.NewDataType(NUdf::EDataSlot::String)
  84. };
  85. TTestedSets sets{setup.TypeBuilder.NewTupleType(elems)};
  86. const size_t N = 100; //be aware of O(n^2) complexity in data generation
  87. for (size_t i = 0; i != N; ++i) {
  88. NUdf::TUnboxedValue* items;
  89. NUdf::TUnboxedValue v = setup.HolderFactory.CreateDirectArrayHolder(2, items);
  90. items[0] = NUdf::TUnboxedValuePod{(ui32)i};
  91. items[1] = NMiniKQL::MakeString(std::string(i, 'x'));
  92. sets.HashSet.insert(std::move(v));
  93. }
  94. UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
  95. sets.MoveFromHashToCmpSet();
  96. for (size_t i = 0; i != N; ++i) {
  97. UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->GetElement(0).Get<ui32>());
  98. NUdf::TUnboxedValue s = sets.CmpSet.begin()->GetElement(1);
  99. UNIT_ASSERT_EQUAL(i, s.AsStringRef().Size());
  100. sets.CmpSet.erase(sets.CmpSet.begin());
  101. }
  102. }
  103. Y_UNIT_TEST(StoreStructs) {
  104. TSetup setup;
  105. std::pair<std::string_view, TType*> elems[] = {
  106. std::pair<std::string_view, TType*>{"i", setup.TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32)},
  107. std::pair<std::string_view, TType*>{"s", setup.TypeBuilder.NewDataType(NUdf::EDataSlot::String)}
  108. };
  109. TTestedSets sets{setup.TypeBuilder.NewStructType(elems)};
  110. const size_t N = 100; //be aware of O(n^2) complexity in data generation
  111. for (size_t i = 0; i != N; ++i) {
  112. NUdf::TUnboxedValue* items;
  113. NUdf::TUnboxedValue v = setup.HolderFactory.CreateDirectArrayHolder(2, items);
  114. items[0] = NUdf::TUnboxedValuePod{(ui32)i};
  115. items[1] = NMiniKQL::MakeString(std::string(i, 'x'));
  116. sets.HashSet.insert(std::move(v));
  117. }
  118. UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
  119. sets.MoveFromHashToCmpSet();
  120. for (size_t i = 0; i != N; ++i) {
  121. UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->GetElement(0).Get<ui32>());
  122. NUdf::TUnboxedValue s = sets.CmpSet.begin()->GetElement(1);
  123. UNIT_ASSERT_EQUAL(i, s.AsStringRef().Size());
  124. sets.CmpSet.erase(sets.CmpSet.begin());
  125. }
  126. }
  127. Y_UNIT_TEST(ReleaseAllResources) {
  128. auto setup = std::make_shared<TSetup>();
  129. auto guard = Guard(setup->Alloc);
  130. TTestedSets intSets{setup->TypeBuilder.NewDataType(NUdf::EDataSlot::Int32)};
  131. TTestedSets stringSets{setup->TypeBuilder.NewDataType(NUdf::EDataSlot::String)};
  132. TType* tupleElems[] = {
  133. setup->TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32),
  134. setup->TypeBuilder.NewDataType(NUdf::EDataSlot::String)
  135. };
  136. TTestedSets tupleSets{setup->TypeBuilder.NewTupleType(tupleElems)};
  137. std::pair<std::string_view, TType*> structElems[] = {
  138. std::pair<std::string_view, TType*>{"i", setup->TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32)},
  139. std::pair<std::string_view, TType*>{"s", setup->TypeBuilder.NewDataType(NUdf::EDataSlot::String)}
  140. };
  141. TTestedSets structSets{setup->TypeBuilder.NewStructType(structElems)};
  142. intSets.Reset();
  143. stringSets.Reset();
  144. tupleSets.Reset();
  145. structSets.Reset();
  146. guard.Release();
  147. setup.reset();
  148. }
  149. }
  150. } //namespace namespace NKikimr::NMiniKQL