Skip to content
Closed
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
12 changes: 10 additions & 2 deletions pyiceberg/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,19 @@ def as_bound(self) -> type[BoundReference]:
return BoundReference


class And(BooleanExpression):
class And(IcebergBaseModel, BooleanExpression):
"""AND operation expression - logical conjunction."""

model_config = ConfigDict(arbitrary_types_allowed=True)

type: TypingLiteral["and"] = Field(default="and", alias="type")
left: BooleanExpression
right: BooleanExpression

def __init__(self, left: BooleanExpression, right: BooleanExpression, *rest: BooleanExpression) -> None:
if isinstance(self, And) and not hasattr(self, "left") and not hasattr(self, "right"):
super().__init__(left=left, right=right)

def __new__(cls, left: BooleanExpression, right: BooleanExpression, *rest: BooleanExpression) -> BooleanExpression: # type: ignore
if rest:
return _build_balanced_tree(And, (left, right, *rest))
Expand All @@ -254,6 +261,7 @@ def __new__(cls, left: BooleanExpression, right: BooleanExpression, *rest: Boole
return left
else:
obj = super().__new__(cls)
obj.__pydantic_fields_set__ = set()
obj.left = left
obj.right = right
return obj
Expand All @@ -264,7 +272,7 @@ def __eq__(self, other: Any) -> bool:

def __str__(self) -> str:
"""Return the string representation of the And class."""
return f"And(left={str(self.left)}, right={str(self.right)})"
return f"{str(self.__class__.__name__)}(left={repr(self.left)}, right={repr(self.right)})"

def __repr__(self) -> str:
"""Return the string representation of the And class."""
Expand Down
8 changes: 8 additions & 0 deletions tests/table/test_partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import pytest

from pyiceberg.expressions import And, EqualTo
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec
from pyiceberg.schema import Schema
from pyiceberg.transforms import (
Expand Down Expand Up @@ -125,6 +126,13 @@ def test_serialize_partition_spec() -> None:
)


def test_serialize_and_expression() -> None:
expr = And(EqualTo("foo", 1), EqualTo("bar", 2))
assert expr.model_dump_json(by_alias=True) == (
'{"type":"and","left":{"type":"equal_to","term":"foo","literal":1},"right":{"type":"equal_to","term":"bar","literal":2}}'
)


def test_deserialize_unpartition_spec() -> None:
json_partition_spec = """{"spec-id":0,"fields":[]}"""
spec = PartitionSpec.model_validate_json(json_partition_spec)
Expand Down
Loading