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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
#pragma once
#include "udf_ptr.h"
#include "udf_types.h"
#include "udf_type_builder.h"
#include "udf_string.h"
#include "udf_type_size_check.h"
#include "udf_value.h"
#include <array>
struct ArrowArray;
namespace NYql {
namespace NUdf {
class IArrowType;
///////////////////////////////////////////////////////////////////////////////
// IDictValueBuilder
///////////////////////////////////////////////////////////////////////////////
struct TDictFlags {
enum EDictKind: ui32 {
Sorted = 0x01,
Hashed = 0x02,
Multi = 0x04,
};
};
class IDictValueBuilder
{
public:
using TPtr = TUniquePtr<IDictValueBuilder>;
public:
virtual ~IDictValueBuilder() = default;
virtual IDictValueBuilder& Add(TUnboxedValue&& key, TUnboxedValue&& value) = 0;
virtual TUnboxedValue Build() = 0;
};
UDF_ASSERT_TYPE_SIZE(IDictValueBuilder, 8);
class IListValueBuilder {
public:
using TPtr = TUniquePtr<IListValueBuilder>;
public:
virtual ~IListValueBuilder() = default;
// Destroys (moves out from) the element
virtual IListValueBuilder& Add(TUnboxedValue&& element) = 0;
// Destroys (moves out from) the elements
virtual IListValueBuilder& AddMany(const NUdf::TUnboxedValue* elements, size_t count) = 0;
virtual TUnboxedValue Build() = 0;
};
UDF_ASSERT_TYPE_SIZE(IListValueBuilder, 8);
///////////////////////////////////////////////////////////////////////////////
// IDateBuilder
///////////////////////////////////////////////////////////////////////////////
class IDateBuilder1
{
public:
virtual ~IDateBuilder1() = default;
virtual bool MakeDate(ui32 year, ui32 month, ui32 day, ui16& value) const = 0;
virtual bool SplitDate(ui16 value, ui32& year, ui32& month, ui32& day) const = 0;
virtual bool MakeDatetime(ui32 year, ui32 month, ui32 day, ui32 hour, ui32 minute, ui32 second, ui32& value,
ui16 timezoneId = 0) const = 0;
virtual bool SplitDatetime(ui32 value, ui32& year, ui32& month, ui32& day, ui32& hour, ui32& minute, ui32& second,
ui16 timezoneId = 0) const = 0;
// deprecated
virtual bool EnrichDate(ui16 date, ui32& dayOfYear, ui32& weekOfYear, ui32& dayOfWeek) const = 0;
// in minutes
virtual bool GetTimezoneShift(ui32 year, ui32 month, ui32 day, ui32 hour, ui32 minute, ui32 second,
ui16 timezoneId, i32& value) const = 0;
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT < UDF_ABI_COMPATIBILITY_VERSION(2, 23)
virtual void Unused7() const = 0;
virtual void Unused8() const = 0;
#else
virtual bool FullSplitDate(ui16 value, ui32& year, ui32& month, ui32& day,
ui32& dayOfYear, ui32& weekOfYear, ui32& dayOfWeek, ui16 timezoneId = 0) const = 0;
virtual bool FullSplitDatetime(ui32 value, ui32& year, ui32& month, ui32& day, ui32& hour, ui32& minute, ui32& second,
ui32& dayOfYear, ui32& weekOfYear, ui32& dayOfWeek, ui16 timezoneId = 0) const = 0;
#endif
};
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 7)
class IDateBuilder2: public IDateBuilder1
{
public:
virtual bool FindTimezoneName(ui32 id, TStringRef& name) const = 0;
virtual bool FindTimezoneId(const TStringRef& name, ui32& id) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 24)
class IDateBuilder3: public IDateBuilder2
{
public:
virtual bool EnrichDate2(ui16 date, ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek) const = 0;
virtual bool FullSplitDate2(ui16 value, ui32& year, ui32& month, ui32& day,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 timezoneId = 0) const = 0;
virtual bool FullSplitDatetime2(ui32 value, ui32& year, ui32& month, ui32& day, ui32& hour, ui32& minute, ui32& second,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 timezoneId = 0) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 40)
class IDateBuilder4: public IDateBuilder3
{
public:
virtual bool SplitTzDate32(i32 date, i32& year, ui32& month, ui32& day,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 timezoneId = 0) const = 0;
virtual bool SplitTzDatetime64(i64 datetime, i32& year, ui32& month, ui32& day,
ui32& hour, ui32& minute, ui32& second,
ui32& dayOfYear, ui32& weekOfYear, ui32& weekOfYearIso8601, ui32& dayOfWeek, ui16 timezoneId = 0) const = 0;
virtual bool MakeTzDate32(i32 year, ui32 month, ui32 day, i32& date, ui16 timezoneId = 0) const = 0;
virtual bool MakeTzDatetime64(i32 year, ui32 month, ui32 day,
ui32 hour, ui32 minute, ui32 second, i64& datetime, ui16 timezoneId = 0) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 40)
class IDateBuilder: public IDateBuilder4 {
protected:
IDateBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 24)
class IDateBuilder: public IDateBuilder3 {
protected:
IDateBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 7)
class IDateBuilder: public IDateBuilder2 {
protected:
IDateBuilder();
};
#else
class IDateBuilder: public IDateBuilder1 {
protected:
IDateBuilder();
};
#endif
UDF_ASSERT_TYPE_SIZE(IDateBuilder, 8);
///////////////////////////////////////////////////////////////////////////////
// IPgBuilder
///////////////////////////////////////////////////////////////////////////////
class IPgBuilder1 {
public:
virtual ~IPgBuilder1() = default;
// returns Null in case of text format parsing error, error message passed via 'error' arg
virtual TUnboxedValue ValueFromText(ui32 typeId, const TStringRef& value, TStringValue& error) const = 0;
// returns Null in case of wire format parsing error, error message passed via 'error' arg
virtual TUnboxedValue ValueFromBinary(ui32 typeId, const TStringRef& value, TStringValue& error) const = 0;
// targetType is required for diagnostic only in debug mode
virtual TUnboxedValue ConvertFromPg(TUnboxedValue source, ui32 sourceTypeId, const TType* targetType) const = 0;
// sourceType is required for diagnostic only in debug mode
virtual TUnboxedValue ConvertToPg(TUnboxedValue source, const TType* sourceType, ui32 targetTypeId) const = 0;
// targetTypeId is required for diagnostic only in debug mode
virtual TUnboxedValue NewString(i32 typeLen, ui32 targetTypeId, TStringRef data) const = 0;
};
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 31)
class IPgBuilder2: public IPgBuilder1
{
public:
virtual TStringRef AsCStringBuffer(const TUnboxedValue& value) const = 0;
virtual TStringRef AsTextBuffer(const TUnboxedValue& value) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 33)
class IPgBuilder3: public IPgBuilder2
{
public:
virtual TUnboxedValue MakeCString(const char* value) const = 0;
virtual TUnboxedValue MakeText(const char* value) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 35)
class IPgBuilder4: public IPgBuilder3
{
public:
virtual TStringRef AsFixedStringBuffer(const TUnboxedValue& value, ui32 length) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 35)
class IPgBuilder: public IPgBuilder4 {
protected:
IPgBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 33)
class IPgBuilder: public IPgBuilder3 {
protected:
IPgBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 31)
class IPgBuilder: public IPgBuilder2 {
protected:
IPgBuilder();
};
#else
class IPgBuilder: public IPgBuilder1 {
protected:
IPgBuilder();
};
#endif
UDF_ASSERT_TYPE_SIZE(IPgBuilder, 8);
///////////////////////////////////////////////////////////////////////////////
// IValueBuilder
///////////////////////////////////////////////////////////////////////////////
class IValueBuilder1
{
public:
virtual ~IValueBuilder1() = default;
virtual TUnboxedValue NewStringNotFilled(ui32 size) const = 0;
virtual TUnboxedValue NewString(const TStringRef& ref) const = 0;
virtual TUnboxedValue ConcatStrings(TUnboxedValuePod first, TUnboxedValuePod second) const = 0;
virtual TUnboxedValue AppendString(TUnboxedValuePod value, const TStringRef& ref) const = 0;
virtual TUnboxedValue PrependString(const TStringRef& ref, TUnboxedValuePod value) const = 0;
virtual TUnboxedValue SubString(TUnboxedValuePod value, ui32 offset, ui32 size) const = 0;
virtual IDictValueBuilder::TPtr NewDict(const TType* dictType, ui32 flags) const = 0;
// Destroys (moves out from) items
virtual TUnboxedValue NewList(TUnboxedValue* items, ui64 count) const = 0;
virtual TUnboxedValue ReverseList(const TUnboxedValuePod& list) const = 0;
virtual TUnboxedValue SkipList(const TUnboxedValuePod& list, ui64 count) const = 0;
virtual TUnboxedValue TakeList(const TUnboxedValuePod& list, ui64 count) const = 0;
virtual TUnboxedValue ToIndexDict(const TUnboxedValuePod& list) const = 0;
/// Default representation for Tuple, Struct or List with a known size.
TUnboxedValue NewArray(ui32 count, TUnboxedValue*& itemsPtr) const {
return NewArray32(count, itemsPtr);
}
virtual TUnboxedValue NewArray32(ui32 count, TUnboxedValue*& itemsPtr) const = 0;
virtual TUnboxedValue NewVariant(ui32 index, TUnboxedValue&& value) const = 0;
inline TUnboxedValue NewEmptyList() const { return NewList(nullptr, 0); }
};
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 1)
class IValueBuilder2: public IValueBuilder1 {
public:
virtual const IDateBuilder& GetDateBuilder() const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 14)
class IValueBuilder3: public IValueBuilder2 {
public:
virtual bool GetSecureParam(TStringRef key, TStringRef& value) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 17)
class IValueBuilder4: public IValueBuilder3 {
public:
virtual const TSourcePosition* CalleePosition() const = 0;
TSourcePosition WithCalleePosition(const TSourcePosition& def) const {
auto callee = CalleePosition();
return callee ? *callee : def;
}
virtual TUnboxedValue Run(const TSourcePosition& callee, const IBoxedValue& value, const TUnboxedValuePod* args) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 19)
class IValueBuilder5: public IValueBuilder4 {
public:
// exports one Array or Scalar to out. should be called for each chunk of ChunkedArray
// returns array with one element for scalars
virtual void ExportArrowBlock(TUnboxedValuePod value, ui32 chunk, ArrowArray* out) const = 0;
// imports all chunks. returns Scalar, ChunkedArray if chunkCount > 1, otherwise Array
// arrays should be a pointer to array of chunkCount structs
// the ArrowArray struct has its contents moved to a private object held alive by the result.
virtual TUnboxedValue ImportArrowBlock(ArrowArray* arrays, ui32 chunkCount, bool isScalar, const IArrowType& type) const = 0;
// always returns 1 for Scalar and Array, >= 1 for ChunkedArray
virtual ui32 GetArrowBlockChunks(TUnboxedValuePod value, bool& isScalar, ui64& length) const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 25)
class IValueBuilder6: public IValueBuilder5 {
public:
virtual const IPgBuilder& GetPgBuilder() const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 27)
class IValueBuilder7: public IValueBuilder6 {
public:
virtual TUnboxedValue NewArray64(ui64 count, TUnboxedValue*& itemsPtr) const = 0;
TUnboxedValue NewArray(ui64 count, TUnboxedValue*& itemsPtr) const {
return NewArray64(count, itemsPtr);
}
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 39)
class IValueBuilder8: public IValueBuilder7 {
public:
virtual IListValueBuilder::TPtr NewListBuilder() const = 0;
};
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 39)
class IValueBuilder: public IValueBuilder8 {
protected:
IValueBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 27)
class IValueBuilder: public IValueBuilder7 {
protected:
IValueBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 25)
class IValueBuilder: public IValueBuilder6 {
protected:
IValueBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 19)
class IValueBuilder: public IValueBuilder5 {
protected:
IValueBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 17)
class IValueBuilder: public IValueBuilder4 {
protected:
IValueBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 14)
class IValueBuilder: public IValueBuilder3 {
protected:
IValueBuilder();
};
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 1)
class IValueBuilder: public IValueBuilder2 {
protected:
IValueBuilder();
};
#else
class IValueBuilder: public IValueBuilder1 {
protected:
IValueBuilder();
};
#endif
UDF_ASSERT_TYPE_SIZE(IValueBuilder, 8);
class TPlainArrayCache {
private:
const ui32 Size;
std::array<TUnboxedValue, 2U> Cached;
std::array<TUnboxedValue*, 2U> CachedItems;
ui8 CacheIndex = 0U;
public:
TPlainArrayCache(ui32 size): Size(size) { Clear(); }
TPlainArrayCache(TPlainArrayCache&&) = delete;
TPlainArrayCache(const TPlainArrayCache&) = delete;
TPlainArrayCache& operator=(TPlainArrayCache&&) = delete;
TPlainArrayCache& operator=(const TPlainArrayCache&) = delete;
void Clear() {
Cached.fill(TUnboxedValue());
CachedItems.fill(nullptr);
}
TUnboxedValue NewArray(const IValueBuilder& builder, TUnboxedValue*& items) {
if (!CachedItems[CacheIndex] || !Cached[CacheIndex].UniqueBoxed()) {
CacheIndex ^= 1U;
if (!CachedItems[CacheIndex] || !Cached[CacheIndex].UniqueBoxed()) {
Cached[CacheIndex] = builder.NewArray(Size, CachedItems[CacheIndex]);
items = CachedItems[CacheIndex];
return Cached[CacheIndex];
}
}
items = CachedItems[CacheIndex];
std::fill_n(items, Size, TUnboxedValue());
return Cached[CacheIndex];
}
};
} // namespace NUdf
} // namespace NYql
|