Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::HyperLogLog Class Reference

Algorithms from "New cardinality estimation algorithms for HyperLogLog sketches" Otmar Ertl, arXiv:1702.01284. More...

#include <duckdb.hpp>

Public Member Functions

void InsertElement (hash_t h)
 Algorithm 1.
void Update (const idx_t &i, const uint8_t &z)
uint8_t GetRegister (const idx_t &i) const
idx_t Count () const
void Merge (const HyperLogLog &other)
 Algorithm 2.
void Update (Vector &input, Vector &hashes, idx_t count)
 Add data to this HLL.
unique_ptr< HyperLogLog > Copy () const
 Get copy of the HLL.
void Serialize (Serializer &serializer) const
void ExtractCounts (uint32_t *c) const
 Algorithm 4.

Static Public Member Functions

static double GetErrorRate ()
static unique_ptr< HyperLogLog > Deserialize (Deserializer &deserializer)
static int64_t EstimateCardinality (uint32_t *c)
 Algorithm 6.

Static Public Attributes

static constexpr idx_t P = 6
static constexpr idx_t Q = 64 - P
static constexpr idx_t M = 1 << P
static constexpr double ALPHA = 0.721347520444481703680

Private Attributes

uint8_t k [M]

Detailed Description

Algorithms from "New cardinality estimation algorithms for HyperLogLog sketches" Otmar Ertl, arXiv:1702.01284.

Constructor & Destructor Documentation

◆ HyperLogLog()

duckdb::HyperLogLog::HyperLogLog ( )
inline
48243 {
48244 memset(k, 0, sizeof(k));
48245 }

Member Function Documentation

◆ GetErrorRate()

double duckdb::HyperLogLog::GetErrorRate ( )
inlinestatic
48238 {
48239 return std::sqrt(PI / 2.0) / sqrt(M);
48240 }
void sqrt(InputArray src, OutputArray dst)

◆ InsertElement()

void duckdb::HyperLogLog::InsertElement ( hash_t h)
inline

Algorithm 1.

48248 {
48249 const auto i = h & ((1 << P) - 1);
48250 h >>= P;
48251 h |= hash_t(1) << Q;
48252 const uint8_t z = UnsafeNumericCast<uint8_t>(CountZeros<hash_t>::Trailing(h) + 1);
48253 Update(i, z);
48254 }
z
::uint8_t uint8_t
uint64_t hash_t
The type used for hashes.
Definition duckdb.hpp:243
Here is the caller graph for this function:

◆ Update() [1/2]

void duckdb::HyperLogLog::Update ( const idx_t & i,
const uint8_t & z )
inline
48256 {
48257 k[i] = MaxValue<uint8_t>(k[i], z);
48258 }

◆ GetRegister()

uint8_t duckdb::HyperLogLog::GetRegister ( const idx_t & i) const
inline
48260 {
48261 return k[i];
48262 }

◆ Count()

idx_t duckdb::HyperLogLog::Count ( ) const
122843 {
122844 uint32_t c[Q + 2] = {0};
122845 ExtractCounts(c);
122846 return static_cast<idx_t>(EstimateCardinality(c));
122847}
static int64_t EstimateCardinality(uint32_t *c)
Algorithm 6.
Definition duckdb.cpp:122898
void ExtractCounts(uint32_t *c) const
Algorithm 4.
Definition duckdb.cpp:122857
::uint32_t uint32_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ Merge()

void duckdb::HyperLogLog::Merge ( const HyperLogLog & other)

Algorithm 2.

122850 {
122851 for (idx_t i = 0; i < M; ++i) {
122852 Update(i, other.k[i]);
122853 }
122854}

◆ Update() [2/2]

void duckdb::HyperLogLog::Update ( Vector & input,
Vector & hashes,
idx_t count )

Add data to this HLL.

122911 {
122912 UnifiedVectorFormat idata;
122913 input.ToUnifiedFormat(count, idata);
122914
122915 UnifiedVectorFormat hdata;
122916 hash_vec.ToUnifiedFormat(count, hdata);
122917 const auto hashes = UnifiedVectorFormat::GetData<hash_t>(hdata);
122918
122919 if (hash_vec.GetVectorType() == VectorType::CONSTANT_VECTOR) {
122920 if (idata.validity.RowIsValid(0)) {
122921 InsertElement(hashes[0]);
122922 }
122923 } else {
122924 D_ASSERT(hash_vec.GetVectorType() == VectorType::FLAT_VECTOR);
122925 if (idata.validity.AllValid()) {
122926 for (idx_t i = 0; i < count; ++i) {
122927 const auto hash = hashes[i];
122928 InsertElement(hash);
122929 }
122930 } else {
122931 for (idx_t i = 0; i < count; ++i) {
122932 if (idata.validity.RowIsValid(idata.sel->get_index(i))) {
122933 const auto hash = hashes[i];
122934 InsertElement(hash);
122935 }
122936 }
122937 }
122938 }
122939}
void InsertElement(hash_t h)
Algorithm 1.
Definition duckdb.hpp:48248
Here is the call graph for this function:

◆ Copy()

unique_ptr< HyperLogLog > duckdb::HyperLogLog::Copy ( ) const

Get copy of the HLL.

122941 {
122942 auto result = make_uniq<HyperLogLog>();
122943 memcpy(result->k, this->k, sizeof(k));
122944 D_ASSERT(result->Count() == Count());
122945 return result;
122946}

◆ Serialize()

void duckdb::HyperLogLog::Serialize ( Serializer & serializer) const
123064 {
123065 if (serializer.ShouldSerialize(3)) {
123066 serializer.WriteProperty(100, "type", HLLStorageType::HLL_V2);
123067 serializer.WriteProperty(101, "data", k, sizeof(k));
123068 } else {
123069 auto old = make_uniq<HLLV1>();
123070 old->FromNew(*this);
123071
123072 serializer.WriteProperty(100, "type", HLLStorageType::HLL_V1);
123073 serializer.WriteProperty(101, "data", old->GetPtr(), old->GetSize());
123074 }
123075}
@ HLL_V2
Redis HLL.
Definition duckdb.hpp:48222

◆ Deserialize()

unique_ptr< HyperLogLog > duckdb::HyperLogLog::Deserialize ( Deserializer & deserializer)
static
123077 {
123078 auto result = make_uniq<HyperLogLog>();
123079 auto storage_type = deserializer.ReadProperty<HLLStorageType>(100, "type");
123080 switch (storage_type) {
123081 case HLLStorageType::HLL_V1: {
123082 auto old = make_uniq<HLLV1>();
123083 deserializer.ReadProperty(101, "data", old->GetPtr(), old->GetSize());
123084 old->ToNew(*result);
123085 break;
123086 }
123088 deserializer.ReadProperty(101, "data", result->k, sizeof(k));
123089 break;
123090 default:
123091 throw SerializationException("Unknown HyperLogLog storage type!");
123092 }
123093 return result;
123094}
HLLStorageType
Definition duckdb.hpp:48220

◆ ExtractCounts()

void duckdb::HyperLogLog::ExtractCounts ( uint32_t * c) const

Algorithm 4.

122857 {
122858 for (idx_t i = 0; i < M; ++i) {
122859 c[k[i]]++;
122860 }
122861}

◆ EstimateCardinality()

int64_t duckdb::HyperLogLog::EstimateCardinality ( uint32_t * c)
static

Algorithm 6.

122898 {
122899 auto z = M * HLLTau((double(M) - c[Q]) / double(M));
122900
122901 for (idx_t k = Q; k >= 1; --k) {
122902 z += c[k];
122903 z *= 0.5;
122904 }
122905
122906 z += M * HLLSigma(c[0] / double(M));
122907
122908 return llroundl(ALPHA * M * M / z);
122909}
static double HLLTau(double x)
Taken from redis code.
Definition duckdb.cpp:122881
static double HLLSigma(double x)
Taken from redis code.
Definition duckdb.cpp:122864
Here is the call graph for this function:

The documentation for this class was generated from the following files: