mkql_block_agg_factory.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #include <yql/essentials/minikql/computation/mkql_computation_node.h>
  3. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  4. namespace NKikimr {
  5. namespace NMiniKQL {
  6. class IAggColumnBuilder {
  7. public:
  8. virtual ~IAggColumnBuilder() = default;
  9. virtual void Add(const void* state) = 0;
  10. virtual NUdf::TUnboxedValue Build() = 0;
  11. };
  12. class IBlockAggregatorBase {
  13. public:
  14. virtual ~IBlockAggregatorBase() = default;
  15. const ui32 StateSize;
  16. explicit IBlockAggregatorBase(ui32 stateSize)
  17. : StateSize(stateSize)
  18. {}
  19. virtual void DestroyState(void* state) noexcept = 0;
  20. };
  21. class IBlockAggregatorCombineAll : public IBlockAggregatorBase {
  22. public:
  23. virtual void InitState(void* state) = 0;
  24. virtual void AddMany(void* state, const NUdf::TUnboxedValue* columns, ui64 batchLength, std::optional<ui64> filtered) = 0;
  25. virtual NUdf::TUnboxedValue FinishOne(const void* state) = 0;
  26. explicit IBlockAggregatorCombineAll(ui32 stateSize)
  27. : IBlockAggregatorBase(stateSize)
  28. {}
  29. };
  30. class IBlockAggregatorCombineKeys : public IBlockAggregatorBase {
  31. public:
  32. virtual void InitKey(void* state, ui64 batchNum, const NUdf::TUnboxedValue* columns, ui64 row) = 0;
  33. virtual void UpdateKey(void* state, ui64 batchNum, const NUdf::TUnboxedValue* columns, ui64 row) = 0;
  34. virtual std::unique_ptr<IAggColumnBuilder> MakeStateBuilder(ui64 size) = 0;
  35. explicit IBlockAggregatorCombineKeys(ui32 stateSize)
  36. : IBlockAggregatorBase(stateSize)
  37. {}
  38. };
  39. class IBlockAggregatorFinalizeKeys : public IBlockAggregatorBase {
  40. public:
  41. virtual void LoadState(void* state, ui64 batchNum, const NUdf::TUnboxedValue* columns, ui64 row) = 0;
  42. virtual void UpdateState(void* state, ui64 batchNum, const NUdf::TUnboxedValue* columns, ui64 row) = 0;
  43. virtual std::unique_ptr<IAggColumnBuilder> MakeResultBuilder(ui64 size) = 0;
  44. explicit IBlockAggregatorFinalizeKeys(ui32 stateSize)
  45. : IBlockAggregatorBase(stateSize)
  46. {}
  47. };
  48. template <typename TBase>
  49. class TBlockAggregatorBase : public TBase {
  50. public:
  51. TBlockAggregatorBase(ui32 stateSize, std::optional<ui32> filterColumn, TComputationContext& ctx)
  52. : TBase(stateSize)
  53. , FilterColumn_(filterColumn)
  54. , Ctx_(ctx)
  55. {
  56. }
  57. protected:
  58. const std::optional<ui32> FilterColumn_;
  59. TComputationContext& Ctx_;
  60. };
  61. template <typename T>
  62. class IPreparedBlockAggregator {
  63. public:
  64. virtual ~IPreparedBlockAggregator() = default;
  65. virtual std::unique_ptr<T> Make(TComputationContext& ctx) const = 0;
  66. const ui32 StateSize;
  67. explicit IPreparedBlockAggregator(ui32 stateSize)
  68. : StateSize(stateSize)
  69. {}
  70. };
  71. class IBlockAggregatorFactory {
  72. public:
  73. virtual ~IBlockAggregatorFactory() = default;
  74. virtual std::unique_ptr<IPreparedBlockAggregator<IBlockAggregatorCombineAll>> PrepareCombineAll(
  75. TTupleType* tupleType,
  76. std::optional<ui32> filterColumn,
  77. const std::vector<ui32>& argsColumns,
  78. const TTypeEnvironment& env) const = 0;
  79. virtual std::unique_ptr<IPreparedBlockAggregator<IBlockAggregatorCombineKeys>> PrepareCombineKeys(
  80. TTupleType* tupleType,
  81. const std::vector<ui32>& argsColumns,
  82. const TTypeEnvironment& env) const = 0;
  83. virtual std::unique_ptr<IPreparedBlockAggregator<IBlockAggregatorFinalizeKeys>> PrepareFinalizeKeys(
  84. TTupleType* tupleType,
  85. const std::vector<ui32>& argsColumns,
  86. const TTypeEnvironment& env,
  87. TType* returnType,
  88. ui32 hint) const = 0;
  89. };
  90. const IBlockAggregatorFactory& GetBlockAggregatorFactory(TStringBuf name);
  91. struct TCombineAllTag {
  92. using TAggregator = IBlockAggregatorCombineAll;
  93. using TPreparedAggregator = IPreparedBlockAggregator<TAggregator>;
  94. using TBase = TBlockAggregatorBase<TAggregator>;
  95. };
  96. struct TCombineKeysTag {
  97. using TAggregator = IBlockAggregatorCombineKeys;
  98. using TPreparedAggregator = IPreparedBlockAggregator<TAggregator>;
  99. using TBase = TBlockAggregatorBase<TAggregator>;
  100. };
  101. struct TFinalizeKeysTag {
  102. using TAggregator = IBlockAggregatorFinalizeKeys;
  103. using TPreparedAggregator = IPreparedBlockAggregator<TAggregator>;
  104. using TBase = TBlockAggregatorBase<TAggregator>;
  105. };
  106. }
  107. }