@@ -884,6 +884,20 @@ int32_t TableMetadataBuilder::Impl::ReuseOrCreateNewSortOrderId(
884884 return new_order_id;
885885}
886886
887+ int32_t TableMetadataBuilder::Impl::ReuseOrCreateNewPartitionSpecId (
888+ const PartitionSpec& new_spec) {
889+ // if the spec already exists, use the same ID. otherwise, use the highest ID + 1.
890+ int32_t new_spec_id = PartitionSpec::kInitialSpecId ;
891+ for (const auto & spec : metadata_.partition_specs ) {
892+ if (new_spec.CompatibleWith (*spec)) {
893+ return spec->spec_id ();
894+ } else if (new_spec_id <= spec->spec_id ()) {
895+ new_spec_id = spec->spec_id () + 1 ;
896+ }
897+ }
898+ return new_spec_id;
899+ }
900+
887901int32_t TableMetadataBuilder::Impl::ReuseOrCreateNewSchemaId (
888902 const Schema& new_schema) const {
889903 // if the schema already exists, use its id; otherwise use the highest id + 1
@@ -913,9 +927,9 @@ Result<std::shared_ptr<PartitionSpec>> TableMetadataBuilder::Impl::UpdateSpecSch
913927 last_assigned_field_id = std::max (last_assigned_field_id, field.field_id ());
914928 }
915929
916- // Build without validation because the schema may have changed in a way that makes
917- // this spec invalid. The spec should still be preserved so that older metadata can
918- // be interpreted.
930+ // Build without validation because the schema may have changed in a way that
931+ // makes this spec invalid. The spec should still be preserved so that older
932+ // metadata can be interpreted.
919933 ICEBERG_ASSIGN_OR_RAISE (auto new_partition_spec,
920934 PartitionSpec::Make (partition_spec.spec_id (), std::move (fields),
921935 last_assigned_field_id));
@@ -927,9 +941,9 @@ Result<std::shared_ptr<PartitionSpec>> TableMetadataBuilder::Impl::UpdateSpecSch
927941
928942Result<std::unique_ptr<SortOrder>> TableMetadataBuilder::Impl::UpdateSortOrderSchema (
929943 const Schema& schema, const SortOrder& sort_order) {
930- // Build without validation because the schema may have changed in a way that makes
931- // this order invalid. The order should still be preserved so that older metadata can
932- // be interpreted.
944+ // Build without validation because the schema may have changed in a way that
945+ // makes this order invalid. The order should still be preserved so that older
946+ // metadata can be interpreted.
933947 auto fields = sort_order.fields ();
934948 std::vector<SortField> new_fields{fields.begin (), fields.end ()};
935949 return SortOrder::Make (sort_order.order_id (), std::move (new_fields));
0 commit comments