-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat: Add ip_prefix function [4/n] #11514
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
=================== | ||
IP Functions | ||
=================== | ||
|
||
.. function:: ip_prefix(ip_address, prefix_bits) -> ipprefix | ||
|
||
Returns the IP prefix of a given ``ip_address`` with subnet size of ``prefix_bits``. | ||
``ip_address`` can be either of type ``VARCHAR`` or type ``IPADDRESS``. :: | ||
|
||
SELECT ip_prefix(CAST('192.168.255.255' AS IPADDRESS), 9); -- {192.128.0.0/9} | ||
SELECT ip_prefix('2001:0db8:85a3:0001:0001:8a2e:0370:7334', 48); -- {2001:db8:85a3::/48} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
velox/functions/prestosql/tests/IPAddressFunctionsTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "velox/common/base/tests/GTestUtils.h" | ||
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" | ||
|
||
namespace facebook::velox::functions::prestosql { | ||
class IPAddressFunctionsTest : public functions::test::FunctionBaseTest { | ||
protected: | ||
std::optional<std::string> ipPrefixFunctionFromIpAddress( | ||
const std::optional<std::string>& input, | ||
const std::optional<int64_t>& mask) { | ||
return evaluateOnce<std::string>( | ||
"cast(ip_prefix(cast(c0 as ipaddress), c1) as varchar)", input, mask); | ||
} | ||
|
||
std::optional<std::string> ipPrefixFromVarChar( | ||
const std::optional<std::string>& input, | ||
const std::optional<int64_t>& mask) { | ||
return evaluateOnce<std::string>( | ||
"cast(ip_prefix(c0, c1) as varchar)", input, mask); | ||
} | ||
Comment on lines
+23
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are |
||
}; | ||
|
||
TEST_F(IPAddressFunctionsTest, ipPrefixFromIpAddress) { | ||
ASSERT_EQ(ipPrefixFunctionFromIpAddress("1.2.3.4", 24), "1.2.3.0/24"); | ||
ASSERT_EQ(ipPrefixFunctionFromIpAddress("1.2.3.4", 32), "1.2.3.4/32"); | ||
ASSERT_EQ(ipPrefixFunctionFromIpAddress("1.2.3.4", 0), "0.0.0.0/0"); | ||
ASSERT_EQ(ipPrefixFunctionFromIpAddress("::ffff:1.2.3.4", 24), "1.2.3.0/24"); | ||
ASSERT_EQ(ipPrefixFunctionFromIpAddress("64:ff9b::17", 64), "64:ff9b::/64"); | ||
ASSERT_EQ( | ||
ipPrefixFunctionFromIpAddress("64:ff9b::17", 127), "64:ff9b::16/127"); | ||
ASSERT_EQ( | ||
ipPrefixFunctionFromIpAddress("64:ff9b::17", 128), "64:ff9b::17/128"); | ||
ASSERT_EQ(ipPrefixFunctionFromIpAddress("64:ff9b::17", 0), "::/0"); | ||
ASSERT_EQ( | ||
ipPrefixFunctionFromIpAddress( | ||
"2001:0db8:85a3:0001:0001:8a2e:0370:7334", 48), | ||
"2001:db8:85a3::/48"); | ||
ASSERT_EQ( | ||
ipPrefixFunctionFromIpAddress( | ||
"2001:0db8:85a3:0001:0001:8a2e:0370:7334", 52), | ||
"2001:db8:85a3::/52"); | ||
ASSERT_EQ( | ||
ipPrefixFunctionFromIpAddress( | ||
"2001:0db8:85a3:0001:0001:8a2e:0370:7334", 128), | ||
"2001:db8:85a3:1:1:8a2e:370:7334/128"); | ||
ASSERT_EQ( | ||
ipPrefixFunctionFromIpAddress( | ||
"2001:0db8:85a3:0001:0001:8a2e:0370:7334", 0), | ||
"::/0"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFunctionFromIpAddress("::ffff:1.2.3.4", -1), | ||
"IPv4 subnet size must be in range [0, 32]"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFunctionFromIpAddress("::ffff:1.2.3.4", 33), | ||
"IPv4 subnet size must be in range [0, 32]"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFunctionFromIpAddress("64:ff9b::10", -1), | ||
"IPv6 subnet size must be in range [0, 128]"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFunctionFromIpAddress("64:ff9b::10", 129), | ||
"IPv6 subnet size must be in range [0, 128]"); | ||
} | ||
|
||
TEST_F(IPAddressFunctionsTest, ipPrefixFromVarChar) { | ||
ASSERT_EQ(ipPrefixFromVarChar("1.2.3.4", 24), "1.2.3.0/24"); | ||
ASSERT_EQ(ipPrefixFromVarChar("1.2.3.4", 32), "1.2.3.4/32"); | ||
ASSERT_EQ(ipPrefixFromVarChar("1.2.3.4", 0), "0.0.0.0/0"); | ||
ASSERT_EQ(ipPrefixFromVarChar("::ffff:1.2.3.4", 24), "1.2.3.0/24"); | ||
ASSERT_EQ(ipPrefixFromVarChar("64:ff9b::17", 64), "64:ff9b::/64"); | ||
ASSERT_EQ(ipPrefixFromVarChar("64:ff9b::17", 127), "64:ff9b::16/127"); | ||
ASSERT_EQ(ipPrefixFromVarChar("64:ff9b::17", 128), "64:ff9b::17/128"); | ||
ASSERT_EQ(ipPrefixFromVarChar("64:ff9b::17", 0), "::/0"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("::ffff:1.2.3.4", -1), | ||
"IPv4 subnet size must be in range [0, 32]"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("::ffff:1.2.3.4", 33), | ||
"IPv4 subnet size must be in range [0, 32]"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("64:ff9b::10", -1), | ||
"IPv6 subnet size must be in range [0, 128]"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("64:ff9b::10", 129), | ||
"IPv6 subnet size must be in range [0, 128]"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("localhost", 24), | ||
"Cannot cast value to IPADDRESS: localhost"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("64::ff9b::10", 24), | ||
"Cannot cast value to IPADDRESS: 64::ff9b::10"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("64:face:book::10", 24), | ||
"Cannot cast value to IPADDRESS: 64:face:book::10"); | ||
VELOX_ASSERT_THROW( | ||
ipPrefixFromVarChar("123.456.789.012", 24), | ||
"Cannot cast value to IPADDRESS: 123.456.789.012"); | ||
} | ||
|
||
} // namespace facebook::velox::functions::prestosql |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we call
std::reverse
twice.While it is fine for the initial implementation and probably not having a major performance impact, but is there a way not to
std::reverse
at all?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't think of a good way because we store the IPAddress as int128_t instead of varbinary. So we need to pay 2 reverses, one to get the ip, and then another to get back the int128_t.
Since it is only 16 bytes, it shouldn't be that expensive