ClickHouse/src/Functions/sign.cpp

48 lines
1.2 KiB
C++
Raw Normal View History

2021-01-24 05:18:59 +00:00
#include <Functions/FunctionFactory.h>
2021-01-26 11:26:15 +00:00
#include <Functions/FunctionUnaryArithmetic.h>
#include <DataTypes/NumberTraits.h>
2021-05-03 22:46:51 +00:00
2021-01-24 05:18:59 +00:00
namespace DB
{
2021-01-26 11:26:15 +00:00
template <typename A>
struct SignImpl
2021-01-24 05:18:59 +00:00
{
2021-01-26 11:26:15 +00:00
using ResultType = Int8;
static const constexpr bool allow_fixed_string = false;
2021-01-24 05:18:59 +00:00
2021-01-26 11:26:15 +00:00
static inline NO_SANITIZE_UNDEFINED ResultType apply(A a)
2021-01-24 05:18:59 +00:00
{
2021-01-26 11:26:15 +00:00
if constexpr (IsDecimalNumber<A> || std::is_floating_point_v<A>)
return a < A(0) ? -1 : a == A(0) ? 0 : 1;
else if constexpr (is_signed_v<A>)
return a < 0 ? -1 : a == 0 ? 0 : 1;
else if constexpr (is_unsigned_v<A>)
return a == 0 ? 0 : 1;
2021-01-24 05:18:59 +00:00
}
2021-01-26 11:26:15 +00:00
#if USE_EMBEDDED_COMPILER
static constexpr bool compilable = false;
#endif
};
2021-01-24 05:18:59 +00:00
2021-01-26 11:26:15 +00:00
struct NameSign
{
static constexpr auto name = "sign";
};
using FunctionSign = FunctionUnaryArithmetic<SignImpl, NameSign, false>;
2021-01-24 05:18:59 +00:00
2021-01-26 11:26:15 +00:00
template <>
struct FunctionUnaryArithmeticMonotonicity<NameSign>
{
static bool has() { return true; }
static IFunction::Monotonicity get(const Field &, const Field &) { return {true, true, false}; }
2021-01-24 05:18:59 +00:00
};
void registerFunctionSign(FunctionFactory & factory)
{
2021-01-26 11:26:15 +00:00
factory.registerFunction<FunctionSign>(FunctionFactory::CaseInsensitive);
2021-01-24 05:18:59 +00:00
}
2021-01-26 11:26:15 +00:00
2021-01-24 05:18:59 +00:00
}