ClickHouse/src/Functions/bitBoolMaskOr.cpp

56 lines
1.8 KiB
C++
Raw Normal View History

2019-08-02 19:21:55 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionBinaryArithmetic.h>
#include <DataTypes/NumberTraits.h>
2019-12-29 01:13:17 +00:00
2019-08-02 19:21:55 +00:00
namespace DB
{
2020-09-07 18:00:37 +00:00
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 "OR" operation for BoolMasks.
/// Returns: "can be true" = A."can be true" OR B."can be true"
/// "can be false" = A."can be false" AND B."can be false"
template <typename A, typename B>
struct BitBoolMaskOrImpl
{
using ResultType = UInt8;
static const constexpr bool allow_fixed_string = false;
static const constexpr bool allow_string_integer = false;
2019-08-02 19:21:55 +00:00
2020-09-07 18:00:37 +00:00
template <typename Result = ResultType>
static inline Result apply([[maybe_unused]] A left, [[maybe_unused]] B right)
2019-08-02 19:21:55 +00:00
{
2020-09-07 18:00:37 +00:00
if constexpr (!std::is_same_v<A, ResultType> || !std::is_same_v<B, ResultType>)
// Should be a logical error, but this function is callable from SQL.
// Need to investigate this.
throw DB::Exception("It's a bug! Only UInt8 type is supported by __bitBoolMaskOr.", ErrorCodes::BAD_ARGUMENTS);
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));
}
2019-08-02 19:21:55 +00:00
#if USE_EMBEDDED_COMPILER
2020-09-07 18:00:37 +00:00
static constexpr bool compilable = false;
2019-08-02 19:21:55 +00:00
#endif
2020-09-07 18:00:37 +00:00
};
2019-08-02 19:21:55 +00:00
2020-09-07 18:00:37 +00:00
struct NameBitBoolMaskOr { static constexpr auto name = "__bitBoolMaskOr"; };
2020-09-05 14:12:47 +00:00
using FunctionBitBoolMaskOr = BinaryArithmeticOverloadResolver<BitBoolMaskOrImpl, NameBitBoolMaskOr>;
2020-09-07 18:00:37 +00:00
}
void registerFunctionBitBoolMaskOr(FunctionFactory & factory)
{
factory.registerFunction<FunctionBitBoolMaskOr>();
}
2019-08-02 19:21:55 +00:00
}