Skip to content
Open
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
32 changes: 28 additions & 4 deletions pyiceberg/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import annotations

import typing
from abc import ABC, abstractmethod
from functools import cached_property
from typing import (
Expand All @@ -32,14 +33,16 @@
Union,
)

from pydantic import ConfigDict, Field, field_serializer

from pyiceberg.expressions.literals import (
AboveMax,
BelowMin,
Literal,
literal,
)
from pyiceberg.schema import Accessor, Schema
from pyiceberg.typedef import IcebergRootModel, L, StructProtocol
from pyiceberg.typedef import IcebergBaseModel, IcebergRootModel, L, StructProtocol
from pyiceberg.types import DoubleType, FloatType, NestedField
from pyiceberg.utils.singleton import Singleton

Expand Down Expand Up @@ -290,12 +293,18 @@ def __getnewargs__(self) -> Tuple[BooleanExpression, BooleanExpression]:
return (self.left, self.right)


class Or(BooleanExpression):
class Or(IcebergBaseModel, BooleanExpression):
"""OR operation expression - logical disjunction."""

model_config = ConfigDict(arbitrary_types_allowed=True)

type: str = Field(default="or", repr=False)
left: BooleanExpression
right: BooleanExpression

def __init__(self, left: typing.Union[BooleanExpression, Or], right: typing.Union[BooleanExpression, Or], *rest: Any):
return super().__init__(left=left, right=right)

def __new__(cls, left: BooleanExpression, right: BooleanExpression, *rest: BooleanExpression) -> BooleanExpression: # type: ignore
if rest:
return _build_balanced_tree(Or, (left, right, *rest))
Expand All @@ -307,10 +316,25 @@ def __new__(cls, left: BooleanExpression, right: BooleanExpression, *rest: Boole
return left
else:
obj = super().__new__(cls)
obj.left = left
obj.right = right
super(Or, obj).__init__(left=left, right=right)
return obj

@field_serializer("left")
def ser_left(self, left: BooleanExpression) -> str:
if isinstance(left, IcebergRootModel):
return left.root
return str(left)

@field_serializer("right")
def ser_right(self, right: BooleanExpression) -> str:
if isinstance(right, IcebergRootModel):
return right.root
return str(right)

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

def __eq__(self, other: Any) -> bool:
"""Return the equality of two instances of the Or class."""
return self.left == other.left and self.right == other.right if isinstance(other, Or) else False
Expand Down
4 changes: 4 additions & 0 deletions tests/expressions/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ def test_or() -> None:
# Some syntactic sugar
assert or_ == null | nan

assert (
or_.model_dump_json()
== '{"type":"or","left":"IsNull(term=Reference(name=\'a\'))","right":"IsNaN(term=Reference(name=\'b\'))"}'
)
assert str(or_) == f"Or(left={str(null)}, right={str(nan)})"
assert repr(or_) == f"Or(left={repr(null)}, right={repr(nan)})"
assert or_ == eval(repr(or_))
Expand Down
Loading