1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionBinaryArithmetic.h>
3#include <common/arithmeticOverflow.h>
4
5namespace DB
6{
7
8template <typename A, typename B>
9struct MinusImpl
10{
11 using ResultType = typename NumberTraits::ResultOfSubtraction<A, B>::Type;
12 static const constexpr bool allow_decimal = true;
13
14 template <typename Result = ResultType>
15 static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
16 {
17 return static_cast<Result>(a) - b;
18 }
19
20 /// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
21 template <typename Result = ResultType>
22 static inline bool apply(A a, B b, Result & c)
23 {
24 return common::subOverflow(static_cast<Result>(a), b, c);
25 }
26
27#if USE_EMBEDDED_COMPILER
28 static constexpr bool compilable = true;
29
30 static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool)
31 {
32 return left->getType()->isIntegerTy() ? b.CreateSub(left, right) : b.CreateFSub(left, right);
33 }
34#endif
35};
36
37struct NameMinus { static constexpr auto name = "minus"; };
38using FunctionMinus = FunctionBinaryArithmetic<MinusImpl, NameMinus>;
39
40void registerFunctionMinus(FunctionFactory & factory)
41{
42 factory.registerFunction<FunctionMinus>();
43}
44
45}
46