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


##########
src/paimon/core/table/file_store_table_factory.cpp:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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_factory.h"
+
+#include <map>
+#include <memory>
+#include <optional>
+#include <string>
+#include <utility>
+
+#include "fmt/format.h"
+#include "paimon/catalog/catalog.h"
+#include "paimon/catalog/identifier.h"
+#include "paimon/catalog/table.h"
+#include "paimon/commit_context.h"
+#include "paimon/core/catalog/catalog_utils.h"
+#include "paimon/core/catalog/version_managed_catalog.h"
+#include "paimon/core/core_options.h"
+#include "paimon/core/options/table_type.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/table/file_store_table.h"
+#include "paimon/core/table/system/system_table.h"
+#include "paimon/core/utils/branch_manager.h"
+#include "paimon/defs.h"
+#include "paimon/fs/file_system.h"
+#include "paimon/read_context.h"
+#include "paimon/result.h"
+#include "paimon/scan_context.h"
+#include "paimon/schema/schema.h"
+#include "paimon/status.h"
+#include "paimon/write_context.h"
+
+namespace paimon {
+
+namespace {
+
+Result<std::shared_ptr<Table>> ManagedTableOf(
+    const std::shared_ptr<FileSystem>& file_system,
+    const std::shared_ptr<FileSystem>& explicit_file_system, const 
std::string& path,
+    const std::string& branch, const std::map<std::string, std::string>& 
options,
+    const std::optional<std::string>& schema_json, const 
std::optional<Identifier>& identifier,
+    const std::string& uuid, const std::map<std::string, std::string>& 
fs_scheme_map) {
+    SchemaManager schema_manager(file_system, path, branch);
+    std::shared_ptr<TableSchema> latest_schema;
+    if (branch == BranchManager::DEFAULT_MAIN_BRANCH && schema_json) {
+        PAIMON_ASSIGN_OR_RAISE(latest_schema, 
TableSchema::CreateFromJson(schema_json.value()));
+    } else {
+        PAIMON_ASSIGN_OR_RAISE(std::optional<std::shared_ptr<TableSchema>> 
latest,
+                               schema_manager.Latest());
+        if (latest) {
+            latest_schema = *latest;
+        }
+    }
+    if (latest_schema != nullptr) {
+        PAIMON_ASSIGN_OR_RAISE(bool is_format_table,
+                               
TableTypeDefine::IsFormatTable(latest_schema->Options()));
+        if (is_format_table) {
+            return Status::Invalid(fmt::format(
+                "the schema under {} names a format table, which is not loaded 
from a path: a "
+                "catalog hands the table back through GetTable, and its 
operators come from "
+                "table->NewScan/NewRead/NewWrite/NewCommit on it",
+                path));
+        }
+    }
+    Identifier table_identifier =
+        identifier.value_or(CatalogUtils::IdentifierFromPath(path, branch));
+    // Fold the context options into the schema before the store resolves 
them: resolving the
+    // schema's own options first would fail on a format plugin the context 
overrides away,
+    // before the override has a chance to take effect. Only options that 
differ from the
+    // schema's own restyle the load, the way `Copy` decides; the rest leave 
the schema and the
+    // dynamic options as they are.
+    std::shared_ptr<Schema> loaded_schema = latest_schema;
+    std::map<std::string, std::string> dynamic_options;
+    if (latest_schema != nullptr && !options.empty()) {
+        const std::map<std::string, std::string>& schema_options = 
latest_schema->Options();
+        bool options_differ = false;
+        for (const auto& [key, value] : options) {
+            auto schema_option = schema_options.find(key);
+            if (schema_option == schema_options.end() || schema_option->second 
!= value) {
+                options_differ = true;
+                break;
+            }
+        }
+        if (options_differ) {
+            dynamic_options = options;
+            dynamic_options.erase(Options::TYPE);
+            dynamic_options.erase(Options::BRANCH);

Review Comment:
   **[P2] Preserve explicit options that match the main schema.** When every 
context option already matches the main schema, `options_differ` stays false 
and `dynamic_options` remains empty. Those explicit options are then lost when 
constructing the fallback reader.
   
   For example, if the main schema has `key-value.sequence_number.enabled=true` 
and `scan.fallback-branch=fb`, the fallback schema omits the sequence-number 
setting, and the read context explicitly sets it to `true` while projecting 
`_SEQUENCE_NUMBER`, `TableRead::Create()` fails when the fallback treats that 
column as an unknown schema field. Previously, context options were merged into 
both branches.
   
   Please preserve the supplied dynamic options even when the main schema needs 
no merge, and add a regression test for this matching-value case.



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