Skip to main content

common_function/aggrs/approximate/
hll.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Two UDAFs are implemented for HyperLogLog:
16//!
17//! - `hll`: Accepts a string column and aggregates the values into a
18//!   HyperLogLog state.
19//! - `hll_merge`: Accepts a binary column of states generated by `hll`
20//!   and merges them into a single state.
21//!
22//! The states can be then used to estimate the cardinality of the
23//! values in the column by `hll_count` UDF.
24
25use std::sync::Arc;
26
27use common_query::prelude::*;
28use common_sketch::SketchState;
29use common_sketch::hll::{HllState as HllSketch, HllValue};
30use datafusion::arrow::array::ArrayRef;
31use datafusion::common::cast::{as_binary_array, as_string_array};
32use datafusion::common::not_impl_err;
33use datafusion::error::{DataFusionError, Result as DfResult};
34use datafusion::logical_expr::function::AccumulatorArgs;
35use datafusion::logical_expr::{Accumulator as DfAccumulator, AggregateUDF};
36use datafusion::prelude::create_udaf;
37use datafusion_expr::Volatility;
38use datatypes::arrow::datatypes::DataType;
39#[cfg(test)]
40use hyperloglogplus::{HyperLogLog, HyperLogLogPlus};
41
42#[cfg(test)]
43use crate::utils::FixedRandomState;
44
45pub const HLL_NAME: &str = "hll";
46pub const HLL_MERGE_NAME: &str = "hll_merge";
47
48const DEFAULT_PRECISION: u8 = 14;
49
50#[cfg(test)]
51pub(crate) type HllStateType = HyperLogLogPlus<String, FixedRandomState>;
52
53pub struct HllState {
54    hll: HllSketch,
55}
56
57impl std::fmt::Debug for HllState {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(f, "HllState<Opaque>")
60    }
61}
62
63impl Default for HllState {
64    fn default() -> Self {
65        Self::new()
66    }
67}
68
69impl HllState {
70    pub fn new() -> Self {
71        Self {
72            hll: HllSketch::new(DEFAULT_PRECISION).unwrap(),
73        }
74    }
75
76    /// Create a UDF for the `hll` function.
77    ///
78    /// `hll` accepts a string column and aggregates the
79    /// values into a HyperLogLog state.
80    pub fn state_udf_impl() -> AggregateUDF {
81        create_udaf(
82            HLL_NAME,
83            vec![DataType::Utf8],
84            Arc::new(DataType::Binary),
85            Volatility::Immutable,
86            Arc::new(Self::create_accumulator),
87            Arc::new(vec![DataType::Binary]),
88        )
89    }
90
91    /// Create a UDF for the `hll_merge` function.
92    ///
93    /// `hll_merge` accepts a binary column of states generated by `hll`
94    /// and merges them into a single state.
95    pub fn merge_udf_impl() -> AggregateUDF {
96        create_udaf(
97            HLL_MERGE_NAME,
98            vec![DataType::Binary],
99            Arc::new(DataType::Binary),
100            Volatility::Immutable,
101            Arc::new(Self::create_merge_accumulator),
102            Arc::new(vec![DataType::Binary]),
103        )
104    }
105
106    fn update(&mut self, value: &str) -> DfResult<()> {
107        self.hll
108            .update_repeated(HllValue::String(value), 1)
109            .map_err(|e| DataFusionError::Execution(e.to_string()))
110    }
111
112    fn merge(&mut self, raw: &[u8]) {
113        if let Ok(other) = HllSketch::decode(raw)
114            && self.hll.merge(&other).is_ok()
115        {
116            return;
117        }
118        common_telemetry::trace!("Failed to merge HLL state");
119    }
120
121    fn create_accumulator(acc_args: AccumulatorArgs) -> DfResult<Box<dyn DfAccumulator>> {
122        let data_type = acc_args.exprs[0].data_type(acc_args.schema)?;
123
124        match data_type {
125            DataType::Utf8 => Ok(Box::new(HllState::new())),
126            other => not_impl_err!("{HLL_NAME} does not support data type: {other}"),
127        }
128    }
129
130    fn create_merge_accumulator(acc_args: AccumulatorArgs) -> DfResult<Box<dyn DfAccumulator>> {
131        let data_type = acc_args.exprs[0].data_type(acc_args.schema)?;
132
133        match data_type {
134            DataType::Binary => Ok(Box::new(HllState::new())),
135            other => not_impl_err!("{HLL_MERGE_NAME} does not support data type: {other}"),
136        }
137    }
138}
139
140impl DfAccumulator for HllState {
141    fn update_batch(&mut self, values: &[ArrayRef]) -> DfResult<()> {
142        let array = &values[0];
143
144        match array.data_type() {
145            DataType::Utf8 => {
146                let string_array = as_string_array(array)?;
147                for value in string_array.iter().flatten() {
148                    self.update(value)?;
149                }
150            }
151            DataType::Binary => {
152                let binary_array = as_binary_array(array)?;
153                for v in binary_array.iter().flatten() {
154                    self.merge(v);
155                }
156            }
157            _ => {
158                return not_impl_err!(
159                    "HLL functions do not support data type: {}",
160                    array.data_type()
161                );
162            }
163        }
164
165        Ok(())
166    }
167
168    fn evaluate(&mut self) -> DfResult<ScalarValue> {
169        let bytes = self.hll.encode().map_err(|err| {
170            DataFusionError::Internal(format!("Failed to serialize HyperLogLog: {err}"))
171        })?;
172        Ok(ScalarValue::Binary(Some(bytes)))
173    }
174
175    fn size(&self) -> usize {
176        std::mem::size_of_val(&self.hll)
177    }
178
179    fn state(&mut self) -> DfResult<Vec<ScalarValue>> {
180        Ok(vec![self.evaluate()?])
181    }
182
183    fn merge_batch(&mut self, states: &[ArrayRef]) -> DfResult<()> {
184        let array = &states[0];
185        let binary_array = as_binary_array(array)?;
186        for v in binary_array.iter().flatten() {
187            self.merge(v);
188        }
189
190        Ok(())
191    }
192}
193
194#[cfg(test)]
195mod tests {
196    use datafusion::arrow::array::{BinaryArray, StringArray};
197
198    use super::*;
199
200    #[test]
201    fn test_hll_basic() {
202        let mut state = HllState::new();
203        state.update("1").unwrap();
204        state.update("2").unwrap();
205        state.update("3").unwrap();
206
207        let result = state.evaluate().unwrap();
208        if let ScalarValue::Binary(Some(bytes)) = result {
209            let mut hll: HllStateType = bincode::deserialize(&bytes).unwrap();
210            assert_eq!(hll.count().trunc() as u32, 3);
211        } else {
212            panic!("Expected binary scalar value");
213        }
214    }
215
216    #[test]
217    fn test_hll_roundtrip() {
218        let mut state = HllState::new();
219        state.update("1").unwrap();
220        state.update("2").unwrap();
221
222        // Serialize
223        let serialized = state.evaluate().unwrap();
224
225        // Create new state and merge the serialized data
226        let mut new_state = HllState::new();
227        if let ScalarValue::Binary(Some(bytes)) = &serialized {
228            new_state.merge(bytes);
229
230            // Verify the merged state matches original
231            let result = new_state.evaluate().unwrap();
232            if let ScalarValue::Binary(Some(new_bytes)) = result {
233                let mut original: HllStateType = bincode::deserialize(bytes).unwrap();
234                let mut merged: HllStateType = bincode::deserialize(&new_bytes).unwrap();
235                assert_eq!(original.count(), merged.count());
236            } else {
237                panic!("Expected binary scalar value");
238            }
239        } else {
240            panic!("Expected binary scalar value");
241        }
242    }
243
244    #[test]
245    fn test_hll_batch_update() {
246        let mut state = HllState::new();
247
248        // Test string values
249        let str_values = vec!["a", "b", "c", "d", "e", "f", "g", "h", "i"];
250        let str_array = Arc::new(StringArray::from(str_values)) as ArrayRef;
251        state.update_batch(&[str_array]).unwrap();
252
253        let result = state.evaluate().unwrap();
254        if let ScalarValue::Binary(Some(bytes)) = result {
255            let mut hll: HllStateType = bincode::deserialize(&bytes).unwrap();
256            assert_eq!(hll.count().trunc() as u32, 9);
257        } else {
258            panic!("Expected binary scalar value");
259        }
260    }
261
262    #[test]
263    fn test_hll_merge_batch() {
264        let mut state1 = HllState::new();
265        state1.update("1").unwrap();
266        let state1_binary = state1.evaluate().unwrap();
267
268        let mut state2 = HllState::new();
269        state2.update("2").unwrap();
270        let state2_binary = state2.evaluate().unwrap();
271
272        let mut merged_state = HllState::new();
273        if let (ScalarValue::Binary(Some(bytes1)), ScalarValue::Binary(Some(bytes2))) =
274            (&state1_binary, &state2_binary)
275        {
276            let binary_array = Arc::new(BinaryArray::from(vec![
277                bytes1.as_slice(),
278                bytes2.as_slice(),
279            ])) as ArrayRef;
280            merged_state.merge_batch(&[binary_array]).unwrap();
281
282            let result = merged_state.evaluate().unwrap();
283            if let ScalarValue::Binary(Some(bytes)) = result {
284                let mut hll: HllStateType = bincode::deserialize(&bytes).unwrap();
285                assert_eq!(hll.count().trunc() as u32, 2);
286            } else {
287                panic!("Expected binary scalar value");
288            }
289        } else {
290            panic!("Expected binary scalar values");
291        }
292    }
293
294    #[test]
295    fn test_hll_merge_function() {
296        // Create two HLL states with different values
297        let mut state1 = HllState::new();
298        state1.update("1").unwrap();
299        state1.update("2").unwrap();
300        let state1_binary = state1.evaluate().unwrap();
301
302        let mut state2 = HllState::new();
303        state2.update("2").unwrap();
304        state2.update("3").unwrap();
305        let state2_binary = state2.evaluate().unwrap();
306
307        // Create a merge state and merge both states
308        let mut merge_state = HllState::new();
309        if let (ScalarValue::Binary(Some(bytes1)), ScalarValue::Binary(Some(bytes2))) =
310            (&state1_binary, &state2_binary)
311        {
312            let binary_array = Arc::new(BinaryArray::from(vec![
313                bytes1.as_slice(),
314                bytes2.as_slice(),
315            ])) as ArrayRef;
316            merge_state.update_batch(&[binary_array]).unwrap();
317
318            let result = merge_state.evaluate().unwrap();
319            if let ScalarValue::Binary(Some(bytes)) = result {
320                let mut hll: HllStateType = bincode::deserialize(&bytes).unwrap();
321                // Should have 3 unique values: "1", "2", "3"
322                assert_eq!(hll.count().trunc() as u32, 3);
323            } else {
324                panic!("Expected binary scalar value");
325            }
326        } else {
327            panic!("Expected binary scalar values");
328        }
329    }
330}