Skip to content

Commit

Permalink
Fix inference for mapreduce on skipmissing (#35475)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Apr 18, 2020
1 parent c3fc367 commit 24a7997
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function _mapreduce(f, op, ::IndexLinear, itr::SkipMissing{<:AbstractArray})
i += 1
end
i > ilast && return mapreduce_empty(f, op, eltype(itr))
a1 = ai
a1::eltype(itr) = ai
i += 1
while i <= ilast
@inbounds ai = A[i]
Expand Down
27 changes: 17 additions & 10 deletions test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,24 +440,31 @@ end
for T in (Int, Float64),
A in (rand(T, 10), rand(T, 1000), rand(T, 10000))
if T === Int
@test sum(A) === sum(skipmissing(A)) ===
reduce(+, skipmissing(A)) === mapreduce(identity, +, skipmissing(A))
@test sum(A) === @inferred(sum(skipmissing(A))) ===
@inferred(reduce(+, skipmissing(A))) ===
@inferred(mapreduce(identity, +, skipmissing(A)))
else
@test sum(A) sum(skipmissing(A)) ===
reduce(+, skipmissing(A)) === mapreduce(identity, +, skipmissing(A))
@test sum(A) @inferred(sum(skipmissing(A))) ===
@inferred(reduce(+, skipmissing(A))) ===
@inferred(mapreduce(identity, +, skipmissing(A)))
end
@test mapreduce(cos, *, A) mapreduce(cos, *, skipmissing(A))
@test mapreduce(cos, *, A)
@inferred(mapreduce(cos, *, skipmissing(A)))

B = Vector{Union{T,Missing}}(A)
replace!(x -> rand(Bool) ? x : missing, B)
if T === Int
@test sum(collect(skipmissing(B))) === sum(skipmissing(B)) ===
reduce(+, skipmissing(B)) === mapreduce(identity, +, skipmissing(B))
@test sum(collect(skipmissing(B))) ===
@inferred(sum(skipmissing(B))) ===
@inferred(reduce(+, skipmissing(B))) ===
@inferred(mapreduce(identity, +, skipmissing(B)))
else
@test sum(collect(skipmissing(B))) sum(skipmissing(B)) ===
reduce(+, skipmissing(B)) === mapreduce(identity, +, skipmissing(B))
@test sum(collect(skipmissing(B))) @inferred(sum(skipmissing(B))) ===
@inferred(reduce(+, skipmissing(B))) ===
@inferred(mapreduce(identity, +, skipmissing(B)))
end
@test mapreduce(cos, *, collect(skipmissing(A))) mapreduce(cos, *, skipmissing(A))
@test mapreduce(cos, *, collect(skipmissing(A)))
@inferred(mapreduce(cos, *, skipmissing(A)))

# Test block full of missing values
B[1:length(B)÷2] .= missing
Expand Down

0 comments on commit 24a7997

Please sign in to comment.