Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cubestore): HyperLogLog++ support for BigQuery (#1872)
* chore(cubestore): port ZetaSketch to Rust This is a direct port of the [ZetaSketch](https://github.com/google/zetasketch) library. See README.md for details. * feat(cubestore): Support HyperLogLog++ sketches This allows to consume HyperLogLog sketches produced by BigQuery.
- Loading branch information
1 parent
74ffacd
commit 357ecef
Showing
27 changed files
with
2,636 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
members = [ | ||
"cubestore", | ||
"cubehll", | ||
"cubezetasketch", | ||
] | ||
|
||
[patch.crates-io] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::CubeError; | ||
use cubehll::HllSketch; | ||
use cubezetasketch::HyperLogLogPlusPlus; | ||
|
||
#[derive(Debug)] | ||
pub enum Hll { | ||
Airlift(HllSketch), // Compatible with Athena, Presto, etc. | ||
ZetaSketch(HyperLogLogPlusPlus), // Compatible with BigQuery. | ||
} | ||
|
||
impl Hll { | ||
pub fn read(data: &[u8]) -> Result<Hll, CubeError> { | ||
if data.is_empty() { | ||
return Err(CubeError::internal( | ||
"invalid serialized HLL (empty data)".to_string(), | ||
)); | ||
} | ||
// The first byte: | ||
// - must larger than 3 due to how protos are encoded in ZetaSketch. | ||
// - represents the data format version and is <= 3 in AirLift. | ||
if data[0] <= 3 { | ||
return Ok(Hll::Airlift(HllSketch::read(data)?)); | ||
} else { | ||
return Ok(Hll::ZetaSketch(HyperLogLogPlusPlus::read(data)?)); | ||
} | ||
} | ||
|
||
pub fn write(&self) -> Vec<u8> { | ||
match self { | ||
Hll::Airlift(h) => h.write(), | ||
Hll::ZetaSketch(h) => h.write(), | ||
} | ||
} | ||
|
||
pub fn is_compatible(&self, other: &Hll) -> bool { | ||
match (self, other) { | ||
(Hll::Airlift(l), Hll::Airlift(r)) => l.index_bit_len() == r.index_bit_len(), | ||
(Hll::ZetaSketch(l), Hll::ZetaSketch(r)) => l.is_compatible(r), | ||
_ => return false, | ||
} | ||
} | ||
|
||
pub fn cardinality(&self) -> u64 { | ||
match self { | ||
Hll::Airlift(h) => h.cardinality(), | ||
Hll::ZetaSketch(h) => h.cardinality(), | ||
} | ||
} | ||
|
||
/// Clients are responsible for calling `is_compatible` before running this function. | ||
/// On error, `self` may end up in inconsistent state and must be discarded. | ||
pub fn merge_with(&mut self, other: &Hll) -> Result<(), CubeError> { | ||
debug_assert!(self.is_compatible(other)); | ||
match (self, other) { | ||
(Hll::Airlift(l), Hll::Airlift(r)) => l.merge_with(r), | ||
(Hll::ZetaSketch(l), Hll::ZetaSketch(r)) => l.merge_with(r)?, | ||
_ => panic!("incompatible HLL types"), | ||
} | ||
return Ok(()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod hll; | ||
pub mod query_executor; | ||
pub mod serialized_plan; | ||
pub mod udfs; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "cubezetasketch" | ||
version = "0.1.0" | ||
authors = ["Cube Dev, Inc."] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
description = "HyperLogLog++ implementation ported from ZetaSketch" | ||
|
||
[dependencies] | ||
itertools = "0.10.0" | ||
protobuf = "2.20.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Overview | ||
|
||
Rust implementation of HyperLogLog++ directly ported from the Java code in [ZetaSketch](https://github.com/google/zetasketch). | ||
Based on commit `a2f2692fae8cf61103330f9f70e696c4ba8b94b0`. | ||
|
||
This library allows to directly interoperate with sketches produced by `ZetaSketch`. | ||
Only portion of the code is ported. In particular, we currently support: | ||
- reading and writing sketches in the binary proto format, | ||
- computing set cardinality estimates, | ||
- merging sketches. | ||
|
||
The major unsupported bits are: | ||
- adding values to the sketches, | ||
- mixing sketches of different precisions. |
Oops, something went wrong.