yql_config_provider.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. #include "yql_config_provider.h"
  2. #include <yql/essentials/providers/common/provider/yql_provider_names.h>
  3. #include <yql/essentials/providers/common/provider/yql_data_provider_impl.h>
  4. #include <yql/essentials/providers/common/proto/gateways_config.pb.h>
  5. #include <yql/essentials/providers/common/provider/yql_provider.h>
  6. #include <yql/essentials/providers/common/activation/yql_activation.h>
  7. #include <yql/essentials/core/expr_nodes/yql_expr_nodes.h>
  8. #include <yql/essentials/core/yql_execution.h>
  9. #include <yql/essentials/core/yql_expr_optimize.h>
  10. #include <yql/essentials/core/yql_expr_type_annotation.h>
  11. #include <yql/essentials/core/type_ann/type_ann_core.h>
  12. #include <yql/essentials/ast/yql_gc_nodes.h>
  13. #include <yql/essentials/utils/log/log.h>
  14. #include <yql/essentials/utils/fetch/fetch.h>
  15. #include <yql/essentials/utils/retry.h>
  16. #include <library/cpp/json/json_reader.h>
  17. #include <util/string/cast.h>
  18. #include <util/generic/hash.h>
  19. #include <util/generic/utility.h>
  20. #include <util/string/builder.h>
  21. #include <vector>
  22. namespace NYql {
  23. namespace {
  24. using namespace NNodes;
  25. class TConfigCallableExecutionTransformer : public TSyncTransformerBase {
  26. public:
  27. TConfigCallableExecutionTransformer(const TTypeAnnotationContext& types)
  28. : Types(types)
  29. {
  30. Y_UNUSED(Types);
  31. }
  32. TStatus DoTransform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) final {
  33. output = input;
  34. YQL_ENSURE(input->Type() == TExprNode::Callable);
  35. if (input->Content() == "Pull") {
  36. auto requireStatus = RequireChild(*input, 0);
  37. if (requireStatus.Level != TStatus::Ok) {
  38. return requireStatus;
  39. }
  40. IDataProvider::TFillSettings fillSettings = NCommon::GetFillSettings(*input);
  41. YQL_ENSURE(fillSettings.Format == IDataProvider::EResultFormat::Yson);
  42. NYson::EYsonFormat ysonFormat = NCommon::GetYsonFormat(fillSettings);
  43. auto nodeToPull = input->Child(0)->Child(0);
  44. if (nodeToPull->IsCallable(ConfReadName)) {
  45. auto key = nodeToPull->Child(2);
  46. auto tag = key->Child(0)->Child(0)->Content();
  47. if (tag == "data_sinks" || tag == "data_sources") {
  48. TStringStream out;
  49. NYson::TYsonWriter writer(&out, ysonFormat);
  50. writer.OnBeginMap();
  51. writer.OnKeyedItem("Data");
  52. writer.OnBeginList();
  53. if (tag == "data_sinks") {
  54. writer.OnListItem();
  55. writer.OnStringScalar(KikimrProviderName);
  56. writer.OnListItem();
  57. writer.OnStringScalar(YtProviderName);
  58. writer.OnListItem();
  59. writer.OnStringScalar(ResultProviderName);
  60. } else if (tag == "data_sources") {
  61. writer.OnListItem();
  62. writer.OnStringScalar(KikimrProviderName);
  63. writer.OnListItem();
  64. writer.OnStringScalar(YtProviderName);
  65. writer.OnListItem();
  66. writer.OnStringScalar(ConfigProviderName);
  67. }
  68. writer.OnEndList();
  69. writer.OnEndMap();
  70. input->SetResult(ctx.NewAtom(input->Pos(), out.Str()));
  71. input->SetState(TExprNode::EState::ExecutionComplete);
  72. return TStatus::Ok;
  73. } else {
  74. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() << "Unsupported tag: " << tag));
  75. return TStatus::Error;
  76. }
  77. }
  78. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() << "Unknown node to pull, type: "
  79. << nodeToPull->Type() << ", content: " << nodeToPull->Content()));
  80. return TStatus::Error;
  81. }
  82. if (input->Content() == ConfReadName) {
  83. auto requireStatus = RequireChild(*input, 0);
  84. if (requireStatus.Level != TStatus::Ok) {
  85. return requireStatus;
  86. }
  87. input->SetState(TExprNode::EState::ExecutionComplete);
  88. input->SetResult(ctx.NewWorld(input->Pos()));
  89. return TStatus::Ok;
  90. }
  91. if (input->Content() == ConfigureName) {
  92. auto requireStatus = RequireChild(*input, 0);
  93. if (requireStatus.Level != TStatus::Ok) {
  94. return requireStatus;
  95. }
  96. input->SetState(TExprNode::EState::ExecutionComplete);
  97. input->SetResult(ctx.NewWorld(input->Pos()));
  98. return TStatus::Ok;
  99. }
  100. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() << "Failed to execute node: " << input->Content()));
  101. return TStatus::Error;
  102. }
  103. void Rewind() final {
  104. }
  105. private:
  106. const TTypeAnnotationContext& Types;
  107. };
  108. class TConfigProvider : public TDataProviderBase {
  109. public:
  110. struct TFunctions {
  111. THashSet<TStringBuf> Names;
  112. TFunctions() {
  113. Names.insert(ConfReadName);
  114. }
  115. };
  116. TConfigProvider(TTypeAnnotationContext& types, const TGatewaysConfig* config, const TString& username, const TAllowSettingPolicy& policy)
  117. : Types(types)
  118. , CoreConfig(config && config->HasYqlCore() ? &config->GetYqlCore() : nullptr)
  119. , Username(username)
  120. , Policy(policy)
  121. {}
  122. TStringBuf GetName() const override {
  123. return ConfigProviderName;
  124. }
  125. bool Initialize(TExprContext& ctx) override {
  126. std::unordered_set<std::string_view> groups;
  127. if (Types.Credentials != nullptr) {
  128. groups.insert(Types.Credentials->GetGroups().begin(), Types.Credentials->GetGroups().end());
  129. }
  130. auto filter = [this, groups = std::move(groups)](const TCoreAttr& attr) {
  131. if (!attr.HasActivation() || !Username) {
  132. return true;
  133. }
  134. if (NConfig::Allow(attr.GetActivation(), Username, groups)) {
  135. Statistics.Entries.emplace_back(TStringBuilder() << "Activation:" << attr.GetName(), 0, 0, 0, 0, 1);
  136. return true;
  137. }
  138. return false;
  139. };
  140. if (CoreConfig) {
  141. TPosition pos;
  142. for (auto& flag: CoreConfig->GetFlags()) {
  143. if (filter(flag)) {
  144. TVector<TStringBuf> args;
  145. for (auto& arg: flag.GetArgs()) {
  146. args.push_back(arg);
  147. }
  148. if (!ApplyFlag(pos, flag.GetName(), args, ctx)) {
  149. return false;
  150. }
  151. }
  152. }
  153. }
  154. return true;
  155. }
  156. bool CollectStatistics(NYson::TYsonWriter& writer, bool totalOnly) override {
  157. if (Statistics.Entries.empty()) {
  158. return false;
  159. }
  160. THashMap<ui32, TOperationStatistics> tmp;
  161. tmp.emplace(Max<ui32>(), Statistics);
  162. NCommon::WriteStatistics(writer, totalOnly, tmp);
  163. return true;
  164. }
  165. bool ValidateParameters(TExprNode& node, TExprContext& ctx, TMaybe<TString>& cluster) override {
  166. if (!EnsureArgsCount(node, 1, ctx)) {
  167. return false;
  168. }
  169. cluster = Nothing();
  170. return true;
  171. }
  172. bool MatchCategory(const TExprNode& node) {
  173. return (node.Child(1)->Child(0)->Content() == ConfigProviderName);
  174. }
  175. bool CanParse(const TExprNode& node) override {
  176. if (ConfigProviderFunctions().contains(node.Content()) ||
  177. node.Content() == ConfigureName)
  178. {
  179. return MatchCategory(node);
  180. }
  181. return false;
  182. }
  183. IGraphTransformer& GetConfigurationTransformer() override {
  184. if (ConfigurationTransformer) {
  185. return *ConfigurationTransformer;
  186. }
  187. ConfigurationTransformer = CreateFunctorTransformer(
  188. [this](const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) -> IGraphTransformer::TStatus {
  189. output = input;
  190. if (ctx.Step.IsDone(TExprStep::Configure)) {
  191. return IGraphTransformer::TStatus::Ok;
  192. }
  193. bool hasPendingEvaluations = false;
  194. TOptimizeExprSettings settings(nullptr);
  195. settings.VisitChanges = true;
  196. auto status = OptimizeExpr(input, output, [&](const TExprNode::TPtr& node, TExprContext& ctx) -> TExprNode::TPtr {
  197. auto res = node;
  198. if (!hasPendingEvaluations && node->Content() == ConfigureName) {
  199. if (!EnsureMinArgsCount(*node, 2, ctx)) {
  200. return {};
  201. }
  202. if (!node->Child(1)->IsCallable("DataSource")) {
  203. return node;
  204. }
  205. if (node->Child(1)->Child(0)->Content() != ConfigProviderName) {
  206. return node;
  207. }
  208. if (!EnsureMinArgsCount(*node, 3, ctx)) {
  209. return {};
  210. }
  211. if (!EnsureAtom(*node->Child(2), ctx)) {
  212. return {};
  213. }
  214. TStringBuf command = node->Child(2)->Content();
  215. if (command.length() && '_' == command[0]) {
  216. ctx.AddError(TIssue(ctx.GetPosition(node->Child(2)->Pos()), "Flags started with underscore are not allowed"));
  217. return {};
  218. }
  219. TVector<TStringBuf> args;
  220. for (size_t i = 3; i < node->ChildrenSize(); ++i) {
  221. if (node->Child(i)->IsCallable("EvaluateAtom")) {
  222. hasPendingEvaluations = true;
  223. return res;
  224. }
  225. if (!EnsureAtom(*node->Child(i), ctx)) {
  226. return {};
  227. }
  228. args.push_back(node->Child(i)->Content());
  229. }
  230. if (!ApplyFlag(ctx.GetPosition(node->Child(2)->Pos()), command, args, ctx)) {
  231. return {};
  232. }
  233. if (command == "PureDataSource") {
  234. if (Types.PureResultDataSource != node->Child(3)->Content()) {
  235. res = ctx.ChangeChild(*node, 3, ctx.RenameNode(*node->Child(3), Types.PureResultDataSource));
  236. }
  237. }
  238. }
  239. return res;
  240. }, ctx, settings);
  241. return status;
  242. });
  243. return *ConfigurationTransformer;
  244. }
  245. IGraphTransformer& GetTypeAnnotationTransformer(bool instantOnly) override {
  246. Y_UNUSED(instantOnly);
  247. if (!TypeAnnotationTransformer) {
  248. TypeAnnotationTransformer = CreateFunctorTransformer(
  249. [&](const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) -> IGraphTransformer::TStatus {
  250. output = input;
  251. if (input->Content() == ConfReadName) {
  252. if (!EnsureWorldType(*input->Child(0), ctx)) {
  253. return IGraphTransformer::TStatus::Error;
  254. }
  255. if (!EnsureSpecificDataSource(*input->Child(1), ConfigProviderName, ctx)) {
  256. return IGraphTransformer::TStatus::Error;
  257. }
  258. auto key = input->Child(2);
  259. if (!key->IsCallable("Key")) {
  260. ctx.AddError(TIssue(ctx.GetPosition(key->Pos()), "Expected key"));
  261. return IGraphTransformer::TStatus::Error;
  262. }
  263. if (key->ChildrenSize() == 0) {
  264. ctx.AddError(TIssue(ctx.GetPosition(key->Pos()), "Empty key is not allowed"));
  265. return IGraphTransformer::TStatus::Error;
  266. }
  267. auto tag = key->Child(0)->Child(0)->Content();
  268. if (key->Child(0)->ChildrenSize() > 1) {
  269. ctx.AddError(TIssue(ctx.GetPosition(key->Child(0)->Pos()), "Only tag must be specified"));
  270. return IGraphTransformer::TStatus::Error;
  271. }
  272. if (key->ChildrenSize() > 1) {
  273. ctx.AddError(TIssue(ctx.GetPosition(key->Pos()), "Too many tags"));
  274. return IGraphTransformer::TStatus::Error;
  275. }
  276. auto fields = input->Child(3);
  277. if (!EnsureTuple(*fields, ctx)) {
  278. return IGraphTransformer::TStatus::Error;
  279. }
  280. if (fields->ChildrenSize() != 0) {
  281. ctx.AddError(TIssue(ctx.GetPosition(fields->Pos()), "Fields tuple must be empty"));
  282. return IGraphTransformer::TStatus::Error;
  283. }
  284. if (!input->Child(3)->GetTypeAnn() || !input->Child(3)->IsComposable()) {
  285. ctx.AddError(TIssue(ctx.GetPosition(input->Child(3)->Pos()), "Expected composable data"));
  286. return IGraphTransformer::TStatus::Error;
  287. }
  288. auto settings = input->Child(4);
  289. if (!EnsureTuple(*settings, ctx)) {
  290. return IGraphTransformer::TStatus::Error;
  291. }
  292. if (settings->ChildrenSize() != 0) {
  293. ctx.AddError(TIssue(ctx.GetPosition(settings->Pos()), "Unsupported settings"));
  294. return IGraphTransformer::TStatus::Error;
  295. }
  296. auto stringAnnotation = ctx.MakeType<TDataExprType>(EDataSlot::String);
  297. auto listOfString = ctx.MakeType<TListExprType>(stringAnnotation);
  298. TTypeAnnotationNode::TListType children;
  299. children.push_back(input->Child(0)->GetTypeAnn());
  300. if (tag == "data_sources" || tag == "data_sinks") {
  301. children.push_back(listOfString);
  302. } else {
  303. ctx.AddError(TIssue(ctx.GetPosition(key->Pos()), TStringBuilder() << "Unknown tag: " << tag));
  304. return IGraphTransformer::TStatus::Error;
  305. }
  306. auto tupleAnn = ctx.MakeType<TTupleExprType>(children);
  307. input->SetTypeAnn(tupleAnn);
  308. return IGraphTransformer::TStatus::Ok;
  309. }
  310. else if (input->Content() == ConfigureName) {
  311. if (!EnsureWorldType(*input->Child(0), ctx)) {
  312. return IGraphTransformer::TStatus::Error;
  313. }
  314. input->SetTypeAnn(input->Child(0)->GetTypeAnn());
  315. return IGraphTransformer::TStatus::Ok;
  316. }
  317. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() << "(Config) Unsupported function: " << input->Content()));
  318. return IGraphTransformer::TStatus::Error;
  319. });
  320. }
  321. return *TypeAnnotationTransformer;
  322. }
  323. TExprNode::TPtr RewriteIO(const TExprNode::TPtr& node, TExprContext& ctx) override {
  324. auto read = node->Child(0);
  325. TString newName;
  326. if (read->Content() == ReadName) {
  327. newName = ConfReadName;
  328. }
  329. else {
  330. YQL_ENSURE(false, "Expected Read!");
  331. }
  332. YQL_CLOG(INFO, ProviderConfig) << "RewriteIO";
  333. auto newRead = ctx.RenameNode(*read, newName);
  334. auto retChildren = node->ChildrenList();
  335. retChildren[0] = newRead;
  336. return ctx.ChangeChildren(*node, std::move(retChildren));
  337. }
  338. bool CanPullResult(const TExprNode& node, TSyncMap& syncList, bool& canRef) override {
  339. Y_UNUSED(syncList);
  340. if (node.IsCallable(RightName)) {
  341. if (node.Child(0)->IsCallable(ConfReadName)) {
  342. canRef = false;
  343. return true;
  344. }
  345. }
  346. return false;
  347. }
  348. bool CanExecute(const TExprNode& node) override {
  349. if (ConfigProviderFunctions().contains(node.Content()) ||
  350. node.Content() == ConfigureName)
  351. {
  352. return MatchCategory(node);
  353. }
  354. return false;
  355. }
  356. IGraphTransformer& GetCallableExecutionTransformer() override {
  357. if (!CallableExecutionTransformer) {
  358. CallableExecutionTransformer = new TConfigCallableExecutionTransformer(Types);
  359. }
  360. return *CallableExecutionTransformer;
  361. }
  362. bool GetDependencies(const TExprNode& node, TExprNode::TListType& children, bool compact) override {
  363. Y_UNUSED(compact);
  364. if (CanExecute(node)) {
  365. children.push_back(node.ChildPtr(0));
  366. }
  367. return false;
  368. }
  369. void WritePullDetails(const TExprNode& node, NYson::TYsonWriter& writer) override {
  370. YQL_ENSURE(node.IsCallable(RightName));
  371. writer.OnKeyedItem("PullOperation");
  372. writer.OnStringScalar(node.Child(0)->Content());
  373. }
  374. TString GetProviderPath(const TExprNode& node) override {
  375. Y_UNUSED(node);
  376. return "config";
  377. }
  378. private:
  379. bool IsSettingAllowed(const TPosition& pos, TStringBuf name, TExprContext& ctx) {
  380. if (Policy && !Policy(name)) {
  381. ctx.AddError(TIssue(pos, TStringBuilder() << "Changing setting " << name << " is not allowed"));
  382. return false;
  383. }
  384. return true;
  385. }
  386. bool ApplyFlag(const TPosition& pos, const TStringBuf name, const TVector<TStringBuf>& args, TExprContext& ctx) {
  387. if (!IsSettingAllowed(pos, name, ctx)) {
  388. return false;
  389. }
  390. if (name == "UnsecureCredential") {
  391. if (!AddCredential(pos, args, ctx)) {
  392. return false;
  393. }
  394. } else if (name == "ImportUdfs") {
  395. if (!ImportUdfs(pos, args, ctx)) {
  396. return false;
  397. }
  398. } else if (name == "AddFileByUrl") {
  399. if (!AddFileByUrl(pos, args, ctx)) {
  400. return false;
  401. }
  402. } else if (name == "SetFileOption") {
  403. if (!SetFileOption(pos, args, ctx)) {
  404. return false;
  405. }
  406. } else if (name == "AddFolderByUrl") {
  407. if (!AddFolderByUrl(pos, args, ctx)) {
  408. return false;
  409. }
  410. } else if (name == "SetPackageVersion") {
  411. if (!SetPackageVersion(pos, args, ctx)) {
  412. return false;
  413. }
  414. }
  415. else if (name == "ValidateUdf") {
  416. if (args.size() != 1) {
  417. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  418. return false;
  419. }
  420. try {
  421. Types.ValidateMode = NKikimr::NUdf::ValidateModeByStr(TString(args[0]));
  422. } catch (const yexception& err) {
  423. ctx.AddError(TIssue(pos, TStringBuilder() << err.AsStrBuf() << ", available modes: " << NKikimr::NUdf::ValidateModeAvailables()));
  424. return false;
  425. }
  426. }
  427. else if (name == "LLVM_OFF") {
  428. if (args.size() != 0) {
  429. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  430. return false;
  431. }
  432. Types.OptLLVM = "OFF";
  433. }
  434. else if (name == "LLVM") {
  435. if (args.size() > 1) {
  436. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  437. return false;
  438. }
  439. Types.OptLLVM = args.empty() ? TString() : TString(args[0]);
  440. }
  441. else if (name == "NodesAllocationLimit") {
  442. if (args.size() != 1) {
  443. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  444. return false;
  445. }
  446. if (!TryFromString(args[0], ctx.NodesAllocationLimit)) {
  447. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  448. return false;
  449. }
  450. }
  451. else if (name == "StringsAllocationLimit") {
  452. if (args.size() != 1) {
  453. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  454. return false;
  455. }
  456. if (!TryFromString(args[0], ctx.StringsAllocationLimit)) {
  457. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  458. return false;
  459. }
  460. }
  461. else if (name == "RepeatTransformLimit") {
  462. if (args.size() != 1) {
  463. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  464. return false;
  465. }
  466. if (!TryFromString(args[0], ctx.RepeatTransformLimit)) {
  467. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  468. return false;
  469. }
  470. }
  471. else if (name == "TypeAnnNodeRepeatLimit") {
  472. if (args.size() != 1) {
  473. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  474. return false;
  475. }
  476. if (!TryFromString(args[0], ctx.TypeAnnNodeRepeatLimit)) {
  477. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  478. return false;
  479. }
  480. }
  481. else if (name == "PureDataSource") {
  482. if (args.size() != 1) {
  483. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  484. return false;
  485. }
  486. auto dataSource = args[0];
  487. if (Find(Types.AvailablePureResultDataSources, dataSource) == Types.AvailablePureResultDataSources.end()) {
  488. ctx.AddError(TIssue(pos, TStringBuilder() << "Unsupported datasource for result provider: " << dataSource));
  489. return false;
  490. }
  491. if (auto p = Types.DataSourceMap.FindPtr(dataSource)) {
  492. if ((*p)->GetName() != dataSource) {
  493. dataSource = (*p)->GetName();
  494. }
  495. } else {
  496. ctx.AddError(TIssue(pos, TStringBuilder() << "Unknown datasource for result provider: " << dataSource));
  497. return false;
  498. }
  499. Types.PureResultDataSource = dataSource;
  500. }
  501. else if (name == "FullResultDataSink") {
  502. if (args.size() != 1) {
  503. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  504. return false;
  505. }
  506. auto dataSink = args[0];
  507. if (auto p = Types.DataSinkMap.FindPtr(dataSink)) {
  508. if ((*p)->GetName() != dataSink) {
  509. dataSink = (*p)->GetName();
  510. }
  511. } else {
  512. ctx.AddError(TIssue(pos, TStringBuilder() << "Unknown datasink for full result provider: " << dataSink));
  513. return false;
  514. }
  515. Types.FullResultDataSink = dataSink;
  516. }
  517. else if (name == "Diagnostics") {
  518. if (args.size() != 0) {
  519. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  520. return false;
  521. }
  522. Types.Diagnostics = true;
  523. }
  524. else if (name == TStringBuf("Warning")) {
  525. if (!SetWarningRule(pos, args, ctx)) {
  526. return false;
  527. }
  528. }
  529. else if (name == "UdfSupportsYield") {
  530. if (args.size() > 1) {
  531. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  532. return false;
  533. }
  534. bool res = true;
  535. if (!args.empty()) {
  536. if (!TryFromString(args[0], res)) {
  537. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected bool, but got: " << args[0]));
  538. return false;
  539. }
  540. }
  541. Types.UdfSupportsYield = res;
  542. }
  543. else if (name == "EvaluateForLimit") {
  544. if (args.size() != 1) {
  545. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  546. return false;
  547. }
  548. if (!TryFromString(args[0], Types.EvaluateForLimit)) {
  549. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  550. return false;
  551. }
  552. }
  553. else if (name == "EvaluateParallelForLimit") {
  554. if (args.size() != 1) {
  555. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  556. return false;
  557. }
  558. if (!TryFromString(args[0], Types.EvaluateParallelForLimit)) {
  559. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  560. return false;
  561. }
  562. }
  563. else if (name == "DisablePullUpFlatMapOverJoin" || name == "PullUpFlatMapOverJoin") {
  564. if (args.size() != 0) {
  565. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  566. return false;
  567. }
  568. Types.PullUpFlatMapOverJoin = (name == "PullUpFlatMapOverJoin");
  569. } else if (name == "DisableFilterPushdownOverJoinOptionalSide" || name == "FilterPushdownOverJoinOptionalSide") {
  570. if (args.size() != 0) {
  571. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  572. return false;
  573. }
  574. Types.FilterPushdownOverJoinOptionalSide = (name == "FilterPushdownOverJoinOptionalSide");
  575. } else if (name == "RotateJoinTree") {
  576. if (args.size() > 1) {
  577. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  578. return false;
  579. }
  580. bool res = true;
  581. if (!args.empty()) {
  582. if (!TryFromString(args[0], res)) {
  583. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected bool, but got: " << args[0]));
  584. return false;
  585. }
  586. }
  587. Types.RotateJoinTree = res;
  588. }
  589. else if (name == "SQL") {
  590. if (args.size() > 1) {
  591. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  592. return false;
  593. }
  594. Types.DeprecatedSQL = (args[0] == "0");
  595. }
  596. else if (name == "DisableConstraintCheck") {
  597. if (args.empty()) {
  598. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at least 1 argument, but got " << args.size()));
  599. return false;
  600. }
  601. for (auto arg: args) {
  602. Types.DisableConstraintCheck.emplace(arg);
  603. }
  604. }
  605. else if (name == "EnableConstraintCheck") {
  606. if (args.empty()) {
  607. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at least 1 argument, but got " << args.size()));
  608. return false;
  609. }
  610. for (auto arg: args) {
  611. Types.DisableConstraintCheck.erase(TString{arg});
  612. }
  613. }
  614. else if (name == "DisableConstraints") {
  615. if (args.empty()) {
  616. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at least 1 argument, but got " << args.size()));
  617. return false;
  618. }
  619. for (auto arg: args) {
  620. ctx.DisabledConstraints.emplace(arg);
  621. }
  622. }
  623. else if (name == "EnableConstraints") {
  624. if (args.empty()) {
  625. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at least 1 argument, but got " << args.size()));
  626. return false;
  627. }
  628. for (auto arg: args) {
  629. ctx.DisabledConstraints.erase(arg);
  630. }
  631. }
  632. else if (name == "UseTableMetaFromGraph") {
  633. if (args.size() > 1) {
  634. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  635. return false;
  636. }
  637. bool res = true;
  638. if (!args.empty()) {
  639. if (!TryFromString(args[0], res)) {
  640. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected bool, but got: " << args[0]));
  641. return false;
  642. }
  643. }
  644. Types.UseTableMetaFromGraph = res;
  645. }
  646. else if (name == "DiscoveryMode") {
  647. if (args.size() != 0) {
  648. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  649. return false;
  650. }
  651. Types.DiscoveryMode = true;
  652. }
  653. else if (name == "EnableSystemColumns") {
  654. if (args.size() != 0) {
  655. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  656. return false;
  657. }
  658. }
  659. else if (name == "UdfIgnoreCase" || name == "UdfStrictCase") {
  660. if (args.size() != 0) {
  661. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  662. return false;
  663. }
  664. if (!Types.UdfIndex) {
  665. ctx.AddError(TIssue(pos, "UdfIndex is not available"));
  666. return false;
  667. }
  668. Types.UdfIndex->SetCaseSentiveSearch(name == "UdfStrictCase");
  669. } else if (name == "DqEngine") {
  670. if (args.size() != 1) {
  671. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  672. return false;
  673. }
  674. auto arg = TString{args[0]};
  675. if (Find(Types.AvailablePureResultDataSources, DqProviderName) == Types.AvailablePureResultDataSources.end() || arg == "disable") {
  676. ; // reserved
  677. } else if (arg == "auto") {
  678. Types.PureResultDataSource = DqProviderName;
  679. Types.ForceDq = false;
  680. } else if (arg == "force") {
  681. Types.PureResultDataSource = DqProviderName;
  682. Types.ForceDq = true;
  683. } else {
  684. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected `disable|auto|force', but got: " << args[0]));
  685. return false;
  686. }
  687. }
  688. else if (name == "IssueCountLimit") {
  689. if (args.size() != 1) {
  690. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  691. return false;
  692. }
  693. size_t limit = 0;
  694. if (!TryFromString(args[0], limit)) {
  695. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected unsigned integer, but got: " << args[0]));
  696. return false;
  697. }
  698. ctx.IssueManager.SetIssueCountLimit(limit);
  699. }
  700. else if (name == "StrictTableProps") {
  701. if (args.size() != 0) {
  702. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  703. return false;
  704. }
  705. Types.StrictTableProps = true;
  706. }
  707. else if (name == "DisableStrictTableProps") {
  708. if (args.size() != 0) {
  709. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  710. return false;
  711. }
  712. Types.StrictTableProps = false;
  713. }
  714. else if (name == "GeobaseDownloadUrl") {
  715. if (args.size() != 1) {
  716. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  717. return false;
  718. }
  719. auto& userDataBlock = (Types.UserDataStorageCrutches[TUserDataKey::File(TStringBuf("/home/geodata6.bin"))] = TUserDataBlock{EUserDataType::URL, {}, TString(args[0]), {}, {}});
  720. userDataBlock.Usage.Set(EUserDataBlockUsage::Path);
  721. }
  722. else if (name == "JsonQueryReturnsJsonDocument" || name == "DisableJsonQueryReturnsJsonDocument") {
  723. if (args.size() != 0) {
  724. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  725. return false;
  726. }
  727. Types.JsonQueryReturnsJsonDocument = (name == "JsonQueryReturnsJsonDocument");
  728. }
  729. else if (name == "OrderedColumns" || name == "DisableOrderedColumns") {
  730. if (args.size() != 0) {
  731. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  732. return false;
  733. }
  734. Types.OrderedColumns = (name == "OrderedColumns");
  735. }
  736. else if (name == "FolderSubDirsLimit") {
  737. if (args.size() != 1) {
  738. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 1 argument, but got " << args.size()));
  739. return false;
  740. }
  741. if (!TryFromString(args[0], Types.FolderSubDirsLimit)) {
  742. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  743. return false;
  744. }
  745. }
  746. else if (name == "YsonCastToString" || name == "DisableYsonCastToString") {
  747. if (args.size() != 0) {
  748. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  749. return false;
  750. }
  751. Types.YsonCastToString = (name == "YsonCastToString");
  752. }
  753. else if (name == "UseBlocks" || name == "DisableUseBlocks") {
  754. if (args.size() != 0) {
  755. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  756. return false;
  757. }
  758. Types.UseBlocks = (name == "UseBlocks");
  759. }
  760. else if (name == "PgEmitAggApply" || name == "DisablePgEmitAggApply") {
  761. if (args.size() != 0) {
  762. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  763. return false;
  764. }
  765. Types.PgEmitAggApply = (name == "PgEmitAggApply");
  766. }
  767. else if (name == "CostBasedOptimizer") {
  768. if (args.size() != 1) {
  769. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  770. return false;
  771. }
  772. if (!TryFromString(args[0], Types.CostBasedOptimizer)) {
  773. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected `disable|pg|native', but got: " << args[0]));
  774. return false;
  775. }
  776. }
  777. else if (name == "_EnableMatchRecognize" || name == "DisableMatchRecognize") {
  778. if (args.size() != 0) {
  779. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  780. return false;
  781. }
  782. Types.MatchRecognize = name == "_EnableMatchRecognize";
  783. }
  784. else if (name == "TimeOrderRecoverDelay") {
  785. if (args.size() != 1) {
  786. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected one argument, but got " << args.size()));
  787. return false;
  788. }
  789. if (!TryFromString(args[0], Types.TimeOrderRecoverDelay)) {
  790. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  791. return false;
  792. }
  793. if (Types.TimeOrderRecoverDelay >= 0) {
  794. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected negative value, but got: " << args[0]));
  795. return false;
  796. }
  797. }
  798. else if (name == "TimeOrderRecoverAhead") {
  799. if (args.size() != 1) {
  800. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected one argument, but got " << args.size()));
  801. return false;
  802. }
  803. if (!TryFromString(args[0], Types.TimeOrderRecoverAhead)) {
  804. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  805. return false;
  806. }
  807. if (Types.TimeOrderRecoverAhead <= 0) {
  808. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected positive value, but got: " << args[0]));
  809. return false;
  810. }
  811. }
  812. else if (name == "TimeOrderRecoverRowLimit") {
  813. if (args.size() != 1) {
  814. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected one argument, but got " << args.size()));
  815. return false;
  816. }
  817. if (!TryFromString(args[0], Types.TimeOrderRecoverRowLimit)) {
  818. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected integer, but got: " << args[0]));
  819. return false;
  820. }
  821. if (Types.TimeOrderRecoverRowLimit == 0) {
  822. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected positive value, but got: " << args[0]));
  823. return false;
  824. }
  825. }
  826. else if (name == "MatchRecognizeStream") {
  827. if (args.size() != 1) {
  828. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  829. return false;
  830. }
  831. const auto& arg = args[0];
  832. if (arg == "disable") {
  833. Types.MatchRecognizeStreaming = EMatchRecognizeStreamingMode::Disable;
  834. } else if (arg == "auto") {
  835. Types.MatchRecognizeStreaming = EMatchRecognizeStreamingMode::Auto;
  836. } else if (arg == "force") {
  837. Types.MatchRecognizeStreaming = EMatchRecognizeStreamingMode::Force;
  838. } else {
  839. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected `disable|auto|force', but got: " << args[0]));
  840. return false;
  841. }
  842. }
  843. else if (name == "BlockEngine") {
  844. if (args.size() != 1) {
  845. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected at most 1 argument, but got " << args.size()));
  846. return false;
  847. }
  848. auto arg = TString{args[0]};
  849. if (!TryFromString(arg, Types.BlockEngineMode)) {
  850. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected `disable|auto|force', but got: " << args[0]));
  851. return false;
  852. }
  853. }
  854. else if (name == "OptimizerFlags") {
  855. for (auto& arg : args) {
  856. if (arg.empty()) {
  857. ctx.AddError(TIssue(pos, "Empty flags are not supported"));
  858. return false;
  859. }
  860. Types.OptimizerFlags.insert(to_lower(ToString(arg)));
  861. }
  862. }
  863. else if (name == "PeepholeFlags") {
  864. for (auto& arg : args) {
  865. if (arg.empty()) {
  866. ctx.AddError(TIssue(pos, "Empty flags are not supported"));
  867. return false;
  868. }
  869. Types.PeepholeFlags.insert(to_lower(ToString(arg)));
  870. }
  871. }
  872. else if (name == "_EnableStreamLookupJoin" || name == "DisableStreamLookupJoin") {
  873. if (args.size() != 0) {
  874. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected no arguments, but got " << args.size()));
  875. return false;
  876. }
  877. Types.StreamLookupJoin = name == "_EnableStreamLookupJoin";
  878. } else if (name == "MaxAggPushdownPredicates") {
  879. if (args.size() != 1) {
  880. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected single numeric argument, but got " << args.size()));
  881. return false;
  882. }
  883. ui32 value;
  884. if (!TryFromString(args[0], value)) {
  885. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected non-negative integer, but got: " << args[0]));
  886. return false;
  887. }
  888. const ui32 hardLimit = 10;
  889. if (value > hardLimit) {
  890. ctx.AddError(TIssue(pos, TStringBuilder() << "Hard limit for setting MaxAggPushdownPredicates is " << hardLimit << ", but got: " << args[0]));
  891. return false;
  892. }
  893. Types.MaxAggPushdownPredicates = value;
  894. } else {
  895. ctx.AddError(TIssue(pos, TStringBuilder() << "Unsupported command: " << name));
  896. return false;
  897. }
  898. return true;
  899. }
  900. bool ImportUdfs(const TPosition& pos, const TVector<TStringBuf>& args, TExprContext& ctx) {
  901. if (args.size() != 1 && args.size() != 2) {
  902. ctx.AddError(TIssue(pos, TStringBuilder()
  903. << "Expected 1 or 2 arguments, but got " << args.size()));
  904. return false;
  905. }
  906. if (Types.DisableNativeUdfSupport) {
  907. ctx.AddError(TIssue(pos, "Native UDF support is disabled"));
  908. return false;
  909. }
  910. // file alias
  911. const auto& fileAlias = args[0];
  912. TString customUdfPrefix = args.size() > 1 ? TString(args[1]) : "";
  913. const auto key = TUserDataStorage::ComposeUserDataKey(fileAlias);
  914. TString errorMessage;
  915. const TUserDataBlock* udfSource = Types.UserDataStorage->FreezeUdfNoThrow(key,
  916. errorMessage,
  917. customUdfPrefix);
  918. if (!udfSource) {
  919. ctx.AddError(TIssue(pos, TStringBuilder() << "Unknown file: " << fileAlias << ", details: " << errorMessage));
  920. return false;
  921. }
  922. IUdfResolver::TImport import;
  923. import.Pos = pos;
  924. import.FileAlias = fileAlias;
  925. import.Block = udfSource;
  926. Types.UdfImports.insert({ TString(fileAlias), import });
  927. return true;
  928. }
  929. bool AddCredential(const TPosition& pos, const TVector<TStringBuf>& args, TExprContext& ctx) {
  930. if (args.size() != 4) {
  931. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 4 arguments, but got " << args.size()));
  932. return false;
  933. }
  934. if (Types.Credentials->FindCredential(args[0])) {
  935. return true;
  936. }
  937. Types.Credentials->AddCredential(TString(args[0]), TCredential(TString(args[1]), TString(args[2]), TString(args[3])));
  938. return true;
  939. }
  940. bool AddFileByUrlImpl(const TStringBuf alias, const TStringBuf url, const TStringBuf token, const TPosition pos, TExprContext& ctx) {
  941. if (url.empty()) {
  942. ctx.AddError(TIssue(pos, TStringBuilder() << "Empty URL for file '" << alias << "'."));
  943. return false;
  944. }
  945. auto key = TUserDataStorage::ComposeUserDataKey(alias);
  946. if (Types.UserDataStorage->ContainsUserDataBlock(key)) {
  947. // Don't overwrite.
  948. return true;
  949. }
  950. TUserDataBlock block;
  951. if (Types.QContext.CanRead()) {
  952. block.Type = EUserDataType::RAW_INLINE_DATA;
  953. } else {
  954. block.Type = EUserDataType::URL;
  955. block.Data = url;
  956. if (token) {
  957. block.UrlToken = token;
  958. }
  959. }
  960. Types.UserDataStorage->AddUserDataBlock(key, block);
  961. return true;
  962. }
  963. bool AddFileByUrl(const TPosition& pos, const TVector<TStringBuf>& args, TExprContext& ctx) {
  964. if (args.size() < 2 || args.size() > 3) {
  965. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 2 or 3 arguments, but got " << args.size()));
  966. return false;
  967. }
  968. TStringBuf token = args.size() == 3 ? args[2] : TStringBuf();
  969. if (token) {
  970. if (auto cred = Types.Credentials->FindCredential(token)) {
  971. token = cred->Content;
  972. } else {
  973. ctx.AddError(TIssue(pos, TStringBuilder() << "Unknown token name '" << token << "'."));
  974. return false;
  975. }
  976. }
  977. return AddFileByUrlImpl(args[0], args[1], token, pos, ctx);
  978. }
  979. bool SetFileOptionImpl(const TStringBuf alias, const TString& key, const TString& value, const TPosition& pos, TExprContext& ctx) {
  980. const auto dataKey = TUserDataStorage::ComposeUserDataKey(alias);
  981. const auto dataBlock = Types.UserDataStorage->FindUserDataBlock(dataKey);
  982. if (!dataBlock) {
  983. ctx.AddError(TIssue(pos, TStringBuilder() << "No such file '" << alias << "'"));
  984. return false;
  985. }
  986. dataBlock->Options[key] = value;
  987. return true;
  988. }
  989. bool SetFileOption(const TPosition& pos, const TVector<TStringBuf>& args, TExprContext& ctx) {
  990. if (args.size() != 3) {
  991. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 3 arguments, but got " << args.size()));
  992. return false;
  993. }
  994. return SetFileOptionImpl(args[0], ToString(args[1]), ToString(args[2]), pos, ctx);
  995. }
  996. bool SetPackageVersion(const TPosition& pos, const TVector<TStringBuf>& args, TExprContext& ctx) {
  997. if (args.size() != 2) {
  998. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 2 arguments, but got " << args.size()));
  999. return false;
  1000. }
  1001. ui32 version = 0;
  1002. if (!TryFromString(args[1], version)) {
  1003. ctx.AddError(TIssue(pos, TStringBuilder() << "Unable to parse package version from " << args[1]));
  1004. return false;
  1005. }
  1006. if (!Types.UdfIndexPackageSet || !Types.UdfIndex) {
  1007. ctx.AddError(TIssue(pos, TStringBuilder() << "UdfIndex is not initialized, unable to set version for package " << args[0]));
  1008. return false;
  1009. }
  1010. if (!Types.UdfIndexPackageSet->AddResourceTo(TString(args[0]), version, Types.UdfIndex)) {
  1011. ctx.AddError(TIssue(pos, TStringBuilder() << "Unable set default version to " << version << " for package " << args[0]));
  1012. return false;
  1013. }
  1014. return true;
  1015. }
  1016. bool DoListSandboxFolder(const TStringBuf url, const TStringBuf token, const TPosition& pos, TExprContext& ctx, NJson::TJsonValue& content) {
  1017. TString urlStr(url);
  1018. if (!url.empty() && url.back() != '/') {
  1019. urlStr += "/";
  1020. }
  1021. const THttpURL& httpUrl = ParseURL(urlStr);
  1022. if (httpUrl.GetHost() != "proxy.sandbox.yandex-team.ru") {
  1023. ctx.AddError(TIssue(pos, TStringBuilder() << "Adding folder by URL is currently supported only for proxy.sandbox.yandex-team.ru. Host " << httpUrl.GetHost() << " is not supported"));
  1024. return false;
  1025. }
  1026. THttpHeaders headers;
  1027. headers.AddHeader("Accept", "application/json");
  1028. if (token) {
  1029. headers.AddHeader("Authorization", TString("OAuth ").append(token));
  1030. }
  1031. auto result = Fetch(httpUrl, headers, TDuration::Seconds(30));
  1032. if (!result) {
  1033. ctx.AddError(TIssue(pos, TStringBuilder() << "Failed to fetch " << url << " for building folder"));
  1034. return false;
  1035. }
  1036. if (result->GetRetCode() != 200) {
  1037. ctx.AddError(TIssue(pos, TStringBuilder() << "Failed to fetch " << url << " for building folder (http code: " << result->GetRetCode() << ")"));
  1038. return false;
  1039. }
  1040. if (!NJson::ReadJsonTree(result->GetStream().ReadAll(), &content)) {
  1041. ctx.AddError(TIssue(pos, TStringBuilder() << "Failed to parse json from " << url << " for building folder"));
  1042. return false;
  1043. }
  1044. return true;
  1045. }
  1046. bool ListSandboxFolder(const TStringBuf url, const TStringBuf token, const TPosition& pos, TExprContext& ctx, NJson::TJsonValue& content) {
  1047. try {
  1048. return WithRetry<std::exception>(3, [&]() {
  1049. return DoListSandboxFolder(url, token, pos, ctx, content);
  1050. }, [&](const auto& e, int attempt, int attemptCount) {
  1051. YQL_CLOG(WARN, ProviderConfig) << "Error in loading sandbox folder " << url << ", attempt " << attempt << "/" << attemptCount << ", details: " << e.what();
  1052. });
  1053. } catch (const std::exception& e) {
  1054. ctx.AddError(TIssue(pos, TStringBuilder() << "Failed to load sandbox folder content from " << url << ", details: " << e.what()));
  1055. return false;
  1056. }
  1057. }
  1058. static TString MakeHttps(const TString& url) {
  1059. if (url.StartsWith("http:")) {
  1060. return "https:" + url.substr(5);
  1061. }
  1062. return url;
  1063. }
  1064. bool AddFolderByUrl(const TPosition& pos, const TVector<TStringBuf>& args, TExprContext& ctx) {
  1065. if (args.size() < 2 || args.size() > 3) {
  1066. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 2 or 3 arguments, but got " << args.size()));
  1067. return false;
  1068. }
  1069. TStringBuf token = args.size() == 3 ? args[2] : TStringBuf();
  1070. if (token) {
  1071. if (auto cred = Types.Credentials->FindCredential(token)) {
  1072. token = cred->Content;
  1073. } else {
  1074. ctx.AddError(TIssue(pos, TStringBuilder() << "Unknown token name '" << token << "' for folder."));
  1075. return false;
  1076. }
  1077. } else if (auto cred = Types.Credentials->FindCredential("default_sandbox")) {
  1078. token = cred->Content;
  1079. }
  1080. std::vector<std::pair<TString, TString>> queue;
  1081. queue.emplace_back(args[0], args[1]);
  1082. size_t count = 0;
  1083. while (!queue.empty()) {
  1084. auto [prefix, url] = queue.back();
  1085. queue.pop_back();
  1086. YQL_CLOG(DEBUG, ProviderConfig) << "Listing sandbox folder " << prefix << ": " << url;
  1087. NJson::TJsonValue content;
  1088. if (!ListSandboxFolder(url, token, pos, ctx, content)) {
  1089. return false;
  1090. }
  1091. for (const auto& file : content.GetMap()) {
  1092. const auto& fileAttrs = file.second.GetMap();
  1093. auto fileUrl = fileAttrs.FindPtr("url");
  1094. if (fileUrl) {
  1095. TString type = "REGULAR";
  1096. if (auto t = fileAttrs.FindPtr("type")) {
  1097. type = t->GetString();
  1098. }
  1099. TStringBuilder alias;
  1100. if (!prefix.empty()) {
  1101. alias << prefix << "/";
  1102. }
  1103. alias << file.first;
  1104. if (type == "REGULAR") {
  1105. if (!AddFileByUrlImpl(alias, TStringBuf(MakeHttps(fileUrl->GetString())), token, pos, ctx)) {
  1106. return false;
  1107. }
  1108. } else if (type == "DIRECTORY") {
  1109. queue.emplace_back(alias, fileUrl->GetString());
  1110. if (++count > Types.FolderSubDirsLimit) {
  1111. ctx.AddError(TIssue(pos, TStringBuilder() << "Sandbox resource has too many subfolders. Limit is " << Types.FolderSubDirsLimit));
  1112. return false;
  1113. }
  1114. } else {
  1115. YQL_CLOG(WARN, ProviderConfig) << "Got unknown sandbox item type: " << type << ", name=" << alias;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. return true;
  1121. }
  1122. bool SetWarningRule(const TPosition& pos, const TVector<TStringBuf>& args, TExprContext& ctx) {
  1123. if (args.size() != 2) {
  1124. ctx.AddError(TIssue(pos, TStringBuilder() << "Expected 2 arguments, but got " << args.size()));
  1125. return false;
  1126. }
  1127. TString codePattern = TString{args[0]};
  1128. TString action = TString{args[1]};
  1129. TWarningRule rule;
  1130. TString parseError;
  1131. auto parseResult = TWarningRule::ParseFrom(codePattern, action, rule, parseError);
  1132. switch (parseResult) {
  1133. case TWarningRule::EParseResult::PARSE_OK:
  1134. ctx.IssueManager.AddWarningRule(rule);
  1135. break;
  1136. case TWarningRule::EParseResult::PARSE_PATTERN_FAIL:
  1137. case TWarningRule::EParseResult::PARSE_ACTION_FAIL:
  1138. ctx.AddError(TIssue(pos, parseError));
  1139. break;
  1140. default:
  1141. YQL_ENSURE(false, "Unknown parse result");
  1142. }
  1143. return parseResult == TWarningRule::EParseResult::PARSE_OK;
  1144. }
  1145. private:
  1146. TTypeAnnotationContext& Types;
  1147. TAutoPtr<IGraphTransformer> TypeAnnotationTransformer;
  1148. TAutoPtr<IGraphTransformer> ConfigurationTransformer;
  1149. TAutoPtr<IGraphTransformer> CallableExecutionTransformer;
  1150. const TYqlCoreConfig* CoreConfig;
  1151. TString Username;
  1152. const TAllowSettingPolicy Policy;
  1153. TOperationStatistics Statistics;
  1154. };
  1155. }
  1156. TIntrusivePtr<IDataProvider> CreateConfigProvider(TTypeAnnotationContext& types, const TGatewaysConfig* config, const TString& username,
  1157. const TAllowSettingPolicy& policy)
  1158. {
  1159. return new TConfigProvider(types, config, username, policy);
  1160. }
  1161. const THashSet<TStringBuf>& ConfigProviderFunctions() {
  1162. return Singleton<TConfigProvider::TFunctions>()->Names;
  1163. }
  1164. }