diff --git a/src/ColumnImplementations.jl b/src/ColumnImplementations.jl index b810a3e..31f06bc 100644 --- a/src/ColumnImplementations.jl +++ b/src/ColumnImplementations.jl @@ -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) diff --git a/src/PreimageCaches.jl b/src/PreimageCaches.jl index 6034436..205fe8a 100644 --- a/src/PreimageCaches.jl +++ b/src/PreimageCaches.jl @@ -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 diff --git a/test/Columns.jl b/test/Columns.jl index 7087a19..c29bc51 100644 --- a/test/Columns.jl +++ b/test/Columns.jl @@ -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