2022-01-19 06:02:24 +00:00
|
|
|
#include <Functions/FunctionBinaryArithmetic.h>
|
2019-11-06 10:35:23 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2022-07-31 14:34:05 +00:00
|
|
|
#include <bit>
|
2019-11-06 10:35:23 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct BitHammingDistanceImpl
|
|
|
|
{
|
|
|
|
using ResultType = UInt8;
|
2022-01-19 06:02:24 +00:00
|
|
|
static const constexpr bool allow_fixed_string = false;
|
|
|
|
static const constexpr bool allow_string_integer = false;
|
2019-11-06 10:35:23 +00:00
|
|
|
|
2022-01-19 06:02:24 +00:00
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline NO_SANITIZE_UNDEFINED Result apply(A a, B b)
|
2019-11-06 10:35:23 +00:00
|
|
|
{
|
2022-01-19 06:02:24 +00:00
|
|
|
UInt64 res = static_cast<UInt64>(a) ^ static_cast<UInt64>(b);
|
2022-07-31 14:34:05 +00:00
|
|
|
return std::popcount(res);
|
2019-11-06 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 06:02:24 +00:00
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static constexpr bool compilable = false; /// special type handling, some other time
|
|
|
|
#endif
|
|
|
|
};
|
2019-11-06 10:35:23 +00:00
|
|
|
|
2022-01-19 06:02:24 +00:00
|
|
|
struct NameBitHammingDistance
|
2019-11-06 10:35:23 +00:00
|
|
|
{
|
|
|
|
static constexpr auto name = "bitHammingDistance";
|
|
|
|
};
|
2022-01-19 06:02:24 +00:00
|
|
|
using FunctionBitHammingDistance = BinaryArithmeticOverloadResolver<BitHammingDistanceImpl, NameBitHammingDistance>;
|
2019-11-06 10:35:23 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(BitHammingDistance)
|
2019-11-06 10:35:23 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionBitHammingDistance>();
|
|
|
|
}
|
|
|
|
}
|