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
|
#include "mkql_computation_node_ut.h"
#include <yql/essentials/public/udf/udf_helpers.h>
#include <yql/essentials/minikql/mkql_node_serialization.h>
namespace NKikimr {
namespace NMiniKQL {
// Class, implementing the closure with run config.
class TRunConfig : public NYql::NUdf::TBoxedValue {
public:
explicit TRunConfig(NYql::NUdf::TSourcePosition pos)
: Pos_(pos)
{}
static const NYql::NUdf::TStringRef& Name() {
static auto name = NYql::NUdf::TStringRef::Of("Test");
return name;
}
static bool DeclareSignature(const NYql::NUdf::TStringRef& name,
NYql::NUdf::TType*,
NYql::NUdf::IFunctionTypeInfoBuilder& builder,
bool typesOnly)
{
if (Name() != name) {
return false;
}
builder.RunConfig<char*>().Args(1)->Add<char*>();
builder.Returns<char*>();
if (!typesOnly) {
builder.Implementation(new TRunConfig(builder.GetSourcePosition()));
}
return true;
}
private:
const NYql::NUdf::TSourcePosition Pos_;
};
// Class, implementing the closure with currying.
class TCurrying : public NYql::NUdf::TBoxedValue {
public:
explicit TCurrying(NYql::NUdf::TSourcePosition pos)
: Pos_(pos)
{}
static const NYql::NUdf::TStringRef& Name() {
static auto name = NYql::NUdf::TStringRef::Of("Test");
return name;
}
static bool DeclareSignature(const NYql::NUdf::TStringRef& name,
NYql::NUdf::TType*,
NYql::NUdf::IFunctionTypeInfoBuilder& builder,
bool typesOnly)
{
if (Name() != name) {
return false;
}
builder.OptionalArgs(1).Args(2)->Add<char*>()
.Add<NYql::NUdf::TOptional<bool>>().Name("NewOptionalArg");
builder.Returns(builder.SimpleSignatureType<char*(char*)>());
if (!typesOnly) {
builder.Implementation(new TCurrying(builder.GetSourcePosition()));
}
return true;
}
NYql::NUdf::TUnboxedValue Run(const NYql::NUdf::IValueBuilder*,
const NYql::NUdf::TUnboxedValuePod* args)
const final try {
const std::string_view upvalue(args[0].AsStringRef());
UNIT_ASSERT(!args[1]);
return NYql::NUdf::TUnboxedValuePod(new TImpl(Pos_, upvalue));
} catch (const std::exception& e) {
UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
}
private:
class TImpl : public NYql::NUdf::TBoxedValue {
public:
explicit TImpl(NYql::NUdf::TSourcePosition pos,
const std::string_view upvalue)
: Pos_(pos)
, Upvalue_(upvalue)
{}
NYql::NUdf::TUnboxedValue Run(const NYql::NUdf::IValueBuilder* valueBuilder,
const NYql::NUdf::TUnboxedValuePod* args)
const override try {
TStringStream concat;
concat << Upvalue_ << " " << args[0].AsStringRef();
return valueBuilder->NewString(NYql::NUdf::TStringRef(concat.Data(),
concat.Size()));
} catch (const std::exception& e) {
UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
}
private:
const NYql::NUdf::TSourcePosition Pos_;
const TString Upvalue_;
};
const NYql::NUdf::TSourcePosition Pos_;
};
// XXX: To properly test the issue described in YQL-19967, there
// should be two modules, registered by the same name, which
// provide the functions with different signatures, exported by
// the same name. Hence, class names for UDFs are different, but
// use the same name in MKQL bytecode.
SIMPLE_MODULE(TRunConfigUTModule, TRunConfig)
SIMPLE_MODULE(TCurryingUTModule, TCurrying)
Y_UNIT_TEST_SUITE(TMiniKQLUdfTest) {
Y_UNIT_TEST_LLVM(RunconfigToCurrying) {
// Create the test setup, using TRunConfig implementation
// for TestModule.Test UDF.
TVector<TUdfModuleInfo> compileModules;
compileModules.emplace_back(
TUdfModuleInfo{"", "TestModule", new TRunConfigUTModule()}
);
TSetup<LLVM> compileSetup(GetTestFactory(), std::move(compileModules));
TProgramBuilder& pb = *compileSetup.PgmBuilder;
// Build the graph on the setup with TRunConfig implementation.
const auto strType = pb.NewDataType(NUdf::TDataType<char*>::Id);
const auto upvalue = pb.NewDataLiteral<NUdf::EDataSlot::String>("Canary");
const auto value = pb.NewDataLiteral<NUdf::EDataSlot::String>("is alive");
const auto userType = pb.NewTupleType({
pb.NewTupleType({strType}),
pb.NewEmptyStructType(),
pb.NewEmptyTupleType()});
const auto udf = pb.Udf("TestModule.Test", upvalue, userType);
const auto list = pb.NewList(strType, {value});
const auto pgmReturn = pb.Map(list, [&pb, udf](const TRuntimeNode item) {
return pb.Apply(udf, {item});
});
// Create the test setup, using TCurrying implementation
// for TestModule.Test UDF.
TVector<TUdfModuleInfo> runModules;
runModules.emplace_back(
TUdfModuleInfo{"", "TestModule", new TCurryingUTModule()}
);
TSetup<LLVM> runSetup(GetTestFactory(), std::move(runModules));
// Move the graph from the one setup to another as a
// serialized bytecode sequence.
const auto bytecode = SerializeRuntimeNode(pgmReturn, *compileSetup.Env);
const auto root = DeserializeRuntimeNode(bytecode, *runSetup.Env);
// Run the graph on the setup with TCurrying implementation.
const auto graph = runSetup.BuildGraph(root);
const auto iterator = graph->GetValue().GetListIterator();
NUdf::TUnboxedValue result;
UNIT_ASSERT(iterator.Next(result));
UNIT_ASSERT_STRINGS_EQUAL(TStringBuf(result.AsStringRef()), "Canary is alive");
UNIT_ASSERT(!iterator.Next(result));
}
} // Y_UNIT_TEST_SUITE
} // namespace NMiniKQL
} // namespace NKikimr
|