Skip to content
Draft
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
3 changes: 3 additions & 0 deletions docs/source/api/catalog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Interface
.. doxygenclass:: paimon::Catalog
:members:

.. doxygenclass:: paimon::CatalogFactory
:members:

.. doxygenclass:: paimon::Identifier
:members:
:undoc-members:
Expand Down
40 changes: 38 additions & 2 deletions docs/source/user_guide/catalog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ Paimon C++ provides a :ref:`Catalog abstraction <cpp-api-catalog>` 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
~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -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<std::unique_ptr<paimon::Catalog>> Create(
const std::string& root_path, const std::map<std::string, std::string>& options,
const std::shared_ptr<paimon::FileSystem>& file_system,
const std::map<std::string, std::string>& fs_scheme_to_identifier_map) const override {
return std::make_unique<MyCatalog>(root_path, options, file_system);
}
};

REGISTER_PAIMON_FACTORY(MyCatalogFactory);

std::map<std::string, std::string> options = {{"metastore", "my-catalog"}};
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<paimon::Catalog> 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``.
5 changes: 5 additions & 0 deletions include/paimon/catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class PAIMON_EXPORT Catalog : public std::enable_shared_from_this<Catalog> {

/// %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.
Expand All @@ -70,6 +74,7 @@ class PAIMON_EXPORT Catalog : public std::enable_shared_from_this<Catalog> {
/// 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<std::unique_ptr<Catalog>> Create(
const std::string& root_path, const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system = nullptr,
Expand Down
71 changes: 71 additions & 0 deletions include/paimon/catalog/catalog_factory.h
Original file line number Diff line number Diff line change
@@ -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 <map>
#include <memory>
#include <string>

#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<std::unique_ptr<Catalog>> Create(
const std::string& root_path, const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system = nullptr,
const std::map<std::string, std::string>& fs_scheme_to_identifier_map = {}) const = 0;

/// Gets a `Catalog` instance using the factory registered under `identifier`.
/// @pre Factory is already registered.
static Result<std::unique_ptr<Catalog>> Get(
const std::string& identifier, const std::string& root_path,
const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system = nullptr,
const std::map<std::string, std::string>& fs_scheme_to_identifier_map = {});
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a suggestion: if it doesn’t affect interface compatibility, could we remove the default parameters?


} // namespace paimon
2 changes: 2 additions & 0 deletions src/paimon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 3 additions & 24 deletions src/paimon/core/catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@

#include "paimon/catalog/catalog.h"

#include <optional>
#include <string>
#include <utility>
#include <vector>

#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 {

Expand All @@ -53,21 +45,8 @@ Result<std::unique_ptr<Catalog>> 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<FileSystemCatalog>(core_options.GetFileSystem(), root_path, options);
return CatalogFactory::Get(metastore, root_path, options, file_system,
fs_scheme_to_identifier_map);
}

bool Catalog::SupportsVersionManagement() const {
Expand Down
121 changes: 121 additions & 0 deletions src/paimon/core/catalog/catalog_factory.cpp
Original file line number Diff line number Diff line change
@@ -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 <map>
#include <memory>
#include <string>
#include <utility>

#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<std::unique_ptr<Catalog>> Create(
const std::string& root_path, const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system,
const std::map<std::string, std::string>& 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<FileSystemCatalog>(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<std::unique_ptr<Catalog>> Create(
const std::string& root_path, const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system,
const std::map<std::string, std::string>& 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<std::unique_ptr<Catalog>> CatalogFactory::Get(
const std::string& identifier, const std::string& root_path,
const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system,
const std::map<std::string, std::string>& 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<CatalogFactory*>(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,
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
Loading
Loading