Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions velox/functions/remote/client/tests/RemoteFunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ class RemoteFunctionTest
registerFunction<OpaqueTypeFunction, int64_t, std::shared_ptr<Foo>>(
{params.functionPrefix + ".remote_opaque"});

registerOpaqueType<Foo>("Foo");
OpaqueType::registerSerialization<Foo>(
"Foo", Foo::serialize, Foo::deserialize);
registerOpaqueType<Foo>("Foo", Foo::serialize, Foo::deserialize);
}
};

Expand Down
19 changes: 17 additions & 2 deletions velox/type/OpaqueCustomTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ class CastOperator;
template <typename T, const char* customTypeName>
class OpaqueCustomTypeRegister {
public:
static bool registerType() {
return facebook::velox::registerCustomType(
static bool registerType(
OpaqueType::SerializeFunc<T> serialize = nullptr,
OpaqueType::DeserializeFunc<T> deserialize = nullptr) {
bool success = facebook::velox::registerCustomType(
customTypeName, std::make_unique<const TypeFactory>());
if (success) {
OpaqueType::registerSerialization<T>(
customTypeName, serialize, deserialize);
}
return success;
}

static bool unregisterType() {
OpaqueType::unregisterSerialization<T>(customTypeName);
return facebook::velox::unregisterCustomType(customTypeName);
}

Expand Down Expand Up @@ -77,6 +85,13 @@ class OpaqueCustomTypeRegister {
std::string toString() const override {
return customTypeName;
}

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

static const TypePtr& singletonTypePtr() {
Expand Down
8 changes: 8 additions & 0 deletions velox/type/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,14 @@ void OpaqueType::registerSerializationTypeErased(
registry.reverse[persistentName] = type;
}

void OpaqueType::unregisterSerializationTypeErased(
const std::shared_ptr<const OpaqueType>& type,
const std::string& persistentName) {
auto& registry = OpaqueSerdeRegistry::get();
registry.mapping.erase(type->typeIndex_);
registry.reverse.erase(persistentName);
}

ArrayTypePtr ARRAY(TypePtr elementType) {
return TypeFactory<TypeKind::ARRAY>::create(std::move(elementType));
}
Expand Down
26 changes: 24 additions & 2 deletions velox/type/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,12 @@ class OpaqueType : public TypeBase<TypeKind::OPAQUE> {

static void clearSerializationRegistry();

template <typename T>
FOLLY_NOINLINE static void unregisterSerialization(
const std::string& persistentName) {
unregisterSerializationTypeErased(OpaqueType::create<T>(), persistentName);
}

protected:
bool equals(const Type& other) const override;

Expand All @@ -1364,6 +1370,10 @@ class OpaqueType : public TypeBase<TypeKind::OPAQUE> {
const std::string& persistentName,
SerializeFunc<void> serialize = nullptr,
DeserializeFunc<void> deserialize = nullptr);

static void unregisterSerializationTypeErased(
const std::shared_ptr<const OpaqueType>& type,
const std::string& persistentName);
};

using IntegerType = ScalarType<TypeKind::INTEGER>;
Expand Down Expand Up @@ -2342,11 +2352,22 @@ std::string getOpaqueAliasForTypeId(std::type_index typeIndex);
/// might not be able to deserialize it in another process. To solve this
/// problem, we require that both the serializing and deserializing processes
/// register the opaque type using registerOpaqueType() with the same alias.
///
/// This function also registers the serialization for the opaque type. If
/// custom serialize/deserialize functions are provided, they will be used;
/// otherwise, default (nullptr) will be registered.
template <typename Class>
bool registerOpaqueType(const std::string& alias) {
bool registerOpaqueType(
const std::string& alias,
OpaqueType::SerializeFunc<Class> serialize = nullptr,
OpaqueType::DeserializeFunc<Class> deserialize = nullptr) {
auto typeIndex = std::type_index(typeid(Class));
return getTypeIndexByOpaqueAlias().emplace(alias, typeIndex).second &&
bool success = getTypeIndexByOpaqueAlias().emplace(alias, typeIndex).second &&
getOpaqueAliasByTypeIndex().emplace(typeIndex, alias).second;
if (success) {
OpaqueType::registerSerialization<Class>(alias, serialize, deserialize);
}
return success;
}

/// Unregisters an opaque type. Returns true if the type was unregistered.
Expand All @@ -2355,6 +2376,7 @@ bool registerOpaqueType(const std::string& alias) {
template <typename Class>
bool unregisterOpaqueType(const std::string& alias) {
auto typeIndex = std::type_index(typeid(Class));
OpaqueType::unregisterSerialization<Class>(alias);
return getTypeIndexByOpaqueAlias().erase(alias) == 1 &&
getOpaqueAliasByTypeIndex().erase(typeIndex) == 1;
}
Expand Down
Loading