blackmwk commented on code in PR #2120: URL: https://github.com/apache/iceberg-rust/pull/2120#discussion_r2928848526
########## crates/iceberg/src/transaction/update_schema.rs: ########## @@ -0,0 +1,1200 @@ +// 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 async_trait::async_trait; + +use crate::spec::{ + ListType, Literal, MapType, NestedField, NestedFieldRef, Schema, StructType, Type, +}; +use crate::table::Table; +use crate::transaction::action::{ActionCommit, TransactionAction}; +use crate::{Error, ErrorKind, Result, TableRequirement, TableUpdate}; + +/// Sentinel parent ID representing the table root (top-level columns). +const TABLE_ROOT_ID: i32 = -1; + +/// A pending column addition, recording the parent path and the field to add. +struct PendingAdd { + /// `None` means a root-level addition; `Some("person")` or `Some("person.address")` + /// identifies the nested struct to add the column to. + parent: Option<String>, + /// The field to add. Uses placeholder ID `0` which is auto-assigned at commit time. + field: NestedFieldRef, +} + +/// Schema evolution API modeled after the Java `SchemaUpdate` implementation. +/// +/// This action accumulates schema modifications (column additions and deletions) +/// via builder methods. At commit time, it validates all operations against the +/// current table schema, auto-assigns field IDs from `table.metadata().last_column_id()`, +/// builds a new schema, and emits `AddSchema` + `SetCurrentSchema` updates with a +/// `CurrentSchemaIdMatch` requirement. +/// +/// # Example +/// +/// ```ignore +/// let tx = Transaction::new(&table); +/// let action = tx.update_schema() +/// .add_column("new_col", Type::Primitive(PrimitiveType::Int)) +/// .add_column_to("person", "email", Type::Primitive(PrimitiveType::String)) +/// .delete_column("old_col"); +/// let tx = action.apply(tx).unwrap(); +/// let table = tx.commit(&catalog).await.unwrap(); +/// ``` +pub struct UpdateSchemaAction { + additions: Vec<PendingAdd>, + deletes: Vec<String>, + auto_assign_ids: bool, +} + +impl UpdateSchemaAction { + /// Creates a new empty `UpdateSchemaAction`. + pub(crate) fn new() -> Self { + Self { + additions: Vec::new(), + deletes: Vec::new(), + auto_assign_ids: true, + } + } + + // --- Root-level additions --- + + /// Add a `NestedFieldRef` column to the table root. + pub fn add_field(self, field: NestedFieldRef) -> Self { + self.add_field_internal(None, field) + } + + /// Add an optional column to the table root. + /// + /// The field ID is a placeholder (`0`) and will be auto-assigned at commit time. + pub fn add_column(self, name: impl ToString, field_type: Type) -> Self { + self.add_field(Arc::new(NestedField::optional(0, name, field_type))) Review Comment: We should use a constant rather 0 directly. ########## crates/iceberg/src/transaction/update_schema.rs: ########## @@ -0,0 +1,1200 @@ +// 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 async_trait::async_trait; + +use crate::spec::{ + ListType, Literal, MapType, NestedField, NestedFieldRef, Schema, StructType, Type, +}; +use crate::table::Table; +use crate::transaction::action::{ActionCommit, TransactionAction}; +use crate::{Error, ErrorKind, Result, TableRequirement, TableUpdate}; + +/// Sentinel parent ID representing the table root (top-level columns). +const TABLE_ROOT_ID: i32 = -1; + +/// A pending column addition, recording the parent path and the field to add. +struct PendingAdd { + /// `None` means a root-level addition; `Some("person")` or `Some("person.address")` + /// identifies the nested struct to add the column to. + parent: Option<String>, + /// The field to add. Uses placeholder ID `0` which is auto-assigned at commit time. + field: NestedFieldRef, +} + +/// Schema evolution API modeled after the Java `SchemaUpdate` implementation. +/// +/// This action accumulates schema modifications (column additions and deletions) +/// via builder methods. At commit time, it validates all operations against the +/// current table schema, auto-assigns field IDs from `table.metadata().last_column_id()`, +/// builds a new schema, and emits `AddSchema` + `SetCurrentSchema` updates with a +/// `CurrentSchemaIdMatch` requirement. +/// +/// # Example +/// +/// ```ignore +/// let tx = Transaction::new(&table); +/// let action = tx.update_schema() +/// .add_column("new_col", Type::Primitive(PrimitiveType::Int)) +/// .add_column_to("person", "email", Type::Primitive(PrimitiveType::String)) +/// .delete_column("old_col"); +/// let tx = action.apply(tx).unwrap(); +/// let table = tx.commit(&catalog).await.unwrap(); +/// ``` +pub struct UpdateSchemaAction { + additions: Vec<PendingAdd>, + deletes: Vec<String>, + auto_assign_ids: bool, +} + +impl UpdateSchemaAction { + /// Creates a new empty `UpdateSchemaAction`. + pub(crate) fn new() -> Self { + Self { + additions: Vec::new(), + deletes: Vec::new(), + auto_assign_ids: true, + } + } + + // --- Root-level additions --- + + /// Add a `NestedFieldRef` column to the table root. + pub fn add_field(self, field: NestedFieldRef) -> Self { Review Comment: We should add some comments to explain that the field id is ignored for now. ########## crates/iceberg/src/transaction/update_schema.rs: ########## @@ -0,0 +1,1200 @@ +// 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 async_trait::async_trait; + +use crate::spec::{ + ListType, Literal, MapType, NestedField, NestedFieldRef, Schema, StructType, Type, +}; +use crate::table::Table; +use crate::transaction::action::{ActionCommit, TransactionAction}; +use crate::{Error, ErrorKind, Result, TableRequirement, TableUpdate}; + +/// Sentinel parent ID representing the table root (top-level columns). +const TABLE_ROOT_ID: i32 = -1; + +/// A pending column addition, recording the parent path and the field to add. +struct PendingAdd { + /// `None` means a root-level addition; `Some("person")` or `Some("person.address")` + /// identifies the nested struct to add the column to. + parent: Option<String>, + /// The field to add. Uses placeholder ID `0` which is auto-assigned at commit time. + field: NestedFieldRef, +} + +/// Schema evolution API modeled after the Java `SchemaUpdate` implementation. +/// +/// This action accumulates schema modifications (column additions and deletions) +/// via builder methods. At commit time, it validates all operations against the +/// current table schema, auto-assigns field IDs from `table.metadata().last_column_id()`, +/// builds a new schema, and emits `AddSchema` + `SetCurrentSchema` updates with a +/// `CurrentSchemaIdMatch` requirement. +/// +/// # Example +/// +/// ```ignore +/// let tx = Transaction::new(&table); +/// let action = tx.update_schema() +/// .add_column("new_col", Type::Primitive(PrimitiveType::Int)) +/// .add_column_to("person", "email", Type::Primitive(PrimitiveType::String)) +/// .delete_column("old_col"); +/// let tx = action.apply(tx).unwrap(); +/// let table = tx.commit(&catalog).await.unwrap(); +/// ``` +pub struct UpdateSchemaAction { + additions: Vec<PendingAdd>, + deletes: Vec<String>, + auto_assign_ids: bool, +} + +impl UpdateSchemaAction { + /// Creates a new empty `UpdateSchemaAction`. + pub(crate) fn new() -> Self { + Self { + additions: Vec::new(), + deletes: Vec::new(), + auto_assign_ids: true, + } + } + + // --- Root-level additions --- + + /// Add a `NestedFieldRef` column to the table root. + pub fn add_field(self, field: NestedFieldRef) -> Self { + self.add_field_internal(None, field) + } + + /// Add an optional column to the table root. + /// + /// The field ID is a placeholder (`0`) and will be auto-assigned at commit time. + pub fn add_column(self, name: impl ToString, field_type: Type) -> Self { + self.add_field(Arc::new(NestedField::optional(0, name, field_type))) + } + + /// Add an optional column with a doc string to the table root. + /// + /// The field ID is a placeholder (`0`) and will be auto-assigned at commit time. + pub fn add_column_with_doc( + self, + name: impl ToString, + field_type: Type, + doc: impl ToString, + ) -> Self { + self.add_field(Arc::new( + NestedField::optional(0, name, field_type).with_doc(doc), + )) + } + + /// Add a required column to the table root. + /// + /// An `initial_default` value is required per the Iceberg spec: it is used to populate + /// this field for all records that were written before the field was added. + /// The field ID is a placeholder (`0`) and will be auto-assigned at commit time. + pub fn add_required_column( + self, + name: impl ToString, + field_type: Type, + initial_default: Literal, + ) -> Self { + self.add_field(Arc::new( + NestedField::required(0, name, field_type) + .with_initial_default(initial_default.clone()) + .with_write_default(initial_default), + )) + } + + /// Add a required column with a doc string to the table root. + /// + /// An `initial_default` value is required per the Iceberg spec: it is used to populate + /// this field for all records that were written before the field was added. + /// The field ID is a placeholder (`0`) and will be auto-assigned at commit time. + pub fn add_required_column_with_doc( + self, + name: impl ToString, + field_type: Type, + initial_default: Literal, + doc: impl ToString, + ) -> Self { + self.add_field(Arc::new( + NestedField::required(0, name, field_type) + .with_initial_default(initial_default.clone()) + .with_write_default(initial_default) + .with_doc(doc), + )) + } + + // --- Nested additions --- + + /// Add a `NestedFieldRef` column under a parent struct identified by name. + /// + /// If the parent is a map, the column is added to the map value's struct. + /// If the parent is a list, the column is added to the list element's struct. + pub fn add_field_to(self, parent: impl ToString, field: NestedFieldRef) -> Self { + self.add_field_internal(Some(parent.to_string()), field) + } + + /// Add an optional column under a parent struct identified by name. + /// + /// The `parent` can be a dotted path (e.g. `"person"` or `"person.address"`). + /// If the parent is a map, the column is added to the map value's struct. + /// If the parent is a list, the column is added to the list element's struct. + /// The field ID is a placeholder (`0`) and will be auto-assigned at commit time. + pub fn add_column_to( + self, + parent: impl ToString, + name: impl ToString, + field_type: Type, + ) -> Self { + self.add_field_to(parent, Arc::new(NestedField::optional(0, name, field_type))) + } + + /// Add an optional column with a doc string under a parent struct. + /// + /// See [`add_column_to`](Self::add_column_to) for parent path details. + pub fn add_column_to_with_doc( + self, + parent: impl ToString, + name: impl ToString, + field_type: Type, + doc: impl ToString, + ) -> Self { + self.add_field_to( + parent, + Arc::new(NestedField::optional(0, name, field_type).with_doc(doc)), + ) + } + + /// Add a required column under a parent struct. + /// + /// See [`add_column_to`](Self::add_column_to) for parent path details. + /// An `initial_default` value is required per the Iceberg spec. + pub fn add_required_column_to( + self, + parent: impl ToString, + name: impl ToString, + field_type: Type, + initial_default: Literal, + ) -> Self { + self.add_field_to( + parent, + Arc::new( + NestedField::required(0, name, field_type) + .with_initial_default(initial_default.clone()) + .with_write_default(initial_default), + ), + ) + } + + /// Add a required column with a doc string under a parent struct. + /// + /// See [`add_column_to`](Self::add_column_to) for parent path details. + /// An `initial_default` value is required per the Iceberg spec. + pub fn add_required_column_to_with_doc( + self, + parent: impl ToString, + name: impl ToString, + field_type: Type, + initial_default: Literal, + doc: impl ToString, + ) -> Self { + self.add_field_to( + parent, + Arc::new( + NestedField::required(0, name, field_type) + .with_initial_default(initial_default.clone()) + .with_write_default(initial_default) + .with_doc(doc), + ), + ) + } + + // --- Other builder methods --- + + /// Record a column deletion by name. + /// + /// At commit time, the column must exist in the current schema. + pub fn delete_column(mut self, name: impl ToString) -> Self { + self.deletes.push(name.to_string()); + self + } + + /// Disable automatic field ID assignment. When disabled, the placeholder IDs + /// provided in builder methods are used as-is. + pub fn disable_id_auto_assignment(mut self) -> Self { Review Comment: I don't think we should provide this action, it's quite dangerous. ########## crates/iceberg/src/transaction/update_schema.rs: ########## @@ -0,0 +1,1200 @@ +// 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 async_trait::async_trait; + +use crate::spec::{ + ListType, Literal, MapType, NestedField, NestedFieldRef, Schema, StructType, Type, +}; +use crate::table::Table; +use crate::transaction::action::{ActionCommit, TransactionAction}; +use crate::{Error, ErrorKind, Result, TableRequirement, TableUpdate}; + +/// Sentinel parent ID representing the table root (top-level columns). +const TABLE_ROOT_ID: i32 = -1; + +/// A pending column addition, recording the parent path and the field to add. +struct PendingAdd { + /// `None` means a root-level addition; `Some("person")` or `Some("person.address")` + /// identifies the nested struct to add the column to. + parent: Option<String>, + /// The field to add. Uses placeholder ID `0` which is auto-assigned at commit time. + field: NestedFieldRef, +} + +/// Schema evolution API modeled after the Java `SchemaUpdate` implementation. +/// +/// This action accumulates schema modifications (column additions and deletions) +/// via builder methods. At commit time, it validates all operations against the +/// current table schema, auto-assigns field IDs from `table.metadata().last_column_id()`, +/// builds a new schema, and emits `AddSchema` + `SetCurrentSchema` updates with a +/// `CurrentSchemaIdMatch` requirement. +/// +/// # Example +/// +/// ```ignore +/// let tx = Transaction::new(&table); +/// let action = tx.update_schema() +/// .add_column("new_col", Type::Primitive(PrimitiveType::Int)) +/// .add_column_to("person", "email", Type::Primitive(PrimitiveType::String)) +/// .delete_column("old_col"); +/// let tx = action.apply(tx).unwrap(); +/// let table = tx.commit(&catalog).await.unwrap(); +/// ``` +pub struct UpdateSchemaAction { + additions: Vec<PendingAdd>, + deletes: Vec<String>, + auto_assign_ids: bool, +} + +impl UpdateSchemaAction { + /// Creates a new empty `UpdateSchemaAction`. + pub(crate) fn new() -> Self { + Self { + additions: Vec::new(), + deletes: Vec::new(), + auto_assign_ids: true, + } + } + + // --- Root-level additions --- + + /// Add a `NestedFieldRef` column to the table root. + pub fn add_field(self, field: NestedFieldRef) -> Self { + self.add_field_internal(None, field) + } + + /// Add an optional column to the table root. + /// + /// The field ID is a placeholder (`0`) and will be auto-assigned at commit time. + pub fn add_column(self, name: impl ToString, field_type: Type) -> Self { + self.add_field(Arc::new(NestedField::optional(0, name, field_type))) Review Comment: I'm not sure if providing so many `add_xxx` method is a good practice in rust. It's popular in java because java provide function overload, but rust doesn't not. I think many another options is as following: ``` #[derive(TypedBuilder)] pub struct AddColumn { parent: Option<String>, name: String, optional: bool, type: Type, default_value: Option<Literal> } impl UpdateSchemaAction { pub fn add_col(mut self, add_col: AddColumn) { .... } } ``` ########## crates/integration_tests/tests/update_schema.rs: ########## Review Comment: We should avoid adding it as much as possible. You could add ut using `MemoryCatalog` -- 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]
