Fokko commented on code in PR #245: URL: https://github.com/apache/iceberg-go/pull/245#discussion_r1916018700
########## table/metadata.go: ########## @@ -1029,3 +1030,76 @@ func (m *metadataV2) UnmarshalJSON(b []byte) error { m.preValidate() return m.validate() } + +const DefaultFormatVersion = 2 + +// NewMetadata creates a new table metadata object using the provided schema, information, generating a fresh UUID for +// the new table metadata. By default, this will generate a V2 table metadata, but this can be modified +// by adding a "format-version" property to the props map. An error will be returned if the "format-version" +// property exists and is not a valid version number. +func NewMetadata(sc *iceberg.Schema, partitions *iceberg.PartitionSpec, sortOrder SortOrder, location string, props iceberg.Properties) (Metadata, error) { + return NewMetadataWithUUID(sc, partitions, sortOrder, location, props, uuid.Nil) +} + +// NewMetadataWithUUID is like NewMetadata, but allows the caller to specify the UUID of the table rather than creating a new one. +func NewMetadataWithUUID(sc *iceberg.Schema, partitions *iceberg.PartitionSpec, sortOrder SortOrder, location string, props iceberg.Properties, tableUuid uuid.UUID) (Metadata, error) { + freshSchema, err := iceberg.AssignFreshSchemaIDs(sc, nil) + if err != nil { + return nil, err + } + + freshPartitions, err := iceberg.AssignFreshPartitionSpecIDs(partitions, sc, freshSchema) + if err != nil { + return nil, err + } + + freshSortOrder, err := AssignFreshSortOrderIDs(sortOrder, sc, freshSchema) + if err != nil { + return nil, err + } + + if tableUuid == uuid.Nil { + tableUuid = uuid.New() + } + + formatVersion := DefaultFormatVersion + if props != nil { + verStr, ok := props["format-version"] + if ok { + if formatVersion, err = strconv.Atoi(verStr); err != nil { + formatVersion = DefaultFormatVersion + } + delete(props, "format-version") + } + } + + lastPartitionID := freshPartitions.LastAssignedFieldID() + common := commonMetadata{ + LastUpdatedMS: time.Now().UnixMilli(), + LastColumnId: freshSchema.HighestFieldID(), + FormatVersion: formatVersion, + UUID: tableUuid, + Loc: location, + SchemaList: []*iceberg.Schema{freshSchema}, + CurrentSchemaID: freshSchema.ID, + Specs: []iceberg.PartitionSpec{freshPartitions}, + DefaultSpecID: freshPartitions.ID(), + LastPartitionID: &lastPartitionID, + Props: props, + SortOrderList: []SortOrder{freshSortOrder}, + DefaultSortOrderID: freshSortOrder.OrderID, + } + + switch formatVersion { + case 1: + return &metadataV1{ + commonMetadata: common, + Schema: freshSchema, + Partition: slices.Collect(freshPartitions.Fields()), Review Comment: In PyIceberg we decided to upgrade everything to V2 internally, and do the V1-specific things when serializing/deserializing. This is not a blocker for this PR. -- 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