ClickHouse/src/Functions/sign.cpp

52 lines
1.3 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;
static const constexpr bool allow_string_integer = 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-09-10 11:49:22 +00:00
if constexpr (is_decimal<A> || std::is_floating_point_v<A>)
2021-01-26 11:26:15 +00:00
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 { .is_monotonic = true };
}
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
}