You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "gty404 (via GitHub)" <gi...@apache.org> on 2025/10/12 01:55:53 UTC

[PR] feat: implement metadata updates and update requirements [iceberg-cpp]

gty404 opened a new pull request, #257:
URL: https://github.com/apache/iceberg-cpp/pull/257

   This commit introduces the metadata update and update requirements system for table metadata modifications.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2429216468


##########
src/iceberg/table_metadata.h:
##########
@@ -29,6 +29,7 @@
 #include <vector>
 
 #include "iceberg/iceberg_export.h"
+#include "iceberg/metadata_update.h"

Review Comment:
   Use forward declaration and remove this?



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static TableMetadataBuilder BuildFrom(const std::shared_ptr<const TableMetadata>& base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();
+
+  /// \brief Assign a specific UUID to the table
+  ///
+  /// \param uuid The UUID string to assign
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID(const std::string& uuid);
+
+  /// \brief Upgrade the format version of the table
+  ///
+  /// \param new_format_version The new format version (must be >= current version)
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& UpgradeFormatVersion(int8_t new_format_version);
+
+  /// \brief Set the current schema for the table
+  ///
+  /// \param schema The schema to set as current
+  /// \param new_last_column_id The highest column ID in the schema
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetCurrentSchema(std::shared_ptr<iceberg::Schema> schema,

Review Comment:
   ```suggestion
     TableMetadataBuilder& SetCurrentSchema(std::shared_ptr<Schema> schema,
   ```



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(

Review Comment:
   Should we return `std::unique_ptr<TableMetadataBuilder>`? I'm also fine with the current one.



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static TableMetadataBuilder BuildFrom(const std::shared_ptr<const TableMetadata>& base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();
+
+  /// \brief Assign a specific UUID to the table
+  ///
+  /// \param uuid The UUID string to assign
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID(const std::string& uuid);

Review Comment:
   ```suggestion
     TableMetadataBuilder& AssignUUID(std::string_view uuid);
   ```



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static TableMetadataBuilder BuildFrom(const std::shared_ptr<const TableMetadata>& base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();

Review Comment:
   Any specific reason to choose this subset of functions? Should we add all Java equivalents to not miss any?



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static TableMetadataBuilder BuildFrom(const std::shared_ptr<const TableMetadata>& base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();
+
+  /// \brief Assign a specific UUID to the table
+  ///
+  /// \param uuid The UUID string to assign
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID(const std::string& uuid);
+
+  /// \brief Upgrade the format version of the table
+  ///
+  /// \param new_format_version The new format version (must be >= current version)
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& UpgradeFormatVersion(int8_t new_format_version);
+
+  /// \brief Set the current schema for the table
+  ///
+  /// \param schema The schema to set as current
+  /// \param new_last_column_id The highest column ID in the schema
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetCurrentSchema(std::shared_ptr<iceberg::Schema> schema,

Review Comment:
   Same for below to remove redundant namespace prefix.



##########
src/iceberg/table_metadata.cc:
##########
@@ -196,4 +197,229 @@ Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
   return io.WriteFile(location, json_string);
 }
 
+// TableMetadataBuilder implementation
+
+struct TableMetadataBuilder::Impl {
+  // Base metadata (if building from existing metadata)
+  std::shared_ptr<const TableMetadata> base;

Review Comment:
   Is it better to make it `const TableMetadata*` so it can accept broader types?



##########
src/iceberg/table_metadata.cc:
##########
@@ -196,4 +197,229 @@ Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
   return io.WriteFile(location, json_string);
 }
 
+// TableMetadataBuilder implementation
+
+struct TableMetadataBuilder::Impl {
+  // Base metadata (if building from existing metadata)
+  std::shared_ptr<const TableMetadata> base;
+
+  // Mutable fields that will be used to build the final TableMetadata
+  int8_t format_version;
+  std::string table_uuid;
+  std::string location;
+  int64_t last_sequence_number;
+  TimePointMs last_updated_ms;
+  int32_t last_column_id;
+  std::vector<std::shared_ptr<Schema>> schemas;
+  std::optional<int32_t> current_schema_id;
+  std::vector<std::shared_ptr<PartitionSpec>> partition_specs;
+  int32_t default_spec_id;
+  int32_t last_partition_id;
+  std::unordered_map<std::string, std::string> properties;
+  int64_t current_snapshot_id;
+  std::vector<std::shared_ptr<Snapshot>> snapshots;
+  std::vector<SnapshotLogEntry> snapshot_log;
+  std::vector<MetadataLogEntry> metadata_log;
+  std::vector<std::shared_ptr<SortOrder>> sort_orders;
+  int32_t default_sort_order_id;
+  std::unordered_map<std::string, std::shared_ptr<SnapshotRef>> refs;
+  std::vector<std::shared_ptr<StatisticsFile>> statistics;
+  std::vector<std::shared_ptr<PartitionStatisticsFile>> partition_statistics;
+  int64_t next_row_id;
+
+  // List of changes (MetadataUpdate objects)
+  std::vector<std::unique_ptr<MetadataUpdate>> changes;
+
+  explicit Impl(int8_t fmt_version)
+      : format_version(fmt_version),
+        last_sequence_number(TableMetadata::kInitialSequenceNumber),

Review Comment:
   Should we use in-place initialization?



##########
src/iceberg/metadata_update.h:
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+/// \file iceberg/metadata_update.h
+/// Metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+class TableMetadataBuilder;
+class UpdateRequirementsContext;
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT MetadataUpdate {
+ public:
+  virtual ~MetadataUpdate() = default;
+
+  /// \brief Clone this metadata update
+  virtual std::unique_ptr<MetadataUpdate> Clone() const = 0;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  virtual void GenerateRequirements(UpdateRequirementsContext& context) const = 0;
+};
+
+/// \brief Represents an assignment of a UUID to the table
+class ICEBERG_EXPORT AssignUUID : public MetadataUpdate {
+ public:
+  explicit AssignUUID(std::string uuid) : uuid_(std::move(uuid)) {}
+
+  const std::string& uuid() const { return uuid_; }
+
+  std::unique_ptr<MetadataUpdate> Clone() const override {
+    return std::make_unique<AssignUUID>(uuid_);
+  }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  void GenerateRequirements(UpdateRequirementsContext& context) const override;
+
+ private:
+  std::string uuid_;
+};
+
+/// \brief Represents an upgrade of the table format version
+class ICEBERG_EXPORT UpgradeFormatVersion : public MetadataUpdate {
+ public:
+  explicit UpgradeFormatVersion(int8_t format_version)
+      : format_version_(format_version) {}
+
+  int8_t format_version() const { return format_version_; }
+
+  std::unique_ptr<MetadataUpdate> Clone() const override {
+    return std::make_unique<UpgradeFormatVersion>(format_version_);
+  }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  void GenerateRequirements(UpdateRequirementsContext& context) const override;
+
+ private:
+  int8_t format_version_;
+};
+
+/// \brief Represents adding a new schema to the table
+class ICEBERG_EXPORT AddSchema : public MetadataUpdate {
+ public:
+  explicit AddSchema(std::shared_ptr<iceberg::Schema> schema, int32_t last_column_id)

Review Comment:
   ```suggestion
     explicit AddSchema(std::shared_ptr<Schema> schema, int32_t last_column_id)
   ```
   
   Remove redundant namespace prefix. Same for below.



##########
src/iceberg/table_metadata.cc:
##########
@@ -196,4 +197,229 @@ Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
   return io.WriteFile(location, json_string);
 }
 
+// TableMetadataBuilder implementation
+
+struct TableMetadataBuilder::Impl {
+  // Base metadata (if building from existing metadata)
+  std::shared_ptr<const TableMetadata> base;
+
+  // Mutable fields that will be used to build the final TableMetadata
+  int8_t format_version;

Review Comment:
   Should we just use `std::unique_ptr<TableMetadata>` and set everything to it?



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2437994016


##########
src/iceberg/table_update.h:
##########
@@ -0,0 +1,356 @@
+/*
+ * 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
+
+/// \file iceberg/table_update.h
+/// Table metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT TableUpdate {
+ public:
+  virtual ~TableUpdate() = default;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  /// \return Status indicating success or failure with error details
+  virtual Status GenerateRequirements(MetadataUpdateContext& context) const = 0;
+};
+
+/// \brief Represents an assignment of a UUID to the table
+class ICEBERG_EXPORT AssignUUID : public TableUpdate {

Review Comment:
   Will it cause name conflicts once we add `class AssignUUID : public ViewUpdate`? Or do we want to modify it as `class AssignUUID : public TableUpdate, public ViewUpdate` ? If latter, we may need to rename the file to `metadata_update.h/cc`?



##########
src/iceberg/table_requirements.h:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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
+
+/// \file iceberg/update_requirements.h

Review Comment:
   The comment looks inconsistent now.



##########
src/iceberg/CMakeLists.txt:
##########
@@ -47,7 +47,10 @@ set(ICEBERG_SOURCES
     table.cc
     table_metadata.cc
     table_properties.cc
+    table_requirement.cc

Review Comment:
   @gty404 Please add new header and source files to the corresponding `meson.build` file.



##########
src/iceberg/util/string_util.h:
##########
@@ -44,6 +44,11 @@ class ICEBERG_EXPORT StringUtils {
                    [](char c) { return std::toupper(c); });    // NOLINT
     return input;
   }
+
+  static bool EqualsIgnoreCase(const std::string& a, const std::string& b) {

Review Comment:
   +1



##########
src/iceberg/table_requirements.h:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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
+
+/// \file iceberg/update_requirements.h
+/// Factory for generating update requirements from metadata updates.
+///
+/// This utility class generates the appropriate UpdateRequirement instances
+/// based on a list of MetadataUpdate operations. The requirements are used
+/// for optimistic concurrency control when committing table changes.
+
+#include <memory>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/table_requirement.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Context for generating update requirements
+///
+/// This context is passed to each MetadataUpdate's GenerateRequirements method
+/// and maintains state about what requirements have already been added to avoid
+/// duplicates.
+class ICEBERG_EXPORT MetadataUpdateContext {

Review Comment:
   ```suggestion
   class ICEBERG_EXPORT TableUpdateContext {
   ```
   
   Also fix the comments after renaming



##########
src/iceberg/table_metadata.cc:
##########
@@ -196,4 +197,185 @@ Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
   return io.WriteFile(location, json_string);
 }
 
+// TableMetadataBuilder implementation
+
+struct TableMetadataBuilder::Impl {};
+
+TableMetadataBuilder::TableMetadataBuilder(int8_t format_version)
+    : impl_(std::make_unique<Impl>()) {}
+
+TableMetadataBuilder::TableMetadataBuilder(const TableMetadata* base)
+    : impl_(std::make_unique<Impl>()) {}
+
+TableMetadataBuilder::~TableMetadataBuilder() = default;
+
+TableMetadataBuilder::TableMetadataBuilder(TableMetadataBuilder&&) noexcept = default;
+
+TableMetadataBuilder& TableMetadataBuilder::operator=(TableMetadataBuilder&&) noexcept =
+    default;
+
+std::unique_ptr<TableMetadataBuilder> TableMetadataBuilder::BuildFromEmpty(
+    int8_t format_version) {
+  return std::unique_ptr<TableMetadataBuilder>(
+      new TableMetadataBuilder(format_version));  // NOLINT
+}
+
+std::unique_ptr<TableMetadataBuilder> TableMetadataBuilder::BuildFrom(
+    const TableMetadata* base) {
+  return std::unique_ptr<TableMetadataBuilder>(new TableMetadataBuilder(base));  // NOLINT
+}
+
+TableMetadataBuilder& TableMetadataBuilder::SetMetadataLocation(
+    std::string_view metadata_location) {
+  return *this;

Review Comment:
   We should throw for unimplemented functions.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "nullccxsy (via GitHub)" <gi...@apache.org>.
nullccxsy commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2426636987


##########
src/iceberg/util/string_util.h:
##########
@@ -44,6 +44,13 @@ class ICEBERG_EXPORT StringUtils {
                    [](char c) { return std::toupper(c); });    // NOLINT
     return input;
   }
+
+  static bool EqualsIgnoreCase(const std::string& a, const std::string& b) {
+    return a.size() == b.size() &&
+           std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) {

Review Comment:
   There we can use `ToLower()` in `util\string_util.h`



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2425124475


##########
src/iceberg/util/string_util.h:
##########
@@ -44,6 +44,13 @@ class ICEBERG_EXPORT StringUtils {
                    [](char c) { return std::toupper(c); });    // NOLINT
     return input;
   }
+
+  static bool EqualsIgnoreCase(const std::string& a, const std::string& b) {
+    return a.size() == b.size() &&
+           std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) {

Review Comment:
   We can use `std::ranges::equal` here.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2438415762


##########
src/iceberg/table_update.h:
##########
@@ -0,0 +1,358 @@
+/*
+ * 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
+
+/// \file iceberg/table_update.h
+/// Table metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT TableUpdate {
+ public:
+  virtual ~TableUpdate() = default;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  /// \return Status indicating success or failure with error details
+  virtual Status GenerateRequirements(TableUpdateContext& context) const = 0;
+};
+
+/// \brief Represents an assignment of a UUID to the table
+class ICEBERG_EXPORT AssignTableUUID : public TableUpdate {
+ public:
+  explicit AssignTableUUID(std::string uuid) : uuid_(std::move(uuid)) {}
+
+  const std::string& uuid() const { return uuid_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  std::string uuid_;
+};
+
+/// \brief Represents an upgrade of the table format version
+class ICEBERG_EXPORT UpgradeTableFormatVersion : public TableUpdate {
+ public:
+  explicit UpgradeTableFormatVersion(int8_t format_version)
+      : format_version_(format_version) {}
+
+  int8_t format_version() const { return format_version_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  int8_t format_version_;
+};
+
+/// \brief Represents adding a new schema to the table
+class ICEBERG_EXPORT AddTableSchema : public TableUpdate {
+ public:
+  explicit AddTableSchema(std::shared_ptr<Schema> schema, int32_t last_column_id)
+      : schema_(std::move(schema)), last_column_id_(last_column_id) {}
+
+  const std::shared_ptr<Schema>& schema() const { return schema_; }
+
+  int32_t last_column_id() const { return last_column_id_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  std::shared_ptr<Schema> schema_;
+  int32_t last_column_id_;
+};
+
+/// \brief Represents setting the current schema
+class ICEBERG_EXPORT SetCurrentTableSchema : public TableUpdate {

Review Comment:
   These names look a little bit weird. What about this?
   
   ```cpp
   class ICEBERG_EXPORT TableUpdate {
     // ...
   };
   
   namespace table {
   
   class ICEBERG_EXPORT AssignUUID : public TableUpdate {
     // ...
   };
   
   class ICEBERG_EXPORT UpgradeFormatVersion : public TableUpdate {
     // ...
   };
   
   }  // namespace table
   ```



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "gty404 (via GitHub)" <gi...@apache.org>.
gty404 commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2430922304


##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static TableMetadataBuilder BuildFrom(const std::shared_ptr<const TableMetadata>& base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();

Review Comment:
   I have checked the corresponding interface for Java, and all the fields supported in the current C++ have corresponding functions.



##########
src/iceberg/table_metadata.cc:
##########
@@ -196,4 +197,229 @@ Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
   return io.WriteFile(location, json_string);
 }
 
+// TableMetadataBuilder implementation
+
+struct TableMetadataBuilder::Impl {
+  // Base metadata (if building from existing metadata)
+  std::shared_ptr<const TableMetadata> base;
+
+  // Mutable fields that will be used to build the final TableMetadata
+  int8_t format_version;

Review Comment:
   I consider making TableMetadata read-only, once TableMetadata is constructed, it cannot be modified anymore.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "gty404 (via GitHub)" <gi...@apache.org>.
gty404 commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2430926517


##########
src/iceberg/util/string_util.h:
##########
@@ -44,6 +44,13 @@ class ICEBERG_EXPORT StringUtils {
                    [](char c) { return std::toupper(c); });    // NOLINT
     return input;
   }
+
+  static bool EqualsIgnoreCase(const std::string& a, const std::string& b) {
+    return a.size() == b.size() &&
+           std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) {

Review Comment:
   I have changed to use std::ranges::equal



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "zhjwpku (via GitHub)" <gi...@apache.org>.
zhjwpku commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2434396808


##########
src/iceberg/CMakeLists.txt:
##########
@@ -47,7 +47,10 @@ set(ICEBERG_SOURCES
     table.cc
     table_metadata.cc
     table_properties.cc
+    table_requirement.cc

Review Comment:
   I guess you should also add these files to meson.build.



##########
src/iceberg/util/string_util.h:
##########
@@ -44,6 +44,11 @@ class ICEBERG_EXPORT StringUtils {
                    [](char c) { return std::toupper(c); });    // NOLINT
     return input;
   }
+
+  static bool EqualsIgnoreCase(const std::string& a, const std::string& b) {

Review Comment:
   nit: We have some `operator==` definitions that use `lhs`/`rhs` as the parameters names, should we follow the same convention here?



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "gty404 (via GitHub)" <gi...@apache.org>.
gty404 commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2432826097


##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static TableMetadataBuilder BuildFrom(const std::shared_ptr<const TableMetadata>& base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();

Review Comment:
   There is a SetLocation similar to it, and I mistakenly thought it was the same method. I am adding a new interface.



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +144,202 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static std::unique_ptr<TableMetadataBuilder> BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static std::unique_ptr<TableMetadataBuilder> BuildFrom(const TableMetadata* base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();
+
+  /// \brief Assign a specific UUID to the table
+  ///
+  /// \param uuid The UUID string to assign
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID(std::string_view uuid);
+
+  /// \brief Upgrade the format version of the table
+  ///
+  /// \param new_format_version The new format version (must be >= current version)
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& UpgradeFormatVersion(int8_t new_format_version);
+
+  /// \brief Set the current schema for the table
+  ///
+  /// \param schema The schema to set as current
+  /// \param new_last_column_id The highest column ID in the schema
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetCurrentSchema(std::shared_ptr<Schema> schema,
+                                         int32_t new_last_column_id);
+
+  /// \brief Set the current schema by schema ID
+  ///
+  /// \param schema_id The ID of the schema to set as current
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetCurrentSchema(int32_t schema_id);
+
+  /// \brief Add a schema to the table
+  ///
+  /// \param schema The schema to add
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AddSchema(std::shared_ptr<Schema> schema);
+
+  /// \brief Set the default partition spec for the table
+  ///
+  /// \param spec The partition spec to set as default
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetDefaultPartitionSpec(std::shared_ptr<PartitionSpec> spec);

Review Comment:
   I prefer the third option



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2431252167


##########
src/iceberg/table_metadata.cc:
##########
@@ -196,4 +198,222 @@ Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
   return io.WriteFile(location, json_string);
 }
 
+// TableMetadataBuilder implementation
+
+struct TableMetadataBuilder::Impl {
+  // Base metadata (if building from existing metadata)
+  const TableMetadata* base{nullptr};
+
+  // Mutable fields that will be used to build the final TableMetadata
+  int8_t format_version;
+  std::string table_uuid;
+  std::string location;
+  int64_t last_sequence_number{TableMetadata::kInitialSequenceNumber};
+  TimePointMs last_updated_ms{std::chrono::milliseconds(0)};
+  int32_t last_column_id{0};
+  std::vector<std::shared_ptr<Schema>> schemas;
+  std::optional<int32_t> current_schema_id;
+  std::vector<std::shared_ptr<PartitionSpec>> partition_specs;
+  int32_t default_spec_id{0};

Review Comment:
   Do we have predefined constants for this and below? For example, `PartitionSpec::kInitialSpecId`?



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +144,202 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static std::unique_ptr<TableMetadataBuilder> BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static std::unique_ptr<TableMetadataBuilder> BuildFrom(const TableMetadata* base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();
+
+  /// \brief Assign a specific UUID to the table
+  ///
+  /// \param uuid The UUID string to assign
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID(std::string_view uuid);
+
+  /// \brief Upgrade the format version of the table
+  ///
+  /// \param new_format_version The new format version (must be >= current version)
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& UpgradeFormatVersion(int8_t new_format_version);
+
+  /// \brief Set the current schema for the table
+  ///
+  /// \param schema The schema to set as current
+  /// \param new_last_column_id The highest column ID in the schema
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetCurrentSchema(std::shared_ptr<Schema> schema,
+                                         int32_t new_last_column_id);
+
+  /// \brief Set the current schema by schema ID
+  ///
+  /// \param schema_id The ID of the schema to set as current
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetCurrentSchema(int32_t schema_id);
+
+  /// \brief Add a schema to the table
+  ///
+  /// \param schema The schema to add
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AddSchema(std::shared_ptr<Schema> schema);
+
+  /// \brief Set the default partition spec for the table
+  ///
+  /// \param spec The partition spec to set as default
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& SetDefaultPartitionSpec(std::shared_ptr<PartitionSpec> spec);

Review Comment:
   We cannot throw from these setters. How do you plan to deal with invalid inputs?
   
   1. Change the setters to return `Result<std::reference_wrapper<TableMetadataBuilder>>`, but this makes return value chaining unappealing.
   2. Change the setters to return `Status`.
   3. Postpone any error reporting. Add a state to record most recent error and report it in the `Build` call.
   4. ...



##########
src/iceberg/metadata_update.h:
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+/// \file iceberg/metadata_update.h
+/// Metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+class TableMetadataBuilder;
+class UpdateRequirementsContext;

Review Comment:
   ```suggestion
   ```
   
   They are already declared by type_fwd.h



##########
src/iceberg/update_requirement.h:
##########
@@ -0,0 +1,220 @@
+/*
+ * 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
+
+/// \file iceberg/update_requirement.h
+/// Update requirements for Iceberg table operations.
+///
+/// Update requirements are conditions that must be satisfied before
+/// applying metadata updates to a table. They are used for optimistic
+/// concurrency control in table operations.
+
+#include <memory>
+#include <optional>
+#include <string>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Base class for update requirement operations
+///
+/// Represents a requirement that must be validated before applying
+/// metadata updates to a table. Each concrete subclass represents
+/// a specific type of requirement check.
+class ICEBERG_EXPORT UpdateRequirement {
+ public:
+  virtual ~UpdateRequirement() = default;
+
+  /// \brief Clone this update requirement
+  virtual std::unique_ptr<UpdateRequirement> Clone() const = 0;

Review Comment:
   Is it actually used? If not, remove it for now?



##########
src/iceberg/update_requirement.cc:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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 "iceberg/update_requirement.h"
+
+#include "iceberg/snapshot.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/util/string_util.h"
+
+namespace iceberg {
+
+Status AssertTableDoesNotExist::Validate(const TableMetadata* base) const {

Review Comment:
   I haven't reviewed all the function definitions yet but I strongly recommend to leave implementation out in this PR. We can add individual followup PRs to make it easier to review. Let's focus on the interface design here.



##########
src/iceberg/table_metadata.cc:
##########
@@ -196,4 +198,222 @@ Status TableMetadataUtil::Write(FileIO& io, const std::string& location,
   return io.WriteFile(location, json_string);
 }
 
+// TableMetadataBuilder implementation
+
+struct TableMetadataBuilder::Impl {
+  // Base metadata (if building from existing metadata)
+  const TableMetadata* base{nullptr};
+
+  // Mutable fields that will be used to build the final TableMetadata
+  int8_t format_version;
+  std::string table_uuid;
+  std::string location;
+  int64_t last_sequence_number{TableMetadata::kInitialSequenceNumber};
+  TimePointMs last_updated_ms{std::chrono::milliseconds(0)};
+  int32_t last_column_id{0};
+  std::vector<std::shared_ptr<Schema>> schemas;
+  std::optional<int32_t> current_schema_id;
+  std::vector<std::shared_ptr<PartitionSpec>> partition_specs;
+  int32_t default_spec_id{0};
+  int32_t last_partition_id{0};
+  std::unordered_map<std::string, std::string> properties;
+  int64_t current_snapshot_id{-1};
+  std::vector<std::shared_ptr<Snapshot>> snapshots;
+  std::vector<SnapshotLogEntry> snapshot_log;
+  std::vector<MetadataLogEntry> metadata_log;
+  std::vector<std::shared_ptr<SortOrder>> sort_orders;
+  int32_t default_sort_order_id{0};
+  std::unordered_map<std::string, std::shared_ptr<SnapshotRef>> refs;
+  std::vector<std::shared_ptr<StatisticsFile>> statistics;
+  std::vector<std::shared_ptr<PartitionStatisticsFile>> partition_statistics;
+  int64_t next_row_id{TableMetadata::kInitialRowId};
+
+  // List of changes (MetadataUpdate objects)
+  std::vector<std::unique_ptr<MetadataUpdate>> changes;
+
+  explicit Impl(int8_t fmt_version) : format_version(fmt_version) {}
+
+  explicit Impl(const TableMetadata* base_metadata)
+      : base(base_metadata),

Review Comment:
   What if `base_metadata` is nullptr?



##########
src/iceberg/update_requirements.h:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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
+
+/// \file iceberg/update_requirements.h
+/// Factory for generating update requirements from metadata updates.
+///
+/// This utility class generates the appropriate UpdateRequirement instances
+/// based on a list of MetadataUpdate operations. The requirements are used
+/// for optimistic concurrency control when committing table changes.
+
+#include <memory>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update_requirement.h"
+
+namespace iceberg {
+
+/// \brief Context for generating update requirements
+///
+/// This context is passed to each MetadataUpdate's GenerateRequirements method
+/// and maintains state about what requirements have already been added to avoid
+/// duplicates.
+class ICEBERG_EXPORT UpdateRequirementsContext {
+ public:
+  /// \brief Construct a context for requirement generation
+  ///
+  /// \param base The base table metadata (may be nullptr for table creation)
+  /// \param is_replace Whether this is a replace operation (more permissive)
+  UpdateRequirementsContext(const TableMetadata* base, bool is_replace)
+      : base_(base), is_replace_(is_replace) {}
+
+  // Delete copy operations (contains unique_ptr members)
+  UpdateRequirementsContext(const UpdateRequirementsContext&) = delete;
+  UpdateRequirementsContext& operator=(const UpdateRequirementsContext&) = delete;
+
+  // Enable move construction only (assignment deleted due to const members)
+  UpdateRequirementsContext(UpdateRequirementsContext&&) noexcept = default;
+
+  /// \brief Add a requirement to the list
+  void AddRequirement(std::unique_ptr<UpdateRequirement> requirement) {
+    requirements_.push_back(std::move(requirement));
+  }
+
+  /// \brief Get the base table metadata
+  const TableMetadata* base() const { return base_; }
+
+  /// \brief Check if this is a replace operation
+  bool is_replace() const { return is_replace_; }
+
+  /// \brief Build and return the list of requirements
+  std::vector<std::unique_ptr<UpdateRequirement>> Build() {

Review Comment:
   How do we handle errors? Should we return a `Result` wrapper?



##########
src/iceberg/metadata_update.h:
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+/// \file iceberg/metadata_update.h
+/// Metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+class TableMetadataBuilder;
+class UpdateRequirementsContext;
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT MetadataUpdate {
+ public:
+  virtual ~MetadataUpdate() = default;
+
+  /// \brief Clone this metadata update
+  virtual std::unique_ptr<MetadataUpdate> Clone() const = 0;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  virtual void GenerateRequirements(UpdateRequirementsContext& context) const = 0;

Review Comment:
   What about renaming `UpdateRequirementsContext` to `MetadataUpdateContext` or `TableMetadataUpdateContext` (same as iceberg-python) ?



##########
src/iceberg/table_metadata.h:
##########
@@ -144,6 +145,203 @@ ICEBERG_EXPORT std::string ToString(const SnapshotLogEntry& entry);
 /// \brief Returns a string representation of a MetadataLogEntry
 ICEBERG_EXPORT std::string ToString(const MetadataLogEntry& entry);
 
+/// \brief Builder class for constructing TableMetadata objects
+///
+/// This builder provides a fluent interface for creating and modifying table metadata.
+/// It supports both creating new tables and building from existing metadata.
+///
+/// Each modification method generates a corresponding MetadataUpdate that is tracked
+/// in a changes list. This allows the builder to maintain a complete history of all
+/// modifications made to the table metadata, which is important for tracking table
+/// evolution and for serialization purposes.
+class ICEBERG_EXPORT TableMetadataBuilder {
+ public:
+  /// \brief Create a builder for a new table
+  ///
+  /// \param format_version The format version for the table
+  /// \return A new TableMetadataBuilder instance
+  static TableMetadataBuilder BuildFromEmpty(
+      int8_t format_version = TableMetadata::kDefaultTableFormatVersion);
+
+  /// \brief Create a builder from existing table metadata
+  ///
+  /// \param base The base table metadata to build from
+  /// \return A new TableMetadataBuilder instance initialized with base metadata
+  static TableMetadataBuilder BuildFrom(const std::shared_ptr<const TableMetadata>& base);
+
+  /// \brief Assign a UUID to the table
+  ///
+  /// If no UUID is provided, a random UUID will be generated.
+  /// \return Reference to this builder for method chaining
+  TableMetadataBuilder& AssignUUID();

Review Comment:
   I just took a quick glance and it seems that `Builder withMetadataLocation(String newMetadataLocation)` is missing?



##########
src/iceberg/metadata_update.h:
##########
@@ -0,0 +1,431 @@
+/*
+ * 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
+
+/// \file iceberg/metadata_update.h
+/// Metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+class TableMetadataBuilder;
+class UpdateRequirementsContext;
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT MetadataUpdate {
+ public:
+  virtual ~MetadataUpdate() = default;
+
+  /// \brief Clone this metadata update
+  virtual std::unique_ptr<MetadataUpdate> Clone() const = 0;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  virtual void GenerateRequirements(UpdateRequirementsContext& context) const = 0;

Review Comment:
   Perhaps like below:
   
   ```
   virtual void AddTo(MetadataUpdateContext& context) const = 0;
   ```



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#issuecomment-3413476684

   Thanks @liurenjie1024 for the input! I think we should stick with the rest catalog api to use `TableUpdate`, `ViewUpdate`, etc.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "gty404 (via GitHub)" <gi...@apache.org>.
gty404 commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2438678487


##########
src/iceberg/table_update.h:
##########
@@ -0,0 +1,358 @@
+/*
+ * 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
+
+/// \file iceberg/table_update.h
+/// Table metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT TableUpdate {
+ public:
+  virtual ~TableUpdate() = default;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  /// \return Status indicating success or failure with error details
+  virtual Status GenerateRequirements(TableUpdateContext& context) const = 0;
+};
+
+/// \brief Represents an assignment of a UUID to the table
+class ICEBERG_EXPORT AssignTableUUID : public TableUpdate {
+ public:
+  explicit AssignTableUUID(std::string uuid) : uuid_(std::move(uuid)) {}
+
+  const std::string& uuid() const { return uuid_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  std::string uuid_;
+};
+
+/// \brief Represents an upgrade of the table format version
+class ICEBERG_EXPORT UpgradeTableFormatVersion : public TableUpdate {
+ public:
+  explicit UpgradeTableFormatVersion(int8_t format_version)
+      : format_version_(format_version) {}
+
+  int8_t format_version() const { return format_version_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  int8_t format_version_;
+};
+
+/// \brief Represents adding a new schema to the table
+class ICEBERG_EXPORT AddTableSchema : public TableUpdate {
+ public:
+  explicit AddTableSchema(std::shared_ptr<Schema> schema, int32_t last_column_id)
+      : schema_(std::move(schema)), last_column_id_(last_column_id) {}
+
+  const std::shared_ptr<Schema>& schema() const { return schema_; }
+
+  int32_t last_column_id() const { return last_column_id_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  std::shared_ptr<Schema> schema_;
+  int32_t last_column_id_;
+};
+
+/// \brief Represents setting the current schema
+class ICEBERG_EXPORT SetCurrentTableSchema : public TableUpdate {

Review Comment:
   Alright, I'll add a namespace.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "liurenjie1024 (via GitHub)" <gi...@apache.org>.
liurenjie1024 commented on PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#issuecomment-3409017255

   > While I know both `iceberg-python` and `iceberg-rust` name them as `TableUpdate` and `TableRequirement`. However, it might looks weird to use `Table` prefix in terms of dealing with updates to `table`, `view` and perhaps the in-discussion `materialized view`.
   > 
   > WDYT? @Fokko @liurenjie1024
   
   Thanks @wgtmac for pinging me. I prefer to add an prefix to improve readability.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2427763534


##########
src/iceberg/util/string_util.h:
##########
@@ -44,6 +44,13 @@ class ICEBERG_EXPORT StringUtils {
                    [](char c) { return std::toupper(c); });    // NOLINT
     return input;
   }
+
+  static bool EqualsIgnoreCase(const std::string& a, const std::string& b) {
+    return a.size() == b.size() &&
+           std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) {

Review Comment:
   `ToLower` will allocate heap memory. I don't think it's a good idea.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2434448827


##########
src/iceberg/CMakeLists.txt:
##########
@@ -47,7 +47,10 @@ set(ICEBERG_SOURCES
     table.cc
     table_metadata.cc
     table_properties.cc
+    table_requirement.cc

Review Comment:
   +1
   
   Perhaps the CI does not fail because the file is not referenced by others.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac merged PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2438419197


##########
src/iceberg/table_update.h:
##########
@@ -0,0 +1,358 @@
+/*
+ * 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
+
+/// \file iceberg/table_update.h
+/// Table metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT TableUpdate {
+ public:
+  virtual ~TableUpdate() = default;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  /// \return Status indicating success or failure with error details
+  virtual Status GenerateRequirements(TableUpdateContext& context) const = 0;
+};
+
+/// \brief Represents an assignment of a UUID to the table
+class ICEBERG_EXPORT AssignTableUUID : public TableUpdate {
+ public:
+  explicit AssignTableUUID(std::string uuid) : uuid_(std::move(uuid)) {}
+
+  const std::string& uuid() const { return uuid_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  std::string uuid_;
+};
+
+/// \brief Represents an upgrade of the table format version
+class ICEBERG_EXPORT UpgradeTableFormatVersion : public TableUpdate {
+ public:
+  explicit UpgradeTableFormatVersion(int8_t format_version)
+      : format_version_(format_version) {}
+
+  int8_t format_version() const { return format_version_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  int8_t format_version_;
+};
+
+/// \brief Represents adding a new schema to the table
+class ICEBERG_EXPORT AddTableSchema : public TableUpdate {
+ public:
+  explicit AddTableSchema(std::shared_ptr<Schema> schema, int32_t last_column_id)
+      : schema_(std::move(schema)), last_column_id_(last_column_id) {}
+
+  const std::shared_ptr<Schema>& schema() const { return schema_; }
+
+  int32_t last_column_id() const { return last_column_id_; }
+
+  void ApplyTo(TableMetadataBuilder& builder) const override;
+
+  Status GenerateRequirements(TableUpdateContext& context) const override;
+
+ private:
+  std::shared_ptr<Schema> schema_;
+  int32_t last_column_id_;
+};
+
+/// \brief Represents setting the current schema
+class ICEBERG_EXPORT SetCurrentTableSchema : public TableUpdate {

Review Comment:
   Same for `TableRequirement`.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#issuecomment-3409008254

   While I know both `iceberg-python` and `iceberg-rust` name them as `TableUpdate` and `TableRequirement`. However, it might looks weird to use `Table` prefix in terms of dealing with updates to `table`, `view` and perhaps the in-discussion `materialized view`.
   
   WDYT? @Fokko @liurenjie1024


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: add metadata updates and requirements interface [iceberg-cpp]

Posted by "gty404 (via GitHub)" <gi...@apache.org>.
gty404 commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2438238764


##########
src/iceberg/table_update.h:
##########
@@ -0,0 +1,356 @@
+/*
+ * 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
+
+/// \file iceberg/table_update.h
+/// Table metadata update operations for Iceberg tables.
+
+#include <memory>
+#include <optional>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Base class for metadata update operations
+///
+/// Represents a change to table metadata. Each concrete subclass
+/// represents a specific type of update operation.
+class ICEBERG_EXPORT TableUpdate {
+ public:
+  virtual ~TableUpdate() = default;
+
+  /// \brief Apply this update to a TableMetadataBuilder
+  ///
+  /// This method modifies the builder by applying the update operation
+  /// it represents. Each subclass implements this to apply its specific
+  /// type of update.
+  ///
+  /// \param builder The builder to apply this update to
+  virtual void ApplyTo(TableMetadataBuilder& builder) const = 0;
+
+  /// \brief Generate update requirements for this metadata update
+  ///
+  /// This method generates the appropriate UpdateRequirement instances
+  /// that must be validated before this update can be applied. The context
+  /// provides information about the base metadata and operation mode.
+  ///
+  /// \param context The context containing base metadata and operation state
+  /// \return Status indicating success or failure with error details
+  virtual Status GenerateRequirements(MetadataUpdateContext& context) const = 0;
+};
+
+/// \brief Represents an assignment of a UUID to the table
+class ICEBERG_EXPORT AssignUUID : public TableUpdate {

Review Comment:
   The View is expected to have a separate ViewMetadata, and the ViewUpdate interface will differ from TableUpdate. Therefore, I plan to add the 'table' keyword to these updates/requirements to distinguish them.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] feat: implement metadata updates and update requirements [iceberg-cpp]

Posted by "HuaHuaY (via GitHub)" <gi...@apache.org>.
HuaHuaY commented on code in PR #257:
URL: https://github.com/apache/iceberg-cpp/pull/257#discussion_r2430994549


##########
src/iceberg/util/string_util.h:
##########
@@ -44,6 +44,12 @@ class ICEBERG_EXPORT StringUtils {
                    [](char c) { return std::toupper(c); });    // NOLINT
     return input;
   }
+
+  static bool EqualsIgnoreCase(const std::string& a, const std::string& b) {
+    return a.size() == b.size() && std::ranges::equal(a, b, [](char ca, char cb) {

Review Comment:
   `std::ranges::equal` will compare length so we don't need to write `a.size() == b.size()`



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org