2018-11-26 16:20:40 +00:00
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
|
|
|
|
#include <Core/AccurateComparison.h>
|
2020-04-16 22:28:08 +00:00
|
|
|
|
#include <Functions/LeastGreatestGeneric.h>
|
|
|
|
|
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
|
struct LeastBaseImpl
|
|
|
|
|
{
|
|
|
|
|
using ResultType = NumberTraits::ResultOfLeast<A, B>;
|
2020-02-14 07:11:37 +00:00
|
|
|
|
static const constexpr bool allow_fixed_string = false;
|
2021-08-30 06:37:23 +00:00
|
|
|
|
static const constexpr bool allow_string_integer = false;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
|
/** gcc 4.9.2 successfully vectorizes a loop from this function. */
|
2021-01-26 19:04:03 +00:00
|
|
|
|
return static_cast<Result>(a) < static_cast<Result>(b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
2018-11-26 16:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
static constexpr bool compilable = true;
|
|
|
|
|
|
|
|
|
|
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool is_signed)
|
|
|
|
|
{
|
|
|
|
|
if (!left->getType()->isIntegerTy())
|
2021-05-05 20:08:31 +00:00
|
|
|
|
{
|
|
|
|
|
/// Follows the IEEE-754 semantics for minNum, except for handling of signaling NaNs. This match’s the behavior of libc fmin.
|
2018-11-26 16:20:40 +00:00
|
|
|
|
return b.CreateMinNum(left, right);
|
2021-05-05 20:08:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto * compare_value = is_signed ? b.CreateICmpSLT(left, right) : b.CreateICmpULT(left, right);
|
|
|
|
|
return b.CreateSelect(compare_value, left, right);
|
2018-11-26 16:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
|
struct LeastSpecialImpl
|
|
|
|
|
{
|
|
|
|
|
using ResultType = std::make_signed_t<A>;
|
2020-02-14 07:11:37 +00:00
|
|
|
|
static const constexpr bool allow_fixed_string = false;
|
2021-08-30 06:37:23 +00:00
|
|
|
|
static const constexpr bool allow_string_integer = false;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
|
{
|
|
|
|
|
static_assert(std::is_same_v<Result, ResultType>, "ResultType != Result");
|
|
|
|
|
return accurate::lessOp(a, b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
static constexpr bool compilable = false; /// ???
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
|
using LeastImpl = std::conditional_t<!NumberTraits::LeastGreatestSpecialCase<A, B>, LeastBaseImpl<A, B>, LeastSpecialImpl<A, B>>;
|
|
|
|
|
|
|
|
|
|
struct NameLeast { static constexpr auto name = "least"; };
|
|
|
|
|
using FunctionLeast = FunctionBinaryArithmetic<LeastImpl, NameLeast>;
|
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
|
REGISTER_FUNCTION(Least)
|
2018-11-26 16:20:40 +00:00
|
|
|
|
{
|
2022-08-27 20:06:03 +00:00
|
|
|
|
factory.registerFunction<LeastGreatestOverloadResolver<LeastGreatest::Least, FunctionLeast>>({}, FunctionFactory::CaseInsensitive);
|
2018-11-26 16:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|