ClickHouse/src/Functions/sign.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 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;
2023-04-22 17:21:43 +00:00
static constexpr bool allow_string_or_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-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
};
REGISTER_FUNCTION(Sign)
2021-01-24 05:18:59 +00:00
{
2022-08-27 20:06:03 +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
}