lxy-9602 commented on code in PR #447:
URL: https://github.com/apache/paimon-cpp/pull/447#discussion_r4376487941


##########
src/paimon/rest/rest_catalog.cpp:
##########
@@ -388,78 +390,113 @@ Result<std::shared_ptr<Schema>> 
RestCatalog::LoadTableSchema(const Identifier& i
         context.catalog_options = api_->GetMergedOptions();
         PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<SystemTable> system_table,
                                
GlobalSystemTableLoader::Load(identifier.GetObjectName(), context));
-        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Schema> arrow_schema,
-                               system_table->ArrowSchema());
-        return std::make_shared<SystemTableSchema>(std::move(arrow_schema));
+        return system_table->LatestSchema();
     }
     PAIMON_ASSIGN_OR_RAISE(bool is_system_table, identifier.IsSystemTable());
     PAIMON_ASSIGN_OR_RAISE(std::optional<std::string> branch, 
identifier.GetBranchName());
     branch = NormalizeBranch(std::move(branch));
     PAIMON_ASSIGN_OR_RAISE(Identifier load_identifier, 
ToLoadIdentifier(identifier));
     if (is_system_table) {
-        PAIMON_ASSIGN_OR_RAISE(std::optional<std::string> system_table_name,
-                               identifier.GetSystemTableName());
-        if (!system_table_name || 
!SystemTableLoader::IsSupported(system_table_name.value())) {
-            return Status::NotExist(fmt::format("{} not exist", 
identifier.ToString()));
-        }
-        std::string data_table_path;
-        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<TableSchema> latest_schema,
-                               LoadDataTableSchema(load_identifier, branch, 
&data_table_path,
-                                                   /*table_id=*/nullptr));
+        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<SystemTable> system_table,
+                               LoadSystemTable(identifier, load_identifier, 
branch));
         if (table_path != nullptr) {
-            *table_path = data_table_path;
-        }
-        std::map<std::string, std::string> dynamic_options;
-        if (branch) {
-            dynamic_options[Options::BRANCH] = branch.value();
+            *table_path = system_table->GetRootPath();
         }
-        // The file system is only used to build the system table, whose 
schema does not
-        // read any file, so the catalog wide one is enough here. The 
credentials of the
-        // table are applied when its rows are read, through the file system 
of the read or
-        // scan context.
-        PAIMON_ASSIGN_OR_RAISE(
-            std::shared_ptr<SystemTable> system_table,
-            SystemTableLoader::Load(system_table_name.value(), fs_, 
data_table_path, latest_schema,
-                                    dynamic_options));
-        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Schema> arrow_schema,
-                               system_table->ArrowSchema());
-        return std::make_shared<SystemTableSchema>(std::move(arrow_schema));
+        return system_table->LatestSchema();
     }
     PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<TableSchema> schema,
                            LoadDataTableSchema(load_identifier, branch, 
table_path, table_id));
     return checked_pointer_cast<Schema>(schema);
 }
 
-Result<std::shared_ptr<FormatTable>> RestCatalog::LoadFormatTable(
-    const Identifier& identifier) const {
-    // A system table is already refused by `Catalog::GetFormatTable()`, which 
every caller goes
-    // through, so this only has to load.
-    PAIMON_ASSIGN_OR_RAISE(std::optional<std::string> branch, 
identifier.GetBranchName());
-    branch = NormalizeBranch(std::move(branch));
-    PAIMON_ASSIGN_OR_RAISE(Identifier load_identifier, 
ToLoadIdentifier(identifier));
-    std::string location;
-    PAIMON_ASSIGN_OR_RAISE(
-        std::shared_ptr<TableSchema> schema,
-        LoadDataTableSchema(load_identifier, branch, &location, 
/*table_id=*/nullptr));
-    // A rest catalog holds the schema itself, so everything below the 
location is data.
-    return FormatTable::Create(fs_, location, identifier, 
checked_pointer_cast<DataSchema>(schema),
-                               /*location_carries_paimon_metadata=*/false,
-                               /*dynamic_options=*/{});
+Result<std::shared_ptr<SystemTable>> RestCatalog::LoadSystemTable(
+    const Identifier& identifier, const Identifier& load_identifier,
+    const std::optional<std::string>& branch) const {
+    PAIMON_ASSIGN_OR_RAISE(std::optional<std::string> system_table_name,
+                           identifier.GetSystemTableName());
+    if (!system_table_name || 
!SystemTableLoader::IsSupported(system_table_name.value())) {
+        return Status::NotExist(fmt::format("{} not exist", 
identifier.ToString()));
+    }
+    std::string data_table_path;
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<TableSchema> latest_schema,
+                           LoadDataTableSchema(load_identifier, branch, 
&data_table_path,
+                                               /*table_id=*/nullptr));
+    std::map<std::string, std::string> dynamic_options;
+    if (branch) {
+        dynamic_options[Options::BRANCH] = branch.value();
+    }
+    // The system table reads the data table's own files, so it is served by 
that table's own
+    // credentials - with data tokens the only file system that can open them.
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<FileSystem> table_file_system,
+                           GetTableFileSystem(identifier));
+    return SystemTableLoader::Load(system_table_name.value(), 
table_file_system, data_table_path,
+                                   latest_schema, dynamic_options);
 }
 
 Result<std::shared_ptr<Table>> RestCatalog::GetTable(const Identifier& 
identifier) const {
+    // The global system tables of the "sys" database expose the catalog 
itself rather than any
+    // table the server stands behind, so they are served locally and never 
reach the managed-table
+    // load below, whose schema cast a read-only system table schema would not 
survive.
+    if (CatalogUtils::IsSystemDatabase(identifier.GetDatabaseName())) {
+        PAIMON_ASSIGN_OR_RAISE(bool supported,
+                               
GlobalSystemTableLoader::IsSupported(identifier.GetObjectName(),
+                                                                    
api_->GetMergedOptions()));
+        if (!supported) {
+            return Status::NotExist(fmt::format("{} not exist", 
identifier.ToString()));
+        }
+        GlobalSystemTableContext context;
+        context.catalog = this;
+        // A global system table builds its rows lazily through the catalog 
above, so the table
+        // handed back has to keep it alive or the pointer dangles at the 
first `BuildRows`. A
+        // catalog not owned by a `std::shared_ptr` cannot be kept, so it is 
refused by name.
+        std::shared_ptr<const RestCatalog> self = weak_from_this().lock();
+        if (self == nullptr) {

Review Comment:
   `Catalog::Create()` returns `std::unique_ptr<Catalog>`. Converting that 
result to `std::shared_ptr<Catalog>` does not initialize 
`enable_shared_from_this<RestCatalog>`, because the constructor sees the static 
type `Catalog`, which has no such base. Consequently, `weak_from_this().lock()` 
remains empty and loading `sys.tables` fails even when the catalog is owned by 
a `shared_ptr`.
   
   The new test converts `unique_ptr<RestCatalog>` directly to 
`shared_ptr<RestCatalog>`, so it does not cover this public factory path. Could 
we make catalog retention work through `Catalog::Create()` and add a regression 
test using that path?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to