mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Added function bitCount #8702
This commit is contained in:
parent
e2d8360a76
commit
da1b51a496
42
dbms/src/Functions/bitCount.cpp
Normal file
42
dbms/src/Functions/bitCount.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include <ext/bit_cast.h>
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <Functions/FunctionUnaryArithmetic.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
template <typename A>
|
||||
struct BitCountImpl
|
||||
{
|
||||
using ResultType = UInt8;
|
||||
|
||||
static inline ResultType apply(A a)
|
||||
{
|
||||
return __builtin_popcountll(ext::bit_cast<unsigned long long>(a));
|
||||
}
|
||||
|
||||
#if USE_EMBEDDED_COMPILER
|
||||
static constexpr bool compilable = false;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct NameBitCount { static constexpr auto name = "bitCount"; };
|
||||
using FunctionBitCount = FunctionUnaryArithmetic<BitCountImpl, NameBitCount, true>;
|
||||
|
||||
/// The function has no ranges of monotonicity.
|
||||
template <> struct FunctionUnaryArithmeticMonotonicity<NameBitCount>
|
||||
{
|
||||
static bool has() { return false; }
|
||||
static IFunction::Monotonicity get(const Field &, const Field &)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
void registerFunctionBitCount(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionBitCount>();
|
||||
}
|
||||
|
||||
}
|
@ -20,6 +20,7 @@ void registerFunctionBitShiftLeft(FunctionFactory & factory);
|
||||
void registerFunctionBitShiftRight(FunctionFactory & factory);
|
||||
void registerFunctionBitRotateLeft(FunctionFactory & factory);
|
||||
void registerFunctionBitRotateRight(FunctionFactory & factory);
|
||||
void registerFunctionBitCount(FunctionFactory & factory);
|
||||
void registerFunctionLeast(FunctionFactory & factory);
|
||||
void registerFunctionGreatest(FunctionFactory & factory);
|
||||
void registerFunctionBitTest(FunctionFactory & factory);
|
||||
@ -58,6 +59,7 @@ void registerFunctionsArithmetic(FunctionFactory & factory)
|
||||
registerFunctionBitShiftRight(factory);
|
||||
registerFunctionBitRotateLeft(factory);
|
||||
registerFunctionBitRotateRight(factory);
|
||||
registerFunctionBitCount(factory);
|
||||
registerFunctionLeast(factory);
|
||||
registerFunctionGreatest(factory);
|
||||
registerFunctionBitTest(factory);
|
||||
|
21
dbms/tests/queries/0_stateless/01066_bit_count.reference
Normal file
21
dbms/tests/queries/0_stateless/01066_bit_count.reference
Normal file
@ -0,0 +1,21 @@
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
1
|
||||
2
|
||||
2
|
||||
3
|
||||
1
|
||||
2
|
||||
4
|
||||
0
|
||||
1
|
||||
8
|
||||
64
|
||||
32
|
||||
16
|
||||
8
|
||||
1 10 000000000000F03F
|
||||
-1 11 000000000000F0BF
|
||||
inf 11 000000000000F07F
|
13
dbms/tests/queries/0_stateless/01066_bit_count.sql
Normal file
13
dbms/tests/queries/0_stateless/01066_bit_count.sql
Normal file
@ -0,0 +1,13 @@
|
||||
SELECT bitCount(number) FROM numbers(10);
|
||||
SELECT avg(bitCount(number)) FROM numbers(256);
|
||||
|
||||
SELECT bitCount(0);
|
||||
SELECT bitCount(1);
|
||||
SELECT bitCount(-1);
|
||||
|
||||
SELECT bitCount(toInt64(-1));
|
||||
SELECT bitCount(toInt32(-1));
|
||||
SELECT bitCount(toInt16(-1));
|
||||
SELECT bitCount(toInt8(-1));
|
||||
|
||||
SELECT x, bitCount(x), hex(reinterpretAsString(x)) FROM VALUES ('x Float64', (1), (-1), (inf));
|
Loading…
Reference in New Issue
Block a user