Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All significant changes to this project will be documented in this file.

### New features

* Add human-readable `Display` summaries for HLL and CPC sketches and unions.
* Add Relative Error Quantiles (REQ) sketches behind the `req` feature, including configurable high- or low-rank accuracy, rank, quantile, PMF, and CDF queries, merging and unions, and C++/Java-compatible serialization.

### Performance improvements
Expand Down
30 changes: 30 additions & 0 deletions datasketches/src/cpc/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::fmt;
use std::hash::Hash;

use crate::codec::SketchBytes;
Expand Down Expand Up @@ -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",
};
Comment on lines +477 to +483

Copy link
Copy Markdown
Member

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 Debug impl for flavor here or define a pub const fn as_str for Flavor`.


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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why NumStdDev::One? Could you point out the referenced implementation?

)?;
writeln!(f, " estimate : {}", self.estimate())?;
writeln!(
f,
" upper bound : {}",
self.upper_bound(NumStdDev::One)
Comment on lines +497 to +498

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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> {
Expand Down
16 changes: 16 additions & 0 deletions datasketches/src/cpc/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
//! which requires doing some extra work to figure out the values of num_coupons, offset,
//! first_interesting_column, and kxp.

use std::fmt;

use crate::cpc::CpcSketch;
use crate::cpc::DEFAULT_LG_K;
use crate::cpc::Flavor;
Expand Down Expand Up @@ -347,6 +349,20 @@ impl CpcUnion {
}
}

impl fmt::Display for CpcUnion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let state = match &self.state {
UnionState::Accumulator(_) => "Accumulator",
UnionState::BitMatrix(_) => "BitMatrix",
};

writeln!(f, "CPC Union Summary:")?;
writeln!(f, " lg k : {}", self.lg_k())?;
writeln!(f, " state : {state}")?;
writeln!(f, " num coupons : {}", self.num_coupons())
}
}

// testing methods
impl CpcUnion {
/// Returns the number of coupons in the union.
Expand Down
32 changes: 32 additions & 0 deletions datasketches/src/hll/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//! This module provides the main [`HllSketch`] struct, which is the primary interface
//! for creating and using HLL sketches for cardinality estimation.

use std::fmt;
use std::hash::Hash;

use crate::codec::SketchSlice;
Expand Down Expand Up @@ -458,6 +459,37 @@ impl HllSketch {
}
}

impl fmt::Display for HllSketch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let target_type = match self.target_type() {
HllType::Hll4 => "Hll4",
HllType::Hll6 => "Hll6",
HllType::Hll8 => "Hll8",
};
let current_mode = match &self.mode {
Mode::List { .. } => "List",
Mode::Set { .. } => "Set",
Mode::Array4(_) | Mode::Array6(_) | Mode::Array8(_) => "Hll",
};

writeln!(f, "HLL Sketch Summary:")?;
writeln!(f, " lg config k : {}", self.lg_config_k())?;
writeln!(f, " target type : {target_type}")?;
writeln!(f, " current mode : {current_mode}")?;
writeln!(
f,
" lower bound : {}",
self.lower_bound(NumStdDev::One)
)?;
writeln!(f, " estimate : {}", self.estimate())?;
writeln!(
f,
" upper bound : {}",
self.upper_bound(NumStdDev::One)
)
}
}

fn promote_container_to_set(container: &Container, hll_type: HllType) -> Mode {
let mut set = HashSet::default();
for coupon in container.iter() {
Expand Down
20 changes: 20 additions & 0 deletions datasketches/src/hll/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//! * Different modes (List, Set, Array4/6/8)
//! * Different target HLL types

use std::fmt;
use std::hash::Hash;

use crate::common::NumStdDev;
Expand Down Expand Up @@ -342,6 +343,25 @@ impl HllUnion {
}
}

impl fmt::Display for HllUnion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "HLL Union Summary:")?;
writeln!(f, " lg max k : {}", self.lg_max_k())?;
writeln!(f, " lg config k : {}", self.lg_config_k())?;
writeln!(
f,
" lower bound : {}",
self.lower_bound(NumStdDev::One)
)?;
writeln!(f, " estimate : {}", self.estimate())?;
writeln!(
f,
" upper bound : {}",
self.upper_bound(NumStdDev::One)
)
}
}

/// Convert a coupon mode (List or Set) to Hll8 target type
fn convert_coupon_mode_to_hll8(src_mode: &Mode, src_lg_k: u8) -> HllSketch {
match src_mode {
Expand Down
67 changes: 67 additions & 0 deletions tests-integration/tests/cpc_test/display.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You may use insta for snapshot testing.

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",
)
);
}
1 change: 1 addition & 0 deletions tests-integration/tests/cpc_test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

mod deserialize;
mod display;
mod union;
mod update;
mod wrapper;
63 changes: 63 additions & 0 deletions tests-integration/tests/hll_test/display.rs
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"));
}
1 change: 1 addition & 0 deletions tests-integration/tests/hll_test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// specific language governing permissions and limitations
// under the License.

mod display;
mod union;
mod update;