mkql_factory.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "mkql_factories.h"
  2. #include "mkql_addmember.h"
  3. #include "mkql_aggrcount.h"
  4. #include "mkql_append.h"
  5. #include "mkql_apply.h"
  6. #include "mkql_block_func.h"
  7. #include "mkql_blocks.h"
  8. #include "mkql_block_agg.h"
  9. #include "mkql_block_coalesce.h"
  10. #include "mkql_block_container.h"
  11. #include "mkql_block_decimal.h"
  12. #include "mkql_block_exists.h"
  13. #include "mkql_block_getelem.h"
  14. #include "mkql_block_if.h"
  15. #include "mkql_block_just.h"
  16. #include "mkql_block_logical.h"
  17. #include "mkql_block_map_join.h"
  18. #include "mkql_block_compress.h"
  19. #include "mkql_block_skiptake.h"
  20. #include "mkql_block_top.h"
  21. #include "mkql_callable.h"
  22. #include "mkql_chain_map.h"
  23. #include "mkql_chain1_map.h"
  24. #include "mkql_chopper.h"
  25. #include "mkql_coalesce.h"
  26. #include "mkql_collect.h"
  27. #include "mkql_combine.h"
  28. #include "mkql_contains.h"
  29. #include "mkql_decimal_div.h"
  30. #include "mkql_decimal_mod.h"
  31. #include "mkql_decimal_mul.h"
  32. #include "mkql_dictitems.h"
  33. #include "mkql_discard.h"
  34. #include "mkql_dynamic_variant.h"
  35. #include "mkql_element.h"
  36. #include "mkql_ensure.h"
  37. #include "mkql_enumerate.h"
  38. #include "mkql_exists.h"
  39. #include "mkql_extend.h"
  40. #include "mkql_filter.h"
  41. #include "mkql_flatmap.h"
  42. #include "mkql_flow.h"
  43. #include "mkql_fold.h"
  44. #include "mkql_fold1.h"
  45. #include "mkql_frombytes.h"
  46. #include "mkql_fromstring.h"
  47. #include "mkql_fromyson.h"
  48. #include "mkql_guess.h"
  49. #include "mkql_group.h"
  50. #include "mkql_heap.h"
  51. #include "mkql_hasitems.h"
  52. #include "mkql_hopping.h"
  53. #include "mkql_if.h"
  54. #include "mkql_ifpresent.h"
  55. #include "mkql_invoke.h"
  56. #include "mkql_iterable.h"
  57. #include "mkql_iterator.h"
  58. #include "mkql_join.h"
  59. #include "mkql_join_dict.h"
  60. #include "mkql_grace_join.h"
  61. #include "mkql_lazy_list.h"
  62. #include "mkql_length.h"
  63. #include "mkql_listfromrange.h"
  64. #include "mkql_logical.h"
  65. #include "mkql_lookup.h"
  66. #include "mkql_map.h"
  67. #include "mkql_mapnext.h"
  68. #include "mkql_map_join.h"
  69. #include "mkql_match_recognize.h"
  70. #include "mkql_multimap.h"
  71. #include "mkql_next_value.h"
  72. #include "mkql_nop.h"
  73. #include "mkql_now.h"
  74. #include "mkql_null.h"
  75. #include "mkql_pickle.h"
  76. #include "mkql_prepend.h"
  77. #include "mkql_queue.h"
  78. #include "mkql_random.h"
  79. #include "mkql_range.h"
  80. #include "mkql_reduce.h"
  81. #include "mkql_removemember.h"
  82. #include "mkql_replicate.h"
  83. #include "mkql_reverse.h"
  84. #include "mkql_round.h"
  85. #include "mkql_scalar_apply.h"
  86. #include "mkql_seq.h"
  87. #include "mkql_size.h"
  88. #include "mkql_skip.h"
  89. #include "mkql_sort.h"
  90. #include "mkql_condense.h"
  91. #include "mkql_condense1.h"
  92. #include "mkql_source.h"
  93. #include "mkql_squeeze_to_list.h"
  94. #include "mkql_switch.h"
  95. #include "mkql_take.h"
  96. #include "mkql_time_order_recover.h"
  97. #include "mkql_timezone.h"
  98. #include "mkql_tobytes.h"
  99. #include "mkql_todict.h"
  100. #include "mkql_toindexdict.h"
  101. #include "mkql_tooptional.h"
  102. #include "mkql_tostring.h"
  103. #include "mkql_udf.h"
  104. #include "mkql_unwrap.h"
  105. #include "mkql_varitem.h"
  106. #include "mkql_visitall.h"
  107. #include "mkql_way.h"
  108. #include "mkql_weakmember.h"
  109. #include "mkql_while.h"
  110. #include "mkql_wide_chain_map.h"
  111. #include "mkql_wide_chopper.h"
  112. #include "mkql_wide_combine.h"
  113. #include "mkql_wide_condense.h"
  114. #include "mkql_wide_filter.h"
  115. #include "mkql_wide_map.h"
  116. #include "mkql_wide_top_sort.h"
  117. #include "mkql_withcontext.h"
  118. #include "mkql_zip.h"
  119. #include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
  120. #include <string_view>
  121. #include <unordered_map>
  122. namespace NKikimr {
  123. namespace NMiniKQL {
  124. IComputationNode* WrapArg(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  125. MKQL_ENSURE(callable.GetInputsCount() == 0, "Expected 0 args");
  126. MKQL_ENSURE(callable.GetType()->IsMergeDisabled(), "Merge mode is not disabled");
  127. return new TExternalCodegeneratorNode(ctx.Mutables, GetValueRepresentation(callable.GetType()->GetReturnType()));
  128. }
  129. IComputationNode* WrapWideFlowArg(TCallable& callable, const TComputationNodeFactoryContext&) {
  130. MKQL_ENSURE(callable.GetInputsCount() == 0, "Expected 0 args");
  131. MKQL_ENSURE(callable.GetType()->IsMergeDisabled(), "Merge mode is not disabled");
  132. return new TWideFlowProxyCodegeneratorNode;
  133. }
  134. using TCallableComputationNodeBuilderMap = std::unordered_map<std::string_view, TCallableComputationNodeBuilder>;
  135. namespace {
  136. struct TCallableComputationNodeBuilderFuncMapFiller {
  137. TCallableComputationNodeBuilderFuncMapFiller()
  138. {}
  139. const TCallableComputationNodeBuilderMap Map = {
  140. {"Append", &WrapAppend},
  141. {"Prepend", &WrapPrepend},
  142. {"Extend", &WrapExtend},
  143. {"OrderedExtend", &WrapOrderedExtend},
  144. {"Arg", &WrapArg},
  145. {"Null", &WrapNull},
  146. {"Fold", &WrapFold},
  147. {"Condense", &WrapCondense},
  148. {"Condense1", &WrapCondense1},
  149. {"Squeeze", &WrapSqueeze},
  150. {"Squeeze1", &WrapSqueeze1},
  151. {"Discard", &WrapDiscard},
  152. {"Fold1", &WrapFold1},
  153. {"Map", &WrapMap},
  154. {"OrderedMap", &WrapMap},
  155. {"MapNext", &WrapMapNext},
  156. {"MultiMap", &WrapMultiMap},
  157. {"FlatMap", &WrapFlatMap},
  158. {"OrderedFlatMap", &WrapFlatMap},
  159. {"ChainMap", &WrapChainMap},
  160. {"Chain1Map", &WrapChain1Map},
  161. {"Filter", &WrapFilter},
  162. {"OrderedFilter", &WrapFilter},
  163. {"TakeWhile", &WrapTakeWhile},
  164. {"SkipWhile", &WrapSkipWhile},
  165. {"TakeWhileInclusive", &WrapTakeWhileInclusive},
  166. {"SkipWhileInclusive", &WrapSkipWhileInclusive},
  167. {"AddMember", WrapComputationBuilder(&AddMember)},
  168. {"Member", &WrapMember},
  169. {"RemoveMember", &WrapRemoveMember},
  170. {"Exists", &WrapExists},
  171. {"Contains", &WrapContains},
  172. {"Lookup", &WrapLookup},
  173. {"ToSortedDict", &WrapToSortedDict},
  174. {"ToHashedDict", &WrapToHashedDict},
  175. {"SqueezeToList", &WrapSqueezeToList},
  176. {"SqueezeToSortedDict", &WrapSqueezeToSortedDict},
  177. {"SqueezeToHashedDict", &WrapSqueezeToHashedDict},
  178. {"NarrowSqueezeToSortedDict", &WrapSqueezeToSortedDict},
  179. {"NarrowSqueezeToHashedDict", &WrapSqueezeToHashedDict},
  180. {"Coalesce", &WrapCoalesce},
  181. {"ToOptional", &WrapHead},
  182. {"Head", &WrapHead},
  183. {"Last", &WrapLast},
  184. {"Unwrap", &WrapUnwrap},
  185. {"Ensure", &WrapEnsure},
  186. {"If", &WrapIf},
  187. {"IfPresent", &WrapIfPresent},
  188. {"And", &WrapAnd},
  189. {"Or", &WrapOr},
  190. {"Xor", &WrapXor},
  191. {"Not", &WrapNot},
  192. {"Zip", &WrapZip<false>},
  193. {"ZipAll", &WrapZip<true>},
  194. {"WithContext", &WrapWithContext},
  195. {"Reduce", &WrapReduce},
  196. {"Length", &WrapLength},
  197. {"Iterable", &WrapIterable},
  198. {"Iterator", &WrapIterator},
  199. {"EmptyIterator", &WrapEmptyIterator},
  200. {"ForwardList", &WrapForwardList},
  201. {"Switch", &WrapSwitch},
  202. {"Collect", &WrapCollect},
  203. {"ListFromRange", &WrapListFromRange},
  204. {"HasItems", &WrapHasItems},
  205. {"Reverse", &WrapReverse},
  206. {"Skip", &WrapSkip},
  207. {"Take", &WrapTake},
  208. {"Replicate", &WrapReplicate},
  209. {"Invoke", &WrapInvoke},
  210. {"Udf", &WrapUdf},
  211. {"ScriptUdf", &WrapScriptUdf},
  212. {"Apply", &WrapApply},
  213. {"Apply2", &WrapApply},
  214. {"Callable", &WrapCallable},
  215. {"Size", &WrapSize},
  216. {"ToString", &WrapToString},
  217. {"FromString", &WrapFromString},
  218. {"StrictFromString", &WrapStrictFromString},
  219. {"Enumerate", &WrapEnumerate},
  220. {"Sort", &WrapSort},
  221. {"UnstableSort", &WrapUnstableSort},
  222. {"DictItems", &WrapDictItems},
  223. {"DictKeys", &WrapDictKeys},
  224. {"DictPayloads", &WrapDictPayloads},
  225. {"Nth", &WrapNth},
  226. {"ToIndexDict", &WrapToIndexDict},
  227. {"JoinDict", &WrapJoinDict},
  228. {"GraceJoin", &WrapGraceJoin},
  229. {"GraceSelfJoin", &WrapGraceSelfJoin},
  230. {"GraceJoinWithSpilling", &WrapGraceJoin},
  231. {"GraceSelfJoinWithSpilling", &WrapGraceSelfJoin},
  232. {"MapJoinCore", &WrapMapJoinCore},
  233. {"CommonJoinCore", &WrapCommonJoinCore},
  234. {"CombineCore", &WrapCombineCore},
  235. {"GroupingCore", &WrapGroupingCore},
  236. {"HoppingCore", &WrapHoppingCore},
  237. {"ToBytes", &WrapToBytes},
  238. {"FromBytes", &WrapFromBytes},
  239. {"NewMTRand", &WrapNewMTRand},
  240. {"NextMTRand", &WrapNextMTRand},
  241. {"Random", &WrapRandom<ERandom::Double>},
  242. {"RandomNumber", &WrapRandom<ERandom::Number>},
  243. {"RandomUuid", &WrapRandom<ERandom::Uuid>},
  244. {"Now", &WrapNow},
  245. {"Pickle", &WrapPickle},
  246. {"StablePickle", &WrapStablePickle},
  247. {"Unpickle", &WrapUnpickle},
  248. {"Ascending", &WrapAscending},
  249. {"Descending", &WrapDescending},
  250. {"Guess", &WrapGuess},
  251. {"VariantItem", &WrapVariantItem},
  252. {"Way", &WrapWay},
  253. {"VisitAll", &WrapVisitAll},
  254. {"DynamicVariant", &WrapDynamicVariant},
  255. {"AggrCountInit", &WrapAggrCountInit},
  256. {"AggrCountUpdate", &WrapAggrCountUpdate},
  257. {"QueueCreate", &WrapQueueCreate},
  258. {"QueuePush", &WrapQueuePush},
  259. {"QueuePop", &WrapQueuePop},
  260. {"QueuePeek", &WrapQueuePeek},
  261. {"QueueRange", &WrapQueueRange},
  262. {"Seq", &WrapSeq},
  263. {"PreserveStream", &WrapPreserveStream},
  264. {"FromYsonSimpleType", &WrapFromYsonSimpleType},
  265. {"TryWeakMemberFromDict", &WrapTryWeakMemberFromDict},
  266. {"TimezoneId", &WrapTimezoneId},
  267. {"TimezoneName", &WrapTimezoneName},
  268. {"AddTimezone", &WrapAddTimezone},
  269. {"DecimalDiv", &WrapDecimalDiv},
  270. {"DecimalMod", &WrapDecimalMod},
  271. {"DecimalMul", &WrapDecimalMul},
  272. {"ToFlow", &WrapToFlow},
  273. {"FromFlow", &WrapFromFlow},
  274. {"ToBlocks", &WrapToBlocks},
  275. {"WideToBlocks", &WrapWideToBlocks},
  276. {"BlockFunc", &WrapBlockFunc},
  277. {"FromBlocks", &WrapFromBlocks},
  278. {"WideFromBlocks", &WrapWideFromBlocks},
  279. {"WideSkipBlocks", &WrapWideSkipBlocks},
  280. {"WideTakeBlocks", &WrapWideTakeBlocks},
  281. {"WideTopBlocks", &WrapWideTopBlocks},
  282. {"WideTopSortBlocks", &WrapWideTopSortBlocks},
  283. {"WideSortBlocks", &WrapWideSortBlocks},
  284. {"AsScalar", &WrapAsScalar},
  285. {"ReplicateScalar", &WrapReplicateScalar},
  286. {"BlockCoalesce", &WrapBlockCoalesce},
  287. {"BlockExists", &WrapBlockExists},
  288. {"BlockIf", &WrapBlockIf},
  289. {"BlockAnd", &WrapBlockAnd},
  290. {"BlockOr", &WrapBlockOr},
  291. {"BlockXor", &WrapBlockXor},
  292. {"BlockNot", &WrapBlockNot},
  293. {"BlockJust", &WrapBlockJust},
  294. {"BlockCompress", &WrapBlockCompress},
  295. {"BlockAsTuple", &WrapBlockAsContainer},
  296. {"BlockAsStruct", &WrapBlockAsContainer},
  297. {"BlockMember", &WrapBlockMember},
  298. {"BlockNth", &WrapBlockNth},
  299. {"BlockExpandChunked", &WrapBlockExpandChunked},
  300. {"BlockCombineAll", &WrapBlockCombineAll},
  301. {"BlockCombineHashed", &WrapBlockCombineHashed},
  302. {"BlockMergeFinalizeHashed", &WrapBlockMergeFinalizeHashed},
  303. {"BlockMergeManyFinalizeHashed", &WrapBlockMergeManyFinalizeHashed},
  304. {"BlockDecimalMul", &WrapBlockDecimalMul},
  305. {"BlockDecimalDiv", &WrapBlockDecimalDiv},
  306. {"BlockDecimalMod", &WrapBlockDecimalMod},
  307. {"ScalarApply", &WrapScalarApply},
  308. {"BlockMapJoinCore", &WrapBlockMapJoinCore},
  309. {"MakeHeap", &WrapMakeHeap},
  310. {"PushHeap", &WrapPushHeap},
  311. {"PopHeap", &WrapPopHeap},
  312. {"SortHeap", &WrapSortHeap},
  313. {"StableSort", &WrapStableSort},
  314. {"NthElement", &WrapNthElement},
  315. {"PartialSort", &WrapPartialSort},
  316. {"KeepTop", &WrapKeepTop},
  317. {"Top", &WrapTop},
  318. {"TopSort", &WrapTopSort},
  319. {"SourceOf", &WrapSourceOf},
  320. {"LazyList", &WrapLazyList},
  321. {"Chopper", &WrapChopper},
  322. {"ExpandMap", &WrapExpandMap},
  323. {"WideMap", &WrapWideMap},
  324. {"WideChain1Map", &WrapWideChain1Map},
  325. {"NarrowMap", &WrapNarrowMap},
  326. {"NarrowFlatMap", &WrapNarrowFlatMap},
  327. {"NarrowMultiMap", &WrapNarrowMultiMap},
  328. {"WideFilter", &WrapWideFilter},
  329. {"WideTakeWhile", &WrapWideTakeWhile},
  330. {"WideSkipWhile", &WrapWideSkipWhile},
  331. {"WideTakeWhileInclusive", &WrapWideTakeWhileInclusive},
  332. {"WideSkipWhileInclusive", &WrapWideSkipWhileInclusive},
  333. {"WideCombiner", &WrapWideCombiner},
  334. {"WideLastCombiner", &WrapWideLastCombiner},
  335. {"WideLastCombinerWithSpilling", &WrapWideLastCombinerWithSpilling},
  336. {"WideCondense1", &WrapWideCondense1},
  337. {"WideChopper", &WrapWideChopper},
  338. {"WideTop", &WrapWideTop},
  339. {"WideTopSort", &WrapWideTopSort},
  340. {"WideSort", &WrapWideSort},
  341. {"WideFlowArg", &WrapWideFlowArg},
  342. {"Source", &WrapSource},
  343. {"RangeCreate", &WrapRangeCreate},
  344. {"RangeUnion", &WrapRangeUnion},
  345. {"RangeIntersect", &WrapRangeIntersect},
  346. {"RangeMultiply", &WrapRangeMultiply},
  347. {"RangeFinalize", &WrapRangeFinalize},
  348. {"RoundUp", &WrapRound},
  349. {"RoundDown", &WrapRound},
  350. {"NextValue", &WrapNextValue},
  351. {"Nop", &WrapNop},
  352. {"MatchRecognizeCore", &WrapMatchRecognizeCore},
  353. {"TimeOrderRecover", WrapComputationBuilder(TimeOrderRecover)}
  354. };
  355. };
  356. }
  357. TComputationNodeFactory GetBuiltinFactory() {
  358. return [](TCallable& callable, const TComputationNodeFactoryContext& ctx) -> IComputationNode* {
  359. const auto& map = Singleton<TCallableComputationNodeBuilderFuncMapFiller>()->Map;
  360. const auto it = map.find(callable.GetType()->GetName());
  361. if (it == map.end())
  362. return nullptr;
  363. return it->second(callable, ctx);
  364. };
  365. }
  366. TComputationNodeFactory GetCompositeWithBuiltinFactory(TVector<TComputationNodeFactory> factories) {
  367. return [factories = std::move(factories), builtins = GetBuiltinFactory()](TCallable& callable, const TComputationNodeFactoryContext& ctx) -> IComputationNode* {
  368. for (auto& f: factories) {
  369. if (auto res = f(callable, ctx)) {
  370. return res;
  371. }
  372. }
  373. return builtins(callable, ctx);
  374. };
  375. }
  376. }
  377. }