SteNicholas commented on code in PR #527:
URL: https://github.com/apache/paimon-cpp/pull/527#discussion_r4495119801


##########
src/paimon/rest/rest_catalog.cpp:
##########
@@ -448,19 +450,88 @@ Result<std::shared_ptr<FormatTable>> 
RestCatalog::LoadFormatTable(
                                /*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();
+    }
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<FileSystem> table_file_system,
+                           GetTableFileSystem(identifier));
+    // Use the catalog name, including any branch, even when data lives under 
a UUID directory.
+    PAIMON_ASSIGN_OR_RAISE(
+        Identifier data_identifier,
+        CatalogUtils::BranchIdentifier(identifier, 
branch.value_or(std::string())));
+    return SystemTableLoader::Load(system_table_name.value(), 
table_file_system, data_table_path,
+                                   latest_schema, dynamic_options, 
data_identifier.GetObjectName(),
+                                   fs_scheme_to_identifier_map_);
+}
+
 Result<std::shared_ptr<Table>> RestCatalog::GetTable(const Identifier& 
identifier) const {
-    // Read UUID, path and schema together to avoid mixing table generations 
after recreation.
+    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()));
+        }
+        std::shared_ptr<const Catalog> self = weak_from_this().lock();
+        if (self == nullptr) {
+            return Status::Invalid(
+                "a global system table retains its catalog, so the catalog 
must be owned by "
+                "a std::shared_ptr before it is asked for one");
+        }
+        GlobalSystemTableContext context;
+        context.catalog = self.get();
+        context.catalog_owner = std::move(self);
+        context.fs = fs_;
+        context.warehouse = warehouse_;
+        context.catalog_options = api_->GetMergedOptions();
+        return GlobalSystemTableLoader::Load(identifier.GetObjectName(), 
context);
+    }
+    PAIMON_ASSIGN_OR_RAISE(bool is_system_table, identifier.IsSystemTable());
+    if (is_system_table) {
+        PAIMON_ASSIGN_OR_RAISE(std::optional<std::string> branch, 
identifier.GetBranchName());
+        PAIMON_ASSIGN_OR_RAISE(Identifier load_identifier, 
ToLoadIdentifier(identifier));
+        return LoadSystemTable(identifier, load_identifier, 
NormalizeBranch(std::move(branch)));
+    }
+    // Load UUID, location and schema together to avoid mixing table 
generations.
+    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 uuid;
     std::string table_path;
-    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<Schema> schema,
-                           LoadTableSchema(identifier, &uuid, &table_path));
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<TableSchema> schema,
+                           LoadDataTableSchema(load_identifier, branch, 
&table_path, &uuid));
+    std::shared_ptr<DataSchema> data_schema = 
std::dynamic_pointer_cast<DataSchema>(schema);
+    if (data_schema != nullptr) {
+        PAIMON_ASSIGN_OR_RAISE(bool is_format_table,
+                               
TableTypeDefine::IsFormatTable(data_schema->Options()));
+        if (is_format_table) {
+            return FormatTable::Create(fs_, table_path, identifier, 
data_schema,

Review Comment:
   **Bind format tables to the table-specific filesystem before returning 
them.** When `DATA_TOKEN_ENABLED` is true and no filesystem was explicitly 
supplied, `fs_` is the catalog-wide filesystem; only 
`GetTableFileSystem(identifier)` creates the `RestTokenFileSystem` that fetches 
and refreshes this table's credentials. This new format-table branch returns 
before that call. `FormatTable::ForContext()` preserves the bound filesystem, 
and its scan/read/write implementations access storage through it, so even 
supplying `WithCatalog()` on an operation context cannot recover the table 
token. On deployments where storage access requires the table-scoped 
credentials, `GetTable()` succeeds but subsequent file operations fail. Resolve 
the filesystem as the managed and system-table branches do, and add a 
format-table regression with data tokens enabled that verifies the token 
endpoint is used on storage access.



##########
src/paimon/core/operation/scan_context.cpp:
##########
@@ -240,14 +243,25 @@ Result<std::unique_ptr<ScanContext>> 
ScanContextBuilder::Finish() {
         PAIMON_ASSIGN_OR_RAISE(impl_->specific_file_system_,
                                
impl_->catalog_->GetTableFileSystem(impl_->identifier_.value()));
     }
+    PAIMON_ASSIGN_OR_RAISE(
+        std::string branch,
+        BranchManager::ResolveBranch(impl_->identifier_, impl_->options_, 
std::nullopt, "scan"));
+    std::map<std::string, std::string> options = impl_->options_;
+    std::optional<std::string> named_branch;
+    if (impl_->identifier_) {
+        PAIMON_ASSIGN_OR_RAISE(named_branch, 
impl_->identifier_->GetBranchName());
+    }
+    if (named_branch || options.count(Options::BRANCH) != 0) {
+        options[Options::BRANCH] = branch;

Review Comment:
   **Canonicalize identifier aliases of the main branch before adding the 
branch option.** For `Identifier("db", "tbl$branch_MAIN")`, the catalog and 
`Identifier::GetBranchNameOrDefault()` resolve the table to `main`, but 
`ResolveBranch()` reads `GetBranchName()` and only normalizes blank strings, so 
this code inserts `branch=MAIN`. A context built with 
`ScanContextBuilder(path).WithCatalog(catalog, identifier)` then makes 
`TableScan::Create()` look for a schema under `branch/branch-MAIN` instead of 
the existing main schema; passing it to the catalog-loaded table's `NewScan()` 
instead fails with a branch contradiction. Before this change the scan builder 
did not inject that option and the same main-table scan worked. Canonicalize 
the identifier's main alias while preserving the case-sensitive semantics of 
explicitly named branch options, and cover both static and loaded-table scan 
entrypoints with `$branch_MAIN`.



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