Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsaka committed Aug 2, 2024
1 parent d1fdbef commit f4957db
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 49 deletions.
106 changes: 79 additions & 27 deletions velox/functions/prestosql/tests/IPAddressFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,78 @@ TEST_F(IPAddressTest, testVarcharIpPrefixCast) {
EXPECT_EQ(castIPPrefixVarcharCycle("1.2.3.255/28"), "1.2.3.240/28");
EXPECT_EQ(castIPPrefixVarcharCycle("1.2.3.255/30"), "1.2.3.252/30");
EXPECT_EQ(castIPPrefixVarcharCycle("1.2.3.255/32"), "1.2.3.255/32");
EXPECT_EQ(castIPPrefixVarcharCycle("2001:0db8:0000:0000:0000:ff00:0042:8329/128"), "2001:db8::ff00:42:8329/128");
EXPECT_EQ(castIPPrefixVarcharCycle("2001:db8::ff00:42:8329/128"), "2001:db8::ff00:42:8329/128");
EXPECT_EQ(castIPPrefixVarcharCycle("2001:db8:0:0:1:0:0:1/128"), "2001:db8::1:0:0:1/128");
EXPECT_EQ(castIPPrefixVarcharCycle("2001:db8:0:0:1::1/128"), "2001:db8::1:0:0:1/128");
EXPECT_EQ(castIPPrefixVarcharCycle("2001:db8::1:0:0:1/128"), "2001:db8::1:0:0:1/128");
EXPECT_EQ(castIPPrefixVarcharCycle("2001:DB8::FF00:ABCD:12EF/128"), "2001:db8::ff00:abcd:12ef/128");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"), "::/0");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/1"), "8000::/1");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/2"), "c000::/2");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/4"), "f000::/4");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/8"), "ff00::/8");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/16"), "ffff::/16");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/32"), "ffff:ffff::/32");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/48"), "ffff:ffff:ffff::/48");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"), "ffff:ffff:ffff:ffff::/64");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/80"), "ffff:ffff:ffff:ffff:ffff::/80");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/96"), "ffff:ffff:ffff:ffff:ffff:ffff::/96");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/112"), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/120"), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00/120");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/124"), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0/124");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/126"), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc/126");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/127"), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe/127");
EXPECT_EQ(castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("2001:0db8:0000:0000:0000:ff00:0042:8329/128"),
"2001:db8::ff00:42:8329/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("2001:db8::ff00:42:8329/128"),
"2001:db8::ff00:42:8329/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("2001:db8:0:0:1:0:0:1/128"),
"2001:db8::1:0:0:1/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("2001:db8:0:0:1::1/128"),
"2001:db8::1:0:0:1/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("2001:db8::1:0:0:1/128"),
"2001:db8::1:0:0:1/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("2001:DB8::FF00:ABCD:12EF/128"),
"2001:db8::ff00:abcd:12ef/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"),
"::/0");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/1"),
"8000::/1");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/2"),
"c000::/2");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/4"),
"f000::/4");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/8"),
"ff00::/8");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/16"),
"ffff::/16");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/32"),
"ffff:ffff::/32");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/48"),
"ffff:ffff:ffff::/48");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"),
"ffff:ffff:ffff:ffff::/64");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/80"),
"ffff:ffff:ffff:ffff:ffff::/80");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/96"),
"ffff:ffff:ffff:ffff:ffff:ffff::/96");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/112"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/120"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00/120");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/124"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0/124");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/126"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc/126");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/127"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe/127");
EXPECT_EQ(
castIPPrefixVarcharCycle("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
EXPECT_EQ(castIPPrefixVarcharCycle("10.0.0.0/32"), "10.0.0.0/32");
EXPECT_EQ(castIPPrefixVarcharCycle("64:ff9b::10.0.0.0/128"), "64:ff9b::a00:0/128");
EXPECT_EQ(
castIPPrefixVarcharCycle("64:ff9b::10.0.0.0/128"), "64:ff9b::a00:0/128");
EXPECT_THROW(castIPPrefixVarcharCycle("facebook.com/32"), VeloxUserError);
EXPECT_THROW(castIPPrefixVarcharCycle("localhost/32"), VeloxUserError);
EXPECT_THROW(castIPPrefixVarcharCycle("2001:db8::1::1/128"), VeloxUserError);
Expand All @@ -189,15 +236,20 @@ TEST_F(IPAddressTest, testCastIPAddressIPPrefixVarchar) {
EXPECT_EQ(castIPAddressIPPrefixVarchar("1.2.3.4"), "1.2.3.4/32");
EXPECT_EQ(castIPAddressIPPrefixVarchar("::ffff:102:304"), "1.2.3.4/32");
EXPECT_EQ(castIPAddressIPPrefixVarchar("::1"), "::1/128");
EXPECT_EQ(castIPAddressIPPrefixVarchar("2001:db8::ff00:42:8329"), "2001:db8::ff00:42:8329/128");
EXPECT_EQ(
castIPAddressIPPrefixVarchar("2001:db8::ff00:42:8329"),
"2001:db8::ff00:42:8329/128");
}

TEST_F(IPAddressTest, testCastIPPrefixIPAddressVarchar) {
EXPECT_EQ(castIPPrefixIPAddressVarchar("1.2.3.4/32"), "1.2.3.4");
EXPECT_EQ(castIPPrefixIPAddressVarchar("1.2.3.4/24"), "1.2.3.0");
EXPECT_EQ(castIPPrefixIPAddressVarchar("::1/128"), "::1");
EXPECT_EQ(castIPPrefixIPAddressVarchar("2001:db8::ff00:42:8329/128"), "2001:db8::ff00:42:8329");
EXPECT_EQ(castIPPrefixIPAddressVarchar("2001:db8::ff00:42:8329/64"), "2001:db8::");
EXPECT_EQ(
castIPPrefixIPAddressVarchar("2001:db8::ff00:42:8329/128"),
"2001:db8::ff00:42:8329");
EXPECT_EQ(
castIPPrefixIPAddressVarchar("2001:db8::ff00:42:8329/64"), "2001:db8::");
}

} // namespace
Expand Down
14 changes: 7 additions & 7 deletions velox/functions/prestosql/types/IPAddressType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class IPAddressCastOperator : public exec::CastOperator {
if (input.typeKind() == TypeKind::VARCHAR) {
castFromString(input, context, rows, *result);
} else if (input.typeKind() == TypeKind::VARBINARY) {
if(isIPPrefixType(input.type())){
if (isIPPrefixType(input.type())) {
castFromIPPrefix(input, context, rows, *result);
} else {
castFromVarbinary(input, context, rows, *result);
Expand All @@ -78,7 +78,7 @@ class IPAddressCastOperator : public exec::CastOperator {
if (resultType->kind() == TypeKind::VARCHAR) {
castToString(input, context, rows, *result);
} else if (resultType->kind() == TypeKind::VARBINARY) {
if(isIPPrefixType(resultType)){
if (isIPPrefixType(resultType)) {
castToIPPrefix(input, context, rows, *result);
}
castToVarbinary(input, context, rows, *result);
Expand Down Expand Up @@ -201,10 +201,10 @@ class IPAddressCastOperator : public exec::CastOperator {
}

static void castFromIPPrefix(
const BaseVector& input,
exec::EvalCtx& context,
const SelectivityVector& rows,
BaseVector& result) {
const BaseVector& input,
exec::EvalCtx& context,
const SelectivityVector& rows,
BaseVector& result) {
auto* flatResult = result.as<FlatVector<int128_t>>();
const auto* ipaddresses = input.as<SimpleVector<StringView>>();

Expand All @@ -221,7 +221,7 @@ class IPAddressCastOperator : public exec::CastOperator {
});
}

static void castToIPPrefix(
static void castToIPPrefix(
const BaseVector& input,
exec::EvalCtx& context,
const SelectivityVector& rows,
Expand Down
28 changes: 13 additions & 15 deletions velox/functions/prestosql/types/IPPrefixType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ namespace {
class IPPrefixCastOperator : public exec::CastOperator {
public:
bool isSupportedFromType(const TypePtr& other) const override {
switch (other->kind()) {
switch (other->kind()) {
case TypeKind::VARCHAR:
return true;
case TypeKind::HUGEINT:
if (isIPAddressType(other)){
if (isIPAddressType(other)) {
return true;
}
return false;
Expand All @@ -41,7 +41,7 @@ class IPPrefixCastOperator : public exec::CastOperator {
case TypeKind::VARCHAR:
return true;
case TypeKind::HUGEINT:
if (isIPAddressType(other)){
if (isIPAddressType(other)) {
return true;
}
return false;
Expand Down Expand Up @@ -125,10 +125,8 @@ class IPPrefixCastOperator : public exec::CastOperator {

context.applyToSelectedNoThrow(rows, [&](auto row) {
auto ipAddressString = ipAddressStrings->valueAt(row);
if( ipAddressString.str().find('/') == std::string::npos) {
context.setStatus(
row,
Status::UserError("String missing '/'"));
if (ipAddressString.str().find('/') == std::string::npos) {
context.setStatus(row, Status::UserError("String missing '/'"));
}
folly::CIDRNetwork net =
folly::IPAddress::createNetwork(ipAddressString, -1, false);
Expand All @@ -155,10 +153,10 @@ class IPPrefixCastOperator : public exec::CastOperator {
}

static void castToIPAddress(
const BaseVector& input,
exec::EvalCtx& context,
const SelectivityVector& rows,
BaseVector& result) {
const BaseVector& input,
exec::EvalCtx& context,
const SelectivityVector& rows,
BaseVector& result) {
auto* flatResult = result.as<FlatVector<int128_t>>();
const auto* ipaddresses = input.as<SimpleVector<StringView>>();

Expand All @@ -176,10 +174,10 @@ class IPPrefixCastOperator : public exec::CastOperator {
}

static void castFromIPAddress(
const BaseVector& input,
exec::EvalCtx& context,
const SelectivityVector& rows,
BaseVector& result) {
const BaseVector& input,
exec::EvalCtx& context,
const SelectivityVector& rows,
BaseVector& result) {
auto* flatResult = result.as<FlatVector<StringView>>();
const auto* ipAddresses = input.as<SimpleVector<int128_t>>();

Expand Down

0 comments on commit f4957db

Please sign in to comment.