mkql_program_builder.h 47 KB

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