Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions pyiceberg/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
Union,
)

from pydantic import Field

from pyiceberg.expressions.literals import (
AboveMax,
BelowMin,
Expand Down Expand Up @@ -202,7 +204,7 @@ class UnboundTerm(Term[Any], Unbound[BoundTerm[L]], ABC):
def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundTerm[L]: ...


class Reference(UnboundTerm[Any]):
class Reference(UnboundTerm[Any], IcebergRootModel[str]):
"""A reference not yet bound to a field in a schema.

Args:
Expand All @@ -212,18 +214,18 @@ class Reference(UnboundTerm[Any]):
An unbound reference is sometimes referred to as a "named" reference.
"""

name: str
root: str = Field()

def __init__(self, name: str) -> None:
self.name = name
super().__init__(name)

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

def __eq__(self, other: Any) -> bool:
"""Return the equality of two instances of the Reference class."""
return self.name == other.name if isinstance(other, Reference) else False
Comment on lines -224 to -226
Copy link
Contributor

@kevinjqliu kevinjqliu Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pydantic should fall back to the RootModel's __eq__ which checks the root fields

def __str__(self) -> str:
"""Return the string representation of the Reference class."""
return f"Reference(name={repr(self.root)})"

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

@property
def name(self) -> str:
return self.root

@property
def as_bound(self) -> Type[BoundReference[L]]:
return BoundReference[L]
Expand Down
2 changes: 2 additions & 0 deletions tests/expressions/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ def test_reference() -> None:
assert repr(ref) == "Reference(name='abc')"
assert ref == eval(repr(ref))
assert ref == pickle.loads(pickle.dumps(ref))
assert ref.model_dump_json() == '"abc"'
assert Reference.model_validate_json('"abc"') == ref


def test_and() -> None:
Expand Down