2019-02-05 14:50:25 +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 LOGICAL_ERROR;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2019-08-04 13:03:38 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
/// Working with UInt8: last bit = can be true, previous = can be false (Like src/Storages/MergeTree/BoolMask.h).
|
|
|
|
/// This function provides "NOT" operation for BoolMasks by swapping last two bits ("can be true" <-> "can be false").
|
|
|
|
template <typename A>
|
|
|
|
struct BitSwapLastTwoImpl
|
|
|
|
{
|
|
|
|
using ResultType = UInt8;
|
2023-04-22 17:21:43 +00:00
|
|
|
static constexpr const bool allow_string_or_fixed_string = false;
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
static inline ResultType NO_SANITIZE_UNDEFINED apply([[maybe_unused]] A a)
|
|
|
|
{
|
|
|
|
if constexpr (!std::is_same_v<A, ResultType>)
|
|
|
|
// Should be a logical error, but this function is callable from SQL.
|
|
|
|
// Need to investigate this.
|
2023-01-23 21:13:58 +00:00
|
|
|
throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "It's a bug! Only UInt8 type is supported by __bitSwapLastTwo.");
|
2020-09-07 18:00:37 +00:00
|
|
|
|
|
|
|
auto little_bits = littleBits<A>(a);
|
|
|
|
return static_cast<ResultType>(((little_bits & 1) << 1) | ((little_bits >> 1) & 1));
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
2020-09-07 18:00:37 +00:00
|
|
|
static constexpr bool compilable = true;
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
static inline llvm::Value * compile(llvm::IRBuilder<> & b, llvm::Value * arg, bool)
|
|
|
|
{
|
|
|
|
if (!arg->getType()->isIntegerTy())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "__bitSwapLastTwo expected an integral type");
|
2020-09-07 18:00:37 +00:00
|
|
|
return b.CreateOr(
|
|
|
|
b.CreateShl(b.CreateAnd(arg, 1), 1),
|
|
|
|
b.CreateAnd(b.CreateLShr(arg, 1), 1)
|
|
|
|
);
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
#endif
|
2020-09-07 18:00:37 +00:00
|
|
|
};
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
struct NameBitSwapLastTwo { static constexpr auto name = "__bitSwapLastTwo"; };
|
|
|
|
using FunctionBitSwapLastTwo = FunctionUnaryArithmetic<BitSwapLastTwoImpl, NameBitSwapLastTwo, true>;
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameBitSwapLastTwo>
|
|
|
|
{
|
|
|
|
static bool has() { return false; }
|
|
|
|
static IFunction::Monotonicity get(const Field &, const Field &)
|
2019-08-04 13:03:38 +00:00
|
|
|
{
|
2020-09-07 18:00:37 +00:00
|
|
|
return {};
|
2019-02-05 14:50:25 +00:00
|
|
|
}
|
2020-09-07 18:00:37 +00:00
|
|
|
};
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(BitSwapLastTwo)
|
2020-09-07 18:00:37 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionBitSwapLastTwo>();
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
}
|