diff --git a/pyiceberg/catalog/rest/__init__.py b/pyiceberg/catalog/rest/__init__.py index 2ad3684c9a..88981ff85b 100644 --- a/pyiceberg/catalog/rest/__init__.py +++ b/pyiceberg/catalog/rest/__init__.py @@ -904,7 +904,8 @@ def _plan_scan_result(self, identifier: str | Identifier, request: PlanTableScan if not isinstance(response, PlanCompleted): raise RuntimeError(f"Invalid planStatus for response: {type(response).__name__}") - tasks = self._expand_plan_tasks(identifier, response, request.filter or AlwaysTrue()) + # BooleanExpression disallows truthiness checks; avoid `request.filter or AlwaysTrue()` + tasks = self._expand_plan_tasks(identifier, response, request.filter if request.filter is not None else AlwaysTrue()) return PlannedScanResult( tasks=tasks, storage_credentials=list(response.storage_credentials or []), diff --git a/pyiceberg/expressions/__init__.py b/pyiceberg/expressions/__init__.py index ad916171c5..c6bdac17e0 100644 --- a/pyiceberg/expressions/__init__.py +++ b/pyiceberg/expressions/__init__.py @@ -52,6 +52,13 @@ def _to_literal(value: L | Literal[L]) -> Literal[L]: class BooleanExpression(IcebergBaseModel, ABC): """An expression that evaluates to a boolean.""" + def __bool__(self) -> bool: + """Raise an error to prevent truthiness evaluation of non-constant expressions.""" + raise TypeError( + f"Cannot evaluate truthiness of non-constant BooleanExpression: {self}. " + "Use bitwise operators (~, &, |) instead of `and`, `or`, `not` for combining expressions." + ) + @abstractmethod def __invert__(self) -> BooleanExpression: """Transform the Expression into its negated version.""" @@ -452,6 +459,10 @@ class AlwaysTrue(BooleanExpression, Singleton, IcebergRootModel[bool]): root: bool = True + def __bool__(self) -> bool: + """Return True for AlwaysTrue.""" + return True + def __invert__(self) -> AlwaysFalse: """Transform the Expression into its negated version.""" return AlwaysFalse() @@ -470,6 +481,10 @@ class AlwaysFalse(BooleanExpression, Singleton, IcebergRootModel[bool]): root: bool = False + def __bool__(self) -> bool: + """Return False for AlwaysFalse.""" + return False + def __invert__(self) -> AlwaysTrue: """Transform the Expression into its negated version.""" return AlwaysTrue() diff --git a/tests/expressions/test_expressions.py b/tests/expressions/test_expressions.py index ff0c5e80aa..1832483be5 100644 --- a/tests/expressions/test_expressions.py +++ b/tests/expressions/test_expressions.py @@ -1397,6 +1397,51 @@ def test_deepcopy_then_pickle() -> None: assert restored == expr +def test_always_true_truthiness() -> None: + expr = AlwaysTrue() + assert bool(expr) is True + assert (not expr) is False + executed = False + if expr: + executed = True + assert executed is True + + +def test_always_false_truthiness() -> None: + expr = AlwaysFalse() + assert bool(expr) is False + assert (not expr) is True + executed = False + if not expr: + executed = True + assert executed is True + + +@pytest.mark.parametrize( + "expr", + [ + EqualTo("a", 1), + NotEqualTo("a", 1), + And(AlwaysTrue(), EqualTo("a", 1)), + Or(AlwaysFalse(), EqualTo("a", 1)), + Not(EqualTo("a", 1)), + In("a", (1, 2)), + NotIn("a", (1, 2)), + ], +) +def test_non_constant_boolean_expression_truthiness_raises(expr: BooleanExpression) -> None: + expected_pattern = ( + r"Cannot evaluate truthiness of non-constant BooleanExpression: .* " + r"Use bitwise operators \(~, &, \|\) instead of `and`, `or`, `not` for combining expressions\." + ) + with pytest.raises(TypeError, match=expected_pattern): + bool(expr) + + with pytest.raises(TypeError, match=expected_pattern): + if expr: + pass + + # __ __ ___ # | \/ |_ _| _ \_ _ # | |\/| | || | _/ || | diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index ca21fd05f0..3a33fb2cde 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -1259,7 +1259,8 @@ def _set_spec_id(datafile: DataFile) -> DataFile: ), io=PyArrowFileIO(), projected_schema=schema, - row_filter=expr or AlwaysTrue(), + # BooleanExpression disallows truthiness checks; avoid `expr or AlwaysTrue()` + row_filter=expr if expr is not None else AlwaysTrue(), case_sensitive=True, ).to_table( tasks=[