impl.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <yt/yql/plugin/bridge/interface.h>
  2. #include <yt/yql/plugin/native/plugin.h>
  3. #include <type_traits>
  4. using namespace NYT::NYqlPlugin;
  5. using namespace NYT::NYson;
  6. extern "C" {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. ssize_t BridgeGetAbiVersion()
  9. {
  10. return 0;
  11. }
  12. TBridgeYqlPlugin* BridgeCreateYqlPlugin(const TBridgeYqlPluginOptions* bridgeOptions)
  13. {
  14. static const TYsonString EmptyMap = TYsonString(TString("{}"));
  15. auto operationAttributes = bridgeOptions->OperationAttributes
  16. ? TYsonString(TString(bridgeOptions->OperationAttributes, bridgeOptions->OperationAttributesLength))
  17. : EmptyMap;
  18. auto singletonsConfig = bridgeOptions->SingletonsConfig
  19. ? TYsonString(TString(bridgeOptions->SingletonsConfig, bridgeOptions->SingletonsConfigLength))
  20. : EmptyMap;
  21. TYqlPluginOptions options{
  22. .SingletonsConfig = singletonsConfig,
  23. .GatewayConfig = TYsonString(TStringBuf(bridgeOptions->GatewayConfig, bridgeOptions->GatewayConfigLength)),
  24. .FileStorageConfig = TYsonString(TStringBuf(bridgeOptions->FileStorageConfig, bridgeOptions->FileStorageConfigLength)),
  25. .OperationAttributes = TYsonString(TStringBuf(bridgeOptions->OperationAttributes, bridgeOptions->OperationAttributesLength)),
  26. .YTTokenPath = TString(bridgeOptions->YTTokenPath),
  27. .LogBackend = std::move(*reinterpret_cast<THolder<TLogBackend>*>(bridgeOptions->LogBackend)),
  28. };
  29. auto nativePlugin = CreateYqlPlugin(std::move(options));
  30. return nativePlugin.release();
  31. }
  32. void BridgeFreeYqlPlugin(TBridgeYqlPlugin* plugin)
  33. {
  34. auto* nativePlugin = reinterpret_cast<IYqlPlugin*>(plugin);
  35. delete nativePlugin;
  36. }
  37. void BridgeFreeQueryResult(TBridgeQueryResult* result)
  38. {
  39. delete result->TaskInfo;
  40. delete result->Statistics;
  41. delete result->Plan;
  42. delete result->YsonResult;
  43. delete result->YsonError;
  44. delete result;
  45. }
  46. void FillString(const char*& str, ssize_t& strLength, const std::optional<TString>& original)
  47. {
  48. if (!original) {
  49. str = nullptr;
  50. strLength = 0;
  51. return;
  52. }
  53. char* copy = new char[original->size() + 1];
  54. memcpy(copy, original->data(), original->size() + 1);
  55. str = copy;
  56. strLength = original->size();
  57. }
  58. TBridgeQueryResult* BridgeRun(TBridgeYqlPlugin* plugin, const char* queryId, const char* impersonationUser, const char* queryText, const char* settings, const TBridgeQueryFile* bridgeFiles, int bridgeFileCount)
  59. {
  60. static const auto EmptyMap = TYsonString(TString("{}"));
  61. auto* nativePlugin = reinterpret_cast<IYqlPlugin*>(plugin);
  62. auto* bridgeResult = new TBridgeQueryResult;
  63. std::vector<TQueryFile> files(bridgeFileCount);
  64. for (int index = 0; index < bridgeFileCount; index++) {
  65. const auto& file = bridgeFiles[index];
  66. files.push_back(TQueryFile {
  67. .Name = TStringBuf(file.Name, file.NameLength),
  68. .Content = TStringBuf(file.Content, file.ContentLength),
  69. .Type = file.Type,
  70. });
  71. }
  72. auto result = nativePlugin->Run(
  73. NYT::TGuid::FromString(queryId),
  74. TString(impersonationUser),
  75. TString(queryText),
  76. settings ? TYsonString(TString(settings)) : EmptyMap,
  77. files);
  78. FillString(bridgeResult->YsonResult, bridgeResult->YsonResultLength, result.YsonResult);
  79. FillString(bridgeResult->Plan, bridgeResult->PlanLength, result.Plan);
  80. FillString(bridgeResult->Statistics, bridgeResult->StatisticsLength, result.Statistics);
  81. FillString(bridgeResult->Progress, bridgeResult->ProgressLength, result.Progress);
  82. FillString(bridgeResult->TaskInfo, bridgeResult->TaskInfoLength, result.TaskInfo);
  83. FillString(bridgeResult->YsonError, bridgeResult->YsonErrorLength, result.YsonError);
  84. return bridgeResult;
  85. }
  86. TBridgeQueryResult* BridgeGetProgress(TBridgeYqlPlugin* plugin, const char* queryId)
  87. {
  88. auto* nativePlugin = reinterpret_cast<IYqlPlugin*>(plugin);
  89. auto* bridgeResult = new TBridgeQueryResult;
  90. auto result = nativePlugin->GetProgress(NYT::TGuid::FromString(queryId));
  91. FillString(bridgeResult->Plan, bridgeResult->PlanLength, result.Plan);
  92. FillString(bridgeResult->Progress, bridgeResult->ProgressLength, result.Progress);
  93. return bridgeResult;
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////
  96. // Validate that the all functions from the bridge interface are implemented with proper signatures.
  97. #define XX(function) static_assert(std::is_same_v<decltype(&(function)), TFunc ## function*>);
  98. FOR_EACH_BRIDGE_INTERFACE_FUNCTION(XX)
  99. #undef XX
  100. ////////////////////////////////////////////////////////////////////////////////
  101. } // extern "C"