Skip to content

Commit

Permalink
Merge pull request #58 from slwu89/slwu89/convert
Browse files Browse the repository at this point in the history
Support `Missing` in union attribute types
  • Loading branch information
epatters authored Sep 28, 2023
2 parents 9d9c019 + 07fb97d commit 1655674
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ColumnImplementations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ our Attr Variable indices with the Julia type of Int
end
Base.isless(x::AttrVar,y::AttrVar) = x.val < y.val
Base.convert(::Type{T}, x::T) where {T>:Union{Nothing,AttrVar}} = x
Base.convert(::Type{T}, x::T) where {T>:Union{Missing,AttrVar}} = x
Base.convert(::Type{T}, x::T) where {T>:AttrVar} = x
Base.convert(::Type{T}, x) where {T>:AttrVar} = convert(Base.typesplit(T, AttrVar), x)

Expand Down
8 changes: 8 additions & 0 deletions src/PreimageCaches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ function preimage(dom, m::Mapping{S,T}, ::TrivialCache{S,T}, y) where {S,T}
[x for x in dom if get(m, x, nothing) == y]
end

function preimage(dom, m::Mapping{S,T}, ::TrivialCache{S,T}, y::Missing) where {S,T>:Union{Missing}}
[x for x in dom if ismissing(get(m, x, nothing))]
end

function preimage(dom, m::Mapping{S,T}, ::TrivialCache{S,T}, y) where {S,T>:Union{Missing}}
[x for x in dom if coalesce(get(m, x, nothing) == y, false)]
end

function preimage_multi(dom, m::Mapping{S,T}, pc::TrivialCache{S,T}, ys) where {S,T}
map(y -> preimage(dom, m, pc, y), ys)
end
Expand Down
28 changes: 28 additions & 0 deletions test/Columns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,32 @@ for coltype in coltypes
@test collect(preimage(OneTo(3), col, :A)) == [1]
end

# Int → Union type columns
#---------------------

coltypes = [
column_type(AttrChoice(Union{Symbol,Nothing}), NoIndex, sparsity)
for sparsity in [Dense, Sparse(Int)]
]

for coltype in coltypes
col = coltype(1=>:A,2=>:B,3=>nothing)
@test isempty(preimage(OneTo(3), col, :F))
@test collect(preimage(OneTo(3), col, :A)) == [1]
@test collect(preimage(OneTo(3), col, nothing)) == [3]
end

coltypes = [
column_type(AttrChoice(Union{Symbol,Missing}), NoIndex, sparsity)
for sparsity in [Dense, Sparse(Int)]
]

for coltype in coltypes
col = coltype(1=>:A,2=>:B,3=>missing)
@test isempty(preimage(OneTo(3), col, :F))
@test collect(preimage(OneTo(3), col, :A)) == [1]
@test collect(preimage(OneTo(3), col, missing)) == [3]
end


end # module

0 comments on commit 1655674

Please sign in to comment.