samredai commented on code in PR #4685:
URL: https://github.com/apache/iceberg/pull/4685#discussion_r868742221


##########
python/src/iceberg/schema.py:
##########
@@ -262,10 +267,45 @@ def primitive(self, primitive: PrimitiveType) -> T:
         ...  # pragma: no cover
 
 
+@dataclass(init=True, eq=True)
+class Accessor:
+    """An accessor for a specific position in a container that implements the 
StructProtocol"""
+
+    position: int
+    inner: Optional["Accessor"] = None
+
+    def __str__(self):
+        return f"Accessor(position={self.position},inner={self.inner})"
+
+    def __repr__(self):
+        return self.__str__()
+
+    def with_inner(self, accessor: "Accessor") -> "Accessor":
+        parent = copy.deepcopy(self)
+        acc = parent
+        while acc.inner:
+            acc = acc.inner
+        acc.inner = accessor
+        return parent
+
+    def get(self, container: StructProtocol) -> Any:
+        """Returns the value at self.position in `container`
+
+        Args:
+            container(StructProtocol): A container to access at position 
`self.position`
+
+        Returns:
+            Any: The value at position `self.position` in the container
+        """
+        return container.get(self.position)
+
+
 @singledispatch
 def visit(obj, visitor: SchemaVisitor[T]) -> T:
     """A generic function for applying a schema visitor to any point within a 
schema
 
+    The function traverses the schema in pre-order fashion

Review Comment:
   I agree that pre-order feels more intuitive, especially since the index gets 
built in ascending field ID order. That being said, it's not worth it if there 
are gotchas that we're missing here.
   
   > ...and I think makes the visitor you're building here more complicated 
because it can't rely on traversal to create the child accessors it needs.
   
   This is a great point--I'm noticing now that the visit method for 
NestedField here is becoming responsible for much than just visiting the 
NestedField. I couldn't think of a post-order solution for creating the wrapped 
accessor after the inner accessor is visited, but I completely missed what 
we're doing there in `_IndexByName`. I think mimicking that is a good idea and 
keeping something like a `self._inner` property on the 
`_BuildPositionAccessors` class.



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