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
|
|
|
|
{
|
2019-08-15 18:46:16 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-06-03 12:59:11 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2019-08-15 18:46:16 +00:00
|
|
|
}
|
2019-08-02 19:21:55 +00:00
|
|
|
|
2020-04-03 15:14:31 +00:00
|
|
|
/// Working with UInt8: last bit = can be true, previous = can be false (Like src/Storages/MergeTree/BoolMask.h).
|
2019-08-04 13:03:38 +00:00
|
|
|
/// 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"
|
2019-08-02 19:21:55 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
struct BitBoolMaskAndImpl
|
|
|
|
{
|
|
|
|
using ResultType = UInt8;
|
2020-02-14 07:11:37 +00:00
|
|
|
static const constexpr bool allow_fixed_string = false;
|
2019-08-02 19:21:55 +00:00
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A left, B right)
|
|
|
|
{
|
2019-08-15 18:46:16 +00:00
|
|
|
if constexpr (!std::is_same_v<A, ResultType> || !std::is_same_v<B, ResultType>)
|
2020-06-03 12:59:11 +00:00
|
|
|
throw DB::Exception("It's a bug! Only UInt8 type is supported by __bitBoolMaskAnd.", ErrorCodes::LOGICAL_ERROR);
|
2019-08-02 19:21:55 +00:00
|
|
|
return static_cast<ResultType>(
|
|
|
|
((static_cast<ResultType>(left) & static_cast<ResultType>(right)) & 1)
|
|
|
|
| ((((static_cast<ResultType>(left) >> 1) | (static_cast<ResultType>(right) >> 1)) & 1) << 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameBitBoolMaskAnd { static constexpr auto name = "__bitBoolMaskAnd"; };
|
|
|
|
using FunctionBitBoolMaskAnd = FunctionBinaryArithmetic<BitBoolMaskAndImpl, NameBitBoolMaskAnd>;
|
|
|
|
|
|
|
|
void registerFunctionBitBoolMaskAnd(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionBitBoolMaskAnd>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|