@@ -784,6 +784,36 @@ impl TableMetadataBuilder {
784
784
. set_default_partition_spec ( Self :: LAST_ADDED )
785
785
}
786
786
787
+ /// Remove partition specs by their ids from the table metadata.
788
+ /// Does nothing if a spec id is not present. Active partition specs
789
+ /// should not be removed.
790
+ ///
791
+ /// # Errors
792
+ /// - Cannot remove the default partition spec.
793
+ pub fn remove_partition_specs ( mut self , spec_ids : & [ i32 ] ) -> Result < Self > {
794
+ if spec_ids. contains ( & self . metadata . default_spec . spec_id ( ) ) {
795
+ return Err ( Error :: new (
796
+ ErrorKind :: DataInvalid ,
797
+ "Cannot remove default partition spec" ,
798
+ ) ) ;
799
+ }
800
+
801
+ let mut removed_specs = Vec :: with_capacity ( spec_ids. len ( ) ) ;
802
+ spec_ids. iter ( ) . for_each ( |id| {
803
+ if self . metadata . partition_specs . remove ( id) . is_some ( ) {
804
+ removed_specs. push ( * id) ;
805
+ }
806
+ } ) ;
807
+
808
+ if !removed_specs. is_empty ( ) {
809
+ self . changes . push ( TableUpdate :: RemovePartitionSpecs {
810
+ spec_ids : removed_specs,
811
+ } ) ;
812
+ }
813
+
814
+ Ok ( self )
815
+ }
816
+
787
817
/// Add a sort order to the table metadata.
788
818
///
789
819
/// The spec is bound eagerly to the current schema and must be valid for it.
@@ -1584,6 +1614,21 @@ mod tests {
1584
1614
pretty_assertions:: assert_eq!( build_result. changes[ 0 ] , TableUpdate :: AddSpec {
1585
1615
spec: expected_change
1586
1616
} ) ;
1617
+
1618
+ // Remove the spec
1619
+ let build_result = build_result
1620
+ . metadata
1621
+ . into_builder ( Some (
1622
+ "s3://bucket/test/location/metadata/metadata1.json" . to_string ( ) ,
1623
+ ) )
1624
+ . remove_partition_specs ( & [ 1 ] )
1625
+ . unwrap ( )
1626
+ . build ( )
1627
+ . unwrap ( ) ;
1628
+
1629
+ assert_eq ! ( build_result. changes. len( ) , 1 ) ;
1630
+ assert_eq ! ( build_result. metadata. partition_specs. len( ) , 1 ) ;
1631
+ assert ! ( build_result. metadata. partition_spec_by_id( 1 ) . is_none( ) ) ;
1587
1632
}
1588
1633
1589
1634
#[ test]
@@ -2193,6 +2238,13 @@ mod tests {
2193
2238
. contains( "Cannot add snapshot with sequence number" ) ) ;
2194
2239
}
2195
2240
2241
+ #[ test]
2242
+ fn test_default_spec_cannot_be_removed ( ) {
2243
+ let builder = builder_without_changes ( FormatVersion :: V2 ) ;
2244
+
2245
+ builder. remove_partition_specs ( & [ 0 ] ) . unwrap_err ( ) ;
2246
+ }
2247
+
2196
2248
#[ test]
2197
2249
fn test_statistics ( ) {
2198
2250
let builder = builder_without_changes ( FormatVersion :: V2 ) ;
0 commit comments