aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/computation/mkql_computation_node_holders_ut.cpp
blob: d6bfab59dbf2c8b1213dc92634376d0b5d2f87b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/mkql_string_util.h>
#include <library/cpp/testing/unittest/registar.h>

namespace NKikimr::NMiniKQL {

Y_UNIT_TEST_SUITE(TKeyTypeContanerHelper) {
    struct TSetup {
        TSetup()
            : Alloc(__LOCATION__)
            , TypeEnv(Alloc)
            , TypeBuilder(TypeEnv)
            , MemUsage("TKeyTypeContanerHelperTest")
            , HolderFactory(Alloc.Ref(), MemUsage)
        {}
        TScopedAlloc Alloc;
        TTypeEnvironment TypeEnv;
        TTypeBuilder TypeBuilder;
        TMemoryUsageInfo MemUsage;
        THolderFactory HolderFactory;
    };

    struct TTestedSets {
        TTestedSets() = default;
        TTestedSets(const TType* type)
            : HashSetHelper(type)
            , HashSet(0, HashSetHelper.GetValueHash(), HashSetHelper.GetValueEqual())
            , CmpSetHelper(type)
            , CmpSet(CmpSetHelper.GetValueLess())
        {}

        void MoveFromHashToCmpSet() {
            const auto size = HashSet.size();
            while(!HashSet.empty()) {
                CmpSet.insert(std::move(*HashSet.begin()));
                HashSet.erase(HashSet.begin());
            }
            UNIT_ASSERT_EQUAL(size, CmpSet.size());
        }

        void Reset() {
            HashSetHelper = TKeyTypeContanerHelper<true, true, false>{};
            HashSet = std::unordered_set<NUdf::TUnboxedValue, TValueHasher, TValueEqual, TMKQLAllocator<NUdf::TUnboxedValue>>{
                0,
                HashSetHelper.GetValueHash(),
                HashSetHelper.GetValueEqual(),
            };
            CmpSetHelper = TKeyTypeContanerHelper<false, false, true>{};
            CmpSet = std::set<NUdf::TUnboxedValue, TValueLess, TMKQLAllocator<NUdf::TUnboxedValue>>{CmpSetHelper.GetValueLess()};
        }

        TKeyTypeContanerHelper<true, true, false> HashSetHelper;
        std::unordered_set<NUdf::TUnboxedValue, TValueHasher, TValueEqual, TMKQLAllocator<NUdf::TUnboxedValue>> HashSet;
        TKeyTypeContanerHelper<false, false, true> CmpSetHelper;
        std::set<NUdf::TUnboxedValue, TValueLess, TMKQLAllocator<NUdf::TUnboxedValue>> CmpSet;
    };

    Y_UNIT_TEST(StoreInts) {
        TSetup setup;
        TTestedSets sets{setup.TypeBuilder.NewDataType(NUdf::EDataSlot::Int32)};
        const size_t N = 100;
        for (size_t i = 0; i != N; ++i) {
            sets.HashSet.insert(NUdf::TUnboxedValuePod{(ui32)i});
        }
        UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
        sets.MoveFromHashToCmpSet();
        for (size_t i = 0; i != N; ++i) {
            UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->Get<ui32>());
            sets.CmpSet.erase(sets.CmpSet.begin());
        }
    }

    Y_UNIT_TEST(StoreStrings) {
        TSetup setup;
        TTestedSets sets{setup.TypeBuilder.NewDataType(NUdf::EDataSlot::String)};
        const size_t N = 100; //be aware of O(n^2) complexity in data generation
        for (size_t i = 0; i != N; ++i) {
            sets.HashSet.insert(NMiniKQL::MakeString(std::string(i, 'x')));
        }
        UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
        sets.MoveFromHashToCmpSet();
        for (size_t i = 0; i != N; ++i) {
            UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->AsStringRef().Size());
            sets.CmpSet.erase(sets.CmpSet.begin());
        }
    }

    Y_UNIT_TEST(StoreTuples) {
        TSetup setup;
        TType* elems[] = {
            setup.TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32),
            setup.TypeBuilder.NewDataType(NUdf::EDataSlot::String)
        };
        TTestedSets sets{setup.TypeBuilder.NewTupleType(elems)};
        const size_t N = 100; //be aware of O(n^2) complexity in data generation
        for (size_t i = 0; i != N; ++i) {
            NUdf::TUnboxedValue* items;
            NUdf::TUnboxedValue v = setup.HolderFactory.CreateDirectArrayHolder(2, items);
            items[0] = NUdf::TUnboxedValuePod{(ui32)i};
            items[1] = NMiniKQL::MakeString(std::string(i, 'x'));
            sets.HashSet.insert(std::move(v));
        }
        UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
        sets.MoveFromHashToCmpSet();
        for (size_t i = 0; i != N; ++i) {
            UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->GetElement(0).Get<ui32>());
            NUdf::TUnboxedValue s = sets.CmpSet.begin()->GetElement(1);
            UNIT_ASSERT_EQUAL(i, s.AsStringRef().Size());
            sets.CmpSet.erase(sets.CmpSet.begin());
        }
    }

    Y_UNIT_TEST(StoreStructs) {
        TSetup setup;
        std::pair<std::string_view, TType*> elems[] = {
            std::pair<std::string_view, TType*>{"i", setup.TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32)},
            std::pair<std::string_view, TType*>{"s", setup.TypeBuilder.NewDataType(NUdf::EDataSlot::String)}
        };
        TTestedSets sets{setup.TypeBuilder.NewStructType(elems)};
        const size_t N = 100; //be aware of O(n^2) complexity in data generation
        for (size_t i = 0; i != N; ++i) {
            NUdf::TUnboxedValue* items;
            NUdf::TUnboxedValue v = setup.HolderFactory.CreateDirectArrayHolder(2, items);
            items[0] = NUdf::TUnboxedValuePod{(ui32)i};
            items[1] = NMiniKQL::MakeString(std::string(i, 'x'));
            sets.HashSet.insert(std::move(v));
        }
        UNIT_ASSERT_EQUAL(N, sets.HashSet.size());
        sets.MoveFromHashToCmpSet();
        for (size_t i = 0; i != N; ++i) {
            UNIT_ASSERT_EQUAL(i, sets.CmpSet.begin()->GetElement(0).Get<ui32>());
            NUdf::TUnboxedValue s = sets.CmpSet.begin()->GetElement(1);
            UNIT_ASSERT_EQUAL(i, s.AsStringRef().Size());
            sets.CmpSet.erase(sets.CmpSet.begin());
        }
    }

    Y_UNIT_TEST(ReleaseAllResources) {
        auto setup = std::make_shared<TSetup>();
        auto guard = Guard(setup->Alloc);

        TTestedSets intSets{setup->TypeBuilder.NewDataType(NUdf::EDataSlot::Int32)};

        TTestedSets stringSets{setup->TypeBuilder.NewDataType(NUdf::EDataSlot::String)};

        TType* tupleElems[] = {
            setup->TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32),
            setup->TypeBuilder.NewDataType(NUdf::EDataSlot::String)
        };
        TTestedSets tupleSets{setup->TypeBuilder.NewTupleType(tupleElems)};

        std::pair<std::string_view, TType*> structElems[] = {
            std::pair<std::string_view, TType*>{"i", setup->TypeBuilder.NewDataType(NUdf::EDataSlot::Uint32)},
            std::pair<std::string_view, TType*>{"s", setup->TypeBuilder.NewDataType(NUdf::EDataSlot::String)}
        };
        TTestedSets structSets{setup->TypeBuilder.NewStructType(structElems)};

        intSets.Reset();
        stringSets.Reset();
        tupleSets.Reset();
        structSets.Reset();

        guard.Release();
        setup.reset();
    }
}

} //namespace namespace NKikimr::NMiniKQL