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
|
#pragma once
#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_value_builder.h>
#include <yql/essentials/utils/utf8.h>
#include <util/string/escape.h>
#include <util/string/cast.h>
#include <util/string/builder.h>
#include <functional>
namespace NYql::NDom {
template<bool Strict, bool AutoConvert>
TUnboxedValuePod ConvertToBool(TUnboxedValuePod x, const IValueBuilder* valueBuilder, const TSourcePosition& pos) {
switch (GetNodeType(x)) {
case ENodeType::Bool:
return TUnboxedValuePod(x.Get<bool>());
case ENodeType::String:
if (const std::string_view str = x.AsStringRef(); str == "true")
return TUnboxedValuePod(true);
else if (str == "false")
return TUnboxedValuePod(false);
else if constexpr (AutoConvert)
return TUnboxedValuePod(x.AsStringRef().Size() > 0U);
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Uint64:
if constexpr (AutoConvert)
return TUnboxedValuePod(x.Get<ui64>() != 0ULL);
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Int64:
if constexpr (AutoConvert)
return TUnboxedValuePod(x.Get<i64>() != 0LL);
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Double:
if constexpr (AutoConvert)
return TUnboxedValuePod(x.Get<double>() != 0.);
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Entity:
if constexpr (AutoConvert)
return TUnboxedValuePod(false);
else if constexpr (Strict)
break;
else if constexpr (AutoConvert)
return TUnboxedValuePod(false);
else
return {};
case ENodeType::List:
if constexpr (AutoConvert)
return TUnboxedValuePod(x.IsBoxed() && x.HasListItems());
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Dict:
if constexpr (AutoConvert)
return TUnboxedValuePod(x.IsBoxed() && x.HasDictItems());
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Attr:
return ConvertToBool<Strict, AutoConvert>(x.GetVariantItem().Release(), valueBuilder, pos);
}
UdfTerminate((::TStringBuilder() << valueBuilder->WithCalleePosition(pos) << " Cannot parse boolean value from " << TDebugPrinter(x)).c_str());
}
template<typename TDst, typename TSrc>
constexpr inline bool InBounds(const TSrc v) {
if constexpr (std::is_same<TSrc, TDst>())
return true;
if constexpr (sizeof(TSrc) > sizeof(TDst))
if constexpr (std::is_signed<TSrc>())
return v <= TSrc(std::numeric_limits<TDst>::max()) && v >= TSrc(std::numeric_limits<TDst>::min());
else
return v <= TSrc(std::numeric_limits<TDst>::max());
else
if constexpr (std::is_signed<TSrc>())
return v >= TSrc(std::numeric_limits<TDst>::min());
else
return v <= TSrc(std::numeric_limits<TDst>::max());
static_assert(sizeof(TSrc) >= sizeof(TDst), "Expects wide to short.");
}
template<bool Strict, bool AutoConvert, typename TargetType>
TUnboxedValuePod ConvertToIntegral(TUnboxedValuePod x, const IValueBuilder* valueBuilder, const TSourcePosition& pos) {
switch (GetNodeType(x)) {
case ENodeType::Int64: {
const auto s = x.Get<i64>();
if constexpr (AutoConvert)
return TUnboxedValuePod(TargetType(s));
else if (InBounds<TargetType>(s))
return TUnboxedValuePod(TargetType(s));
else if constexpr (Strict)
break;
else
return {};
}
case ENodeType::Uint64: {
const auto u = x.Get<ui64>();
if constexpr (AutoConvert)
return TUnboxedValuePod(TargetType(u));
else if (InBounds<TargetType>(u))
return TUnboxedValuePod(TargetType(u));
else if constexpr (Strict)
break;
else
return {};
}
case ENodeType::Bool:
if constexpr (AutoConvert)
return TUnboxedValuePod(TargetType(x.Get<bool>() ? 1 : 0));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Double:
if constexpr (AutoConvert)
return TUnboxedValuePod(TargetType(x.Get<double>()));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::String:
if constexpr (AutoConvert)
return TUnboxedValuePod(FromStringWithDefault(std::string_view(x.AsStringRef()), TargetType(0)));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Entity:
if constexpr (AutoConvert)
return TUnboxedValuePod::Zero();
else if constexpr (Strict)
break;
else
return {};
case ENodeType::List:
if constexpr (AutoConvert)
return TUnboxedValuePod::Zero();
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Dict:
if constexpr (AutoConvert)
return TUnboxedValuePod::Zero();
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Attr:
return ConvertToIntegral<Strict, AutoConvert, TargetType>(x.GetVariantItem().Release(), valueBuilder, pos);
}
UdfTerminate((::TStringBuilder() << valueBuilder->WithCalleePosition(pos) << " Cannot parse integer value from " << TDebugPrinter(x)).c_str());
static_assert(std::is_integral<TargetType>(), "Expect integral.");
}
template<bool Strict, bool AutoConvert, typename TargetType>
TUnboxedValuePod ConvertToFloat(TUnboxedValuePod x, const IValueBuilder* valueBuilder, const TSourcePosition& pos) {
switch (GetNodeType(x)) {
case ENodeType::Double:
return TUnboxedValuePod(TargetType(x.Get<double>()));
case ENodeType::Uint64:
return TUnboxedValuePod(TargetType(x.Get<ui64>()));
case ENodeType::Int64:
return TUnboxedValuePod(TargetType(x.Get<i64>()));
case ENodeType::Bool:
if constexpr (AutoConvert)
return TUnboxedValuePod(x.Get<bool>() ? TargetType(1) : TargetType(0));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::String:
if constexpr (AutoConvert)
return TUnboxedValuePod(FromStringWithDefault(std::string_view(x.AsStringRef()), TargetType(0)));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Entity:
if constexpr (AutoConvert)
return TUnboxedValuePod(TargetType(0));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::List:
if constexpr (AutoConvert)
return TUnboxedValuePod(TargetType(0));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Dict:
if constexpr (AutoConvert)
return TUnboxedValuePod(TargetType(0));
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Attr:
return ConvertToFloat<Strict, AutoConvert, TargetType>(x.GetVariantItem().Release(), valueBuilder, pos);
}
UdfTerminate((::TStringBuilder() << valueBuilder->WithCalleePosition(pos) << " Cannot parse floating point value from " << TDebugPrinter(x)).c_str());
static_assert(std::is_floating_point<TargetType>(), "Expect float.");
}
template<bool Strict, bool AutoConvert, bool Utf8>
TUnboxedValuePod ConvertToString(TUnboxedValuePod x, const IValueBuilder* valueBuilder, const TSourcePosition& pos) {
switch (GetNodeType(x)) {
case ENodeType::String:
if constexpr (Utf8)
if (IsUtf8(x.AsStringRef()))
return x;
else
if (AutoConvert)
return valueBuilder->NewString(EscapeC(TStringBuf(x.AsStringRef()))).Release();
else if constexpr (Strict)
break;
else
return {};
else
return x;
case ENodeType::Uint64:
if constexpr (AutoConvert)
return valueBuilder->NewString(ToString(x.Get<ui64>())).Release();
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Int64:
if constexpr (AutoConvert)
return valueBuilder->NewString(ToString(x.Get<i64>())).Release();
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Bool:
if constexpr (AutoConvert)
return x.Get<bool>() ? TUnboxedValuePod::Embedded("true") : TUnboxedValuePod::Embedded("false");
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Double:
if constexpr (AutoConvert)
return valueBuilder->NewString(::FloatToString(x.Get<double>())).Release();
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Entity:
case ENodeType::List:
case ENodeType::Dict:
if constexpr (AutoConvert)
return TUnboxedValuePod::Embedded("");
else if constexpr (Strict)
break;
else
return {};
case ENodeType::Attr:
return ConvertToString<Strict, AutoConvert, Utf8>(x.GetVariantItem().Release(), valueBuilder, pos);
}
UdfTerminate((::TStringBuilder() << valueBuilder->WithCalleePosition(pos) << " Cannot parse string value from " << TDebugPrinter(x)).c_str());
}
class TLazyConveter : public TManagedBoxedValue {
public:
using TConverter = std::function<TUnboxedValuePod(TUnboxedValuePod)>;
TLazyConveter(TUnboxedValue&& original, TConverter&& converter)
: Original(std::move(original)), Converter(std::move(converter))
{}
private:
template <bool NoSwap>
class TIterator: public TManagedBoxedValue {
public:
TIterator(TUnboxedValue&& original, const TConverter& converter)
: Original(std::move(original)), Converter(converter)
{}
private:
bool Skip() final {
return Original.Skip();
}
bool Next(TUnboxedValue& value) final {
if (Original.Next(value)) {
if constexpr (!NoSwap) {
value = Converter(value.Release());
}
return true;
}
return false;
}
bool NextPair(TUnboxedValue& key, TUnboxedValue& payload) final {
if (Original.NextPair(key, payload)) {
if constexpr (NoSwap) {
payload = Converter(payload.Release());
} else {
key = Converter(key.Release());
}
return true;
}
return false;
}
const TUnboxedValue Original;
const TConverter Converter;
};
ui64 GetDictLength() const final {
return Original.GetDictLength();
}
ui64 GetListLength() const final {
return Original.GetListLength();
}
bool HasFastListLength() const final {
return Original.HasFastListLength();
}
bool HasDictItems() const final {
return Original.HasDictItems();
}
bool HasListItems() const final {
return Original.HasListItems();
}
TUnboxedValue GetListIterator() const final {
return TUnboxedValuePod(new TIterator<false>(Original.GetListIterator(), Converter));
}
TUnboxedValue GetDictIterator() const final {
return TUnboxedValuePod(new TIterator<true>(Original.GetDictIterator(), Converter));
}
TUnboxedValue GetKeysIterator() const final {
return TUnboxedValuePod(new TIterator<true>(Original.GetKeysIterator(), Converter));
}
TUnboxedValue GetPayloadsIterator() const {
return TUnboxedValuePod(new TIterator<false>(Original.GetPayloadsIterator(), Converter));
}
bool Contains(const TUnboxedValuePod& key) const final {
return Original.Contains(key);
}
TUnboxedValue Lookup(const TUnboxedValuePod& key) const final {
if (auto lookup = Original.Lookup(key)) {
return Converter(lookup.Release().GetOptionalValue()).MakeOptional();
}
return {};
}
bool IsSortedDict() const final {
return Original.IsSortedDict();
}
private:
const TUnboxedValue Original;
const TConverter Converter;
};
}
|