aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/computation/mkql_vector_spiller_adapter_ut.cpp
blob: 43ed19f4897a3ee867455d0d92821412b603501f (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "mkql_vector_spiller_adapter.h"
#include "mock_spiller_ut.h"

#include <library/cpp/testing/unittest/registar.h>

namespace NKikimr::NMiniKQL {

namespace {

    template <typename T>
    std::vector<T, TMKQLAllocator<T>> CreateSimpleVectorOfSize(size_t size) {
        std::vector<T, TMKQLAllocator<T>> v;
        v.reserve(size);

        for (size_t i = 0; i < size; ++i) {
            v.push_back(i);
        }

        return v;
    }

    template <typename T>
    void SaveRestoreAndCompareVectors(const std::vector<std::vector<T, TMKQLAllocator<T>>>& vectors, size_t spillerChunkSizeInBytes) {
        auto spiller = TVectorSpillerAdapter<T, TMKQLAllocator<T>>(CreateMockSpiller(), spillerChunkSizeInBytes);

        for (const auto& vec : vectors) {
            auto copiedVector = vec;
            spiller.AddData(std::move(copiedVector));

            while (!spiller.IsAcceptingData()) {
                spiller.Update();
            }
        }

        spiller.Finalize();

        while (!spiller.IsAcceptingDataRequests()) {
            spiller.Update();
        }
        
        for (const auto& vec : vectors) {
            spiller.RequestNextVector();

            while (!spiller.IsDataReady()) {
                spiller.Update();
            }

            auto extractedVector = spiller.ExtractVector();

            UNIT_ASSERT_VALUES_EQUAL(vec, extractedVector);
        }   
    }

    template <typename T>
    void RunTestForSingleVector(size_t vectorSize, size_t chunkSize, bool sizeInBytes) {
        std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
        size_t chunkSizeInBytes = sizeInBytes ? chunkSize : chunkSize * sizeof(T);
        SaveRestoreAndCompareVectors<T>({v}, chunkSizeInBytes);
    }
}


Y_UNIT_TEST_SUITE(TVectorSpillerAdapterTest_SingleVector) {
    Y_UNIT_TEST(VectorOfExactChunkSize) {
        TScopedAlloc Alloc(__LOCATION__);
        size_t vectorSize = 5;

        RunTestForSingleVector<int>(vectorSize, vectorSize, false);
        RunTestForSingleVector<char>(vectorSize, vectorSize, false);
    }

    Y_UNIT_TEST(VectorLargerThanChunkSize) {
        TScopedAlloc Alloc(__LOCATION__);
        size_t vectorSize = 10;
        size_t chunkSize = 3;

        RunTestForSingleVector<int>(vectorSize, chunkSize, false);
        RunTestForSingleVector<char>(vectorSize, chunkSize, false);
    }

    Y_UNIT_TEST(VectorLargerThanChunkSizePrime) {
        TScopedAlloc Alloc(__LOCATION__);
        size_t vectorSize = 10;
        size_t chunkSizeBytes = 7;

        RunTestForSingleVector<int>(vectorSize, chunkSizeBytes, true);
        RunTestForSingleVector<char>(vectorSize, chunkSizeBytes, true);
    }

    Y_UNIT_TEST(VectorLessThanChunkSize) {
        TScopedAlloc Alloc(__LOCATION__);
        size_t vectorSize = 5;
        size_t chunkSize = 10;

        RunTestForSingleVector<int>(vectorSize, chunkSize, false);
        RunTestForSingleVector<char>(vectorSize, chunkSize, false);
    }
}

Y_UNIT_TEST_SUITE(TVectorSpillerAdapterTest_MultipleVectors) {

    template <typename T>
    void ManyDifferentSizes_TestRunner() {
        std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
        
        for (int vectorSize = 0; vectorSize <= 100; ++vectorSize) {
            vectors.push_back(CreateSimpleVectorOfSize<T>(vectorSize));
        }

        SaveRestoreAndCompareVectors<T>(vectors, 20);
    }

    Y_UNIT_TEST(ManyDifferentSizes) {
        TScopedAlloc Alloc(__LOCATION__);
        ManyDifferentSizes_TestRunner<int>();
        ManyDifferentSizes_TestRunner<char>();
    }

    template <typename T>
    void ManyDifferentSizesReversed_TestRunner() {
        std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
        
        for (int vectorSize = 100; vectorSize >= 0; --vectorSize) {
            vectors.push_back(CreateSimpleVectorOfSize<T>(vectorSize));
        }

        SaveRestoreAndCompareVectors<T>(vectors, 20);
    }

    Y_UNIT_TEST(ManyDifferentSizesReversed) {
        TScopedAlloc Alloc(__LOCATION__);
        ManyDifferentSizesReversed_TestRunner<int>();
        ManyDifferentSizesReversed_TestRunner<char>();
    }

    template <typename T>
    void VectorsInOneChunk_TestRunner() {
        std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
        
        size_t totalSize = 0;

        for (int vectorSize = 1; vectorSize < 5; ++vectorSize) {
            std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
            totalSize += vectorSize;
            vectors.push_back(v);
        }

        SaveRestoreAndCompareVectors<T>(vectors, totalSize * sizeof(int) + 10);
    }

    Y_UNIT_TEST(VectorsInOneChunk) {
        TScopedAlloc Alloc(__LOCATION__);
        VectorsInOneChunk_TestRunner<int>();
        VectorsInOneChunk_TestRunner<char>();
    }

    template <typename T>
    void EmptyVectorsInTheMiddle_TestRunner() {
        std::vector<std::vector<T,TMKQLAllocator<T>>> vectors;
        
        size_t totalSize = 0;

        for (int vectorSize = 1; vectorSize < 5; ++vectorSize) {
            std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
            totalSize += vectorSize;
            vectors.push_back(v);
        }
        vectors.push_back({});
        vectors.push_back({});

        for (int vectorSize = 1; vectorSize < 5; ++vectorSize) {
            std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
            totalSize += vectorSize;
            vectors.push_back(v);
        }

        SaveRestoreAndCompareVectors<T>(vectors, totalSize * sizeof(T) + 10);
    }

    Y_UNIT_TEST(EmptyVectorsInTheMiddle) {
        TScopedAlloc Alloc(__LOCATION__);
        EmptyVectorsInTheMiddle_TestRunner<int>();
        EmptyVectorsInTheMiddle_TestRunner<char>();
    }

    template <typename T>
    void RequestedVectorPartlyInMemory_TestRunner() {
        std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
        std::vector<T, TMKQLAllocator<T>> small = CreateSimpleVectorOfSize<T>(1);
        std::vector<T, TMKQLAllocator<T>> big = CreateSimpleVectorOfSize<T>(10);

        vectors.push_back(small);
        vectors.push_back(big);

        // small vector will also load most of big vector to memory
        size_t chunkSizeBytes = (big.size() - small.size()) * sizeof(T);

        SaveRestoreAndCompareVectors<T>(vectors, chunkSizeBytes);
    }

    Y_UNIT_TEST(RequestedVectorPartlyInMemory) {
        TScopedAlloc Alloc(__LOCATION__);
        RequestedVectorPartlyInMemory_TestRunner<int>();
        RequestedVectorPartlyInMemory_TestRunner<char>();
    }

}

} //namespace namespace NKikimr::NMiniKQL