mkql_builtins_greater.cpp 14 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 Greater(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 Greater(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 Greater(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 Greater(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(r);
  35. }
  36. return l > r;
  37. }
  38. #ifndef MKQL_DISABLE_CODEGEN
  39. Value* GenGreaterUnsigned(Value* lhs, Value* rhs, BasicBlock* block) {
  40. return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, lhs, rhs, "greater", block);
  41. }
  42. Value* GenGreaterSigned(Value* lhs, Value* rhs, BasicBlock* block) {
  43. return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, lhs, rhs, "greater", block);
  44. }
  45. template <bool Aggr>
  46. Value* GenGreaterFloats(Value* lhs, Value* rhs, BasicBlock* block);
  47. template <>
  48. Value* GenGreaterFloats<false>(Value* lhs, Value* rhs, BasicBlock* block) {
  49. return CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OGT, lhs, rhs, "greater", block);
  50. }
  51. template <>
  52. Value* GenGreaterFloats<true>(Value* lhs, Value* rhs, BasicBlock* block) {
  53. const auto ugt = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_UGT, lhs, rhs, "greater", block);
  54. const auto ord = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ORD, ConstantFP::get(rhs->getType(), 0.0), rhs, "ordered", block);
  55. return BinaryOperator::CreateAnd(ugt, ord, "and", block);
  56. }
  57. template <typename T1, typename T2>
  58. Value* GenGreaterIntegralLeftSigned(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_SLE, 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 = GenGreaterUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
  63. return SelectInst::Create(neg, ConstantInt::getFalse(context), comp, "result", block);
  64. }
  65. template <typename T1, typename T2>
  66. Value* GenGreaterIntegralRightSigned(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_SLT, 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 = GenGreaterUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
  71. return SelectInst::Create(neg, ConstantInt::getTrue(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* GenGreater(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  76. using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>;
  77. return GenGreaterUnsigned(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* GenGreater(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  83. using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>;
  84. return GenGreaterSigned(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* GenGreater(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  90. return GenGreaterIntegralLeftSigned<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* GenGreater(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
  96. return GenGreaterIntegralRightSigned<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* GenGreater(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 GenGreaterFloats<Aggr>(StaticCast<T1, FT>(x, context, block), StaticCast<T2, FT>(y, context, block), block);
  105. }
  106. #endif
  107. struct TAggrGreater {
  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_UGT;
  114. #endif
  115. };
  116. template<typename TLeft, typename TRight, bool Aggr>
  117. struct TGreater : public TCompareArithmeticBinary<TLeft, TRight, TGreater<TLeft, TRight, Aggr>>, public TAggrGreater {
  118. static bool Do(TLeft left, TRight right)
  119. {
  120. return Greater<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 GenGreater<TLeft, TRight, Aggr>(left, right, ctx.Codegen.GetContext(), block);
  126. }
  127. #endif
  128. };
  129. template<typename TLeft, typename TRight, typename TOutput>
  130. struct TGreaterOp;
  131. template<typename TLeft, typename TRight>
  132. struct TGreaterOp<TLeft, TRight, bool> : public TGreater<TLeft, TRight, false> {
  133. static constexpr auto NullMode = TKernel::ENullMode::Default;
  134. };
  135. template<typename TLeft, typename TRight, bool Aggr>
  136. struct TDiffDateGreater : public TCompareArithmeticBinary<typename TLeft::TLayout, typename TRight::TLayout, TDiffDateGreater<TLeft, TRight, Aggr>>, public TAggrGreater {
  137. static bool Do(typename TLeft::TLayout left, typename TRight::TLayout right)
  138. {
  139. return std::is_same<TLeft, TRight>::value ?
  140. Greater<typename TLeft::TLayout, typename TRight::TLayout, Aggr>(left, right):
  141. Greater<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. GenGreater<typename TLeft::TLayout, typename TRight::TLayout, Aggr>(left, right, context, block):
  149. GenGreater<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 TDiffDateGreaterOp;
  155. template<typename TLeft, typename TRight>
  156. struct TDiffDateGreaterOp<TLeft, TRight, NUdf::TDataType<bool>> : public TDiffDateGreater<TLeft, TRight, false> {
  157. static constexpr auto NullMode = TKernel::ENullMode::Default;
  158. };
  159. template<typename TLeft, typename TRight, bool Aggr>
  160. struct TAggrTzDateGreater : public TCompareArithmeticBinaryWithTimezone<TLeft, TRight, TAggrTzDateGreater<TLeft, TRight, Aggr>>, public TAggrGreater {
  161. static bool Do(TLeft left, TRight right)
  162. {
  163. return Greater<TLeft, TRight, Aggr>(left, right);
  164. }
  165. static bool DoTz(ui16 left, ui16 right)
  166. {
  167. return Greater<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 GenGreater<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 GenGreater<ui16, ui16, Aggr>(left, right, ctx.Codegen.GetContext(), block);
  177. }
  178. #endif
  179. };
  180. template<NUdf::EDataSlot Slot>
  181. struct TCustomGreater : public TAggrGreater {
  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_SGT, res, ConstantInt::get(res->getType(), 0), "greater", 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 TDecimalGreater {
  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 gt = GenGreaterSigned(l, r, block);
  213. const auto res = BinaryOperator::CreateAnd(both, gt, "res", block);
  214. return MakeBoolean(res, context, block);
  215. }
  216. #endif
  217. };
  218. struct TDecimalAggrGreater : public TAggrGreater {
  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 gt = GenGreaterSigned(l, r, block);
  231. return MakeBoolean(gt, context, block);
  232. }
  233. #endif
  234. };
  235. }
  236. void RegisterGreater(IBuiltinFunctionRegistry& registry) {
  237. const auto name = "Greater";
  238. RegisterComparePrimitive<TGreater, TCompareArgsOpt>(registry, name);
  239. RegisterCompareDatetime<TDiffDateGreater, TCompareArgsOpt>(registry, name);
  240. RegisterCompareBigDatetime<TDiffDateGreater, TCompareArgsOpt>(registry, name);
  241. RegisterCompareStrings<TCustomGreater, TCompareArgsOpt>(registry, name);
  242. RegisterCompareCustomOpt<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, TDecimalGreater, TCompareArgsOpt>(registry, name);
  243. const auto aggrName = "AggrGreater";
  244. RegisterAggrComparePrimitive<TGreater, TCompareArgsOpt>(registry, aggrName);
  245. RegisterAggrCompareDatetime<TDiffDateGreater, TCompareArgsOpt>(registry, aggrName);
  246. RegisterAggrCompareTzDatetime<TAggrTzDateGreater, TCompareArgsOpt>(registry, aggrName);
  247. RegisterAggrCompareBigDatetime<TDiffDateGreater, TCompareArgsOpt>(registry, aggrName);
  248. RegisterAggrCompareBigTzDatetime<TAggrTzDateGreater, TCompareArgsOpt>(registry, aggrName);
  249. RegisterAggrCompareStrings<TCustomGreater, TCompareArgsOpt>(registry, aggrName);
  250. RegisterAggrCompareCustomOpt<NUdf::TDataType<NUdf::TDecimal>, TDecimalAggrGreater, TCompareArgsOpt>(registry, aggrName);
  251. }
  252. void RegisterGreater(TKernelFamilyMap& kernelFamilyMap) {
  253. auto family = std::make_unique<TKernelFamilyBase>();
  254. AddNumericComparisonKernels<TGreaterOp>(*family);
  255. AddDateComparisonKernels<TDiffDateGreaterOp>(*family);
  256. AddDecimalComparisonKernels<TDecimalGreater>(*family);
  257. RegisterStringKernelGreater(*family);
  258. kernelFamilyMap["Greater"] = std::move(family);
  259. }
  260. } // namespace NMiniKQL
  261. } // namespace NKikimr