aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/udf/udf_registrator.h
blob: 1ed184026552acba5c8ef0060843bdbbcce3bdc9 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma once

#include "udf_version.h"
#include "udf_types.h"
#include "udf_ptr.h"
#include "udf_string.h"
#include "udf_type_size_check.h"
#include "udf_value.h"

#include <functional>

#include <stdarg.h>

#ifdef _win_
#  ifdef BUILD_UDF
#    define UDF_API __declspec(dllexport)
#  else
#    define UDF_API __declspec(dllimport)
#  endif
#else
#  define UDF_API __attribute__ ((visibility("default")))
#endif

#ifdef BUILD_UDF
#define REGISTER_MODULES(...) \
    extern "C" UDF_API void Register( \
            ::NYql::NUdf::IRegistrator& registrator, ui32 flags) { \
        Y_UNUSED(flags); \
        ::NYql::NUdf::RegisterHelper<__VA_ARGS__>(registrator); \
    } \
    extern "C" UDF_API ui32 AbiVersion() { \
        return ::NYql::NUdf::CurrentAbiVersion(); \
    }\
    extern "C" UDF_API void SetBackTraceCallback(::NYql::NUdf::TBackTraceCallback callback) { \
        ::NYql::NUdf::SetBackTraceCallbackImpl(callback); \
    }
#else
#define REGISTER_MODULES(...) \
    namespace { \
        struct TYqlStaticUdfRegistrator { \
            inline TYqlStaticUdfRegistrator() { \
                ::NYql::NUdf::AddToStaticUdfRegistry<__VA_ARGS__>(); \
            } \
        } YQL_REGISTRATOR; \
    }
#endif

namespace NYql {
namespace NUdf {

class IFunctionTypeInfoBuilder;

struct TStaticSymbols {
    void* (*UdfAllocateFunc)(ui64 size);
    void (*UdfFreeFunc)(const void* mem);
    void (*UdfTerminate)(const char* message);
    void (*UdfRegisterObject)(TBoxedValue* object);
    void (*UdfUnregisterObject)(TBoxedValue* object);
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
    void* (*UdfAllocateWithSizeFunc)(ui64 size);
    void (*UdfFreeWithSizeFunc)(const void* mem, ui64 size);
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
    void* (*UdfArrowAllocateFunc)(ui64 size);
    void* (*UdfArrowReallocateFunc)(const void* mem, ui64 prevSize, ui64 size);
    void (*UdfArrowFreeFunc)(const void* mem, ui64 size);
#endif
};

#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
UDF_ASSERT_TYPE_SIZE(TStaticSymbols, 80);
#elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
UDF_ASSERT_TYPE_SIZE(TStaticSymbols, 56);
#else
UDF_ASSERT_TYPE_SIZE(TStaticSymbols, 40);
#endif

inline TStaticSymbols GetStaticSymbols();

//////////////////////////////////////////////////////////////////////////////
// IFunctionNamesSink
//////////////////////////////////////////////////////////////////////////////
class IFunctionDescriptor
{
public:
    typedef TUniquePtr<IFunctionDescriptor> TPtr;

    virtual ~IFunctionDescriptor() = default;

    virtual void SetTypeAwareness() = 0;
};

UDF_ASSERT_TYPE_SIZE(IFunctionDescriptor, 8);

//////////////////////////////////////////////////////////////////////////////
// IFunctionNamesSink
//////////////////////////////////////////////////////////////////////////////
class IFunctionsSink
{
public:
    virtual ~IFunctionsSink() = default;

    virtual IFunctionDescriptor::TPtr Add(const TStringRef& name) = 0;
};

UDF_ASSERT_TYPE_SIZE(IFunctionsSink, 8);

typedef IFunctionsSink IFunctionNamesSink;

//////////////////////////////////////////////////////////////////////////////
// IUdfModule
//////////////////////////////////////////////////////////////////////////////
class IUdfModule
{
public:
    struct TFlags {
        enum {
            TypesOnly = 0x01
        };
    };

public:
    virtual ~IUdfModule() = default;

    virtual void GetAllFunctions(IFunctionsSink& sink) const = 0;

    virtual void BuildFunctionTypeInfo(
            const TStringRef& name,
            TType* userType,
            const TStringRef& typeConfig,
            ui32 flags,
            IFunctionTypeInfoBuilder& builder) const = 0;

    virtual void CleanupOnTerminate() const = 0;
};

UDF_ASSERT_TYPE_SIZE(IUdfModule, 8);

//////////////////////////////////////////////////////////////////////////////
// TRegistrator
//////////////////////////////////////////////////////////////////////////////
class IRegistrator
{
public:
    struct TFlags {
        enum {
            TypesOnly = 0x01,
        };
    };

public:
    virtual ~IRegistrator() = default;

    virtual void AddModule(
            const TStringRef& name,
            TUniquePtr<IUdfModule> module) = 0;
};

UDF_ASSERT_TYPE_SIZE(IRegistrator, 8);

typedef void(*TBackTraceCallback)();

using TRegisterFunctionPtr = void (*)(IRegistrator& registrator, ui32 flags);
using TAbiVersionFunctionPtr = ui32 (*)();
using TBindSymbolsFunctionPtr = void (*)(const TStaticSymbols& symbols);
using TSetBackTraceCallbackPtr = void(*)(TBackTraceCallback callback);

template<typename TModule>
static inline void RegisterHelper(IRegistrator& registrator) {
    TUniquePtr<TModule> ptr(new TModule());
    auto name = ptr->Name();
    registrator.AddModule(name, ptr.Release());
}

template<typename THead1, typename THead2, typename... TTail>
static inline void RegisterHelper(IRegistrator& registrator) {
    RegisterHelper<THead1>(registrator);
    RegisterHelper<THead2, TTail...>(registrator);
}

void SetBackTraceCallbackImpl(TBackTraceCallback callback);


using TUdfModuleWrapper = std::function<std::pair<TStringRef, TUniquePtr<IUdfModule>>()>;

void AddToStaticUdfRegistry(TUdfModuleWrapper&&);

template<typename TModule>
static inline void AddToStaticUdfRegistry() {
    AddToStaticUdfRegistry([]() {
        TUniquePtr<TModule> ptr(new TModule());
        auto name = ptr->Name();
        return TUdfModuleWrapper::result_type(name, ptr.Release());
    });
}

template<typename THead1, typename THead2, typename... TTail>
static inline void AddToStaticUdfRegistry() {
    AddToStaticUdfRegistry<THead1>();
    AddToStaticUdfRegistry<THead2, TTail...>();
}


} // namspace NUdf
} // namspace NYql

extern "C" UDF_API void Register(NYql::NUdf::IRegistrator& registrator, ui32 flags);
extern "C" UDF_API ui32 AbiVersion();
#if defined(_win_) || defined(_darwin_)
extern "C" UDF_API void BindSymbols(const NYql::NUdf::TStaticSymbols& symbols);
#endif
extern "C" UDF_API void SetBackTraceCallback(NYql::NUdf::TBackTraceCallback callback);

namespace NYql {
namespace NUdf {

#ifndef BUILD_UDF

Y_PRAGMA_DIAGNOSTIC_PUSH
Y_PRAGMA_NO_DEPRECATED

inline TStaticSymbols GetStaticSymbols() {
    return {&UdfAllocate, &UdfFree, &UdfTerminate, &UdfRegisterObject, &UdfUnregisterObject
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
        ,&UdfAllocateWithSize, &UdfFreeWithSize
#endif
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
        ,&UdfArrowAllocate, &UdfArrowReallocate, &UdfArrowFree
#endif
    };
}

Y_PRAGMA_DIAGNOSTIC_POP

#endif

}
}