mkql_listfromrange.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #include "mkql_listfromrange.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  3. #include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
  4. #include <yql/essentials/minikql/mkql_node_cast.h>
  5. namespace NKikimr {
  6. namespace NMiniKQL {
  7. namespace {
  8. template<typename T>
  9. ui64 ShiftByMaxNegative(T value) {
  10. static_assert(sizeof(T) <= sizeof(ui64));
  11. static_assert(std::is_integral_v<T>);
  12. if constexpr (std::is_signed_v<T>) {
  13. if (value < 0) {
  14. return ui64(value + std::numeric_limits<T>::max() + T(1));
  15. }
  16. return ui64(value) + ui64(std::numeric_limits<T>::max()) + 1ul;
  17. }
  18. return ui64(value);
  19. }
  20. ui64 GetElementsCount(ui64 start, ui64 end, ui64 step) {
  21. if (step == 0 || start >= end) {
  22. return 0;
  23. }
  24. ui64 diff = end - start;
  25. ui64 div = diff / step;
  26. ui64 rem = diff % step;
  27. return rem ? (div + 1) : div;
  28. }
  29. template<typename T, typename TStep>
  30. ui64 GetElementsCount(T start, T end, TStep step) {
  31. ui64 newStart = ShiftByMaxNegative(start);
  32. ui64 newEnd = ShiftByMaxNegative(end);
  33. ui64 newStep;
  34. if (step < 0) {
  35. newStep = (step == std::numeric_limits<TStep>::min()) ? (ui64(std::numeric_limits<TStep>::max()) + 1ul) : ui64(TStep(0) - step);
  36. std::swap(newStart, newEnd);
  37. } else {
  38. newStep = ui64(step);
  39. }
  40. return GetElementsCount(newStart, newEnd, newStep);
  41. }
  42. template <typename T, typename TStep = std::make_signed_t<T>, std::conditional_t<std::is_floating_point_v<TStep>, i8, TStep> TConstFactor = 1, bool TzDate = false>
  43. class TListFromRangeWrapper : public TMutableCodegeneratorNode<TListFromRangeWrapper<T, TStep, TConstFactor, TzDate>> {
  44. private:
  45. using TBaseComputation = TMutableCodegeneratorNode<TListFromRangeWrapper<T, TStep, TConstFactor, TzDate>>;
  46. class TValue : public TComputationValue<TValue> {
  47. public:
  48. template <bool Asc, bool Float>
  49. class TIterator;
  50. template <bool Asc>
  51. class TIterator<Asc, false> : public TComputationValue<TIterator<Asc, false>> {
  52. public:
  53. TIterator(TMemoryUsageInfo* memInfo, T start, T end, TStep step)
  54. : TComputationValue<TIterator>(memInfo)
  55. , Current(start)
  56. , Step(step)
  57. , Count(GetElementsCount<T, TStep>(start, end, step))
  58. {}
  59. protected:
  60. bool Skip() final {
  61. if (!Count) {
  62. return false;
  63. }
  64. Current += Step;
  65. --Count;
  66. return true;
  67. }
  68. bool Next(NUdf::TUnboxedValue& value) override {
  69. if (!Count) {
  70. return false;
  71. }
  72. value = NUdf::TUnboxedValuePod(Current);
  73. Current += Step;
  74. --Count;
  75. return true;
  76. }
  77. T Current;
  78. const TStep Step;
  79. ui64 Count;
  80. };
  81. template <bool Asc>
  82. class TIterator<Asc, true> : public TComputationValue<TIterator<Asc, true>> {
  83. public:
  84. TIterator(TMemoryUsageInfo* memInfo, T start, T end, TStep step)
  85. : TComputationValue<TIterator>(memInfo)
  86. , Start(start)
  87. , Index(-T(1))
  88. , Limit(end - start)
  89. , Step(step)
  90. {}
  91. private:
  92. bool Skip() final {
  93. const auto next = Index + T(1);
  94. if (Asc ? next * Step < Limit : next * Step > Limit) {
  95. Index = next;
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool Next(NUdf::TUnboxedValue& value) final {
  101. if (!Skip()) {
  102. return false;
  103. }
  104. value = NUdf::TUnboxedValuePod(Start + Index * Step);
  105. return true;
  106. }
  107. const T Start;
  108. T Index;
  109. const T Limit;
  110. const TStep Step;
  111. };
  112. TValue(TMemoryUsageInfo* memInfo, TComputationContext& ctx, T start, T end, TStep step)
  113. : TComputationValue<TValue>(memInfo)
  114. , Ctx(ctx)
  115. , Start(start)
  116. , End(end)
  117. , Step(step)
  118. {
  119. }
  120. protected:
  121. NUdf::TUnboxedValue GetListIterator() const override {
  122. if (Step > TStep(0)) {
  123. return Ctx.HolderFactory.template Create<TIterator<true, std::is_floating_point<T>::value>>(Start, End, Step);
  124. } else if (Step < TStep(0)) {
  125. return Ctx.HolderFactory.template Create<TIterator<false, std::is_floating_point<T>::value>>(Start, End, Step);
  126. } else {
  127. return Ctx.HolderFactory.GetEmptyContainerLazy();
  128. }
  129. }
  130. ui64 GetListLength() const final {
  131. if constexpr (std::is_integral_v<T>) {
  132. return GetElementsCount<T, TStep>(Start, End, Step);
  133. }
  134. if (Step > T(0) && Start < End) {
  135. ui64 len = 0ULL;
  136. for (T i = 0; i * Step < End - Start; i += T(1)) {
  137. ++len;
  138. }
  139. return len;
  140. } else if (Step < T(0) && Start > End) {
  141. ui64 len = 0ULL;
  142. for (T i = 0; i * Step > End - Start; i += T(1)) {
  143. ++len;
  144. }
  145. return len;
  146. } else {
  147. return 0ULL;
  148. }
  149. }
  150. bool HasListItems() const final {
  151. if (Step > TStep(0)) {
  152. return Start < End;
  153. } else if (Step < TStep(0)) {
  154. return Start > End;
  155. } else {
  156. return false;
  157. }
  158. }
  159. bool HasFastListLength() const final {
  160. return std::is_integral<T>();
  161. }
  162. TComputationContext& Ctx;
  163. const T Start;
  164. const T End;
  165. const TStep Step;
  166. };
  167. class TTzValue : public TValue {
  168. public:
  169. template <bool Asc>
  170. class TTzIterator : public TValue::template TIterator<Asc, false> {
  171. public:
  172. using TBase = typename TValue::template TIterator<Asc, false>;
  173. TTzIterator(TMemoryUsageInfo* memInfo, T start, T end, TStep step, ui16 Tz)
  174. : TBase(memInfo, start, end, step)
  175. , TimezoneId(Tz)
  176. {}
  177. bool Next(NUdf::TUnboxedValue& value) final {
  178. if (TBase::Next(value)) {
  179. value.SetTimezoneId(TimezoneId);
  180. return true;
  181. }
  182. return false;
  183. }
  184. private:
  185. const ui16 TimezoneId;
  186. };
  187. NUdf::TUnboxedValue GetListIterator() const final {
  188. if (TValue::Step > TStep(0)) {
  189. return TValue::Ctx.HolderFactory.template Create<TTzIterator<true>>(TValue::Start, TValue::End, TValue::Step, TimezoneId);
  190. } else if (TValue::Step < TStep(0)) {
  191. return TValue::Ctx.HolderFactory.template Create<TTzIterator<false>>(TValue::Start, TValue::End, TValue::Step, TimezoneId);
  192. } else {
  193. return TValue::Ctx.HolderFactory.GetEmptyContainerLazy();
  194. }
  195. }
  196. TTzValue(TMemoryUsageInfo* memInfo, TComputationContext& ctx, T start, T end, TStep step, ui16 TimezoneId)
  197. : TValue(memInfo, ctx, start, end, step), TimezoneId(TimezoneId)
  198. {
  199. }
  200. private:
  201. const ui16 TimezoneId;
  202. };
  203. public:
  204. TListFromRangeWrapper(TComputationMutables& mutables, IComputationNode* start, IComputationNode* end, IComputationNode* step)
  205. : TBaseComputation(mutables, EValueRepresentation::Boxed)
  206. , Start(start)
  207. , End(end)
  208. , Step(step)
  209. {}
  210. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  211. const auto start = Start->GetValue(ctx);
  212. const auto end = End->GetValue(ctx);
  213. auto step = Step->GetValue(ctx).Get<TStep>();
  214. if constexpr (TConstFactor > 1) {
  215. if (step % TConstFactor)
  216. step = 0;
  217. else
  218. step /= TConstFactor;
  219. }
  220. if constexpr (TzDate) {
  221. return MakeList(ctx, start.Get<T>(), end.Get<T>(), step, start.GetTimezoneId());
  222. } else {
  223. return MakeList(ctx, start.Get<T>(), end.Get<T>(), step, 0U);
  224. }
  225. }
  226. #ifndef MKQL_DISABLE_CODEGEN
  227. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  228. auto& context = ctx.Codegen.GetContext();
  229. const auto valueType = Type::getInt128Ty(context);
  230. const auto startv = GetNodeValue(Start, ctx, block);
  231. const auto endv = GetNodeValue(End, ctx, block);
  232. const auto stepv = GetNodeValue(Step, ctx, block);
  233. const auto start = GetterFor<T>(startv, context, block);
  234. const auto end = GetterFor<T>(endv, context, block);
  235. auto step = GetterFor<TStep>(stepv, context, block);
  236. if constexpr (TConstFactor > 1) {
  237. const auto zero = ConstantInt::get(GetTypeFor<TStep>(context), 0);
  238. const auto fact = ConstantInt::get(GetTypeFor<TStep>(context), TConstFactor);
  239. const auto div = BinaryOperator::CreateSDiv(step, fact, "div", block);
  240. const auto rem = BinaryOperator::CreateSRem(step, fact, "rem", block);
  241. const auto bad = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, rem, zero, "bad", block);
  242. step = SelectInst::Create(bad, zero, div, "step", block);
  243. }
  244. const auto timezone = TzDate ? GetterForTimezone(context, startv, block) : ConstantInt::get(Type::getInt16Ty(context), 0);
  245. const auto func = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TListFromRangeWrapper::MakeList));
  246. const auto signature = FunctionType::get(valueType, {ctx.Ctx->getType(), start->getType(), end->getType(), step->getType(), timezone->getType()}, false);
  247. const auto creator = CastInst::Create(Instruction::IntToPtr, func, PointerType::getUnqual(signature), "creator", block);
  248. const auto output = CallInst::Create(signature, creator, {ctx.Ctx, start, end, step, timezone}, "output", block);
  249. return output;
  250. }
  251. #endif
  252. private:
  253. static NUdf::TUnboxedValuePod MakeList(TComputationContext& ctx, T start, T end, TStep step, ui16 timezoneId) {
  254. if constexpr(TzDate)
  255. return ctx.HolderFactory.Create<TTzValue>(ctx, start, end, step, timezoneId);
  256. else
  257. return ctx.HolderFactory.Create<TValue>(ctx, start, end, step);
  258. }
  259. void RegisterDependencies() const final {
  260. this->DependsOn(Start);
  261. this->DependsOn(End);
  262. this->DependsOn(Step);
  263. }
  264. IComputationNode* const Start;
  265. IComputationNode* const End;
  266. IComputationNode* const Step;
  267. };
  268. }
  269. IComputationNode* WrapListFromRange(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  270. MKQL_ENSURE(callable.GetInputsCount() == 3, "Expected 3 args");
  271. const auto start = LocateNode(ctx.NodeLocator, callable, 0);
  272. const auto end = LocateNode(ctx.NodeLocator, callable, 1);
  273. const auto step = LocateNode(ctx.NodeLocator, callable, 2);
  274. switch (*AS_TYPE(TDataType, callable.GetInput(0).GetStaticType())->GetDataSlot()) {
  275. case NUdf::EDataSlot::Uint8:
  276. return new TListFromRangeWrapper<ui8>(ctx.Mutables, start, end, step);
  277. case NUdf::EDataSlot::Int8:
  278. return new TListFromRangeWrapper<i8>(ctx.Mutables, start, end, step);
  279. case NUdf::EDataSlot::Uint16:
  280. return new TListFromRangeWrapper<ui16>(ctx.Mutables, start, end, step);
  281. case NUdf::EDataSlot::Int16:
  282. return new TListFromRangeWrapper<i16>(ctx.Mutables, start, end, step);
  283. case NUdf::EDataSlot::Uint32:
  284. return new TListFromRangeWrapper<ui32>(ctx.Mutables, start, end, step);
  285. case NUdf::EDataSlot::Int32:
  286. return new TListFromRangeWrapper<i32>(ctx.Mutables, start, end, step);
  287. case NUdf::EDataSlot::Uint64:
  288. return new TListFromRangeWrapper<ui64>(ctx.Mutables, start, end, step);
  289. case NUdf::EDataSlot::Int64:
  290. return new TListFromRangeWrapper<i64>(ctx.Mutables, start, end, step);
  291. case NUdf::EDataSlot::Float:
  292. return new TListFromRangeWrapper<float, float>(ctx.Mutables, start, end, step);
  293. case NUdf::EDataSlot::Double:
  294. return new TListFromRangeWrapper<double, double>(ctx.Mutables, start, end, step);
  295. case NUdf::EDataSlot::Date:
  296. return new TListFromRangeWrapper<ui16, i64, 86400000000ll>(ctx.Mutables, start, end, step);
  297. case NUdf::EDataSlot::Date32:
  298. return new TListFromRangeWrapper<i32, i64, 86400000000ll>(ctx.Mutables, start, end, step);
  299. case NUdf::EDataSlot::TzDate:
  300. return new TListFromRangeWrapper<ui16, i64, 86400000000ll, true>(ctx.Mutables, start, end, step);
  301. case NUdf::EDataSlot::Datetime:
  302. return new TListFromRangeWrapper<ui32, i64, 1000000>(ctx.Mutables, start, end, step);
  303. case NUdf::EDataSlot::Datetime64:
  304. return new TListFromRangeWrapper<i64, i64, 1000000>(ctx.Mutables, start, end, step);
  305. case NUdf::EDataSlot::TzDatetime:
  306. return new TListFromRangeWrapper<ui32, i64, 1000000, true>(ctx.Mutables, start, end, step);
  307. case NUdf::EDataSlot::Timestamp:
  308. return new TListFromRangeWrapper<ui64, i64, 1>(ctx.Mutables, start, end, step);
  309. case NUdf::EDataSlot::Timestamp64:
  310. return new TListFromRangeWrapper<i64, i64, 1>(ctx.Mutables, start, end, step);
  311. case NUdf::EDataSlot::TzTimestamp:
  312. return new TListFromRangeWrapper<ui64, i64, 1, true>(ctx.Mutables, start, end, step);
  313. case NUdf::EDataSlot::Interval:
  314. case NUdf::EDataSlot::Interval64:
  315. return new TListFromRangeWrapper<i64, i64, 1>(ctx.Mutables, start, end, step);
  316. default:
  317. MKQL_ENSURE(false, "unexpected");
  318. }
  319. }
  320. }
  321. }