lists_udf.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <yql/essentials/public/udf/udf_value.h>
  2. #include <yql/essentials/public/udf/udf_value_builder.h>
  3. #include <yql/essentials/public/udf/udf_type_builder.h>
  4. #include <yql/essentials/public/udf/udf_registrator.h>
  5. #include <util/generic/yexception.h>
  6. #include <vector>
  7. #include <array>
  8. using namespace NKikimr;
  9. using namespace NUdf;
  10. namespace {
  11. //////////////////////////////////////////////////////////////////////////////
  12. // TNumbersList
  13. //////////////////////////////////////////////////////////////////////////////
  14. class TNumbers: public TBoxedValue
  15. {
  16. public:
  17. static TStringRef Name() {
  18. static auto name = TStringRef::Of("Numbers");
  19. return name;
  20. }
  21. private:
  22. TUnboxedValue Run(
  23. const IValueBuilder* valueBuilder,
  24. const TUnboxedValuePod* args) const override
  25. {
  26. const auto appendPrepend = args[0].AsStringRef();
  27. const auto count = args[1].Get<ui32>();
  28. std::vector<TUnboxedValue> list(count);
  29. ui32 i = 0U;
  30. if (TStringRef::Of("Append") == appendPrepend) {
  31. for (auto it = list.begin(); list.end() != it; ++it) {
  32. *it = TUnboxedValuePod(i++);
  33. }
  34. }
  35. else if (TStringRef::Of("Prepend") == appendPrepend) {
  36. for (auto it = list.rbegin(); list.rend() != it; ++it) {
  37. *it = TUnboxedValuePod(i++);
  38. }
  39. }
  40. return valueBuilder->NewList(list.data(), list.size());
  41. }
  42. };
  43. //////////////////////////////////////////////////////////////////////////////
  44. // TExtend
  45. //////////////////////////////////////////////////////////////////////////////
  46. class TExtend: public TBoxedValue
  47. {
  48. public:
  49. static TStringRef Name() {
  50. static auto name = TStringRef::Of("Extend");
  51. return name;
  52. }
  53. private:
  54. TUnboxedValue Run(
  55. const IValueBuilder* valueBuilder,
  56. const TUnboxedValuePod* args) const override
  57. {
  58. std::array<TUnboxedValue, 2U> list = {{TUnboxedValuePod(args[0]), TUnboxedValuePod(args[1])}};
  59. return valueBuilder->NewList(list.data(), list.size());
  60. }
  61. };
  62. //////////////////////////////////////////////////////////////////////////////
  63. // TListsModule
  64. //////////////////////////////////////////////////////////////////////////////
  65. class TListsModule: public IUdfModule
  66. {
  67. public:
  68. TStringRef Name() const {
  69. return TStringRef::Of("Lists");
  70. }
  71. void CleanupOnTerminate() const final {}
  72. void GetAllFunctions(IFunctionsSink& sink) const final {
  73. sink.Add(TNumbers::Name());
  74. sink.Add(TExtend::Name());
  75. }
  76. void BuildFunctionTypeInfo(
  77. const TStringRef& name,
  78. TType* userType,
  79. const TStringRef& typeConfig,
  80. ui32 flags,
  81. IFunctionTypeInfoBuilder& builder) const final
  82. {
  83. try {
  84. Y_UNUSED(userType);
  85. Y_UNUSED(typeConfig);
  86. bool typesOnly = (flags & TFlags::TypesOnly);
  87. if (TNumbers::Name() == name) {
  88. // function signature:
  89. // List<ui32> Numbers(String, ui32)
  90. // runConfig: void
  91. builder.SimpleSignature<TListType<ui32>(char*, ui32)>();
  92. if (!typesOnly) {
  93. builder.Implementation(new TNumbers);
  94. }
  95. }
  96. else if (TExtend::Name() == name) {
  97. // function signature:
  98. // List<ui32> Numbers(List<ui32>, List<ui32>)
  99. // runConfig: void
  100. auto listType = builder.List()->Item<ui32>().Build();
  101. builder.Returns(listType)
  102. .Args()->Add(listType).Add(listType).Done();
  103. if (!typesOnly) {
  104. builder.Implementation(new TExtend);
  105. }
  106. }
  107. } catch (const std::exception& e) {
  108. builder.SetError(CurrentExceptionMessage());
  109. }
  110. }
  111. };
  112. } // namespace
  113. REGISTER_MODULES(TListsModule)