Skip to content

Fix TDIGEST returning nan for a sketch with a single observation - #1035

Merged
gabsow merged 2 commits into
RedisBloom:masterfrom
PerumalsamyR:fix-tdigest-single-observation-nan
Aug 2, 2026
Merged

gabsow merged 2 commits into
RedisBloom:masterfrom
PerumalsamyR:fix-tdigest-single-observation-nan

Conversation

@PerumalsamyR

@PerumalsamyR PerumalsamyR commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #870

Problem

A t-digest sketch holding exactly one weight-1 observation replies nan on every query command:

> TDIGEST.CREATE foo
OK
> TDIGEST.ADD foo 123
OK
> TDIGEST.QUANTILE foo 0.9
1) "nan"

TDIGEST.CDF and TDIGEST.TRIMMED_MEAN are affected the same way. The behavior is inconsistent rather than intentional: adding the same value twice, or a single observation with weight 2, returns the correct result — only the single weight-1 sample case fails.

Root cause

In deps/t-digest-c, td_compress() has an early return if (total_weight <= 1) (introduced upstream in 43fe4be to guard the log(total_weight) denominator against division by zero). It returns before promoting the unmerged node to a merged centroid, so merged_nodes stays 0 and every query path takes its "empty histogram → nan" branch. The library's dedicated single-centroid handling in td_quantile/td_quantiles ("with one data point, all quantiles lead to Rome") was unreachable for this case.

This was fixed upstream in RedisBloom/t-digest-c@50edef3 (MOD-13821), but the submodule here is still pinned one commit behind (5b89d70), so the module still ships the bug.

Fix

  • Bump the deps/t-digest-c submodule from 5b89d70 to 50edef3 (single-commit bump; the only change is the td_compress fix).
  • Add a flow regression test (test_tdigest_single_observation) covering TDIGEST.QUANTILE, TDIGEST.CDF, and TDIGEST.TRIMMED_MEAN on a single-observation sketch, since the upstream fix landed without test coverage.

Testing

Built the module and ran the full tdigest flow suite locally (macOS arm64, Redis 8):

Total Tests Run: 35, Total Tests Failed: 0, Total Tests Passed: 35

Verified the exact repro from #870 now returns the observation value:

> TDIGEST.QUANTILE foo 0 0.5 0.9 1   →  123 123 123 123
> TDIGEST.CDF foo 100 123 200        →  0  0.5  1
> TDIGEST.TRIMMED_MEAN foo 0.1 0.9   →  123

Note

Low Risk
Behavior change is limited to the edge case of one observation in t-digest queries; CI change only gates S3 uploads on fork PRs.

Overview
Fixes TDIGEST returning nan when a sketch has exactly one weight-1 observation (issue #870), by picking up the upstream deps/t-digest-c fix where td_compress() no longer bails out before promoting that lone point into a merged centroid.

Adds flow regression test_tdigest_single_observation, asserting TDIGEST.QUANTILE, TDIGEST.CDF, and TDIGEST.TRIMMED_MEAN all return sensible values for a single added value.

CI: Linux and macOS flow workflows now skip S3 artifact upload on pull requests from forks (github.event.pull_request.head.repo.fork), so untrusted PRs do not run steps that use AWS credentials.

Reviewed by Cursor Bugbot for commit b522bfe. Bugbot is set up for automated code reviews on this repo. Configure here.

@CLAassistant

CLAassistant commented Jul 15, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

gabsow
gabsow previously approved these changes Aug 2, 2026
@gabsow

gabsow commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator

Thank you very much; it's a real fix that wasn't pinned to Bloom yet.
Approving and merging.

PerumalsamyR and others added 2 commits August 2, 2026 11:24
…isBloom#870)

Bump deps/t-digest-c to 50edef3, which fixes td_compress bailing out on
total_weight <= 1 before promoting the sole unmerged node to a merged
centroid. The sketch then looked empty to the query paths, so
TDIGEST.QUANTILE, TDIGEST.CDF and TDIGEST.TRIMMED_MEAN replied nan for
a sketch holding exactly one weight-1 observation.

Add a flow regression test covering all three commands.
@gabsow
gabsow force-pushed the fix-tdigest-single-observation-nan branch from 45bc5a5 to b522bfe Compare August 2, 2026 08:28
@sonarqubecloud

sonarqubecloud Bot commented Aug 2, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.68%. Comparing base (22aedee) to head (b522bfe).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1035      +/-   ##
==========================================
- Coverage   82.71%   82.68%   -0.04%     
==========================================
  Files          21       21              
  Lines        3275     3275              
==========================================
- Hits         2709     2708       -1     
- Misses        566      567       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AvivDavid23
AvivDavid23 enabled auto-merge (squash) August 2, 2026 11:14
@gabsow
gabsow disabled auto-merge August 2, 2026 11:27
@gabsow
gabsow merged commit 6698365 into RedisBloom:master Aug 2, 2026
61 of 62 checks passed
@PerumalsamyR
PerumalsamyR deleted the fix-tdigest-single-observation-nan branch August 2, 2026 18:55
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.

TDIGEST.QUANTILE returns NaN when there is only one observation

3 participants