@@ -30,12 +30,13 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
30
30
use uuid:: Uuid ;
31
31
32
32
use super :: snapshot:: SnapshotReference ;
33
+ pub use super :: table_metadata_builder:: TableMetadataBuilder ;
33
34
use super :: {
34
- PartitionSpec , PartitionSpecRef , SchemaId , SchemaRef , SchemalessPartitionSpecRef , Snapshot ,
35
- SnapshotRef , SnapshotRetention , SortOrder , SortOrderRef , DEFAULT_PARTITION_SPEC_ID ,
35
+ PartitionSpecRef , SchemaId , SchemaRef , SchemalessPartitionSpecRef , Snapshot , SnapshotRef ,
36
+ SnapshotRetention , SortOrder , SortOrderRef , DEFAULT_PARTITION_SPEC_ID ,
36
37
} ;
37
38
use crate :: error:: { timestamp_ms_to_utc, Result } ;
38
- use crate :: { Error , ErrorKind , TableCreation } ;
39
+ use crate :: { Error , ErrorKind } ;
39
40
40
41
static MAIN_BRANCH : & str = "main" ;
41
42
pub ( crate ) static ONE_MINUTE_MS : i64 = 60_000 ;
@@ -165,6 +166,15 @@ pub struct TableMetadata {
165
166
}
166
167
167
168
impl TableMetadata {
169
+ /// Convert this Table Metadata into a builder for modification.
170
+ ///
171
+ /// `current_file_location` is the location where the current version
172
+ /// of the metadata file is stored. This is used to update the metadata log.
173
+ #[ must_use]
174
+ pub fn into_builder ( self , current_file_location : impl Into < String > ) -> TableMetadataBuilder {
175
+ TableMetadataBuilder :: new_from_metadata ( self , current_file_location)
176
+ }
177
+
168
178
/// Returns format version of this metadata.
169
179
#[ inline]
170
180
pub fn format_version ( & self ) -> FormatVersion {
@@ -539,98 +549,6 @@ impl TableMetadata {
539
549
}
540
550
}
541
551
542
- /// Manipulating table metadata.
543
- pub struct TableMetadataBuilder ( TableMetadata ) ;
544
-
545
- impl TableMetadataBuilder {
546
- /// Creates a new table metadata builder from the given table metadata.
547
- pub fn new ( origin : TableMetadata ) -> Self {
548
- Self ( origin)
549
- }
550
-
551
- /// Creates a new table metadata builder from the given table creation.
552
- pub fn from_table_creation ( table_creation : TableCreation ) -> Result < Self > {
553
- let TableCreation {
554
- name : _,
555
- location,
556
- schema,
557
- partition_spec,
558
- sort_order,
559
- properties,
560
- } = table_creation;
561
-
562
- let schema: Arc < super :: Schema > = Arc :: new ( schema) ;
563
- let unpartition_spec = PartitionSpec :: unpartition_spec ( schema. clone ( ) ) ;
564
- let partition_specs = match partition_spec {
565
- Some ( _) => {
566
- return Err ( Error :: new (
567
- ErrorKind :: FeatureUnsupported ,
568
- "Can't create table with partition spec now" ,
569
- ) )
570
- }
571
- None => HashMap :: from ( [ (
572
- unpartition_spec. spec_id ( ) ,
573
- Arc :: new ( unpartition_spec. clone ( ) . into_schemaless ( ) ) ,
574
- ) ] ) ,
575
- } ;
576
-
577
- let sort_orders = match sort_order {
578
- Some ( _) => {
579
- return Err ( Error :: new (
580
- ErrorKind :: FeatureUnsupported ,
581
- "Can't create table with sort order now" ,
582
- ) )
583
- }
584
- None => HashMap :: from ( [ (
585
- SortOrder :: UNSORTED_ORDER_ID ,
586
- Arc :: new ( SortOrder :: unsorted_order ( ) ) ,
587
- ) ] ) ,
588
- } ;
589
-
590
- let mut table_metadata = TableMetadata {
591
- format_version : FormatVersion :: V2 ,
592
- table_uuid : Uuid :: now_v7 ( ) ,
593
- location : location. ok_or_else ( || {
594
- Error :: new (
595
- ErrorKind :: DataInvalid ,
596
- "Can't create table without location" ,
597
- )
598
- } ) ?,
599
- last_sequence_number : 0 ,
600
- last_updated_ms : Utc :: now ( ) . timestamp_millis ( ) ,
601
- last_column_id : schema. highest_field_id ( ) ,
602
- current_schema_id : schema. schema_id ( ) ,
603
- schemas : HashMap :: from ( [ ( schema. schema_id ( ) , schema) ] ) ,
604
- partition_specs,
605
- default_spec : PartitionSpecRef :: new ( unpartition_spec) ,
606
- last_partition_id : 0 ,
607
- properties,
608
- current_snapshot_id : None ,
609
- snapshots : Default :: default ( ) ,
610
- snapshot_log : vec ! [ ] ,
611
- sort_orders,
612
- metadata_log : vec ! [ ] ,
613
- default_sort_order_id : SortOrder :: UNSORTED_ORDER_ID ,
614
- refs : Default :: default ( ) ,
615
- } ;
616
-
617
- table_metadata. try_normalize ( ) ?;
618
-
619
- Ok ( Self ( table_metadata) )
620
- }
621
-
622
- /// Changes uuid of table metadata.
623
- pub fn assign_uuid ( mut self , uuid : Uuid ) -> Result < Self > {
624
- self . 0 . table_uuid = uuid;
625
- Ok ( self )
626
- }
627
-
628
- /// Returns the new table metadata after changes.
629
- pub fn build ( self ) -> Result < TableMetadata > {
630
- Ok ( self . 0 )
631
- }
632
- }
633
-
634
552
pub ( super ) mod _serde {
635
553
use std:: borrow:: BorrowMut ;
636
554
/// This is a helper module that defines types to help with serialization/deserialization.
@@ -2308,7 +2226,8 @@ mod tests {
2308
2226
let table_metadata = TableMetadataBuilder :: from_table_creation ( table_creation)
2309
2227
. unwrap ( )
2310
2228
. build ( )
2311
- . unwrap ( ) ;
2229
+ . unwrap ( )
2230
+ . metadata ;
2312
2231
assert_eq ! ( table_metadata. location, "s3://db/table" ) ;
2313
2232
assert_eq ! ( table_metadata. schemas. len( ) , 1 ) ;
2314
2233
assert_eq ! (
0 commit comments