Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created generic for hashers #3

Closed
wants to merge 8 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions src/hyperloglog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,31 @@
//! 1. https://github.com/crepererum/pdatastructs.rs/blob/3997ed50f6b6871c9e53c4c5e0f48f431405fc63/src/hyperloglog.rs
//! 2. https://github.com/apache/arrow-datafusion/blob/f203d863f5c8bc9f133f6dd9b2e34e57ac3cdddc/datafusion/physical-expr/src/aggregate/hyperloglog.rs

use std::hash::Hash;

use ahash::RandomState;
use std::hash::{Hash, Hasher};
use ahash::AHasher;

/// By default, we use 2**14 registers like redis
const DEFAULT_P: usize = 14_usize;

/// Fixed seed
const SEED: RandomState = RandomState::with_seeds(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these will cause incompatiable changes.

We need to use fixed seed.

0x355e438b4b1478c7_u64,
0xd0e8453cd135b473_u64,
0xf7b252066a57836a_u64,
0xb8a829e3713c09bf_u64,
);

/// Note: We don't make HyperLogLog as static struct by keeping `PhantomData<T>`
/// Callers should take care of its hash function to be unchanged.
/// P is the bucket number, must be [4, 18]
/// Q = 64 - P
/// Register num is 1 << P
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
pub struct HyperLogLog<const P: usize = DEFAULT_P> {
pub struct HyperLogLog<H, const P: usize = DEFAULT_P> {
LucaCappelletti94 marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) registers: Vec<u8>,
_hasher: std::marker::PhantomData<H>,
}

impl<const P: usize> Default for HyperLogLog<P> {
impl<const P: usize> Default for HyperLogLog<AHasher, P> {
fn default() -> Self {
Self::new()
}
}

impl<const P: usize> HyperLogLog<P> {
impl<H: Default + Hasher, const P: usize> HyperLogLog<H, P> {
/// note that this method should not be invoked in untrusted environment
pub fn new() -> Self {
assert!(
Expand All @@ -48,13 +40,14 @@ impl<const P: usize> HyperLogLog<P> {

Self {
registers: vec![0; 1 << P],
_hasher: std::marker::PhantomData,
}
}

pub fn with_registers(registers: Vec<u8>) -> Self {
assert_eq!(registers.len(), Self::number_registers());

Self { registers }
Self { registers, _hasher: std::marker::PhantomData }
}

/// Adds an hash to the HyperLogLog.
Expand All @@ -69,7 +62,9 @@ impl<const P: usize> HyperLogLog<P> {
/// Adds an object to the HyperLogLog.
/// Though we could pass different types into this method, caller should notice that
pub fn add_object<T: Hash>(&mut self, obj: &T) {
let hash = SEED.hash_one(obj);
let mut hasher = H::default();
obj.hash(&mut hasher);
let hash = hasher.finish();
self.add_hash(hash);
}

Expand Down Expand Up @@ -191,7 +186,7 @@ fn hll_tau(x: f64) -> f64 {

#[cfg(test)]
mod tests {
use crate::HyperLogLog;
use super::*;

const P: usize = 14;
const NUM_REGISTERS: usize = 1 << P;
Expand All @@ -217,7 +212,7 @@ mod tests {

macro_rules! sized_number_test {
($SIZE: expr, $T: tt) => {{
let mut hll = HyperLogLog::<P>::new();
let mut hll = HyperLogLog::<AHasher, P>::new();
for i in 0..$SIZE {
hll.add_object(&(i as $T));
}
Expand Down Expand Up @@ -246,13 +241,13 @@ mod tests {

#[test]
fn test_empty() {
let hll = HyperLogLog::<P>::new();
let hll = HyperLogLog::<AHasher, P>::new();
assert_eq!(hll.count(), 0);
}

#[test]
fn test_one() {
let mut hll = HyperLogLog::<P>::new();
let mut hll = HyperLogLog::<AHasher, P>::new();
hll.add_hash(1);
assert_eq!(hll.count(), 1);
}
Expand Down Expand Up @@ -284,19 +279,19 @@ mod tests {

#[test]
fn test_empty_merge() {
let mut hll = HyperLogLog::<P>::new();
hll.merge(&HyperLogLog::<P>::new());
let mut hll = HyperLogLog::<AHasher, P>::new();
hll.merge(&HyperLogLog::<AHasher, P>::new());
assert_eq!(hll.count(), 0);
}

#[test]
fn test_merge_overlapped() {
let mut hll = HyperLogLog::<P>::new();
let mut hll = HyperLogLog::<AHasher, P>::new();
for i in 0..1000 {
hll.add_object(&i);
}

let other = HyperLogLog::<P>::new();
let other = HyperLogLog::<AHasher, P>::new();
for i in 0..1000 {
hll.add_object(&i);
}
Expand All @@ -307,7 +302,7 @@ mod tests {

#[test]
fn test_repetition() {
let mut hll = HyperLogLog::<P>::new();
let mut hll = HyperLogLog::<AHasher, P>::new();
for i in 0..1_000_000 {
hll.add_object(&(i % 1000));
}
Expand Down
Loading