hililiwei commented on code in PR #8393:
URL: https://github.com/apache/iceberg/pull/8393#discussion_r1307206090


##########
python/pyiceberg/schema.py:
##########
@@ -882,6 +929,57 @@ def index_by_id(schema_or_type: Union[Schema, 
IcebergType]) -> Dict[int, NestedF
     return visit(schema_or_type, _IndexById())
 
 
+class _IndexParents(SchemaVisitor[Dict[int, int]]):
+    def __init__(self) -> None:
+        self.id_to_parent: Dict[int, int] = {}
+        self.id_stack: List[int] = []
+
+    def before_field(self, field: NestedField) -> None:
+        self.id_stack.append(field.field_id)
+
+    def after_field(self, field: NestedField) -> None:
+        self.id_stack.pop()
+
+    def schema(self, schema: Schema, struct_result: Dict[int, int]) -> 
Dict[int, int]:
+        return self.id_to_parent
+
+    def struct(self, struct: StructType, field_results: List[Dict[int, int]]) 
-> Dict[int, int]:
+        for field in struct.fields:
+            parent_id = self.id_stack[-1] if self.id_stack else None
+            if parent_id is not None:
+                # fields in the root struct are not added
+                self.id_to_parent[field.field_id] = parent_id
+
+        return self.id_to_parent
+
+    def field(self, field: NestedField, field_result: Dict[int, int]) -> 
Dict[int, int]:
+        return self.id_to_parent
+
+    def list(self, list_type: ListType, element_result: Dict[int, int]) -> 
Dict[int, int]:
+        self.id_to_parent[list_type.element_id] = self.id_stack[-1]
+        return self.id_to_parent
+
+    def map(self, map_type: MapType, key_result: Dict[int, int], value_result: 
Dict[int, int]) -> Dict[int, int]:
+        self.id_to_parent[map_type.key_id] = self.id_stack[-1]
+        self.id_to_parent[map_type.value_id] = self.id_stack[-1]
+        return self.id_to_parent
+
+    def primitive(self, primitive: PrimitiveType) -> Dict[int, int]:
+        return self.id_to_parent
+
+
+def index_parents(schema_or_type: Union[Schema, IcebergType]) -> Dict[int, 
int]:

Review Comment:
   I created `_lazy_id_to_parent`. 
https://github.com/apache/iceberg/pull/8393/files#diff-514b42afa20282d1ad0e3e2518ceb390ca79cb675d66c43bd1d51eac2b96d05fR142-R147
   I'm not sure if we should create a method like `find_field_parent 
(filed_int: int)`?
   
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to