mkql_program_builder.h 49 KB

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