This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git


The following commit(s) were added to refs/heads/main by this push:
     new a7f207f7 UUID literal to binary and fixed (#529)
a7f207f7 is described below

commit a7f207f7e5831b3be02bd023c4b33babc3ea13f6
Author: Seb Pretzer <[email protected]>
AuthorDate: Mon Mar 18 16:40:51 2024 -0500

    UUID literal to binary and fixed (#529)
    
    * poetry lock --no-update
    
    * adding uuid literal to fixed/binary type
    
    * simplifying uuid generation
    
    * editted wrong line of test
    
    * adding assert_type for uuid literal
    
    * assertation doesn't add much
    
    * mypy no longer failing
    
    * old code got into PR...oops
    
    * cleaning up empty lines
---
 pyiceberg/expressions/literals.py  | 13 +++++++++++++
 tests/expressions/test_literals.py | 20 +++++++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/pyiceberg/expressions/literals.py 
b/pyiceberg/expressions/literals.py
index bf76be36..d9f66ae2 100644
--- a/pyiceberg/expressions/literals.py
+++ b/pyiceberg/expressions/literals.py
@@ -609,6 +609,19 @@ class UUIDLiteral(Literal[bytes]):
     def _(self, _: UUIDType) -> Literal[bytes]:
         return self
 
+    @to.register(FixedType)
+    def _(self, type_var: FixedType) -> Literal[bytes]:
+        if len(type_var) == UUID_BYTES_LENGTH:
+            return FixedLiteral(self.value)
+        else:
+            raise TypeError(
+                f"Cannot convert UUIDLiteral into {type_var}, different 
length: {len(type_var)} <> {UUID_BYTES_LENGTH}"
+            )
+
+    @to.register(BinaryType)
+    def _(self, _: BinaryType) -> Literal[bytes]:
+        return BinaryLiteral(self.value)
+
 
 class FixedLiteral(Literal[bytes]):
     def __init__(self, value: bytes) -> None:
diff --git a/tests/expressions/test_literals.py 
b/tests/expressions/test_literals.py
index 309bd28c..c974fc8b 100644
--- a/tests/expressions/test_literals.py
+++ b/tests/expressions/test_literals.py
@@ -758,7 +758,6 @@ def test_invalid_uuid_conversions() -> None:
             DecimalType(9, 2),
             StringType(),
             FixedType(1),
-            BinaryType(),
         ],
     )
 
@@ -882,6 +881,25 @@ def test_uuid_literal_initialization() -> None:
     assert test_uuid.bytes == uuid_literal.value
 
 
+def test_uuid_to_fixed() -> None:
+    test_uuid = uuid.uuid4()
+    uuid_literal = literal(test_uuid)
+    fixed_literal = uuid_literal.to(FixedType(16))
+    assert test_uuid.bytes == fixed_literal.value
+    with pytest.raises(TypeError) as e:
+        uuid_literal.to(FixedType(15))
+    assert "Cannot convert UUIDLiteral into fixed[15], different length: 15 <> 
16" in str(e.value)
+    assert isinstance(fixed_literal, FixedLiteral)  # type: ignore
+
+
+def test_uuid_to_binary() -> None:
+    test_uuid = uuid.uuid4()
+    uuid_literal = literal(test_uuid)
+    binary_literal = uuid_literal.to(BinaryType())
+    assert test_uuid.bytes == binary_literal.value
+    assert isinstance(binary_literal, BinaryLiteral)  # type: ignore
+
+
 #   __  __      ___
 #  |  \/  |_  _| _ \_  _
 #  | |\/| | || |  _/ || |

Reply via email to