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
|
#include "mkql_builtins.h"
#include "mkql_builtins_impl.h" // Y_IGNORE
#include "mkql_builtins_compare.h"
#include "mkql_builtins_string_kernels.h"
#include <yql/essentials/minikql/arrow/arrow_defs.h>
#include <util/digest/murmur.h>
#include <util/generic/yexception.h>
#include <util/generic/maybe.h>
#include <algorithm>
#include <arrow/compute/registry.h>
#include <arrow/compute/registry_internal.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
void RegisterDefaultOperations(IBuiltinFunctionRegistry& registry, TKernelFamilyMap& kernelFamilyMap) {
RegisterAdd(registry);
RegisterAdd(kernelFamilyMap);
RegisterAggrAdd(registry);
RegisterSub(registry);
RegisterSub(kernelFamilyMap);
RegisterMul(registry);
RegisterMul(kernelFamilyMap);
RegisterDiv(registry);
RegisterDiv(kernelFamilyMap);
RegisterMod(registry);
RegisterMod(kernelFamilyMap);
RegisterIncrement(registry);
RegisterDecrement(registry);
RegisterBitAnd(registry);
RegisterBitOr(registry);
RegisterBitXor(registry);
RegisterShiftLeft(registry);
RegisterShiftRight(registry);
RegisterRotLeft(registry);
RegisterRotRight(registry);
RegisterPlus(registry);
RegisterMinus(registry);
RegisterMinus(kernelFamilyMap);
RegisterBitNot(registry);
RegisterCountBits(registry);
RegisterAbs(registry);
RegisterAbs(kernelFamilyMap);
RegisterConvert(registry);
RegisterConcat(registry);
RegisterSubstring(registry);
RegisterFind(registry);
RegisterWith(registry);
RegisterWith(kernelFamilyMap);
RegisterInversePresortString(registry);
RegisterInverseString(registry);
RegisterNanvl(registry);
RegisterByteAt(registry);
RegisterMax(registry);
RegisterMin(registry);
RegisterAggrMax(registry);
RegisterAggrMin(registry);
RegisterEquals(registry);
RegisterEquals(kernelFamilyMap);
RegisterNotEquals(registry);
RegisterNotEquals(kernelFamilyMap);
RegisterLess(registry);
RegisterLess(kernelFamilyMap);
RegisterLessOrEqual(registry);
RegisterLessOrEqual(kernelFamilyMap);
RegisterGreater(registry);
RegisterGreater(kernelFamilyMap);
RegisterGreaterOrEqual(registry);
RegisterGreaterOrEqual(kernelFamilyMap);
// Size is missing in registry
RegisterSizeBuiltin(kernelFamilyMap);
}
void PrintType(NUdf::TDataTypeId schemeType, bool isOptional, IOutputStream& out)
{
const auto slot = NUdf::FindDataSlot(schemeType);
out << (slot ? NUdf::GetDataTypeInfo(*slot).Name : "unknown");
if (isOptional) {
out << '?';
}
}
void PrintFunctionSignature(
const std::string_view& funcName,
const TFunctionDescriptor& desc,
IOutputStream& out)
{
const auto* param = desc.ResultAndArgs;
out << '\t';
// print results type
PrintType(param->SchemeType, param->IsNullable(), out);
++param;
// print function name and args types
out << ' ' << funcName << '(';
while (param->SchemeType != 0) {
PrintType(param->SchemeType, param->IsNullable(), out);
++param;
if (param->SchemeType != 0) {
out << ", ";
}
}
out << ')';
}
bool IsArgumentsMatch(
const TFunctionParamMetadata* paramsMetadata,
const std::pair<NUdf::TDataTypeId, bool>* argTypes, size_t argTypesCount)
{
size_t index = 0;
while (paramsMetadata->SchemeType) {
if (index >= argTypesCount) {
return false;
}
if (argTypes[index].first != paramsMetadata->SchemeType) {
return false;
}
if (argTypes[index].second != paramsMetadata->IsNullable()) {
return false;
}
++paramsMetadata;
++index;
}
return index == argTypesCount;
}
//////////////////////////////////////////////////////////////////////////////
// TBuiltinFunctionRegistry
//////////////////////////////////////////////////////////////////////////////
class TBuiltinFunctionRegistry: public IBuiltinFunctionRegistry
{
public:
TBuiltinFunctionRegistry();
private:
TFunctionDescriptor GetBuiltin(const std::string_view& name,
const std::pair<NUdf::TDataTypeId, bool>* argTypes, size_t argTypesCount) const final;
bool HasBuiltin(const std::string_view& name) const final;
ui64 GetMetadataEtag() const final;
void PrintInfoTo(IOutputStream& out) const final;
void Register(const std::string_view& name, const TFunctionDescriptor& description) final;
void RegisterAll(TFunctionsMap&& functions, TFunctionParamMetadataList&& arguments) final;
const TFunctionsMap& GetFunctions() const final;
void CalculateMetadataEtag();
std::optional<TFunctionDescriptor> FindBuiltin(const std::string_view& name, const std::pair<NUdf::TDataTypeId, bool>* argTypes, size_t argTypesCount) const;
const TDescriptionList& FindCandidates(const std::string_view& name) const;
const TKernel* FindKernel(const std::string_view& name, const NUdf::TDataTypeId* argTypes, size_t argTypesCount, NUdf::TDataTypeId returnType) const final;
void RegisterKernelFamily(const std::string_view& name, std::unique_ptr<TKernelFamily>&& family) final;
TVector<std::pair<TString, const TKernelFamily*>> GetAllKernelFamilies() const final;
TFunctionsMap Functions;
TFunctionParamMetadataList ArgumentsMetadata;
std::optional<ui64> MetadataEtag;
TKernelFamilyMap KernelFamilyMap;
};
TBuiltinFunctionRegistry::TBuiltinFunctionRegistry()
{
RegisterDefaultOperations(*this, KernelFamilyMap);
CalculateMetadataEtag();
}
void TBuiltinFunctionRegistry::Register(const std::string_view& name, const TFunctionDescriptor& description)
{
Functions[TString(name)].push_back(description);
}
void TBuiltinFunctionRegistry::RegisterAll(TFunctionsMap&& functions, TFunctionParamMetadataList&& arguments)
{
Functions = std::move(functions);
ArgumentsMetadata = std::move(arguments);
CalculateMetadataEtag();
}
const TFunctionsMap& TBuiltinFunctionRegistry::GetFunctions() const
{
return Functions;
}
const TDescriptionList& TBuiltinFunctionRegistry::FindCandidates(const std::string_view& name) const {
if (const auto it = Functions.find(TString(name)); it != Functions.cend())
return it->second;
ythrow yexception() << "Not found builtin function: '" << name << "' in " << Functions.size() << " total.";
}
std::optional<TFunctionDescriptor> TBuiltinFunctionRegistry::FindBuiltin(const std::string_view& name, const std::pair<NUdf::TDataTypeId, bool>* argTypes, size_t argTypesCount) const
{
for (const auto& desc: FindCandidates(name)) {
if (IsArgumentsMatch(desc.ResultAndArgs, argTypes, argTypesCount)) {
return desc;
}
}
return std::nullopt;
}
TFunctionDescriptor TBuiltinFunctionRegistry::GetBuiltin(const std::string_view& name,
const std::pair<NUdf::TDataTypeId, bool>* argTypes, size_t argTypesCount) const
{
if (const auto desc = FindBuiltin(name, argTypes, argTypesCount)) {
return *desc;
}
TStringStream ss;
PrintType(argTypes[0].first, argTypes[0].second, ss);
ss << ' ' << name << '(';
for (size_t i = 1U; i < argTypesCount; i++) {
if (i > 1U) {
ss << ", ";
}
PrintType(argTypes[i].first, argTypes[i].second, ss);
}
ss << ").\nCandidates are: [\n";
ui32 i = 0;
for (const TFunctionDescriptor& desc: FindCandidates(name)) {
PrintFunctionSignature(name, desc, ss);
ss << '\n';
if (++i > 32) {
ss << "\t...\n";
break;
}
}
ss << ']';
ythrow yexception() << "Unsupported builtin function: " << ss.Str();
}
bool TBuiltinFunctionRegistry::HasBuiltin(const std::string_view& name) const
{
return Functions.find(TString(name)) != Functions.cend();
}
void TBuiltinFunctionRegistry::CalculateMetadataEtag() {
using TFunctionPair = std::pair<std::string_view, const TDescriptionList*>;
std::vector<TFunctionPair> operations;
for (const auto& func : Functions) {
operations.emplace_back(func.first, &func.second);
}
std::sort(operations.begin(), operations.end(), [](const TFunctionPair& x, const TFunctionPair& y) {
return x.first < y.first;
});
ui64 hash = 0;
for (const auto& op : operations) {
const ui64 nameLength = op.first.size();
hash = MurmurHash<ui64>(&nameLength, sizeof(nameLength), hash);
hash = MurmurHash<ui64>(op.first.data(), op.first.size(), hash);
const auto& descriptions = *op.second;
const ui64 descriptionCount = descriptions.size();
hash = MurmurHash<ui64>(&descriptionCount, sizeof(descriptionCount), hash);
for (const auto& description : descriptions) {
for (const auto* args = description.ResultAndArgs; args->SchemeType; ++args) {
hash = MurmurHash<ui64>(args, sizeof(*args), hash);
}
}
}
MetadataEtag = hash;
}
ui64 TBuiltinFunctionRegistry::GetMetadataEtag() const
{
return *MetadataEtag;
}
void TBuiltinFunctionRegistry::PrintInfoTo(IOutputStream& out) const
{
for (const auto& f: Functions) {
out << f.first << ": [\n";
for (const TFunctionDescriptor& desc: f.second) {
PrintFunctionSignature(f.first, desc, out);
out << '\n';
}
out << "]\n\n";
}
}
const TKernel* TBuiltinFunctionRegistry::FindKernel(const std::string_view& name, const NUdf::TDataTypeId* argTypes, size_t argTypesCount, NUdf::TDataTypeId returnType) const {
auto fit = KernelFamilyMap.find(TString(name));
if (fit == KernelFamilyMap.end()) {
return nullptr;
}
return fit->second->FindKernel(argTypes, argTypesCount, returnType);
}
void TBuiltinFunctionRegistry::RegisterKernelFamily(const std::string_view& name, std::unique_ptr<TKernelFamily>&& family) {
Y_ENSURE(KernelFamilyMap.emplace(TString(name), std::move(family)).second);
}
TVector<std::pair<TString, const TKernelFamily*>> TBuiltinFunctionRegistry::GetAllKernelFamilies() const {
TVector<std::pair<TString, const TKernelFamily*>> ret;
for (const auto& f : KernelFamilyMap) {
ret.emplace_back(std::make_pair(f.first, f.second.get()));
}
return ret;
}
} // namespace
IBuiltinFunctionRegistry::TPtr CreateBuiltinRegistry() {
return MakeIntrusive<TBuiltinFunctionRegistry>();
}
} // namespace NMiniKQL
} // namespace NKikimr
|