Skip to content

Throw a clear error for degenerate-length data vectors (fixes #121) - #206

Merged
nalimilan merged 4 commits into
JuliaStats:masterfrom
JohnCobbler:fix/121-quantile-degenerate-range
Sep 25, 2026
Merged

nalimilan merged 4 commits into
JuliaStats:masterfrom
JohnCobbler:fix/121-quantile-degenerate-range

Conversation

@JohnCobbler

@JohnCobbler JohnCobbler commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

quantile! throws ArgumentError("empty data vector") when length is 0. Fixes #121.

typemin(Int):typemax(Int) reports length 0. isempty is false, so the old emptiness check let it through to @assert n > 0. The guard is length(v) == 0.

r = typemin(Int):typemax(Int)
quantile!([0], r, [1])        # ArgumentError
quantile(r, 0.5; sorted=true) # ArgumentError

quantile(r, 0.5) with sorted=false still errors in Base.copymutable.

…ats#121)

A range whose length overflows (e.g. typemin(Int):typemax(Int)) reports
length 0 while not being empty, so it passed the isempty guard in
_quantilesort! and then tripped an internal '@Assert n > 0' in _quantile.
Add a length-0 guard that throws an informative ArgumentError, and turn the
now-redundant assertion into a defensive ArgumentError so the check also holds
under --check-bounds=no.

Co-authored-by: Claude <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Jun 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.43%. Comparing base (e7bf7a4) to head (3f8b0f0).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #206   +/-   ##
=======================================
  Coverage   96.43%   96.43%           
=======================================
  Files           2        2           
  Lines         449      449           
=======================================
  Hits          433      433           
  Misses         16       16           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/Statistics.jl Outdated
Comment on lines +1031 to +1032
"integer range whose length overflows (e.g. `typemin(Int):typemax(Int)`) — " *
"collect it to a concrete vector first"))

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 believe that such a vector would take up 148 exabytes, so probably not the right recommendation.

@JohnCobbler

Copy link
Copy Markdown
Contributor Author

Agreed. 148 exabytes is not a useful suggestion. The message says the length doesn't fit in Int, and asks for a collection whose length does.

The example range is enormous. Point the caller at a collection
whose length fits in Int.
@nalimilan

Copy link
Copy Markdown
Member

Thanks! Though I have the impression that printing a specific message for this particular range is overkill. There are probably many places in Statistics, and other JuliaStats packages where we would have to do the same. Yet it seems very unlikely that people would use such a range in practice. So while being correct is of course a requirement, providing a nice error message doesn't seem super important.

How about just replacing the isempty(v) check with length(v) == 0? Or maybe even better, try to fix this in Base, along the lines of JuliaLang/julia#45389 (comment).

@JohnCobbler

Copy link
Copy Markdown
Contributor Author

Fair point. The guard is length(v) == 0 now, and this range gets the same empty-vector error as Int[]. I'll leave the Base length change to JuliaLang/julia#45389.

An overflowing range reports length 0. It now gets the same
empty-vector error. No separate message.
@nalimilan
nalimilan merged commit 59c5fec into JuliaStats:master Sep 25, 2026
18 checks passed
@nalimilan

Copy link
Copy Markdown
Member

Thanks!

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.

quantile! produces an unrelated assertion error when computing quantiles of a large range

4 participants