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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
#include "mkql_computation_node_ut.h"
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_string_util.h>
namespace NKikimr {
namespace NMiniKQL {
Y_UNIT_TEST_SUITE(TMiniKQLDictRelatedNodesTest) {
Y_UNIT_TEST_LLVM(TestDictLength) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto key1 = pgmBuilder.NewDataLiteral<ui32>(1);
const auto key2 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto key3 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto payload1 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("A");
const auto payload2 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("B");
const auto payload3 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("C");
TVector<std::pair<TRuntimeNode, TRuntimeNode>> dictItems;
dictItems.push_back(std::make_pair(key1, payload1));
dictItems.push_back(std::make_pair(key2, payload2));
dictItems.push_back(std::make_pair(key3, payload3));
const auto dictType = pgmBuilder.NewDictType(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id),
pgmBuilder.NewDataType(NUdf::TDataType<char*>::Id), false);
const auto dict = pgmBuilder.NewDict(dictType, dictItems);
const auto pgmReturn = pgmBuilder.Length(dict);
const auto graph = setup.BuildGraph(pgmReturn);
UNIT_ASSERT_VALUES_EQUAL(graph->GetValue().template Get<ui64>(), 2);
}
Y_UNIT_TEST_LLVM(TestDictContains) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto key1 = pgmBuilder.NewDataLiteral<ui32>(1);
const auto key2 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto key3 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto missingKey = pgmBuilder.NewDataLiteral<ui32>(3);
const auto payload1 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("A");
const auto payload2 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("B");
const auto payload3 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("C");
TVector<std::pair<TRuntimeNode, TRuntimeNode>> dictItems;
dictItems.push_back(std::make_pair(key1, payload1));
dictItems.push_back(std::make_pair(key2, payload2));
dictItems.push_back(std::make_pair(key3, payload3));
const auto dictType = pgmBuilder.NewDictType(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id),
pgmBuilder.NewDataType(NUdf::TDataType<char*>::Id), false);
const auto dict = pgmBuilder.NewDict(dictType, dictItems);
const auto keys = pgmBuilder.NewList(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id), {key1, key2, missingKey});
const auto pgmReturn = pgmBuilder.Map(keys,
[&](TRuntimeNode key) {
return pgmBuilder.Contains(dict, key);
});
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.template Get<bool>(), true);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.template Get<bool>(), true);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.template Get<bool>(), false);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
Y_UNIT_TEST_LLVM(TestDictLookup) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto key1 = pgmBuilder.NewDataLiteral<ui32>(1);
const auto key2 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto key3 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto missingKey = pgmBuilder.NewDataLiteral<ui32>(3);
const auto payload1 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("A");
const auto payload2 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("B");
const auto payload3 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("C");
TVector<std::pair<TRuntimeNode, TRuntimeNode>> dictItems;
dictItems.push_back(std::make_pair(key1, payload1));
dictItems.push_back(std::make_pair(key2, payload2));
dictItems.push_back(std::make_pair(key3, payload3));
const auto dictType = pgmBuilder.NewDictType(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id),
pgmBuilder.NewDataType(NUdf::TDataType<char*>::Id), false);
const auto dict = pgmBuilder.NewDict(dictType, dictItems);
const auto keys = pgmBuilder.NewList(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id), {key1, key2, missingKey});
const auto pgmReturn = pgmBuilder.Map(keys,
[&](TRuntimeNode key) {
return pgmBuilder.Lookup(dict, key);
});
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(item);
UNBOXED_VALUE_STR_EQUAL(item, "A");
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(item);
UNBOXED_VALUE_STR_EQUAL(item, "B");
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(!item);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
template<bool Multi>
TRuntimeNode PrepareTestDict(TProgramBuilder& pgmBuilder, TRuntimeNode(TProgramBuilder::* factory)(TRuntimeNode list, bool multi,
const TProgramBuilder::TUnaryLambda& keySelector,
const TProgramBuilder::TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint)) {
const auto key1 = pgmBuilder.NewDataLiteral<ui32>(1);
const auto key2 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto key3 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto key4 = pgmBuilder.NewDataLiteral<ui32>(5);
const auto key5 = pgmBuilder.NewDataLiteral<ui32>(7);
const auto payload1 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("A");
const auto payload2 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("B");
const auto payload3 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("C");
const auto payload4 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("D");
const auto payload5 = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>("E");
auto structType = pgmBuilder.NewStructType(pgmBuilder.NewEmptyStructType(), "Key", pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id));
structType = pgmBuilder.NewStructType(structType, "Payload", pgmBuilder.NewDataType(NUdf::TDataType<char*>::Id));
const auto list = pgmBuilder.NewList(structType, {
pgmBuilder.AddMember(pgmBuilder.AddMember(pgmBuilder.NewEmptyStruct(), "Key", key3), "Payload", payload3),
pgmBuilder.AddMember(pgmBuilder.AddMember(pgmBuilder.NewEmptyStruct(), "Key", key1), "Payload", payload1),
pgmBuilder.AddMember(pgmBuilder.AddMember(pgmBuilder.NewEmptyStruct(), "Key", key5), "Payload", payload5),
pgmBuilder.AddMember(pgmBuilder.AddMember(pgmBuilder.NewEmptyStruct(), "Key", key4), "Payload", payload4),
pgmBuilder.AddMember(pgmBuilder.AddMember(pgmBuilder.NewEmptyStruct(), "Key", key2), "Payload", payload2)
});
const auto dict = (pgmBuilder.*factory)(list, Multi,
[&](TRuntimeNode item) {
return pgmBuilder.Member(item, "Key");
},
[&](TRuntimeNode item) {
return pgmBuilder.Member(item, "Payload");
}, false, 0);
return dict;
}
template<bool LLVM>
void TestConvertedDictContains(TRuntimeNode(TProgramBuilder::* factory)(TRuntimeNode list, bool multi,
const TProgramBuilder::TUnaryLambda& keySelector,
const TProgramBuilder::TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint)) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto dict = PrepareTestDict<false>(pgmBuilder, factory);
const auto key1 = pgmBuilder.NewDataLiteral<ui32>(1);
const auto key2 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto missingKey = pgmBuilder.NewDataLiteral<ui32>(42);
const auto keys = pgmBuilder.NewList(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id), {key1, key2, missingKey});
const auto pgmReturn = pgmBuilder.Map(keys,
[&](TRuntimeNode key) {
return pgmBuilder.Contains(dict, key);
});
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.template Get<bool>(), true);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.template Get<bool>(), true);
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT_VALUES_EQUAL(item.template Get<bool>(), false);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
template<bool LLVM>
void TestConvertedDictLookup(TRuntimeNode(TProgramBuilder::* factory)(TRuntimeNode list, bool multi,
const TProgramBuilder::TUnaryLambda& keySelector,
const TProgramBuilder::TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint)) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto dict = PrepareTestDict<false>(pgmBuilder, factory);
const auto key1 = pgmBuilder.NewDataLiteral<ui32>(1);
const auto key2 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto missingKey = pgmBuilder.NewDataLiteral<ui32>(18);
const auto keys = pgmBuilder.NewList(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id), {key1, key2, missingKey});
const auto pgmReturn = pgmBuilder.Map(keys,
[&](TRuntimeNode key) {
return pgmBuilder.Lookup(dict, key);
});
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(item);
UNBOXED_VALUE_STR_EQUAL(item, "A");
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(item);
UNBOXED_VALUE_STR_EQUAL(item, "C");
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(!item);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
Y_UNIT_TEST_LLVM(TestSortedDictContains) {
TestConvertedDictContains<LLVM>(&TProgramBuilder::ToSortedDict);
}
Y_UNIT_TEST_LLVM(TestSortedDictLookup) {
TestConvertedDictLookup<LLVM>(&TProgramBuilder::ToSortedDict);
}
Y_UNIT_TEST_LLVM(TestHashedDictContains) {
TestConvertedDictContains<LLVM>(&TProgramBuilder::ToHashedDict);
}
Y_UNIT_TEST_LLVM(TestHashedDictLookup) {
TestConvertedDictLookup<LLVM>(&TProgramBuilder::ToHashedDict);
}
template<bool LLVM, bool SortBeforeCompare>
void TestDictItemsImpl(TRuntimeNode(TProgramBuilder::* factory)(TRuntimeNode list, bool multi,
const TProgramBuilder::TUnaryLambda& keySelector,
const TProgramBuilder::TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint)) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto dict = PrepareTestDict<false>(pgmBuilder, factory);
const auto pgmReturn = pgmBuilder.DictItems(dict);
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
std::vector<std::pair<ui32, TString>> items;
for (NUdf::TUnboxedValue item; iterator.Next(item);) {
const auto& pay = item.GetElement(1);
items.emplace_back(item.GetElement(0).template Get<ui32>(), pay.AsStringRef());
}
if (SortBeforeCompare) {
std::sort(items.begin(), items.end(), [](const std::pair<ui32, TString>& left, const std::pair<ui32, TString>& right) {
return left.first < right.first;
});
}
UNIT_ASSERT_VALUES_EQUAL(items.size(), 4U);
UNIT_ASSERT_VALUES_EQUAL(items[0].first, 1);
UNIT_ASSERT_VALUES_EQUAL(items[0].second, "A");
UNIT_ASSERT_VALUES_EQUAL(items[1].first, 2);
UNIT_ASSERT_VALUES_EQUAL(items[1].second, "C");
UNIT_ASSERT_VALUES_EQUAL(items[2].first, 5);
UNIT_ASSERT_VALUES_EQUAL(items[2].second, "D");
UNIT_ASSERT_VALUES_EQUAL(items[3].first, 7);
UNIT_ASSERT_VALUES_EQUAL(items[3].second, "E");
}
template<bool LLVM, bool SortBeforeCompare>
void TestDictKeysImpl(TRuntimeNode(TProgramBuilder::* factory)(TRuntimeNode list, bool multi,
const TProgramBuilder::TUnaryLambda& keySelector,
const TProgramBuilder::TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint)) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto dict = PrepareTestDict<false>(pgmBuilder, factory);
const auto pgmReturn = pgmBuilder.DictKeys(dict);
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
std::vector<ui32> items;
for (NUdf::TUnboxedValue item; iterator.Next(item);) {
items.emplace_back(item.template Get<ui32>());
}
if (SortBeforeCompare) {
std::sort(items.begin(), items.end());
}
UNIT_ASSERT_VALUES_EQUAL(items.size(), 4U);
UNIT_ASSERT_VALUES_EQUAL(items[0], 1);
UNIT_ASSERT_VALUES_EQUAL(items[1], 2);
UNIT_ASSERT_VALUES_EQUAL(items[2], 5);
UNIT_ASSERT_VALUES_EQUAL(items[3], 7);
}
template<bool LLVM, bool SortBeforeCompare>
void TestDictPayloadsImpl(TRuntimeNode(TProgramBuilder::* factory)(TRuntimeNode list, bool multi,
const TProgramBuilder::TUnaryLambda& keySelector,
const TProgramBuilder::TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint)) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto dict = PrepareTestDict<false>(pgmBuilder, factory);
const auto pgmReturn = pgmBuilder.DictPayloads(dict);
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
std::vector<TString> items;
for (NUdf::TUnboxedValue item; iterator.Next(item);) {
items.emplace_back(item.AsStringRef());
}
if (SortBeforeCompare) {
std::sort(items.begin(), items.end());
}
UNIT_ASSERT_VALUES_EQUAL(items.size(), 4U);
UNIT_ASSERT_VALUES_EQUAL(items[0], "A");
UNIT_ASSERT_VALUES_EQUAL(items[1], "C");
UNIT_ASSERT_VALUES_EQUAL(items[2], "D");
UNIT_ASSERT_VALUES_EQUAL(items[3], "E");
}
Y_UNIT_TEST_LLVM(TestSortedDictItems) {
TestDictItemsImpl<LLVM, false>(&TProgramBuilder::ToSortedDict);
}
Y_UNIT_TEST_LLVM(TestHashedDictItems) {
TestDictItemsImpl<LLVM, true>(&TProgramBuilder::ToHashedDict);
}
Y_UNIT_TEST_LLVM(TestSortedDictKeys) {
TestDictKeysImpl<LLVM, false>(&TProgramBuilder::ToSortedDict);
}
Y_UNIT_TEST_LLVM(TestHashedDictKeys) {
TestDictKeysImpl<LLVM, true>(&TProgramBuilder::ToHashedDict);
}
Y_UNIT_TEST_LLVM(TestSortedPayloadsKeys) {
TestDictPayloadsImpl<LLVM, false>(&TProgramBuilder::ToSortedDict);
}
Y_UNIT_TEST_LLVM(TestHashedPayloadsKeys) {
TestDictPayloadsImpl<LLVM, true>(&TProgramBuilder::ToHashedDict);
}
template<bool LLVM>
void TestConvertedMultiDictLookup(TRuntimeNode(TProgramBuilder::* factory)(TRuntimeNode list, bool multi,
const TProgramBuilder::TUnaryLambda& keySelector,
const TProgramBuilder::TUnaryLambda& payloadSelector, bool isCompact, ui64 itemsCountHint)) {
TSetup<LLVM> setup;
TProgramBuilder& pgmBuilder = *setup.PgmBuilder;
const auto dict = PrepareTestDict<true>(pgmBuilder, factory);
const auto key1 = pgmBuilder.NewDataLiteral<ui32>(1);
const auto key2 = pgmBuilder.NewDataLiteral<ui32>(2);
const auto missingKey = pgmBuilder.NewDataLiteral<ui32>(3);
const auto keys = pgmBuilder.NewList(pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id), {key1, key2, missingKey});
const auto pgmReturn = pgmBuilder.Map(keys,
[&](TRuntimeNode key) {
return pgmBuilder.Lookup(dict, key);
});
const auto graph = setup.BuildGraph(pgmReturn);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue item, item2;
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(item);
auto iter2 = item.GetListIterator();
UNIT_ASSERT(iter2.Next(item2));
UNBOXED_VALUE_STR_EQUAL(item2, "A");
UNIT_ASSERT(!iter2.Next(item2));
UNIT_ASSERT(!iter2.Next(item2));
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(item);
iter2 = item.GetListIterator();
UNIT_ASSERT(iter2.Next(item2));
UNBOXED_VALUE_STR_EQUAL(item2, "C");
UNIT_ASSERT(iter2.Next(item2));
UNBOXED_VALUE_STR_EQUAL(item2, "B");
UNIT_ASSERT(!iter2.Next(item2));
UNIT_ASSERT(!iter2.Next(item2));
UNIT_ASSERT(iterator.Next(item));
UNIT_ASSERT(!item);
UNIT_ASSERT(!iterator.Next(item));
UNIT_ASSERT(!iterator.Next(item));
}
Y_UNIT_TEST_LLVM(TestSortedMultiDictLookup) {
TestConvertedMultiDictLookup<LLVM>(&TProgramBuilder::ToSortedDict);
}
Y_UNIT_TEST_LLVM(TestHashedMultiDictLookup) {
TestConvertedMultiDictLookup<LLVM>(&TProgramBuilder::ToHashedDict);
}
}
}
}
|