jun-he commented on code in PR #4908:
URL: https://github.com/apache/iceberg/pull/4908#discussion_r895253856
##########
python/src/iceberg/transforms.py:
##########
@@ -224,6 +227,80 @@ def hash(self, value: UUID) -> int:
)
+def _base64encode(buffer: bytes) -> str:
+ """Converts bytes to base64 string"""
+ return base64.b64encode(buffer).decode("ISO-8859-1")
+
+
+class IdentityTransform(Transform[S, S]):
+ """Transforms a value into itself.
+
+ Example:
+ >>> transform = IdentityTransform(StringType())
+ >>> transform.apply('hello-world')
+ 'hello-world'
+ """
+
+ def __init__(self, source_type: IcebergType):
+ super().__init__(
+ "identity",
+ f"transforms.identity(source_type={repr(source_type)})",
+ )
+ self._type = source_type
+
+ def apply(self, value: Optional[S]) -> Optional[S]:
+ return value
+
+ def can_transform(self, source: IcebergType) -> bool:
+ return source.is_primitive
+
+ def result_type(self, source: IcebergType) -> IcebergType:
+ return source
+
+ @property
+ def preserves_order(self) -> bool:
+ return True
+
+ def satisfies_order_of(self, other: Transform) -> bool:
+ """ordering by value is the same as long as the other preserves
order"""
+ return other.preserves_order
+
+ def to_human_string(self, value: Optional[S]) -> str:
+ return self._human_string(value)
+
+ @singledispatchmethod
+ def _human_string(self, value: Optional[S]) -> str:
Review Comment:
You mean moving them out of the class?
Wondering if we can save `_human_string` but only keeps
`_int_to_human_string`.
like
```
def to_human_string(self, value: Optional[S]) -> str:
if value is None:
return "null"
elif isinstance(value, bytes):
return _base64encode(value)
elif isinstance(value, int):
return self._int_to_human_string(self._type, value)
else:
return str(value)
```
--
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]