2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/arithmeticOverflow.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct MinusImpl
|
|
|
|
{
|
|
|
|
using ResultType = typename NumberTraits::ResultOfSubtraction<A, B>::Type;
|
2020-02-14 07:11:37 +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
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
2018-12-26 23:55:09 +00:00
|
|
|
static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2020-08-19 11:52:17 +00:00
|
|
|
if constexpr (is_big_int_v<A> || is_big_int_v<B>)
|
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
using CastA = std::conditional_t<std::is_floating_point_v<B>, B, A>;
|
|
|
|
using CastB = std::conditional_t<std::is_floating_point_v<A>, A, B>;
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2021-01-26 19:04:03 +00:00
|
|
|
return static_cast<Result>(static_cast<CastA>(a)) - static_cast<Result>(static_cast<CastB>(b));
|
2020-08-19 11:52:17 +00:00
|
|
|
}
|
|
|
|
else
|
2022-10-07 10:46:45 +00:00
|
|
|
return static_cast<Result>(a) - static_cast<Result>(b);
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 19:56:53 +00:00
|
|
|
/// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
|
2018-11-26 16:20:40 +00:00
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline bool apply(A a, B b, Result & c)
|
|
|
|
{
|
|
|
|
return common::subOverflow(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.CreateSub(left, right) : b.CreateFSub(left, right);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameMinus { static constexpr auto name = "minus"; };
|
2020-09-05 14:12:47 +00:00
|
|
|
using FunctionMinus = BinaryArithmeticOverloadResolver<MinusImpl, NameMinus>;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(Minus)
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionMinus>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|