ClickHouse

Форк
0
56 строк · 1.9 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/FunctionBinaryArithmetic.h>
3
#include <base/arithmeticOverflow.h>
4

5
namespace DB
6
{
7

8
template <typename A, typename B>
9
struct PlusImpl
10
{
11
    using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
12
    static const constexpr bool allow_fixed_string = false;
13
    static const constexpr bool allow_string_integer = false;
14
    static const constexpr bool is_commutative = true;
15

16
    template <typename Result = ResultType>
17
    static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
18
    {
19
        /// Next everywhere, static_cast - so that there is no wrong result in expressions of the form Int64 c = UInt32(a) * Int32(-1).
20
        if constexpr (is_big_int_v<A> || is_big_int_v<B>)
21
        {
22
            using CastA = std::conditional_t<std::is_floating_point_v<B>, B, A>;
23
            using CastB = std::conditional_t<std::is_floating_point_v<A>, A, B>;
24

25
            return static_cast<Result>(static_cast<CastA>(a)) + static_cast<Result>(static_cast<CastB>(b));
26
        }
27
        else
28
            return static_cast<Result>(a) + static_cast<Result>(b);
29
    }
30

31
    /// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
32
    template <typename Result = ResultType>
33
    static inline bool apply(A a, B b, Result & c)
34
    {
35
        return common::addOverflow(static_cast<Result>(a), b, c);
36
    }
37

38
#if USE_EMBEDDED_COMPILER
39
    static constexpr bool compilable = true;
40

41
    static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
42
    {
43
        return left->getType()->isIntegerTy() ? b.CreateAdd(left, right) : b.CreateFAdd(left, right);
44
    }
45
#endif
46
};
47

48
struct NamePlus { static constexpr auto name = "plus"; };
49
using FunctionPlus = BinaryArithmeticOverloadResolver<PlusImpl, NamePlus>;
50

51
REGISTER_FUNCTION(Plus)
52
{
53
    factory.registerFunction<FunctionPlus>();
54
}
55

56
}
57

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.