ClickHouse
46 строк · 1.2 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionUnaryArithmetic.h>
3#include <DataTypes/NumberTraits.h>
4
5namespace DB
6{
7
8template <typename A>
9struct NegateImpl
10{
11using ResultType = std::conditional_t<is_decimal<A>, A, typename NumberTraits::ResultOfNegate<A>::Type>;
12static constexpr const bool allow_string_or_fixed_string = false;
13
14static inline NO_SANITIZE_UNDEFINED ResultType apply(A a)
15{
16return -static_cast<ResultType>(a);
17}
18
19#if USE_EMBEDDED_COMPILER
20static constexpr bool compilable = true;
21
22static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * arg, bool)
23{
24return arg->getType()->isIntegerTy() ? b.CreateNeg(arg) : b.CreateFNeg(arg);
25}
26#endif
27};
28
29struct NameNegate { static constexpr auto name = "negate"; };
30using FunctionNegate = FunctionUnaryArithmetic<NegateImpl, NameNegate, true>;
31
32template <> struct FunctionUnaryArithmeticMonotonicity<NameNegate>
33{
34static bool has() { return true; }
35static IFunction::Monotonicity get(const Field &, const Field &)
36{
37return { .is_monotonic = true, .is_positive = false, .is_strict = true };
38}
39};
40
41REGISTER_FUNCTION(Negate)
42{
43factory.registerFunction<FunctionNegate>();
44}
45
46}
47