Skip to content

Commit 472173e

Browse files
committed
Widen truncate project() to a boolean expression
Truncate project() can return Or, so annotate it as BooleanExpression and evaluate only single predicates in the residual visitor. mypy can now flag callers that assume a predicate.
1 parent 3601078 commit 472173e

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

‎pyiceberg/expressions/visitors.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,15 +1975,14 @@ def visit_bound_predicate(self, predicate: BoundPredicate) -> BooleanExpression:
19751975
return AlwaysTrue()
19761976

19771977
inclusive_projection = part.transform.project(part.name, predicate)
1978-
if isinstance(inclusive_projection, UnboundPredicate) and isinstance(
1979-
self._eval_projection(inclusive_projection), AlwaysFalse
1980-
):
1978+
if isinstance(self._eval_projection(inclusive_projection), AlwaysFalse):
19811979
return AlwaysFalse()
19821980

19831981
return predicate
19841982

1985-
def _eval_projection(self, projection: UnboundPredicate | None) -> BooleanExpression | None:
1986-
if projection is None:
1983+
def _eval_projection(self, projection: BooleanExpression | None) -> BooleanExpression | None:
1984+
if not isinstance(projection, UnboundPredicate):
1985+
# only a single predicate can be evaluated against the partition value
19871986
return None
19881987

19891988
bound = projection.bind(self.partition_schema, case_sensitive=self.case_sensitive)

‎pyiceberg/transforms.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pydantic import Field, PositiveInt, PrivateAttr
3131

3232
from pyiceberg.expressions import (
33+
BooleanExpression,
3334
BoundEqualTo,
3435
BoundGreaterThan,
3536
BoundGreaterThanOrEqual,
@@ -174,7 +175,7 @@ def result_type(self, source: IcebergType) -> IcebergType:
174175
...
175176

176177
@abstractmethod
177-
def project(self, name: str, pred: BoundPredicate) -> UnboundPredicate | None: ...
178+
def project(self, name: str, pred: BoundPredicate) -> BooleanExpression | None: ...
178179

179180
@abstractmethod
180181
def strict_project(self, name: str, pred: BoundPredicate) -> UnboundPredicate | None: ...
@@ -789,7 +790,7 @@ def preserves_order(self) -> bool:
789790
def source_type(self) -> IcebergType:
790791
return self._source_type
791792

792-
def project(self, name: str, pred: BoundPredicate) -> UnboundPredicate | None:
793+
def project(self, name: str, pred: BoundPredicate) -> BooleanExpression | None:
793794
field_type = pred.term.ref().field.field_type
794795

795796
if isinstance(pred.term, BoundTransform):
@@ -1054,7 +1055,7 @@ def _truncate_integer_projection(
10541055
transform: Callable[[Any | None], Any | None],
10551056
source: IcebergType,
10561057
width: int,
1057-
) -> UnboundPredicate | None:
1058+
) -> BooleanExpression | None:
10581059
"""Project a predicate on a truncate partition of an integer column.
10591060
10601061
The values from the minimum of the source type truncate to a partition value at the top of the

0 commit comments

Comments
 (0)