ClickHouse
54 строки · 1.7 Кб
1#include <Functions/FunctionFactory.h>2#include <Functions/FunctionBinaryArithmetic.h>3#include <base/arithmeticOverflow.h>4
5namespace DB6{
7
8template <typename A, typename B>9struct MinusImpl10{
11using ResultType = typename NumberTraits::ResultOfSubtraction<A, B>::Type;12static const constexpr bool allow_fixed_string = false;13static const constexpr bool allow_string_integer = false;14
15template <typename Result = ResultType>16static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)17{18if constexpr (is_big_int_v<A> || is_big_int_v<B>)19{20using CastA = std::conditional_t<std::is_floating_point_v<B>, B, A>;21using CastB = std::conditional_t<std::is_floating_point_v<A>, A, B>;22
23return static_cast<Result>(static_cast<CastA>(a)) - static_cast<Result>(static_cast<CastB>(b));24}25else26return static_cast<Result>(a) - static_cast<Result>(b);27}28
29/// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.30template <typename Result = ResultType>31static inline bool apply(A a, B b, Result & c)32{33return common::subOverflow(static_cast<Result>(a), b, c);34}35
36#if USE_EMBEDDED_COMPILER37static constexpr bool compilable = true;38
39static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)40{41return left->getType()->isIntegerTy() ? b.CreateSub(left, right) : b.CreateFSub(left, right);42}43#endif44};45
46struct NameMinus { static constexpr auto name = "minus"; };47using FunctionMinus = BinaryArithmeticOverloadResolver<MinusImpl, NameMinus>;48
49REGISTER_FUNCTION(Minus)50{
51factory.registerFunction<FunctionMinus>();52}
53
54}
55