xanderbailey commented on code in PR #3057:
URL: https://github.com/apache/iceberg-rust/pull/3057#discussion_r4493521279
##########
crates/iceberg/src/spec/table_metadata_builder.rs:
##########
@@ -192,6 +193,86 @@ impl TableMetadataBuilder {
)
}
+ /// Replaces the table's schema, partition spec and sort order, leaving it
with no
+ /// current snapshot. Backs `REPLACE TABLE`.
+ ///
+ /// The table keeps its uuid, properties and snapshots, so earlier
snapshots stay readable.
+ /// Columns the table already has keep their field ids, so data written
before the replacement
+ /// stays readable through the columns both schemas share; new columns get
ids above
+ /// `last_column_id`, never a dropped column's id.
+ ///
+ /// Partition fields reuse the highest id an equivalent field has in any
of the table's specs,
+ /// and other fields get ids above `last_partition_id`.
+ ///
+ /// `location` moves the table, `None` leaves it in place. `properties`
are applied on top of
+ /// the existing ones. `format-version` upgrades the table without being
persisted; other
+ /// reserved properties are ignored.
+ ///
+ /// # Errors
+ /// - The table is format version 1, which is not supported.
+ /// - `format-version` is invalid or would downgrade the table.
+ /// - The partition spec or sort order names a column missing from
`schema`.
+ /// - `schema` takes the name of a partition field in one of the table's
specs without being
+ /// that field's source column.
Review Comment:
[1213eac](https://github.com/apache/iceberg-rust/pull/3057/commits/1213eace50245d808e54f86b21ba15307e04ce38)
##########
crates/iceberg/src/spec/table_metadata_builder.rs:
##########
@@ -192,6 +193,86 @@ impl TableMetadataBuilder {
)
}
+ /// Replaces the table's schema, partition spec and sort order, leaving it
with no
+ /// current snapshot. Backs `REPLACE TABLE`.
+ ///
+ /// The table keeps its uuid, properties and snapshots, so earlier
snapshots stay readable.
+ /// Columns the table already has keep their field ids, so data written
before the replacement
+ /// stays readable through the columns both schemas share; new columns get
ids above
+ /// `last_column_id`, never a dropped column's id.
+ ///
+ /// Partition fields reuse the highest id an equivalent field has in any
of the table's specs,
+ /// and other fields get ids above `last_partition_id`.
+ ///
+ /// `location` moves the table, `None` leaves it in place. `properties`
are applied on top of
+ /// the existing ones. `format-version` upgrades the table without being
persisted; other
+ /// reserved properties are ignored.
+ ///
+ /// # Errors
+ /// - The table is format version 1, which is not supported.
+ /// - `format-version` is invalid or would downgrade the table.
+ /// - The partition spec or sort order names a column missing from
`schema`.
+ /// - `schema` takes the name of a partition field in one of the table's
specs without being
+ /// that field's source column.
+ pub fn build_replacement(
Review Comment:
[1213eac](https://github.com/apache/iceberg-rust/pull/3057/commits/1213eace50245d808e54f86b21ba15307e04ce38)
##########
crates/iceberg/src/spec/table_metadata_builder.rs:
##########
@@ -192,6 +193,86 @@ impl TableMetadataBuilder {
)
}
+ /// Replaces the table's schema, partition spec and sort order, leaving it
with no
+ /// current snapshot. Backs `REPLACE TABLE`.
+ ///
+ /// The table keeps its uuid, properties and snapshots, so earlier
snapshots stay readable.
+ /// Columns the table already has keep their field ids, so data written
before the replacement
+ /// stays readable through the columns both schemas share; new columns get
ids above
+ /// `last_column_id`, never a dropped column's id.
+ ///
+ /// Partition fields reuse the highest id an equivalent field has in any
of the table's specs,
+ /// and other fields get ids above `last_partition_id`.
+ ///
+ /// `location` moves the table, `None` leaves it in place. `properties`
are applied on top of
+ /// the existing ones. `format-version` upgrades the table without being
persisted; other
+ /// reserved properties are ignored.
+ ///
+ /// # Errors
+ /// - The table is format version 1, which is not supported.
+ /// - `format-version` is invalid or would downgrade the table.
+ /// - The partition spec or sort order names a column missing from
`schema`.
+ /// - `schema` takes the name of a partition field in one of the table's
specs without being
+ /// that field's source column.
+ pub fn build_replacement(
+ self,
+ schema: Schema,
+ spec: impl Into<UnboundPartitionSpec>,
+ sort_order: SortOrder,
+ location: Option<String>,
+ properties: HashMap<String, String>,
+ ) -> Result<Self> {
+ if self.metadata.format_version == FormatVersion::V1 {
+ return Err(Error::new(
+ ErrorKind::FeatureUnsupported,
+ "Table replacement is not supported for format version 1",
+ ));
+ }
+
+ let format_version = properties
+ .get(TableProperties::PROPERTY_FORMAT_VERSION)
+ .map(|value| {
+ serde_json::from_str::<FormatVersion>(value).map_err(|err| {
+ invalid_data!("Unsupported format version:
{value}").with_source(err)
+ })
+ })
+ .transpose()?
+ .unwrap_or(self.metadata.format_version);
+ let properties = properties
+ .into_iter()
+ .filter(|(key, _)|
!TableProperties::RESERVED_PROPERTIES.contains(&key.as_str()))
+ .collect();
Review Comment:
[66baae3](https://github.com/apache/iceberg-rust/pull/3057/commits/66baae31a6e46cf0016c6cbcb5808c357d7e11dd)
[1213eac](https://github.com/apache/iceberg-rust/pull/3057/commits/1213eace50245d808e54f86b21ba15307e04ce38)
##########
crates/iceberg/src/spec/table_metadata_builder.rs:
##########
@@ -192,6 +193,86 @@ impl TableMetadataBuilder {
)
}
+ /// Replaces the table's schema, partition spec and sort order, leaving it
with no
+ /// current snapshot. Backs `REPLACE TABLE`.
+ ///
+ /// The table keeps its uuid, properties and snapshots, so earlier
snapshots stay readable.
+ /// Columns the table already has keep their field ids, so data written
before the replacement
+ /// stays readable through the columns both schemas share; new columns get
ids above
+ /// `last_column_id`, never a dropped column's id.
Review Comment:
[1213eac](https://github.com/apache/iceberg-rust/pull/3057/commits/1213eace50245d808e54f86b21ba15307e04ce38)
##########
crates/iceberg/src/spec/table_metadata_builder.rs:
##########
@@ -192,6 +193,86 @@ impl TableMetadataBuilder {
)
}
+ /// Replaces the table's schema, partition spec and sort order, leaving it
with no
+ /// current snapshot. Backs `REPLACE TABLE`.
+ ///
+ /// The table keeps its uuid, properties and snapshots, so earlier
snapshots stay readable.
+ /// Columns the table already has keep their field ids, so data written
before the replacement
+ /// stays readable through the columns both schemas share; new columns get
ids above
+ /// `last_column_id`, never a dropped column's id.
+ ///
+ /// Partition fields reuse the highest id an equivalent field has in any
of the table's specs,
+ /// and other fields get ids above `last_partition_id`.
+ ///
+ /// `location` moves the table, `None` leaves it in place. `properties`
are applied on top of
+ /// the existing ones. `format-version` upgrades the table without being
persisted; other
+ /// reserved properties are ignored.
+ ///
+ /// # Errors
+ /// - The table is format version 1, which is not supported.
+ /// - `format-version` is invalid or would downgrade the table.
+ /// - The partition spec or sort order names a column missing from
`schema`.
+ /// - `schema` takes the name of a partition field in one of the table's
specs without being
+ /// that field's source column.
+ pub fn build_replacement(
+ self,
+ schema: Schema,
+ spec: impl Into<UnboundPartitionSpec>,
+ sort_order: SortOrder,
+ location: Option<String>,
+ properties: HashMap<String, String>,
+ ) -> Result<Self> {
+ if self.metadata.format_version == FormatVersion::V1 {
+ return Err(Error::new(
+ ErrorKind::FeatureUnsupported,
+ "Table replacement is not supported for format version 1",
+ ));
+ }
+
+ let format_version = properties
+ .get(TableProperties::PROPERTY_FORMAT_VERSION)
+ .map(|value| {
+ serde_json::from_str::<FormatVersion>(value).map_err(|err| {
+ invalid_data!("Unsupported format version:
{value}").with_source(err)
+ })
+ })
+ .transpose()?
+ .unwrap_or(self.metadata.format_version);
+ let properties = properties
+ .into_iter()
+ .filter(|(key, _)|
!TableProperties::RESERVED_PROPERTIES.contains(&key.as_str()))
+ .collect();
+ let spec = spec.into();
+
+ let previous_id_to_name = schema.field_id_to_name_map().clone();
+ let fresh_schema = assign_fresh_ids(
+ schema,
+ self.get_current_schema()?,
+ self.metadata.last_column_id,
+ )?;
+
+ let (fresh_spec, fresh_sort_order) =
+ Self::rebind_to_schema(&previous_id_to_name, &fresh_schema, spec,
sort_order)?;
+ let fresh_spec = self.reassign_partition_field_ids(fresh_spec)?;
+
+ let mut builder = self.upgrade_format_version(format_version)?;
+ if builder.metadata.refs.contains_key(MAIN_BRANCH) {
Review Comment:
[1213eac](https://github.com/apache/iceberg-rust/pull/3057/commits/1213eace50245d808e54f86b21ba15307e04ce38)
--
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]