ClickHouse
55 строк · 1.7 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionBinaryArithmetic.h>
3#include <DataTypes/NumberTraits.h>
4
5
6namespace DB
7{
8namespace ErrorCodes
9{
10extern const int BAD_ARGUMENTS;
11}
12
13namespace
14{
15
16/// Working with UInt8: last bit = can be true, previous = can be false (Like src/Storages/MergeTree/BoolMask.h).
17/// This function provides "AND" operation for BoolMasks.
18/// Returns: "can be true" = A."can be true" AND B."can be true"
19/// "can be false" = A."can be false" OR B."can be false"
20template <typename A, typename B>
21struct BitBoolMaskAndImpl
22{
23using ResultType = UInt8;
24static const constexpr bool allow_fixed_string = false;
25static const constexpr bool allow_string_integer = false;
26
27template <typename Result = ResultType>
28static inline Result apply([[maybe_unused]] A left, [[maybe_unused]] B right)
29{
30// Should be a logical error, but this function is callable from SQL.
31// Need to investigate this.
32if constexpr (!std::is_same_v<A, ResultType> || !std::is_same_v<B, ResultType>)
33throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "It's a bug! Only UInt8 type is supported by __bitBoolMaskAnd.");
34
35auto left_bits = littleBits<A>(left);
36auto right_bits = littleBits<B>(right);
37return static_cast<ResultType>((left_bits & right_bits & 1) | ((((left_bits >> 1) | (right_bits >> 1)) & 1) << 1));
38}
39
40#if USE_EMBEDDED_COMPILER
41static constexpr bool compilable = false;
42#endif
43};
44
45struct NameBitBoolMaskAnd { static constexpr auto name = "__bitBoolMaskAnd"; };
46using FunctionBitBoolMaskAnd = BinaryArithmeticOverloadResolver<BitBoolMaskAndImpl, NameBitBoolMaskAnd>;
47
48}
49
50REGISTER_FUNCTION(BitBoolMaskAnd)
51{
52factory.registerFunction<FunctionBitBoolMaskAnd>();
53}
54
55}
56