Skip to content

Commit 21416d6

Browse files
Serialize true false (#2538)
<!-- 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 #2521 # Rationale for this change Make true/false expression serializable ## Are these changes tested? yes ## Are there any user-facing changes? <!-- In the case of user-facing changes, please add the changelog label. -->
1 parent dbe2486 commit 21416d6

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
@@ -39,7 +39,7 @@
3939
literal,
4040
)
4141
from pyiceberg.schema import Accessor, Schema
42-
from pyiceberg.typedef import L, StructProtocol
42+
from pyiceberg.typedef import IcebergRootModel, L, StructProtocol
4343
from pyiceberg.types import DoubleType, FloatType, NestedField
4444
from pyiceberg.utils.singleton import Singleton
4545

@@ -361,10 +361,14 @@ def __getnewargs__(self) -> Tuple[BooleanExpression]:
361361
"""Pickle the Not class."""
362362
return (self.child,)
363363

364+
"""TRUE expression."""
365+
364366

365-
class AlwaysTrue(BooleanExpression, Singleton):
367+
class AlwaysTrue(BooleanExpression, Singleton, IcebergRootModel[str]):
366368
"""TRUE expression."""
367369

370+
root: str = "true"
371+
368372
def __invert__(self) -> AlwaysFalse:
369373
"""Transform the Expression into its negated version."""
370374
return AlwaysFalse()
@@ -378,9 +382,11 @@ def __repr__(self) -> str:
378382
return "AlwaysTrue()"
379383

380384

381-
class AlwaysFalse(BooleanExpression, Singleton):
385+
class AlwaysFalse(BooleanExpression, Singleton, IcebergRootModel[str]):
382386
"""FALSE expression."""
383387

388+
root: str = "false"
389+
384390
def __invert__(self) -> AlwaysTrue:
385391
"""Transform the Expression into its negated version."""
386392
return AlwaysTrue()
@@ -732,14 +738,14 @@ def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundLiteralPredi
732738

733739
if isinstance(lit, AboveMax):
734740
if isinstance(self, (LessThan, LessThanOrEqual, NotEqualTo)):
735-
return AlwaysTrue() # type: ignore
741+
return AlwaysTrue()
736742
elif isinstance(self, (GreaterThan, GreaterThanOrEqual, EqualTo)):
737-
return AlwaysFalse() # type: ignore
743+
return AlwaysFalse()
738744
elif isinstance(lit, BelowMin):
739745
if isinstance(self, (GreaterThan, GreaterThanOrEqual, NotEqualTo)):
740-
return AlwaysTrue() # type: ignore
746+
return AlwaysTrue()
741747
elif isinstance(self, (LessThan, LessThanOrEqual, EqualTo)):
742-
return AlwaysFalse() # type: ignore
748+
return AlwaysFalse()
743749

744750
return self.as_bound(bound_term, lit)
745751

tests/expressions/test_expressions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ def test_not() -> None:
738738

739739
def test_always_true() -> None:
740740
always_true = AlwaysTrue()
741+
assert always_true.model_dump_json() == '"true"'
741742
assert str(always_true) == "AlwaysTrue()"
742743
assert repr(always_true) == "AlwaysTrue()"
743744
assert always_true == eval(repr(always_true))
@@ -746,6 +747,7 @@ def test_always_true() -> None:
746747

747748
def test_always_false() -> None:
748749
always_false = AlwaysFalse()
750+
assert always_false.model_dump_json() == '"false"'
749751
assert str(always_false) == "AlwaysFalse()"
750752
assert repr(always_false) == "AlwaysFalse()"
751753
assert always_false == eval(repr(always_false))

0 commit comments

Comments
 (0)