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

Address MinAndMax generics warnings #52642

Merged
merged 6 commits into from
Mar 3, 2020

Conversation

javanna
Copy link
Member

@javanna javanna commented Feb 21, 2020

The MinAndMax encapsulates min and max values for a shard. It uses generics to make sure that the values are of the same type and are also comparable. Though there are warnings whenever this class is currently used, which are addressed with this commit.

Relates to #49092

The MinAndMax encapsulates min and max values for a shard. It uses generics to make sure that the values are of the same type and are also comparable. Though there are warnings whenever this class is currently used, which are addressed with this commit.

Relates to elastic#49092
@javanna javanna added >enhancement :Search/Search Search-related issues that do not fall into other categories v8.0.0 v7.7.0 labels Feb 21, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/Search)

Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

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

Some part of the change makes sense to me, like replacing Comparable with Comparable<T> but I wonder that some other changes are not needed like replacing T extends Comparable<? super T> with T extends Comparable<T> as the former is usually the right way to use generics with wildcards that need to extend Comparable.

@@ -520,6 +518,14 @@ public static FieldSortBuilder getPrimaryFieldSortOrNull(SearchSourceBuilder sou
return null;
}

@SuppressWarnings("unchecked")
private static <T extends Comparable<T>> MinAndMax<T> extractMinAndMax(IndexReader reader, String fieldName,
Function<byte[], Comparable<?>> converter) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

should it be a Function<byte[], ? extends T> to avoid the unchecked cast?

Copy link
Member Author

Choose a reason for hiding this comment

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

great point, I missed this

*/
public class MinAndMax<T extends Comparable<? super T>> implements Writeable {
public class MinAndMax<T extends Comparable<T>> implements Writeable {
Copy link
Contributor

Choose a reason for hiding this comment

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

In general this is the right way to use generics with Comparable?

@javanna
Copy link
Member Author

javanna commented Feb 25, 2020

thanks @jpountz for your feedback.

When I was working on this change I asked myself even if generics were really useful in this case.
I think the main benefit of generics in MinAndMax is to enforce that the two values (min and max) are of the same type. Currently we don't though as we have unsafe casts whenever we create an instance of MinAndMax. I modified the logic wherever we create an instance of MinAndMax so that we are more explicit and we always create MinAndMax safely.

I agree with you, T extends Comparable<? super T> is usually the way to go and gives more freedom to users, as what we need is a Comparable that does not necessarily has to only compare to self, it can compare to anything really. That though causes problems as we end up having to create a Comparator<MinAndMax<?>> and the following does not compile unless I replace T extends Comparable<? super T> with T extends Comparable<T>:

Comparator<MinAndMax<?>> cmp = order == SortOrder.ASC  ?
            Comparator.comparing(MinAndMax::getMin) : Comparator.comparing(MinAndMax::getMax);

At this point I wonder if all the flexibility given by <? super T> is needed. This is internal code, and we only ever instantiate this class providing either Long, Integer, Float, Double, String or BytesRef which are all comparable to self only.

@javanna
Copy link
Member Author

javanna commented Feb 28, 2020

run elasticsearch-ci/2

@javanna
Copy link
Member Author

javanna commented Feb 28, 2020

turns out that the compile errors that I was seeing are shown only in IntelliJ. They were weird so it is not surprising that this compiles from command line. I did not understand yet what's causing the error in IntelliJ either, I use the same compiler, and the project itself compiles but when I open MinAndMax there's a line that's marked error where a generics error is shown (it is not a warning).

I think this is ready then, I went back to <T extends Comparable<? super T>> which I agree is the best way to go.

@javanna
Copy link
Member Author

javanna commented Mar 2, 2020

run elasticsearch-ci/bwc

@javanna
Copy link
Member Author

javanna commented Mar 2, 2020

Build is green, this should be finally ready ;)

Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

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

Thanks Luca, this LGTM.

@javanna javanna merged commit bd2fc15 into elastic:master Mar 3, 2020
javanna added a commit to javanna/elasticsearch that referenced this pull request Mar 3, 2020
`MinAndMax` encapsulates min and max values for a shard. It uses generics to make sure that the values are of the same type and are also comparable. Though there are warnings whenever this class is currently used, which are addressed with this commit.

Relates to elastic#49092
@javanna
Copy link
Member Author

javanna commented Mar 3, 2020

thank you very much @jpountz ;)

javanna added a commit that referenced this pull request Mar 3, 2020
`MinAndMax` encapsulates min and max values for a shard. It uses generics to make sure that the values are of the same type and are also comparable. Though there are warnings whenever this class is currently used, which are addressed with this commit.

Relates to #49092
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search/Search Search-related issues that do not fall into other categories v7.7.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants