mkql_group.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "mkql_group.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  3. #include <yql/essentials/minikql/mkql_node_cast.h>
  4. #include <yql/essentials/minikql/mkql_stats_registry.h>
  5. #include <yql/essentials/minikql/compact_hash.h>
  6. #include <yql/essentials/minikql/defs.h>
  7. #include <util/generic/maybe.h>
  8. namespace NKikimr {
  9. namespace NMiniKQL {
  10. namespace {
  11. template <bool WithHandler>
  12. class TGroupingCoreWrapper: public TMutableComputationNode<TGroupingCoreWrapper<WithHandler>> {
  13. using TSelf = TGroupingCoreWrapper;
  14. typedef TMutableComputationNode<TSelf> TBaseComputation;
  15. public:
  16. class TSplitStreamValue : public TComputationValue<TSplitStreamValue> {
  17. public:
  18. using TBase = TComputationValue<TSplitStreamValue>;
  19. enum EState {
  20. AtStart,
  21. AtGroupStart,
  22. Fetching,
  23. GroupFinished,
  24. Finished,
  25. };
  26. TSplitStreamValue(TMemoryUsageInfo* memInfo, TComputationContext& compCtx, const TSelf* self, NUdf::TUnboxedValue&& stream)
  27. : TBase(memInfo)
  28. , CompCtx(compCtx)
  29. , Self(self)
  30. , Stream(std::move(stream))
  31. {
  32. }
  33. NUdf::EFetchStatus NextKey(NUdf::TUnboxedValue& key) {
  34. if (Fetching == State || AtGroupStart == State) {
  35. NUdf::EFetchStatus status = NUdf::EFetchStatus::Ok;
  36. for (NUdf::TUnboxedValue item; NUdf::EFetchStatus::Ok == status; status = Fetch(item)) {
  37. }
  38. if (NUdf::EFetchStatus::Finish != status) {
  39. return status;
  40. }
  41. }
  42. if (Finished == State) {
  43. return NUdf::EFetchStatus::Finish;
  44. }
  45. if (GroupFinished != State) {
  46. auto status = Stream.Fetch(Value);
  47. if (NUdf::EFetchStatus::Finish == status) {
  48. State = Finished;
  49. }
  50. if (NUdf::EFetchStatus::Ok != status) {
  51. return status;
  52. }
  53. }
  54. Self->KeyExtractorItemNode->SetValue(CompCtx, NUdf::TUnboxedValue(Value));
  55. key = Self->KeyExtractorResultNode->GetValue(CompCtx);
  56. Self->GroupSwitchKeyNode->SetValue(CompCtx, NUdf::TUnboxedValue(key));
  57. Self->GroupSwitchItemNode->SetValue(CompCtx, NUdf::TUnboxedValue(Value));
  58. State = AtGroupStart;
  59. return NUdf::EFetchStatus::Ok;
  60. }
  61. private:
  62. NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue& result) override {
  63. if (Finished == State) {
  64. return NUdf::EFetchStatus::Finish;
  65. }
  66. if (AtGroupStart != State) {
  67. auto status = Stream.Fetch(Value);
  68. if (NUdf::EFetchStatus::Finish == status) {
  69. State = Finished;
  70. }
  71. if (NUdf::EFetchStatus::Ok != status) {
  72. return status;
  73. }
  74. }
  75. if (Fetching == State) {
  76. Self->GroupSwitchItemNode->SetValue(CompCtx, NUdf::TUnboxedValue(Value));
  77. if (Self->GroupSwitchResultNode->GetValue(CompCtx).template Get<bool>()) {
  78. State = GroupFinished;
  79. return NUdf::EFetchStatus::Finish;
  80. }
  81. } else {
  82. State = Fetching;
  83. }
  84. if constexpr (WithHandler) {
  85. Self->HandlerItemNode->SetValue(CompCtx, std::move(Value));
  86. result = Self->HandlerResultNode->GetValue(CompCtx);
  87. } else {
  88. result = std::move(Value);
  89. }
  90. return NUdf::EFetchStatus::Ok;
  91. }
  92. private:
  93. TComputationContext& CompCtx;
  94. const TSelf* const Self;
  95. NUdf::TUnboxedValue Stream;
  96. EState State = AtStart;
  97. NUdf::TUnboxedValue Value;
  98. };
  99. class TGroupStreamValue : public TComputationValue<TGroupStreamValue> {
  100. public:
  101. using TBase = TComputationValue<TGroupStreamValue>;
  102. TGroupStreamValue(TMemoryUsageInfo* memInfo, TComputationContext& compCtx, const TSelf* self, NUdf::TUnboxedValue&& stream)
  103. : TBase(memInfo)
  104. , CompCtx(compCtx)
  105. , SplitStream(CompCtx.HolderFactory.Create<TSplitStreamValue>(CompCtx, self, std::move(stream)))
  106. , SplitStreamValue(static_cast<TSplitStreamValue*>(SplitStream.AsBoxed().Get()))
  107. {
  108. }
  109. private:
  110. NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue& result) override {
  111. NUdf::TUnboxedValue key;
  112. auto status = SplitStreamValue->NextKey(key);
  113. if (status != NUdf::EFetchStatus::Ok) {
  114. return status;
  115. }
  116. NKikimr::NUdf::TUnboxedValue* itemsPtr;
  117. result = CompCtx.HolderFactory.CreateDirectArrayHolder(2, itemsPtr);
  118. itemsPtr[0] = std::move(key);
  119. itemsPtr[1] = SplitStream;
  120. return status;
  121. }
  122. private:
  123. TComputationContext& CompCtx;
  124. NUdf::TUnboxedValue SplitStream;
  125. TSplitStreamValue* SplitStreamValue;
  126. };
  127. TGroupingCoreWrapper(TComputationMutables& mutables,
  128. IComputationNode* stream,
  129. IComputationExternalNode* keyExtractorItem,
  130. IComputationNode* keyExtractorResult,
  131. IComputationExternalNode* groupSwitchKey,
  132. IComputationExternalNode* groupSwitchItem,
  133. IComputationNode* groupSwitchResult,
  134. IComputationExternalNode* handlerItem,
  135. IComputationNode* handlerResult)
  136. : TBaseComputation(mutables)
  137. , Stream(stream)
  138. , KeyExtractorItemNode(keyExtractorItem)
  139. , KeyExtractorResultNode(keyExtractorResult)
  140. , GroupSwitchKeyNode(groupSwitchKey)
  141. , GroupSwitchItemNode(groupSwitchItem)
  142. , GroupSwitchResultNode(groupSwitchResult)
  143. , HandlerItemNode(handlerItem)
  144. , HandlerResultNode(handlerResult)
  145. {
  146. }
  147. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  148. return ctx.HolderFactory.Create<TGroupStreamValue>(ctx, this, Stream->GetValue(ctx));
  149. }
  150. private:
  151. void RegisterDependencies() const final {
  152. this->DependsOn(Stream);
  153. this->DependsOn(KeyExtractorResultNode);
  154. this->DependsOn(GroupSwitchResultNode);
  155. this->DependsOn(HandlerResultNode);
  156. this->Own(KeyExtractorItemNode);
  157. this->Own(GroupSwitchKeyNode);
  158. this->Own(GroupSwitchItemNode);
  159. this->Own(HandlerItemNode);
  160. }
  161. private:
  162. IComputationNode* const Stream;
  163. IComputationExternalNode* const KeyExtractorItemNode;
  164. IComputationNode* const KeyExtractorResultNode;
  165. IComputationExternalNode* const GroupSwitchKeyNode;
  166. IComputationExternalNode* const GroupSwitchItemNode;
  167. IComputationNode* const GroupSwitchResultNode;
  168. IComputationExternalNode* const HandlerItemNode;
  169. IComputationNode* const HandlerResultNode;
  170. };
  171. }
  172. IComputationNode* WrapGroupingCore(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  173. MKQL_ENSURE(callable.GetInputsCount() == 6 || callable.GetInputsCount() == 8, "Expected 6 or 8 args");
  174. const auto stream = LocateNode(ctx.NodeLocator, callable, 0);
  175. const auto keyExtractorResult = LocateNode(ctx.NodeLocator, callable, 1);
  176. const auto groupSwitchResult = LocateNode(ctx.NodeLocator, callable, 2);
  177. const auto keyExtractorItem = LocateExternalNode(ctx.NodeLocator, callable, 3);
  178. const auto groupSwitchKey = LocateExternalNode(ctx.NodeLocator, callable, 4);
  179. const auto groupSwitchItem = LocateExternalNode(ctx.NodeLocator, callable, 5);
  180. if (callable.GetInputsCount() == 8) {
  181. auto handlerResult = LocateNode(ctx.NodeLocator, callable, 6);
  182. auto handlerItem = LocateExternalNode(ctx.NodeLocator, callable, 7);
  183. return new TGroupingCoreWrapper<true>(
  184. ctx.Mutables,
  185. stream,
  186. keyExtractorItem,
  187. keyExtractorResult,
  188. groupSwitchKey,
  189. groupSwitchItem,
  190. groupSwitchResult,
  191. handlerItem,
  192. handlerResult);
  193. }
  194. return new TGroupingCoreWrapper<false>(
  195. ctx.Mutables,
  196. stream,
  197. keyExtractorItem,
  198. keyExtractorResult,
  199. groupSwitchKey,
  200. groupSwitchItem,
  201. groupSwitchResult,
  202. nullptr,
  203. nullptr);
  204. }
  205. }
  206. }