Skip to content

fix: distinguish empty sketch queries from invalid arguments - #295

Merged
tisonkun merged 6 commits into
apache:mainfrom
tisonkun:codex/kll-req-query-semantics
Oct 20, 2026
Merged

tisonkun merged 6 commits into
apache:mainfrom
tisonkun:codex/kll-req-query-semantics

Conversation

@tisonkun

@tisonkun tisonkun commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Why

KLL and REQ reported empty distributions as InvalidArgument, while T-Digest returned None but panicked on invalid query arguments. This change separates absent results from invalid requests, following the discussion in #266 and the implementation in #273.

Design

  • Validate query arguments first. Invalid arguments return InvalidArgument even when the sketch is empty.
  • Return a meaningful result when one exists; otherwise return None. Queries that also validate arguments return Result<Option<T>, Error>.

These principles are recorded briefly in CONTRIBUTING.md. Each method documents its own empty-result and error contract.

API changes

API Return type
KLL / REQ rank Option<f64>
KLL / REQ quantile, quantiles, cdf, pmf Result<Option<_>, Error>
TDigest / TDigestMut rank, quantile, quantiles, cdf, pmf Result<Option<_>, Error>

The changes include the queries exposed by KLL/REQ sorted views. Valid queries on empty data return None or Ok(None); callers of fallible queries can use ? before handling the optional result.

  • Ranks outside [0, 1], NaN arguments, and invalid split-point ordering produce errors where applicable.
  • On non-empty data, quantiles(&[]) returns Ok(Some(vec![])); cdf(&[]) and pmf(&[]) return Ok(Some(vec![1.0])). Empty data returns Ok(None).
  • The audit of the other sketch families found meaningful zero, false, or empty results, or existing Option contracts such as extrema and intersection initialization. Those APIs retain their behavior.
  • The changelog describes the REQ and T-Digest migrations. KLL is unreleased. Numeric algorithms and serialization formats are preserved.

Commits

  1. 5bda675 fix(kll): distinguish empty queries from invalid arguments
  2. fd871f9 fix(req): distinguish empty queries from invalid arguments
  3. 6947ee0 fix(tdigest): distinguish empty queries from invalid arguments
  4. d938a1b docs: keep query contracts on their methods
  5. 4e0ecc6 docs: show imports in doctest examples

The first three commits each cover one sketch family. Documentation cleanup and visible doctest imports are separate commits. Hidden trailing Ok(...) expressions remain available for examples using ?.

Validation

  • cargo x check
  • cargo x test, including 144 doctests and cross-language serialization tests
  • cargo x lint

Coverage includes empty, singleton, and compressed states; invalid arguments before emptiness; empty query slices; valid infinite T-Digest rank/split-point arguments; and existing endpoint and large-weight regressions.

Credit

Adapted from @Phoenix500526's work in #273. The three implementation commits retain:

Co-authored-by: Jiawei Zhao <Phoenix500526@163.com>

tisonkun and others added 3 commits October 20, 2026 15:55
Return None for valid queries on an empty sketch or sorted view, and
validate ranks and split points before checking emptiness. Document the
query contract and cover invalid inputs, empty arguments, and endpoints.

Co-authored-by: Jiawei Zhao <Phoenix500526@163.com>
Apply the query contract to REQ sketches and sorted views, preserving
exact extrema and validating ranks and split points before emptiness.
Document the migration and distinguish sample queries from error models.

Co-authored-by: Jiawei Zhao <Phoenix500526@163.com>
Return Result<Option<_>, Error> from rank, quantile, quantiles, CDF,
and PMF queries on both mutable and frozen digests. Validate arguments
before emptiness and compression, preserving None for empty data.

Migrate callers, test invalid arguments and valid boundary queries,
and document the empty-result inventory across all sketch families.

Co-authored-by: Jiawei Zhao <Phoenix500526@163.com>
@tisonkun
tisonkun force-pushed the codex/kll-req-query-semantics branch from e93ec9c to 6947ee0 Compare October 20, 2026 07:57
@tisonkun tisonkun changed the title fix: distinguish empty KLL and REQ queries from invalid arguments fix: distinguish empty sketch queries from invalid arguments Oct 20, 2026
Remove the crate-wide query inventory and repeated module summaries.
Keep empty-result and argument-error contracts on the query methods,
and record the shared design principles briefly in CONTRIBUTING.
Make all hidden doctest imports visible and record the example convention.
Keep trailing hidden Ok expressions that allow examples to use ?.
Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun enabled auto-merge (squash) October 20, 2026 13:03
@tisonkun
tisonkun merged commit cc614c2 into apache:main Oct 20, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant