ClickHouse/dbms/src/Functions/plus.cpp

47 lines
1.4 KiB
C++
Raw Normal View History

#include <Functions/FunctionFactory.h>
#include <Functions/FunctionBinaryArithmetic.h>
#include <common/arithmeticOverflow.h>
namespace DB
{
template <typename A, typename B>
struct PlusImpl
{
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
static const constexpr bool allow_decimal = true;
template <typename Result = ResultType>
2018-12-26 23:55:09 +00:00
static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
{
/// Next everywhere, static_cast - so that there is no wrong result in expressions of the form Int64 c = UInt32(a) * Int32(-1).
return static_cast<Result>(a) + b;
}
/// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false othervise.
template <typename Result = ResultType>
static inline bool apply(A a, B b, Result & c)
{
return common::addOverflow(static_cast<Result>(a), b, c);
}
#if USE_EMBEDDED_COMPILER
static constexpr bool compilable = true;
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
{
return left->getType()->isIntegerTy() ? b.CreateAdd(left, right) : b.CreateFAdd(left, right);
}
#endif
};
struct NamePlus { static constexpr auto name = "plus"; };
using FunctionPlus = FunctionBinaryArithmetic<PlusImpl, NamePlus>;
void registerFunctionPlus(FunctionFactory & factory)
{
factory.registerFunction<FunctionPlus>();
}
}