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

Add back range support to ip fields. #17777

Merged
merged 1 commit into from
Apr 22, 2016

Conversation

jpountz
Copy link
Contributor

@jpountz jpountz commented Apr 15, 2016

ip fields currently fail range queries when either bound is inclusive. This
commit makes ranges also work in the exclusive case to be consistent with other
data types.

Relates to #17971

@jpountz jpountz added >non-issue :Search/Search Search-related issues that do not fall into other categories v5.0.0-alpha2 labels Apr 15, 2016
InetAddresses.forString("2001:db8::1"),
InetAddresses.forString("2001:db8::fffe")),
ft.rangeQuery("2001:db8::", "2001:db8::ffff", false, false));
}
Copy link
Member

Choose a reason for hiding this comment

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

What about passing in the same value for upper and lower, and both inclusive=false. This should be an invalid range right? Do we have a check for that?

@rjernst
Copy link
Member

rjernst commented Apr 15, 2016

LGTM

@rmuir
Copy link
Contributor

rmuir commented Apr 17, 2016

this looks correct to me (for ipv4 and ipv6). we encode into internal format (which is always 128 bits) and treat it as a 128-bit number, adding or subtracting one to the endpoints.

maybe it would be good to test out some of the real corner cases (address already all zeros or all 1s, addresses on the ipv4-mapped boundary). Its not clear to me what will happen, i think probably a strange exception from numericutils.

@jpountz
Copy link
Contributor Author

jpountz commented Apr 18, 2016

@rjernst @rmuir I pushed more tests

@rmuir
Copy link
Contributor

rmuir commented Apr 18, 2016

Thanks for the endpoints test. I'm not sure if its worth trying to improve the exception to be more understandable by the user (maybe the generic one could be improved, but it would still be limited to generic terms like underflow/overflow).

As far as the "boundaries" I meant more the cases around ::ffff:0.0.0.0 and ::ffff:255.255.255.255. I can see from your code that should work fine, because you increment/decrement in 128-bit space. But bugs around this area (where addresses change from 4 bytes to 16 bytes) seem likely if the code is refactored.

@rmuir
Copy link
Contributor

rmuir commented Apr 18, 2016

Also, I strongly dislike booleans and nulls in the lucene api, but if we are going to support this thing, maybe we should consider methods in InetAddressPoint.java like:

public static InetAddress nextUp(InetAddress address);
public static InetAddress nextDown(InetAddress address);

These could have better error messages for overflow and anyone wanting to do this can do it in a less fragile way.

@rmuir
Copy link
Contributor

rmuir commented Apr 18, 2016

Also, it is worth thinking about consistency for these ranges across all types as much as possible. Do integer/long fields throw exceptions in these cases or will Integer.MAX_VALUE exclusive still return Integer.MAX_VALUE?

For double/float fields the behavior may really need to be different, depends if you think -Infinity exclusive will still include -Infinity and so on.

@jpountz
Copy link
Contributor Author

jpountz commented Apr 20, 2016

Do integer/long fields throw exceptions in these cases or will Integer.MAX_VALUE exclusive still return Integer.MAX_VALUE?

They return a MatchNoDocsQuery in that case since there is no greator integer than MAX_VALUE. We could do the same for ip addresses.

For double/float fields the behavior may really need to be different, depends if you think -Infinity exclusive will still include -Infinity and so on.

Right, this is what is happening right now. If you pass POSITIVE_INFINITY as a lower bound and it is exclusive, then we just call Math.nextUp which returns POSITIVE_INFINITY. So POSITIVE_INFINITY will be included in practice.

@jpountz
Copy link
Contributor Author

jpountz commented Apr 20, 2016

maybe we should consider methods in InetAddressPoint.java

I opened https://issues.apache.org/jira/browse/LUCENE-7234.

@rmuir
Copy link
Contributor

rmuir commented Apr 20, 2016

Right, this is what is happening right now. If you pass POSITIVE_INFINITY as a lower bound and it is exclusive, then we just call Math.nextUp which returns POSITIVE_INFINITY. So POSITIVE_INFINITY will be included in practice.

Technically i think NaN is better here (it sorts after POSITIVE_INFINITY). In all cases I don't know what to suggest: this is why i really think having an api taking booleans and nulls is unintuitive and too hard to reason about.

All of these problems are artificially created by a bad query DSL.

@jpountz
Copy link
Contributor Author

jpountz commented Apr 20, 2016

I pushed one more commit. I had to fork some Lucene code in order to be able to leverage the new nextUp/nextDown functions and have the fix for newPrefixQuery. Hopefully Lucene 6.1 will be out before elasticsearch 5.0 and we will be able to remove this X class before the GA.

The range method now behaves consistently with integers: if the lower (resp. upper) bound is the maximum (resp. minimum) value and is not inclusive, then a MatchNoDocsQuery is returned.

`ip` fields currently fail range queries when either bound is inclusive. This
commit makes ranges also work in the exclusive case to be consistent with other
data types.
@jpountz jpountz merged commit 370af45 into elastic:master Apr 22, 2016
@jpountz jpountz deleted the enhancement/ip_ranges branch April 22, 2016 07:58
@jpountz jpountz mentioned this pull request Apr 26, 2016
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Search/Search Search-related issues that do not fall into other categories v5.0.0-alpha2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants