Skip to content

Commit a2adce0

Browse files
committed
Fix minimum/maximum over dimensions with missing values for BigInt
Types such as `BigInt` don't support `typemin` and `typemax` so the current method to find an initial value different from `missing` fails. Use the largest/smallest non-missing value to initialize the array instead. This is an inefficient approach. Faster alternatives would be to avoid using an initial value at all, and instead keep track of whether a value has been set in a separate mask; or to use `typemax`/`typemin` for types that support them.
1 parent 85d7cca commit a2adce0

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

base/reducedim.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,18 @@ for (f1, f2, initval, typeextreme) in ((:min, :max, :Inf, :typemax), (:max, :min
146146
T = _realtype(f, promote_union(eltype(A)))
147147
Tr = v0 isa T ? T : typeof(v0)
148148

149-
# but NaNs and missing need to be avoided as initial values
149+
# but NaNs, missing and unordered values need to be avoided as initial values
150150
if v0 isa Number && isnan(v0)
151151
# v0 is NaN
152152
v0 = oftype(v0, $initval)
153-
elseif isunordered(v0)
154-
# v0 is missing or a third-party unordered value
153+
elseif ismissing(v0)
154+
if !all(ismissing, A)
155+
v0 = mapreduce(f, $f2, skipmissing(A))
156+
end
157+
elseif isunordered(v0) # v0 is a third-party unordered value
155158
Tnm = nonmissingtype(Tr)
156159
# TODO: Some types, like BigInt, don't support typemin/typemax.
157-
# So a Matrix{Union{BigInt, Missing}} can still error here.
160+
# So a Matrix{Union{BigInt, T}} can still error here.
158161
v0 = $typeextreme(Tnm)
159162
end
160163
# v0 may have changed type.

test/reduce.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,16 @@ end
377377
@test maximum(Vector(Int16(1):Int16(100))) === Int16(100)
378378
@test maximum(Int32[1,2]) === Int32(2)
379379

380+
@testset "minimum/maximum over dims with missing (#35308)" begin
381+
for T in (Int, Float64, BigInt, BigFloat)
382+
x = Union{T, Missing}[1 missing; 2 missing]
383+
@test isequal(minimum(x, dims=1), reshape([1, missing], 1, :))
384+
@test isequal(maximum(x, dims=1), reshape([2, missing], 1, :))
385+
@test isequal(minimum(x, dims=2), reshape([missing, missing], :, 1))
386+
@test isequal(maximum(x, dims=2), reshape([missing, missing], :, 1))
387+
end
388+
end
389+
380390
A = circshift(reshape(1:24,2,3,4), (0,1,1))
381391
@test extrema(A,dims=1) == reshape([(23,24),(19,20),(21,22),(5,6),(1,2),(3,4),(11,12),(7,8),(9,10),(17,18),(13,14),(15,16)],1,3,4)
382392
@test extrema(A,dims=2) == reshape([(19,23),(20,24),(1,5),(2,6),(7,11),(8,12),(13,17),(14,18)],2,1,4)

0 commit comments

Comments
 (0)