Fokko commented on code in PR #5627:
URL: https://github.com/apache/iceberg/pull/5627#discussion_r958853064


##########
python/pyiceberg/schema.py:
##########
@@ -638,3 +724,61 @@ def map(self, map_type: MapType, key_result: int, 
value_result: int) -> int:
 
     def primitive(self, primitive: PrimitiveType) -> int:
         return 0
+
+
+def assign_fresh_schema_ids(schema: Schema) -> Schema:
+    """Traverses the schema, and sets new IDs"""
+    schema_struct = pre_order_visit(schema.as_struct(), _SetFreshIDs())
+
+    fresh_identifier_field_ids = []
+    new_schema = Schema(*schema_struct.fields)
+    for field_id in schema.identifier_field_ids:
+        original_field_name = schema.find_column_name(field_id)
+        if original_field_name is None:
+            raise ValueError(f"Could not find field: {field_id}")
+        fresh_field = new_schema.find_field(original_field_name)
+        if fresh_field is None:
+            raise ValueError(f"Could not lookup field in new schema: 
{original_field_name}")
+        fresh_identifier_field_ids.append(fresh_field.field_id)
+
+    return new_schema.copy(update={"identifier_field_ids": 
fresh_identifier_field_ids})
+
+
+class _SetFreshIDs(PreOrderSchemaVisitor[IcebergType]):
+    """Traverses the schema and assigns monotonically increasing ids"""
+
+    counter: itertools.count
+
+    def __init__(self, start: int = 1) -> None:
+        self.counter = itertools.count(start)
+
+    def _get_and_increment(self) -> int:
+        return next(self.counter)
+
+    def schema(self, schema: Schema, struct_result: Callable[[], StructType]) 
-> Schema:
+        return Schema(*struct_result().fields, 
identifier_field_ids=schema.identifier_field_ids)

Review Comment:
   Ah, I've refactored this because we need to build a map anyway 👍🏻 



##########
python/pyiceberg/schema.py:
##########
@@ -638,3 +724,61 @@ def map(self, map_type: MapType, key_result: int, 
value_result: int) -> int:
 
     def primitive(self, primitive: PrimitiveType) -> int:
         return 0
+
+
+def assign_fresh_schema_ids(schema: Schema) -> Schema:
+    """Traverses the schema, and sets new IDs"""
+    schema_struct = pre_order_visit(schema.as_struct(), _SetFreshIDs())
+
+    fresh_identifier_field_ids = []
+    new_schema = Schema(*schema_struct.fields)
+    for field_id in schema.identifier_field_ids:
+        original_field_name = schema.find_column_name(field_id)
+        if original_field_name is None:
+            raise ValueError(f"Could not find field: {field_id}")
+        fresh_field = new_schema.find_field(original_field_name)
+        if fresh_field is None:
+            raise ValueError(f"Could not lookup field in new schema: 
{original_field_name}")
+        fresh_identifier_field_ids.append(fresh_field.field_id)
+
+    return new_schema.copy(update={"identifier_field_ids": 
fresh_identifier_field_ids})
+
+
+class _SetFreshIDs(PreOrderSchemaVisitor[IcebergType]):
+    """Traverses the schema and assigns monotonically increasing ids"""
+
+    counter: itertools.count
+
+    def __init__(self, start: int = 1) -> None:
+        self.counter = itertools.count(start)
+
+    def _get_and_increment(self) -> int:
+        return next(self.counter)
+
+    def schema(self, schema: Schema, struct_result: Callable[[], StructType]) 
-> Schema:
+        return Schema(*struct_result().fields, 
identifier_field_ids=schema.identifier_field_ids)
+
+    def struct(self, struct: StructType, field_results: List[Callable[[], 
IcebergType]]) -> StructType:
+        return StructType(*[field() for field in field_results])
+
+    def field(self, field: NestedField, field_result: Callable[[], 
IcebergType]) -> IcebergType:
+        return NestedField(
+            field_id=self._get_and_increment(), name=field.name, 
field_type=field_result(), required=field.required, doc=field.doc

Review Comment:
   Missed that one, thanks! Just updated the code and tests



-- 
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

Reply via email to