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


##########
src/paimon/core/table/file_store_table.h:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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/catalog/identifier.h"
+#include "paimon/catalog/table.h"
+#include "paimon/core/operation/commit/commit_scanner.h"
+#include "paimon/core/schema/schema_manager.h"
+#include "paimon/core/schema/table_schema.h"
+#include "paimon/core/table/file_store.h"
+#include "paimon/core/utils/branch_manager.h"
+#include "paimon/core/utils/snapshot_manager.h"
+#include "paimon/result.h"
+#include "paimon/type_fwd.h"
+
+namespace paimon {
+
+class InternalReadContext;
+class IOManager;
+class ManifestFile;
+class ManifestList;
+class RealtimeContext;
+class SplitGenerator;
+
+/// Managed table backed by snapshots and manifests.
+/// Shares operator setup across append-only and primary-key tables, selected 
from the schema.
+class FileStoreTable : public Table {
+ public:
+    /// Creates an append-only or primary-key table according to the schema's 
primary keys.
+    /// @param specified_file_system Explicit file system, overriding 
option-based resolution.
+    /// @param schema Loaded table schema; must not be null.
+    /// @param branch Branch used by all operators and metadata managers.
+    /// @param fs_scheme_map URI scheme routing for option-based file system 
resolution.
+    static Result<std::shared_ptr<FileStoreTable>> Create(
+        const std::shared_ptr<FileSystem>& specified_file_system, const 
std::string& root_path,
+        const std::shared_ptr<TableSchema>& schema, const Identifier& 
identifier,
+        const std::string& uuid, const std::string& branch,
+        const std::map<std::string, std::string>& fs_scheme_map = {});
+
+    /// Binds a resolved store without replacing the loaded schema's options.
+    static Result<std::shared_ptr<FileStoreTable>> Create(
+        std::unique_ptr<FileStore> store, const std::shared_ptr<TableSchema>& 
schema,
+        const Identifier& identifier, const std::string& uuid,
+        const std::map<std::string, std::string>& fs_scheme_map = {});
+
+    std::string CurrentBranch() const {
+        auto branch = options_.find(Options::BRANCH);
+        return branch == options_.end() ? BranchManager::DEFAULT_MAIN_BRANCH
+                                        : 
BranchManager::NormalizeBranch(branch->second);
+    }
+
+    std::shared_ptr<SchemaManager> GetSchemaManager() const {

Review Comment:
   `GetSchemaManager()` creates a new `SchemaManager` with an empty cache on 
every call. Write and commit now get several separate instances: `CreateWrite` 
(append_only_file_store_table.cpp:65, primary_key_file_store_table.cpp:106), 
`NewCommit` (file_store_table.cpp:692) and the commit scan suppliers 
(append_only_file_store_table.cpp:191, primary_key_file_store_table.cpp:266).
   
   The old `table.cpp` reused the manager that `FormatTableLoader::TryLoad` had 
already used to read the latest schema. So each writer or committer now reads 
schema files at least once more. This is more visible on object stores with 
many short-lived writers.



##########
src/paimon/core/table/system/in_memory_system_table.cpp:
##########
@@ -104,19 +106,28 @@ class InMemorySystemTableRead : public TableRead {
 
 }  // namespace
 
-InMemorySystemTable::InMemorySystemTable(std::string table_path)
-    : table_path_(std::move(table_path)) {}
+InMemorySystemTable::InMemorySystemTable(const std::shared_ptr<FileSystem>& fs,
+                                         std::string table_path, std::string 
name)
+    : SystemTable(fs, table_path, std::move(name)) {}
 
 Result<std::unique_ptr<TableScan>> InMemorySystemTable::NewScan(
-    const std::shared_ptr<ScanContext>& /*context*/) const {
-    return std::make_unique<SystemTableScan>(table_path_);
+    std::unique_ptr<ScanContext> /*context*/) const {
+    return std::make_unique<SystemTableScan>(GetRootPath());
 }
 
 Result<std::unique_ptr<TableRead>> InMemorySystemTable::NewRead(
-    const std::shared_ptr<ReadContext>& context) const {
-    return std::make_unique<InMemorySystemTableRead>(
-        checked_pointer_cast<const InMemorySystemTable>(shared_from_this()),
-        context->GetMemoryPool());
+    std::unique_ptr<ReadContext> ctx) const {
+    const std::shared_ptr<ReadContext> context = std::move(ctx);
+    PAIMON_RETURN_NOT_OK(ContextUtils::CheckOperatorContext(context, "read"));
+    PAIMON_RETURN_NOT_OK(CheckNoSearch(context));
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<const InMemorySystemTable> table,
+                           ForRead(context->GetOptions()));
+    return std::make_unique<InMemorySystemTableRead>(std::move(table), 
context->GetMemoryPool());
+}
+
+Result<std::shared_ptr<const InMemorySystemTable>> 
InMemorySystemTable::ForRead(

Review Comment:
   The default `ForRead` ignores the read options. Load `db.tbl$snapshots` on 
main through `Catalog::GetTable()`, then call `NewRead` with `branch=dev` in 
the context. The read succeeds and returns main's snapshots, with no error. 
`$schemas`, `$tags`, `$branches` and `$consumers` behave the same.
   
   `$manifests`, `$files` and `FileStoreTable` reject the same context with 
"context branch contradicts the loaded table". No test covers these five tables.



##########
src/paimon/core/table/system/audit_log_system_table.cpp:
##########
@@ -430,8 +444,11 @@ Result<std::unique_ptr<TableRead>> 
AuditLogSystemTable::NewChangelogRead(
     PAIMON_ASSIGN_OR_RAISE(CoreOptions core_options, 
CoreOptions::FromMap(read_options));
     PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ReadContext> data_context,
                            CreateDataReadContext(context, read_options));
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<FileStoreTable> data_table,

Review Comment:
   `ReadOptions()`, `BaseReadSchema()` and `ArrowSchema()` only use the loaded 
`options_`. For a table loaded by `Catalog::GetTable()`, 
`table-read.sequence-number.enabled=true` in the read context is ignored. The 
output has no sequence number column, and no error is returned.
   
   The static `TableRead::Create("tbl$audit_log", ...)` path merges this option 
in `SystemTableLoader::Load`, so it does return the column. `$binlog` has the 
same behavior through `NewChangelogRead`.



##########
include/paimon/catalog/catalog.h:
##########
@@ -146,7 +146,7 @@ class PAIMON_EXPORT Catalog {
     virtual Status RenameTable(const Identifier& from_table, const Identifier& 
to_table,
                                bool ignore_if_not_exists) = 0;
 
-    /// Gets a table.
+    /// Gets a managed, format or read-only system table.

Review Comment:
   `Catalog::Create()` returns a `std::unique_ptr`. If the catalog is held by a 
`unique_ptr`, `GetTable()` for a `sys.*` table returns Invalid, while 
`TableExists()` returns true for the same identifier. This requirement is only 
in `catalog.rst` and in the error message. The API doc here does not mention 
it, and no test covers the `unique_ptr` case.



##########
src/paimon/core/table/table_factory.h:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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 <memory>
+
+#include "paimon/result.h"
+namespace paimon {
+class Table;
+class ScanContext;
+class ReadContext;
+class WriteContext;
+class CommitContext;
+/// Compatibility loading for the existing static operator factories.
+class TableFactory {

Review Comment:
   `TableFactory` only has static methods. `docs/code-style.md` (Utility 
Classes) requires static-only classes to delete their constructor and 
destructor.



##########
src/paimon/core/table/file_store_table.cpp:
##########
@@ -0,0 +1,695 @@
+/*
+ * 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/core/table/file_store_table.h"
+
+#include <map>
+#include <memory>
+#include <optional>
+#include <set>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "fmt/format.h"
+#include "paimon/catalog/catalog.h"
+#include "paimon/catalog/identifier.h"
+#include "paimon/commit_context.h"
+#include "paimon/common/fs/resolving_file_system.h"
+#include "paimon/common/predicate/predicate_validator.h"
+#include "paimon/common/types/data_field.h"
+#include "paimon/common/utils/binary_row_partition_computer.h"
+#include "paimon/common/utils/options_utils.h"
+#include "paimon/common/utils/path_util.h"
+#include "paimon/common/utils/sensitive_config_utils.h"
+#include "paimon/common/utils/string_utils.h"
+#include "paimon/core/catalog/catalog_snapshot_commit.h"
+#include "paimon/core/catalog/catalog_utils.h"
+#include "paimon/core/catalog/commit_lock.h"
+#include "paimon/core/catalog/renaming_snapshot_commit.h"
+#include "paimon/core/catalog/snapshot_commit.h"
+#include "paimon/core/catalog/version_managed_catalog.h"
+#include "paimon/core/core_options.h"
+#include "paimon/core/disk/io_manager.h"
+#include "paimon/core/index/index_file_handler.h"
+#include "paimon/core/index/pk/primary_key_index_definitions.h"
+#include "paimon/core/manifest/index_manifest_file.h"
+#include "paimon/core/operation/commit/realtime_commit_properties.h"
+#include "paimon/core/operation/expire_snapshots.h"
+#include "paimon/core/operation/file_store_commit_impl.h"
+#include "paimon/core/operation/file_store_scan.h"
+#include "paimon/core/operation/internal_read_context.h"
+#include "paimon/core/options/table_type.h"
+#include "paimon/core/realtime/realtime_context_impl.h"
+#include "paimon/core/realtime/realtime_utils.h"
+#include "paimon/core/schema/schema_manager.h"
+#include "paimon/core/schema/schema_validation.h"
+#include "paimon/core/schema/table_schema.h"
+#include "paimon/core/snapshot.h"
+#include "paimon/core/table/append_only_file_store_table.h"
+#include "paimon/core/table/bucket_mode.h"
+#include "paimon/core/table/file_store.h"
+#include "paimon/core/table/primary_key_file_store_table.h"
+#include "paimon/core/table/source/append_only_table_read.h"
+#include "paimon/core/table/source/data_evolution_batch_scan.h"
+#include "paimon/core/table/source/data_table_batch_scan.h"
+#include "paimon/core/table/source/data_table_stream_scan.h"
+#include "paimon/core/table/source/fallback_table_read.h"
+#include "paimon/core/table/source/key_value_table_read.h"
+#include "paimon/core/table/source/primary_key_index_batch_scan.h"
+#include "paimon/core/table/source/read_optimized_scan_options.h"
+#include "paimon/core/table/source/realtime_table_scan.h"
+#include "paimon/core/table/source/snapshot/snapshot_reader.h"
+#include "paimon/core/utils/branch_manager.h"
+#include "paimon/core/utils/context_utils.h"
+#include "paimon/core/utils/field_mapping.h"
+#include "paimon/core/utils/file_store_path_factory.h"
+#include "paimon/core/utils/primary_key_table_utils.h"
+#include "paimon/core/utils/snapshot_manager.h"
+#include "paimon/file_store_commit.h"
+#include "paimon/file_store_write.h"
+#include "paimon/format/file_format.h"
+#include "paimon/fs/file_system.h"
+#include "paimon/read_context.h"
+#include "paimon/realtime/realtime_context.h"
+#include "paimon/result.h"
+#include "paimon/scan_context.h"
+#include "paimon/schema/schema.h"
+#include "paimon/status.h"
+#include "paimon/table/source/table_read.h"
+#include "paimon/table/source/table_scan.h"
+#include "paimon/write_context.h"
+
+namespace arrow {
+class Schema;
+}  // namespace arrow
+
+namespace paimon {
+
+namespace {
+
+/// Allows object-store commits in integration tests.
+constexpr const char kObjectStoreCommitInteTestOption[] = 
"enable-object-store-commit-in-inte-test";
+
+Status ValidateRealtimeScan(const TableSchema& table_schema, const 
CoreOptions& core_options,
+                            const ScanContext& context, bool read_optimized) {
+    if (!context.GetRealtimeContext()) {
+        return Status::OK();
+    }
+    if (!core_options.RealtimeEnabled()) {
+        return Status::Invalid("real-time scan requires 
realtime.enabled=true");
+    }
+    if (core_options.GetBucket() <= 0) {
+        return Status::Invalid("real-time union read requires fixed bucket 
mode");
+    }
+    if (core_options.DataEvolutionEnabled()) {
+        return Status::Invalid("real-time union read does not support data 
evolution");
+    }
+    if (!table_schema.PrimaryKeys().empty()) {
+        if (read_optimized) {
+            return Status::NotImplemented(
+                "PK real-time union read does not support read-optimized 
scans");
+        }
+        PAIMON_RETURN_NOT_OK(
+            PrimaryKeyTableUtils::ValidateRealtimeOptions(core_options, 
table_schema));
+    }
+    if (context.IsStreamingMode()) {
+        return Status::Invalid("real-time union read currently supports batch 
scans only");
+    }
+    if (context.GetLimit()) {
+        return Status::Invalid("real-time union read does not support scan 
limit pushdown");
+    }
+    if (context.GetGlobalIndexResult()) {
+        return Status::Invalid("real-time union read does not support global 
index splits");
+    }
+    StartupMode startup_mode = core_options.GetStartupMode();
+    const bool specified_snapshot =
+        startup_mode == StartupMode::FromSnapshot() && 
core_options.GetScanSnapshotId().has_value();
+    if (!(startup_mode == StartupMode::LatestFull() || startup_mode == 
StartupMode::Latest() ||
+          specified_snapshot)) {
+        return Status::Invalid(
+            "real-time union read requires the latest snapshot or an explicit 
snapshot id");
+    }
+    return Status::OK();
+}
+
+bool IsCatalogCommit(const CommitContext& ctx) {
+    return AsVersionManaged(ctx.GetCatalog()) != nullptr;
+}
+
+Result<std::shared_ptr<SnapshotCommit>> NewSnapshotCommit(
+    const CommitContext& ctx, const std::optional<Identifier>& 
catalog_identifier,
+    const CoreOptions& options, const std::shared_ptr<SnapshotManager>& 
snapshot_manager) {
+    if (IsCatalogCommit(ctx)) {
+        if (!catalog_identifier) {
+            return Status::Invalid("a catalog commit requires a table 
identifier");
+        }
+        PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<CommitLock> lock,
+                               AsVersionManaged(ctx.GetCatalog())
+                                   
->CreateCommitLock(catalog_identifier.value(), ctx.GetTableId(),
+                                                      ctx.GetCommitUser(), 
options));
+        // Reuse the caller's UUID so table recreation cannot redirect this 
commit.
+        return 
std::shared_ptr<SnapshotCommit>(std::make_shared<CatalogSnapshotCommit>(
+            ctx.GetCatalog(), catalog_identifier.value(), ctx.GetTableId(), 
std::move(lock)));
+    }
+    if (ctx.UseRESTCatalogCommit()) {
+        if (options.RestCommitAcquireLock()) {
+            return Status::Invalid(fmt::format(
+                "'{}' needs a commit through a REST catalog with 
WithCatalog(); a commit table "
+                "request built for the caller to send cannot hold a commit 
lease",
+                Options::REST_COMMIT_ACQUIRE_LOCK));
+        }
+        return std::shared_ptr<SnapshotCommit>(
+            std::make_shared<CatalogSnapshotCommit>(ctx.GetTableId()));
+    }
+    // Catalogs without version management may still supply a table ID.
+    if (ctx.GetTableId() && ctx.GetCatalog() == nullptr) {
+        return Status::Invalid(
+            "a commit which publishes the snapshot by writing it to the table 
directory sends no "
+            "commit table request, so there is nowhere to put a table id; 
commit through a "
+            "catalog with WithCatalog() or ask for the request with 
UseRESTCatalogCommit()");
+    }
+    return std::shared_ptr<SnapshotCommit>(
+        std::make_shared<RenamingSnapshotCommit>(options.GetFileSystem(), 
snapshot_manager));
+}
+
+}  // namespace
+
+class Executor;
+class MemoryPool;
+
+FileStoreTable::FileStoreTable(const std::shared_ptr<FileSystem>& file_system,
+                               const std::string& root_path,
+                               const std::shared_ptr<TableSchema>& schema,
+                               const Identifier& identifier, const 
std::string& uuid,
+                               const std::map<std::string, std::string>& 
options)
+    : Table(file_system, root_path, schema, identifier, uuid), 
options_(options) {}
+
+Result<std::shared_ptr<FileStoreTable>> FileStoreTable::Create(
+    const std::shared_ptr<FileSystem>& specified_file_system, const 
std::string& root_path,
+    const std::shared_ptr<TableSchema>& schema, const Identifier& identifier,
+    const std::string& uuid, const std::string& branch,
+    const std::map<std::string, std::string>& fs_scheme_map) {
+    if (schema == nullptr) {
+        return Status::Invalid(
+            fmt::format("not found latest schema of {}", 
identifier.GetFullName()));
+    }
+    PAIMON_RETURN_NOT_OK(
+        CatalogUtils::CheckManagedTableType(identifier, schema, 
"FileStoreTable::Create"));
+    PAIMON_RETURN_NOT_OK(BranchManager::CheckValidBranch(branch));
+    std::map<std::string, std::string> options = schema->Options();
+    const std::string normalized_branch = 
BranchManager::NormalizeBranch(branch);
+    if (BranchManager::IsMainBranch(normalized_branch)) {
+        options.erase(Options::BRANCH);
+    } else {
+        options[Options::BRANCH] = normalized_branch;
+    }
+    std::shared_ptr<TableSchema> effective_schema = schema;
+    if (options != schema->Options()) {
+        PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<TableSchema> normalized_schema,
+                               schema->Copy(options));
+        effective_schema = 
std::shared_ptr<TableSchema>(std::move(normalized_schema));
+    }
+    return CreateWithOptions(specified_file_system, root_path, 
effective_schema, identifier, uuid,
+                             options, fs_scheme_map);
+}
+
+Result<std::shared_ptr<FileStoreTable>> FileStoreTable::Create(
+    std::unique_ptr<FileStore> store, const std::shared_ptr<TableSchema>& 
schema,
+    const Identifier& identifier, const std::string& uuid,
+    const std::map<std::string, std::string>& fs_scheme_map) {
+    if (!store || !schema) {
+        return Status::Invalid("managed table requires a store and schema");
+    }
+    PAIMON_RETURN_NOT_OK(
+        CatalogUtils::CheckManagedTableType(identifier, schema, 
"FileStoreTable::Create"));
+    
PAIMON_RETURN_NOT_OK(BranchManager::CheckValidBranch(store->CurrentBranch()));
+    return CreateWithOptions(store->GetFileSystem(), store->GetRootPath(), 
schema, identifier, uuid,
+                             store->GetOptions().ToMap(), fs_scheme_map);
+}
+
+Result<std::shared_ptr<FileStoreTable>> FileStoreTable::CreateWithOptions(
+    const std::shared_ptr<FileSystem>& specified_file_system, const 
std::string& root_path,
+    const std::shared_ptr<TableSchema>& schema, const Identifier& identifier,
+    const std::string& uuid, const std::map<std::string, std::string>& options,
+    const std::map<std::string, std::string>& fs_scheme_map) {
+    // Loading metadata must not require the data-format plugins used by 
operators.
+    std::shared_ptr<FileSystem> file_system = specified_file_system;
+    if (!file_system) {
+        PAIMON_ASSIGN_OR_RAISE(std::string default_fs, 
OptionsUtils::GetValueFromMap<std::string>(
+                                                           options, 
Options::FILE_SYSTEM, "local"));
+        file_system = std::make_shared<ResolvingFileSystem>(
+            fs_scheme_map, StringUtils::ToLowerCase(default_fs), options);
+    }
+    std::shared_ptr<FileStoreTable> table;
+    if (!schema->PrimaryKeys().empty()) {
+        table = std::make_shared<PrimaryKeyFileStoreTable>(file_system, 
root_path, schema,
+                                                           identifier, uuid, 
options);
+    } else {
+        table = std::make_shared<AppendOnlyFileStoreTable>(file_system, 
root_path, schema,
+                                                           identifier, uuid, 
options);
+    }
+    table->fs_scheme_map_ = fs_scheme_map;
+    return table;
+}
+
+Result<CoreOptions> FileStoreTable::ResolveOptions(
+    const std::map<std::string, std::string>& overrides) const {
+    auto options = options_;
+    for (const auto& [key, value] : overrides) {
+        options[key] = value;
+    }
+    auto branch = overrides.find(Options::BRANCH);
+    if (branch != overrides.end() &&
+        BranchManager::NormalizeBranch(branch->second) != CurrentBranch()) {
+        return Status::Invalid("context branch contradicts the loaded table");
+    }
+    PAIMON_ASSIGN_OR_RAISE(TableType type, 
TableTypeDefine::FromOptions(options));
+    PAIMON_ASSIGN_OR_RAISE(TableType bound_type, 
TableTypeDefine::FromOptions(options_));
+    if (type != bound_type) {
+        return Status::Invalid("context type contradicts the loaded table");
+    }
+    return CoreOptions::FromMap(options, file_system_, fs_scheme_map_);
+}
+
+Status FileStoreTable::RestoreRealtimeCommittedProgress(
+    const std::shared_ptr<RealtimeContext>& realtime_context,
+    const std::shared_ptr<SnapshotManager>& snapshot_manager, const 
CoreOptions& options) {
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<RealtimeContextImpl> 
realtime_context_impl,
+                           RealtimeContextImpl::Cast(realtime_context));
+    PAIMON_RETURN_NOT_OK(realtime_context_impl->CheckUsable());
+    PAIMON_ASSIGN_OR_RAISE(std::optional<Snapshot> latest_snapshot,
+                           snapshot_manager->LatestSnapshot());
+    if (latest_snapshot) {
+        PAIMON_ASSIGN_OR_RAISE(
+            RealtimeOffsetMap realtime_committed_offsets,
+            RealtimeCommitProperties::ReadOffsets(latest_snapshot, 
options.GetFileSystem()));
+        PAIMON_RETURN_NOT_OK(realtime_context_impl->AdvanceCommittedProgress(
+            latest_snapshot->Id(), realtime_committed_offsets, 
/*committed_data_files=*/{}));
+    }
+    return Status::OK();
+}
+
+Status FileStoreTable::CheckContextNamesThisTable(
+    const std::string& kind, const std::string& context_root_path,
+    const std::optional<Identifier>& context_identifier,
+    const std::optional<std::string>& context_table_id) const {
+    if (!root_path_.empty()) {
+        PAIMON_ASSIGN_OR_RAISE(std::string context_path,
+                               PathUtil::NormalizePath(context_root_path));
+        PAIMON_ASSIGN_OR_RAISE(std::string table_path, 
PathUtil::NormalizePath(root_path_));
+        if (context_path != table_path) {
+            return Status::Invalid(fmt::format(
+                "the {} context names root path '{}' but this table serves 
'{}': the store below "
+                "reads and writes this table's files, so a context naming 
another path would "
+                "leave this table's metadata there and publish it as the 
other's",
+                kind, context_root_path, root_path_));
+        }
+    }
+    if (context_identifier) {
+        PAIMON_ASSIGN_OR_RAISE(std::optional<std::string> context_branch,
+                               context_identifier.value().GetBranchName());
+        if (context_branch) {
+            PAIMON_ASSIGN_OR_RAISE(context_branch,
+                                   
context_identifier.value().GetBranchNameOrDefault());
+        }
+        if (context_branch && context_branch.value() != CurrentBranch()) {
+            return Status::Invalid(fmt::format(
+                "the {} context names branch '{}' of '{}' but this table 
serves branch '{}', "
+                "which is the one a {} of this table writes and publishes",
+                kind, context_branch.value(), 
context_identifier.value().ToString(),
+                CurrentBranch(), kind));
+        }
+        PAIMON_ASSIGN_OR_RAISE(
+            Identifier context_table,
+            CatalogUtils::BranchIdentifier(context_identifier.value(), 
CurrentBranch()));
+        PAIMON_ASSIGN_OR_RAISE(Identifier served_table,
+                               CatalogUtils::BranchIdentifier(identifier_, 
CurrentBranch()));
+        const bool names_this_table =
+            (served_table.GetDatabaseName() == Identifier::kUnknownDatabase ||
+             context_table.GetDatabaseName() == 
served_table.GetDatabaseName()) &&
+            context_table.GetObjectName() == served_table.GetObjectName();
+        if (!names_this_table) {
+            return Status::Invalid(fmt::format(
+                "the {} context names table '{}' but this table is '{}': the 
store below serves "
+                "this table's files, so a context naming another table would 
write this table's "
+                "metadata and publish it as the other's",
+                kind, context_identifier.value().ToString(), 
identifier_.ToString()));
+        }
+    }
+    if (context_table_id && context_table_id != CatalogUuid()) {
+        return Status::Invalid(fmt::format(
+            "the {} context carries table id '{}' but this table's is '{}': 
the id tells a "
+            "recreated table from the one the changes were prepared for, so 
one this table does "
+            "not carry would publish this table's metadata as the 
recreation's",
+            kind, context_table_id.value(), CatalogUuid().value_or("none")));
+    }
+    return Status::OK();
+}
+
+std::unique_ptr<FileStoreScan> 
FileStoreTable::WithTablePath(std::unique_ptr<FileStoreScan>&& scan,
+                                                             const 
std::string& root_path) {
+    scan->WithTablePath(root_path);
+    return std::move(scan);
+}
+
+Result<std::unique_ptr<FileStoreWrite>> FileStoreTable::NewWrite(
+    std::unique_ptr<WriteContext> ctx) const {
+    PAIMON_RETURN_NOT_OK(ContextUtils::CheckOperatorContext(ctx, "write"));
+    PAIMON_RETURN_NOT_OK(CheckContextNamesThisTable("write", 
ctx->GetRootPath(),
+                                                    ctx->GetIdentifier(),
+                                                    
/*context_table_id=*/std::nullopt));
+    if (AsVersionManaged(ctx->GetCatalog()) != nullptr) {
+        
PAIMON_RETURN_NOT_OK(BranchManager::CheckCatalogAddressableBranch(CurrentBranch()));
+    }
+    PAIMON_RETURN_NOT_OK(CheckVersionManagementImplemented(ctx->GetCatalog()));
+    std::shared_ptr<FileSystem> specific_fs = ctx->GetSpecificFileSystem();
+    if (specific_fs == nullptr && 
ctx->GetFileSystemSchemeToIdentifierMap().empty() &&
+        ctx->GetCatalog() != nullptr && !ctx->GetIdentifier()) {
+        return Status::Invalid("a catalog write requires a table identifier");
+    }
+    std::optional<Identifier> catalog_identifier;
+    if (ctx->GetIdentifier()) {
+        PAIMON_ASSIGN_OR_RAISE(Identifier identifier, 
CatalogUtils::BranchIdentifier(
+                                                          
*ctx->GetIdentifier(), CurrentBranch()));
+        catalog_identifier.emplace(std::move(identifier));
+    }
+    if (ctx->GetRealtimeContext()) {
+        PAIMON_RETURN_NOT_OK(RealtimeUtils::ValidateBranch(CurrentBranch()));
+    }
+    std::shared_ptr<TableSchema> schema = 
std::static_pointer_cast<TableSchema>(schema_);

Review Comment:
   `docs/code-style.md` (Checked Class-Pointer Casts) requires 
`checked_pointer_cast` instead of `std::static_pointer_cast`. The same 
`std::static_pointer_cast<TableSchema>(schema_)` also appears at lines 441, 568 
and 636 here, and in `append_only_file_store_table.cpp` and 
`primary_key_file_store_table.cpp`.



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