2019-08-01 19:18:36 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionUnaryArithmetic.h>
|
|
|
|
#include <DataTypes/NumberTraits.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-08-15 18:46:16 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-06-06 13:05:50 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2019-08-15 18:46:16 +00:00
|
|
|
}
|
2019-08-01 19:18:36 +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 wraps bool atomic functions
|
|
|
|
/// and transforms their boolean return value to the BoolMask ("can be false" and "can be true" bits).
|
2019-08-01 19:18:36 +00:00
|
|
|
template <typename A>
|
2019-08-02 19:21:55 +00:00
|
|
|
struct BitWrapperFuncImpl
|
2019-08-01 19:18:36 +00:00
|
|
|
{
|
|
|
|
using ResultType = UInt8;
|
2020-02-14 08:17:32 +00:00
|
|
|
static constexpr const bool allow_fixed_string = false;
|
2019-08-01 19:18:36 +00:00
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
static inline ResultType NO_SANITIZE_UNDEFINED apply(A a [[maybe_unused]])
|
2019-08-01 19:18:36 +00:00
|
|
|
{
|
2020-06-06 13:05:50 +00:00
|
|
|
// Should be a logical error, but this function is callable from SQL.
|
|
|
|
// Need to investigate this.
|
2020-08-19 11:52:17 +00:00
|
|
|
if constexpr (!is_integer_v<A>)
|
2020-06-06 13:05:50 +00:00
|
|
|
throw DB::Exception("It's a bug! Only integer types are supported by __bitWrapperFunc.", ErrorCodes::BAD_ARGUMENTS);
|
2019-08-15 18:46:16 +00:00
|
|
|
return a == 0 ? static_cast<ResultType>(0b10) : static_cast<ResultType >(0b1);
|
2019-08-01 19:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-08-02 19:21:55 +00:00
|
|
|
struct NameBitWrapperFunc { static constexpr auto name = "__bitWrapperFunc"; };
|
|
|
|
using FunctionBitWrapperFunc = FunctionUnaryArithmetic<BitWrapperFuncImpl, NameBitWrapperFunc, true>;
|
2019-08-01 19:18:36 +00:00
|
|
|
|
2019-08-02 19:21:55 +00:00
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameBitWrapperFunc>
|
2019-08-01 19:18:36 +00:00
|
|
|
{
|
|
|
|
static bool has() { return false; }
|
|
|
|
static IFunction::Monotonicity get(const Field &, const Field &)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-02 19:21:55 +00:00
|
|
|
void registerFunctionBitWrapperFunc(FunctionFactory & factory)
|
2019-08-01 19:18:36 +00:00
|
|
|
{
|
2019-08-02 19:21:55 +00:00
|
|
|
factory.registerFunction<FunctionBitWrapperFunc>();
|
2019-08-01 19:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|