mkql_program_builder.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. #pragma once
  2. #include "defs.h"
  3. #include "mkql_node.h"
  4. #include "mkql_node_builder.h"
  5. #include "mkql_type_builder.h"
  6. #include <yql/essentials/public/udf/udf_value.h>
  7. #include <yql/essentials/core/sql_types/match_recognize.h>
  8. #include <functional>
  9. namespace NKikimr {
  10. namespace NMiniKQL {
  11. class IFunctionRegistry;
  12. class TBuiltinFunctionRegistry;
  13. constexpr std::string_view RandomMTResource = "MTRand";
  14. constexpr std::string_view ResourceQueuePrefix = "TResourceQueue:";
  15. enum class EJoinKind {
  16. Min = 1,
  17. LeftOnly = 1,
  18. Inner = 2,
  19. RightOnly = 4,
  20. Left = 1 | 2,
  21. Right = 4 | 2,
  22. Exclusion = 1 | 4,
  23. Full = 1 | 2 | 4,
  24. Max = 7,
  25. LeftSemi = 2 | 8 | 0,
  26. RightSemi = 2 | 8 | 16,
  27. SemiMask = 8,
  28. SemiSide = 16,
  29. Cross = 32
  30. };
  31. enum class EDictItems {
  32. Both = 0,
  33. Keys = 1,
  34. Payloads = 2
  35. };
  36. enum class EAnyJoinSettings {
  37. None = 0,
  38. Left = 1,
  39. Right = 2,
  40. Both = 1 | 2,
  41. };
  42. inline EJoinKind GetJoinKind(ui32 kind) {
  43. MKQL_ENSURE(kind >= (ui32)EJoinKind::Min && kind <= (ui32)EJoinKind::Max ||
  44. kind == (ui32)EJoinKind::LeftSemi || kind == (ui32)EJoinKind::RightSemi ||
  45. kind == (ui32)EJoinKind::Cross, "Bad join kind: " << kind);
  46. return (EJoinKind)kind;
  47. }
  48. constexpr bool IsLeftOptional(EJoinKind kind) {
  49. return ((ui32)EJoinKind::RightOnly & (ui32)kind) != 0 && (kind != EJoinKind::RightOnly);
  50. }
  51. constexpr bool IsRightOptional(EJoinKind kind) {
  52. return ((ui32)EJoinKind::LeftOnly & (ui32)kind) != 0 && (kind != EJoinKind::LeftOnly);
  53. }
  54. constexpr bool IsSemiJoin(EJoinKind kind) {
  55. return ((ui32)EJoinKind::SemiMask & (ui32)kind) != 0;
  56. }
  57. inline EAnyJoinSettings GetAnyJoinSettings(ui32 settings) {
  58. MKQL_ENSURE(settings <= (ui32)EAnyJoinSettings::Both, "Bad AnyJoin settings: " << settings);
  59. return (EAnyJoinSettings)settings;
  60. }
  61. inline bool IsAnyJoinLeft(EAnyJoinSettings settings) {
  62. return ((ui32)settings & (ui32)EAnyJoinSettings::Left) != 0;
  63. }
  64. inline bool IsAnyJoinRight(EAnyJoinSettings settings) {
  65. return ((ui32)settings & (ui32)EAnyJoinSettings::Right) != 0;
  66. }
  67. inline void AddAnyJoinSide(EAnyJoinSettings& combined, EAnyJoinSettings value) {
  68. ui32 combinedVal = (ui32)combined;
  69. combinedVal |= (ui32)value;
  70. combined = (EAnyJoinSettings)combinedVal;
  71. }
  72. inline bool HasSpillingFlag(const TCallable& callable) {
  73. return TStringBuf(callable.GetType()->GetName()).EndsWith("WithSpilling"_sb);
  74. }
  75. #define MKQL_SCRIPT_TYPES(xx) \
  76. xx(Unknown, 0, unknown, false) \
  77. xx(Python, 1, python, false) \
  78. xx(Lua, 2, lua, false) \
  79. xx(ArcPython, 3, arcpython, false) \
  80. xx(CustomPython, 4, custompython, true) \
  81. xx(Javascript, 5, javascript, false) \
  82. xx(Python2, 6, python2, false) \
  83. xx(ArcPython2, 7, arcpython2, false) \
  84. xx(CustomPython2, 8, custompython2, true) \
  85. xx(Python3, 9, python3, false) \
  86. xx(ArcPython3, 10, arcpython3, false) \
  87. xx(CustomPython3, 11, custompython3, true) \
  88. xx(SystemPython2, 12, systempython2, false) \
  89. xx(SystemPython3, 13, systempython3, false) \
  90. xx(SystemPython3_8, 14, systempython3_8, false) \
  91. xx(SystemPython3_9, 15, systempython3_9, false) \
  92. xx(SystemPython3_10, 16, systempython3_10, false) \
  93. xx(SystemPython3_11, 17, systempython3_11, false) \
  94. xx(SystemPython3_12, 18, systempython3_12, false) \
  95. xx(SystemPython3_13, 19, systempython3_13, false) \
  96. enum class EScriptType {
  97. MKQL_SCRIPT_TYPES(ENUM_VALUE_GEN)
  98. };
  99. std::string_view ScriptTypeAsStr(EScriptType type);
  100. EScriptType ScriptTypeFromStr(std::string_view str);
  101. bool IsCustomPython(EScriptType type);
  102. bool IsSystemPython(EScriptType type);
  103. EScriptType CanonizeScriptType(EScriptType type);
  104. struct TSwitchInput {
  105. std::vector<ui32> Indicies;
  106. TType* InputType = nullptr;
  107. std::optional<ui32> ResultVariantOffset;
  108. };
  109. struct TAggInfo {
  110. TString Name;
  111. std::vector<ui32> ArgsColumns;
  112. };
  113. std::vector<TType*> ValidateBlockStreamType(const TType* streamType, bool unwrap = true);
  114. std::vector<TType*> ValidateBlockFlowType(const TType* flowType, bool unwrap = true);
  115. class TProgramBuilder : public TTypeBuilder {
  116. public:
  117. TProgramBuilder(const TTypeEnvironment& env, const IFunctionRegistry& functionRegistry, bool voidWithEffects = false);
  118. const TTypeEnvironment& GetTypeEnvironment() const;
  119. const IFunctionRegistry& GetFunctionRegistry() const;
  120. TRuntimeNode Arg(TType* type) const;
  121. TRuntimeNode WideFlowArg(TType* type) const;
  122. //-- literal functions
  123. TRuntimeNode NewVoid();
  124. TRuntimeNode NewNull();
  125. template <typename T, typename = std::enable_if_t<NUdf::TKnownDataType<T>::Result>>
  126. TRuntimeNode NewDataLiteral(T data) const {
  127. return TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod(data), NUdf::TDataType<T>::Id, Env), true);
  128. }
  129. template <typename T, typename = std::enable_if_t<NUdf::TTzDataType<T>::Result>>
  130. TRuntimeNode NewTzDataLiteral(typename NUdf::TDataType<T>::TLayout value, ui16 tzId) const {
  131. auto data = NUdf::TUnboxedValuePod(value);
  132. data.SetTimezoneId(tzId);
  133. return TRuntimeNode(BuildDataLiteral(data, NUdf::TDataType<T>::Id, Env), true);
  134. }
  135. template <NUdf::EDataSlot Type>
  136. TRuntimeNode NewDataLiteral(const NUdf::TStringRef& data) const;
  137. TRuntimeNode NewDecimalLiteral(NYql::NDecimal::TInt128 data, ui8 precision, ui8 scale) const;
  138. TRuntimeNode NewEmptyOptional(TType* optionalOrPgType);
  139. TRuntimeNode NewEmptyOptionalDataLiteral(NUdf::TDataTypeId schemeType);
  140. TRuntimeNode NewOptional(TRuntimeNode data);
  141. TRuntimeNode NewOptional(TType* optionalType, TRuntimeNode data);
  142. TRuntimeNode NewEmptyStruct();
  143. TRuntimeNode NewStruct(const TArrayRef<const std::pair<std::string_view, TRuntimeNode>>& members);
  144. TRuntimeNode NewStruct(TType* structType, const TArrayRef<const std::pair<std::string_view, TRuntimeNode>>& members);
  145. TRuntimeNode NewEmptyList();
  146. TRuntimeNode NewEmptyList(TType* itemType);
  147. TRuntimeNode NewEmptyListOfVoid();
  148. TRuntimeNode NewList(TType* itemType, const TArrayRef<const TRuntimeNode>& items);
  149. TRuntimeNode NewEmptyDict();
  150. TRuntimeNode NewDict(TType* dictType, const TArrayRef<const std::pair<TRuntimeNode, TRuntimeNode>>& items);
  151. TRuntimeNode NewEmptyTuple();
  152. TRuntimeNode NewTuple(TType* tupleType, const TArrayRef<const TRuntimeNode>& elements);
  153. TRuntimeNode NewTuple(const TArrayRef<const TRuntimeNode>& elements);
  154. TRuntimeNode NewVariant(TRuntimeNode item, ui32 tupleIndex, TType* variantType);
  155. TRuntimeNode NewVariant(TRuntimeNode item, const std::string_view& member, TType* variantType);
  156. // generic data transformation, some args could be optional
  157. TRuntimeNode Convert(TRuntimeNode data, TType* type);
  158. TRuntimeNode ToIntegral(TRuntimeNode data, TType* type);
  159. TRuntimeNode ToDecimal(TRuntimeNode data, ui8 precision, ui8 scale);
  160. TRuntimeNode Concat(TRuntimeNode data1, TRuntimeNode data2);
  161. TRuntimeNode AggrConcat(TRuntimeNode data1, TRuntimeNode data2);
  162. TRuntimeNode Substring(TRuntimeNode data, TRuntimeNode start, TRuntimeNode count);
  163. TRuntimeNode Find(TRuntimeNode haystack, TRuntimeNode needle, TRuntimeNode pos);
  164. TRuntimeNode RFind(TRuntimeNode haystack, TRuntimeNode needle, TRuntimeNode pos);
  165. TRuntimeNode StartsWith(TRuntimeNode string, TRuntimeNode prefix);
  166. TRuntimeNode EndsWith(TRuntimeNode string, TRuntimeNode suffix);
  167. TRuntimeNode StringContains(TRuntimeNode string, TRuntimeNode pattern);
  168. TRuntimeNode ByteAt(TRuntimeNode data, TRuntimeNode index);
  169. TRuntimeNode Size(TRuntimeNode data);
  170. template <bool Utf8 = false>
  171. TRuntimeNode ToString(TRuntimeNode data);
  172. TRuntimeNode FromString(TRuntimeNode data, TType* type);
  173. TRuntimeNode StrictFromString(TRuntimeNode data, TType* type);
  174. TRuntimeNode ToBytes(TRuntimeNode data);
  175. TRuntimeNode FromBytes(TRuntimeNode data, TType* type);
  176. TRuntimeNode InversePresortString(TRuntimeNode data);
  177. TRuntimeNode InverseString(TRuntimeNode data);
  178. TRuntimeNode Random(const TArrayRef<const TRuntimeNode>& dependentNodes);
  179. TRuntimeNode RandomNumber(const TArrayRef<const TRuntimeNode>& dependentNodes);
  180. TRuntimeNode RandomUuid(const TArrayRef<const TRuntimeNode>& dependentNodes);
  181. TRuntimeNode Now(const TArrayRef<const TRuntimeNode>& dependentNodes);
  182. TRuntimeNode CurrentUtcDate(const TArrayRef<const TRuntimeNode>& dependentNodes);
  183. TRuntimeNode CurrentUtcDatetime(const TArrayRef<const TRuntimeNode>& dependentNodes);
  184. TRuntimeNode CurrentUtcTimestamp(const TArrayRef<const TRuntimeNode>& dependentNodes);
  185. TRuntimeNode Pickle(TRuntimeNode data);
  186. TRuntimeNode StablePickle(TRuntimeNode data);
  187. TRuntimeNode Unpickle(TType* type, TRuntimeNode serialized);
  188. TRuntimeNode Ascending(TRuntimeNode data);
  189. TRuntimeNode Descending(TRuntimeNode data);
  190. TRuntimeNode ToFlow(TRuntimeNode stream);
  191. TRuntimeNode FromFlow(TRuntimeNode flow);
  192. TRuntimeNode Steal(TRuntimeNode input);
  193. TRuntimeNode ToBlocks(TRuntimeNode flow);
  194. TRuntimeNode WideToBlocks(TRuntimeNode flow);
  195. TRuntimeNode FromBlocks(TRuntimeNode flow);
  196. TRuntimeNode WideFromBlocks(TRuntimeNode flow);
  197. TRuntimeNode WideSkipBlocks(TRuntimeNode flow, TRuntimeNode count);
  198. TRuntimeNode WideTakeBlocks(TRuntimeNode flow, TRuntimeNode count);
  199. TRuntimeNode WideTopBlocks(TRuntimeNode flow, TRuntimeNode count, const std::vector<std::pair<ui32, TRuntimeNode>>& keys);
  200. TRuntimeNode WideTopSortBlocks(TRuntimeNode flow, TRuntimeNode count, const std::vector<std::pair<ui32, TRuntimeNode>>& keys);
  201. TRuntimeNode WideSortBlocks(TRuntimeNode flow, const std::vector<std::pair<ui32, TRuntimeNode>>& keys);
  202. TRuntimeNode AsScalar(TRuntimeNode value);
  203. TRuntimeNode ReplicateScalar(TRuntimeNode value, TRuntimeNode count);
  204. TRuntimeNode BlockCompress(TRuntimeNode flow, ui32 bitmapIndex);
  205. TRuntimeNode BlockExpandChunked(TRuntimeNode flow);
  206. TRuntimeNode BlockCoalesce(TRuntimeNode first, TRuntimeNode second);
  207. TRuntimeNode BlockExists(TRuntimeNode data);
  208. TRuntimeNode BlockMember(TRuntimeNode structure, const std::string_view& memberName);
  209. TRuntimeNode BlockNth(TRuntimeNode tuple, ui32 index);
  210. TRuntimeNode BlockAsStruct(const TArrayRef<std::pair<std::string_view, TRuntimeNode>>& args);
  211. TRuntimeNode BlockAsTuple(const TArrayRef<const TRuntimeNode>& args);
  212. TRuntimeNode BlockToPg(TRuntimeNode input, TType* returnType);
  213. TRuntimeNode BlockFromPg(TRuntimeNode input, TType* returnType);
  214. TRuntimeNode BlockPgResolvedCall(const std::string_view& name, ui32 id,
  215. const TArrayRef<const TRuntimeNode>& args, TType* returnType);
  216. TRuntimeNode BlockMapJoinCore(TRuntimeNode leftStream, TRuntimeNode rightStream, EJoinKind joinKind,
  217. const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& leftKeyDrops,
  218. const TArrayRef<const ui32>& rightKeyColumns, const TArrayRef<const ui32>& rightKeyDrops, bool rightAny, TType* returnType);
  219. //-- logical functions
  220. TRuntimeNode BlockNot(TRuntimeNode data);
  221. TRuntimeNode BlockAnd(TRuntimeNode first, TRuntimeNode second);
  222. TRuntimeNode BlockOr(TRuntimeNode first, TRuntimeNode second);
  223. TRuntimeNode BlockXor(TRuntimeNode first, TRuntimeNode second);
  224. TRuntimeNode BlockIf(TRuntimeNode condition, TRuntimeNode thenBranch, TRuntimeNode elseBranch);
  225. TRuntimeNode BlockJust(TRuntimeNode data);
  226. TRuntimeNode BlockFunc(const std::string_view& funcName, TType* returnType, const TArrayRef<const TRuntimeNode>& args);
  227. TRuntimeNode BlockCombineAll(TRuntimeNode flow, std::optional<ui32> filterColumn,
  228. const TArrayRef<const TAggInfo>& aggs, TType* returnType);
  229. TRuntimeNode BlockCombineHashed(TRuntimeNode flow, std::optional<ui32> filterColumn, const TArrayRef<ui32>& keys,
  230. const TArrayRef<const TAggInfo>& aggs, TType* returnType);
  231. TRuntimeNode BlockMergeFinalizeHashed(TRuntimeNode flow, const TArrayRef<ui32>& keys,
  232. const TArrayRef<const TAggInfo>& aggs, TType* returnType);
  233. TRuntimeNode BlockMergeManyFinalizeHashed(TRuntimeNode flow, const TArrayRef<ui32>& keys,
  234. const TArrayRef<const TAggInfo>& aggs, ui32 streamIndex, const TVector<TVector<ui32>>& streams, TType* returnType);
  235. // udfs
  236. TRuntimeNode Udf(
  237. const std::string_view& funcName,
  238. TRuntimeNode runConfig = TRuntimeNode(),
  239. TType* userType = nullptr,
  240. const std::string_view& typeConfig = std::string_view(""));
  241. TRuntimeNode TypedUdf(
  242. const std::string_view& funcName,
  243. TType* funcType,
  244. TRuntimeNode runConfig = TRuntimeNode(),
  245. TType* userType = nullptr,
  246. const std::string_view& typeConfig = std::string_view(""),
  247. const std::string_view& file = std::string_view(""), ui32 row = 0, ui32 column = 0);
  248. TRuntimeNode ScriptUdf(
  249. const std::string_view& moduleName,
  250. const std::string_view& funcName,
  251. TType* funcType,
  252. TRuntimeNode script,
  253. const std::string_view& file = std::string_view(""), ui32 row = 0, ui32 column = 0);
  254. typedef std::function<TRuntimeNode ()> TZeroLambda;
  255. typedef std::function<TRuntimeNode (TRuntimeNode)> TUnaryLambda;
  256. typedef std::function<TRuntimeNode (TRuntimeNode, TRuntimeNode)> TBinaryLambda;
  257. typedef std::function<TRuntimeNode (TRuntimeNode, TRuntimeNode, TRuntimeNode)> TTernaryLambda;
  258. typedef std::function<TRuntimeNode(const TArrayRef<const TRuntimeNode>& args)> TArrayLambda;
  259. typedef std::function<TRuntimeNodePair (TRuntimeNode)> TUnarySplitLambda;
  260. typedef std::function<TRuntimeNodePair (TRuntimeNode, TRuntimeNode)> TBinarySplitLambda;
  261. typedef std::function<TRuntimeNode::TList (TRuntimeNode)> TExpandLambda;
  262. typedef std::function<TRuntimeNode::TList (TRuntimeNode::TList)> TWideLambda;
  263. typedef std::function<TRuntimeNode::TList (TRuntimeNode::TList, TRuntimeNode::TList)> TBinaryWideLambda;
  264. typedef std::function<TRuntimeNode::TList (TRuntimeNode::TList, TRuntimeNode::TList, TRuntimeNode::TList)> TTernaryWideLambda;
  265. typedef std::function<TRuntimeNode (TRuntimeNode::TList)> TNarrowLambda;
  266. typedef std::function<TRuntimeNode (TRuntimeNode::TList, TRuntimeNode::TList)> TWideSwitchLambda;
  267. TRuntimeNode Apply(TRuntimeNode callableNode, const TArrayRef<const TRuntimeNode>& args, ui32 dependentCount = 0);
  268. TRuntimeNode Apply(TRuntimeNode callableNode, const TArrayRef<const TRuntimeNode>& args,
  269. const std::string_view& file, ui32 row, ui32 column, ui32 dependentCount = 0);
  270. TRuntimeNode Callable(TType* callableType, const TArrayLambda& handler);
  271. //-- struct functions
  272. TRuntimeNode Member(TRuntimeNode structObj, const std::string_view& memberName);
  273. TRuntimeNode Element(TRuntimeNode tuple, const std::string_view& memberName);
  274. TRuntimeNode AddMember(TRuntimeNode structObj, const std::string_view& memberName, TRuntimeNode memberValue);
  275. TRuntimeNode RemoveMember(TRuntimeNode structObj, const std::string_view& memberName, bool forced);
  276. TRuntimeNode RemoveMembers(TRuntimeNode structObj, const TArrayRef<const std::string_view>& members, bool forced);
  277. //-- list functions
  278. TRuntimeNode Append(TRuntimeNode list, TRuntimeNode item);
  279. TRuntimeNode Prepend(TRuntimeNode item, TRuntimeNode list);
  280. TRuntimeNode Extend(const TArrayRef<const TRuntimeNode>& lists);
  281. TRuntimeNode OrderedExtend(const TArrayRef<const TRuntimeNode>& lists);
  282. // returns list of tuples with items, stops at the shortest list
  283. TRuntimeNode Zip(const TArrayRef<const TRuntimeNode>& lists);
  284. // returns list of tuples with optional of items, has length of the longest list
  285. TRuntimeNode ZipAll(const TArrayRef<const TRuntimeNode>& lists);
  286. TRuntimeNode Enumerate(TRuntimeNode list);
  287. TRuntimeNode Enumerate(TRuntimeNode list, TRuntimeNode start, TRuntimeNode step);
  288. TRuntimeNode Fold(TRuntimeNode list, TRuntimeNode state, const TBinaryLambda& handler);
  289. TRuntimeNode Fold1(TRuntimeNode list, const TUnaryLambda& init, const TBinaryLambda& handler);
  290. TRuntimeNode Reduce(TRuntimeNode list, TRuntimeNode state1,
  291. const TBinaryLambda& handler1,
  292. const TUnaryLambda& handler2,
  293. TRuntimeNode state3,
  294. const TBinaryLambda& handler3);
  295. TRuntimeNode Condense(TRuntimeNode stream, TRuntimeNode state,
  296. const TBinaryLambda& switcher,
  297. const TBinaryLambda& handler, bool useCtx = false);
  298. TRuntimeNode Condense1(TRuntimeNode stream, const TUnaryLambda& init,
  299. const TBinaryLambda& switcher,
  300. const TBinaryLambda& handler, bool useCtx = false);
  301. TRuntimeNode Squeeze(TRuntimeNode stream, TRuntimeNode state,
  302. const TBinaryLambda& handler,
  303. const TUnaryLambda& save = {},
  304. const TUnaryLambda& load = {});
  305. TRuntimeNode Squeeze1(TRuntimeNode stream, const TUnaryLambda& init,
  306. const TBinaryLambda& handler,
  307. const TUnaryLambda& save = {},
  308. const TUnaryLambda& load = {});
  309. TRuntimeNode Discard(TRuntimeNode stream);
  310. TRuntimeNode Map(TRuntimeNode list, const TUnaryLambda& handler);
  311. TRuntimeNode OrderedMap(TRuntimeNode list, const TUnaryLambda& handler);
  312. TRuntimeNode MapNext(TRuntimeNode list, const TBinaryLambda& handler);
  313. TRuntimeNode Extract(TRuntimeNode list, const std::string_view& name);
  314. TRuntimeNode OrderedExtract(TRuntimeNode list, const std::string_view& name);
  315. TRuntimeNode ChainMap(TRuntimeNode list, TRuntimeNode state, const TBinaryLambda& handler);
  316. TRuntimeNode ChainMap(TRuntimeNode list, TRuntimeNode state, const TBinarySplitLambda& handler);
  317. TRuntimeNode Chain1Map(TRuntimeNode list, const TUnaryLambda& init, const TBinaryLambda& handler);
  318. TRuntimeNode Chain1Map(TRuntimeNode list, const TUnarySplitLambda& init, const TBinarySplitLambda& handler);
  319. TRuntimeNode FlatMap(TRuntimeNode list, const TUnaryLambda& handler);
  320. TRuntimeNode OrderedFlatMap(TRuntimeNode list, const TUnaryLambda& handler);
  321. TRuntimeNode MultiMap(TRuntimeNode list, const TExpandLambda& handler);
  322. TRuntimeNode Filter(TRuntimeNode list, const TUnaryLambda& handler);
  323. TRuntimeNode Filter(TRuntimeNode list, TRuntimeNode limit, const TUnaryLambda& handler);
  324. TRuntimeNode OrderedFilter(TRuntimeNode list, const TUnaryLambda& handler);
  325. TRuntimeNode OrderedFilter(TRuntimeNode list, TRuntimeNode limit, const TUnaryLambda& handler);
  326. TRuntimeNode TakeWhile(TRuntimeNode list, const TUnaryLambda& handler);
  327. TRuntimeNode SkipWhile(TRuntimeNode list, const TUnaryLambda& handler);
  328. TRuntimeNode TakeWhileInclusive(TRuntimeNode list, const TUnaryLambda& handler);
  329. TRuntimeNode SkipWhileInclusive(TRuntimeNode list, const TUnaryLambda& handler);
  330. TRuntimeNode FilterNullMembers(TRuntimeNode list);
  331. TRuntimeNode SkipNullMembers(TRuntimeNode list);
  332. TRuntimeNode FilterNullMembers(TRuntimeNode list, const TArrayRef<const std::string_view>& members);
  333. TRuntimeNode SkipNullMembers(TRuntimeNode list, const TArrayRef<const std::string_view>& members);
  334. TRuntimeNode FilterNullElements(TRuntimeNode list);
  335. TRuntimeNode SkipNullElements(TRuntimeNode list);
  336. TRuntimeNode FilterNullElements(TRuntimeNode list, const TArrayRef<const ui32>& elements);
  337. TRuntimeNode SkipNullElements(TRuntimeNode list, const TArrayRef<const ui32>& elements);
  338. TRuntimeNode ExpandMap(TRuntimeNode flow, const TExpandLambda& handler);
  339. TRuntimeNode WideMap(TRuntimeNode flow, const TWideLambda& handler);
  340. TRuntimeNode NarrowMap(TRuntimeNode flow, const TNarrowLambda& handler);
  341. TRuntimeNode NarrowFlatMap(TRuntimeNode flow, const TNarrowLambda& handler);
  342. TRuntimeNode NarrowMultiMap(TRuntimeNode flow, const TWideLambda& handler);
  343. TRuntimeNode WideChain1Map(TRuntimeNode flow, const TWideLambda& init, const TBinaryWideLambda& update);
  344. TRuntimeNode WideFilter(TRuntimeNode flow, const TNarrowLambda& handler);
  345. TRuntimeNode WideFilter(TRuntimeNode flow, TRuntimeNode limit, const TNarrowLambda& handler);
  346. TRuntimeNode WideTakeWhile(TRuntimeNode flow, const TNarrowLambda& handler);
  347. TRuntimeNode WideSkipWhile(TRuntimeNode flow, const TNarrowLambda& handler);
  348. TRuntimeNode WideTakeWhileInclusive(TRuntimeNode flow, const TNarrowLambda& handler);
  349. TRuntimeNode WideSkipWhileInclusive(TRuntimeNode flow, const TNarrowLambda& handler);
  350. TRuntimeNode WideCombiner(TRuntimeNode flow, i64 memLimit, const TWideLambda& keyExtractor, const TBinaryWideLambda& init, const TTernaryWideLambda& update, const TBinaryWideLambda& finish);
  351. TRuntimeNode WideLastCombinerCommon(const TStringBuf& funcName, TRuntimeNode flow, const TWideLambda& keyExtractor, const TBinaryWideLambda& init, const TTernaryWideLambda& update, const TBinaryWideLambda& finish);
  352. TRuntimeNode WideLastCombiner(TRuntimeNode flow, const TWideLambda& keyExtractor, const TBinaryWideLambda& init, const TTernaryWideLambda& update, const TBinaryWideLambda& finish);
  353. TRuntimeNode WideLastCombinerWithSpilling(TRuntimeNode flow, const TWideLambda& keyExtractor, const TBinaryWideLambda& init, const TTernaryWideLambda& update, const TBinaryWideLambda& finish);
  354. TRuntimeNode WideCondense1(TRuntimeNode stream, const TWideLambda& init, const TWideSwitchLambda& switcher, const TBinaryWideLambda& handler, bool useCtx = false);
  355. TRuntimeNode WideTop(TRuntimeNode flow, TRuntimeNode count, const std::vector<std::pair<ui32, TRuntimeNode>>& keys);
  356. TRuntimeNode WideTopSort(TRuntimeNode flow, TRuntimeNode count, const std::vector<std::pair<ui32, TRuntimeNode>>& keys);
  357. TRuntimeNode WideSort(TRuntimeNode flow, const std::vector<std::pair<ui32, TRuntimeNode>>& keys);
  358. TRuntimeNode Length(TRuntimeNode listOrDict);
  359. TRuntimeNode Iterator(TRuntimeNode list, const TArrayRef<const TRuntimeNode>& dependentNodes);
  360. TRuntimeNode EmptyIterator(TType* streamType);
  361. TRuntimeNode Collect(TRuntimeNode listOrStream);
  362. TRuntimeNode LazyList(TRuntimeNode list);
  363. TRuntimeNode ListFromRange(TRuntimeNode start, TRuntimeNode end, TRuntimeNode step);
  364. TRuntimeNode ForwardList(TRuntimeNode stream);
  365. TRuntimeNode Switch(TRuntimeNode stream,
  366. const TArrayRef<const TSwitchInput>& handlerInputs,
  367. std::function<TRuntimeNode(ui32 index, TRuntimeNode item)> handler,
  368. ui64 memoryLimitBytes, TType* returnType);
  369. TRuntimeNode HasItems(TRuntimeNode listOrDict);
  370. TRuntimeNode Reverse(TRuntimeNode list);
  371. TRuntimeNode Skip(TRuntimeNode list, TRuntimeNode count);
  372. TRuntimeNode Take(TRuntimeNode list, TRuntimeNode count);
  373. TRuntimeNode Replicate(TRuntimeNode item, TRuntimeNode count, const std::string_view& file, ui32 row, ui32 column);
  374. TRuntimeNode Sort(TRuntimeNode list, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  375. TRuntimeNode Top(TRuntimeNode list, TRuntimeNode count, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  376. TRuntimeNode TopSort(TRuntimeNode list, TRuntimeNode count, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  377. TRuntimeNode KeepTop(TRuntimeNode count, TRuntimeNode list, TRuntimeNode item, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  378. TRuntimeNode ListIf(TRuntimeNode predicate, TRuntimeNode item);
  379. TRuntimeNode AsList(TRuntimeNode item);
  380. TRuntimeNode AsList(const TArrayRef<const TRuntimeNode>& items);
  381. TRuntimeNode MapJoinCore(TRuntimeNode flow, TRuntimeNode dict, EJoinKind joinKind,
  382. const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& leftRenames,
  383. const TArrayRef<const ui32>& rightRenames, TType* returnType);
  384. TRuntimeNode CommonJoinCore(TRuntimeNode list, EJoinKind joinKind,
  385. const TArrayRef<const ui32>& leftColumns, const TArrayRef<const ui32>& rightColumns,
  386. const TArrayRef<const ui32>& requiredColumns, const TArrayRef<const ui32>& keyColumns,
  387. ui64 memLimit, std::optional<ui32> sortedTableOrder,
  388. EAnyJoinSettings anyJoinSettings, const ui32 tableIndexField,
  389. TType* returnType);
  390. TRuntimeNode GraceJoinCommon(const TStringBuf& funcName, TRuntimeNode flowLeft, TRuntimeNode flowRight, EJoinKind joinKind,
  391. const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& rightKeyColumns,
  392. const TArrayRef<const ui32>& leftRenames, const TArrayRef<const ui32>& rightRenames, TType* returnType, EAnyJoinSettings anyJoinSettings = EAnyJoinSettings::None);
  393. TRuntimeNode GraceJoin(TRuntimeNode flowLeft, TRuntimeNode flowRight, EJoinKind joinKind,
  394. const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& rightKeyColumns,
  395. const TArrayRef<const ui32>& leftRenames, const TArrayRef<const ui32>& rightRenames, TType* returnType, EAnyJoinSettings anyJoinSettings = EAnyJoinSettings::None);
  396. TRuntimeNode GraceSelfJoin(TRuntimeNode flowLeft, EJoinKind joinKind, const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& rightKeyColumns,
  397. const TArrayRef<const ui32>& leftRenames, const TArrayRef<const ui32>& rightRenames, TType* returnType, EAnyJoinSettings anyJoinSettings = EAnyJoinSettings::None);
  398. TRuntimeNode GraceJoinWithSpilling(TRuntimeNode flowLeft, TRuntimeNode flowRight, EJoinKind joinKind,
  399. const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& rightKeyColumns,
  400. const TArrayRef<const ui32>& leftRenames, const TArrayRef<const ui32>& rightRenames, TType* returnType, EAnyJoinSettings anyJoinSettings = EAnyJoinSettings::None);
  401. TRuntimeNode GraceSelfJoinWithSpilling(TRuntimeNode flowLeft, EJoinKind joinKind, const TArrayRef<const ui32>& leftKeyColumns, const TArrayRef<const ui32>& rightKeyColumns,
  402. const TArrayRef<const ui32>& leftRenames, const TArrayRef<const ui32>& rightRenames, TType* returnType, EAnyJoinSettings anyJoinSettings = EAnyJoinSettings::None);
  403. TRuntimeNode CombineCore(TRuntimeNode stream,
  404. const TUnaryLambda& keyExtractor,
  405. const TBinaryLambda& init,
  406. const TTernaryLambda& update,
  407. const TBinaryLambda& finish,
  408. ui64 memLimit);
  409. TRuntimeNode GroupingCore(TRuntimeNode stream,
  410. const TBinaryLambda& groupSwitch,
  411. const TUnaryLambda& keyExtractor,
  412. const TUnaryLambda& handler = {});
  413. TRuntimeNode HoppingCore(TRuntimeNode list,
  414. const TUnaryLambda& timeExtractor,
  415. const TUnaryLambda& init,
  416. const TBinaryLambda& update,
  417. const TUnaryLambda& save,
  418. const TUnaryLambda& load,
  419. const TBinaryLambda& merge,
  420. const TBinaryLambda& finish,
  421. TRuntimeNode hop, TRuntimeNode interval, TRuntimeNode delay);
  422. TRuntimeNode MultiHoppingCore(TRuntimeNode list,
  423. const TUnaryLambda& keyExtractor,
  424. const TUnaryLambda& timeExtractor,
  425. const TUnaryLambda& init,
  426. const TBinaryLambda& update,
  427. const TUnaryLambda& save,
  428. const TUnaryLambda& load,
  429. const TBinaryLambda& merge,
  430. const TTernaryLambda& finish,
  431. TRuntimeNode hop, TRuntimeNode interval, TRuntimeNode delay,
  432. TRuntimeNode dataWatermarks, TRuntimeNode watermarksMode);
  433. TRuntimeNode Chopper(TRuntimeNode flow, const TUnaryLambda& keyExtractor, const TBinaryLambda& groupSwitch, const TBinaryLambda& groupHandler);
  434. TRuntimeNode WideChopper(TRuntimeNode flow, const TWideLambda& keyExtractor, const TWideSwitchLambda& groupSwitch,
  435. const std::function<TRuntimeNode (TRuntimeNode::TList, TRuntimeNode)>& groupHandler
  436. );
  437. //-- dict functions
  438. TRuntimeNode Contains(TRuntimeNode dict, TRuntimeNode key);
  439. TRuntimeNode Lookup(TRuntimeNode dict, TRuntimeNode key);
  440. // all - keep all payloads in list or keep first payload only
  441. TRuntimeNode ToSortedDict(TRuntimeNode list, bool all, const TUnaryLambda& keySelector,
  442. const TUnaryLambda& payloadSelector, bool isCompact = false, ui64 itemsCountHint = 0);
  443. TRuntimeNode ToHashedDict(TRuntimeNode list, bool all, const TUnaryLambda& keySelector,
  444. const TUnaryLambda& payloadSelector, bool isCompact = false, ui64 itemsCountHint = 0);
  445. TRuntimeNode SqueezeToSortedDict(TRuntimeNode stream, bool all, const TUnaryLambda& keySelector,
  446. const TUnaryLambda& payloadSelector, bool isCompact = false, ui64 itemsCountHint = 0);
  447. TRuntimeNode SqueezeToHashedDict(TRuntimeNode stream, bool all, const TUnaryLambda& keySelector,
  448. const TUnaryLambda& payloadSelector, bool isCompact = false, ui64 itemsCountHint = 0);
  449. TRuntimeNode NarrowSqueezeToSortedDict(TRuntimeNode stream, bool all, const TNarrowLambda& keySelector,
  450. const TNarrowLambda& payloadSelector, bool isCompact = false, ui64 itemsCountHint = 0);
  451. TRuntimeNode NarrowSqueezeToHashedDict(TRuntimeNode stream, bool all, const TNarrowLambda& keySelector,
  452. const TNarrowLambda& payloadSelector, bool isCompact = false, ui64 itemsCountHint = 0);
  453. TRuntimeNode SqueezeToList(TRuntimeNode flow, TRuntimeNode limit);
  454. // return list of 2-item tuples with key and payload
  455. TRuntimeNode DictItems(TRuntimeNode dict);
  456. TRuntimeNode DictKeys(TRuntimeNode dict);
  457. TRuntimeNode DictPayloads(TRuntimeNode dict);
  458. TRuntimeNode ToIndexDict(TRuntimeNode list); // make a dict ui64->item
  459. // build a list from tuple of payloads or just a list if semijoin kind is used
  460. TRuntimeNode JoinDict(TRuntimeNode dict1, bool isMulti1, TRuntimeNode dict2, bool isMulti2, EJoinKind joinKind);
  461. //-- branching functions
  462. TRuntimeNode Coalesce(TRuntimeNode data, TRuntimeNode defaultData);
  463. TRuntimeNode Unwrap(TRuntimeNode optional, TRuntimeNode message, const std::string_view& file, ui32 row, ui32 column);
  464. TRuntimeNode Exists(TRuntimeNode data);
  465. TRuntimeNode If(const TArrayRef<const TRuntimeNode>& args);
  466. TRuntimeNode If(TRuntimeNode condition, TRuntimeNode thenBranch, TRuntimeNode elseBranch);
  467. TRuntimeNode IfPresent(TRuntimeNode::TList optional, const TNarrowLambda& thenBranch, TRuntimeNode elseBranch);
  468. TRuntimeNode ToList(TRuntimeNode optional);
  469. TRuntimeNode Iterable(TZeroLambda lambda);
  470. TRuntimeNode ToOptional(TRuntimeNode list);
  471. TRuntimeNode Head(TRuntimeNode list);
  472. TRuntimeNode Last(TRuntimeNode list);
  473. TRuntimeNode Nanvl(TRuntimeNode data, TRuntimeNode dataIfNaN);
  474. TRuntimeNode Ensure(TRuntimeNode value, TRuntimeNode predicate, TRuntimeNode message, const std::string_view& file, ui32 row, ui32 column);
  475. TRuntimeNode SourceOf(TType* returnType);
  476. TRuntimeNode Source();
  477. TRuntimeNode MakeHeap(TRuntimeNode list, const TBinaryLambda& comparator);
  478. TRuntimeNode PushHeap(TRuntimeNode list, const TBinaryLambda& comparator);
  479. TRuntimeNode PopHeap(TRuntimeNode list, const TBinaryLambda& comparator);
  480. TRuntimeNode SortHeap(TRuntimeNode list, const TBinaryLambda& comparator);
  481. TRuntimeNode StableSort(TRuntimeNode list, const TBinaryLambda& comparator);
  482. TRuntimeNode NthElement(TRuntimeNode list, TRuntimeNode n, const TBinaryLambda& comparator);
  483. TRuntimeNode PartialSort(TRuntimeNode list, TRuntimeNode n, const TBinaryLambda& comparator);
  484. //-- arithmetic functions
  485. TRuntimeNode Increment(TRuntimeNode data);
  486. TRuntimeNode Decrement(TRuntimeNode data);
  487. TRuntimeNode Abs(TRuntimeNode data);
  488. TRuntimeNode Plus(TRuntimeNode data);
  489. TRuntimeNode Minus(TRuntimeNode data);
  490. TRuntimeNode Add(TRuntimeNode data1, TRuntimeNode data2);
  491. TRuntimeNode Sub(TRuntimeNode data1, TRuntimeNode data2);
  492. TRuntimeNode Mul(TRuntimeNode data1, TRuntimeNode data2);
  493. TRuntimeNode Div(TRuntimeNode data1, TRuntimeNode data2);
  494. TRuntimeNode Mod(TRuntimeNode data1, TRuntimeNode data2);
  495. TRuntimeNode Min(const TArrayRef<const TRuntimeNode>& args);
  496. TRuntimeNode Max(const TArrayRef<const TRuntimeNode>& args);
  497. TRuntimeNode Min(TRuntimeNode data1, TRuntimeNode data2);
  498. TRuntimeNode Max(TRuntimeNode data1, TRuntimeNode data2);
  499. TRuntimeNode DecimalDiv(TRuntimeNode data1, TRuntimeNode data2);
  500. TRuntimeNode DecimalMod(TRuntimeNode data1, TRuntimeNode data2);
  501. TRuntimeNode DecimalMul(TRuntimeNode data1, TRuntimeNode data2);
  502. TRuntimeNode BlockDecimalDiv(TRuntimeNode first, TRuntimeNode second);
  503. TRuntimeNode BlockDecimalMod(TRuntimeNode first, TRuntimeNode second);
  504. TRuntimeNode BlockDecimalMul(TRuntimeNode first, TRuntimeNode second);
  505. //-- bit logical functions
  506. TRuntimeNode BitNot(TRuntimeNode data);
  507. TRuntimeNode CountBits(TRuntimeNode data);
  508. TRuntimeNode BitAnd(TRuntimeNode data1, TRuntimeNode data2);
  509. TRuntimeNode BitOr(TRuntimeNode data1, TRuntimeNode data2);
  510. TRuntimeNode BitXor(TRuntimeNode data1, TRuntimeNode data2);
  511. //-- bit shifts
  512. TRuntimeNode ShiftLeft(TRuntimeNode arg, TRuntimeNode bits);
  513. TRuntimeNode RotLeft(TRuntimeNode arg, TRuntimeNode bits);
  514. TRuntimeNode ShiftRight(TRuntimeNode arg, TRuntimeNode bits);
  515. TRuntimeNode RotRight(TRuntimeNode arg, TRuntimeNode bits);
  516. // -- sql comparison functions - empty optional looks like an unknown value
  517. TRuntimeNode Equals(TRuntimeNode data1, TRuntimeNode data2);
  518. TRuntimeNode NotEquals(TRuntimeNode data1, TRuntimeNode data2);
  519. TRuntimeNode Less(TRuntimeNode data1, TRuntimeNode data2);
  520. TRuntimeNode LessOrEqual(TRuntimeNode data1, TRuntimeNode data2);
  521. TRuntimeNode Greater(TRuntimeNode data1, TRuntimeNode data2);
  522. TRuntimeNode GreaterOrEqual(TRuntimeNode data1, TRuntimeNode data2);
  523. // -- aggr comparison functions
  524. TRuntimeNode AggrEquals(TRuntimeNode data1, TRuntimeNode data2);
  525. TRuntimeNode AggrNotEquals(TRuntimeNode data1, TRuntimeNode data2);
  526. TRuntimeNode AggrLess(TRuntimeNode data1, TRuntimeNode data2);
  527. TRuntimeNode AggrLessOrEqual(TRuntimeNode data1, TRuntimeNode data2);
  528. TRuntimeNode AggrGreater(TRuntimeNode data1, TRuntimeNode data2);
  529. TRuntimeNode AggrGreaterOrEqual(TRuntimeNode data1, TRuntimeNode data2);
  530. //-- logical functions
  531. TRuntimeNode Not(TRuntimeNode data);
  532. TRuntimeNode And(const TArrayRef<const TRuntimeNode>& args);
  533. TRuntimeNode Or(const TArrayRef<const TRuntimeNode>& args);
  534. TRuntimeNode Xor(const TArrayRef<const TRuntimeNode>& args);
  535. //-- tuple functions
  536. TRuntimeNode Nth(TRuntimeNode tuple, ui32 index);
  537. TRuntimeNode Element(TRuntimeNode tuple, ui32 index);
  538. //-- variant functions
  539. TRuntimeNode Guess(TRuntimeNode variant, ui32 tupleIndex);
  540. TRuntimeNode Guess(TRuntimeNode variant, const std::string_view& memberName);
  541. TRuntimeNode VisitAll(TRuntimeNode variant, std::function<TRuntimeNode(ui32, TRuntimeNode)> handler);
  542. TRuntimeNode Way(TRuntimeNode variant);
  543. TRuntimeNode VariantItem(TRuntimeNode variant);
  544. TRuntimeNode DynamicVariant(TRuntimeNode item, TRuntimeNode index, TType* variantType);
  545. //-- random functions
  546. // expects ui64 seed, returns resource
  547. TRuntimeNode NewMTRand(TRuntimeNode seed);
  548. // returns tuple of (ui64 random value, resource)
  549. TRuntimeNode NextMTRand(TRuntimeNode rand);
  550. //-- aggregation functions
  551. TRuntimeNode AggrCountInit(TRuntimeNode value);
  552. TRuntimeNode AggrCountUpdate(TRuntimeNode value, TRuntimeNode state);
  553. TRuntimeNode AggrMin(TRuntimeNode data1, TRuntimeNode data2);
  554. TRuntimeNode AggrMax(TRuntimeNode data1, TRuntimeNode data2);
  555. TRuntimeNode AggrAdd(TRuntimeNode data1, TRuntimeNode data2);
  556. //-- queue functions
  557. TRuntimeNode QueueCreate(TRuntimeNode initCapacity, TRuntimeNode initCreate, const TArrayRef<const TRuntimeNode>& dependentNodes, TType* returnType);
  558. TRuntimeNode QueuePush(TRuntimeNode resource, TRuntimeNode value);
  559. TRuntimeNode QueuePop(TRuntimeNode resource);
  560. TRuntimeNode QueuePeek(TRuntimeNode resource, TRuntimeNode index, const TArrayRef<const TRuntimeNode>& dependentNodes, TType* returnType);
  561. TRuntimeNode QueueRange(TRuntimeNode resource, TRuntimeNode begin, TRuntimeNode end, const TArrayRef<const TRuntimeNode>& dependentNodes, TType* returnType);
  562. TRuntimeNode PreserveStream(TRuntimeNode stream, TRuntimeNode preserve, TRuntimeNode outpace);
  563. TRuntimeNode Seq(const TArrayRef<const TRuntimeNode>& items, TType* returnType);
  564. TRuntimeNode FromYsonSimpleType(TRuntimeNode input, NUdf::TDataTypeId schemeType);
  565. TRuntimeNode TryWeakMemberFromDict(TRuntimeNode other, TRuntimeNode rest, NUdf::TDataTypeId schemeType, const std::string_view& memberName);
  566. TRuntimeNode TimezoneId(TRuntimeNode name);
  567. TRuntimeNode TimezoneName(TRuntimeNode id);
  568. TRuntimeNode AddTimezone(TRuntimeNode utc, TRuntimeNode id);
  569. TRuntimeNode RemoveTimezone(TRuntimeNode local);
  570. TRuntimeNode AllOf(TRuntimeNode list, const TUnaryLambda& predicate);
  571. TRuntimeNode NotAllOf(TRuntimeNode list, const TUnaryLambda& predicate);
  572. TRuntimeNode Cast(TRuntimeNode data, TType* type);
  573. TRuntimeNode Default(TType* type);
  574. TRuntimeNode RangeCreate(TRuntimeNode list);
  575. TRuntimeNode RangeUnion(const TArrayRef<const TRuntimeNode>& lists);
  576. TRuntimeNode RangeIntersect(const TArrayRef<const TRuntimeNode>& lists);
  577. TRuntimeNode RangeMultiply(const TArrayRef<const TRuntimeNode>& args);
  578. TRuntimeNode RangeFinalize(TRuntimeNode list);
  579. TRuntimeNode Round(const std::string_view& callableName, TRuntimeNode source, TType* targetType);
  580. TRuntimeNode NextValue(TRuntimeNode value);
  581. TRuntimeNode Nop(TRuntimeNode value, TType* returnType);
  582. typedef TRuntimeNode (TProgramBuilder::*UnaryFunctionMethod)(TRuntimeNode);
  583. typedef TRuntimeNode (TProgramBuilder::*BinaryFunctionMethod)(TRuntimeNode, TRuntimeNode);
  584. typedef TRuntimeNode (TProgramBuilder::*TernaryFunctionMethod)(TRuntimeNode, TRuntimeNode, TRuntimeNode);
  585. typedef TRuntimeNode (TProgramBuilder::*ArrayFunctionMethod)(const TArrayRef<const TRuntimeNode>&);
  586. typedef TRuntimeNode (TProgramBuilder::*ProcessFunctionMethod)(TRuntimeNode, const TUnaryLambda&);
  587. typedef TRuntimeNode (TProgramBuilder::*NarrowFunctionMethod)(TRuntimeNode, const TNarrowLambda&);
  588. TRuntimeNode PgConst(TPgType* pgType, const std::string_view& value, TRuntimeNode typeMod = {});
  589. TRuntimeNode PgResolvedCall(bool useContext, const std::string_view& name, ui32 id,
  590. const TArrayRef<const TRuntimeNode>& args, TType* returnType, bool rangeFunction);
  591. TRuntimeNode PgCast(TRuntimeNode input, TType* returnType, TRuntimeNode typeMod = {});
  592. TRuntimeNode FromPg(TRuntimeNode input, TType* returnType);
  593. TRuntimeNode ToPg(TRuntimeNode input, TType* returnType);
  594. TRuntimeNode PgClone(TRuntimeNode input, const TArrayRef<const TRuntimeNode>& dependentNodes);
  595. TRuntimeNode WithContext(TRuntimeNode input, const std::string_view& contextType);
  596. TRuntimeNode PgInternal0(TType* returnType);
  597. TRuntimeNode PgArray(const TArrayRef<const TRuntimeNode>& args, TType* returnType);
  598. TRuntimeNode PgTableContent(
  599. const std::string_view& cluster,
  600. const std::string_view& table,
  601. TType* returnType);
  602. TRuntimeNode PgToRecord(TRuntimeNode input, const TArrayRef<std::pair<std::string_view, std::string_view>>& members);
  603. TRuntimeNode ScalarApply(const TArrayRef<const TRuntimeNode>& args, const TArrayLambda& handler);
  604. TRuntimeNode MatchRecognizeCore(
  605. TRuntimeNode inputStream,
  606. const TUnaryLambda& getPartitionKeySelectorNode,
  607. const TArrayRef<TStringBuf>& partitionColumnNames,
  608. const TVector<TStringBuf>& measureColumnNames,
  609. const TVector<TBinaryLambda>& getMeasures,
  610. const NYql::NMatchRecognize::TRowPattern& pattern,
  611. const TVector<TStringBuf>& defineVarNames,
  612. const TVector<TTernaryLambda>& getDefines,
  613. bool streamingMode,
  614. const NYql::NMatchRecognize::TAfterMatchSkipTo& skipTo,
  615. NYql::NMatchRecognize::ERowsPerMatch rowsPerMatch
  616. );
  617. TRuntimeNode TimeOrderRecover(
  618. TRuntimeNode inputStream,
  619. const TUnaryLambda& getTimeExtractor,
  620. TRuntimeNode delay,
  621. TRuntimeNode ahead,
  622. TRuntimeNode rowLimit
  623. );
  624. protected:
  625. TRuntimeNode Invoke(const std::string_view& funcName, TType* resultType, const TArrayRef<const TRuntimeNode>& args);
  626. TRuntimeNode IfPresent(TRuntimeNode optional, const TUnaryLambda& thenBranch, TRuntimeNode elseBranch);
  627. void ThrowIfListOfVoid(TType* type);
  628. template <typename ResultType>
  629. TRuntimeNode BuildContainerProperty(const std::string_view& callableName, TRuntimeNode listOrDict);
  630. TRuntimeNode Filter(TRuntimeNode list, const TUnaryLambda& handler, TType* resultType);
  631. template <bool Ordered>
  632. TRuntimeNode BuildExtract(TRuntimeNode list, const std::string_view& name);
  633. TRuntimeNode BuildMap(const std::string_view& callableName, TRuntimeNode list, const TUnaryLambda& handler);
  634. TRuntimeNode BuildFlatMap(const std::string_view& callableName, TRuntimeNode list, const TUnaryLambda& handler);
  635. TRuntimeNode BuildFilter(const std::string_view& callableName, TRuntimeNode list, const TUnaryLambda& handler, TType* resultType = nullptr);
  636. TRuntimeNode BuildFilter(const std::string_view& callableName, TRuntimeNode list, TRuntimeNode limit, const TUnaryLambda& handler, TType* resultType = nullptr);
  637. TRuntimeNode BuildSort(const std::string_view& callableName, TRuntimeNode list, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  638. TRuntimeNode BuildListSort(const std::string_view& callableName, TRuntimeNode list, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  639. TRuntimeNode BuildNth(const std::string_view& callableName, TRuntimeNode list, TRuntimeNode n, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  640. TRuntimeNode BuildListNth(const std::string_view& callableName, TRuntimeNode list, TRuntimeNode n, TRuntimeNode ascending, const TUnaryLambda& keyExtractor);
  641. TRuntimeNode BuildTake(const std::string_view& callableName, TRuntimeNode list, TRuntimeNode count);
  642. TRuntimeNode BuildHeap(const std::string_view& callableName, TRuntimeNode list, const TBinaryLambda& comparator);
  643. TRuntimeNode BuildNth(const std::string_view& callableName, TRuntimeNode list, TRuntimeNode n, const TBinaryLambda& comparator);
  644. TRuntimeNode BuildLogical(const std::string_view& callableName, const TArrayRef<const TRuntimeNode>& args);
  645. TRuntimeNode BuildBinaryLogical(const std::string_view& callableName, TRuntimeNode data1, TRuntimeNode data2);
  646. TRuntimeNode BuildMinMax(const std::string_view& callableName, const TRuntimeNode* data, size_t size);
  647. TRuntimeNode BuildWideSkipTakeBlocks(const std::string_view& callableName, TRuntimeNode flow, TRuntimeNode count);
  648. TRuntimeNode BuildBlockLogical(const std::string_view& callableName, TRuntimeNode first, TRuntimeNode second);
  649. TRuntimeNode BuildExtend(const std::string_view& callableName, const TArrayRef<const TRuntimeNode>& lists);
  650. TRuntimeNode BuildBlockDecimalBinary(const std::string_view& callableName, TRuntimeNode first, TRuntimeNode second);
  651. private:
  652. TRuntimeNode BuildWideFilter(const std::string_view& callableName, TRuntimeNode flow, const TNarrowLambda& handler);
  653. TRuntimeNode BuildBlockCombineAll(const std::string_view& callableName, TRuntimeNode input, std::optional<ui32> filterColumn,
  654. const TArrayRef<const TAggInfo>& aggs, TType* returnType);
  655. TRuntimeNode BuildBlockCombineHashed(const std::string_view& callableName, TRuntimeNode input, std::optional<ui32> filterColumn,
  656. const TArrayRef<ui32>& keys, const TArrayRef<const TAggInfo>& aggs, TType* returnType);
  657. TRuntimeNode BuildBlockMergeFinalizeHashed(const std::string_view& callableName, TRuntimeNode input, const TArrayRef<ui32>& keys,
  658. const TArrayRef<const TAggInfo>& aggs, TType* returnType);
  659. TRuntimeNode BuildBlockMergeManyFinalizeHashed(const std::string_view& callableName, TRuntimeNode input, const TArrayRef<ui32>& keys,
  660. const TArrayRef<const TAggInfo>& aggs, ui32 streamIndex, const TVector<TVector<ui32>>& streams, TType* returnType);
  661. TRuntimeNode DictItems(TRuntimeNode dict, EDictItems mode);
  662. TRuntimeNode If(TRuntimeNode condition, TRuntimeNode thenBranch, TRuntimeNode elseBranch, TType* resultType);
  663. TRuntimeNode ToDict(TRuntimeNode list, bool multi, const TUnaryLambda& keySelector,
  664. const TUnaryLambda& payloadSelector, std::string_view callableName, bool isCompact, ui64 itemsCountHint);
  665. TRuntimeNode SqueezeToDict(TRuntimeNode stream, bool multi, const TUnaryLambda& keySelector,
  666. const TUnaryLambda& payloadSelector, std::string_view callableName, bool isCompact, ui64 itemsCountHint);
  667. TRuntimeNode NarrowSqueezeToDict(TRuntimeNode stream, bool multi, const TNarrowLambda& keySelector,
  668. const TNarrowLambda& payloadSelector, std::string_view callableName, bool isCompact, ui64 itemsCountHint);
  669. TRuntimeNode UnaryDataFunction(TRuntimeNode data, const std::string_view& callableName, ui32 flags);
  670. template<bool IsFilter, bool OnStruct>
  671. TRuntimeNode BuildFilterNulls(TRuntimeNode list);
  672. template<bool IsFilter, bool OnStruct>
  673. TRuntimeNode BuildFilterNulls(TRuntimeNode list, const TArrayRef<std::conditional_t<OnStruct, const std::string_view, const ui32>>& members);
  674. template<bool OnStruct>
  675. TRuntimeNode BuildFilterNulls(TRuntimeNode list, const TArrayRef<std::conditional_t<OnStruct, const std::string_view, const ui32>>& members,
  676. const std::conditional_t<OnStruct, std::vector<std::pair<std::string_view, TType*>>, std::vector<TType*>>& filteredItems);
  677. TRuntimeNode BuildWideTopOrSort(const std::string_view& callableName, TRuntimeNode flow, TMaybe<TRuntimeNode> count, const std::vector<std::pair<ui32, TRuntimeNode>>& keys);
  678. TRuntimeNode InvokeBinary(const std::string_view& callableName, TType* type, TRuntimeNode data1, TRuntimeNode data2);
  679. TRuntimeNode AggrCompare(const std::string_view& callableName, TRuntimeNode data1, TRuntimeNode data2);
  680. TRuntimeNode DataCompare(const std::string_view& callableName, TRuntimeNode data1, TRuntimeNode data2);
  681. TRuntimeNode BuildRangeLogical(const std::string_view& callableName, const TArrayRef<const TRuntimeNode>& lists);
  682. template<bool Default>
  683. TRuntimeNode DefaultResult(TRuntimeNode data);
  684. template<BinaryFunctionMethod Compare>
  685. TRuntimeNode BuildOptionalCompare(TRuntimeNode data1, TRuntimeNode data2);
  686. template<BinaryFunctionMethod Compare, bool OnEqual>
  687. TRuntimeNode BuildOptionalCompare(TRuntimeNode data1, TRuntimeNode data2);
  688. template<BinaryFunctionMethod Compare, bool OnEqual, bool Asc>
  689. TRuntimeNode BuildOptionalCompare(TRuntimeNode data1, TRuntimeNode data2);
  690. template<BinaryFunctionMethod Equal, BinaryFunctionMethod Compare, BinaryFunctionMethod FinalCompare>
  691. TRuntimeNode BuildByNthCompare(ui32 count, TRuntimeNode data1, TRuntimeNode data2, ui32 index = 0U);
  692. template<BinaryFunctionMethod Compare, bool Fallback>
  693. TRuntimeNode BuildVariantCompare(TRuntimeNode data1, TRuntimeNode data2);
  694. template<BinaryFunctionMethod Compare>
  695. TRuntimeNode BuildVariantCompare(TRuntimeNode data1, TRuntimeNode data2);
  696. template<BinaryFunctionMethod Equal, BinaryFunctionMethod Compare, BinaryFunctionMethod FinalCompare>
  697. TRuntimeNode BuildMembersCompare(const TStructType* type, TRuntimeNode data1, TRuntimeNode data2, ui32 index = 0U);
  698. template<TProgramBuilder::BinaryFunctionMethod Compare, TProgramBuilder::ArrayFunctionMethod Join, bool OnEqual>
  699. TRuntimeNode BuildStructCompare(TRuntimeNode data1, TRuntimeNode data2);
  700. template<BinaryFunctionMethod Equal, BinaryFunctionMethod Compare, BinaryFunctionMethod FinalCompare, bool OnEqual>
  701. TRuntimeNode BuildStructCompare(TRuntimeNode data1, TRuntimeNode data2);
  702. template<TProgramBuilder::BinaryFunctionMethod Compare, TProgramBuilder::ArrayFunctionMethod Join, bool OnEqual>
  703. TRuntimeNode BuildTupleCompare(TRuntimeNode data1, TRuntimeNode data2);
  704. template<BinaryFunctionMethod Equal, BinaryFunctionMethod Compare, BinaryFunctionMethod FinalCompare, bool OnEqual>
  705. TRuntimeNode BuildTupleCompare(TRuntimeNode data1, TRuntimeNode data2);
  706. template<bool Equal>
  707. TRuntimeNode BuildAggrCompare(const std::string_view& callableName, TRuntimeNode data1, TRuntimeNode data2);
  708. template <bool Equal>
  709. TRuntimeNode BuildSqlCompare(const std::string_view& callableName, TRuntimeNode data1, TRuntimeNode data2);
  710. template <bool Asc, bool Equal>
  711. TRuntimeNode BuildAggrCompare(const std::string_view& callableName, TRuntimeNode data1, TRuntimeNode data2);
  712. template <bool Asc, bool Equal>
  713. TRuntimeNode BuildSqlCompare(const std::string_view& callableName, TRuntimeNode data1, TRuntimeNode data2);
  714. TType* ChooseCommonType(TType* type1, TType* type2);
  715. TType* BuildArithmeticCommonType(TType* type1, TType* type2);
  716. bool IsNull(TRuntimeNode arg);
  717. protected:
  718. const IFunctionRegistry& FunctionRegistry;
  719. const bool VoidWithEffects;
  720. NUdf::ITypeInfoHelper::TPtr TypeInfoHelper;
  721. };
  722. bool CanExportType(TType* type, const TTypeEnvironment& env);
  723. void EnsureDataOrOptionalOfData(TRuntimeNode node);
  724. }
  725. }