mkql_function_registry.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #include "mkql_function_metadata.h"
  3. #include <yql/essentials/public/udf/udf_counter.h>
  4. #include <yql/essentials/public/udf/udf_registrator.h>
  5. #include <yql/essentials/public/udf/udf_type_builder.h>
  6. #include <util/generic/ptr.h>
  7. #include <util/generic/vector.h>
  8. #include <util/generic/maybe.h>
  9. #include <util/generic/hash.h>
  10. #include <util/generic/hash_set.h>
  11. #include <map>
  12. #ifdef _win_
  13. # define MKQL_UDF_LIB_PREFIX ""
  14. # define MKQL_UDF_LIB_SUFFIX ".dll"
  15. #elif defined(_darwin_)
  16. # define MKQL_UDF_LIB_PREFIX "lib"
  17. # define MKQL_UDF_LIB_SUFFIX ".dylib"
  18. #else
  19. # define MKQL_UDF_LIB_PREFIX "lib"
  20. # define MKQL_UDF_LIB_SUFFIX ".so"
  21. #endif
  22. //////////////////////////////////////////////////////////////////////////////
  23. // forward declarations
  24. //////////////////////////////////////////////////////////////////////////////
  25. class IOutputStream;
  26. namespace NKikimr {
  27. namespace NMiniKQL {
  28. class IMutableFunctionRegistry;
  29. class TTypeEnvironment;
  30. struct TFunctionTypeInfo;
  31. class TStatus;
  32. class TType;
  33. }
  34. }
  35. namespace NKikimr {
  36. namespace NMiniKQL {
  37. using TUdfModuleRemappings = THashMap<TString, TString>; // old => new
  38. using TUdfModulePathsMap = THashMap<TString, TString>; // module name => udf path
  39. //////////////////////////////////////////////////////////////////////////////
  40. // IFunctionRegistry
  41. //////////////////////////////////////////////////////////////////////////////
  42. class IFunctionRegistry: public TThrRefBase
  43. {
  44. public:
  45. typedef TIntrusivePtr<IFunctionRegistry> TPtr;
  46. virtual ~IFunctionRegistry() = default;
  47. virtual const IBuiltinFunctionRegistry::TPtr& GetBuiltins() const = 0;
  48. virtual void AllowUdfPatch() = 0;
  49. virtual TStatus FindFunctionTypeInfo(
  50. const TTypeEnvironment& env,
  51. NUdf::ITypeInfoHelper::TPtr typeInfoHelper,
  52. NUdf::ICountersProvider* countersProvider,
  53. const TStringBuf& name,
  54. TType* userType,
  55. const TStringBuf& typeConfig,
  56. ui32 flags,
  57. const NUdf::TSourcePosition& pos,
  58. const NUdf::ISecureParamsProvider* secureParamsProvider,
  59. TFunctionTypeInfo* funcInfo) const = 0;
  60. virtual TMaybe<TString> FindUdfPath(const TStringBuf& moduleName) const = 0;
  61. virtual bool IsLoadedUdfModule(const TStringBuf& moduleName) const = 0;
  62. virtual THashSet<TString> GetAllModuleNames() const = 0;
  63. virtual void PrintInfoTo(IOutputStream& out) const = 0;
  64. virtual void CleanupModulesOnTerminate() const = 0;
  65. virtual TIntrusivePtr<IMutableFunctionRegistry> Clone() const = 0;
  66. struct TFunctionProperties { bool IsTypeAwareness = false; };
  67. typedef std::map<TString, TFunctionProperties> TFunctionsMap;
  68. virtual TFunctionsMap GetModuleFunctions(const TStringBuf& moduleName) const = 0;
  69. virtual bool SupportsSizedAllocators() const = 0;
  70. };
  71. //////////////////////////////////////////////////////////////////////////////
  72. // IMutableFunctionRegistry
  73. //////////////////////////////////////////////////////////////////////////////
  74. class IMutableFunctionRegistry: public IFunctionRegistry
  75. {
  76. public:
  77. virtual void SetBackTraceCallback(NUdf::TBackTraceCallback callback) = 0;
  78. virtual void LoadUdfs(
  79. const TString& libraryPath,
  80. const TUdfModuleRemappings& remmapings,
  81. ui32 flags = 0,
  82. const TString& customUdfPrefix = {},
  83. THashSet<TString>* modules = nullptr) = 0;
  84. virtual void AddModule(
  85. const TStringBuf& libraryPath,
  86. const TStringBuf& moduleName,
  87. NUdf::TUniquePtr<NUdf::IUdfModule> module) = 0;
  88. virtual void SetSystemModulePaths(const TUdfModulePathsMap& paths) = 0;
  89. };
  90. //////////////////////////////////////////////////////////////////////////////
  91. // factories
  92. //////////////////////////////////////////////////////////////////////////////
  93. TIntrusivePtr<IFunctionRegistry> CreateFunctionRegistry(IBuiltinFunctionRegistry::TPtr&& builtins);
  94. TIntrusivePtr<IFunctionRegistry> CreateFunctionRegistry(
  95. NKikimr::NUdf::TBackTraceCallback backtraceCallback,
  96. IBuiltinFunctionRegistry::TPtr&& builtins,
  97. bool allowUdfPatch,
  98. const TVector<TString>& udfsPaths,
  99. ui32 flags = 0); // see NUdf::IRegistrator::TFlags
  100. //////////////////////////////////////////////////////////////////////////////
  101. // helper functions
  102. //////////////////////////////////////////////////////////////////////////////
  103. void FindUdfsInDir(const TString& dirPath, TVector<TString>* paths);
  104. bool SplitModuleAndFuncName(
  105. const TStringBuf& name, TStringBuf& module, TStringBuf& func);
  106. TString FullName(const TStringBuf& module, const TStringBuf& func);
  107. inline TStringBuf ModuleName(const TStringBuf& name) {
  108. TStringBuf moduleName, _;
  109. if (SplitModuleAndFuncName(name, moduleName, _)) {
  110. return moduleName;
  111. }
  112. return name;
  113. }
  114. const TStringBuf StaticModulePrefix(TStringBuf("<static>::"));
  115. void FillStaticModules(IMutableFunctionRegistry& registry);
  116. } // namespace NMiniKQL
  117. } // namespace NKikimr