aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/abs.cpp
blob: 25ff6cc93d0a8763dffe84b09f897489a97c49d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionUnaryArithmetic.h>
#include <DataTypes/NumberTraits.h>
#include <Common/FieldVisitorConvertToNumber.h>

namespace DB
{

template <typename A>
struct AbsImpl
{
    using ResultType = std::conditional_t<is_decimal<A>, A, typename NumberTraits::ResultOfAbs<A>::Type>;
    static constexpr bool allow_string_or_fixed_string = false;

    static inline NO_SANITIZE_UNDEFINED ResultType apply(A a)
    {
        if constexpr (is_decimal<A>)
            return a < A(0) ? A(-a) : a;
        else if constexpr (is_big_int_v<A> && is_signed_v<A>)
            return (a < 0) ? -a : a;
        else if constexpr (is_integer<A> && is_signed_v<A>)
            return a < 0 ? static_cast<ResultType>(~a) + 1 : static_cast<ResultType>(a);
        else if constexpr (is_integer<A> && is_unsigned_v<A>)
            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 {};

        return { .is_monotonic = true, .is_positive = left_float > 0, .is_strict = true, };
    }
};

REGISTER_FUNCTION(Abs)
{
    factory.registerFunction<FunctionAbs>({}, FunctionFactory::CaseInsensitive);
}

}