This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] 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 ebbc0ba3e Register `to_bytes` for `UnknownType` (#3899)
ebbc0ba3e is described below
commit ebbc0ba3e4c633179b4069a70a8f9a4453e75d88
Author: Alex Stephen <[email protected]>
AuthorDate: Thu Sep 24 20:15:27 2026 +0000
Register `to_bytes` for `UnknownType` (#3899)
`from_bytes` and `partition_to_py` both handle `UnknownType`, but
`to_bytes` did not, so serializing an unknown value fell through to the
dispatch fallback and raised. Unknown values are always null and have no
binary representation, so return None, matching `Conversions.toByteBuffer`
in the Java implementation.
The JSON single-value entry points are deliberately left unregistered:
the spec defines no JSON representation for unknown, since a non-null
initial-default or write-default is invalid for the type.
---
pyiceberg/conversions.py | 6 ++++++
tests/test_conversions.py | 18 ++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/pyiceberg/conversions.py b/pyiceberg/conversions.py
index 268cbb93e..6dd964f43 100644
--- a/pyiceberg/conversions.py
+++ b/pyiceberg/conversions.py
@@ -321,6 +321,12 @@ def _(primitive_type: DecimalType, value: Decimal) ->
bytes:
return decimal_to_bytes(value)
+@to_bytes.register(UnknownType)
+def _(_: UnknownType, value: Any) -> None:
+ """Return None since the unknown type has no binary representation."""
+ return None
+
+
@singledispatch # type: ignore
def from_bytes(primitive_type: PrimitiveType, b: bytes) -> L: # type: ignore
"""Convert bytes to a built-in python value.
diff --git a/tests/test_conversions.py b/tests/test_conversions.py
index ec04d393c..e786ae068 100644
--- a/tests/test_conversions.py
+++ b/tests/test_conversions.py
@@ -109,6 +109,7 @@ from pyiceberg.types import (
TimestamptzNanoType,
TimestamptzType,
TimeType,
+ UnknownType,
UUIDType,
)
@@ -606,3 +607,20 @@ def test_json_single_serialization(primitive_type:
PrimitiveType, value: Any, ex
)
def test_json_serialize_roundtrip(primitive_type: PrimitiveType, value: Any)
-> None:
assert value == conversions.from_json(primitive_type,
conversions.to_json(primitive_type, value))
+
+
+def test_unknown_type_conversions() -> None:
+ """Unknown values are always null, so they have no binary or partition
representation."""
+ unknown = UnknownType()
+
+ assert conversions.to_bytes(unknown, "iceberg") is None
+ assert conversions.from_bytes(unknown, b"\x01\x02\x03\xff") is None
+ assert conversions.partition_to_py(unknown, "iceberg") is None
+
+ # The spec defines no JSON single-value representation for unknown, since a
+ # non-null initial-default or write-default is invalid for the type.
+ with pytest.raises(TypeError):
+ conversions.to_json(unknown, "iceberg")
+
+ with pytest.raises(TypeError):
+ conversions.from_json(unknown, "iceberg")