aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/examples/dummylog/dummylog_udf.cpp
blob: e3e227f1303fbfe3bf65af243d6b67ea40c44f9b (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
#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>

using namespace NKikimr;
using namespace NUdf;

namespace {

struct TRecordInfo
{
    ui32 Key;
    ui32 Subkey;
    ui32 Value;
    static constexpr ui32 FieldsCount = 3U;
};


//////////////////////////////////////////////////////////////////////////////
// TDummyLog
//////////////////////////////////////////////////////////////////////////////
class TDummyLog: public TBoxedValue
{
public:
    explicit TDummyLog(
            const TRecordInfo& fieldIndexes)
        : RecordInfo_(fieldIndexes)
    {
    }

private:
    TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const override
    {
        auto keyData = args[0].GetElement(RecordInfo_.Key);
        auto subkeyData = args[0].GetElement(RecordInfo_.Subkey);
        auto valueData = args[0].GetElement(RecordInfo_.Value);

        TString key = TString("Key: ") + keyData.AsStringRef();
        TString subkey = TString("Subkey: ") + subkeyData.AsStringRef();
        TString value = TString("Value: ") + valueData.AsStringRef();

        TUnboxedValue* items = nullptr;
        auto res = valueBuilder->NewArray(RecordInfo_.FieldsCount, items);
        items[RecordInfo_.Key] = valueBuilder->NewString(key);
        items[RecordInfo_.Subkey] = valueBuilder->NewString(subkey);
        items[RecordInfo_.Value] = valueBuilder->NewString(value);
        return res;
    }

    const TRecordInfo RecordInfo_;
};

class TDummyLog2 : public TBoxedValue
{
public:
    class TFactory : public TBoxedValue {
    public:
        TFactory(const TRecordInfo& inputInfo, const TRecordInfo& outputInfo)
            : InputInfo_(inputInfo)
            , OutputInfo_(outputInfo)
        {}

private:
        TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const override
        {
            Y_UNUSED(valueBuilder);
            return TUnboxedValuePod(new TDummyLog2(args[0], InputInfo_, OutputInfo_));
        }

        const TRecordInfo InputInfo_;
        const TRecordInfo OutputInfo_;
    };

    explicit TDummyLog2(
        const TUnboxedValuePod& runConfig,
        const TRecordInfo& inputInfo,
        const TRecordInfo& outputInfo
    )
        : Prefix_(runConfig.AsStringRef())
        , InputInfo_(inputInfo)
        , OutputInfo_(outputInfo)
    {
    }

private:
    TUnboxedValue Run(
        const IValueBuilder* valueBuilder,
        const TUnboxedValuePod* args) const override
    {
        auto keyData = args[0].GetElement(InputInfo_.Key);
        auto valueData = args[0].GetElement(InputInfo_.Value);

        TString key = Prefix_ + TString("Key: ") + keyData.AsStringRef();
        TString value = Prefix_ + TString("Value: ") + valueData.AsStringRef();

        TUnboxedValue* items = nullptr;
        auto res = valueBuilder->NewArray(2U, items);
        items[OutputInfo_.Key] = valueBuilder->NewString(key);
        items[OutputInfo_.Value] = valueBuilder->NewString(value);
        return res;
    }

    const TString Prefix_;
    const TRecordInfo InputInfo_;
    const TRecordInfo OutputInfo_;
};

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

    void CleanupOnTerminate() const final {}

    void GetAllFunctions(IFunctionsSink& sink) const final {
        sink.Add(TStringRef::Of("ReadRecord"));
        sink.Add(TStringRef::Of("ReadRecord2"))->SetTypeAwareness();
    }

    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 (TStringRef::Of("ReadRecord") == name) {
                TRecordInfo recordInfo;
                auto recordType = builder.Struct(recordInfo.FieldsCount)->
                        AddField<char*>("key", &recordInfo.Key)
                        .AddField<char*>("subkey", &recordInfo.Subkey)
                        .AddField<char*>("value", &recordInfo.Value)
                        .Build();

                builder.Returns(recordType).Args()->Add(recordType).Done();

                if (!typesOnly) {
                    builder.Implementation(new TDummyLog(recordInfo));
                }
            }

            if (TStringRef::Of("ReadRecord2") == name) {
                if (TStringBuf(typeConfig) != TStringBuf("AAA")) {
                    builder.SetError(TStringRef::Of("Only AAA is valid type config"));
                }
                TRecordInfo inputInfo;
                auto inputType = builder.Struct(inputInfo.FieldsCount)->
                    AddField<char*>("key", &inputInfo.Key)
                    .AddField<char*>("subkey", &inputInfo.Subkey)
                    .AddField<char*>("value", &inputInfo.Value)
                    .Build();

                TRecordInfo outputInfo;
                auto outputType = builder.Struct(2U)->
                    AddField<char*>("key", &outputInfo.Key)
                    .AddField<char*>("value", &outputInfo.Value)
                    .Build();


                builder.Returns(outputType).Args()->Add(inputType).Done();
                builder.RunConfig<char*>();

                if (!typesOnly) {
                    builder.Implementation(new TDummyLog2::TFactory(inputInfo, outputInfo));
                }
            }
        } catch (const std::exception& e) {
            builder.SetError(CurrentExceptionMessage());
        }
    }
};

} // namespace

REGISTER_MODULES(TDummyLogModule)