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
|
#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_value_builder.h>
#include <yql/essentials/public/udf/udf_type_builder.h>
#include <yql/essentials/public/udf/udf_registrator.h>
#include <util/generic/yexception.h>
#include <util/string/cast.h>
using namespace NKikimr;
using namespace NUdf;
namespace {
struct TPersonInfo
{
ui32 FirstName = 0;
ui32 LastName = 0;
ui32 Age = 0;
bool RemapKSV = false;
ui32 Key = 0;
ui32 Subkey = 0;
ui32 Value = 0;
static constexpr ui32 FieldsCount = 3U;
};
//////////////////////////////////////////////////////////////////////////////
// TPersonMember
//////////////////////////////////////////////////////////////////////////////
class TPersonMember: public TBoxedValue
{
public:
explicit TPersonMember(ui32 memberIndex)
: MemberIndex_(memberIndex)
{
}
private:
TUnboxedValue Run(
const IValueBuilder* valueBuilder,
const TUnboxedValuePod* args) const override
{
Y_UNUSED(valueBuilder);
return args[0].GetElement(MemberIndex_);
}
const ui32 MemberIndex_;
};
//////////////////////////////////////////////////////////////////////////////
// TNewPerson
//////////////////////////////////////////////////////////////////////////////
class TNewPerson: public TBoxedValue
{
public:
explicit TNewPerson(const TPersonInfo& personIndexes)
: Info_(personIndexes)
{
}
private:
TUnboxedValue Run(
const IValueBuilder* valueBuilder,
const TUnboxedValuePod* args) const override
{
TUnboxedValue name, surname, age;
if (Info_.RemapKSV) {
name = args->GetElement(Info_.Key);
surname = args->GetElement(Info_.Subkey);
const auto ageStr = args->GetElement(Info_.Value);
const ui32 ageNum = FromString(ageStr.AsStringRef().Data(), ageStr.AsStringRef().Size());
age = TUnboxedValuePod(ageNum);
} else {
name = TUnboxedValuePod(args[0]);
surname = TUnboxedValuePod(args[1]);
age = TUnboxedValuePod(args[2]);
}
TUnboxedValue* items = nullptr;
auto result = valueBuilder->NewArray(Info_.FieldsCount, items);
items[Info_.FirstName] = std::move(name);
items[Info_.LastName] = std::move(surname);
items[Info_.Age] = std::move(age);
return result;
}
const TPersonInfo Info_;
};
//////////////////////////////////////////////////////////////////////////////
// TPersonModule
//////////////////////////////////////////////////////////////////////////////
class TPersonModule: public IUdfModule
{
public:
TStringRef Name() const {
return TStringRef::Of("Person");
}
void CleanupOnTerminate() const final {}
void GetAllFunctions(IFunctionsSink& sink) const final {
sink.Add(TStringRef::Of("FirstName"));
sink.Add(TStringRef::Of("LastName"));
sink.Add(TStringRef::Of("Age"));
sink.Add(TStringRef::Of("New"))->SetTypeAwareness();
}
void BuildFunctionTypeInfo(
const TStringRef& name,
TType* userType,
const TStringRef& typeConfig,
ui32 flags,
IFunctionTypeInfoBuilder& builder) const final
{
Y_UNUSED(userType);
try {
bool typesOnly = (flags & TFlags::TypesOnly);
TPersonInfo personInfo;
auto personType = builder.Struct(personInfo.FieldsCount)->
AddField<char*>("FirstName", &personInfo.FirstName)
.AddField<char*>("LastName", &personInfo.LastName)
.AddField<ui32>("Age", &personInfo.Age)
.Build();
if (TStringRef::Of("FirstName") == name) {
// function signature: String FirstName(PersonStruct p)
// runConfig: void
builder.Returns<char*>().Args()->Add(personType).Done();
if (!typesOnly) {
builder.Implementation(new TPersonMember(personInfo.FirstName));
}
}
else if (TStringRef::Of("LastName") == name) {
// function signature: String LastName(PersonStruct p)
// runConfig: void
builder.Returns<char*>().Args()->Add(personType).Done();
if (!typesOnly) {
builder.Implementation(new TPersonMember(personInfo.LastName));
}
}
else if (TStringRef::Of("Age") == name) {
// function signature: ui32 Age(PersonStruct p)
// runConfig: void
builder.Returns<ui32>().Args()->Add(personType).Done();
if (!typesOnly) {
builder.Implementation(new TPersonMember(personInfo.Age));
}
}
else if (TStringRef::Of("New") == name) {
// function signature:
// PersonStruct New(String firstName, String lastName, ui32 age)
// runConfig: void
builder.Returns(personType);
if (TStringRef::Of("RemapKSV") == typeConfig) {
personInfo.RemapKSV = true;
auto inputType = builder.Struct(personInfo.FieldsCount)->
AddField<char*>("key", &personInfo.Key)
.AddField<char*>("subkey", &personInfo.Subkey)
.AddField<char*>("value", &personInfo.Value)
.Build();
builder.Args()->Add(inputType);
} else {
builder.Args()->Add<char*>().Add<char*>().Add<ui32>();
}
builder.RunConfig<void>(); // this is optional because default runConfigType is Void
if (!typesOnly) {
builder.Implementation(new TNewPerson(personInfo));
}
}
} catch (const std::exception& e) {
builder.SetError(CurrentExceptionMessage());
}
}
};
} // namespace
REGISTER_MODULES(TPersonModule)
|