forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
501 additions
and
9 deletions.
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
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,7 @@ | ||
============== | ||
IPADDRESS functions | ||
============== | ||
|
||
.. function:: ipaddress() -> uuid | ||
|
||
Returns a pseudo randomly generated UUID (type 4). |
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
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,96 @@ | ||
/* | ||
* 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. | ||
*/ | ||
#pragma once | ||
|
||
#include <boost/uuid/uuid.hpp> | ||
#include <boost/uuid/uuid_generators.hpp> | ||
|
||
#include "velox/functions/Macros.h" | ||
#include "velox/functions/Registerer.h" | ||
#include "velox/functions/prestosql/types/UuidType.h" | ||
#include "velox/functions/prestosql/types/IPAddressType.h" | ||
#include "velox/functions/lib/string/StringImpl.h" | ||
|
||
#include <iostream> | ||
|
||
namespace facebook::velox::functions { | ||
|
||
template <typename T> | ||
struct IPAddressFunction { | ||
VELOX_DEFINE_FUNCTION_TYPES(T); | ||
static constexpr bool is_deterministic = false; | ||
|
||
FOLLY_ALWAYS_INLINE void call(int128_t& result) { | ||
boost::uuids::uuid uuid = generator_(); | ||
memcpy(&result, uuid.data, 16); | ||
} | ||
|
||
private: | ||
boost::uuids::random_generator generator_; | ||
}; | ||
|
||
|
||
template <typename T> | ||
struct IPPrefixFunction { | ||
VELOX_DEFINE_FUNCTION_TYPES(T); | ||
|
||
FOLLY_ALWAYS_INLINE void call(out_type<Varchar>& result, | ||
const arg_type<IPAddress>& ip, | ||
const arg_type<int8_t> prefixBits) { | ||
|
||
std::string res; | ||
boost::asio::ip::address_v6::bytes_type addrBytes; | ||
memcpy(&addrBytes, &ip, 16); | ||
auto v6Addr = boost::asio::ip::make_address_v6(addrBytes); | ||
|
||
// Presto stores prefixBits in one byte. Cast to unsigned | ||
if(v6Addr.is_v4_mapped()){ | ||
auto v4Addr = boost::asio::ip::make_address_v4(boost::asio::ip::v4_mapped, v6Addr); | ||
auto v4Network = boost::asio::ip::make_network_v4(v4Addr, (uint8_t)prefixBits); | ||
res = boost::lexical_cast<std::string>(v4Network.canonical()); | ||
}else{ | ||
auto v6Network = boost::asio::ip::make_network_v6(v6Addr, (uint8_t)prefixBits); | ||
res = boost::lexical_cast<std::string>(v6Network.canonical()); | ||
} | ||
|
||
|
||
/* | ||
folly::ByteArray16 addrBytes; | ||
folly::IPAddressV6 addr(addrBytes); | ||
// Presto stores prefixBits in one byte. Cast to unsigned | ||
if(addr.isIPv4Mapped()){ | ||
res = addr.createIPv4().mask((uint8_t)prefixBits).str(); | ||
}else{ | ||
res = addr.mask((uint8_t)prefixBits).str(); | ||
}*/ | ||
|
||
|
||
|
||
result.reserve(res.length()); | ||
result.append(res); | ||
} | ||
}; | ||
|
||
|
||
inline void registerIPAddressFunctions(const std::string& prefix) { | ||
registerIPAddressType(); | ||
registerFunction<IPAddressFunction, IPAddress>({prefix + "ipaddress"}); | ||
registerFunction<IPPrefixFunction, Varchar, IPAddress, int8_t>({prefix + "ip_prefix"}); | ||
|
||
} | ||
|
||
} // namespace facebook::velox::functions |
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
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
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
91 changes: 91 additions & 0 deletions
91
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,91 @@ | ||
/* | ||
* 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 { | ||
|
||
namespace { | ||
|
||
class IPAddressTest : public functions::test::FunctionBaseTest { | ||
protected: | ||
std::optional<std::string> getIPPrefix(const std::optional<std::string> input, std::optional<int8_t> mask) { | ||
auto result = evaluateOnce<std::string>("ip_prefix(cast(c0 as ipaddress), c1)", input, mask); | ||
return result; | ||
} | ||
}; | ||
|
||
TEST_F(IPAddressTest, castAsVarchar) { | ||
auto result = evaluate<FlatVector<StringView>>( | ||
"cast(ipaddress() as varchar)", makeRowVector(ROW({}), 10)); | ||
|
||
// Sanity check results. All strings are unique. | ||
std::unordered_set<std::string> ipaddresses; | ||
for (auto i = 0; i < 10; ++i) { | ||
const auto ipaddress = result->valueAt(i).str(); | ||
ASSERT_TRUE(ipaddresses.insert(ipaddress).second); | ||
} | ||
ASSERT_EQ(10, ipaddresses.size()); | ||
} | ||
|
||
TEST_F(IPAddressTest, castRoundTrip) { | ||
auto strings = makeFlatVector<std::string>({ | ||
"87a0:ce14:8989:44c9:826e:b4d8:73f9:1542", | ||
"7cd6:bcec:1216:5c20:4b67:b1bd:173:ced", | ||
"192.128.0.0" | ||
}); | ||
|
||
auto ipaddresses = evaluate("cast(c0 as ipaddress)", makeRowVector({strings})); | ||
auto stringsCopy = evaluate("cast(c0 as varchar)", makeRowVector({ipaddresses})); | ||
auto ipaddressesCopy = evaluate("cast(c0 as ipaddress)", makeRowVector({stringsCopy})); | ||
|
||
velox::test::assertEqualVectors(strings, stringsCopy); | ||
velox::test::assertEqualVectors(ipaddresses, ipaddressesCopy); | ||
} | ||
|
||
TEST_F(IPAddressTest, IPPrefixv4) { | ||
|
||
EXPECT_EQ("10.0.0.0/8", getIPPrefix("10.135.23.12",8)); | ||
EXPECT_EQ("192.128.0.0/9", getIPPrefix("192.168.255.255", 9)); | ||
EXPECT_EQ("192.168.255.255/32", getIPPrefix("192.168.255.255",32)); | ||
EXPECT_EQ("0.0.0.0/0", getIPPrefix("192.168.255.255",0)); | ||
|
||
EXPECT_THROW(getIPPrefix("12.483.09.1",8), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("10.135.23.12.12",8), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("10.135.23",8), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("12.135.23.12",-1), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("10.135.23.12",33), VeloxUserError); | ||
} | ||
|
||
TEST_F(IPAddressTest, IPPrefixv6) { | ||
EXPECT_EQ("2001:db8:85a3::/48", getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370:7334", 48)); | ||
EXPECT_EQ("2001:db8:85a3::/52", getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370:7334", 52)); | ||
EXPECT_EQ("2001:db8:85a3:1:1:8a2e:370:7334/128", getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370:7334", 128)); | ||
EXPECT_EQ("::/0", getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370:7334", 0)); | ||
|
||
EXPECT_THROW(getIPPrefix("q001:0db8:85a3:0001:0001:8a2e:0370:7334", 8), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("2001:0db8:85a3:542e:0001:0001:8a2e:0370:7334", 8), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370", 8), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370:7334", -1), VeloxUserError); | ||
EXPECT_THROW(getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370:7334", 140), VeloxUserError); | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
} |
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
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
Oops, something went wrong.