utils.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "utils.h"
  2. #include <yql/essentials/public/purecalc/common/names.h>
  3. #include <yql/essentials/core/yql_expr_type_annotation.h>
  4. using namespace NYql;
  5. using namespace NYql::NPureCalc;
  6. TExprNode::TPtr NYql::NPureCalc::NodeFromBlocks(
  7. const TPositionHandle& pos,
  8. const TStructExprType* structType,
  9. TExprContext& ctx
  10. ) {
  11. const auto items = structType->GetItems();
  12. Y_ENSURE(items.size() > 0);
  13. return ctx.Builder(pos)
  14. .Lambda()
  15. .Param("stream")
  16. .Callable(0, "FromFlow")
  17. .Callable(0, "NarrowMap")
  18. .Callable(0, "WideFromBlocks")
  19. .Callable(0, "ExpandMap")
  20. .Callable(0, "ToFlow")
  21. .Arg(0, "stream")
  22. .Seal()
  23. .Lambda(1)
  24. .Param("item")
  25. .Do([&](TExprNodeBuilder& lambda) -> TExprNodeBuilder& {
  26. ui32 i = 0;
  27. for (const auto& item : items) {
  28. lambda.Callable(i++, "Member")
  29. .Arg(0, "item")
  30. .Atom(1, item->GetName())
  31. .Seal();
  32. }
  33. lambda.Callable(i, "Member")
  34. .Arg(0, "item")
  35. .Atom(1, PurecalcBlockColumnLength)
  36. .Seal();
  37. return lambda;
  38. })
  39. .Seal()
  40. .Seal()
  41. .Seal()
  42. .Lambda(1)
  43. .Params("fields", items.size())
  44. .Callable("AsStruct")
  45. .Do([&](TExprNodeBuilder& parent) -> TExprNodeBuilder& {
  46. ui32 i = 0;
  47. for (const auto& item : items) {
  48. parent.List(i)
  49. .Atom(0, item->GetName())
  50. .Arg(1, "fields", i++)
  51. .Seal();
  52. }
  53. return parent;
  54. })
  55. .Seal()
  56. .Seal()
  57. .Seal()
  58. .Seal()
  59. .Seal()
  60. .Build();
  61. }
  62. TExprNode::TPtr NYql::NPureCalc::NodeToBlocks(
  63. const TPositionHandle& pos,
  64. const TStructExprType* structType,
  65. TExprContext& ctx
  66. ) {
  67. const auto items = structType->GetItems();
  68. Y_ENSURE(items.size() > 0);
  69. return ctx.Builder(pos)
  70. .Lambda()
  71. .Param("stream")
  72. .Callable("FromFlow")
  73. .Callable(0, "NarrowMap")
  74. .Callable(0, "WideToBlocks")
  75. .Callable(0, "ExpandMap")
  76. .Callable(0, "ToFlow")
  77. .Arg(0, "stream")
  78. .Seal()
  79. .Lambda(1)
  80. .Param("item")
  81. .Do([&](TExprNodeBuilder& lambda) -> TExprNodeBuilder& {
  82. ui32 i = 0;
  83. for (const auto& item : items) {
  84. lambda.Callable(i++, "Member")
  85. .Arg(0, "item")
  86. .Atom(1, item->GetName())
  87. .Seal();
  88. }
  89. return lambda;
  90. })
  91. .Seal()
  92. .Seal()
  93. .Seal()
  94. .Lambda(1)
  95. .Params("fields", items.size() + 1)
  96. .Callable("AsStruct")
  97. .Do([&](TExprNodeBuilder& parent) -> TExprNodeBuilder& {
  98. ui32 i = 0;
  99. for (const auto& item : items) {
  100. parent.List(i)
  101. .Atom(0, item->GetName())
  102. .Arg(1, "fields", i++)
  103. .Seal();
  104. }
  105. parent.List(i)
  106. .Atom(0, PurecalcBlockColumnLength)
  107. .Arg(1, "fields", i)
  108. .Seal();
  109. return parent;
  110. })
  111. .Seal()
  112. .Seal()
  113. .Seal()
  114. .Seal()
  115. .Seal()
  116. .Build();
  117. }
  118. TExprNode::TPtr NYql::NPureCalc::ApplyToIterable(
  119. const TPositionHandle& pos,
  120. const TExprNode::TPtr iterable,
  121. const TExprNode::TPtr lambda,
  122. bool wrapLMap,
  123. TExprContext& ctx
  124. ) {
  125. if (wrapLMap) {
  126. return ctx.Builder(pos)
  127. .Callable("LMap")
  128. .Add(0, iterable)
  129. .Lambda(1)
  130. .Param("stream")
  131. .Apply(lambda)
  132. .With(0, "stream")
  133. .Seal()
  134. .Seal()
  135. .Seal()
  136. .Build();
  137. } else {
  138. return ctx.Builder(pos)
  139. .Apply(lambda)
  140. .With(0, iterable)
  141. .Seal()
  142. .Build();
  143. }
  144. }
  145. const TStructExprType* NYql::NPureCalc::WrapBlockStruct(
  146. const TStructExprType* structType,
  147. TExprContext& ctx
  148. ) {
  149. TVector<const TItemExprType*> members;
  150. for (const auto& item : structType->GetItems()) {
  151. const auto blockItemType = ctx.MakeType<TBlockExprType>(item->GetItemType());
  152. members.push_back(ctx.MakeType<TItemExprType>(item->GetName(), blockItemType));
  153. }
  154. const auto scalarItemType = ctx.MakeType<TScalarExprType>(ctx.MakeType<TDataExprType>(EDataSlot::Uint64));
  155. members.push_back(ctx.MakeType<TItemExprType>(PurecalcBlockColumnLength, scalarItemType));
  156. return ctx.MakeType<TStructExprType>(members);
  157. }
  158. const TStructExprType* NYql::NPureCalc::UnwrapBlockStruct(
  159. const TStructExprType* structType,
  160. TExprContext& ctx
  161. ) {
  162. TVector<const TItemExprType*> members;
  163. for (const auto& item : structType->GetItems()) {
  164. if (item->GetName() == PurecalcBlockColumnLength) {
  165. continue;
  166. }
  167. bool isScalarUnused;
  168. const auto blockItemType = GetBlockItemType(*item->GetItemType(), isScalarUnused);
  169. members.push_back(ctx.MakeType<TItemExprType>(item->GetName(), blockItemType));
  170. }
  171. return ctx.MakeType<TStructExprType>(members);
  172. }