Skip to content

Throw from any and all of a sparse array on a non-Boolean value, as dense does - #1056

Merged
ViralBShah merged 2 commits into
mainfrom
vs/any-all-nonbool
Oct 13, 2026
Merged

ViralBShah merged 2 commits into
mainfrom
vs/any-all-nonbool

Conversation

@ViralBShah

Copy link
Copy Markdown
Member

any and all of a sparse matrix or vector reduce the predicate's values with | and & and never check that they are Boolean, so a non-Boolean value is returned as the result where dense throws a TypeError. Next to a missing it is absorbed, and the result is missing.

Before

julia> any([1 0; 0 0])
ERROR: TypeError: non-boolean (Int64) used in boolean context

julia> any(sparse([1 0; 0 0])), all(sparse([1 0; 0 0])), any(sparsevec([1, 0]))
(1, 0, 1)

julia> M = reshape(Union{Missing,Int}[missing, 1], 2, 1);

julia> any(M)
ERROR: TypeError: non-boolean (Int64) used in boolean context

julia> any(sparse(M)), all(sparse(M)), any(sparse(vec(M)))
(missing, missing, missing)

After

julia> any(sparse([1 0; 0 0]))
ERROR: TypeError: non-boolean (Int64) used in boolean context

julia> all(sparse([1 0; 0 0]))
ERROR: TypeError: non-boolean (Int64) used in boolean context

julia> any(sparsevec([1, 0]))
ERROR: TypeError: non-boolean (Int64) used in boolean context

julia> any(sparse(M))
ERROR: TypeError: non-boolean (Int64) used in boolean context

julia> all(sparse(M))
ERROR: TypeError: non-boolean (Int64) used in boolean context

julia> any(sparse(vec(M)))
ERROR: TypeError: non-boolean (Int64) used in boolean context

Base._any and Base._all for SparseMatrixCSCOrColumnSubset and SparseVectorOrView pass the predicate straight to the |/& _mapreduce kernels, which accept any value those operators do. They now pass _boolean ∘ f, where _boolean lets a Bool or missing through and asserts ::Bool on anything else, the check the dense reductions make on each value. The composed predicate still specializes: any(iszero, A) and all(!iszero, A) at 10^6 stored entries take the same time and allocations as on main. The reductions along a dimension (dims=1) are not touched here.

Split from #1022.

This is a bug fix and could be backported, with the caveat that calls which returned a non-Boolean value now throw.

Written by Claude Code.

🤖 Generated with Claude Code

…as dense does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ViralBShah
ViralBShah enabled auto-merge (squash) October 13, 2026 03:14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ViralBShah
ViralBShah merged commit 49282d1 into main Oct 13, 2026
8 checks passed
@ViralBShah
ViralBShah deleted the vs/any-all-nonbool branch October 13, 2026 03:58
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.78%. Comparing base (c0f75f0) to head (62ecbde).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/reductions.jl 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1056      +/-   ##
==========================================
+ Coverage   94.75%   94.78%   +0.02%     
==========================================
  Files          16       16              
  Lines        9885     9886       +1     
==========================================
+ Hits         9367     9370       +3     
+ Misses        518      516       -2     

☔ 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.

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