1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionUnaryArithmetic.h>
3#include <DataTypes/NumberTraits.h>
4
5namespace DB
6{
7 namespace ErrorCodes
8 {
9 extern const int BAD_CAST;
10 }
11
12 /// Working with UInt8: last bit = can be true, previous = can be false (Like dbms/src/Storages/MergeTree/BoolMask.h).
13 /// This function wraps bool atomic functions
14 /// and transforms their boolean return value to the BoolMask ("can be false" and "can be true" bits).
15 template <typename A>
16 struct BitWrapperFuncImpl
17 {
18 using ResultType = UInt8;
19
20 static inline ResultType NO_SANITIZE_UNDEFINED apply(A a)
21 {
22 if constexpr (!is_integral_v<A>)
23 throw DB::Exception("It's a bug! Only integer types are supported by __bitWrapperFunc.", ErrorCodes::BAD_CAST);
24 return a == 0 ? static_cast<ResultType>(0b10) : static_cast<ResultType >(0b1);
25 }
26
27#if USE_EMBEDDED_COMPILER
28 static constexpr bool compilable = false;
29
30#endif
31 };
32
33 struct NameBitWrapperFunc { static constexpr auto name = "__bitWrapperFunc"; };
34 using FunctionBitWrapperFunc = FunctionUnaryArithmetic<BitWrapperFuncImpl, NameBitWrapperFunc, true>;
35
36 template <> struct FunctionUnaryArithmeticMonotonicity<NameBitWrapperFunc>
37 {
38 static bool has() { return false; }
39 static IFunction::Monotonicity get(const Field &, const Field &)
40 {
41 return {};
42 }
43 };
44
45 void registerFunctionBitWrapperFunc(FunctionFactory & factory)
46 {
47 factory.registerFunction<FunctionBitWrapperFunc>();
48 }
49
50}
51