-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add Display summaries for HLL and CPC #232
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::fmt; | ||
| use std::hash::Hash; | ||
|
|
||
| use crate::codec::SketchBytes; | ||
|
|
@@ -471,6 +472,35 @@ impl CpcSketch { | |
| } | ||
| } | ||
|
|
||
| impl fmt::Display for CpcSketch { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let flavor = match self.flavor() { | ||
| Flavor::Empty => "Empty", | ||
| Flavor::Sparse => "Sparse", | ||
| Flavor::Hybrid => "Hybrid", | ||
| Flavor::Pinned => "Pinned", | ||
| Flavor::Sliding => "Sliding", | ||
| }; | ||
|
|
||
| writeln!(f, "CPC Sketch Summary:")?; | ||
| writeln!(f, " flavor : {flavor}")?; | ||
| writeln!(f, " lg k : {}", self.lg_k())?; | ||
| writeln!(f, " merged : {}", self.merge_flag)?; | ||
| writeln!( | ||
| f, | ||
| " lower bound : {}", | ||
| self.lower_bound(NumStdDev::One) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
| )?; | ||
| writeln!(f, " estimate : {}", self.estimate())?; | ||
| writeln!( | ||
| f, | ||
| " upper bound : {}", | ||
| self.upper_bound(NumStdDev::One) | ||
|
Comment on lines
+497
to
+498
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
| )?; | ||
| writeln!(f, " num coupons : {}", self.num_coupons) | ||
| } | ||
| } | ||
|
|
||
| impl CpcSketch { | ||
| /// Serializes this `CpcSketch` to bytes. | ||
| pub fn serialize(&self) -> Vec<u8> { | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may use However, I have more to consider here now. Let me comment on the issue. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // 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. | ||
|
|
||
| use datasketches::cpc::CpcSketch; | ||
| use datasketches::cpc::CpcUnion; | ||
|
|
||
| #[test] | ||
| fn display_empty_sketch() { | ||
| let sketch = CpcSketch::new(11); | ||
|
|
||
| assert_eq!( | ||
| sketch.to_string(), | ||
| concat!( | ||
| "CPC Sketch Summary:\n", | ||
| " flavor : Empty\n", | ||
| " lg k : 11\n", | ||
| " merged : false\n", | ||
| " lower bound : 0\n", | ||
| " estimate : 0\n", | ||
| " upper bound : 0\n", | ||
| " num coupons : 0\n", | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_populated_sketch() { | ||
| let mut sketch = CpcSketch::new(11); | ||
| sketch.update("apple"); | ||
|
|
||
| let summary = sketch.to_string(); | ||
| assert!(summary.contains("flavor : Sparse\n")); | ||
| assert!(summary.contains("num coupons : 1\n")); | ||
| assert!(!summary.contains("estimate : 0\n")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_union() { | ||
| let mut sketch = CpcSketch::new(11); | ||
| sketch.update("apple"); | ||
| let mut union = CpcUnion::new(11); | ||
| union.update(&sketch); | ||
|
|
||
| assert_eq!( | ||
| union.to_string(), | ||
| concat!( | ||
| "CPC Union Summary:\n", | ||
| " lg k : 11\n", | ||
| " state : Accumulator\n", | ||
| " num coupons : 1\n", | ||
| ) | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| // under the License. | ||
|
|
||
| mod deserialize; | ||
| mod display; | ||
| mod union; | ||
| mod update; | ||
| mod wrapper; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // 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. | ||
|
|
||
| use datasketches::hll::HllSketch; | ||
| use datasketches::hll::HllType; | ||
| use datasketches::hll::HllUnion; | ||
|
|
||
| #[test] | ||
| fn display_empty_sketch() { | ||
| let sketch = HllSketch::new(12, HllType::Hll8); | ||
|
|
||
| assert_eq!( | ||
| sketch.to_string(), | ||
| concat!( | ||
| "HLL Sketch Summary:\n", | ||
| " lg config k : 12\n", | ||
| " target type : Hll8\n", | ||
| " current mode : List\n", | ||
| " lower bound : 0\n", | ||
| " estimate : 0\n", | ||
| " upper bound : 0\n", | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_populated_sketch() { | ||
| let mut sketch = HllSketch::new(10, HllType::Hll4); | ||
| for value in 0..1_000 { | ||
| sketch.update(value); | ||
| } | ||
|
|
||
| let summary = sketch.to_string(); | ||
| assert!(summary.contains("target type : Hll4\n")); | ||
| assert!(summary.contains("current mode : Hll\n")); | ||
| assert!(!summary.contains("estimate : 0\n")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_union() { | ||
| let mut union = HllUnion::new(12); | ||
| union.update_value("apple"); | ||
|
|
||
| let summary = union.to_string(); | ||
| assert!(summary.starts_with("HLL Union Summary:\n")); | ||
| assert!(summary.contains("lg max k : 12\n")); | ||
| assert!(summary.contains("lg config k : 12\n")); | ||
| assert!(!summary.contains("estimate : 0\n")); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can either just use the
Debugimpl for flavor here or define apub const fn as_strfor Flavor`.