Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
17e8b77
refactor(kll): clarify capacity module boundaries
tisonkun Sep 2, 2026
28a74d2
refactor(kll): minimize public configuration API
tisonkun Sep 2, 2026
f6868d2
refactor(kll): unify item ordering and serialization
tisonkun Sep 2, 2026
1610ba7
refactor(kll): align quantile query APIs
tisonkun Sep 2, 2026
c936114
fix(kll): reject incompatible sketch merges
tisonkun Sep 2, 2026
20e0707
perf(kll): optimize updates and repeated queries
tisonkun Sep 2, 2026
8b5eed1
bench(kll): cover update query merge and serde
tisonkun Sep 2, 2026
9402a41
test(kll): organize deterministic coverage
tisonkun Sep 2, 2026
a9d61f5
docs(kll): describe compatibility and query model
tisonkun Sep 2, 2026
226a538
docs(kll): polish public query types
tisonkun Sep 2, 2026
1c836ac
docs(kll): simplify changelog entries
tisonkun Sep 2, 2026
5b99707
refactor: expose search criteria from common only
tisonkun Sep 2, 2026
52949d6
refactor: require explicit search criteria
tisonkun Sep 2, 2026
101fa1b
docs: record search criteria migration
tisonkun Sep 2, 2026
83c1c7f
refactor(kll): tighten capacity helpers
tisonkun Sep 2, 2026
b043f02
docs(req): clarify section size comment
tisonkun Sep 2, 2026
75d7d6c
refactor(kll): improve invariant diagnostics
tisonkun Sep 2, 2026
a2a7647
refactor(kll): encode ordering in item types
tisonkun Sep 2, 2026
043333d
refactor(codec): unify truncated input diagnostics
tisonkun Sep 2, 2026
b21c4cb
refactor(codec): keep byte helpers internal
tisonkun Sep 2, 2026
b4b8302
refactor(codec): keep cursor abstraction minimal
tisonkun Sep 2, 2026
40e2fd0
Apply batched suggestions from code review
tisonkun Sep 2, 2026
3096c90
Merge branch 'main' into codex/kll-api-and-performance
tisonkun Sep 2, 2026
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
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ All significant changes to this project will be documented in this file.

## Unreleased

### Breaking changes

* Move `SearchCriteria` from `req` to `common` and remove its `Default` implementation. Import `datasketches::common::SearchCriteria` and explicitly choose `Inclusive` or `Exclusive` for each query.

### New features

* Add KLL sketches behind the `kll` feature, including rank, quantile, PMF, and CDF queries, custom item ordering, merging, and C++/Java-compatible serialization.
* Add KLL sketches behind the `kll` feature, with rank, quantile, PMF, and CDF queries, merging, totally ordered custom item types, a `KllFloat` adapter for non-NaN floating-point values, and serialization.

### Improvements

* Improve truncated-input diagnostics across sketch deserializers.

### Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ edition.workspace = true
rust-version.workspace = true

[dev-dependencies]
datasketches = { workspace = true, features = ["cpc", "req", "tdigest"] }
datasketches = { workspace = true, features = ["cpc", "kll", "req", "tdigest"] }
divan = { workspace = true }
rand = { workspace = true }

Expand Down
38 changes: 38 additions & 0 deletions benchmarks/kll/merge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 divan::Bencher;
use divan::black_box;
use divan::counter::ItemsCount;

use super::support::build_sketch;
use super::support::values;

#[divan::bench]
fn merge(bencher: Bencher) {
let values = values(200_000);
let left = build_sketch(&values[..100_000]);
let right = build_sketch(&values[100_000..]);

bencher
.counter(ItemsCount::new(values.len()))
.with_inputs(|| left.clone())
.bench_local_values(|mut left| {
left.merge(black_box(&right)).unwrap();
black_box(left)
});
}
22 changes: 22 additions & 0 deletions benchmarks/kll/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

mod merge;
mod query;
mod serde;
mod support;
mod update;
50 changes: 50 additions & 0 deletions benchmarks/kll/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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::common::SearchCriteria;
use datasketches::kll::KllFloat;
use divan::Bencher;
use divan::black_box;

use super::support::prepared_sketch;

#[divan::bench]
fn rank(bencher: Bencher) {
let sketch = prepared_sketch();
let item = KllFloat::<f64>::new(500_000.0).unwrap();
bencher.bench_local(|| black_box(&sketch).rank(black_box(&item), SearchCriteria::Inclusive));
}

#[divan::bench]
fn quantile(bencher: Bencher) {
let sketch = prepared_sketch();
bencher.bench_local(|| black_box(&sketch).quantile(black_box(0.5), SearchCriteria::Inclusive));
}

#[divan::bench]
fn sorted_view_quantile(bencher: Bencher) {
let view = prepared_sketch().sorted_view();
bencher.bench_local(|| black_box(&view).quantile(black_box(0.5), SearchCriteria::Inclusive));
}

#[divan::bench]
fn batch_quantiles(bencher: Bencher) {
let sketch = prepared_sketch();
let ranks = [0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99];
bencher
.bench_local(|| black_box(&sketch).quantiles(black_box(&ranks), SearchCriteria::Inclusive));
}
41 changes: 41 additions & 0 deletions benchmarks/kll/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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::kll::KllFloat;
use datasketches::kll::KllSketch;
use divan::Bencher;
use divan::black_box;
use divan::counter::BytesCount;

use super::support::prepared_sketch;

#[divan::bench]
fn serialize(bencher: Bencher) {
let sketch = prepared_sketch();
let bytes = sketch.serialize();
bencher
.counter(BytesCount::new(bytes.len()))
.bench_local(|| black_box(&sketch).serialize());
}

#[divan::bench]
fn deserialize(bencher: Bencher) {
let bytes = prepared_sketch().serialize();
bencher
.counter(BytesCount::new(bytes.len()))
.bench_local(|| KllSketch::<KllFloat<f64>>::deserialize(black_box(&bytes)).unwrap());
}
43 changes: 43 additions & 0 deletions benchmarks/kll/support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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::kll::KllFloat;
use datasketches::kll::KllSketch;
use rand::RngExt;
use rand::SeedableRng;
use rand::rngs::StdRng;

pub(super) const DEFAULT_K: u16 = 200;

pub(super) fn values(len: usize) -> Vec<f64> {
let mut rng = StdRng::seed_from_u64(42);
(0..len)
.map(|_| rng.random_range(0.0..1_000_000.0))
.collect()
}

pub(super) fn build_sketch(values: &[f64]) -> KllSketch<KllFloat<f64>> {
let mut sketch = KllSketch::new(DEFAULT_K).unwrap();
for &value in values {
sketch.update(KllFloat::<f64>::new(value).unwrap());
}
sketch
}

pub(super) fn prepared_sketch() -> KllSketch<KllFloat<f64>> {
build_sketch(&values(100_000))
}
31 changes: 31 additions & 0 deletions benchmarks/kll/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 divan::Bencher;
use divan::black_box;
use divan::counter::ItemsCount;

use super::support::build_sketch;
use super::support::values;

#[divan::bench(args = [1_000, 10_000, 100_000])]
fn update(bencher: Bencher, len: usize) {
let values = values(len);
bencher
.counter(ItemsCount::new(len))
.bench_local(|| build_sketch(black_box(&values)));
}
1 change: 1 addition & 0 deletions benchmarks/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use divan::AllocProfiler;
static ALLOC: AllocProfiler = AllocProfiler::system();

mod cpc;
mod kll;
mod req;
mod tdigest;

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/req/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// specific language governing permissions and limitations
// under the License.

use datasketches::common::SearchCriteria;
use datasketches::req::ReqFloat;
use datasketches::req::SearchCriteria;
use divan::Bencher;
use divan::black_box;

Expand Down
11 changes: 6 additions & 5 deletions datasketches/src/bloom/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,12 @@ impl BloomFilter {
.checked_add(1)
.and_then(|words| words.checked_mul(size_of::<u64>()))
.ok_or_else(|| Error::deserial("Bloom filter payload length overflows"))?;
if payload_bytes > cursor.remaining().len() {
return Err(Error::insufficient_data(format!(
"Bloom filter payload requires {payload_bytes} bytes, got {}",
cursor.remaining().len()
)));
let available_bytes = cursor.remaining().len();
if available_bytes < payload_bytes {
return Err(Error::insufficient_data_of(
"Bloom filter payload",
format_args!("expected {payload_bytes} bytes, got {available_bytes}"),
));
}
}
let mut bit_array = vec![0u64; num_words].into_boxed_slice();
Expand Down
2 changes: 1 addition & 1 deletion datasketches/src/codec/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::ops::RangeBounds;
use crate::error::Error;

pub fn insufficient_data(tag: &'static str) -> impl FnOnce(std::io::Error) -> Error {
move |_| Error::insufficient_data(tag)
move |error| Error::insufficient_data_of(tag, error)
}

pub fn ensure_serial_version_is(expected: u8, actual: u8) -> Result<(), Error> {
Expand Down
2 changes: 2 additions & 0 deletions datasketches/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

mod num_std_dev;
mod resize;
mod search_criteria;
pub use self::num_std_dev::NumStdDev;
pub use self::resize::ResizeFactor;
pub use self::search_criteria::SearchCriteria;

#[cfg(any(feature = "cpc", feature = "hll"))]
pub(crate) mod inv_pow2;
25 changes: 25 additions & 0 deletions datasketches/src/common/search_criteria.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

/// Selects the rank definition used by rank, quantile, PMF, and CDF queries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchCriteria {
/// Define rank as the fraction of values less than or equal to the boundary.
Inclusive,
/// Define rank as the fraction of values strictly less than the boundary.
Exclusive,
}
11 changes: 6 additions & 5 deletions datasketches/src/countmin/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,12 @@ impl<T: CountMinValue> CountMinSketch<T> {
let payload_bytes = payload_values
.checked_mul(LONG_SIZE_BYTES)
.ok_or_else(|| Error::deserial("CountMin payload size overflows"))?;
if payload_bytes > cursor.remaining().len() {
return Err(Error::insufficient_data(format!(
"CountMin payload requires {payload_bytes} bytes, got {}",
cursor.remaining().len()
)));
let available_bytes = cursor.remaining().len();
if available_bytes < payload_bytes {
return Err(Error::insufficient_data_of(
"CountMin payload",
format_args!("expected {payload_bytes} bytes, got {available_bytes}"),
));
}
}

Expand Down
12 changes: 8 additions & 4 deletions datasketches/src/cpc/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,14 @@ impl CpcSketch {
let payload_bytes = window_data_bytes
.checked_add(table_data_bytes)
.ok_or_else(|| Error::deserial("CPC payload length overflows"))?;
let payload = cursor
.remaining()
.get(..payload_bytes)
.ok_or_else(|| Error::deserial("insufficient data for CPC compressed payload"))?;
let available_bytes = cursor.remaining().len();
if available_bytes < payload_bytes {
return Err(Error::insufficient_data_of(
"CPC compressed payload",
format_args!("expected {payload_bytes} bytes, got {available_bytes}"),
));
}
let payload = &cursor.remaining()[..payload_bytes];
let (window_data, table_data) = payload.split_at(window_data_bytes);
let (table, window) = match flavor {
Flavor::Empty => (PairTable::new(2, lg_k + 6), vec![]),
Expand Down
Loading