Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsaka committed Dec 10, 2024
1 parent 926ec64 commit abf9f72
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 17 deletions.
2 changes: 1 addition & 1 deletion velox/expression/FunctionSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include "velox/common/base/Exceptions.h"
#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"
#include "velox/type/Type.h"

namespace facebook::velox::exec {
Expand Down
4 changes: 2 additions & 2 deletions velox/functions/prestosql/IPAddressFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
#pragma once

#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"

namespace facebook::velox::functions {

Expand Down
4 changes: 2 additions & 2 deletions velox/functions/prestosql/TypeOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/
#include "velox/expression/VectorFunction.h"
#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/HyperLogLogType.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/functions/prestosql/types/JsonType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"
#include "velox/functions/prestosql/types/UuidType.h"
Expand Down
2 changes: 1 addition & 1 deletion velox/functions/prestosql/aggregates/PrestoHasher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <xxhash.h>

#include "velox/functions/lib/RowsTranslationUtil.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"

namespace facebook::velox::aggregate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <gtest/gtest.h>

#include "velox/functions/prestosql/aggregates/PrestoHasher.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"
#include "velox/type/tz/TimeZoneMap.h"
#include "velox/vector/tests/utils/VectorTestBase.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "velox/functions/Registerer.h"
#include "velox/functions/lib/RegistrationHelpers.h"
#include "velox/functions/prestosql/Comparisons.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"
#include "velox/type/Type.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "velox/functions/prestosql/GreatestLeast.h"
#include "velox/functions/prestosql/InPredicate.h"
#include "velox/functions/prestosql/Reduce.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"

namespace facebook::velox::functions {
Expand Down
2 changes: 1 addition & 1 deletion velox/functions/prestosql/tests/ComparisonsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "velox/functions/Udf.h"
#include "velox/functions/lib/RegistrationHelpers.h"
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"
#include "velox/type/tests/utils/CustomTypesForTesting.h"
#include "velox/type/tz/TimeZoneMap.h"
Expand Down
4 changes: 2 additions & 2 deletions velox/functions/prestosql/types/IPAddressType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/expression/CastExpr.h"
#include "velox/expression/VectorWriters.h"
#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"

namespace facebook::velox {

Expand Down
2 changes: 1 addition & 1 deletion velox/functions/prestosql/types/IPPrefixType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <folly/small_vector.h>

#include "velox/expression/CastExpr.h"
#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"

namespace facebook::velox {

Expand Down
159 changes: 159 additions & 0 deletions velox/functions/prestosql/types/headers/IPPrefixType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <folly/small_vector.h>

#include "velox/common/base/Status.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/type/SimpleFunctionApi.h"
#include "velox/type/Type.h"

namespace facebook::velox {

namespace ipaddress {
constexpr uint8_t kIPV4Bits = 32;
constexpr uint8_t kIPV6Bits = 128;
constexpr int kIPPrefixLengthIndex = 16;
constexpr int kIPPrefixBytes = 17;
constexpr auto kIpRowIndex = "ip";
constexpr auto kIpPrefixRowIndex = "prefix";

inline folly::Expected<int8_t, Status> tryIpPrefixLengthFromIPAddressType(
const int128_t& intIpAddr) {
folly::ByteArray16 addrBytes = {0};
memcpy(&addrBytes, &intIpAddr, sizeof(intIpAddr));
std::reverse(addrBytes.begin(), addrBytes.end());
auto tryV6Addr = folly::IPAddressV6::tryFromBinary(addrBytes);
if (tryV6Addr.hasError()) {
return folly::makeUnexpected(
threadSkipErrorDetails()
? Status::UserError()
: Status::UserError("Received invalid ip address"));
}

return tryV6Addr.value().isIPv4Mapped() ? ipaddress::kIPV4Bits
: ipaddress::kIPV6Bits;
}

inline folly::Expected<std::pair<int128_t, int8_t>, Status>
tryParseIpPrefixString(folly::StringPiece ipprefixString) {
// Ensure '/' is present
if (ipprefixString.find('/') == std::string::npos) {
return folly::makeUnexpected(
threadSkipErrorDetails()
? Status::UserError()
: Status::UserError(
"Cannot cast value to IPPREFIX: {}", ipprefixString));
}

auto tryCdirNetwork = folly::IPAddress::tryCreateNetwork(
ipprefixString, /*defaultCidr*/ -1, /*applyMask*/ false);

if (tryCdirNetwork.hasError()) {
return folly::makeUnexpected(
threadSkipErrorDetails()
? Status::UserError()
: Status::UserError(
"Cannot cast value to IPPREFIX: {}", ipprefixString));
}

auto [ip, prefix] = tryCdirNetwork.value();
if (prefix > ((ip.isIPv4Mapped() || ip.isV4()) ? ipaddress::kIPV4Bits
: ipaddress::kIPV6Bits)) {
return folly::makeUnexpected(
threadSkipErrorDetails()
? Status::UserError()
: Status::UserError(
"Cannot cast value to IPPREFIX: {}", ipprefixString));
}

auto addrBytes = (ip.isIPv4Mapped() || ip.isV4())
? folly::IPAddress::createIPv4(ip).mask(prefix).createIPv6().toByteArray()
: folly::IPAddress::createIPv6(ip).mask(prefix).toByteArray();

std::reverse(addrBytes.begin(), addrBytes.end());

int128_t intAddr = 0;
static_assert(sizeof(intAddr) == ipaddress::kIPAddressBytes);
memcpy(&intAddr, &addrBytes, ipaddress::kIPAddressBytes);
return std::make_pair(intAddr, prefix);
}
}; // namespace ipaddress

class IPPrefixType : public RowType {
IPPrefixType()
: RowType(
{ipaddress::kIpRowIndex, ipaddress::kIpPrefixRowIndex},
{HUGEINT(), TINYINT()}) {}

public:
static const std::shared_ptr<const IPPrefixType>& get() {
static const std::shared_ptr<const IPPrefixType> instance{
new IPPrefixType()};

return instance;
}

bool equivalent(const Type& other) const override {
// Pointer comparison works since this type is a singleton.
return this == &other;
}

const char* name() const override {
return "IPPREFIX";
}

std::string toString() const override {
return name();
}

folly::dynamic serialize() const override {
folly::dynamic obj = folly::dynamic::object;
obj["name"] = "Type";
obj["type"] = name();
return obj;
}

const std::vector<TypeParameter>& parameters() const override {
static const std::vector<TypeParameter> kEmpty = {};
return kEmpty;
}
};

FOLLY_ALWAYS_INLINE bool isIPPrefixType(const TypePtr& type) {
// Pointer comparison works since this type is a singleton.
return IPPrefixType::get() == type;
}

FOLLY_ALWAYS_INLINE bool isIPPrefixName(const std::string& name) {
return (name == IPPrefixType::get()->name());
}

FOLLY_ALWAYS_INLINE std::shared_ptr<const IPPrefixType> IPPREFIX() {
return IPPrefixType::get();
}

struct IPPrefixT {
using type = Row<int128_t, int8_t>;
static constexpr const char* typeName = "ipprefix";
};

using IPPrefix = CustomType<IPPrefixT>;

void registerIPPrefixType();

} // namespace facebook::velox
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/headers/IPAddressType.h"
#include "velox/functions/prestosql/types/tests/TypeTestBase.h"

namespace facebook::velox::test {
Expand Down
2 changes: 1 addition & 1 deletion velox/functions/prestosql/types/tests/IPPrefixTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/tests/TypeTestBase.h"

namespace facebook::velox::test {
Expand Down
2 changes: 1 addition & 1 deletion velox/functions/tests/FunctionRegistryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "velox/functions/Registerer.h"
#include "velox/functions/prestosql/registration/RegistrationFunctions.h"
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"
#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"
#include "velox/functions/tests/RegistryTestUtil.h"
#include "velox/type/Type.h"

Expand Down
2 changes: 1 addition & 1 deletion velox/functions/tests/RegistryTestUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "velox/expression/FunctionSignature.h"
#include "velox/expression/VectorFunction.h"
#include "velox/functions/Macros.h"
#include "velox/functions/prestosql/type_headers/IPPrefixType.h"
#include "velox/functions/prestosql/types/headers/IPPrefixType.h"

namespace facebook::velox {

Expand Down

0 comments on commit abf9f72

Please sign in to comment.