WZhuo commented on code in PR #416:
URL: https://github.com/apache/iceberg-cpp/pull/416#discussion_r2647975715


##########
src/iceberg/table_metadata.h:
##########
@@ -124,6 +124,12 @@ struct ICEBERG_EXPORT TableMetadata {
   /// A `long` higher than all assigned row IDs
   int64_t next_row_id;
 
+  static Result<std::unique_ptr<TableMetadata>> Make(
+      const iceberg::Schema& schema, const iceberg::PartitionSpec& spec,

Review Comment:
   1. Rename the function name like `GetSchema`.
   2. use `class` keyword replace of the namespace specified, like `const class 
Schema& schema, const class PartitionSpec& spec`.
   I prefer solution 2.
   



##########
src/iceberg/table_metadata.cc:
##########
@@ -65,6 +68,106 @@ std::string ToString(const MetadataLogEntry& entry) {
                      entry.metadata_file);
 }
 
+Result<std::shared_ptr<PartitionSpec>> FreshPartitionSpec(int32_t spec_id,
+                                                          const Schema& 
fresh_schema,
+                                                          const Schema& 
base_schema,
+                                                          const PartitionSpec& 
spec) {
+  int32_t last_assigned_field_id = -1;
+  std::vector<PartitionField> partition_fields;
+  for (auto& field : spec.fields()) {
+    ICEBERG_ASSIGN_OR_RAISE(auto source_name,
+                            base_schema.FindColumnNameById(field.field_id()));
+    if (!source_name.has_value()) [[unlikely]] {
+      return InvalidSchema("Partition field id {} does not exist in the 
schema",
+                           field.field_id());
+    }
+    ICEBERG_ASSIGN_OR_RAISE(auto fresh_field,
+                            fresh_schema.FindFieldByName(source_name.value()));
+    if (!fresh_field.has_value()) [[unlikely]] {
+      return InvalidSchema("Partition field {} does not exist in the schema",
+                           source_name.value());
+    }
+    partition_fields.emplace_back(
+        fresh_field.value().get().field_id(), ++last_assigned_field_id,
+        std::string(fresh_field.value().get().name()), field.transform());
+  }
+  return PartitionSpec::Make(fresh_schema, spec_id, 
std::move(partition_fields), false,
+                             last_assigned_field_id);
+}
+
+Result<std::shared_ptr<SortOrder>> FreshSortOrder(int32_t order_id, const 
Schema& schema,
+                                                  const SortOrder& order) {
+  if (order.is_unsorted()) {
+    return SortOrder::Unsorted();
+  }
+
+  std::vector<SortField> fresh_fields;
+  for (const auto& field : order.fields()) {
+    ICEBERG_ASSIGN_OR_RAISE(auto source_name,
+                            schema.FindColumnNameById(field.source_id()));
+    if (!source_name.has_value()) {
+      return InvalidSchema("Unable to find source field with ID {} in the old 
schema",
+                           field.source_id());
+    }
+
+    ICEBERG_ASSIGN_OR_RAISE(auto fresh_field,
+                            schema.FindFieldByName(source_name.value()));
+    if (!fresh_field.has_value()) {
+      return InvalidSchema("Unable to find field '{}' in the new schema",
+                           source_name.value());
+    }
+
+    int32_t new_source_id = fresh_field.value().get().field_id();
+    fresh_fields.emplace_back(new_source_id, field.transform(), 
field.direction(),
+                              field.null_order());
+  }
+
+  return SortOrder::Make(order_id, std::move(fresh_fields));
+}
+
+Result<std::unique_ptr<TableMetadata>> TableMetadata::Make(
+    const iceberg::Schema& schema, const iceberg::PartitionSpec& spec,
+    const iceberg::SortOrder& sort_order, const std::string& location,
+    const std::unordered_map<std::string, std::string>& properties, int 
format_version) {
+  for (const auto& [key, _] : properties) {
+    if (TableProperties::reserved_properties().contains(key)) {
+      return InvalidArgument(
+          "Table properties should not contain reserved properties, but got 
{}", key);
+    }
+  }
+
+  // Reassign all column ids to ensure consistency
+  std::atomic<int32_t> last_column_id = 0;

Review Comment:
   Yeah, It's convert from java, I think it's not really needed, fix 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: [email protected]

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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to