aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yql/plugin/bridge/plugin.cpp
diff options
context:
space:
mode:
authormax42 <max42@yandex-team.com>2023-07-29 00:02:16 +0300
committermax42 <max42@yandex-team.com>2023-07-29 00:02:16 +0300
commit73b89de71748a21e102d27b9f3ed1bf658766cb5 (patch)
tree188bbd2d622fa91cdcbb1b6d6d77fbc84a0646f5 /yt/yql/plugin/bridge/plugin.cpp
parent528e321bcc2a2b67b53aeba58c3bd88305a141ee (diff)
downloadydb-73b89de71748a21e102d27b9f3ed1bf658766cb5.tar.gz
YT-19210: expose YQL shared library for YT.
After this, a new target libyqlplugin.so appears. in open-source cmake build. Diff in open-source YDB repo looks like the following: https://paste.yandex-team.ru/f302bdb4-7ef2-4362-91c7-6ca45f329264
Diffstat (limited to 'yt/yql/plugin/bridge/plugin.cpp')
-rw-r--r--yt/yql/plugin/bridge/plugin.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/yt/yql/plugin/bridge/plugin.cpp b/yt/yql/plugin/bridge/plugin.cpp
new file mode 100644
index 00000000000..52b9f92a01f
--- /dev/null
+++ b/yt/yql/plugin/bridge/plugin.cpp
@@ -0,0 +1,120 @@
+#include "plugin.h"
+
+#include "interface.h"
+
+#include <yt/yql/plugin/plugin.h>
+#include <util/system/dynlib.h>
+
+#include <vector>
+#include <optional>
+
+namespace NYT::NYqlPlugin {
+namespace NBridge {
+
+////////////////////////////////////////////////////////////////////////////////
+
+class TDynamicYqlPlugin
+{
+public:
+ TDynamicYqlPlugin(std::optional<TString> yqlPluginSharedLibrary)
+ {
+ const TString DefaultYqlPluginLibraryName = "./libyqlplugin.so";
+ auto sharedLibraryPath = yqlPluginSharedLibrary.value_or(DefaultYqlPluginLibraryName);
+ Library_.Open(sharedLibraryPath.data());
+ #define XX(function) function = reinterpret_cast<TFunc ## function*>(Library_.Sym(#function));
+ FOR_EACH_BRIDGE_INTERFACE_FUNCTION(XX);
+ #undef XX
+ }
+
+protected:
+ #define XX(function) TFunc ## function* function;
+ FOR_EACH_BRIDGE_INTERFACE_FUNCTION(XX)
+ #undef XX
+
+ TDynamicLibrary Library_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+class TYqlPlugin
+ : public TDynamicYqlPlugin
+ , public IYqlPlugin
+{
+public:
+ explicit TYqlPlugin(TYqlPluginOptions& options)
+ : TDynamicYqlPlugin(options.YqlPluginSharedLibrary)
+ {
+ std::vector<TBridgeYqlPluginOptions::TBridgeCluster> bridgeClusters;
+ for (const auto& [cluster, proxy]: options.Clusters) {
+ bridgeClusters.push_back({
+ .Cluster = cluster.data(),
+ .Proxy = proxy.data(),
+ });
+ }
+
+ const char* operationAttributes = options.OperationAttributes
+ ? options.OperationAttributes.ToString().data()
+ : nullptr;
+
+ const char* defaultCluster = options.DefaultCluster
+ ? options.DefaultCluster->data()
+ : nullptr;
+
+ TBridgeYqlPluginOptions bridgeOptions {
+ .MRJobBinary = options.MRJobBinary.data(),
+ .UdfDirectory = options.UdfDirectory.data(),
+ .ClusterCount = static_cast<int>(bridgeClusters.size()),
+ .Clusters = bridgeClusters.data(),
+ .DefaultCluster = defaultCluster,
+ .OperationAttributes = operationAttributes,
+ .YTTokenPath = options.YTTokenPath.data(),
+ .LogBackend = &options.LogBackend,
+ };
+
+ BridgePlugin_ = BridgeCreateYqlPlugin(&bridgeOptions);
+ }
+
+ TQueryResult Run(TString impersonationUser, TString queryText, NYson::TYsonString settings) noexcept override
+ {
+ const char* settingsData = settings ? settings.ToString().data() : nullptr;
+ auto* bridgeQueryResult = BridgeRun(BridgePlugin_, impersonationUser.data(), queryText.data(), settingsData);
+ auto toString = [] (const char* str, size_t strLength) -> std::optional<TString> {
+ if (!str) {
+ return std::nullopt;
+ }
+ return TString(str, strLength);
+ };
+ TQueryResult queryResult = {
+ .YsonResult = toString(bridgeQueryResult->YsonResult, bridgeQueryResult->YsonResultLength),
+ .Plan = toString(bridgeQueryResult->Plan, bridgeQueryResult->PlanLength),
+ .Statistics = toString(bridgeQueryResult->Statistics, bridgeQueryResult->StatisticsLength),
+ .TaskInfo = toString(bridgeQueryResult->TaskInfo, bridgeQueryResult->TaskInfoLength),
+ .YsonError = toString(bridgeQueryResult->YsonError, bridgeQueryResult->YsonErrorLength),
+ };
+ BridgeFreeQueryResult(bridgeQueryResult);
+ return queryResult;
+ }
+
+ ~TYqlPlugin() override
+ {
+ BridgeFreeYqlPlugin(BridgePlugin_);
+ }
+
+private:
+ TBridgeYqlPlugin* BridgePlugin_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NBridge
+
+////////////////////////////////////////////////////////////////////////////////
+
+std::unique_ptr<IYqlPlugin> CreateYqlPlugin(TYqlPluginOptions& options) noexcept
+{
+ return std::make_unique<NBridge::TYqlPlugin>(options);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT::NYqlPlugin::NBridge