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


##########
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),
+                ), // Overwritten immediately by add_default_partition_spec
+                last_partition_id: UNPARTITIONED_LAST_ASSIGNED_ID,
+                properties: HashMap::new(),
+                current_snapshot_id: None,
+                snapshots: HashMap::new(),
+                snapshot_log: vec![],
+                sort_orders: HashMap::new(),
+                metadata_log: vec![],
+                default_sort_order_id: -1, // Overwritten immediately by 
add_default_sort_order
+                refs: HashMap::default(),
+            },
+            changes: vec![],
+            last_added_schema_id: Some(schema_id),
+            last_added_spec_id: None,
+            last_added_order_id: None,
+            previous_history_entry: None,
+        };
+
+        builder
+            .set_location(location)
+            .add_current_schema(fresh_schema)?
+            .add_default_partition_spec(fresh_spec.into_unbound())?
+            .add_default_sort_order(fresh_sort_order)?
+            .set_properties(properties)
+    }
+
+    /// Creates a new table metadata builder from the given metadata to modify 
it.
+
+    /// `current_file_location` is the location where the current version
+    /// of the metadata file is stored. This is used to update the metadata 
log.
+    /// If `current_file_location` is `None`, the metadata log will not be 
updated.
+    /// This should only be used to stage-create tables.
+    #[must_use]
+    pub fn new_from_metadata(
+        previous: TableMetadata,
+        previous_file_location: Option<String>,
+    ) -> Self {
+        Self {
+            previous_history_entry: previous_file_location.map(|l| MetadataLog 
{
+                metadata_file: l,
+                timestamp_ms: previous.last_updated_ms,
+            }),
+            metadata: previous,
+            changes: Vec::default(),
+            last_added_schema_id: None,
+            last_added_spec_id: None,
+            last_added_order_id: None,
+        }
+    }
+
+    /// Creates a new table metadata builder from the given table creation.
+    pub fn from_table_creation(table_creation: TableCreation) -> Result<Self> {
+        let TableCreation {
+            name: _,
+            location,
+            schema,
+            partition_spec,
+            sort_order,
+            properties,
+        } = table_creation;
+
+        let location = location.ok_or_else(|| {
+            Error::new(
+                ErrorKind::DataInvalid,
+                "Can't create table without location",
+            )
+        })?;
+        let partition_spec = partition_spec.unwrap_or(UnboundPartitionSpec {
+            spec_id: None,

Review Comment:
   Why would 0 be better than `None` here? My reasoning is that users shouldn't 
think much about where to start counting specs. That's why the `new` function a 
few lines below takes care of setting it.
   If we would set it here, we would have a second place where we hard code 
where the numbering of `spec_id` should start.
   
   Currently the only place this is coded is `reassign_ids` in the `new` 
function. So whatever we set the `spec_id` to here, it would be thrown away in 
`new` anyway. Hence I chose `None`.
   
   Does that make sense :D ?



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