c-thiel commented on code in PR #587:
URL: https://github.com/apache/iceberg-rust/pull/587#discussion_r1834692436


##########
crates/iceberg/src/spec/table_metadata_builder.rs:
##########
@@ -0,0 +1,2074 @@
+// 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.
+
+use std::collections::{HashMap, HashSet};
+use std::sync::Arc;
+
+use uuid::Uuid;
+
+use super::{
+    BoundPartitionSpec, FormatVersion, MetadataLog, PartitionSpecBuilder, 
Schema, SchemaRef,
+    Snapshot, SnapshotLog, SnapshotReference, SortOrder, SortOrderRef, 
TableMetadata,
+    UnboundPartitionSpec, DEFAULT_PARTITION_SPEC_ID, DEFAULT_SCHEMA_ID, 
MAIN_BRANCH, ONE_MINUTE_MS,
+    PROPERTY_METADATA_PREVIOUS_VERSIONS_MAX, 
PROPERTY_METADATA_PREVIOUS_VERSIONS_MAX_DEFAULT,
+    RESERVED_PROPERTIES, UNPARTITIONED_LAST_ASSIGNED_ID,
+};
+use crate::error::{Error, ErrorKind, Result};
+use crate::{TableCreation, TableUpdate};
+
+const FIRST_FIELD_ID: u32 = 1;
+
+/// Manipulating table metadata.
+///
+/// For this builder the order of called functions matters. Functions are 
applied in-order.
+/// All operations applied to the `TableMetadata` are tracked in `changes` as  
a chronologically
+/// ordered vec of `TableUpdate`.
+/// If an operation does not lead to a change of the `TableMetadata`, the 
corresponding update
+/// is omitted from `changes`.
+///
+/// Unlike a typical builder pattern, the order of function calls matters.
+/// Some basic rules:
+/// - `add_schema` must be called before `set_current_schema`.
+/// - If a new partition spec and schema are added, the schema should be added 
first.
+#[derive(Debug, Clone)]
+pub struct TableMetadataBuilder {
+    metadata: TableMetadata,
+    changes: Vec<TableUpdate>,
+    last_added_schema_id: Option<i32>,
+    last_added_spec_id: Option<i32>,
+    last_added_order_id: Option<i64>,
+    // None if this is a new table (from_metadata) method not used
+    previous_history_entry: Option<MetadataLog>,
+}
+
+#[derive(Debug, Clone, PartialEq)]
+/// Result of modifying or creating a `TableMetadata`.
+pub struct TableMetadataBuildResult {
+    /// The new `TableMetadata`.
+    pub metadata: TableMetadata,
+    /// The changes that were applied to the metadata.
+    pub changes: Vec<TableUpdate>,
+    /// Expired metadata logs
+    pub expired_metadata_logs: Vec<MetadataLog>,
+}
+
+impl TableMetadataBuilder {
+    const LAST_ADDED: i32 = -1;
+
+    /// Create a `TableMetadata` object from scratch.
+    ///
+    /// This method re-assign ids of fields in the schema, schema.id, 
sort_order.id and
+    /// spec.id. It should only be used to create new table metadata from 
scratch.
+    pub fn new(
+        schema: Schema,
+        spec: impl Into<UnboundPartitionSpec>,
+        sort_order: SortOrder,
+        location: String,
+        format_version: FormatVersion,
+        properties: HashMap<String, String>,
+    ) -> Result<Self> {
+        // Re-assign field_ids, schema.id, sort_order.id and spec.id for a new 
table.
+        let (fresh_schema, fresh_spec, fresh_sort_order) =
+            Self::reassign_ids(schema, spec.into(), sort_order)?;
+        let schema_id = fresh_schema.schema_id();
+
+        let builder = Self {
+            metadata: TableMetadata {
+                format_version,
+                table_uuid: Uuid::now_v7(),
+                location: "".to_string(), // Overwritten immediately by 
set_location
+                last_sequence_number: 0,
+                last_updated_ms: 0,    // Overwritten by build() if not set 
before
+                last_column_id: -1,    // Overwritten immediately by 
add_current_schema
+                current_schema_id: -1, // Overwritten immediately by 
add_current_schema
+                schemas: HashMap::new(),
+                partition_specs: HashMap::new(),
+                default_spec: Arc::new(
+                    
BoundPartitionSpec::unpartition_spec(fresh_schema.clone()).with_spec_id(-1),

Review Comment:
   This is probably the bit I am least happy with.
   I know that it must be 0 after build. However this value is actually not 
used anywhere. It can be any invalid value - it must be smaller than 0. Short 
answer is because otherwise the changes produced by the builder are invalid.
   
   Let me try to explain:
   At the point you highlighted, we just need to set a placeholder for the 
`default_spec`. This is the price we pay for using `TableMetadata` under the 
hood to store state. We could change this to an almost exact copy of the 
`TableMetadata` too, which would allow us to use None here. 
   
   The user specifies a spec always when using the `new` method. This spec is 
then added using the `add_default_partition_spec` method at the End of the 
`new` function. This always also replaces the `default_spec`, so that the value 
specified as id does almost not matter.
   
   There is just one case that can lead to a problem:
   If the user specifies the unpartitioned spec, then the 
`add_default_partition_spec` would not detect any changes to the current 
default spec, as everything is equal. As a result, it would not produce a 
change. Thus `changes` are invalid.
   
   There is actually a test for it called 
`test_new_metadata_changes_unpartitioned_unsorted` that fails if we specify `0` 
here.
   
   I am adding a comment to the code for our future selfs :)
   
   
   



-- 
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-unsubscr...@iceberg.apache.org

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


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

Reply via email to