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


##########
python/src/iceberg/transforms.py:
##########
@@ -301,6 +301,91 @@ def _(self, value_type: IcebergType, value: int) -> str:
         return datetime.to_human_timestamptz(value)
 
 
+class TruncateTransform(Transform[S, S]):
+    """A transform for truncating a value to a specified width.
+    Args:
+      source_type (Type): An Iceberg Type of IntegerType, LongType, 
StringType, BinaryType or DecimalType
+      width (int): The truncate width
+    Raises:
+      ValueError: If a type is provided that is incompatible with a Truncate 
transform
+    """
+
+    def __init__(self, source_type: IcebergType, width: int):
+        super().__init__(
+            f"truncate[{width}]",
+            f"transforms.truncate(source_type={repr(source_type)}, 
width={width})",
+        )
+        self._type = source_type
+        self._width = width
+
+    @property
+    def width(self) -> int:
+        return self._width
+
+    @property
+    def type(self) -> IcebergType:
+        return self._type
+
+    def apply(self, value: Optional[S]) -> Optional[S]:
+        return self._truncate_value(value) if value is not None else None

Review Comment:
   Nit: I'm just trying to popularize the Walrus operator:
   ```suggestion
           return truncated if (truncated := self._truncate_value(value)) else 
None
   ```



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