mkql_node_visitor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. #include "mkql_node_visitor.h"
  2. #include "mkql_node.h"
  3. #include <util/generic/algorithm.h>
  4. namespace NKikimr {
  5. namespace NMiniKQL {
  6. using namespace NDetail;
  7. const ui64 IS_NODE_ENTERED = 1;
  8. const ui64 IS_NODE_EXITED = 2;
  9. void TThrowingNodeVisitor::Visit(TTypeType& node) {
  10. Y_UNUSED(node);
  11. ThrowUnexpectedNodeType();
  12. }
  13. void TThrowingNodeVisitor::Visit(TVoidType& node) {
  14. Y_UNUSED(node);
  15. ThrowUnexpectedNodeType();
  16. }
  17. void TThrowingNodeVisitor::Visit(TNullType& node) {
  18. Y_UNUSED(node);
  19. ThrowUnexpectedNodeType();
  20. }
  21. void TThrowingNodeVisitor::Visit(TEmptyListType& node) {
  22. Y_UNUSED(node);
  23. ThrowUnexpectedNodeType();
  24. }
  25. void TThrowingNodeVisitor::Visit(TEmptyDictType& node) {
  26. Y_UNUSED(node);
  27. ThrowUnexpectedNodeType();
  28. }
  29. void TThrowingNodeVisitor::Visit(TDataType& node) {
  30. Y_UNUSED(node);
  31. ThrowUnexpectedNodeType();
  32. }
  33. void TThrowingNodeVisitor::Visit(TPgType& node) {
  34. Y_UNUSED(node);
  35. ThrowUnexpectedNodeType();
  36. }
  37. void TThrowingNodeVisitor::Visit(TStructType& node) {
  38. Y_UNUSED(node);
  39. ThrowUnexpectedNodeType();
  40. }
  41. void TThrowingNodeVisitor::Visit(TListType& node) {
  42. Y_UNUSED(node);
  43. ThrowUnexpectedNodeType();
  44. }
  45. void TThrowingNodeVisitor::Visit(TOptionalType& node) {
  46. Y_UNUSED(node);
  47. ThrowUnexpectedNodeType();
  48. }
  49. void TThrowingNodeVisitor::Visit(TDictType& node) {
  50. Y_UNUSED(node);
  51. ThrowUnexpectedNodeType();
  52. }
  53. void TThrowingNodeVisitor::Visit(TCallableType& node) {
  54. Y_UNUSED(node);
  55. ThrowUnexpectedNodeType();
  56. }
  57. void TThrowingNodeVisitor::Visit(TAnyType& node) {
  58. Y_UNUSED(node);
  59. ThrowUnexpectedNodeType();
  60. }
  61. void TThrowingNodeVisitor::Visit(TTupleType& node) {
  62. Y_UNUSED(node);
  63. ThrowUnexpectedNodeType();
  64. }
  65. void TThrowingNodeVisitor::Visit(TResourceType& node) {
  66. Y_UNUSED(node);
  67. ThrowUnexpectedNodeType();
  68. }
  69. void TThrowingNodeVisitor::Visit(TVariantType& node) {
  70. Y_UNUSED(node);
  71. ThrowUnexpectedNodeType();
  72. }
  73. void TThrowingNodeVisitor::Visit(TVoid& node) {
  74. Y_UNUSED(node);
  75. ThrowUnexpectedNodeType();
  76. }
  77. void TThrowingNodeVisitor::Visit(TNull& node) {
  78. Y_UNUSED(node);
  79. ThrowUnexpectedNodeType();
  80. }
  81. void TThrowingNodeVisitor::Visit(TEmptyList& node) {
  82. Y_UNUSED(node);
  83. ThrowUnexpectedNodeType();
  84. }
  85. void TThrowingNodeVisitor::Visit(TEmptyDict& node) {
  86. Y_UNUSED(node);
  87. ThrowUnexpectedNodeType();
  88. }
  89. void TThrowingNodeVisitor::Visit(TDataLiteral& node) {
  90. Y_UNUSED(node);
  91. ThrowUnexpectedNodeType();
  92. }
  93. void TThrowingNodeVisitor::Visit(TStructLiteral& node) {
  94. Y_UNUSED(node);
  95. ThrowUnexpectedNodeType();
  96. }
  97. void TThrowingNodeVisitor::Visit(TListLiteral& node) {
  98. Y_UNUSED(node);
  99. ThrowUnexpectedNodeType();
  100. }
  101. void TThrowingNodeVisitor::Visit(TOptionalLiteral& node) {
  102. Y_UNUSED(node);
  103. ThrowUnexpectedNodeType();
  104. }
  105. void TThrowingNodeVisitor::Visit(TDictLiteral& node) {
  106. Y_UNUSED(node);
  107. ThrowUnexpectedNodeType();
  108. }
  109. void TThrowingNodeVisitor::Visit(TCallable& node) {
  110. Y_UNUSED(node);
  111. ThrowUnexpectedNodeType();
  112. }
  113. void TThrowingNodeVisitor::Visit(TAny& node) {
  114. Y_UNUSED(node);
  115. ThrowUnexpectedNodeType();
  116. }
  117. void TThrowingNodeVisitor::Visit(TTupleLiteral& node) {
  118. Y_UNUSED(node);
  119. ThrowUnexpectedNodeType();
  120. }
  121. void TThrowingNodeVisitor::Visit(TVariantLiteral& node) {
  122. Y_UNUSED(node);
  123. ThrowUnexpectedNodeType();
  124. }
  125. void TThrowingNodeVisitor::Visit(TStreamType& node) {
  126. Y_UNUSED(node);
  127. ThrowUnexpectedNodeType();
  128. }
  129. void TThrowingNodeVisitor::Visit(TFlowType& node) {
  130. Y_UNUSED(node);
  131. ThrowUnexpectedNodeType();
  132. }
  133. void TThrowingNodeVisitor::Visit(TTaggedType& node) {
  134. Y_UNUSED(node);
  135. ThrowUnexpectedNodeType();
  136. }
  137. void TThrowingNodeVisitor::Visit(TBlockType& node) {
  138. Y_UNUSED(node);
  139. ThrowUnexpectedNodeType();
  140. }
  141. void TThrowingNodeVisitor::Visit(TMultiType& node) {
  142. Y_UNUSED(node);
  143. ThrowUnexpectedNodeType();
  144. }
  145. void TThrowingNodeVisitor::ThrowUnexpectedNodeType() {
  146. THROW yexception() << "Unexpected node type";
  147. }
  148. void TEmptyNodeVisitor::Visit(TTypeType& node) {
  149. Y_UNUSED(node);
  150. }
  151. void TEmptyNodeVisitor::Visit(TVoidType& node) {
  152. Y_UNUSED(node);
  153. }
  154. void TEmptyNodeVisitor::Visit(TNullType& node) {
  155. Y_UNUSED(node);
  156. }
  157. void TEmptyNodeVisitor::Visit(TEmptyListType& node) {
  158. Y_UNUSED(node);
  159. }
  160. void TEmptyNodeVisitor::Visit(TEmptyDictType& node) {
  161. Y_UNUSED(node);
  162. }
  163. void TEmptyNodeVisitor::Visit(TDataType& node) {
  164. Y_UNUSED(node);
  165. }
  166. void TEmptyNodeVisitor::Visit(TPgType& node) {
  167. Y_UNUSED(node);
  168. }
  169. void TEmptyNodeVisitor::Visit(TStructType& node) {
  170. Y_UNUSED(node);
  171. }
  172. void TEmptyNodeVisitor::Visit(TListType& node) {
  173. Y_UNUSED(node);
  174. }
  175. void TEmptyNodeVisitor::Visit(TOptionalType& node) {
  176. Y_UNUSED(node);
  177. }
  178. void TEmptyNodeVisitor::Visit(TDictType& node) {
  179. Y_UNUSED(node);
  180. }
  181. void TEmptyNodeVisitor::Visit(TCallableType& node) {
  182. Y_UNUSED(node);
  183. }
  184. void TEmptyNodeVisitor::Visit(TAnyType& node) {
  185. Y_UNUSED(node);
  186. }
  187. void TEmptyNodeVisitor::Visit(TTupleType& node) {
  188. Y_UNUSED(node);
  189. }
  190. void TEmptyNodeVisitor::Visit(TResourceType& node) {
  191. Y_UNUSED(node);
  192. }
  193. void TEmptyNodeVisitor::Visit(TVariantType& node) {
  194. Y_UNUSED(node);
  195. }
  196. void TEmptyNodeVisitor::Visit(TVoid& node) {
  197. Y_UNUSED(node);
  198. }
  199. void TEmptyNodeVisitor::Visit(TNull& node) {
  200. Y_UNUSED(node);
  201. }
  202. void TEmptyNodeVisitor::Visit(TEmptyList& node) {
  203. Y_UNUSED(node);
  204. }
  205. void TEmptyNodeVisitor::Visit(TEmptyDict& node) {
  206. Y_UNUSED(node);
  207. }
  208. void TEmptyNodeVisitor::Visit(TDataLiteral& node) {
  209. Y_UNUSED(node);
  210. }
  211. void TEmptyNodeVisitor::Visit(TStructLiteral& node) {
  212. Y_UNUSED(node);
  213. }
  214. void TEmptyNodeVisitor::Visit(TListLiteral& node) {
  215. Y_UNUSED(node);
  216. }
  217. void TEmptyNodeVisitor::Visit(TOptionalLiteral& node) {
  218. Y_UNUSED(node);
  219. }
  220. void TEmptyNodeVisitor::Visit(TDictLiteral& node) {
  221. Y_UNUSED(node);
  222. }
  223. void TEmptyNodeVisitor::Visit(TCallable& node) {
  224. Y_UNUSED(node);
  225. }
  226. void TEmptyNodeVisitor::Visit(TAny& node) {
  227. Y_UNUSED(node);
  228. }
  229. void TEmptyNodeVisitor::Visit(TTupleLiteral& node) {
  230. Y_UNUSED(node);
  231. }
  232. void TEmptyNodeVisitor::Visit(TVariantLiteral& node) {
  233. Y_UNUSED(node);
  234. }
  235. void TEmptyNodeVisitor::Visit(TStreamType& node) {
  236. Y_UNUSED(node);
  237. }
  238. void TEmptyNodeVisitor::Visit(TFlowType& node) {
  239. Y_UNUSED(node);
  240. }
  241. void TEmptyNodeVisitor::Visit(TTaggedType& node) {
  242. Y_UNUSED(node);
  243. }
  244. void TEmptyNodeVisitor::Visit(TBlockType& node) {
  245. Y_UNUSED(node);
  246. }
  247. void TEmptyNodeVisitor::Visit(TMultiType& node) {
  248. Y_UNUSED(node);
  249. }
  250. void TExploringNodeVisitor::Visit(TTypeType& node) {
  251. Y_DEBUG_ABORT_UNLESS(node.GetType() == &node);
  252. }
  253. void TExploringNodeVisitor::Visit(TVoidType& node) {
  254. AddChildNode(&node, *node.GetType());
  255. }
  256. void TExploringNodeVisitor::Visit(TNullType& node) {
  257. AddChildNode(&node, *node.GetType());
  258. }
  259. void TExploringNodeVisitor::Visit(TEmptyListType& node) {
  260. AddChildNode(&node, *node.GetType());
  261. }
  262. void TExploringNodeVisitor::Visit(TEmptyDictType& node) {
  263. AddChildNode(&node, *node.GetType());
  264. }
  265. void TExploringNodeVisitor::Visit(TDataType& node) {
  266. AddChildNode(&node, *node.GetType());
  267. }
  268. void TExploringNodeVisitor::Visit(TPgType& node) {
  269. AddChildNode(&node, *node.GetType());
  270. }
  271. void TExploringNodeVisitor::Visit(TStructType& node) {
  272. AddChildNode(&node, *node.GetType());
  273. for (ui32 i = 0, e = node.GetMembersCount(); i < e; ++i) {
  274. AddChildNode(&node, *node.GetMemberType(i));
  275. }
  276. }
  277. void TExploringNodeVisitor::Visit(TListType& node) {
  278. AddChildNode(&node, *node.GetType());
  279. AddChildNode(&node, *node.GetItemType());
  280. }
  281. void TExploringNodeVisitor::Visit(TOptionalType& node) {
  282. AddChildNode(&node, *node.GetType());
  283. AddChildNode(&node, *node.GetItemType());
  284. }
  285. void TExploringNodeVisitor::Visit(TDictType& node) {
  286. AddChildNode(&node, *node.GetType());
  287. AddChildNode(&node, *node.GetKeyType());
  288. AddChildNode(&node, *node.GetPayloadType());
  289. }
  290. void TExploringNodeVisitor::Visit(TCallableType& node) {
  291. AddChildNode(&node, *node.GetType());
  292. AddChildNode(&node, *node.GetReturnType());
  293. for (ui32 i = 0, e = node.GetArgumentsCount(); i < e; ++i) {
  294. AddChildNode(&node, *node.GetArgumentType(i));
  295. }
  296. if (node.GetPayload()) {
  297. AddChildNode(&node, *node.GetPayload());
  298. }
  299. }
  300. void TExploringNodeVisitor::Visit(TAnyType& node) {
  301. AddChildNode(&node, *node.GetType());
  302. }
  303. void TExploringNodeVisitor::Visit(TTupleType& node) {
  304. AddChildNode(&node, *node.GetType());
  305. for (ui32 i = 0, e = node.GetElementsCount(); i < e; ++i) {
  306. AddChildNode(&node, *node.GetElementType(i));
  307. }
  308. }
  309. void TExploringNodeVisitor::Visit(TResourceType& node) {
  310. AddChildNode(&node, *node.GetType());
  311. }
  312. void TExploringNodeVisitor::Visit(TVariantType& node) {
  313. AddChildNode(&node, *node.GetType());
  314. AddChildNode(&node, *node.GetUnderlyingType());
  315. }
  316. void TExploringNodeVisitor::Visit(TVoid& node) {
  317. AddChildNode(&node, *node.GetType());
  318. }
  319. void TExploringNodeVisitor::Visit(TNull& node) {
  320. AddChildNode(&node, *node.GetType());
  321. }
  322. void TExploringNodeVisitor::Visit(TEmptyList& node) {
  323. AddChildNode(&node, *node.GetType());
  324. }
  325. void TExploringNodeVisitor::Visit(TEmptyDict& node) {
  326. AddChildNode(&node, *node.GetType());
  327. }
  328. void TExploringNodeVisitor::Visit(TDataLiteral& node) {
  329. AddChildNode(&node, *node.GetType());
  330. }
  331. void TExploringNodeVisitor::Visit(TStructLiteral& node) {
  332. AddChildNode(&node, *node.GetType());
  333. for (ui32 i = 0, e = node.GetValuesCount(); i < e; ++i) {
  334. AddChildNode(&node, *node.GetValue(i).GetNode());
  335. }
  336. }
  337. void TExploringNodeVisitor::Visit(TListLiteral& node) {
  338. AddChildNode(&node, *node.GetType());
  339. for (ui32 i = 0; i < node.GetItemsCount(); ++i) {
  340. AddChildNode(&node, *node.GetItems()[i].GetNode());
  341. }
  342. }
  343. void TExploringNodeVisitor::Visit(TOptionalLiteral& node) {
  344. AddChildNode(&node, *node.GetType());
  345. if (node.HasItem()) {
  346. AddChildNode(&node, *node.GetItem().GetNode());
  347. }
  348. }
  349. void TExploringNodeVisitor::Visit(TDictLiteral& node) {
  350. AddChildNode(&node, *node.GetType());
  351. for (ui32 i = 0, e = node.GetItemsCount(); i < e; ++i) {
  352. auto item = node.GetItem(i);
  353. AddChildNode(&node, *item.first.GetNode());
  354. AddChildNode(&node, *item.second.GetNode());
  355. }
  356. }
  357. void TExploringNodeVisitor::Visit(TCallable& node) {
  358. AddChildNode(&node, *node.GetType());
  359. for (ui32 i = 0, e = node.GetInputsCount(); i < e; ++i) {
  360. AddChildNode(&node, *node.GetInput(i).GetNode());
  361. }
  362. if (node.HasResult())
  363. AddChildNode(&node, *node.GetResult().GetNode());
  364. }
  365. void TExploringNodeVisitor::Visit(TAny& node) {
  366. AddChildNode(&node, *node.GetType());
  367. if (node.HasItem()) {
  368. AddChildNode(&node, *node.GetItem().GetNode());
  369. }
  370. }
  371. void TExploringNodeVisitor::Visit(TTupleLiteral& node) {
  372. AddChildNode(&node, *node.GetType());
  373. for (ui32 i = 0, e = node.GetValuesCount(); i < e; ++i) {
  374. AddChildNode(&node, *node.GetValue(i).GetNode());
  375. }
  376. }
  377. void TExploringNodeVisitor::Visit(TVariantLiteral& node) {
  378. AddChildNode(&node, *node.GetType());
  379. AddChildNode(&node, *node.GetItem().GetNode());
  380. }
  381. void TExploringNodeVisitor::Visit(TStreamType& node) {
  382. AddChildNode(&node, *node.GetType());
  383. AddChildNode(&node, *node.GetItemType());
  384. }
  385. void TExploringNodeVisitor::Visit(TFlowType& node) {
  386. AddChildNode(&node, *node.GetType());
  387. AddChildNode(&node, *node.GetItemType());
  388. }
  389. void TExploringNodeVisitor::Visit(TTaggedType& node) {
  390. AddChildNode(&node, *node.GetType());
  391. AddChildNode(&node, *node.GetBaseType());
  392. }
  393. void TExploringNodeVisitor::Visit(TBlockType& node) {
  394. AddChildNode(&node, *node.GetType());
  395. AddChildNode(&node, *node.GetItemType());
  396. }
  397. void TExploringNodeVisitor::Visit(TMultiType& node) {
  398. AddChildNode(&node, *node.GetType());
  399. for (ui32 i = 0, e = node.GetElementsCount(); i < e; ++i) {
  400. AddChildNode(&node, *node.GetElementType(i));
  401. }
  402. }
  403. void TExploringNodeVisitor::AddChildNode(TNode* parent, TNode& child) {
  404. Stack->push_back(&child);
  405. if (BuildConsumersMap) {
  406. if (parent != nullptr) {
  407. ConsumersMap[&child].push_back(parent);
  408. } else {
  409. ConsumersMap[&child] = {};
  410. }
  411. }
  412. }
  413. void TExploringNodeVisitor::Clear() {
  414. NodeList.clear();
  415. Stack = nullptr;
  416. ConsumersMap.clear();
  417. }
  418. void TExploringNodeVisitor::Walk(TNode* root, const TTypeEnvironment& env, const std::vector<TNode*>& terminalNodes,
  419. bool buildConsumersMap, size_t nodesCountHint)
  420. {
  421. BuildConsumersMap = buildConsumersMap;
  422. Clear();
  423. if (BuildConsumersMap && nodesCountHint) {
  424. ConsumersMap.reserve(nodesCountHint);
  425. }
  426. Stack = &env.GetNodeStack();
  427. Stack->clear();
  428. AddChildNode(nullptr, *root);
  429. while (!Stack->empty()) {
  430. auto node = Stack->back();
  431. if (node->GetCookie() == 0) {
  432. node->SetCookie(IS_NODE_ENTERED);
  433. if (Find(terminalNodes.begin(), terminalNodes.end(), node) == terminalNodes.end()) {
  434. node->Accept(*this);
  435. }
  436. } else {
  437. if (node->GetCookie() == IS_NODE_ENTERED) {
  438. NodeList.push_back(node);
  439. node->SetCookie(IS_NODE_EXITED);
  440. } else {
  441. Y_ABORT_UNLESS(node->GetCookie() <= IS_NODE_EXITED, "TNode graph should not be reused");
  442. }
  443. Stack->pop_back();
  444. continue;
  445. }
  446. }
  447. for (auto node : NodeList) {
  448. node->SetCookie(0);
  449. }
  450. Stack = nullptr;
  451. }
  452. const std::vector<TNode*>& TExploringNodeVisitor::GetNodes() {
  453. return NodeList;
  454. }
  455. const TExploringNodeVisitor::TNodesVec& TExploringNodeVisitor::GetConsumerNodes(TNode& node) {
  456. Y_ABORT_UNLESS(BuildConsumersMap);
  457. const auto consumers = ConsumersMap.find(&node);
  458. Y_ABORT_UNLESS(consumers != ConsumersMap.cend());
  459. return consumers->second;
  460. }
  461. template <bool InPlace>
  462. TRuntimeNode SinglePassVisitCallablesImpl(TRuntimeNode root, TExploringNodeVisitor& explorer,
  463. const TCallableVisitFuncProvider& funcProvider, const TTypeEnvironment& env, bool& wereChanges)
  464. {
  465. auto& nodes = explorer.GetNodes();
  466. wereChanges = false;
  467. for (TNode* exploredNode : nodes) {
  468. TNode* node;
  469. if (!InPlace) {
  470. node = exploredNode->CloneOnCallableWrite(env);
  471. } else {
  472. node = exploredNode;
  473. node->Freeze(env);
  474. }
  475. if (node->GetType()->IsCallable()) {
  476. auto& callable = static_cast<TCallable&>(*node);
  477. if (!callable.HasResult()) {
  478. const auto& callableType = callable.GetType();
  479. const auto& name = callableType->GetNameStr();
  480. const auto& func = funcProvider(name);
  481. if (func) {
  482. TRuntimeNode result = func(callable, env);
  483. result.Freeze();
  484. if (result.GetNode() != node) {
  485. if (InPlace) {
  486. callable.SetResult(result, env);
  487. wereChanges = true;
  488. } else {
  489. TNode* wrappedResult = TCallable::Create(result, callable.GetType(), env);
  490. exploredNode->SetCookie((ui64)wrappedResult);
  491. }
  492. continue;
  493. }
  494. }
  495. }
  496. }
  497. if (!InPlace && node != exploredNode) {
  498. exploredNode->SetCookie((ui64)node);
  499. }
  500. }
  501. if (!InPlace) {
  502. auto newRoot = (TNode*)root.GetNode()->GetCookie();
  503. if (newRoot) {
  504. root = TRuntimeNode(newRoot, root.IsImmediate());
  505. wereChanges = true;
  506. }
  507. }
  508. root.Freeze();
  509. if (!InPlace) {
  510. for (TNode* exploredNode : nodes) {
  511. exploredNode->SetCookie(0);
  512. }
  513. }
  514. return root;
  515. }
  516. TRuntimeNode SinglePassVisitCallables(TRuntimeNode root, TExploringNodeVisitor& explorer,
  517. const TCallableVisitFuncProvider& funcProvider, const TTypeEnvironment& env, bool inPlace, bool& wereChanges) {
  518. if (inPlace) {
  519. return SinglePassVisitCallablesImpl<true>(root, explorer, funcProvider, env, wereChanges);
  520. } else {
  521. return SinglePassVisitCallablesImpl<false>(root, explorer, funcProvider, env, wereChanges);
  522. }
  523. }
  524. }
  525. }