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
|
#pragma once
#include <ydb/library/mkql_proto/protos/minikql.pb.h>
#include <ydb/core/scheme/scheme_type_id.h>
#include <google/protobuf/repeated_field.h>
#include <util/generic/hash.h>
#include <util/generic/yexception.h>
namespace NKikimr {
namespace NResultLib {
using TProtoValue = NKikimrMiniKQL::TValue;
using TProtoType = NKikimrMiniKQL::TType;
using EProtoTypeKind = NKikimrMiniKQL::ETypeKind;
class TOptional;
class TListType;
class TTuple;
class TStruct;
class TDict;
namespace NPrivate {
template <typename T>
inline bool HasData(const TProtoValue& value, NScheme::TTypeId schemeType) {
Y_UNUSED(value);
Y_UNUSED(schemeType);
Y_FAIL("Not scpecified type.");
}
template <typename T>
inline T GetData(const TProtoValue& value, NScheme::TTypeId schemeType) {
Y_UNUSED(value);
Y_UNUSED(schemeType);
Y_FAIL("Not scpecified type.");
}
#include "data_funcs.inl"
} // namespace NPrivate
#define ENSURE_KIND(type, expectedKind) \
do { \
auto kind = (EProtoTypeKind) type.GetKind(); \
Y_ENSURE(kind == EProtoTypeKind::expectedKind, \
"Expected " #expectedKind " instead of " << NKikimrMiniKQL::ETypeKind_Name(kind)); \
Y_ENSURE(type.Has ## expectedKind(), "No " #expectedKind " in type, seems like an error."); \
} while (false);
class TOptional {
public:
explicit TOptional(const TProtoValue& value, const TProtoType& type)
: RootValue(value)
, RootType(type)
{
ENSURE_KIND(RootType, Optional);
}
template <typename T>
T GetItem() const;
bool HasItem() const {
return RootValue.HasOptional();
}
private:
const TProtoValue& RootValue;
const TProtoType& RootType;
};
class TListType {
public:
template <typename T>
class iterator {
public:
iterator(google::protobuf::RepeatedPtrField<TProtoValue>::const_iterator item, const TProtoType& itemType);
iterator(const iterator& it);
iterator& operator++();
iterator operator++(int);
bool operator!=(const iterator& it) const;
bool operator==(const iterator& it) const;
T operator*() const;
T Get() const;
private:
google::protobuf::RepeatedPtrField<TProtoValue>::const_iterator Item;
const TProtoType& ItemType;
};
template <typename T>
class TIterableList {
public:
TIterableList(const TListType& l)
: List(l)
{
}
iterator<T> begin() {
return iterator<T>(List.RootValue.GetList().begin(), List.RootType.GetList().GetItem());
}
iterator<T> end() {
return iterator<T>(List.RootValue.GetList().end(), List.RootType.GetList().GetItem());
}
private:
const TListType& List;
};
explicit TListType(const TProtoValue& value, const TProtoType& type)
: RootValue(value)
, RootType(type)
, Size(RootValue.ListSize())
{
ENSURE_KIND(RootType, List);
}
template <typename T>
TIterableList<T> MakeIterable() {
return TIterableList<T>(*this);
}
template <typename T>
T GetItem(size_t index) {
Y_ENSURE(CheckIndex(index), "List item index" << index << " is out of bounds.");
iterator<T> it(RootValue.GetList().begin() + index, RootType.GetList().GetItem());
return it.Get();
}
size_t GetSize() const {
return Size;
}
private:
bool CheckIndex(size_t index) {
return index < Size;
}
private:
const TProtoValue& RootValue;
const TProtoType& RootType;
const size_t Size;
};
class TTuple {
public:
explicit TTuple(const TProtoValue& value, const TProtoType& type)
: RootValue(value)
, RootType(type)
, Size(RootValue.TupleSize())
{
ENSURE_KIND(RootType, Tuple);
Y_ENSURE(RootType.GetTuple().ElementSize() == RootValue.TupleSize(), "Size mismatch.");
}
template <typename T>
T GetElement(size_t index) const;
size_t GetSize() const {
return Size;
}
private:
bool CheckIndex(size_t index) const {
return index < Size;
}
private:
const TProtoValue& RootValue;
const TProtoType& RootType;
const size_t Size;
};
class TStruct {
public:
explicit TStruct(const TProtoValue& value, const TProtoType& type)
: RootValue(value)
, RootType(type)
, Size(RootValue.StructSize())
{
ENSURE_KIND(RootType, Struct);
Y_ENSURE(RootType.GetStruct().MemberSize() == RootValue.StructSize(), "Size mismatch.");
}
template <typename T>
T GetMember(const TStringBuf& memberName);
template <typename T>
T GetMember(size_t memberIndex);
size_t GetMemberIndex(const TStringBuf& memberName) const {
for (size_t i = 0, end = RootType.GetStruct().MemberSize(); i < end; ++i) {
const TStringBuf& name = RootType.GetStruct().GetMember(i).GetName();
if (name == memberName) {
return i;
}
}
ythrow yexception() << "Unknown Struct member name: " << memberName << ".";
}
size_t GetSize() const {
return Size;
}
private:
bool CheckIndex(size_t index) const {
return index < Size;
}
private:
const TProtoValue& RootValue;
const TProtoType& RootType;
const size_t Size;
};
class TDict {
public:
explicit TDict(const TProtoValue& value, const TProtoType& type)
: RootValue(value)
, RootType(type)
{
ENSURE_KIND(RootType, Dict);
ENSURE_KIND(RootType.GetDict().GetKey(), Data);
}
template <typename K, typename V>
THashMap<K, V> GetHashMap() const;
private:
const TProtoValue& RootValue;
const TProtoType& RootType;
};
// TOptional.
template <typename T>
T TOptional::GetItem() const {
Y_ENSURE(HasItem(), "Optional is empty!");
const auto& itemType = RootType.GetOptional().GetItem();
ENSURE_KIND(itemType, Data);
auto schemeType = itemType.GetData().GetScheme();
return NPrivate::GetData<T>(RootValue.GetOptional(), schemeType);
}
#include "optional_funcs.inl"
// TListType.
#include "list_funcs.inl"
// TTuple.
template <typename T>
T TTuple::GetElement(size_t index) const {
Y_ENSURE(CheckIndex(index), "Tuple element index" << index << " is out of bounds.");
const auto& elementType = RootType.GetTuple().GetElement(index);
const auto& element = RootValue.GetTuple(index);
ENSURE_KIND(elementType, Data);
auto schemeType = elementType.GetData().GetScheme();
return NPrivate::GetData<T>(element, schemeType);
}
#include "tuple_funcs.inl"
// TStruct.
template <typename T>
T TStruct::GetMember(const TStringBuf& memberName) {
size_t memberIndex = GetMemberIndex(memberName);
return GetMember<T>(memberIndex);
}
template <typename T>
T TStruct::GetMember(size_t memberIndex) {
Y_ENSURE(CheckIndex(memberIndex), "Struct member index" << memberIndex << " is out of bounds.");
const auto& memberType = RootType.GetStruct().GetMember(memberIndex).GetType();
const auto& member = RootValue.GetStruct(memberIndex);
ENSURE_KIND(memberType, Data);
auto schemeType = memberType.GetData().GetScheme();
return NPrivate::GetData<T>(member, schemeType);
}
#include "struct_funcs.inl"
// TDict.
template <typename K, typename V>
THashMap<K, V> TDict::GetHashMap() const {
THashMap<K, V> m;
ui32 keySchemeType = RootType.GetDict().GetKey().GetData().GetScheme();
for (const auto& kvPair : RootValue.GetDict()) {
auto dictKey = NPrivate::GetData<K>(kvPair.GetKey(), keySchemeType);
const auto& dictValue = kvPair.GetPayload();
const auto& dictValueType = RootType.GetDict().GetPayload();
m[dictKey] = V(dictValue, dictValueType);
}
return m;
}
#undef ENSURE_KIND
} // namespace NResultLib
} // namespace NKikimr
|