aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/examples/callables/callables_udf.cpp
blob: 6d8f1c27d9a412cea691eca0f7a33885d100cbf9 (plain) (blame)
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
#include <yql/essentials/public/udf/udf_value.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 {

//////////////////////////////////////////////////////////////////////////////
// TFromString
//////////////////////////////////////////////////////////////////////////////
class TFromString: public TBoxedValue
{
public:
    static TStringRef Name() {
        static auto name = TStringRef::Of("FromString");
        return name;
    }

private:
    TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const override
    {
        Y_UNUSED(valueBuilder);
        auto str = args[0].AsStringRef();
        int val = FromString<int>(str);
        return TUnboxedValuePod(val);
    }
};

//////////////////////////////////////////////////////////////////////////////
// TSum
//////////////////////////////////////////////////////////////////////////////
class TSum: public TBoxedValue
{
public:
    static TStringRef Name() {
        static auto name = TStringRef::Of("Sum");
        return name;
    }

private:
    TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const override
    {
        int sum = 0;

        auto it = args[0].GetListIterator();
        for (TUnboxedValue arg; it.Next(arg);) {
            auto value = args[1].Run(valueBuilder, &arg);
            sum += value.Get<int>();
        }

        return TUnboxedValuePod(sum);
    }
};

//////////////////////////////////////////////////////////////////////////////
// TMul
//////////////////////////////////////////////////////////////////////////////
class TMul: public TBoxedValue
{
public:
    static TStringRef Name() {
        static auto name = TStringRef::Of("Mul");
        return name;
    }

private:
    TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const override
    {
        int mul = 1;

        const auto it = args[0].GetListIterator();
        for (TUnboxedValue arg; it.Next(arg);) {
            auto value = args[1].Run(valueBuilder, &arg);
            mul *= value.Get<int>();
        }

        return TUnboxedValuePod(mul);
    }
};

extern const char A[] = "a";
using TNamedA = TNamedArg<i32, A>;

//////////////////////////////////////////////////////////////////////////////
// TNamedArgUdf
//////////////////////////////////////////////////////////////////////////////

class TNamedArgUdf: public TBoxedValue {
public:
    static TStringRef Name() {
        static auto name = TStringRef::Of("NamedArgUdf");
        return name;
    }

private:
    TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const override
    {
        Y_UNUSED(valueBuilder);
        auto res = args[0] ? args[0].Get<i32>() : 123;
        return TUnboxedValuePod(res + 1);
    }
};

//////////////////////////////////////////////////////////////////////////////
// TReturnNamedArgCallable
//////////////////////////////////////////////////////////////////////////////

class TReturnNamedArgCallable: public TBoxedValue {
public:
    static TStringRef Name() {
        static auto name = TStringRef::Of("ReturnNamedArgCallable");
        return name;
    }

    TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const override
    {
        Y_UNUSED(valueBuilder);
        Y_UNUSED(args);
        return TUnboxedValuePod(new TNamedArgUdf());
    }
};

//////////////////////////////////////////////////////////////////////////////
// TCallablesModule
//////////////////////////////////////////////////////////////////////////////
class TCallablesModule: public IUdfModule
{
public:
    TStringRef Name() const {
        return TStringRef::Of("Callables");
    }

    void CleanupOnTerminate() const final {}

    void GetAllFunctions(IFunctionsSink& sink) const final {
        sink.Add(TFromString::Name());
        sink.Add(TSum::Name());
        sink.Add(TMul::Name());
    }

    void BuildFunctionTypeInfo(
            const TStringRef& name,
            TType* userType,
            const TStringRef& typeConfig,
            ui32 flags,
            IFunctionTypeInfoBuilder& builder) const final
    {
        try {
            Y_UNUSED(userType);
            Y_UNUSED(typeConfig);

            bool typesOnly = (flags & TFlags::TypesOnly);

            if (TFromString::Name() == name) {
                // function signature:
                //      int (String)
                // run config: void
                builder.SimpleSignature<int(char*)>();

                if (!typesOnly) {
                    builder.Implementation(new TFromString);
                }
            }
            else if (TSum::Name() == name) {
                // function signature:
                //      int (ListOf(String), int(*)(String))
                // run config: void
                builder.Returns<int>().Args()->
                        Add(builder.List()->Item<char*>())
                        .Add(builder.Callable()->Returns<int>().Arg<char*>())
                        .Done();

                if (!typesOnly) {
                    builder.Implementation(new TSum);
                }
            }
            else if (TMul::Name() == name) {
                // function signature:
                //      int (ListOf(String), int(*)(String))
                // run config: void
                using TFuncType = int(*)(char*);
                builder.SimpleSignature<int(TListType<char*>, TFuncType)>();

                if (!typesOnly) {
                    builder.Implementation(new TMul);
                }
            } else if (TNamedArgUdf::Name() == name) {
                builder.SimpleSignature<int(TNamedA)>();

                if (!typesOnly) {
                    builder.Implementation(new TNamedArgUdf());
                }
            } else if (TReturnNamedArgCallable::Name() == name) {
                builder.Returns(builder.Callable()->Returns<int>().Arg<int>().Name(A));

                if (!typesOnly) {
                    builder.Implementation(new TReturnNamedArgCallable());
                }
            }
        } catch (const std::exception& e) {
            builder.SetError(CurrentExceptionMessage());
        }
    }
};

} // namespace

REGISTER_MODULES(TCallablesModule)