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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
#pragma once
#include "udf_value.h"
#include "udf_registrator.h"
#include "udf_value_builder.h"
#include "udf_terminator.h"
#include "udf_type_builder.h"
#include "udf_type_inspection.h"
#include "udf_version.h"
#include "udf_type_printer.h"
#include <util/generic/yexception.h>
#include <util/generic/string.h>
#include <util/generic/strbuf.h>
#include <util/string/builder.h>
namespace NYql {
namespace NUdf {
inline TSourcePosition GetSourcePosition(IFunctionTypeInfoBuilder& builder) {
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 9)
return builder.GetSourcePosition();
#else
Y_UNUSED(builder);
return {};
#endif
}
TString LoadResourceOnce(TStringBuf resourceId);
inline void SetIRImplementation(IFunctionTypeInfoBuilder& builder, TStringBuf resourceId, TStringBuf functionName) {
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 16)
if (functionName) {
builder.IRImplementation(LoadResourceOnce(resourceId), resourceId, functionName);
}
#else
Y_UNUSED(builder);
Y_UNUSED(resourceId);
Y_UNUSED(functionName);
#endif
}
}
}
#define UDF_IMPL_EX(udfName, typeBody, members, init, irResourceId, irFunctionName, blockType, create_impl) \
class udfName: public ::NYql::NUdf::TBoxedValue { \
public: \
using TBlockType = blockType; \
explicit udfName(::NYql::NUdf::IFunctionTypeInfoBuilder& builder) \
: Pos_(GetSourcePosition(builder)) \
{ \
init \
} \
static const ::NYql::NUdf::TStringRef& Name() { \
static auto name = ::NYql::NUdf::TStringRef::Of(#udfName).Substring(1, 256); \
return name; \
} \
inline ::NYql::NUdf::TUnboxedValue RunImpl( \
const ::NYql::NUdf::IValueBuilder* valueBuilder, \
const ::NYql::NUdf::TUnboxedValuePod* args) const; \
::NYql::NUdf::TUnboxedValue Run( \
const ::NYql::NUdf::IValueBuilder* valueBuilder, \
const ::NYql::NUdf::TUnboxedValuePod* args) const override { \
try { \
return RunImpl(valueBuilder, args); \
} catch (const std::exception&) { \
TStringBuilder sb; \
sb << Pos_ << " "; \
sb << CurrentExceptionMessage(); \
sb << Endl << "[" << TStringBuf(Name()) << "]" ; \
UdfTerminate(sb.c_str()); \
} \
} \
static bool DeclareSignature( \
const ::NYql::NUdf::TStringRef& name, \
::NYql::NUdf::TType* userType, \
::NYql::NUdf::IFunctionTypeInfoBuilder& builder, \
bool typesOnly) { \
Y_UNUSED(userType); \
if (Name() == name) { \
typeBody if (!typesOnly) { \
create_impl \
SetIRImplementation(builder, irResourceId, irFunctionName); \
} \
return true; \
} \
return false; \
} \
::NYql::NUdf::TSourcePosition GetPos() const { return Pos_; } \
private: \
::NYql::NUdf::TSourcePosition Pos_; \
members \
}; \
::NYql::NUdf::TUnboxedValue udfName::RunImpl( \
const ::NYql::NUdf::IValueBuilder* valueBuilder, \
const ::NYql::NUdf::TUnboxedValuePod* args) const
#define UDF_IMPL(udfName, typeBody, members, init, irResourceId, irFunctionName, blockType) \
UDF_IMPL_EX(udfName, typeBody, members, init, irResourceId, irFunctionName, blockType, builder.Implementation(new udfName(builder));)
#define UDF(udfName, typeBody) UDF_IMPL(udfName, typeBody, ;, ;, "", "", void)
#define UDF_RUN_IMPL(udfName, typeBody, members, init, irResourceId, irFunctionName) \
struct udfName##Members { \
members \
}; \
\
class udfName: public ::NYql::NUdf::TBoxedValue, public udfName##Members { \
public: \
explicit udfName(::NYql::NUdf::IFunctionTypeInfoBuilder& builder) \
: Pos_(GetSourcePosition(builder)) \
{ \
init \
} \
static const ::NYql::NUdf::TStringRef& Name() { \
static auto name = ::NYql::NUdf::TStringRef::Of(#udfName).Substring(1, 256); \
return name; \
} \
class TImpl: public TBoxedValue, public udfName##Members { \
public: \
TImpl(const udfName##Members& parent, \
const ::NYql::NUdf::TUnboxedValuePod& runConfig, \
::NYql::NUdf::TSourcePosition pos) \
: udfName##Members(parent) \
, RunConfig(::NYql::NUdf::TUnboxedValuePod(runConfig)) \
, Pos_(pos) \
{} \
inline ::NYql::NUdf::TUnboxedValue RunImpl( \
const ::NYql::NUdf::IValueBuilder* valueBuilder, \
const ::NYql::NUdf::TUnboxedValuePod* args) const; \
::NYql::NUdf::TUnboxedValue Run( \
const ::NYql::NUdf::IValueBuilder* valueBuilder, \
const ::NYql::NUdf::TUnboxedValuePod* args) const override { \
try { \
return RunImpl(valueBuilder, args); \
} catch (const std::exception&) { \
TStringBuilder sb; \
sb << Pos_ << " "; \
sb << CurrentExceptionMessage(); \
sb << Endl << "[" << TStringBuf(Name()) << "]" ; \
UdfTerminate(sb.c_str()); \
} \
} \
\
private: \
::NYql::NUdf::TUnboxedValue RunConfig; \
::NYql::NUdf::TSourcePosition Pos_; \
}; \
::NYql::NUdf::TUnboxedValue Run( \
const ::NYql::NUdf::IValueBuilder* valueBuilder, \
const ::NYql::NUdf::TUnboxedValuePod* args) const override { \
Y_UNUSED(valueBuilder); \
return ::NYql::NUdf::TUnboxedValuePod(new TImpl(*this, args[0], Pos_)); \
} \
static bool DeclareSignature( \
const ::NYql::NUdf::TStringRef& name, \
::NYql::NUdf::TType* userType, \
::NYql::NUdf::IFunctionTypeInfoBuilder& builder, \
bool typesOnly) { \
Y_UNUSED(userType); \
if (Name() == name) { \
typeBody if (!typesOnly) { \
builder.Implementation(new udfName(builder)); \
SetIRImplementation(builder, irResourceId, irFunctionName); \
} \
return true; \
} \
return false; \
} \
private: \
::NYql::NUdf::TSourcePosition Pos_; \
}; \
::NYql::NUdf::TUnboxedValue udfName::TImpl::RunImpl( \
const ::NYql::NUdf::IValueBuilder* valueBuilder, \
const ::NYql::NUdf::TUnboxedValuePod* args) const
#define UDF_RUN(udfName, typeBody) UDF_RUN_IMPL(udfName, typeBody, ;, ;, "", "")
#define SIMPLE_UDF_IMPL_EX(udfName, typeBody, signature, irResourceId, irFunctionName, blockType, create_impl) \
UDF_IMPL_EX(udfName, typeBody, const ::NYql::NUdf::TType* ReturnType_;, ReturnType_ = ::NYql::NUdf::NImpl::TSimpleSignatureHelper<signature>::BuildReturnType(builder);, irResourceId, irFunctionName, blockType, create_impl)
#define SIMPLE_UDF_IMPL(udfName, typeBody, signature, irResourceId, irFunctionName, blockType) \
SIMPLE_UDF_IMPL_EX(udfName, typeBody, signature, irResourceId, irFunctionName, blockType, builder.Implementation(new udfName(builder));)
#define SIMPLE_UDF(udfName, signature) \
SIMPLE_UDF_IMPL(udfName, builder.SimpleSignature<signature>();, signature, "", "", void)
#define SIMPLE_STRICT_UDF(udfName, signature) \
SIMPLE_UDF_IMPL(udfName, builder.SimpleSignature<signature>().IsStrict();, signature, "", "", void)
#define SIMPLE_STRICT_UDF_WITH_IR(udfName, signature, optionalArgsCount, irResourceId, irFunctionName) \
SIMPLE_UDF_IMPL(udfName, builder.SimpleSignature<signature>().OptionalArgs(optionalArgsCount).IsStrict();, signature, irResourceId, irFunctionName, void)
#define SIMPLE_UDF_WITH_CREATE_IMPL(udfName, signature, create_impl) \
SIMPLE_UDF_IMPL_EX(udfName, builder.SimpleSignature<signature>();, signature, "", "", void, create_impl)
#define SIMPLE_UDF_OPTIONS(udfName, signature, options) \
SIMPLE_UDF_IMPL(udfName, builder.SimpleSignature<signature>(); options;, signature, "", "", void)
#define SIMPLE_UDF_WITH_OPTIONAL_ARGS(udfName, signature, optionalArgsCount) \
SIMPLE_UDF_IMPL(udfName, builder.SimpleSignature<signature>().OptionalArgs(optionalArgsCount);, signature, "", "", void)
#define SIMPLE_STRICT_UDF_OPTIONS(udfName, signature, options) \
SIMPLE_UDF_IMPL(udfName, builder.SimpleSignature<signature>().IsStrict(); options;, signature, "", "", void)
#define SIMPLE_STRICT_UDF_WITH_OPTIONAL_ARGS(udfName, signature, optionalArgsCount) \
SIMPLE_UDF_IMPL(udfName, builder.SimpleSignature<signature>().OptionalArgs(optionalArgsCount).IsStrict();, signature, "", "", void)
#define SIMPLE_UDF_RUN_OPTIONS(udfName, signature, options) \
UDF_RUN(udfName, builder.SimpleSignature<signature>(); options;)
#define SIMPLE_UDF_RUN(udfName, signature, runConfigSignature) \
SIMPLE_UDF_RUN_OPTIONS(udfName, signature, builder.RunConfig<runConfigSignature>())
#define SIMPLE_MODULE(moduleName, ...) \
class moduleName: public ::NYql::NUdf::TSimpleUdfModuleHelper<__VA_ARGS__> { \
public: \
::NYql::NUdf::TStringRef Name() const { \
auto name = ::NYql::NUdf::TStringRef::Of(#moduleName); \
return name.Substring(1, name.Size() - 7); \
} \
};
#define EMPTY_RESULT_ON_EMPTY_ARG(n) \
if (!args[n]) { \
return ::NYql::NUdf::TUnboxedValue(); \
}
namespace NYql {
namespace NUdf {
template<bool CheckOptional, bool CheckBlock, const char* TFuncName, template<class> class TFunc, typename... TUserTypes>
class TUserDataTypeFuncFactory : public ::NYql::NUdf::TBoxedValue {
public:
typedef bool TTypeAwareMarker;
public:
static const ::NYql::NUdf::TStringRef& Name() {
static auto name = ::NYql::NUdf::TStringRef(TFuncName, std::strlen(TFuncName));
return name;
}
static const TType* ExtractArgFromUserType(::NYql::NUdf::TType const* userType, ::NYql::NUdf::IFunctionTypeInfoBuilder& builder) {
if constexpr (CheckBlock) {
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 26)
TBlockTypeInspector block(*builder.TypeInfoHelper(), userType);
if (block) {
userType = block.GetItemType();
}
#endif
}
if constexpr (CheckOptional) {
TOptionalTypeInspector optionalTypeInspector(*builder.TypeInfoHelper(), userType);
if (optionalTypeInspector) {
userType = optionalTypeInspector.GetItemType();
}
}
return userType;
}
template<typename TUserType>
static bool DeclareSignatureImpl(
const ::NYql::NUdf::TStringRef& name,
TDataTypeId typeId,
::NYql::NUdf::TType* userType,
::NYql::NUdf::IFunctionTypeInfoBuilder& builder,
bool typesOnly)
{
if (TDataType<TUserType>::Id != typeId) {
return false;
}
TFunc<TUserType>::DeclareSignature(name, userType, builder, typesOnly);
return true;
}
template<typename TUserType, typename THead, typename... TTail>
static bool DeclareSignatureImpl(
const ::NYql::NUdf::TStringRef& name,
TDataTypeId typeId,
::NYql::NUdf::TType* userType,
::NYql::NUdf::IFunctionTypeInfoBuilder& builder,
bool typesOnly)
{
if (DeclareSignatureImpl<TUserType>(name, typeId, userType, builder, typesOnly)) {
return true;
}
return DeclareSignatureImpl<THead, TTail...>(name, typeId, userType, builder, typesOnly);
}
static bool DeclareSignature(
const ::NYql::NUdf::TStringRef& name,
::NYql::NUdf::TType* userType,
::NYql::NUdf::IFunctionTypeInfoBuilder& builder,
bool typesOnly)
{
if (Name() != name) {
// the only case when we return false
return false;
}
if (!userType) {
builder.SetError("User type is not specified");
return true;
}
auto typeHelper = builder.TypeInfoHelper();
auto userTypeInspector = TTupleTypeInspector(*typeHelper, userType);
if (!userTypeInspector || userTypeInspector.GetElementsCount() < 1) {
builder.SetError("Missing or invalid user type");
return true;
}
auto argsTypeInspector = TTupleTypeInspector(*typeHelper, userTypeInspector.GetElementType(0));
if (!argsTypeInspector || argsTypeInspector.GetElementsCount() < 1) {
builder.SetError("Missing or invalid user type arguments");
return true;
}
auto argType = ExtractArgFromUserType(argsTypeInspector.GetElementType(0), builder);
TDataTypeInspector dataTypeInspector(*typeHelper, argType);
if (!dataTypeInspector) {
TStringStream ss;
NUdf::TTypePrinter p(*typeHelper, argType);
p.Out(ss);
builder.SetError("User type must be a data type. Got: " + ss.Str());
return true;
}
builder.UserType(userType);
auto typeId = dataTypeInspector.GetTypeId();
if (!DeclareSignatureImpl<TUserTypes...>(name, typeId, userType, builder, typesOnly)) {
TStringBuilder sb;
sb << "User type " << NYql::NUdf::GetDataTypeInfo(NYql::NUdf::GetDataSlot(typeId)).Name << " is not supported";
builder.SetError(sb);
}
return true;
}
};
template<typename... TUdfs>
class TSimpleUdfModuleHelper : public IUdfModule
{
Y_HAS_SUBTYPE(TTypeAwareMarker);
Y_HAS_SUBTYPE(TBlockType);
public:
void CleanupOnTerminate() const override {
}
template<typename TUdfType>
void GetAllFunctionsImpl(IFunctionNamesSink& names) const {
auto r = names.Add(TUdfType::Name());
if (THasTTypeAwareMarker<TUdfType>::value) {
r->SetTypeAwareness();
}
if constexpr (THasTBlockType<TUdfType>::value) {
if constexpr (!std::is_same_v<typename TUdfType::TBlockType, void>) {
auto rBlocks = names.Add(TUdfType::TBlockType::Name());
rBlocks->SetTypeAwareness();
}
}
}
template<typename THead1, typename THead2, typename... TTail>
void GetAllFunctionsImpl(IFunctionNamesSink& names) const {
GetAllFunctionsImpl<THead1>(names);
GetAllFunctionsImpl<THead2, TTail...>(names);
}
template<typename TUdfType>
bool BuildFunctionTypeInfoImpl(
const TStringRef& name,
TType* userType,
const TStringRef& typeConfig,
ui32 flags,
IFunctionTypeInfoBuilder& builder) const
{
Y_UNUSED(typeConfig);
bool typesOnly = (flags & TFlags::TypesOnly);
bool found = TUdfType::DeclareSignature(name, userType, builder, typesOnly);
if (!found) {
if constexpr (THasTBlockType<TUdfType>::value) {
if constexpr (!std::is_same_v<typename TUdfType::TBlockType, void>) {
found = TUdfType::TBlockType::DeclareSignature(name, userType, builder, typesOnly);
}
}
}
return found;
}
template<typename THead1, typename THead2, typename... TTail>
bool BuildFunctionTypeInfoImpl(
const TStringRef& name,
TType* userType,
const TStringRef& typeConfig,
ui32 flags,
IFunctionTypeInfoBuilder& builder) const
{
bool found = BuildFunctionTypeInfoImpl<THead1>(name, userType, typeConfig, flags, builder);
if (!found) {
found = BuildFunctionTypeInfoImpl<THead2, TTail...>(name, userType, typeConfig, flags, builder);
}
return found;
}
void GetAllFunctions(IFunctionsSink& sink) const final {
GetAllFunctionsImpl<TUdfs...>(sink);
}
void BuildFunctionTypeInfo(
const TStringRef& name,
TType* userType,
const TStringRef& typeConfig,
ui32 flags,
IFunctionTypeInfoBuilder& builder) const override
{
try {
bool found = BuildFunctionTypeInfoImpl<TUdfs...>(name, userType, typeConfig, flags, builder);
if (!found) {
TStringBuilder sb;
sb << "Unknown function: " << name.Data();
builder.SetError(sb);
}
} catch (const std::exception&) {
builder.SetError(CurrentExceptionMessage());
}
}
};
} // namspace NUdf
} // namspace NYql
|