Skip to content

Commit

Permalink
Format fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsaka committed Jun 26, 2024
1 parent a7f3336 commit 7ccae8b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
27 changes: 10 additions & 17 deletions velox/functions/prestosql/IPAddressFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ struct IPPrefixFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

// TODO: Template to varchar doesn't work for some reason
//template <typename TInput>
// template <typename TInput>
FOLLY_ALWAYS_INLINE void call(
out_type<TheIPPrefix>& result,
const arg_type<IPAddress>& ip,
const arg_type<int8_t> prefixBits) {

// Presto stores prefixBits in one signed byte. Cast to unsigned
uint8_t prefix = (uint8_t)prefixBits;
boost::asio::ip::address_v6::bytes_type addrBytes;
Expand All @@ -72,13 +71,11 @@ struct IPPrefixFunction {
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, prefix);
auto v4Network = boost::asio::ip::make_network_v4(v4Addr, prefix);
v6CanonicalAddr = boost::asio::ip::make_address_v6(
boost::asio::ip::v4_mapped, v4Network.canonical().address());
} else {
auto v6Network =
boost::asio::ip::make_network_v6(v6Addr, prefix);
auto v6Network = boost::asio::ip::make_network_v6(v6Addr, prefix);
v6CanonicalAddr = v6Network.canonical().address();
}

Expand All @@ -97,9 +94,8 @@ struct IPSubnetMinFunction {
FOLLY_ALWAYS_INLINE void call(
out_type<IPAddress>& result,
const arg_type<TheIPPrefix>& ipPrefix) {

// IPPrefix type should store the smallest(canonical) IP already
memcpy(&result, &ipPrefix->ip, 16);
// IPPrefix type should store the smallest(canonical) IP already
memcpy(&result, &ipPrefix->ip, 16);
}
};

Expand All @@ -110,7 +106,6 @@ struct IPSubnetMaxFunction {
FOLLY_ALWAYS_INLINE void call(
out_type<IPAddress>& result,
const arg_type<TheIPPrefix>& ipPrefix) {

// Presto stores prefixBits in one signed byte. Cast to unsigned
uint8_t prefix = (uint8_t)ipPrefix->prefix;
uint128_t mask = 1;
Expand All @@ -124,17 +119,16 @@ struct IPSubnetMaxFunction {

if (v6Addr.is_v4_mapped()) {
assert(prefix <= 32);
if(prefix < 32){
if (prefix < 32) {
result |= (mask << 32 - prefix) - 1;
}
} else {
assert(prefix <= 128);

// Special case. Return all bits set to 1;
if(prefix == 0){
if (prefix == 0) {
result = -1;
}
else if(prefix < 128){
} else if (prefix < 128) {
result |= (mask << 128 - prefix) - 1;
}
}
Expand All @@ -151,9 +145,8 @@ void registerIPAddressFunctions(const std::string& prefix) {
{prefix + "ip_subset_min"});
registerFunction<IPSubnetMaxFunction, IPAddress, TheIPPrefix>(
{prefix + "ip_subset_max"});
//registerFunction<IPPrefixFunction, TheIPPrefix, Varchar, int8_t>(
// {prefix + "ip_prefix"});

// registerFunction<IPPrefixFunction, TheIPPrefix, Varchar, int8_t>(
// {prefix + "ip_prefix"});
}

} // namespace facebook::velox::functions
52 changes: 32 additions & 20 deletions velox/functions/prestosql/tests/IPAddressFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class IPAddressTest : public functions::test::FunctionBaseTest {
return result;
}

std::optional<std::string> getIPPrefixUsingVarchar(
std::optional<std::string> getIPPrefixUsingVarchar(
const std::optional<std::string> input,
std::optional<int8_t> mask) {
auto result = evaluateOnce<std::string>(
"cast(ip_prefix(c0, c1) as varchar)", input, mask);
return result;
}

std::optional<std::string> getIPSubnetMin(
std::optional<std::string> getIPSubnetMin(
const std::optional<std::string> input) {
auto result = evaluateOnce<std::string>(
"cast(ip_subset_min(cast(c0 as ipprefix)) as varchar)", input);
Expand All @@ -54,7 +54,6 @@ class IPAddressTest : public functions::test::FunctionBaseTest {
}
};


TEST_F(IPAddressTest, castAsVarchar) {
auto result = evaluate<FlatVector<StringView>>(
"cast(ipaddress() as varchar)", makeRowVector(ROW({}), 10));
Expand All @@ -68,7 +67,6 @@ TEST_F(IPAddressTest, castAsVarchar) {
ASSERT_EQ(10, ipaddresses.size());
}


TEST_F(IPAddressTest, castRoundTrip) {
auto strings = makeFlatVector<std::string>(
{"87a0:ce14:8989:44c9:826e:b4d8:73f9:1542",
Expand All @@ -87,7 +85,7 @@ TEST_F(IPAddressTest, castRoundTrip) {
}

TEST_F(IPAddressTest, IPPrefixv4) {
//EXPECT_EQ("10.0.0.0/8", getIPPrefixUsingVarchar("10.135.23.12", 8));
// EXPECT_EQ("10.0.0.0/8", getIPPrefixUsingVarchar("10.135.23.12", 8));

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));
Expand All @@ -102,9 +100,10 @@ TEST_F(IPAddressTest, IPPrefixv4) {
}

TEST_F(IPAddressTest, IPPrefixv6) {
//EXPECT_EQ(
// "2001:db8:85a3::/48",
// getIPPrefixUsingVarchar("2001:0db8:85a3:0001:0001:8a2e:0370:7334", 48));
// EXPECT_EQ(
// "2001:db8:85a3::/48",
// getIPPrefixUsingVarchar("2001:0db8:85a3:0001:0001:8a2e:0370:7334",
// 48));
EXPECT_EQ(
"2001:db8:85a3::/48",
getIPPrefix("2001:0db8:85a3:0001:0001:8a2e:0370:7334", 48));
Expand Down Expand Up @@ -134,9 +133,7 @@ TEST_F(IPAddressTest, IPPrefixv6) {

TEST_F(IPAddressTest, castRoundTripPrefix) {
auto strings = makeFlatVector<std::string>(
{"87a0:ce14:8989::/48",
"7800::/5",
"192.0.0.0/5"});
{"87a0:ce14:8989::/48", "7800::/5", "192.0.0.0/5"});

auto ipprefixes = evaluate("cast(c0 as ipprefix)", makeRowVector({strings}));
auto stringsCopy =
Expand All @@ -154,11 +151,17 @@ TEST_F(IPAddressTest, IPSubsetMin) {
EXPECT_EQ("192.64.1.0", getIPSubnetMin("192.64.1.1/31"));
EXPECT_EQ("192.64.1.1", getIPSubnetMin("192.64.1.1/32"));

EXPECT_EQ("2001:db8:85a3::", getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/48"));
EXPECT_EQ(
"2001:db8:85a3::",
getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/48"));
EXPECT_EQ("::", getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/0"));
EXPECT_EQ("::", getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/1"));
EXPECT_EQ("2001:db8:85a3:1:1:8a2e:370:7334", getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/127"));
EXPECT_EQ("2001:db8:85a3:1:1:8a2e:370:7334", getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/128"));
EXPECT_EQ(
"2001:db8:85a3:1:1:8a2e:370:7334",
getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/127"));
EXPECT_EQ(
"2001:db8:85a3:1:1:8a2e:370:7334",
getIPSubnetMin("2001:0db8:85a3:0001:0001:8a2e:0370:7334/128"));
}

TEST_F(IPAddressTest, IPSubsetMax) {
Expand All @@ -168,12 +171,21 @@ TEST_F(IPAddressTest, IPSubsetMax) {
EXPECT_EQ("192.64.1.1", getIPSubnetMax("192.64.1.1/31"));
EXPECT_EQ("192.64.1.1", getIPSubnetMax("192.64.1.1/32"));

EXPECT_EQ("2001:db8:85a3:ffff:ffff:ffff:ffff:ffff", getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/48"));
EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/0"));
EXPECT_EQ("7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/1"));
EXPECT_EQ("2001:db8:85a3:1:1:8a2e:370:7335", getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/127"));
EXPECT_EQ("2001:db8:85a3:1:1:8a2e:370:7334", getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/128"));

EXPECT_EQ(
"2001:db8:85a3:ffff:ffff:ffff:ffff:ffff",
getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/48"));
EXPECT_EQ(
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/0"));
EXPECT_EQ(
"7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/1"));
EXPECT_EQ(
"2001:db8:85a3:1:1:8a2e:370:7335",
getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/127"));
EXPECT_EQ(
"2001:db8:85a3:1:1:8a2e:370:7334",
getIPSubnetMax("2001:0db8:85a3:0001:0001:8a2e:0370:7334/128"));
}

} // namespace
Expand Down
5 changes: 3 additions & 2 deletions velox/functions/prestosql/types/IPAddressType.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ namespace facebook::velox {

// Converts BigEndian <-> native byte array
// NOOP if system is Big Endian already
inline void bigEndianByteArray(boost::asio::ip::address_v6::bytes_type &addrBytes){
if(folly::kIsLittleEndian){
inline void bigEndianByteArray(
boost::asio::ip::address_v6::bytes_type& addrBytes) {
if (folly::kIsLittleEndian) {
std::reverse(addrBytes.begin(), addrBytes.end());
}
}
Expand Down
4 changes: 2 additions & 2 deletions velox/functions/prestosql/types/IPPrefixType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ class IPPrefixCastOperator : public exec::CastOperator {
res.prefix = (uint8_t)v6Net.prefix_length();
addrBytes = v6Net.canonical().address().to_bytes();
}

bigEndianByteArray(addrBytes);
memcpy(&res.ip, &addrBytes, 16);

flatResult->set(
row, std::make_shared<IPPrefix>(res.ip, (uint8_t)res.prefix));
});
Expand Down

0 comments on commit 7ccae8b

Please sign in to comment.