udf_registrator.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #pragma once
  2. #include "udf_version.h"
  3. #include "udf_types.h"
  4. #include "udf_ptr.h"
  5. #include "udf_string.h"
  6. #include "udf_type_size_check.h"
  7. #include "udf_value.h"
  8. #include <functional>
  9. #include <stdarg.h>
  10. #ifdef _win_
  11. # ifdef BUILD_UDF
  12. # define UDF_API __declspec(dllexport)
  13. # else
  14. # define UDF_API __declspec(dllimport)
  15. # endif
  16. #else
  17. # define UDF_API __attribute__ ((visibility("default")))
  18. #endif
  19. #ifdef BUILD_UDF
  20. #define REGISTER_MODULES(...) \
  21. extern "C" UDF_API void Register( \
  22. ::NYql::NUdf::IRegistrator& registrator, ui32 flags) { \
  23. Y_UNUSED(flags); \
  24. ::NYql::NUdf::RegisterHelper<__VA_ARGS__>(registrator); \
  25. } \
  26. extern "C" UDF_API ui32 AbiVersion() { \
  27. return ::NYql::NUdf::CurrentAbiVersion(); \
  28. }\
  29. extern "C" UDF_API void SetBackTraceCallback(::NYql::NUdf::TBackTraceCallback callback) { \
  30. ::NYql::NUdf::SetBackTraceCallbackImpl(callback); \
  31. }
  32. #else
  33. #define REGISTER_MODULES(...) \
  34. namespace { \
  35. struct TYqlStaticUdfRegistrator { \
  36. inline TYqlStaticUdfRegistrator() { \
  37. ::NYql::NUdf::AddToStaticUdfRegistry<__VA_ARGS__>(); \
  38. } \
  39. } YQL_REGISTRATOR; \
  40. }
  41. #endif
  42. namespace NYql {
  43. namespace NUdf {
  44. class IFunctionTypeInfoBuilder;
  45. struct TStaticSymbols {
  46. void* (*UdfAllocateFunc)(ui64 size);
  47. void (*UdfFreeFunc)(const void* mem);
  48. void (*UdfTerminate)(const char* message);
  49. void (*UdfRegisterObject)(TBoxedValue* object);
  50. void (*UdfUnregisterObject)(TBoxedValue* object);
  51. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  52. void* (*UdfAllocateWithSizeFunc)(ui64 size);
  53. void (*UdfFreeWithSizeFunc)(const void* mem, ui64 size);
  54. #endif
  55. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
  56. void* (*UdfArrowAllocateFunc)(ui64 size);
  57. void* (*UdfArrowReallocateFunc)(const void* mem, ui64 prevSize, ui64 size);
  58. void (*UdfArrowFreeFunc)(const void* mem, ui64 size);
  59. #endif
  60. };
  61. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
  62. UDF_ASSERT_TYPE_SIZE(TStaticSymbols, 80);
  63. #elif UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  64. UDF_ASSERT_TYPE_SIZE(TStaticSymbols, 56);
  65. #else
  66. UDF_ASSERT_TYPE_SIZE(TStaticSymbols, 40);
  67. #endif
  68. inline TStaticSymbols GetStaticSymbols();
  69. //////////////////////////////////////////////////////////////////////////////
  70. // IFunctionNamesSink
  71. //////////////////////////////////////////////////////////////////////////////
  72. class IFunctionDescriptor
  73. {
  74. public:
  75. typedef TUniquePtr<IFunctionDescriptor> TPtr;
  76. virtual ~IFunctionDescriptor() = default;
  77. virtual void SetTypeAwareness() = 0;
  78. };
  79. UDF_ASSERT_TYPE_SIZE(IFunctionDescriptor, 8);
  80. //////////////////////////////////////////////////////////////////////////////
  81. // IFunctionNamesSink
  82. //////////////////////////////////////////////////////////////////////////////
  83. class IFunctionsSink
  84. {
  85. public:
  86. virtual ~IFunctionsSink() = default;
  87. virtual IFunctionDescriptor::TPtr Add(const TStringRef& name) = 0;
  88. };
  89. UDF_ASSERT_TYPE_SIZE(IFunctionsSink, 8);
  90. typedef IFunctionsSink IFunctionNamesSink;
  91. //////////////////////////////////////////////////////////////////////////////
  92. // IUdfModule
  93. //////////////////////////////////////////////////////////////////////////////
  94. class IUdfModule
  95. {
  96. public:
  97. struct TFlags {
  98. enum {
  99. TypesOnly = 0x01
  100. };
  101. };
  102. public:
  103. virtual ~IUdfModule() = default;
  104. virtual void GetAllFunctions(IFunctionsSink& sink) const = 0;
  105. virtual void BuildFunctionTypeInfo(
  106. const TStringRef& name,
  107. TType* userType,
  108. const TStringRef& typeConfig,
  109. ui32 flags,
  110. IFunctionTypeInfoBuilder& builder) const = 0;
  111. virtual void CleanupOnTerminate() const = 0;
  112. };
  113. UDF_ASSERT_TYPE_SIZE(IUdfModule, 8);
  114. //////////////////////////////////////////////////////////////////////////////
  115. // TRegistrator
  116. //////////////////////////////////////////////////////////////////////////////
  117. class IRegistrator
  118. {
  119. public:
  120. struct TFlags {
  121. enum {
  122. TypesOnly = 0x01,
  123. };
  124. };
  125. public:
  126. virtual ~IRegistrator() = default;
  127. virtual void AddModule(
  128. const TStringRef& name,
  129. TUniquePtr<IUdfModule> module) = 0;
  130. };
  131. UDF_ASSERT_TYPE_SIZE(IRegistrator, 8);
  132. typedef void(*TBackTraceCallback)();
  133. using TRegisterFunctionPtr = void (*)(IRegistrator& registrator, ui32 flags);
  134. using TAbiVersionFunctionPtr = ui32 (*)();
  135. using TBindSymbolsFunctionPtr = void (*)(const TStaticSymbols& symbols);
  136. using TSetBackTraceCallbackPtr = void(*)(TBackTraceCallback callback);
  137. template<typename TModule>
  138. static inline void RegisterHelper(IRegistrator& registrator) {
  139. TUniquePtr<TModule> ptr(new TModule());
  140. auto name = ptr->Name();
  141. registrator.AddModule(name, ptr.Release());
  142. }
  143. template<typename THead1, typename THead2, typename... TTail>
  144. static inline void RegisterHelper(IRegistrator& registrator) {
  145. RegisterHelper<THead1>(registrator);
  146. RegisterHelper<THead2, TTail...>(registrator);
  147. }
  148. void SetBackTraceCallbackImpl(TBackTraceCallback callback);
  149. using TUdfModuleWrapper = std::function<std::pair<TStringRef, TUniquePtr<IUdfModule>>()>;
  150. void AddToStaticUdfRegistry(TUdfModuleWrapper&&);
  151. template<typename TModule>
  152. static inline void AddToStaticUdfRegistry() {
  153. AddToStaticUdfRegistry([]() {
  154. TUniquePtr<TModule> ptr(new TModule());
  155. auto name = ptr->Name();
  156. return TUdfModuleWrapper::result_type(name, ptr.Release());
  157. });
  158. }
  159. template<typename THead1, typename THead2, typename... TTail>
  160. static inline void AddToStaticUdfRegistry() {
  161. AddToStaticUdfRegistry<THead1>();
  162. AddToStaticUdfRegistry<THead2, TTail...>();
  163. }
  164. } // namspace NUdf
  165. } // namspace NYql
  166. extern "C" UDF_API void Register(NYql::NUdf::IRegistrator& registrator, ui32 flags);
  167. extern "C" UDF_API ui32 AbiVersion();
  168. #if defined(_win_) || defined(_darwin_)
  169. extern "C" UDF_API void BindSymbols(const NYql::NUdf::TStaticSymbols& symbols);
  170. #endif
  171. extern "C" UDF_API void SetBackTraceCallback(NYql::NUdf::TBackTraceCallback callback);
  172. namespace NYql {
  173. namespace NUdf {
  174. #ifndef BUILD_UDF
  175. Y_PRAGMA_DIAGNOSTIC_PUSH
  176. Y_PRAGMA_NO_DEPRECATED
  177. inline TStaticSymbols GetStaticSymbols() {
  178. return {&UdfAllocate, &UdfFree, &UdfTerminate, &UdfRegisterObject, &UdfUnregisterObject
  179. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  180. ,&UdfAllocateWithSize, &UdfFreeWithSize
  181. #endif
  182. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
  183. ,&UdfArrowAllocate, &UdfArrowReallocate, &UdfArrowFree
  184. #endif
  185. };
  186. }
  187. Y_PRAGMA_DIAGNOSTIC_POP
  188. #endif
  189. }
  190. }