mkql_aggrcount.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "mkql_aggrcount.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
  3. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  4. #include <yql/essentials/minikql/computation/mkql_computation_node_holders_codegen.h>
  5. #include <yql/essentials/minikql/mkql_node_cast.h>
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. namespace {
  9. class TAggrCountInitWrapper : public TDecoratorCodegeneratorNode<TAggrCountInitWrapper> {
  10. typedef TDecoratorCodegeneratorNode<TAggrCountInitWrapper> TBaseComputation;
  11. public:
  12. TAggrCountInitWrapper(IComputationNode* value)
  13. : TBaseComputation(value)
  14. {}
  15. NUdf::TUnboxedValuePod DoCalculate(TComputationContext&, const NUdf::TUnboxedValuePod& value) const {
  16. return NUdf::TUnboxedValuePod(ui64(value ? 1ULL : 0ULL));
  17. }
  18. #ifndef MKQL_DISABLE_CODEGEN
  19. Value* DoGenerateGetValue(const TCodegenContext& ctx, Value* value, BasicBlock*& block) const {
  20. const auto check = IsExists(value, block);
  21. if (Node->IsTemporaryValue())
  22. ValueCleanup(Node->GetRepresentation(), value, ctx, block);
  23. return MakeBoolean(check, ctx.Codegen.GetContext(), block);
  24. }
  25. #endif
  26. };
  27. class TAggrCountUpdateWrapper : public TDecoratorCodegeneratorNode<TAggrCountUpdateWrapper> {
  28. typedef TDecoratorCodegeneratorNode<TAggrCountUpdateWrapper> TBaseComputation;
  29. public:
  30. TAggrCountUpdateWrapper(IComputationNode* state)
  31. : TBaseComputation(state)
  32. {}
  33. NUdf::TUnboxedValuePod DoCalculate(TComputationContext&, const NUdf::TUnboxedValuePod& value) const {
  34. return NUdf::TUnboxedValuePod(value.Get<ui64>() + 1U);
  35. }
  36. #ifndef MKQL_DISABLE_CODEGEN
  37. Value* DoGenerateGetValue(const TCodegenContext&, Value* value, BasicBlock*& block) const {
  38. return BinaryOperator::CreateAdd(value, ConstantInt::get(value->getType(), 1), "incr", block);
  39. }
  40. #endif
  41. };
  42. class TAggrCountIfUpdateWrapper : public TMutableCodegeneratorNode<TAggrCountIfUpdateWrapper> {
  43. typedef TMutableCodegeneratorNode<TAggrCountIfUpdateWrapper> TBaseComputation;
  44. public:
  45. TAggrCountIfUpdateWrapper(TComputationMutables& mutables, IComputationNode* value, IComputationNode* state)
  46. : TBaseComputation(mutables, EValueRepresentation::Embedded)
  47. , Arg(value)
  48. , State(state)
  49. {
  50. }
  51. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& compCtx) const {
  52. auto state = State->GetValue(compCtx);
  53. return Arg->GetValue(compCtx) ? NUdf::TUnboxedValuePod(state.Get<ui64>() + 1U) : state.Release();
  54. }
  55. #ifndef MKQL_DISABLE_CODEGEN
  56. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  57. const auto state = GetNodeValue(State, ctx, block);
  58. const auto value = GetNodeValue(Arg, ctx, block);
  59. const auto check = IsExists(value, block);
  60. if (Arg->IsTemporaryValue())
  61. ValueCleanup(Arg->GetRepresentation(), value, ctx, block);
  62. const auto zext = new ZExtInst(check, state->getType(), "zext", block);
  63. const auto incr = BinaryOperator::CreateAdd(state, zext, "incr", block);
  64. return incr;
  65. }
  66. #endif
  67. private:
  68. void RegisterDependencies() const final {
  69. DependsOn(Arg);
  70. DependsOn(State);
  71. }
  72. IComputationNode* const Arg;
  73. IComputationNode* const State;
  74. };
  75. }
  76. IComputationNode* WrapAggrCountInit(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  77. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  78. if (callable.GetInput(0).GetStaticType()->IsOptional()) {
  79. return new TAggrCountInitWrapper(LocateNode(ctx.NodeLocator, callable, 0));
  80. } else {
  81. return ctx.NodeFactory.CreateImmutableNode(NUdf::TUnboxedValuePod(ui64(1ULL)));
  82. }
  83. }
  84. IComputationNode* WrapAggrCountUpdate(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  85. MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
  86. MKQL_ENSURE(AS_TYPE(TDataType, callable.GetInput(1))->GetSchemeType() == NUdf::TDataType<ui64>::Id, "Expected ui64 type");
  87. if (callable.GetInput(0).GetStaticType()->IsOptional()) {
  88. return new TAggrCountIfUpdateWrapper(ctx.Mutables, LocateNode(ctx.NodeLocator, callable, 0), LocateNode(ctx.NodeLocator, callable, 1));
  89. } else {
  90. return new TAggrCountUpdateWrapper(LocateNode(ctx.NodeLocator, callable, 1));
  91. }
  92. }
  93. }
  94. }