mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-23 16:12:01 +00:00
Merge pull request #49867 from ClickHouse/bitcount-big-int
Add support for big integers in function `bitCount`
This commit is contained in:
commit
8b630f90c4
@ -9,7 +9,7 @@ namespace DB
|
||||
template <typename A>
|
||||
struct BitCountImpl
|
||||
{
|
||||
using ResultType = UInt8;
|
||||
using ResultType = std::conditional_t<(sizeof(A) * 8 >= 256), UInt16, UInt8>;
|
||||
static constexpr bool allow_string_or_fixed_string = true;
|
||||
|
||||
static inline ResultType apply(A a)
|
||||
@ -17,6 +17,13 @@ struct BitCountImpl
|
||||
/// We count bits in the value representation in memory. For example, we support floats.
|
||||
/// We need to avoid sign-extension when converting signed numbers to larger type. So, uint8_t(-1) has 8 bits.
|
||||
|
||||
if constexpr (is_big_int_v<A>)
|
||||
{
|
||||
ResultType res = 0;
|
||||
for (auto item : a.items)
|
||||
res += __builtin_popcountll(item);
|
||||
return res;
|
||||
}
|
||||
if constexpr (std::is_same_v<A, UInt64> || std::is_same_v<A, Int64>)
|
||||
return __builtin_popcountll(a);
|
||||
if constexpr (std::is_same_v<A, UInt32> || std::is_same_v<A, Int32> || std::is_unsigned_v<A>)
|
||||
|
13
tests/queries/0_stateless/02736_bit_count_big_int.reference
Normal file
13
tests/queries/0_stateless/02736_bit_count_big_int.reference
Normal file
@ -0,0 +1,13 @@
|
||||
128
|
||||
256
|
||||
128
|
||||
256
|
||||
127
|
||||
255
|
||||
126
|
||||
255
|
||||
64
|
||||
UInt8
|
||||
UInt16
|
||||
UInt8
|
||||
UInt16
|
19
tests/queries/0_stateless/02736_bit_count_big_int.sql
Normal file
19
tests/queries/0_stateless/02736_bit_count_big_int.sql
Normal file
@ -0,0 +1,19 @@
|
||||
SELECT bitCount(CAST(-1 AS UInt128));
|
||||
SELECT bitCount(CAST(-1 AS UInt256));
|
||||
|
||||
SELECT bitCount(CAST(-1 AS Int128));
|
||||
SELECT bitCount(CAST(-1 AS Int256));
|
||||
|
||||
SELECT bitCount(CAST(-1 AS UInt128) - 1);
|
||||
SELECT bitCount(CAST(-1 AS UInt256) - 2);
|
||||
|
||||
SELECT bitCount(CAST(-1 AS Int128) - 3);
|
||||
SELECT bitCount(CAST(-1 AS Int256) - 4);
|
||||
|
||||
SELECT bitCount(CAST(0xFFFFFFFFFFFFFFFF AS Int256));
|
||||
|
||||
SELECT toTypeName(bitCount(1::UInt128));
|
||||
SELECT toTypeName(bitCount(1::UInt256));
|
||||
|
||||
SELECT toTypeName(bitCount(1::Int128));
|
||||
SELECT toTypeName(bitCount(1::Int256));
|
Loading…
Reference in New Issue
Block a user