aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/bitBoolMaskAnd.cpp
blob: 11c0c1d1b7d6be48cb70d76eeb0d063141be1379 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionBinaryArithmetic.h>
#include <DataTypes/NumberTraits.h>


namespace DB
{
namespace ErrorCodes
{
    extern const int BAD_ARGUMENTS;
}

namespace
{

/// Working with UInt8: last bit = can be true, previous = can be false (Like src/Storages/MergeTree/BoolMask.h).
/// This function provides "AND" operation for BoolMasks.
/// Returns: "can be true" = A."can be true" AND B."can be true"
///          "can be false" = A."can be false" OR B."can be false"
template <typename A, typename B>
struct BitBoolMaskAndImpl
{
    using ResultType = UInt8;
    static const constexpr bool allow_fixed_string = false;
    static const constexpr bool allow_string_integer = false;

    template <typename Result = ResultType>
    static inline Result apply([[maybe_unused]] A left, [[maybe_unused]] B right)
    {
        // Should be a logical error, but this function is callable from SQL.
        // Need to investigate this.
        if constexpr (!std::is_same_v<A, ResultType> || !std::is_same_v<B, ResultType>)
            throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "It's a bug! Only UInt8 type is supported by __bitBoolMaskAnd.");

        auto left_bits = littleBits<A>(left);
        auto right_bits = littleBits<B>(right);
        return static_cast<ResultType>((left_bits & right_bits & 1) | ((((left_bits >> 1) | (right_bits >> 1)) & 1) << 1));
    }

#if USE_EMBEDDED_COMPILER
    static constexpr bool compilable = false;
#endif
};

struct NameBitBoolMaskAnd { static constexpr auto name = "__bitBoolMaskAnd"; };
using FunctionBitBoolMaskAnd = BinaryArithmeticOverloadResolver<BitBoolMaskAndImpl, NameBitBoolMaskAnd>;

}

REGISTER_FUNCTION(BitBoolMaskAnd)
{
    factory.registerFunction<FunctionBitBoolMaskAnd>();
}

}