2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionUnaryArithmetic.h>
|
|
|
|
#include <DataTypes/NumberTraits.h>
|
2021-06-14 04:13:35 +00:00
|
|
|
#include <Common/FieldVisitorConvertToNumber.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename A>
|
|
|
|
struct AbsImpl
|
|
|
|
{
|
2021-09-10 11:49:22 +00:00
|
|
|
using ResultType = std::conditional_t<is_decimal<A>, A, typename NumberTraits::ResultOfAbs<A>::Type>;
|
2020-02-14 08:17:32 +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
|
|
|
|
2018-12-26 23:55:09 +00:00
|
|
|
static inline NO_SANITIZE_UNDEFINED ResultType apply(A a)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2021-09-10 11:49:22 +00:00
|
|
|
if constexpr (is_decimal<A>)
|
2020-08-19 11:52:17 +00:00
|
|
|
return a < A(0) ? A(-a) : a;
|
2020-09-04 13:33:02 +00:00
|
|
|
else if constexpr (is_big_int_v<A> && is_signed_v<A>)
|
|
|
|
return (a < 0) ? -a : a;
|
2021-09-10 11:49:22 +00:00
|
|
|
else if constexpr (is_integer<A> && is_signed_v<A>)
|
2020-09-04 13:33:02 +00:00
|
|
|
return a < 0 ? static_cast<ResultType>(~a) + 1 : static_cast<ResultType>(a);
|
2021-09-10 11:49:22 +00:00
|
|
|
else if constexpr (is_integer<A> && is_unsigned_v<A>)
|
2018-11-26 16:20:40 +00:00
|
|
|
return static_cast<ResultType>(a);
|
|
|
|
else if constexpr (std::is_floating_point_v<A>)
|
|
|
|
return static_cast<ResultType>(std::abs(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false; /// special type handling, some other time
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameAbs { static constexpr auto name = "abs"; };
|
|
|
|
using FunctionAbs = FunctionUnaryArithmetic<AbsImpl, NameAbs, false>;
|
|
|
|
|
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameAbs>
|
|
|
|
{
|
|
|
|
static bool has() { return true; }
|
|
|
|
static IFunction::Monotonicity get(const Field & left, const Field & right)
|
|
|
|
{
|
|
|
|
Float64 left_float = left.isNull() ? -std::numeric_limits<Float64>::infinity() : applyVisitor(FieldVisitorConvertToNumber<Float64>(), left);
|
|
|
|
Float64 right_float = right.isNull() ? std::numeric_limits<Float64>::infinity() : applyVisitor(FieldVisitorConvertToNumber<Float64>(), right);
|
|
|
|
|
|
|
|
if ((left_float < 0 && right_float > 0) || (left_float > 0 && right_float < 0))
|
|
|
|
return {};
|
|
|
|
|
2021-09-29 16:01:26 +00:00
|
|
|
return { .is_monotonic = true, .is_positive = left_float > 0 };
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerFunctionAbs(FunctionFactory & factory)
|
|
|
|
{
|
2018-12-19 14:46:18 +00:00
|
|
|
factory.registerFunction<FunctionAbs>(FunctionFactory::CaseInsensitive);
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|