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

Define OrderStyle for Union{} #36810

Merged
merged 4 commits into from
Aug 13, 2020
Merged

Define OrderStyle for Union{} #36810

merged 4 commits into from
Aug 13, 2020

Conversation

bkamins
Copy link
Member

@bkamins bkamins commented Jul 26, 2020

Removes ambiguity:

julia> Base.OrderStyle(Union{})
ERROR: MethodError: Base.OrderStyle(::Type{Union{}}) is ambiguous. Candidates:

This error is relevant as with current unique! definition that relies on OrderStyle one can have a problem in corner cases. E.g.:

julia> [i for i in ["1"] if i isa Int]
0-element Array{Union{},1}

I propose to make this case Unordered() as probably this was the intention of the original implementation, but probably it does not matter much which option is chosen.

bkamins added 2 commits July 27, 2020 00:01
Removes ambiguity:
```
julia> Base.OrderStyle(Union{})
ERROR: MethodError: Base.OrderStyle(::Type{Union{}}) is ambiguous. Candidates:
```
This error is relevant as with current `unique!` definition that relies on `OrderStyle` one can have a problem in corner cases. E.g.:
```
julia> [i for i in ["1"] if i isa Int]
0-element Array{Union{},1}
```

I propose to make this case `Unordered()` as probably this was the intention of the original implementation, but probably it does not matter much which option is chosen.
@bkamins
Copy link
Member Author

bkamins commented Jul 26, 2020

Actually maybe Union{} should be Ordered
as it is natural to assume that if A <: B then if B is Ordered then A is Ordered,
and for Unordered such a relationship does not have to hold.

@bkamins
Copy link
Member Author

bkamins commented Jul 27, 2020

@nalimilan - I cannot see your comment I got via e-mail, but I can answer it. Initially I thought to use Unordered, but I changed my mind and I recommend to use Ordered for Union{}.

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.

I guess Ordered() is OK since issorted(Union{}[]) is true.

@bkamins
Copy link
Member Author

bkamins commented Jul 30, 2020

@timholy merging this is required I think as otherwise after #36449 this missing definition will manifest itself in practice (we already have a case of this in DataFrames.jl).

@ararslan ararslan added bugfix This change fixes an existing bug triage This should be discussed on a triage call labels Jul 31, 2020
@bkamins
Copy link
Member Author

bkamins commented Aug 3, 2020

Also my question is - what is the exact meaning of this trait? Should it be related to < or isless? The question is that I assume it is isless, and in this case Union{Missing, Real}, Union{Missing, Symbol} and Union{Missing, AbstractString} also can be considered as Ordered() I think.

Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Thanks @bkamins!

I'd merge this as-is except that I see it's been marked for triage.

@KristofferC
Copy link
Member

What's there to triage here @ararslan?

@timholy
Copy link
Member

timholy commented Aug 3, 2020

Also my question is - what is the exact meaning of this trait? Should it be related to < or isless? The question is that I assume it is isless, and in this case Union{Missing, Real}, Union{Missing, Symbol} and Union{Missing, AbstractString} also can be considered as Ordered() I think.

Good question. There's no unique answer, but I'd take "whatever sort does" as the reference. Since the default comparison function for sort is isless, I'd agree with you.

@nalimilan
Copy link
Member

Good point. Maybe let's change this in another PR to avoid holding this one back?

@bkamins
Copy link
Member Author

bkamins commented Aug 3, 2020

OK - I will open a separate PR for this when this is merged.

@timholy
Copy link
Member

timholy commented Aug 13, 2020

Bump @ararslan, does this actually need triage?

@KristofferC KristofferC removed the triage This should be discussed on a triage call label Aug 13, 2020
@timholy timholy merged commit 03e1a89 into JuliaLang:master Aug 13, 2020
@timholy
Copy link
Member

timholy commented Aug 13, 2020

Thanks as always, @bkamins !

@bkamins bkamins deleted the patch-25 branch August 13, 2020 17:02
@bkamins
Copy link
Member Author

bkamins commented Aug 13, 2020

@nalimilan, so before opening a PR, maybe let us discuss if we want to add Ordered trait to:

  • other types defined in base that support fast isless, I do not want to list them here but Char is an example
  • adding union with Missing everywhere for them (and then defining the method only for Missing to avoid dispatch ambiguity)

This will not be used much probably (so we could ignore the fact that we do not have this trait for them), but for consistency it would be cleaner to have it. I am not sure what is best (I am leaning towards leaving things as is and that is why I am commenting here and not opening a PR)

@nalimilan
Copy link
Member

I agree that it seems the trait should be defined for all types that support isless, including Union{T, Missing}. The meaning of the trait should also be documented (notably the fact that it's used to choose fast paths).

@bkamins
Copy link
Member Author

bkamins commented Aug 21, 2020

@ararslan - is this going to be backported into Julia 1.5.1 release?

@KristofferC
Copy link
Member

This didn't have a backport label so it was not included in 1.5.1.

@bkamins
Copy link
Member Author

bkamins commented Aug 21, 2020

I know, but the original discussion was to backport it it - and that is why I am asking.

My question is if then the plan is to have it backported to 1.5 at some point or it will be in 1.6.

@nalimilan
Copy link
Member

Tentatively adding the backport label so that at least it's considered for 1.5.2.

KristofferC pushed a commit that referenced this pull request Aug 26, 2020
Removes ambiguity:
```
julia> Base.OrderStyle(Union{})
ERROR: MethodError: Base.OrderStyle(::Type{Union{}}) is ambiguous. Candidates:
```
This error is relevant as with current `unique!` definition that relies on `OrderStyle` one can have a problem in corner cases. E.g.:
```
julia> [i for i in ["1"] if i isa Int]
0-element Array{Union{},1}
```

Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
(cherry picked from commit 03e1a89)
@KristofferC KristofferC mentioned this pull request Aug 26, 2020
29 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants