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

Improve array promotion rules #387

Merged
merged 2 commits into from
Mar 17, 2022
Merged

Improve array promotion rules #387

merged 2 commits into from
Mar 17, 2022

Conversation

nalimilan
Copy link
Member

PR #384 added promotion rules that recursively promote the eltype and reftype, but this has the drawback that the resulting
type is often not a supertype of the input types, forcing a conversion to a CategoricalArray which may have a Union type.
Instead, do the same as Array and AbstractArray fallbacks, which simply call promote_typejoin if eltypes do not match.

PR #384 added promotion rules that recursively promote the
eltype and reftype, but this has the drawback that the resulting
type is often not a supertype of the input types, forcing a conversion
to a `CategoricalArray` which may have a `Union` type.
Instead, do the same as `Array` and `AbstractArray` fallbacks, which
simply call `promote_typejoin` if eltypes do not match.
@nalimilan nalimilan requested a review from bkamins March 16, 2022 08:57
Comment on lines -2228 to -2258
@test promote_type(CategoricalVector{Int},
CategoricalVector{String}) ==
CategoricalVector{Union{Int, String}}
@test promote_type(CategoricalVector{Int, UInt32},
CategoricalVector{String, UInt32}) ==
CategoricalVector{Union{Int, String}, UInt32}
@test promote_type(CategoricalArray{Int, UInt32},
CategoricalArray{String, UInt32}) ==
CategoricalArray{Union{Int, String}, UInt32}
@test promote_type(CategoricalVector{Int, UInt32},
CategoricalMatrix{String, UInt32}) ==
CategoricalArray{Union{Int, String}}
@test promote_type(CategoricalVector{Int, UInt8},
CategoricalVector{String, UInt16}) ==
CategoricalVector{Union{Int, String}, UInt16}

@test promote_type(CategoricalVector{Int8},
CategoricalVector{Float64}) ==
CategoricalVector{Float64}
@test promote_type(CategoricalVector{Int8, UInt32},
CategoricalVector{Float64, UInt32}) ==
CategoricalVector{Float64, UInt32}
@test promote_type(CategoricalArray{Int8, UInt32},
CategoricalArray{Float64, UInt32}) ==
CategoricalArray{Float64, UInt32}
@test promote_type(CategoricalVector{Int8, UInt32},
CategoricalMatrix{Float64, UInt32}) ==
CategoricalArray{Float64}
@test promote_type(CategoricalVector{Int8, UInt8},
CategoricalVector{Float64, UInt16}) ==
CategoricalVector{Float64, UInt16}
Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately this PR isn't enough to get these two work. CategoricalVector{T, R} isn't a concrete type, and even if the element type of such arrays is completely determined by T and R, the type system doesn't know that, which makes promote_rule and el_same above fail to return a precise type. It is probably fixable, but that's not trivial either. So for now instead of changing these tests to use verbose CategoricalArray{T, R, N, V, C U} types, I rely on array tests below, which are more compact and equivalent.

What do you think? Maybe it's worth supporting promotion of short CategoricalArray{T, N, R} forms, if only to limit confusing users (who will never write all type parameters when checking things manually)?

Copy link
Member

@bkamins bkamins left a comment

Choose a reason for hiding this comment

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

The rules are not ideal, but at least they do not need to implicit conversions, so I think it is OK.

@nalimilan nalimilan merged commit c4ae8cc into master Mar 17, 2022
@nalimilan nalimilan deleted the nl/promote branch March 17, 2022 20:57
@nalimilan
Copy link
Member Author

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.

None yet

2 participants