udf_ut_helpers.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "udf_value.h"
  3. #include <yql/essentials/utils/method_index.h>
  4. #include <util/generic/ymath.h>
  5. #include <util/system/platform.h>
  6. #include <util/generic/yexception.h>
  7. #include <util/system/yassert.h>
  8. #include <util/string/hex.h>
  9. namespace NYql {
  10. namespace NUdf {
  11. template<bool HasLength = true>
  12. class TLazyList: public NUdf::TBoxedValue {
  13. struct TIterator: public NUdf::TBoxedValue {
  14. TIterator(i32 from, i32 to)
  15. : From(from), To(to), Curr(Max<i32>())
  16. {
  17. if (To >= From) {
  18. To--; // exclude last
  19. } else {
  20. From--; // exclude first
  21. }
  22. }
  23. private:
  24. bool Skip() override {
  25. if (Curr == Max<i32>()) {
  26. Curr = From;
  27. return true;
  28. }
  29. if (To >= From) {
  30. if (Curr < To) {
  31. ++Curr;
  32. return true;
  33. }
  34. } else {
  35. if (Curr > To) {
  36. --Curr;
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. bool Next(NUdf::TUnboxedValue& value) override {
  43. if (!Skip())
  44. return false;
  45. value = NUdf::TUnboxedValuePod(Curr);
  46. return true;
  47. }
  48. i32 From, To, Curr;
  49. };
  50. public:
  51. TLazyList(i32 from, i32 to)
  52. : From_(from), To_(to)
  53. {
  54. }
  55. private:
  56. bool HasFastListLength() const override {
  57. return HasLength;
  58. }
  59. ui64 GetListLength() const override {
  60. if (HasLength)
  61. return Abs(To_ - From_);
  62. Y_ABORT("No length!");
  63. }
  64. bool HasListItems() const override {
  65. return To_ != From_;
  66. }
  67. NUdf::TUnboxedValue GetListIterator() const override {
  68. return NUdf::TUnboxedValuePod(new TIterator(From_, To_));
  69. }
  70. NUdf::IBoxedValuePtr ReverseListImpl(const NUdf::IValueBuilder& builder) const override {
  71. Y_UNUSED(builder);
  72. return new TLazyList(To_, From_);
  73. }
  74. NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
  75. Y_UNUSED(builder);
  76. count = std::min<ui64>(count, Abs(To_ - From_));
  77. if (To_ >= From_) {
  78. return new TLazyList(From_ + count, To_);
  79. }
  80. return new TLazyList(From_ - count, To_);
  81. }
  82. NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
  83. Y_UNUSED(builder);
  84. count = std::min<ui64>(count, Abs(To_ - From_));
  85. if (To_ >= From_) {
  86. return new TLazyList(From_, From_ + count);
  87. }
  88. return new TLazyList(From_, From_ - count);
  89. }
  90. NUdf::IBoxedValuePtr ToIndexDictImpl(const NUdf::IValueBuilder& builder) const override {
  91. Y_UNUSED(builder);
  92. return nullptr;
  93. }
  94. i32 From_, To_;
  95. };
  96. }
  97. }