mkql_pickle.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "mkql_pickle.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/computation/mkql_computation_node_pack.h>
  5. #include <yql/essentials/minikql/computation/presort.h>
  6. #include <yql/essentials/minikql/mkql_string_util.h>
  7. namespace NKikimr {
  8. namespace NMiniKQL {
  9. namespace {
  10. template <bool Stable>
  11. class TPickleWrapper : public TMutableComputationNode<TPickleWrapper<Stable>> {
  12. typedef TMutableComputationNode<TPickleWrapper<Stable>> TBaseComputation;
  13. public:
  14. TPickleWrapper(TComputationMutables& mutables, TType* type, IComputationNode* data)
  15. : TBaseComputation(mutables)
  16. , Type(type)
  17. , ValuePacker(mutables)
  18. , Data(data)
  19. {
  20. }
  21. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  22. return MakeString(ValuePacker.RefMutableObject(ctx, Stable, Type).Pack(Data->GetValue(ctx)));
  23. }
  24. private:
  25. void RegisterDependencies() const final {
  26. this->DependsOn(Data);
  27. }
  28. TType* Type;
  29. TMutableObjectOverBoxedValue<TValuePackerBoxed> ValuePacker;
  30. IComputationNode *const Data;
  31. };
  32. class TUnpickleWrapper : public TMutableComputationNode<TUnpickleWrapper> {
  33. typedef TMutableComputationNode<TUnpickleWrapper> TBaseComputation;
  34. public:
  35. TUnpickleWrapper(TComputationMutables& mutables, TType* type, IComputationNode* data)
  36. : TBaseComputation(mutables)
  37. , Type(type)
  38. , ValuePacker(mutables)
  39. , Data(data)
  40. {
  41. }
  42. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  43. auto data = Data->GetValue(ctx);
  44. auto buffer = data.AsStringRef();
  45. return ValuePacker.RefMutableObject(ctx, false, Type).Unpack(buffer, ctx.HolderFactory).Release();
  46. }
  47. private:
  48. void RegisterDependencies() const final {
  49. DependsOn(Data);
  50. }
  51. TType* const Type;
  52. TMutableObjectOverBoxedValue<TValuePackerBoxed> ValuePacker;
  53. IComputationNode *const Data;
  54. };
  55. class TGenericPresortEncoderBoxed : public TComputationValue<TGenericPresortEncoderBoxed>, public TGenericPresortEncoder {
  56. typedef TComputationValue<TGenericPresortEncoderBoxed> TBase;
  57. public:
  58. TGenericPresortEncoderBoxed(TMemoryUsageInfo* memInfo, TType* type)
  59. : TBase(memInfo)
  60. , TGenericPresortEncoder(type)
  61. {}
  62. };
  63. template <bool Desc>
  64. class TPresortEncodeWrapper : public TMutableComputationNode<TPresortEncodeWrapper<Desc>> {
  65. typedef TMutableComputationNode<TPresortEncodeWrapper<Desc>> TBaseComputation;
  66. public:
  67. TPresortEncodeWrapper(TComputationMutables& mutables, TType* type, IComputationNode* data)
  68. : TBaseComputation(mutables)
  69. , Type(type)
  70. , Encoder(mutables)
  71. , Data(data)
  72. {
  73. }
  74. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  75. return MakeString(Encoder.RefMutableObject(ctx, Type).Encode(Data->GetValue(ctx), Desc));
  76. }
  77. private:
  78. void RegisterDependencies() const final {
  79. this->DependsOn(Data);
  80. }
  81. TType* Type;
  82. TMutableObjectOverBoxedValue<TGenericPresortEncoderBoxed> Encoder;
  83. IComputationNode *const Data;
  84. };
  85. }
  86. IComputationNode* WrapPickle(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  87. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  88. return new TPickleWrapper<false>(ctx.Mutables, callable.GetInput(0).GetStaticType(), LocateNode(ctx.NodeLocator, callable, 0));
  89. }
  90. IComputationNode* WrapStablePickle(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  91. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  92. return new TPickleWrapper<true>(ctx.Mutables, callable.GetInput(0).GetStaticType(), LocateNode(ctx.NodeLocator, callable, 0));
  93. }
  94. IComputationNode* WrapUnpickle(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  95. MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
  96. MKQL_ENSURE(callable.GetInput(0).IsImmediate() && callable.GetInput(0).GetNode()->GetType()->IsType(), "Expected type");
  97. return new TUnpickleWrapper(ctx.Mutables, static_cast<TType*>(callable.GetInput(0).GetNode()), LocateNode(ctx.NodeLocator, callable, 1));
  98. }
  99. IComputationNode* WrapAscending(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  100. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  101. return new TPresortEncodeWrapper<false>(ctx.Mutables, callable.GetInput(0).GetStaticType(), LocateNode(ctx.NodeLocator, callable, 0));
  102. }
  103. IComputationNode* WrapDescending(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  104. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  105. return new TPresortEncodeWrapper<true>(ctx.Mutables, callable.GetInput(0).GetStaticType(), LocateNode(ctx.NodeLocator, callable, 0));
  106. }
  107. }
  108. }