-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Feature Request: IP Address Version UDF #13269
Comments
Isn't this possible as |
I don't think that's possible. While it's true that IPv6 has more bits (128) than IPv4 (32), it is possible for the two addresses to still take on the same value. |
For example:
The former is IPv6 and the latter is IPv4. Numerically, they both take on the value 1. |
The two types appear to be incomparable as well:
|
Is there a way I can see how many bits were allocated to store the IPADDRESS? Edit: Just checked, I don't think we can do that. |
Hmm, if you want to distinguish whether an IP is v6 or v4 by format, that's not possible. Internally all ip addresses are represented in v6. |
Actually |
They are comparable, otherwise the above query would fail. |
Ah, I see. I assumed the IPv4 addresses were prefixed with 0s instead of 1s. The context is that we have a table with IP addresses in string form. The table does not indicate whether the IP address is v4 or v6. We want to compute the version of the IP address, based on the string. This is an important signal we need for other parts of our system. I was thinking we could just cast the string to IPADDRESS and then call a IP_VERSION function on it. That seems to be difficult, given that IPv4 and IPv6 addresses use the same datatype for storage. So, perhaps we could instead have a function of the form:
|
You can do |
That looks awesome! It will be amazing if we can do this without a UDF! Let me try it out and verify that it works for our use-case. |
Once we ship SQL function support we can introduce this as a SQL function (#9613). We are targeting end of H2 for that. |
I did find one case that breaks:
I was hoping it would be classified This is not a surprise given that IPv4 addresses are mapped to IPv6 addresses as you described (https://tools.ietf.org/html/rfc4291.html#section-2.5.5.2). That being said, I don't believe IPv6 addresses of the form <80 zeros>:ffff: correspond to legitimate IPv6 addresses, so we may be able to ignore this case. |
So, I'm going to close this task because the solution described here is good enough for our use-case, and probably many others. |
EDIT: Resolved - no need for a UDF. Do this:
This will not work for IPv6 addresses that take the form
<80 zeroes>:ffff:<some stuff>
, but you probably won't see those in practice.===========
Name:
IP_VERSION(ip_address)
Arguments:
ip_address
(typeIPADDRESS
)Return type:
VARCHAR
Return value: Returns 'v4' if the IP address is an IPv4. Returns 'v6' if the IP address is IPv6. Returns NULL if the IPADDRESS is invalid (I don't think this is actually possible).
Implementation:
I think Java's InetAddress type (and related types) could let us do this.
https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html
It's probably easy to implement ourselves too.
The text was updated successfully, but these errors were encountered: