aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/vector/vector_udf.cpp
blob: e8b01e5a05ccfc5e32ba0c9ba79dadce0184ef4e (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
#include <yql/essentials/public/udf/udf_type_ops.h>
#include <yql/essentials/public/udf/udf_helpers.h>

#include <vector>

using namespace NKikimr;
using namespace NUdf;

namespace {

class TVector {
private:
    std::vector<TUnboxedValue, TUnboxedValue::TAllocator> Vector;

public:
    TVector()
        : Vector()
    {}

    TUnboxedValue GetResult(const IValueBuilder* builder) {
        TUnboxedValue* values = nullptr;
        auto list = builder->NewArray(Vector.size(), values);
        std::copy(Vector.begin(), Vector.end(), values);

        return list;
    }

    void Emplace(const ui64 index, const TUnboxedValuePod& value) {
        if (index < Vector.size()) {
            Vector[index] = value;
        } else {
            Vector.push_back(value);
        }
    }

    void Swap(const ui64 a, const ui64 b) {
        if (a < Vector.size() && b < Vector.size()) {
            std::swap(Vector[a], Vector[b]);
        }
    }

    void Reserve(ui64 expectedSize) {
        Vector.reserve(expectedSize);
    }
};

extern const char VectorResourceName[] = "Vector.VectorResource";
class TVectorResource:
    public TBoxedResource<TVector, VectorResourceName>
{
public:
    template <typename... Args>
    inline TVectorResource(Args&&... args)
        : TBoxedResource(std::forward<Args>(args)...)
    {}
};

TVectorResource* GetVectorResource(const TUnboxedValuePod& arg) {
    TVectorResource::Validate(arg);
    return static_cast<TVectorResource*>(arg.AsBoxed().Get());
}

class TVectorCreate: public TBoxedValue {
private:
    TUnboxedValue Run(const IValueBuilder*, const TUnboxedValuePod* args) const override {
        auto resource = new TVectorResource;
        resource->Get()->Reserve(args[0].Get<ui64>());
        return TUnboxedValuePod(resource);
    }
};

class TVectorEmplace: public TBoxedValue {
private:
    TUnboxedValue Run(const IValueBuilder*, const TUnboxedValuePod* args) const override {
        auto resource = GetVectorResource(args[0]);
        resource->Get()->Emplace(args[1].Get<ui64>(), args[2]);
        return TUnboxedValuePod(resource);
    }
};

class TVectorSwap: public TBoxedValue {
private:
    TUnboxedValue Run(const IValueBuilder*, const TUnboxedValuePod* args) const override {
        auto resource = GetVectorResource(args[0]);
        resource->Get()->Swap(args[1].Get<ui64>(), args[2].Get<ui64>());
        return TUnboxedValuePod(resource);
    }
};

class TVectorGetResult: public TBoxedValue {
private:
    TUnboxedValue Run(const IValueBuilder* valueBuilder, const TUnboxedValuePod* args) const override {
        return GetVectorResource(args[0])->Get()->GetResult(valueBuilder);
    }
};

static const auto CreateName = TStringRef::Of("Create");
static const auto EmplaceName = TStringRef::Of("Emplace");
static const auto SwapName = TStringRef::Of("Swap");
static const auto GetResultName = TStringRef::Of("GetResult");

class TVectorModule: public IUdfModule {
public:
    TStringRef Name() const {
        return TStringRef::Of("Vector");
    }

    void CleanupOnTerminate() const final {
    }

    void GetAllFunctions(IFunctionsSink& sink) const final {
        sink.Add(CreateName)->SetTypeAwareness();
        sink.Add(EmplaceName)->SetTypeAwareness();
        sink.Add(SwapName)->SetTypeAwareness();
        sink.Add(GetResultName)->SetTypeAwareness();
    }

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

        try {
            const bool typesOnly = (flags & TFlags::TypesOnly);
            builder.UserType(userType);

            auto typeHelper = builder.TypeInfoHelper();

            auto userTypeInspector = TTupleTypeInspector(*typeHelper, userType);
            if (!userTypeInspector || userTypeInspector.GetElementsCount() != 3) {
                builder.SetError("User type is not a 3-tuple");
                return;
            }

            auto valueType = userTypeInspector.GetElementType(2);
            TType* vectorType = builder.Resource(VectorResourceName);

            if (name == CreateName) {
                builder.IsStrict();

                builder.Args()->Add<ui64>().Done().Returns(vectorType);

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

            if (name == EmplaceName) {
                builder.IsStrict();

                builder.Args()->Add(vectorType).Add<ui64>().Add(valueType).Done().Returns(vectorType);

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

            if (name == SwapName) {
                builder.IsStrict();

                builder.Args()->Add(vectorType).Add<ui64>().Add<ui64>().Done().Returns(vectorType);

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

            if (name == GetResultName) {
                auto resultType = builder.List()->Item(valueType).Build();

                builder.IsStrict();

                builder.Args()->Add(vectorType).Done().Returns(resultType);

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

        } catch (const std::exception& e) {
            builder.SetError(CurrentExceptionMessage());
        }
    }
};

} // namespace

REGISTER_MODULES(TVectorModule)