Fokko commented on code in PR #5362:
URL: https://github.com/apache/iceberg/pull/5362#discussion_r932594200
##########
python/pyiceberg/expressions/base.py:
##########
@@ -342,151 +313,197 @@ def __str__(self) -> str:
@dataclass(frozen=True)
-class AlwaysTrue(BooleanExpression, ABC, Singleton):
+class AlwaysTrue(BooleanExpression, Singleton):
"""TRUE expression"""
def __invert__(self) -> AlwaysFalse:
return AlwaysFalse()
@dataclass(frozen=True)
-class AlwaysFalse(BooleanExpression, ABC, Singleton):
+class AlwaysFalse(BooleanExpression, Singleton):
"""FALSE expression"""
def __invert__(self) -> AlwaysTrue:
return AlwaysTrue()
-class IsNull(UnboundPredicate[T]):
- def __invert__(self) -> NotNull:
- return NotNull(self.term)
+@dataclass(frozen=True)
+class UnaryPredicate(Unbound[T, BooleanExpression], BooleanExpression, ABC):
+ as_bound: ClassVar[type]
+ term: UnboundTerm[T]
- def _validate_literals(self): # pylint: disable=W0238
- if self.literals is not None:
- raise AttributeError("Null is a unary predicate and takes no
Literals.")
+ @abstractmethod
+ def __invert__(self) -> UnaryPredicate:
+ ...
- def bind(self, schema: Schema, case_sensitive: bool) -> BoundIsNull[T]:
- bound_ref = self.term.bind(schema, case_sensitive)
- return BoundIsNull(bound_ref)
+ def bind(self, schema: Schema, case_sensitive: bool = True) ->
BooleanExpression:
+ bound_term = self.term.bind(schema, case_sensitive)
+ return self.as_bound(bound_term)
-class BoundIsNull(BoundPredicate[T]):
- def __invert__(self) -> BoundNotNull:
- return BoundNotNull(self.term)
+@dataclass(frozen=True)
+class BoundUnaryPredicate(Bound[T], BooleanExpression, ABC):
+ term: BoundTerm[T]
- def _validate_literals(self): # pylint: disable=W0238
- if self.literals:
- raise AttributeError("Null is a unary predicate and takes no
Literals.")
+ @abstractmethod
+ def __invert__(self) -> BoundUnaryPredicate:
+ ...
-class NotNull(UnboundPredicate[T]):
- def __invert__(self) -> IsNull:
- return IsNull(self.term)
+class BoundIsNull(BoundUnaryPredicate[T]):
+ def __new__(cls, term: BoundTerm[T]):
+ if term.ref().field.required:
+ return AlwaysFalse()
+ return super().__new__(cls)
- def _validate_literals(self): # pylint: disable=W0238
- if self.literals:
- raise AttributeError("NotNull is a unary predicate and takes no
Literals.")
+ def __invert__(self) -> BoundNotNull:
+ return BoundNotNull(self.term)
- def bind(self, schema: Schema, case_sensitive: bool) -> BoundNotNull[T]:
- bound_ref = self.term.bind(schema, case_sensitive)
- return BoundNotNull(bound_ref)
+class BoundNotNull(BoundUnaryPredicate[T]):
+ def __new__(cls, term: BoundTerm[T]):
+ if term.ref().field.required:
+ return AlwaysTrue()
+ return super().__new__(cls)
-class BoundNotNull(BoundPredicate[T]):
def __invert__(self) -> BoundIsNull:
return BoundIsNull(self.term)
- def _validate_literals(self): # pylint: disable=W0238
- if self.literals:
- raise AttributeError("NotNull is a unary predicate and takes no
Literals.")
+class IsNull(UnaryPredicate[T]):
+ as_bound = BoundIsNull
-class IsNaN(UnboundPredicate[T]):
- def __invert__(self) -> NotNaN:
- return NotNaN(self.term)
+ def __invert__(self) -> NotNull:
+ return NotNull(self.term)
- def _validate_literals(self): # pylint: disable=W0238
- if self.literals:
- raise AttributeError("IsNaN is a unary predicate and takes no
Literals.")
- def bind(self, schema: Schema, case_sensitive: bool) -> BoundIsNaN[T]:
- bound_ref = self.term.bind(schema, case_sensitive)
- return BoundIsNaN(bound_ref)
+class NotNull(UnaryPredicate[T]):
+ as_bound = BoundNotNull
+
+ def __invert__(self) -> IsNull:
+ return IsNull(self.term)
+
+class BoundIsNaN(BoundUnaryPredicate[T]):
+ def __new__(cls, term: BoundTerm[T]):
+ bound_type = term.ref().field.field_type
Review Comment:
Yes, the `__new__` expects to return to the class it belongs to. I think we
can ignore this one because I like the trick of returning a different type very
much.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]