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
17 changes: 13 additions & 4 deletions pyiceberg/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,22 +341,31 @@ def __getnewargs__(self) -> Tuple[BooleanExpression, BooleanExpression]:
return (self.left, self.right)


class Not(BooleanExpression):
class Not(IcebergBaseModel, BooleanExpression):
"""NOT operation expression - logical negation."""

child: BooleanExpression
model_config = ConfigDict(arbitrary_types_allowed=True)

type: TypingLiteral["not"] = Field(default="not")
child: BooleanExpression = Field()

def __init__(self, child: BooleanExpression, **_: Any) -> None:
super().__init__(child=child)

def __new__(cls, child: BooleanExpression) -> BooleanExpression: # type: ignore
def __new__(cls, child: BooleanExpression, **_: Any) -> BooleanExpression: # type: ignore
if child is AlwaysTrue():
return AlwaysFalse()
elif child is AlwaysFalse():
return AlwaysTrue()
elif isinstance(child, Not):
return child.child
obj = super().__new__(cls)
obj.child = child
return obj

def __str__(self) -> str:
"""Return the string representation of the Not class."""
return f"Not(child={self.child})"

def __repr__(self) -> str:
"""Return the string representation of the Not class."""
return f"Not(child={repr(self.child)})"
Expand Down
6 changes: 6 additions & 0 deletions tests/expressions/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,12 @@ def test_not() -> None:
assert not_ == pickle.loads(pickle.dumps(not_))


def test_not_json_serialization_and_deserialization() -> None:
not_expr = Not(GreaterThan("a", 22))
json_str = not_expr.model_dump_json()
assert json_str == """{"type":"not","child":{"term":"a","type":"gt","value":22}}"""


def test_always_true() -> None:
always_true = AlwaysTrue()
assert always_true.model_dump_json() == '"true"'
Expand Down