ClickHouse

Форк
0
/
greatest.cpp 
71 строка · 2.3 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/FunctionBinaryArithmetic.h>
3
#include <Core/AccurateComparison.h>
4
#include <Functions/LeastGreatestGeneric.h>
5

6

7
namespace DB
8
{
9

10
template <typename A, typename B>
11
struct GreatestBaseImpl
12
{
13
    using ResultType = NumberTraits::ResultOfGreatest<A, B>;
14
    static const constexpr bool allow_fixed_string = false;
15
    static const constexpr bool allow_string_integer = false;
16

17
    template <typename Result = ResultType>
18
    static inline Result apply(A a, B b)
19
    {
20
        return static_cast<Result>(a) > static_cast<Result>(b) ?
21
               static_cast<Result>(a) : static_cast<Result>(b);
22
    }
23

24
#if USE_EMBEDDED_COMPILER
25
    static constexpr bool compilable = true;
26

27
    static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * left, llvm::Value * right, bool is_signed)
28
    {
29
        if (!left->getType()->isIntegerTy())
30
        {
31
            /// Follows the IEEE-754 semantics for maxNum except for the handling of signaling NaNs. This matches the behavior of libc fmax.
32
            return b.CreateMaxNum(left, right);
33
        }
34

35
        auto * compare_value = is_signed ? b.CreateICmpSGT(left, right) : b.CreateICmpUGT(left, right);
36
        return b.CreateSelect(compare_value, left, right);
37
    }
38
#endif
39
};
40

41
template <typename A, typename B>
42
struct GreatestSpecialImpl
43
{
44
    using ResultType = make_unsigned_t<A>;
45
    static const constexpr bool allow_fixed_string = false;
46
    static const constexpr bool allow_string_integer = false;
47

48
    template <typename Result = ResultType>
49
    static inline Result apply(A a, B b)
50
    {
51
        static_assert(std::is_same_v<Result, ResultType>, "ResultType != Result");
52
        return accurate::greaterOp(a, b) ? static_cast<Result>(a) : static_cast<Result>(b);
53
    }
54

55
#if USE_EMBEDDED_COMPILER
56
    static constexpr bool compilable = false; /// ???
57
#endif
58
};
59

60
template <typename A, typename B>
61
using GreatestImpl = std::conditional_t<!NumberTraits::LeastGreatestSpecialCase<A, B>, GreatestBaseImpl<A, B>, GreatestSpecialImpl<A, B>>;
62

63
struct NameGreatest { static constexpr auto name = "greatest"; };
64
using FunctionGreatest = FunctionBinaryArithmetic<GreatestImpl, NameGreatest>;
65

66
REGISTER_FUNCTION(Greatest)
67
{
68
    factory.registerFunction<LeastGreatestOverloadResolver<LeastGreatest::Greatest, FunctionGreatest>>({}, FunctionFactory::CaseInsensitive);
69
}
70

71
}
72

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.