Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsaka committed Jun 28, 2024
1 parent 6e11299 commit 334413d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
16 changes: 9 additions & 7 deletions velox/functions/prestosql/tests/IPAddressFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ namespace {

class IPAddressTest : public functions::test::FunctionBaseTest {
protected:

std::optional<std::string> castIPAddress(
std::optional<std::string> castIPAddress(
const std::optional<std::string> input) {
auto result = evaluateOnce<std::string>(
"cast(cast(c0 as ipaddress) as varchar)", input);
return result;
}

std::optional<std::string> castIPPrefix(
std::optional<std::string> castIPPrefix(
const std::optional<std::string> input) {
auto result = evaluateOnce<std::string>(
"cast(cast(c0 as ipprefix) as varchar)", input);
Expand Down Expand Up @@ -107,9 +106,13 @@ TEST_F(IPAddressTest, castFail) {
EXPECT_THROW(castIPAddress("12.483.09.1"), VeloxUserError);
EXPECT_THROW(castIPAddress("10.135.23.12.12"), VeloxUserError);
EXPECT_THROW(castIPAddress("10.135.23"), VeloxUserError);
EXPECT_THROW(castIPAddress("q001:0db8:85a3:0001:0001:8a2e:0370:7334"), VeloxUserError);
EXPECT_THROW(castIPAddress("2001:0db8:85a3:542e:0001:0001:8a2e:0370:7334"), VeloxUserError);
EXPECT_THROW(castIPAddress("2001:0db8:85a3:0001:0001:8a2e:0370"), VeloxUserError);
EXPECT_THROW(
castIPAddress("q001:0db8:85a3:0001:0001:8a2e:0370:7334"), VeloxUserError);
EXPECT_THROW(
castIPAddress("2001:0db8:85a3:542e:0001:0001:8a2e:0370:7334"),
VeloxUserError);
EXPECT_THROW(
castIPAddress("2001:0db8:85a3:0001:0001:8a2e:0370"), VeloxUserError);

EXPECT_THROW(castIPPrefix("12.135.23.12/-1"), VeloxUserError);
EXPECT_THROW(castIPPrefix("10.135.23.12/33"), VeloxUserError);
Expand All @@ -127,7 +130,6 @@ TEST_F(IPAddressTest, castFail) {
EXPECT_THROW(castIPPrefix("64::ff9b::10/24"), VeloxUserError);
EXPECT_THROW(castIPPrefix("64:face:book::10/24"), VeloxUserError);
EXPECT_THROW(castIPPrefix("123.456.789.012/24"), VeloxUserError);

}

TEST_F(IPAddressTest, castRoundTrip) {
Expand Down
17 changes: 10 additions & 7 deletions velox/functions/prestosql/types/IPPrefixType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ class IPPrefixCastOperator : public exec::CastOperator {
.to_bytes();
} else {
auto v6Net = boost::asio::ip::make_network_v6(ipAddressString);
if(addr.to_v6().is_v4_mapped()){
auto v4Addr = boost::asio::ip::make_address_v4(boost::asio::ip::v4_mapped, addr.to_v6());
auto v4Net = boost::asio::ip::make_network_v4(v4Addr, (uint8_t)v6Net.prefix_length());
addrBytes = boost::asio::ip::make_address_v6(
boost::asio::ip::v4_mapped, v4Net.canonical().address())
.to_bytes();
}else{
if (addr.to_v6().is_v4_mapped()) {
auto v4Addr = boost::asio::ip::make_address_v4(
boost::asio::ip::v4_mapped, addr.to_v6());
auto v4Net = boost::asio::ip::make_network_v4(
v4Addr, (uint8_t)v6Net.prefix_length());
addrBytes =
boost::asio::ip::make_address_v6(
boost::asio::ip::v4_mapped, v4Net.canonical().address())
.to_bytes();
} else {
addrBytes = v6Net.canonical().address().to_bytes();
}
res.prefix = (uint8_t)v6Net.prefix_length();
Expand Down

0 comments on commit 334413d

Please sign in to comment.