diff --git a/docs/source/api/catalog.rst b/docs/source/api/catalog.rst index df247f4a8..43ea50b4b 100644 --- a/docs/source/api/catalog.rst +++ b/docs/source/api/catalog.rst @@ -27,6 +27,9 @@ Interface .. doxygenclass:: paimon::Catalog :members: +.. doxygenclass:: paimon::CatalogFactory + :members: + .. doxygenclass:: paimon::Identifier :members: :undoc-members: diff --git a/docs/source/user_guide/catalog.rst b/docs/source/user_guide/catalog.rst index d9ee91f66..747f40f50 100644 --- a/docs/source/user_guide/catalog.rst +++ b/docs/source/user_guide/catalog.rst @@ -23,8 +23,10 @@ Paimon C++ provides a :ref:`Catalog abstraction ` to manage the abstraction provides a series of ways to help you better integrate with computing engines. We always recommend that you use Catalog to access the Paimon table. -Paimon C++ supports two metastores, selected with the catalog option -``metastore``: the filesystem metastore (default) and the REST metastore. +Paimon C++ supports two built-in metastores, selected with the catalog option +``metastore``: the filesystem metastore (default) and the REST metastore. External +engines can also register custom ``CatalogFactory`` implementations to plug in +additional metastores. Filesystem Catalog ~~~~~~~~~~~~~~~~~~ @@ -759,3 +761,37 @@ bucket-scoped variants the fresh credentials replace. All factories share one identifier space, so an identifier a file system factory already takes — ``oss``, ``s3``, ``local``, ``jindo`` — would replace it; name the provider after where its credentials come from instead. + +Custom Catalog +~~~~~~~~~~~~~~ +If an engine manages catalog metadata outside the built-in ``filesystem`` and ``rest`` +metastores, implement ``CatalogFactory`` and register it with +``REGISTER_PAIMON_FACTORY``. ``Catalog::Create`` lowercases ``CatalogOptions::METASTORE`` +before looking up the factory, so register a lowercase identifier. + +.. code-block:: cpp + + class MyCatalogFactory : public paimon::CatalogFactory { + public: + const char* Identifier() const override { + return "my-catalog"; + } + + paimon::Result> Create( + const std::string& root_path, const std::map& options, + const std::shared_ptr& file_system, + const std::map& fs_scheme_to_identifier_map) const override { + return std::make_unique(root_path, options, file_system); + } + }; + + REGISTER_PAIMON_FACTORY(MyCatalogFactory); + + std::map options = {{"metastore", "my-catalog"}}; + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr catalog, + paimon::Catalog::Create(/*root_path=*/"/", options)); + +Because ``CatalogFactory`` shares the global ``FactoryCreator`` registry with file system, +file format, credential provider, and index factories, choose an identifier that does not +collide with built-in names such as ``filesystem``, ``rest``, ``local``, ``oss``, ``s3``, +or ``parquet``. diff --git a/include/paimon/catalog/catalog.h b/include/paimon/catalog/catalog.h index 094b41f0c..7747bf7a1 100644 --- a/include/paimon/catalog/catalog.h +++ b/include/paimon/catalog/catalog.h @@ -57,6 +57,10 @@ class PAIMON_EXPORT Catalog : public std::enable_shared_from_this { /// %Factory method for creating a `Catalog` instance. /// + /// Dispatches through `CatalogFactory` using `CatalogOptions::METASTORE` (default + /// `"filesystem"`), lowercased before lookup so custom catalogs registered with + /// `REGISTER_PAIMON_FACTORY` can be selected via options. + /// /// @param root_path Path to the root directory where the catalog is located. For the /// REST catalog (`CatalogOptions::METASTORE` set to "rest") this is /// instead the warehouse (instance) name registered on the server. @@ -70,6 +74,7 @@ class PAIMON_EXPORT Catalog : public std::enable_shared_from_this { /// tokens, keeps routing each scheme to its backend. Ignored when /// `file_system` is supplied, which serves every scheme itself. /// @return A result containing a unique pointer to a `Catalog` instance, or an error status. + /// @see CatalogFactory static Result> Create( const std::string& root_path, const std::map& options, const std::shared_ptr& file_system = nullptr, diff --git a/include/paimon/catalog/catalog_factory.h b/include/paimon/catalog/catalog_factory.h new file mode 100644 index 000000000..9331f1c41 --- /dev/null +++ b/include/paimon/catalog/catalog_factory.h @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include + +#include "paimon/factories/factory.h" +#include "paimon/result.h" +#include "paimon/visibility.h" + +namespace paimon { + +class Catalog; +class FileSystem; + +/// A factory for creating `Catalog` instances. +/// +/// Register an implementation with `REGISTER_PAIMON_FACTORY` so `Catalog::Create` can resolve a +/// custom `CatalogOptions::METASTORE` identifier through `FactoryCreator`. `Catalog::Create` +/// lowercases `CatalogOptions::METASTORE` before lookup, so custom factories should register +/// lowercase identifiers. +/// +/// @note All factories share one identifier space, so avoid colliding with built-in catalog, +/// file system, format, or index identifiers such as `"filesystem"`, `"rest"`, `"local"`, `"oss"`, +/// `"s3"`, or `"parquet"`. +class PAIMON_EXPORT CatalogFactory : public Factory { + public: + ~CatalogFactory() override = default; + + /// Creates a `Catalog` instance from the configuration options and optional file system. + /// + /// @param root_path Path to the root directory where the catalog is located, or catalog + /// warehouse/instance identifier. + /// @param options Configuration options for catalog initialization. + /// @param file_system Optional caller-supplied file system for file operations. + /// @param fs_scheme_to_identifier_map Maps a URI scheme (like "oss") to the registered file + /// system identifier that serves it when `file_system` is nullptr. + /// @return A result containing a unique pointer to a `Catalog` instance, or an error status. + virtual Result> Create( + const std::string& root_path, const std::map& options, + const std::shared_ptr& file_system = nullptr, + const std::map& fs_scheme_to_identifier_map = {}) const = 0; + + /// Gets a `Catalog` instance using the factory registered under `identifier`. + /// @pre Factory is already registered. + static Result> Get( + const std::string& identifier, const std::string& root_path, + const std::map& options, + const std::shared_ptr& file_system = nullptr, + const std::map& fs_scheme_to_identifier_map = {}); +}; + +} // namespace paimon diff --git a/src/paimon/CMakeLists.txt b/src/paimon/CMakeLists.txt index ad2b174fc..23996ae81 100644 --- a/src/paimon/CMakeLists.txt +++ b/src/paimon/CMakeLists.txt @@ -274,6 +274,7 @@ set(PAIMON_CORE_SRCS core/casting/timestamp_to_timestamp_cast_executor.cpp core/casting/casting_utils.cpp core/catalog/catalog.cpp + core/catalog/catalog_factory.cpp core/catalog/catalog_utils.cpp core/catalog/file_system_catalog.cpp core/catalog/identifier.cpp @@ -883,6 +884,7 @@ if(PAIMON_BUILD_TESTS) core/catalog/commit_table_request_test.cpp core/catalog/renaming_snapshot_commit_test.cpp core/catalog/file_system_catalog_test.cpp + core/catalog/catalog_factory_test.cpp core/catalog/catalog_test.cpp core/catalog/identifier_test.cpp core/catalog/table_query_auth_result_test.cpp diff --git a/src/paimon/core/catalog/catalog.cpp b/src/paimon/core/catalog/catalog.cpp index 978c20e9d..d75719f90 100644 --- a/src/paimon/core/catalog/catalog.cpp +++ b/src/paimon/core/catalog/catalog.cpp @@ -18,22 +18,14 @@ #include "paimon/catalog/catalog.h" -#include #include -#include -#include -#include "fmt/format.h" +#include "paimon/catalog/catalog_factory.h" #include "paimon/catalog_options.h" #include "paimon/common/utils/string_utils.h" #include "paimon/core/catalog/catalog_utils.h" -#include "paimon/core/catalog/file_system_catalog.h" -#include "paimon/core/core_options.h" #include "paimon/schema/schema.h" #include "paimon/table/format/format_table.h" -#ifdef PAIMON_ENABLE_REST -#include "paimon/rest/rest_catalog.h" -#endif namespace paimon { @@ -53,21 +45,8 @@ Result> Catalog::Create( // up by its exact identifier, so only the exact spelling is portable. metastore = StringUtils::ToLowerCase(metastore_iter->second); } - if (metastore == "rest") { -#ifdef PAIMON_ENABLE_REST - return RestCatalog::Create(root_path, options, file_system, RestHttpClient::Config(), - fs_scheme_to_identifier_map); -#else - return Status::NotImplemented( - "the rest catalog requires building paimon with PAIMON_ENABLE_REST=ON"); -#endif - } - if (metastore != "filesystem") { - return Status::Invalid("unsupported metastore: ", metastore); - } - PAIMON_ASSIGN_OR_RAISE(CoreOptions core_options, - CoreOptions::FromMap(options, file_system, fs_scheme_to_identifier_map)); - return std::make_unique(core_options.GetFileSystem(), root_path, options); + return CatalogFactory::Get(metastore, root_path, options, file_system, + fs_scheme_to_identifier_map); } bool Catalog::SupportsVersionManagement() const { diff --git a/src/paimon/core/catalog/catalog_factory.cpp b/src/paimon/core/catalog/catalog_factory.cpp new file mode 100644 index 000000000..e9d7b47d7 --- /dev/null +++ b/src/paimon/core/catalog/catalog_factory.cpp @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "paimon/catalog/catalog_factory.h" + +#include +#include +#include +#include + +#include "fmt/format.h" +#include "paimon/catalog/catalog.h" +#include "paimon/core/catalog/file_system_catalog.h" +#include "paimon/core/core_options.h" +#include "paimon/factories/factory_creator.h" +#include "paimon/status.h" +#ifdef PAIMON_ENABLE_REST +#include "paimon/rest/rest_catalog.h" +#endif + +namespace paimon { +namespace { + +class FileSystemCatalogFactory : public CatalogFactory { + public: + static constexpr const char kIdentifier[] = "filesystem"; + + const char* Identifier() const override { + return kIdentifier; + } + + Result> Create( + const std::string& root_path, const std::map& options, + const std::shared_ptr& file_system, + const std::map& fs_scheme_to_identifier_map) const override { + PAIMON_ASSIGN_OR_RAISE( + CoreOptions core_options, + CoreOptions::FromMap(options, file_system, fs_scheme_to_identifier_map)); + return std::make_unique(core_options.GetFileSystem(), root_path, + options); + } +}; + +#ifdef PAIMON_ENABLE_REST +class RestCatalogFactory : public CatalogFactory { + public: + static constexpr const char kIdentifier[] = "rest"; + + const char* Identifier() const override { + return kIdentifier; + } + + Result> Create( + const std::string& root_path, const std::map& options, + const std::shared_ptr& file_system, + const std::map& fs_scheme_to_identifier_map) const override { + return RestCatalog::Create(root_path, options, file_system, RestHttpClient::Config(), + fs_scheme_to_identifier_map); + } +}; +#endif + +} // namespace + +Result> CatalogFactory::Get( + const std::string& identifier, const std::string& root_path, + const std::map& options, + const std::shared_ptr& file_system, + const std::map& fs_scheme_to_identifier_map) { + FactoryCreator* factory_creator = FactoryCreator::GetInstance(); + if (factory_creator == nullptr) { + return Status::Invalid("factory creator is null pointer"); + } + Factory* factory = factory_creator->Create(identifier); + if (factory == nullptr) { +#ifndef PAIMON_ENABLE_REST + if (identifier == "rest") { + return Status::NotImplemented( + "the rest catalog requires building paimon with PAIMON_ENABLE_REST=ON"); + } +#endif + return Status::Invalid(fmt::format( + "unsupported metastore: '{}' (catalog factory not registered)", identifier)); + } + auto* catalog_factory = dynamic_cast(factory); + if (catalog_factory == nullptr) { + return Status::Invalid( + fmt::format("Failed to cast catalog factory with identifier '{}'.", identifier)); + } + PAIMON_ASSIGN_OR_RAISE( + std::unique_ptr catalog, + catalog_factory->Create(root_path, options, file_system, fs_scheme_to_identifier_map)); + if (catalog == nullptr) { + return Status::Invalid( + fmt::format("Catalog factory '{}' created a null catalog.", identifier)); + } + return catalog; +} + +// Keep built-in registration with the lookup entry point for static linking. +REGISTER_PAIMON_FACTORY(FileSystemCatalogFactory); +#ifdef PAIMON_ENABLE_REST +REGISTER_PAIMON_FACTORY(RestCatalogFactory); +#endif + +} // namespace paimon diff --git a/src/paimon/core/catalog/catalog_factory_test.cpp b/src/paimon/core/catalog/catalog_factory_test.cpp new file mode 100644 index 000000000..929d24fa2 --- /dev/null +++ b/src/paimon/core/catalog/catalog_factory_test.cpp @@ -0,0 +1,224 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "paimon/catalog/catalog_factory.h" + +#include +#include +#include + +#include "arrow/api.h" +#include "gtest/gtest.h" +#include "paimon/catalog/catalog.h" +#include "paimon/catalog/identifier.h" +#include "paimon/catalog_options.h" +#include "paimon/common/catalog/catalog_context.h" +#include "paimon/core/operation/internal_read_context.h" +#include "paimon/core/schema/table_schema.h" +#include "paimon/defs.h" +#include "paimon/fs/local/local_file_system_factory.h" +#include "paimon/read_context.h" +#include "paimon/status.h" +#include "paimon/testing/mock/mock_catalog.h" +#include "paimon/testing/mock/mock_file_system.h" +#include "paimon/testing/utils/testharness.h" + +namespace paimon::test { +namespace { + +class TestCustomCatalog : public MockVersionManagedCatalog { + public: + TestCustomCatalog(std::string root_path, std::map options, + std::shared_ptr file_system, + std::map fs_scheme_to_identifier_map) + : root_path_(std::move(root_path)), + fs_scheme_to_identifier_map_(std::move(fs_scheme_to_identifier_map)) { + SetOptions(options); + SetFileSystem(file_system); + } + + std::string GetRootPath() const override { + return root_path_; + } + + const std::map& GetSchemeMap() const { + return fs_scheme_to_identifier_map_; + } + + private: + std::string root_path_; + std::map fs_scheme_to_identifier_map_; +}; + +class TestCatalogFactory : public CatalogFactory { + public: + static const char IDENTIFIER[]; + + const char* Identifier() const override { + return IDENTIFIER; + } + + Result> Create( + const std::string& root_path, const std::map& options, + const std::shared_ptr& file_system, + const std::map& fs_scheme_to_identifier_map) const override { + if (options.count("test.fail") > 0) { + return Status::Invalid("custom catalog failure: " + options.at("test.fail")); + } + if (options.count("test.null") > 0) { + return std::unique_ptr(nullptr); + } + return std::make_unique(root_path, options, file_system, + fs_scheme_to_identifier_map); + } +}; + +const char TestCatalogFactory::IDENTIFIER[] = "test-custom-catalog"; + +} // namespace + +REGISTER_PAIMON_FACTORY(TestCatalogFactory); + +TEST(CatalogFactoryTest, TestBuiltinFilesystemCatalog) { + const std::map options = {{Options::FILE_SYSTEM, "local"}}; + ASSERT_OK_AND_ASSIGN(std::unique_ptr by_get, + CatalogFactory::Get("filesystem", "/tmp/wh", options)); + ASSERT_EQ(by_get->GetRootPath(), "/tmp/wh"); + + ASSERT_OK_AND_ASSIGN(std::unique_ptr by_default, Catalog::Create("/tmp/wh", options)); + ASSERT_EQ(by_default->GetRootPath(), "/tmp/wh"); + + std::map mixed_case_options = options; + mixed_case_options[CatalogOptions::METASTORE] = "FileSystem"; + ASSERT_OK_AND_ASSIGN(std::unique_ptr by_mixed_case, + Catalog::Create("/tmp/wh", mixed_case_options)); + ASSERT_EQ(by_mixed_case->GetRootPath(), "/tmp/wh"); +} + +TEST(CatalogFactoryTest, TestCustomCatalogFactoryDispatch) { + auto custom_fs = std::make_shared(); + const std::map scheme_map = {{"oss", "local"}, {"s3", "mock_fs"}}; + const std::map direct_options = {{"custom.key", "custom.val"}}; + + ASSERT_OK_AND_ASSIGN(std::unique_ptr direct_catalog, + CatalogFactory::Get(TestCatalogFactory::IDENTIFIER, "custom://root", + direct_options, custom_fs, scheme_map)); + auto* typed_direct = dynamic_cast(direct_catalog.get()); + ASSERT_NE(typed_direct, nullptr); + ASSERT_EQ(typed_direct->GetRootPath(), "custom://root"); + ASSERT_EQ(typed_direct->GetFileSystem(), custom_fs); + ASSERT_EQ(typed_direct->GetOptions(), direct_options); + ASSERT_EQ(typed_direct->GetSchemeMap(), scheme_map); + + const std::map mixed_options = { + {CatalogOptions::METASTORE, "Test-Custom-Catalog"}, {"custom.key", "custom.val"}}; + ASSERT_OK_AND_ASSIGN(std::unique_ptr dispatched_catalog, + Catalog::Create("custom://root", mixed_options, custom_fs, scheme_map)); + auto* typed_dispatched = dynamic_cast(dispatched_catalog.get()); + ASSERT_NE(typed_dispatched, nullptr); + ASSERT_EQ(typed_dispatched->GetRootPath(), "custom://root"); + ASSERT_EQ(typed_dispatched->GetFileSystem(), custom_fs); + ASSERT_EQ(typed_dispatched->GetOptions(), mixed_options); + ASSERT_EQ(typed_dispatched->GetSchemeMap(), scheme_map); +} + +TEST(CatalogFactoryTest, TestUnregisteredIdentifier) { + ASSERT_NOK_WITH_MSG(CatalogFactory::Get("no-such-catalog", "/tmp/wh", {}).status(), + "unsupported metastore"); + ASSERT_NOK_WITH_MSG( + Catalog::Create("/tmp/wh", {{CatalogOptions::METASTORE, "no-such-catalog"}}).status(), + "unsupported metastore"); +} + +TEST(CatalogFactoryTest, TestIdentifierOfAnotherFactoryKind) { + // All factories share one identifier space, so naming another factory kind must fail cleanly. + ASSERT_NOK_WITH_MSG( + CatalogFactory::Get(LocalFileSystemFactory::IDENTIFIER, "/tmp/wh", {}).status(), + "Failed to cast catalog factory"); + ASSERT_NOK_WITH_MSG(Catalog::Create("/tmp/wh", {{CatalogOptions::METASTORE, + LocalFileSystemFactory::IDENTIFIER}}) + .status(), + "Failed to cast catalog factory"); +} + +TEST(CatalogFactoryTest, TestPropagatesFactoryFailure) { + const std::map options = { + {CatalogOptions::METASTORE, TestCatalogFactory::IDENTIFIER}, {"test.fail", "boom"}}; + ASSERT_NOK_WITH_MSG(Catalog::Create("/tmp/wh", options).status(), + "custom catalog failure: boom"); +} + +TEST(CatalogFactoryTest, TestRejectsNullCatalog) { + const std::map options = { + {CatalogOptions::METASTORE, TestCatalogFactory::IDENTIFIER}, {"test.null", "1"}}; + ASSERT_NOK_WITH_MSG(Catalog::Create("/tmp/wh", options).status(), "created a null catalog"); +} + +#ifndef PAIMON_ENABLE_REST +TEST(CatalogFactoryTest, TestRestDisabledError) { + Status status = Catalog::Create("/tmp/wh", {{CatalogOptions::METASTORE, "rest"}}).status(); + ASSERT_TRUE(status.IsNotImplemented()) << status.ToString(); + ASSERT_NOK_WITH_MSG(status, "PAIMON_ENABLE_REST=ON"); +} +#endif + +TEST(CatalogFactoryTest, TestDependencyCatalogContextPropagatesCustomMetastore) { + const std::map catalog_options = { + {CatalogOptions::METASTORE, TestCatalogFactory::IDENTIFIER}, + {"catalog.custom", "from-catalog"}}; + const std::map table_options = { + {Options::FILE_FORMAT, "orc"}, {Options::BUCKET, "-1"}, {Options::FILE_SYSTEM, "local"}}; + ASSERT_OK_AND_ASSIGN( + std::unique_ptr table_schema, + TableSchema::Create(0, arrow::schema({arrow::field("id", arrow::int32())}), + /*partition_keys=*/{}, /*primary_keys=*/{}, table_options)); + std::shared_ptr shared_schema(std::move(table_schema)); + + ASSERT_OK_AND_ASSIGN(std::unique_ptr unique_catalog, + Catalog::Create("custom://root", catalog_options, + std::make_shared(), {{"oss", "local"}})); + std::shared_ptr catalog(std::move(unique_catalog)); + + ReadContextBuilder builder("oss://bucket/custom/table"); + builder.WithCatalog(catalog, Identifier("db1", "tbl1")).AddOption("read.custom", "from-read"); + ASSERT_OK_AND_ASSIGN(std::unique_ptr read_context, builder.Finish()); + std::shared_ptr shared_read_context(std::move(read_context)); + + ASSERT_OK_AND_ASSIGN( + std::unique_ptr internal_context, + InternalReadContext::Create(shared_read_context, shared_schema, table_options)); + std::shared_ptr dependency_context = + internal_context->CreateDependencyCatalogContext(); + + ASSERT_EQ(dependency_context->root_path, "custom://root"); + ASSERT_EQ(dependency_context->options.at(CatalogOptions::METASTORE), + TestCatalogFactory::IDENTIFIER); + ASSERT_EQ(dependency_context->options.at("catalog.custom"), "from-catalog"); + ASSERT_EQ(dependency_context->options.at("read.custom"), "from-read"); + + ASSERT_OK_AND_ASSIGN( + std::unique_ptr dependency_catalog, + Catalog::Create(dependency_context->root_path, dependency_context->options, + /*file_system=*/nullptr, dependency_context->fs_scheme_to_identifier_map)); + auto* typed_dependency = dynamic_cast(dependency_catalog.get()); + ASSERT_NE(typed_dependency, nullptr); + ASSERT_EQ(typed_dependency->GetRootPath(), "custom://root"); + ASSERT_EQ(typed_dependency->GetFileSystem(), nullptr); +} + +} // namespace paimon::test