Skip to content

Commit a8cc4a6

Browse files
sebpretzerFokko
authored andcommitted
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
1 parent bc6bea1 commit a8cc4a6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pyiceberg/expressions/literals.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,19 @@ def to(self, type_var: IcebergType) -> Literal: # type: ignore
609609
def _(self, _: UUIDType) -> Literal[bytes]:
610610
return self
611611

612+
@to.register(FixedType)
613+
def _(self, type_var: FixedType) -> Literal[bytes]:
614+
if len(type_var) == UUID_BYTES_LENGTH:
615+
return FixedLiteral(self.value)
616+
else:
617+
raise TypeError(
618+
f"Cannot convert UUIDLiteral into {type_var}, different length: {len(type_var)} <> {UUID_BYTES_LENGTH}"
619+
)
620+
621+
@to.register(BinaryType)
622+
def _(self, _: BinaryType) -> Literal[bytes]:
623+
return BinaryLiteral(self.value)
624+
612625

613626
class FixedLiteral(Literal[bytes]):
614627
def __init__(self, value: bytes) -> None:

tests/expressions/test_literals.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,6 @@ def test_invalid_uuid_conversions() -> None:
758758
DecimalType(9, 2),
759759
StringType(),
760760
FixedType(1),
761-
BinaryType(),
762761
],
763762
)
764763

@@ -882,6 +881,25 @@ def test_uuid_literal_initialization() -> None:
882881
assert test_uuid.bytes == uuid_literal.value
883882

884883

884+
def test_uuid_to_fixed() -> None:
885+
test_uuid = uuid.uuid4()
886+
uuid_literal = literal(test_uuid)
887+
fixed_literal = uuid_literal.to(FixedType(16))
888+
assert test_uuid.bytes == fixed_literal.value
889+
with pytest.raises(TypeError) as e:
890+
uuid_literal.to(FixedType(15))
891+
assert "Cannot convert UUIDLiteral into fixed[15], different length: 15 <> 16" in str(e.value)
892+
assert isinstance(fixed_literal, FixedLiteral) # type: ignore
893+
894+
895+
def test_uuid_to_binary() -> None:
896+
test_uuid = uuid.uuid4()
897+
uuid_literal = literal(test_uuid)
898+
binary_literal = uuid_literal.to(BinaryType())
899+
assert test_uuid.bytes == binary_literal.value
900+
assert isinstance(binary_literal, BinaryLiteral) # type: ignore
901+
902+
885903
# __ __ ___
886904
# | \/ |_ _| _ \_ _
887905
# | |\/| | || | _/ || |

0 commit comments

Comments
 (0)