mkql_builtins_less.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "mkql_builtins_compare.h"
  2. #include "mkql_builtins_datetime.h"
  3. #include "mkql_builtins_decimal.h" // Y_IGNORE
  4. #include "mkql_builtins_string_kernels.h"
  5. #include <yql/essentials/minikql/mkql_type_ops.h>
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. namespace {
  9. template <typename T1, typename T2,
  10. std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value && std::is_signed<T1>::value == std::is_signed<T2>::value, bool> Aggr>
  11. Y_FORCE_INLINE bool Less(T1 x, T2 y) {
  12. return x < y;
  13. }
  14. template <typename T1, typename T2,
  15. std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value && std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool> Aggr>
  16. Y_FORCE_INLINE bool Less(T1 x, T2 y) {
  17. return x < T1(0) || static_cast<std::make_unsigned_t<T1>>(x) < y;
  18. }
  19. template <typename T1, typename T2,
  20. std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value && std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool> Aggr>
  21. Y_FORCE_INLINE bool Less(T1 x, T2 y) {
  22. return T2(0) < y && x < static_cast<std::make_unsigned_t<T2>>(y);
  23. }
  24. template <typename T1, typename T2,
  25. std::enable_if_t<std::is_floating_point<T1>::value || std::is_floating_point<T2>::value, bool> Aggr>
  26. Y_FORCE_INLINE bool Less(T1 x, T2 y) {
  27. using F1 = std::conditional_t<std::is_floating_point<T1>::value, T1, T2>;
  28. using F2 = std::conditional_t<std::is_floating_point<T2>::value, T2, T1>;
  29. using FT = std::conditional_t<(sizeof(F1) > sizeof(F2)), F1, F2>;
  30. const auto l = static_cast<FT>(x);
  31. const auto r = static_cast<FT>(y);
  32. if constexpr (Aggr) {
  33. if (std::isunordered(l, r))
  34. return !std::isnan(l);
  35. }
  36. return l < r;
  37. }
  38. #ifndef MKQL_DISABLE_CODEGEN
  39. Value* GenLessUnsigned(Value* lhs, Value* rhs, BasicBlock* block) {
  40. return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, lhs, rhs, "less", block);
  41. }
  42. Value* GenLessSigned(Value* lhs, Value* rhs, BasicBlock* block) {
  43. return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, lhs, rhs, "less", block);
  44. }
  45. template <bool Aggr>
  46. Value* GenLessFloats(Value* lhs, Value* rhs, BasicBlock* block);
  47. template <>
  48. Value* GenLessFloats<false>(Value* lhs, Value* rhs, BasicBlock* block) {
  49. return CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OLT, lhs, rhs, "less", block);
  50. }
  51. template <>
  52. Value* GenLessFloats<true>(Value* lhs, Value* rhs, BasicBlock* block) {
  53. const auto ult = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ULT, lhs, rhs, "less", block);
  54. const auto ord = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ORD, ConstantFP::get(lhs->getType(), 0.0), lhs, "ordered", block);
  55. return BinaryOperator::CreateAnd(ult, ord, "and", block);
  56. }
  57. template <typename T1, typename T2>
  58. Value* GenLessIntegralLeftSigned(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  59. const auto zero = ConstantInt::get(x->getType(), 0);
  60. const auto neg = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, x, zero, "negative", block);
  61. using T = std::conditional_t<(sizeof(std::make_unsigned_t<T1>) > sizeof(T2)), std::make_unsigned_t<T1>, T2>;
  62. const auto comp = GenLessUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
  63. return SelectInst::Create(neg, ConstantInt::getTrue(context), comp, "result", block);
  64. }
  65. template <typename T1, typename T2>
  66. Value* GenLessIntegralRightSigned(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  67. const auto zero = ConstantInt::get(y->getType(), 0);
  68. const auto neg = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLE, y, zero, "negative", block);
  69. using T = std::conditional_t<(sizeof(T1) > sizeof(std::make_unsigned_t<T2>)), T1, std::make_unsigned_t<T2>>;
  70. const auto comp = GenLessUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
  71. return SelectInst::Create(neg, ConstantInt::getFalse(context), comp, "result", block);
  72. }
  73. template <typename T1, typename T2,
  74. std::enable_if_t<std::is_unsigned<T1>::value && std::is_unsigned<T2>::value, bool> Aggr>
  75. inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  76. using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>;
  77. return GenLessUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
  78. }
  79. template <typename T1, typename T2,
  80. std::enable_if_t<std::is_signed<T1>::value && std::is_signed<T2>::value &&
  81. std::is_integral<T1>::value && std::is_integral<T2>::value, bool> Aggr>
  82. inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  83. using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>;
  84. return GenLessSigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
  85. }
  86. template <typename T1, typename T2,
  87. std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value
  88. && std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool> Aggr>
  89. inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  90. return GenLessIntegralLeftSigned<T1, T2>(x, y, context, block);
  91. }
  92. template <typename T1, typename T2,
  93. std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value
  94. && std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool> Aggr>
  95. inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  96. return GenLessIntegralRightSigned<T1, T2>(x, y, context, block);
  97. }
  98. template <typename T1, typename T2,
  99. std::enable_if_t<std::is_floating_point<T1>::value || std::is_floating_point<T2>::value, bool> Aggr>
  100. inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  101. using F1 = std::conditional_t<std::is_floating_point<T1>::value, T1, T2>;
  102. using F2 = std::conditional_t<std::is_floating_point<T2>::value, T2, T1>;
  103. using FT = std::conditional_t<(sizeof(F1) > sizeof(F2)), F1, F2>;
  104. return GenLessFloats<Aggr>(StaticCast<T1, FT>(x, context, block), StaticCast<T2, FT>(y, context, block), block);
  105. }
  106. #endif
  107. struct TAggrLess {
  108. static bool Simple(bool left, bool right)
  109. {
  110. return !left && right;
  111. }
  112. #ifndef MKQL_DISABLE_CODEGEN
  113. static constexpr CmpInst::Predicate SimplePredicate = ICmpInst::ICMP_ULT;
  114. #endif
  115. };
  116. template<typename TLeft, typename TRight, bool Aggr>
  117. struct TLess : public TCompareArithmeticBinary<TLeft, TRight, TLess<TLeft, TRight, Aggr>>, public TAggrLess {
  118. static bool Do(TLeft left, TRight right)
  119. {
  120. return Less<TLeft, TRight, Aggr>(left, right);
  121. }
  122. #ifndef MKQL_DISABLE_CODEGEN
  123. static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
  124. {
  125. return GenLess<TLeft, TRight, Aggr>(left, right, ctx.Codegen.GetContext(), block);
  126. }
  127. #endif
  128. };
  129. template<typename TLeft, typename TRight, typename TOutput>
  130. struct TLessOp;
  131. template<typename TLeft, typename TRight>
  132. struct TLessOp<TLeft, TRight, bool> : public TLess<TLeft, TRight, false> {
  133. static constexpr auto NullMode = TKernel::ENullMode::Default;
  134. };
  135. template<typename TLeft, typename TRight, bool Aggr>
  136. struct TDiffDateLess : public TCompareArithmeticBinary<typename TLeft::TLayout, typename TRight::TLayout, TDiffDateLess<TLeft, TRight, Aggr>>, public TAggrLess {
  137. static bool Do(typename TLeft::TLayout left, typename TRight::TLayout right)
  138. {
  139. return std::is_same<TLeft, TRight>::value ?
  140. Less<typename TLeft::TLayout, typename TRight::TLayout, Aggr>(left, right):
  141. Less<TScaledDate, TScaledDate, Aggr>(ToScaledDate<TLeft>(left), ToScaledDate<TRight>(right));
  142. }
  143. #ifndef MKQL_DISABLE_CODEGEN
  144. static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
  145. {
  146. auto& context = ctx.Codegen.GetContext();
  147. return std::is_same<TLeft, TRight>::value ?
  148. GenLess<typename TLeft::TLayout, typename TRight::TLayout, Aggr>(left, right, context, block):
  149. GenLess<TScaledDate, TScaledDate, Aggr>(GenToScaledDate<TLeft>(left, context, block), GenToScaledDate<TRight>(right, context, block), context, block);
  150. }
  151. #endif
  152. };
  153. template<typename TLeft, typename TRight, typename TOutput>
  154. struct TDiffDateLessOp;
  155. template<typename TLeft, typename TRight>
  156. struct TDiffDateLessOp<TLeft, TRight, NUdf::TDataType<bool>> : public TDiffDateLess<TLeft, TRight, false> {
  157. static constexpr auto NullMode = TKernel::ENullMode::Default;
  158. };
  159. template<typename TLeft, typename TRight, bool Aggr>
  160. struct TAggrTzDateLess : public TCompareArithmeticBinaryWithTimezone<TLeft, TRight, TAggrTzDateLess<TLeft, TRight, Aggr>>, public TAggrLess {
  161. static bool Do(TLeft left, TRight right)
  162. {
  163. return Less<TLeft, TRight, Aggr>(left, right);
  164. }
  165. static bool DoTz(ui16 left, ui16 right)
  166. {
  167. return Less<ui16, ui16, Aggr>(left, right);
  168. }
  169. #ifndef MKQL_DISABLE_CODEGEN
  170. static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
  171. {
  172. return GenLess<TLeft, TRight, Aggr>(left, right, ctx.Codegen.GetContext(), block);
  173. }
  174. static Value* GenTz(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
  175. {
  176. return GenLess<ui16, ui16, Aggr>(left, right, ctx.Codegen.GetContext(), block);
  177. }
  178. #endif
  179. };
  180. template<NUdf::EDataSlot Slot>
  181. struct TCustomLess : public TAggrLess {
  182. static NUdf::TUnboxedValuePod Execute(NUdf::TUnboxedValuePod left, NUdf::TUnboxedValuePod right) {
  183. return NUdf::TUnboxedValuePod(CompareCustomsWithCleanup<Slot>(left, right) < 0);
  184. }
  185. #ifndef MKQL_DISABLE_CODEGEN
  186. static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
  187. {
  188. auto& context = ctx.Codegen.GetContext();
  189. const auto res = CallBinaryUnboxedValueFunction(&CompareCustoms<Slot>, Type::getInt32Ty(context), left, right, ctx.Codegen, block);
  190. const auto comp = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, res, ConstantInt::get(res->getType(), 0), "less", block);
  191. ValueCleanup(EValueRepresentation::String, left, ctx, block);
  192. ValueCleanup(EValueRepresentation::String, right, ctx, block);
  193. return MakeBoolean(comp, context, block);
  194. }
  195. #endif
  196. };
  197. struct TDecimalLess {
  198. static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& left, const NUdf::TUnboxedValuePod& right) {
  199. const auto l = left.GetInt128();
  200. const auto r = right.GetInt128();
  201. return NUdf::TUnboxedValuePod(NYql::NDecimal::IsComparable(l) && NYql::NDecimal::IsComparable(r) && l < r);
  202. }
  203. #ifndef MKQL_DISABLE_CODEGEN
  204. static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
  205. {
  206. auto& context = ctx.Codegen.GetContext();
  207. const auto l = GetterForInt128(left, block);
  208. const auto r = GetterForInt128(right, block);
  209. const auto lok = NDecimal::GenIsComparable(l, context, block);
  210. const auto rok = NDecimal::GenIsComparable(r, context, block);
  211. const auto both = BinaryOperator::CreateAnd(lok, rok, "both", block);
  212. const auto ls = GenLessSigned(l, r, block);
  213. const auto res = BinaryOperator::CreateAnd(both, ls, "res", block);
  214. return MakeBoolean(res, context, block);
  215. }
  216. #endif
  217. };
  218. struct TDecimalAggrLess : public TAggrLess {
  219. static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& left, const NUdf::TUnboxedValuePod& right) {
  220. const auto l = left.GetInt128();
  221. const auto r = right.GetInt128();
  222. return NUdf::TUnboxedValuePod(l < r);
  223. }
  224. #ifndef MKQL_DISABLE_CODEGEN
  225. static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
  226. {
  227. auto& context = ctx.Codegen.GetContext();
  228. const auto l = GetterForInt128(left, block);
  229. const auto r = GetterForInt128(right, block);
  230. const auto ls = GenLessSigned(l, r, block);
  231. return MakeBoolean(ls, context, block);
  232. }
  233. #endif
  234. };
  235. }
  236. void RegisterLess(IBuiltinFunctionRegistry& registry) {
  237. const auto name = "Less";
  238. RegisterComparePrimitive<TLess, TCompareArgsOpt>(registry, name);
  239. RegisterCompareDatetime<TDiffDateLess, TCompareArgsOpt>(registry, name);
  240. RegisterCompareBigDatetime<TDiffDateLess, TCompareArgsOpt>(registry, name);
  241. RegisterCompareStrings<TCustomLess, TCompareArgsOpt>(registry, name);
  242. RegisterCompareCustomOpt<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, TDecimalLess, TCompareArgsOpt>(registry, name);
  243. const auto aggrName = "AggrLess";
  244. RegisterAggrComparePrimitive<TLess, TCompareArgsOpt>(registry, aggrName);
  245. RegisterAggrCompareDatetime<TDiffDateLess, TCompareArgsOpt>(registry, aggrName);
  246. RegisterAggrCompareTzDatetime<TAggrTzDateLess, TCompareArgsOpt>(registry, aggrName);
  247. RegisterAggrCompareBigDatetime<TDiffDateLess, TCompareArgsOpt>(registry, aggrName);
  248. RegisterAggrCompareBigTzDatetime<TAggrTzDateLess, TCompareArgsOpt>(registry, aggrName);
  249. RegisterAggrCompareStrings<TCustomLess, TCompareArgsOpt>(registry, aggrName);
  250. RegisterAggrCompareCustomOpt<NUdf::TDataType<NUdf::TDecimal>, TDecimalAggrLess, TCompareArgsOpt>(registry, aggrName);
  251. }
  252. void RegisterLess(TKernelFamilyMap& kernelFamilyMap) {
  253. auto family = std::make_unique<TKernelFamilyBase>();
  254. AddNumericComparisonKernels<TLessOp>(*family);
  255. AddDateComparisonKernels<TDiffDateLessOp>(*family);
  256. AddDecimalComparisonKernels<TDecimalLess>(*family);
  257. RegisterStringKernelLess(*family);
  258. kernelFamilyMap["Less"] = std::move(family);
  259. }
  260. } // namespace NMiniKQL
  261. } // namespace NKikimr