Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsaka committed Aug 21, 2024
1 parent 97f1486 commit a68e69a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 62 deletions.
40 changes: 14 additions & 26 deletions velox/functions/prestosql/tests/IPPrefixCastTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,23 @@ TEST_F(IPPrefixCastTest, varcharCast) {
EXPECT_EQ(
castToVarchar("2001:db8::ff00:42:8329/128"),
"2001:db8::ff00:42:8329/128");
EXPECT_EQ(
castToVarchar("2001:db8:0:0:1:0:0:1/128"),
"2001:db8::1:0:0:1/128");
EXPECT_EQ(
castToVarchar("2001:db8:0:0:1::1/128"),
"2001:db8::1:0:0:1/128");
EXPECT_EQ(
castToVarchar("2001:db8::1:0:0:1/128"),
"2001:db8::1:0:0:1/128");
EXPECT_EQ(castToVarchar("2001:db8:0:0:1:0:0:1/128"), "2001:db8::1:0:0:1/128");
EXPECT_EQ(castToVarchar("2001:db8:0:0:1::1/128"), "2001:db8::1:0:0:1/128");
EXPECT_EQ(castToVarchar("2001:db8::1:0:0:1/128"), "2001:db8::1:0:0:1/128");
EXPECT_EQ(
castToVarchar("2001:DB8::FF00:ABCD:12EF/128"),
"2001:db8::ff00:abcd:12ef/128");
EXPECT_EQ(castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"), "::/0");
EXPECT_EQ(
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"),
"::/0");
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/1"), "8000::/1");
EXPECT_EQ(
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/1"),
"8000::/1");
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/2"), "c000::/2");
EXPECT_EQ(
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/2"),
"c000::/2");
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/4"), "f000::/4");
EXPECT_EQ(
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/4"),
"f000::/4");
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/8"), "ff00::/8");
EXPECT_EQ(
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/8"),
"ff00::/8");
EXPECT_EQ(
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/16"),
"ffff::/16");
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/16"), "ffff::/16");
EXPECT_EQ(
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/32"),
"ffff:ffff::/32");
Expand Down Expand Up @@ -138,8 +125,7 @@ TEST_F(IPPrefixCastTest, varcharCast) {
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
EXPECT_EQ(castToVarchar("10.0.0.0/32"), "10.0.0.0/32");
EXPECT_EQ(
castToVarchar("64:ff9b::10.0.0.0/128"), "64:ff9b::a00:0/128");
EXPECT_EQ(castToVarchar("64:ff9b::10.0.0.0/128"), "64:ff9b::a00:0/128");
EXPECT_THROW(castToVarchar("facebook.com/32"), VeloxUserError);
EXPECT_THROW(castToVarchar("localhost/32"), VeloxUserError);
EXPECT_THROW(castToVarchar("2001:db8::1::1/128"), VeloxUserError);
Expand Down Expand Up @@ -167,9 +153,11 @@ TEST_F(IPPrefixCastTest, toIPAddressCast) {
castToIPAddress("2001:db8::ff00:42:8329/128"),
stringToInt128("42540766411282592856904265327123268393"));
EXPECT_EQ(
castToIPAddress("2001:db8::ff00:42:8329/64"), stringToInt128("42540766411282592856903984951653826560"));
castToIPAddress("2001:db8::ff00:42:8329/64"),
stringToInt128("42540766411282592856903984951653826560"));
}

} // namespace

} // namespace facebook::velox::functions::prestosql
} // namespace facebook::velox::functions::prestosql

2 changes: 1 addition & 1 deletion velox/functions/prestosql/types/IPAddressType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

#include "velox/functions/prestosql/types/IPAddressType.h"
#include <folly/IPAddress.h>
#include "velox/expression/CastExpr.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/IPPrefixType.h"

static constexpr int kIPV4AddressBytes = 4;
Expand Down
93 changes: 60 additions & 33 deletions velox/functions/prestosql/types/IPPrefixType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
* limitations under the License.
*/

#include "velox/functions/prestosql/types/IPPrefixType.h"
#include <folly/IPAddress.h>
#include <folly/small_vector.h>
#include "velox/expression/CastExpr.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/IPPrefixType.h"

static constexpr int kIPAddressBytes = 16;
static constexpr int kIPPrefixBytes = 17;
static constexpr int kIPV4Bits = 32;
static constexpr int kIPV6Bits = 128;


namespace facebook::velox {

namespace {
Expand Down Expand Up @@ -74,7 +73,8 @@ class IPPrefixCastOperator : public exec::CastOperator {
castFromIPAddress(input, context, rows, *result);
} else {
VELOX_UNSUPPORTED(
"Cast from {} to IPPrefix not yet supported", input.type()->toString());
"Cast from {} to IPPrefix not yet supported",
input.type()->toString());
}
}

Expand Down Expand Up @@ -115,9 +115,13 @@ class IPPrefixCastOperator : public exec::CastOperator {

exec::StringWriter<false> result(flatResult, row);
if (v6Addr.isIPv4Mapped()) {
result.append(v6Addr.createIPv4().str() + "/" + std::to_string((uint8_t)intAddr.data()[kIPAddressBytes]));
result.append(
v6Addr.createIPv4().str() + "/" +
std::to_string((uint8_t)intAddr.data()[kIPAddressBytes]));
} else {
result.append(v6Addr.str() + "/" + std::to_string((uint8_t)intAddr.data()[kIPAddressBytes]));
result.append(
v6Addr.str() + "/" +
std::to_string((uint8_t)intAddr.data()[kIPAddressBytes]));
}
result.finalize();
});
Expand All @@ -140,29 +144,39 @@ class IPPrefixCastOperator : public exec::CastOperator {
context.applyToSelectedNoThrow(rows, [&](auto row) {
auto ipAddressString = ipAddressStrings->valueAt(row);

// Folly allows for pure IPADDRESS without prefix. Return an error in this case.
// Folly allows for pure IPADDRESS without prefix. Return an error in this
// case.
if (ipAddressString.str().find('/') == std::string::npos) {
if (threadSkipErrorDetails()) {
context.setStatus(row, Status::UserError());
} else{
context.setStatus(row, Status::UserError("Invalid ipSlashCidr specified. Expected IP/CIDR format, got '{}'",
ipAddressString.str()));
} else {
context.setStatus(
row,
Status::UserError(
"Invalid ipSlashCidr specified. Expected IP/CIDR format, got '{}'",
ipAddressString.str()));
}
return;
}

auto const maybeNet = folly::IPAddress::tryCreateNetwork(ipAddressString, -1, false);
auto const maybeNet =
folly::IPAddress::tryCreateNetwork(ipAddressString, -1, false);
uint8_t prefix = 0;
folly::ByteArray16 addrBytes;

if(maybeNet.hasValue()){
if (maybeNet.hasValue()) {
auto net = maybeNet.value();
if (net.first.isIPv4Mapped() || net.first.isV4()) {
if (net.second > folly::IPAddressV4::bitCount()){
if (net.second > folly::IPAddressV4::bitCount()) {
if (threadSkipErrorDetails()) {
context.setStatus(row, Status::UserError());
} else{
context.setStatus(row, Status::UserError("numBits({}) > bitsCount({})", net.second, folly::IPAddressV4::bitCount()));
} else {
context.setStatus(
row,
Status::UserError(
"numBits({}) > bitsCount({})",
net.second,
folly::IPAddressV4::bitCount()));
}
return;
}
Expand All @@ -172,11 +186,16 @@ class IPPrefixCastOperator : public exec::CastOperator {
.createIPv6()
.toByteArray();
} else {
if (net.second > folly::IPAddressV6::bitCount()){
if (net.second > folly::IPAddressV6::bitCount()) {
if (threadSkipErrorDetails()) {
context.setStatus(row, Status::UserError());
} else{
context.setStatus(row, Status::UserError("numBits({}) > bitsCount({})", net.second, folly::IPAddressV6::bitCount()));
} else {
context.setStatus(
row,
Status::UserError(
"numBits({}) > bitsCount({})",
net.second,
folly::IPAddressV6::bitCount()));
}
return;
}
Expand All @@ -198,17 +217,17 @@ class IPPrefixCastOperator : public exec::CastOperator {
context.setStatus(row, Status::UserError());
return;
}
if(maybeNet.error() == folly::CIDRNetworkError::INVALID_DEFAULT_CIDR){
if (maybeNet.error() == folly::CIDRNetworkError::INVALID_DEFAULT_CIDR) {
context.setStatus(
row,
Status::UserError("defaultCidr must be <= UINT8_MAX"));
row, Status::UserError("defaultCidr must be <= UINT8_MAX"));
return;
}
if (maybeNet.error() == folly::CIDRNetworkError::INVALID_IP_SLASH_CIDR) {
context.setStatus(
row,
Status::UserError("Invalid ipSlashCidr specified. Expected IP/CIDR format, got '{}'",
ipAddressString.str()));
row,
Status::UserError(
"Invalid ipSlashCidr specified. Expected IP/CIDR format, got '{}'",
ipAddressString.str()));
return;
}

Expand All @@ -219,21 +238,28 @@ class IPPrefixCastOperator : public exec::CastOperator {
switch (maybeNet.error()) {
case folly::CIDRNetworkError::INVALID_IP:
context.setStatus(
row,
Status::UserError("Invalid IP address {}", vec.size() > 0 ? vec.at(0) : ""));
row,
Status::UserError(
"Invalid IP address {}", vec.size() > 0 ? vec.at(0) : ""));
break;
case folly::CIDRNetworkError::INVALID_CIDR:
context.setStatus(
row,
Status::UserError("Mask value '{}' not a valid mask", vec.size() > 1 ? vec.at(1) : ""));
row,
Status::UserError(
"Mask value '{}' not a valid mask",
vec.size() > 1 ? vec.at(1) : ""));
break;
case folly::CIDRNetworkError::CIDR_MISMATCH: {
auto const subnet = folly::IPAddress::tryFromString(vec.at(0)).value();
auto const subnet =
folly::IPAddress::tryFromString(vec.at(0)).value();
context.setStatus(
row,
Status::UserError( "CIDR value '{}' is > network bit count '{}'",
vec.size() == 2 ? vec.at(1) : folly::to<std::string>(subnet.isV4() ? 32 : 128),
subnet.bitCount()));
row,
Status::UserError(
"CIDR value '{}' is > network bit count '{}'",
vec.size() == 2
? vec.at(1)
: folly::to<std::string>(subnet.isV4() ? 32 : 128),
subnet.bitCount()));
break;
}
default:
Expand Down Expand Up @@ -312,4 +338,5 @@ void registerIPPrefixType() {
"ipprefix", std::make_unique<const IPPrefixTypeFactories>());
}

} // namespace facebook::velox
} // namespace facebook::velox

3 changes: 2 additions & 1 deletion velox/functions/prestosql/types/IPPrefixType.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ using IPPrefix = CustomType<IPPrefixT>;

void registerIPPrefixType();

} // namespace facebook::velox
} // namespace facebook::velox

3 changes: 2 additions & 1 deletion velox/functions/prestosql/types/tests/IPPrefixTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ TEST_F(IPPrefixTypeTest, basic) {
TEST_F(IPPrefixTypeTest, serde) {
testTypeSerde(IPPREFIX());
}
} // namespace facebook::velox::test
} // namespace facebook::velox::test

0 comments on commit a68e69a

Please sign in to comment.