Dandandan commented on a change in pull request #1095: URL: https://github.com/apache/arrow-datafusion/pull/1095#discussion_r725513283
########## File path: datafusion/src/physical_plan/hyperloglog/mod.rs ########## @@ -0,0 +1,145 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! This module contains a modified version specifically for the +//! implementation of `approx_distinct` function. +//! +//! https://github.com/crepererum/pdatastructs.rs/blob/3997ed50f6b6871c9e53c4c5e0f48f431405fc63/src/hyperloglog.rs +//! https://github.com/redis/redis/blob/b874c6f1fcf844d28f84fc2a2ab6b8f6c2458462/src/hyperloglog.c + +use ahash::AHasher; +use std::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher}; +use std::marker::PhantomData; + +const PRECISION: usize = 14_usize; +const HLL_Q: usize = 64_usize - PRECISION; +const NUM_REGISTERS: usize = 1_usize << PRECISION; + +#[derive(Clone)] +pub(crate) struct HyperLogLog<T, B = BuildHasherDefault<AHasher>> +where + T: Hash + ?Sized, + B: BuildHasher + Clone + Eq, +{ + registers: Vec<u8>, + buildhasher: B, + phantom: PhantomData<T>, +} + +impl<T> HyperLogLog<T> +where + T: Hash + ?Sized, +{ + /// Creates a new, empty HyperLogLog. + pub fn new() -> Self { + let bh = BuildHasherDefault::<AHasher>::default(); + Self::with_hash(bh) + } +} + +impl<T, B> HyperLogLog<T, B> +where + T: Hash + ?Sized, + B: BuildHasher + Clone + Eq, +{ + /// Same as `new` but with a specific `BuildHasher`. + pub fn with_hash(buildhasher: B) -> Self { + let registers = vec![0; NUM_REGISTERS]; + Self { + registers, + buildhasher, + phantom: PhantomData, + } + } + + /// Adds an element to the HyperLogLog. + pub fn add(&mut self, obj: &T) { + let mut hasher = self.buildhasher.build_hasher(); Review comment: As we're using only aHash in this usage here, we could use the aHash methods and avoid the generic hash builder api. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
