Skip to content

WIP/RFC: basic promote_rule#42

Closed
gustafsson wants to merge 1 commit intoJuliaData:masterfrom
gustafsson:promote_rule
Closed

WIP/RFC: basic promote_rule#42
gustafsson wants to merge 1 commit intoJuliaData:masterfrom
gustafsson:promote_rule

Conversation

@gustafsson
Copy link
Copy Markdown
Contributor

@nalimilan is this what you had in mind? To promote arrays with and without Nullable to CategoricalArray or NullableCategoricalArray. Or do you want to capture any and all AbstractArray?

Copy link
Copy Markdown
Member

@nalimilan nalimilan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. That's what I had in mind, though I'm still not completely certain whether CategoricalArray + Array should promote to the former or the latter... Both could be justified, though what this PR does is most likely the best choice.

While you're at it, I would generalize this to any AbstractArray.

Comment thread test/13_arraycommon.jl
@test typeof(x) == CA{Int, 1, UInt8}
@test typeof(ux) == CA{Int, 1, CategoricalArrays.DefaultRefType}

@test promote_type(typeof(CA([3,2,1])), typeof([1,2])) == CA{Int,1,DefaultRefType}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the types directly instead of creating the objects and calling typeof.

Comment thread src/array.jl
@inbounds A.refs[I...] = get!(A.pool, convert(T, v))
end

@inline function promote_rule{T,N,R,T2}(::Type{CatArray{T,N,R}}, ::Type{Array{T2}})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don'tneed @inline. Also, you should add a type parameter <:CatArray and promote to it instead of CatArray (which is a union type). Instead of T and T2, the rest of the code base uses S and T.

Comment thread test/13_arraycommon.jl

@test promote_type(typeof(CA([3,2,1])), typeof([1,2])) == CA{Int,1,DefaultRefType}
@test promote_type(typeof(CA([3,2,1])), typeof(NullableArray([1,3]))) == NullableCategoricalArray{Int,1,DefaultRefType}
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also test cases in which the element types are different, e.g. Int and Float64? Would be good to test promoting (Nullable)CategoricalArray with CA, and check in particular that the reference type is as expected.

@gustafsson
Copy link
Copy Markdown
Contributor Author

What happens if another implementation of abstractarray wants to use promote_rule to promote any abstractarray to itself? Then we would be back to depend on the order of arguments. I don't have an example so perhaps this argument is moot. But I wanted to at least consider for a moment if this is a good design choice.

@nalimilan
Copy link
Copy Markdown
Member

Yes, that's a good question. A good example to consider is NullableArray. It currently doesn't provide any promote_rule or vcat methods, but it should probably (JuliaStats/NullableArrays.jl#167). It would promote any AbstractArray to NullableArray, which would create a conflict with CategoricalArray.

I think the solution is to fix the ambiguity by providing a special method for the CatArray/NullableArray combination. Since the signature is more specific, it would win over the AbstractArray fallback. This is just how any method ambiguity is handled in Julia.

@nalimilan
Copy link
Copy Markdown
Member

Bump. Do you want to finish this PR?

@nalimilan nalimilan closed this Apr 4, 2021
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.

2 participants