#include "mkql_builtins_compare.h" #include "mkql_builtins_datetime.h" #include "mkql_builtins_decimal.h" // Y_IGNORE #include "mkql_builtins_string_kernels.h" #include namespace NKikimr { namespace NMiniKQL { namespace { template ::value && std::is_integral::value && std::is_signed::value == std::is_signed::value, bool> Aggr> Y_FORCE_INLINE bool Less(T1 x, T2 y) { return x < y; } template ::value && std::is_integral::value && std::is_signed::value && std::is_unsigned::value, bool> Aggr> Y_FORCE_INLINE bool Less(T1 x, T2 y) { return x < T1(0) || static_cast>(x) < y; } template ::value && std::is_integral::value && std::is_unsigned::value && std::is_signed::value, bool> Aggr> Y_FORCE_INLINE bool Less(T1 x, T2 y) { return T2(0) < y && x < static_cast>(y); } template ::value || std::is_floating_point::value, bool> Aggr> Y_FORCE_INLINE bool Less(T1 x, T2 y) { using F1 = std::conditional_t::value, T1, T2>; using F2 = std::conditional_t::value, T2, T1>; using FT = std::conditional_t<(sizeof(F1) > sizeof(F2)), F1, F2>; const auto l = static_cast(x); const auto r = static_cast(y); if constexpr (Aggr) { if (std::isunordered(l, r)) return !std::isnan(l); } return l < r; } #ifndef MKQL_DISABLE_CODEGEN Value* GenLessUnsigned(Value* lhs, Value* rhs, BasicBlock* block) { return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, lhs, rhs, "less", block); } Value* GenLessSigned(Value* lhs, Value* rhs, BasicBlock* block) { return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, lhs, rhs, "less", block); } template Value* GenLessFloats(Value* lhs, Value* rhs, BasicBlock* block); template <> Value* GenLessFloats(Value* lhs, Value* rhs, BasicBlock* block) { return CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OLT, lhs, rhs, "less", block); } template <> Value* GenLessFloats(Value* lhs, Value* rhs, BasicBlock* block) { const auto ult = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ULT, lhs, rhs, "less", block); const auto ord = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ORD, ConstantFP::get(lhs->getType(), 0.0), lhs, "ordered", block); return BinaryOperator::CreateAnd(ult, ord, "and", block); } template Value* GenLessIntegralLeftSigned(Value* x, Value* y, LLVMContext &context, BasicBlock* block) { const auto zero = ConstantInt::get(x->getType(), 0); const auto neg = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, x, zero, "negative", block); using T = std::conditional_t<(sizeof(std::make_unsigned_t) > sizeof(T2)), std::make_unsigned_t, T2>; const auto comp = GenLessUnsigned(StaticCast(x, context, block), StaticCast(y, context, block), block); return SelectInst::Create(neg, ConstantInt::getTrue(context), comp, "result", block); } template Value* GenLessIntegralRightSigned(Value* x, Value* y, LLVMContext &context, BasicBlock* block) { const auto zero = ConstantInt::get(y->getType(), 0); const auto neg = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLE, y, zero, "negative", block); using T = std::conditional_t<(sizeof(T1) > sizeof(std::make_unsigned_t)), T1, std::make_unsigned_t>; const auto comp = GenLessUnsigned(StaticCast(x, context, block), StaticCast(y, context, block), block); return SelectInst::Create(neg, ConstantInt::getFalse(context), comp, "result", block); } template ::value && std::is_unsigned::value, bool> Aggr> inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) { using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>; return GenLessUnsigned(StaticCast(x, context, block), StaticCast(y, context, block), block); } template ::value && std::is_signed::value && std::is_integral::value && std::is_integral::value, bool> Aggr> inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) { using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>; return GenLessSigned(StaticCast(x, context, block), StaticCast(y, context, block), block); } template ::value && std::is_integral::value && std::is_signed::value && std::is_unsigned::value, bool> Aggr> inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) { return GenLessIntegralLeftSigned(x, y, context, block); } template ::value && std::is_integral::value && std::is_unsigned::value && std::is_signed::value, bool> Aggr> inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) { return GenLessIntegralRightSigned(x, y, context, block); } template ::value || std::is_floating_point::value, bool> Aggr> inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) { using F1 = std::conditional_t::value, T1, T2>; using F2 = std::conditional_t::value, T2, T1>; using FT = std::conditional_t<(sizeof(F1) > sizeof(F2)), F1, F2>; return GenLessFloats(StaticCast(x, context, block), StaticCast(y, context, block), block); } #endif struct TAggrLess { static bool Simple(bool left, bool right) { return !left && right; } #ifndef MKQL_DISABLE_CODEGEN static constexpr CmpInst::Predicate SimplePredicate = ICmpInst::ICMP_ULT; #endif }; template struct TLess : public TCompareArithmeticBinary>, public TAggrLess { static bool Do(TLeft left, TRight right) { return Less(left, right); } #ifndef MKQL_DISABLE_CODEGEN static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block) { return GenLess(left, right, ctx.Codegen.GetContext(), block); } #endif }; template struct TLessOp; template struct TLessOp : public TLess { static constexpr auto NullMode = TKernel::ENullMode::Default; }; template struct TDiffDateLess : public TCompareArithmeticBinary>, public TAggrLess { static bool Do(typename TLeft::TLayout left, typename TRight::TLayout right) { return std::is_same::value ? Less(left, right): Less(ToScaledDate(left), ToScaledDate(right)); } #ifndef MKQL_DISABLE_CODEGEN static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block) { auto& context = ctx.Codegen.GetContext(); return std::is_same::value ? GenLess(left, right, context, block): GenLess(GenToScaledDate(left, context, block), GenToScaledDate(right, context, block), context, block); } #endif }; template struct TDiffDateLessOp; template struct TDiffDateLessOp> : public TDiffDateLess { static constexpr auto NullMode = TKernel::ENullMode::Default; }; template struct TAggrTzDateLess : public TCompareArithmeticBinaryWithTimezone>, public TAggrLess { static bool Do(TLeft left, TRight right) { return Less(left, right); } static bool DoTz(ui16 left, ui16 right) { return Less(left, right); } #ifndef MKQL_DISABLE_CODEGEN static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block) { return GenLess(left, right, ctx.Codegen.GetContext(), block); } static Value* GenTz(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block) { return GenLess(left, right, ctx.Codegen.GetContext(), block); } #endif }; template struct TCustomLess : public TAggrLess { static NUdf::TUnboxedValuePod Execute(NUdf::TUnboxedValuePod left, NUdf::TUnboxedValuePod right) { return NUdf::TUnboxedValuePod(CompareCustomsWithCleanup(left, right) < 0); } #ifndef MKQL_DISABLE_CODEGEN static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block) { auto& context = ctx.Codegen.GetContext(); const auto res = CallBinaryUnboxedValueFunction(&CompareCustoms, Type::getInt32Ty(context), left, right, ctx.Codegen, block); const auto comp = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, res, ConstantInt::get(res->getType(), 0), "less", block); ValueCleanup(EValueRepresentation::String, left, ctx, block); ValueCleanup(EValueRepresentation::String, right, ctx, block); return MakeBoolean(comp, context, block); } #endif }; struct TDecimalLess { static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& left, const NUdf::TUnboxedValuePod& right) { const auto l = left.GetInt128(); const auto r = right.GetInt128(); return NUdf::TUnboxedValuePod(NYql::NDecimal::IsComparable(l) && NYql::NDecimal::IsComparable(r) && l < r); } #ifndef MKQL_DISABLE_CODEGEN static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block) { auto& context = ctx.Codegen.GetContext(); const auto l = GetterForInt128(left, block); const auto r = GetterForInt128(right, block); const auto lok = NDecimal::GenIsComparable(l, context, block); const auto rok = NDecimal::GenIsComparable(r, context, block); const auto both = BinaryOperator::CreateAnd(lok, rok, "both", block); const auto ls = GenLessSigned(l, r, block); const auto res = BinaryOperator::CreateAnd(both, ls, "res", block); return MakeBoolean(res, context, block); } #endif }; struct TDecimalAggrLess : public TAggrLess { static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& left, const NUdf::TUnboxedValuePod& right) { const auto l = left.GetInt128(); const auto r = right.GetInt128(); return NUdf::TUnboxedValuePod(l < r); } #ifndef MKQL_DISABLE_CODEGEN static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block) { auto& context = ctx.Codegen.GetContext(); const auto l = GetterForInt128(left, block); const auto r = GetterForInt128(right, block); const auto ls = GenLessSigned(l, r, block); return MakeBoolean(ls, context, block); } #endif }; } void RegisterLess(IBuiltinFunctionRegistry& registry) { const auto name = "Less"; RegisterComparePrimitive(registry, name); RegisterCompareDatetime(registry, name); RegisterCompareBigDatetime(registry, name); RegisterCompareStrings(registry, name); RegisterCompareCustomOpt, NUdf::TDataType, TDecimalLess, TCompareArgsOpt>(registry, name); const auto aggrName = "AggrLess"; RegisterAggrComparePrimitive(registry, aggrName); RegisterAggrCompareDatetime(registry, aggrName); RegisterAggrCompareTzDatetime(registry, aggrName); RegisterAggrCompareBigDatetime(registry, aggrName); RegisterAggrCompareBigTzDatetime(registry, aggrName); RegisterAggrCompareStrings(registry, aggrName); RegisterAggrCompareCustomOpt, TDecimalAggrLess, TCompareArgsOpt>(registry, aggrName); } void RegisterLess(TKernelFamilyMap& kernelFamilyMap) { auto family = std::make_unique(); AddNumericComparisonKernels(*family); AddDateComparisonKernels(*family); AddDecimalComparisonKernels(*family); RegisterStringKernelLess(*family); kernelFamilyMap["Less"] = std::move(family); } } // namespace NMiniKQL } // namespace NKikimr