Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP/RFC: basic promote_rule #42

Closed
wants to merge 1 commit into from

Conversation

gustafsson
Copy link
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
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.

@@ -280,6 +280,9 @@ for (CA, A) in ((CategoricalArray, Array), (NullableCategoricalArray, NullableAr
@test x == ux
@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
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.

@@ -339,6 +339,14 @@ end
@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
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.

@@ -280,6 +280,9 @@ for (CA, A) in ((CategoricalArray, Array), (NullableCategoricalArray, NullableAr
@test x == ux
@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}
@test promote_type(typeof(CA([3,2,1])), typeof(NullableArray([1,3]))) == NullableCategoricalArray{Int,1,DefaultRefType}
end
Copy link
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
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
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
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