yql_facade_run.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #pragma once
  2. #include <yql/essentials/core/file_storage/defs/downloader.h>
  3. #include <yql/essentials/core/file_storage/file_storage.h>
  4. #include <yql/essentials/core/credentials/yql_credentials.h>
  5. #include <yql/essentials/core/url_lister/interface/url_lister.h>
  6. #include <yql/essentials/core/yql_data_provider.h>
  7. #include <yql/essentials/core/yql_user_data.h>
  8. #include <yql/essentials/core/facade/yql_facade.h>
  9. #include <yql/essentials/core/qplayer/storage/interface/yql_qstorage.h>
  10. #include <library/cpp/getopt/last_getopt.h>
  11. #include <library/cpp/yson/public.h>
  12. #include <library/cpp/logger/priority.h>
  13. #include <util/generic/hash_set.h>
  14. #include <util/generic/hash.h>
  15. #include <util/generic/strbuf.h>
  16. #include <util/generic/string.h>
  17. #include <functional>
  18. namespace NKikimr::NMiniKQL {
  19. class IFunctionRegistry;
  20. }
  21. namespace NYql {
  22. class TFileStorageConfig;
  23. class TGatewaysConfig;
  24. }
  25. namespace NYql::NProto {
  26. class TPgExtensions;
  27. }
  28. namespace NYqlMountConfig {
  29. class TMountConfig;
  30. }
  31. namespace NYql {
  32. enum class ERunMode {
  33. Parse /* "parse" */,
  34. Compile /* "compile" */,
  35. Validate /* "validate" */,
  36. Optimize /* "optimize" */,
  37. Peephole /* "peephole" */,
  38. Lineage /* "lineage" */,
  39. Discover /* "discover" */,
  40. Run /* "run" */,
  41. };
  42. enum class EProgramType {
  43. SExpr /* "s-expr" */,
  44. Sql /* "sql" */,
  45. Pg /* "pg" */,
  46. };
  47. enum class EQPlayerMode {
  48. None /* "none" */,
  49. Capture /* "capture" */,
  50. Replay /* "replay" */,
  51. };
  52. class TFacadeRunOptions {
  53. public:
  54. TFacadeRunOptions();
  55. ~TFacadeRunOptions();
  56. EProgramType ProgramType = EProgramType::SExpr;
  57. NYson::EYsonFormat ResultsFormat = NYson::EYsonFormat::Text;
  58. ERunMode Mode = ERunMode::Run;
  59. TString ProgramFile;
  60. TString ProgramText;
  61. TString User;
  62. TString Token;
  63. ui64 MemLimit = 0;
  64. EQPlayerMode QPlayerMode = EQPlayerMode::None;
  65. TString OperationId;
  66. TQContext QPlayerContext;
  67. THashSet<TString> SqlFlags;
  68. ui16 SyntaxVersion = 1;
  69. bool AnsiLexer = false;
  70. bool TestAntlr4 = false;
  71. bool AssumeYdbOnClusterWithSlash = false;
  72. bool PrintAst = false;
  73. bool FullExpr = false;
  74. bool WithTypes = false;
  75. bool FullStatistics = false;
  76. int Verbosity = TLOG_ERR;
  77. bool ShowLog = false;
  78. bool WithFinalIssues = false;
  79. IOutputStream* TraceOptStream = nullptr;
  80. IOutputStream* ErrStream = &Cerr;
  81. IOutputStream* PlanStream = nullptr;
  82. IOutputStream* ExprStream = nullptr;
  83. IOutputStream* ResultStream = nullptr;
  84. IOutputStream* StatStream = nullptr;
  85. NYql::TUserDataTable DataTable;
  86. TVector<TString> UdfsPaths;
  87. TString Params;
  88. NUdf::EValidateMode ValidateMode = NUdf::EValidateMode::Greedy;
  89. TCredentials::TPtr Credentials = MakeIntrusive<TCredentials>();
  90. THashSet<TString> GatewayTypes;
  91. TString UdfResolverPath;
  92. bool UdfResolverFilterSyscalls = false;
  93. bool ScanUdfs = false;
  94. THolder<NYqlMountConfig::TMountConfig> MountConfig;
  95. THolder<TGatewaysConfig> GatewaysConfig;
  96. THolder<TFileStorageConfig> FsConfig;
  97. THolder<NProto::TPgExtensions> PgExtConfig;
  98. // No command line options for these settings. Should be configured in the inherited class
  99. bool NoDebug = false;
  100. bool PgSupport = true;
  101. bool FailureInjectionSupport = false;
  102. bool UseRepeatableRandomAndTimeProviders = false;
  103. bool UseMetaFromGrpah = false;
  104. bool TestSqlFormat = false;
  105. bool ValidateResultFormat = false;
  106. bool EnableResultPosition = false;
  107. bool EnableCredentials = false;
  108. bool EnableQPlayer = false;
  109. bool OptimizeLibs = true;
  110. void Parse(int argc, const char *argv[]);
  111. void AddOptExtension(std::function<void(NLastGetopt::TOpts& opts)> optExtender) {
  112. OptExtenders_.push_back(std::move(optExtender));
  113. }
  114. void AddOptHandler(std::function<void(const NLastGetopt::TOptsParseResult& res)> optHandler) {
  115. OptHandlers_.push_back(std::move(optHandler));
  116. }
  117. void SetSupportedGateways(std::initializer_list<TString> gateways) {
  118. SupportedGateways_.insert(gateways);
  119. }
  120. void InitLogger();
  121. void PrintInfo(const TString& msg);
  122. private:
  123. std::vector<std::function<void(NLastGetopt::TOpts&)>> OptExtenders_;
  124. std::vector<std::function<void(const NLastGetopt::TOptsParseResult&)>> OptHandlers_;
  125. THashSet<TString> SupportedGateways_;
  126. THolder<IOutputStream> ErrStreamHolder_;
  127. THolder<IOutputStream> PlanStreamHolder_;
  128. THolder<IOutputStream> ExprStreamHolder_;
  129. THolder<IOutputStream> ResultStreamHolder_;
  130. THolder<IOutputStream> StatStreamHolder_;
  131. IQStoragePtr QPlayerStorage_;
  132. };
  133. class TFacadeRunner {
  134. public:
  135. TFacadeRunner(TString name);
  136. ~TFacadeRunner();
  137. int Main(int argc, const char *argv[]);
  138. void AddFsDownloadFactory(std::function<NFS::IDownloaderPtr()> factory) {
  139. FsDownloadFactories_.push_back(std::move(factory));
  140. }
  141. void AddProviderFactory(std::function<NYql::TDataProviderInitializer()> factory) {
  142. ProviderFactories_.push_back(std::move(factory));
  143. }
  144. void AddUrlListerFactory(std::function<IUrlListerPtr()> factory) {
  145. UrlListerFactories_.push_back(std::move(factory));
  146. }
  147. void AddClusterMapping(TString name, TString provider) {
  148. ClusterMapping_[name] = std::move(provider);
  149. }
  150. template <class TPbConfig>
  151. void FillClusterMapping(const TPbConfig& config, const TString& provider) {
  152. for (auto& cluster: config.GetClusterMapping()) {
  153. ClusterMapping_.emplace(to_lower(cluster.GetName()), provider);
  154. }
  155. }
  156. void SetOperationProgressWriter(TOperationProgressWriter writer) {
  157. ProgressWriter_ = std::move(writer);
  158. }
  159. void SetOptPipelineConfigurator(IPipelineConfigurator* configurator) {
  160. OptPipelineConfigurator_ = configurator;
  161. }
  162. void SetPeepholePipelineConfigurator(IPipelineConfigurator* configurator) {
  163. PeepholePipelineConfigurator_ = configurator;
  164. }
  165. TFileStoragePtr GetFileStorage() const {
  166. return FileStorage_;
  167. }
  168. TIntrusivePtr<NKikimr::NMiniKQL::IFunctionRegistry> GetFuncRegistry();
  169. TFacadeRunOptions& GetRunOptions() {
  170. return RunOptions_;
  171. }
  172. protected:
  173. virtual int DoMain(int argc, const char *argv[]);
  174. virtual int DoRun(TProgramFactory& factory);
  175. virtual TProgram::TStatus DoRunProgram(TProgramPtr program);
  176. private:
  177. TString Name_;
  178. std::vector<std::function<NFS::IDownloaderPtr()>> FsDownloadFactories_;
  179. std::vector<std::function<TDataProviderInitializer()>> ProviderFactories_;
  180. std::vector<std::function<IUrlListerPtr()>> UrlListerFactories_;
  181. THashMap<TString, TString> ClusterMapping_;
  182. THolder<TFileStorageConfig> FileStorageConfig_;
  183. TFileStoragePtr FileStorage_;
  184. TIntrusivePtr<NKikimr::NMiniKQL::IFunctionRegistry> FuncRegistry_;
  185. TOperationProgressWriter ProgressWriter_;
  186. IPipelineConfigurator* OptPipelineConfigurator_ = nullptr;
  187. IPipelineConfigurator* PeepholePipelineConfigurator_ = nullptr;
  188. TFacadeRunOptions RunOptions_;
  189. };
  190. } // NYql