Skip to content

Commit 2ad3280

Browse files
authored
Make Reference JSON serializable (#2564)
<!-- Thanks for opening a pull request! --> <!-- In the case this PR will resolve an issue, please replace ${GITHUB_ISSUE_ID} below with the actual Github issue id. --> <!-- Closes #${GITHUB_ISSUE_ID} --> # Rationale for this change ## Are these changes tested? ## Are there any user-facing changes? <!-- In the case of user-facing changes, please add the changelog label. -->
1 parent bf4cd58 commit 2ad3280

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

pyiceberg/expressions/__init__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
Union,
3333
)
3434

35+
from pydantic import Field
36+
3537
from pyiceberg.expressions.literals import (
3638
AboveMax,
3739
BelowMin,
@@ -202,7 +204,7 @@ class UnboundTerm(Term[Any], Unbound[BoundTerm[L]], ABC):
202204
def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundTerm[L]: ...
203205

204206

205-
class Reference(UnboundTerm[Any]):
207+
class Reference(UnboundTerm[Any], IcebergRootModel[str]):
206208
"""A reference not yet bound to a field in a schema.
207209
208210
Args:
@@ -212,18 +214,18 @@ class Reference(UnboundTerm[Any]):
212214
An unbound reference is sometimes referred to as a "named" reference.
213215
"""
214216

215-
name: str
217+
root: str = Field()
216218

217219
def __init__(self, name: str) -> None:
218-
self.name = name
220+
super().__init__(name)
219221

220222
def __repr__(self) -> str:
221223
"""Return the string representation of the Reference class."""
222-
return f"Reference(name={repr(self.name)})"
224+
return f"Reference(name={repr(self.root)})"
223225

224-
def __eq__(self, other: Any) -> bool:
225-
"""Return the equality of two instances of the Reference class."""
226-
return self.name == other.name if isinstance(other, Reference) else False
226+
def __str__(self) -> str:
227+
"""Return the string representation of the Reference class."""
228+
return f"Reference(name={repr(self.root)})"
227229

228230
def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundReference[L]:
229231
"""Bind the reference to an Iceberg schema.
@@ -242,6 +244,10 @@ def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundReference[L]
242244
accessor = schema.accessor_for_field(field.field_id)
243245
return self.as_bound(field=field, accessor=accessor) # type: ignore
244246

247+
@property
248+
def name(self) -> str:
249+
return self.root
250+
245251
@property
246252
def as_bound(self) -> Type[BoundReference[L]]:
247253
return BoundReference[L]

tests/expressions/test_expressions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ def test_reference() -> None:
691691
assert repr(ref) == "Reference(name='abc')"
692692
assert ref == eval(repr(ref))
693693
assert ref == pickle.loads(pickle.dumps(ref))
694+
assert ref.model_dump_json() == '"abc"'
695+
assert Reference.model_validate_json('"abc"') == ref
694696

695697

696698
def test_and() -> None:

0 commit comments

Comments
 (0)