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 21a8d16 commit 03240bb
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 139 deletions.
95 changes: 44 additions & 51 deletions velox/functions/prestosql/IPAddressFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct IPAddressFunction {
boost::uuids::random_generator generator_;
};

inline bool isIPV4(int128_t ip){
inline bool isIPV4(int128_t ip) {
int128_t ipV4 = 0x0000FFFF00000000;
int128_t mask = 0xFFFFFFFFFFFFFFFF;
mask = (mask << 64) | 0xFFFFFFFF00000000;
Expand Down Expand Up @@ -93,28 +93,26 @@ struct IPPrefixFunction {
result = std::make_shared<IPPrefix>(canonicalAddrInt, prefix);
}

FOLLY_ALWAYS_INLINE void call(
FOLLY_ALWAYS_INLINE void call(
out_type<TheIPPrefix>& result,
const arg_type<Varchar>& ip,
const arg_type<int8_t> prefixBits) {

boost::asio::ip::address_v6::bytes_type addrBytes;
auto addr = boost::asio::ip::make_address(ip);
int128_t intAddr;
if (addr.is_v4()) {
addrBytes = boost::asio::ip::make_address_v6(
boost::asio::ip::v4_mapped, addr.to_v4())
.to_bytes();
} else {
addrBytes = addr.to_v6().to_bytes();
}
boost::asio::ip::address_v6::bytes_type addrBytes;
auto addr = boost::asio::ip::make_address(ip);
int128_t intAddr;
if (addr.is_v4()) {
addrBytes = boost::asio::ip::make_address_v6(
boost::asio::ip::v4_mapped, addr.to_v4())
.to_bytes();
} else {
addrBytes = addr.to_v6().to_bytes();
}

bigEndianByteArray(addrBytes);
memcpy(&intAddr, &addrBytes, 16);
bigEndianByteArray(addrBytes);
memcpy(&intAddr, &addrBytes, 16);

call(result, intAddr, prefixBits);

}
call(result, intAddr, prefixBits);
}
};

template <typename T>
Expand All @@ -129,26 +127,26 @@ struct IPSubnetMinFunction {
}
};

inline int128_t getIPSubnetMax(int128_t ip, uint8_t prefix){
// Presto stores prefixBits in one signed byte. Cast to unsigned
uint128_t mask = 1;
int128_t result;
boost::asio::ip::address_v6::bytes_type addrBytes;
memcpy(&result, &ip, 16);
inline int128_t getIPSubnetMax(int128_t ip, uint8_t prefix) {
// Presto stores prefixBits in one signed byte. Cast to unsigned
uint128_t mask = 1;
int128_t result;
boost::asio::ip::address_v6::bytes_type addrBytes;
memcpy(&result, &ip, 16);

if (isIPV4(ip)) {
if (prefix < 32) {
result |= (mask << 32 - prefix) - 1;
}
} else {
// Special case. Return all bits set to 1;
if (prefix == 0) {
result = -1;
} else if (prefix < 128) {
result |= (mask << 128 - prefix) - 1;
}
if (isIPV4(ip)) {
if (prefix < 32) {
result |= (mask << 32 - prefix) - 1;
}
return result;
} else {
// Special case. Return all bits set to 1;
if (prefix == 0) {
result = -1;
} else if (prefix < 128) {
result |= (mask << 128 - prefix) - 1;
}
}
return result;
}

template <typename T>
Expand All @@ -158,8 +156,7 @@ struct IPSubnetMaxFunction {
FOLLY_ALWAYS_INLINE void call(
out_type<IPAddress>& result,
const arg_type<TheIPPrefix>& ipPrefix) {

result = getIPSubnetMax(ipPrefix->ip, (uint8_t)ipPrefix->prefix);
result = getIPSubnetMax(ipPrefix->ip, (uint8_t)ipPrefix->prefix);
}
};

Expand All @@ -170,24 +167,22 @@ struct IPSubnetRangeFunction {
FOLLY_ALWAYS_INLINE void call(
out_type<Array<IPAddress>>& result,
const arg_type<TheIPPrefix>& ipPrefix) {

result.push_back(ipPrefix->ip);
result.push_back(getIPSubnetMax(ipPrefix->ip, (uint8_t)ipPrefix->prefix));
result.push_back(ipPrefix->ip);
result.push_back(getIPSubnetMax(ipPrefix->ip, (uint8_t)ipPrefix->prefix));
}
};

template <typename T>
struct IPSubnetOfFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);
FOLLY_ALWAYS_INLINE void call(
out_type<bool>& result,
const arg_type<TheIPPrefix>& ipPrefix,
const arg_type<IPAddress>& ip) {

out_type<bool>& result,
const arg_type<TheIPPrefix>& ipPrefix,
const arg_type<IPAddress>& ip) {
uint128_t mask = 1;
uint8_t prefix = (uint8_t)ipPrefix->prefix;
int128_t checkIP = ip;

if (isIPV4(ipPrefix->ip)) {
if (prefix < 32) {
checkIP &= ((mask << 32 - prefix) - 1) ^ -1;
Expand All @@ -201,13 +196,11 @@ struct IPSubnetOfFunction {
}

FOLLY_ALWAYS_INLINE void call(
out_type<bool>& result,
const arg_type<TheIPPrefix>& ipPrefix,
const arg_type<TheIPPrefix>& ipPrefix2) {

out_type<bool>& result,
const arg_type<TheIPPrefix>& ipPrefix,
const arg_type<TheIPPrefix>& ipPrefix2) {
call(result, ipPrefix, ipPrefix2->ip);
result = result && (ipPrefix2->prefix >= ipPrefix->prefix);

}
};

Expand Down
Loading

0 comments on commit 03240bb

Please sign in to comment.