ClickHouse/src/Functions/bitWrapperFunc.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.6 KiB
C++
Raw Normal View History

2019-08-01 19:18:36 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionUnaryArithmetic.h>
#include <DataTypes/NumberTraits.h>
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 wraps bool atomic functions
/// and transforms their boolean return value to the BoolMask ("can be false" and "can be true" bits).
template <typename A>
struct BitWrapperFuncImpl
{
using ResultType = UInt8;
2023-04-22 17:21:43 +00:00
static constexpr const bool allow_string_or_fixed_string = false;
2019-08-01 19:18:36 +00:00
2020-09-07 18:00:37 +00:00
static inline ResultType NO_SANITIZE_UNDEFINED apply(A a [[maybe_unused]])
2019-08-01 19:18:36 +00:00
{
2020-09-07 18:00:37 +00:00
// Should be a logical error, but this function is callable from SQL.
// Need to investigate this.
2021-09-10 11:49:22 +00:00
if constexpr (!is_integer<A>)
throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "It's a bug! Only integer types are supported by __bitWrapperFunc.");
2020-09-07 18:00:37 +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
2020-09-07 18:00:37 +00:00
static constexpr bool compilable = false;
2019-08-01 19:18:36 +00:00
#endif
2020-09-07 18:00:37 +00:00
};
2019-08-01 19:18:36 +00:00
2020-09-07 18:00:37 +00:00
struct NameBitWrapperFunc { static constexpr auto name = "__bitWrapperFunc"; };
using FunctionBitWrapperFunc = FunctionUnaryArithmetic<BitWrapperFuncImpl, NameBitWrapperFunc, true>;
2019-08-01 19:18:36 +00:00
2020-09-07 18:00:37 +00:00
}
template <> struct FunctionUnaryArithmeticMonotonicity<NameBitWrapperFunc>
{
static bool has() { return false; }
static IFunction::Monotonicity get(const Field &, const Field &)
2019-08-01 19:18:36 +00:00
{
2020-09-07 18:00:37 +00:00
return {};
2019-08-01 19:18:36 +00:00
}
2020-09-07 18:00:37 +00:00
};
2019-08-01 19:18:36 +00:00
REGISTER_FUNCTION(BitWrapperFunc)
2020-09-07 18:00:37 +00:00
{
factory.registerFunction<FunctionBitWrapperFunc>();
}
2019-08-01 19:18:36 +00:00
}