diff --git a/pyiceberg/expressions/visitors.py b/pyiceberg/expressions/visitors.py index 7504548507..c232c05b05 100644 --- a/pyiceberg/expressions/visitors.py +++ b/pyiceberg/expressions/visitors.py @@ -1556,10 +1556,13 @@ class _StrictMetricsEvaluationVisitor(_MetricsEvaluationVisitor): """Evaluate strict metrics for one data file.""" struct: StructType + null_counts_missing: bool def __init__(self, struct: StructType, file: DataFile) -> None: super().__init__(file) self.struct = struct + # Without null value counts, any column might contain nulls + self.null_counts_missing = file.null_value_counts is None def visit_is_null(self, term: BoundTerm) -> bool: # no need to check whether the field is required because binding evaluates that case @@ -1817,6 +1820,8 @@ def _get_field(self, field_id: int) -> NestedField: return field def _can_contain_nulls(self, field_id: int) -> bool: + if self.null_counts_missing: + return True return (null_count := self.null_counts.get(field_id)) is not None and null_count > 0 def _can_contain_nans(self, field_id: int) -> bool: diff --git a/tests/expressions/test_evaluator.py b/tests/expressions/test_evaluator.py index ee7265d7b3..629bb8ef72 100644 --- a/tests/expressions/test_evaluator.py +++ b/tests/expressions/test_evaluator.py @@ -1434,6 +1434,33 @@ def test_strict_missing_stats(strict_data_file_schema: Schema, strict_data_file_ assert not should_read, f"Should never match when stats are missing for expr: {expression}" +def test_strict_missing_null_counts() -> None: + schema = Schema(NestedField(1, "x", IntegerType(), required=False)) + + expressions: list[BooleanExpression] = [ + LessThan("x", 10), + LessThanOrEqual("x", 5), + GreaterThan("x", 0), + GreaterThanOrEqual("x", 1), + ] + + # Without null value counts the column might contain nulls, which do not match + for null_value_counts, expected in [({1: 0}, True), (None, False)]: + data_file = DataFile.from_args( + file_path="file_1.parquet", + file_format=FileFormat.PARQUET, + partition=Record(), + record_count=3, + value_counts={1: 3}, + null_value_counts=null_value_counts, + lower_bounds={1: to_bytes(IntegerType(), 1)}, + upper_bounds={1: to_bytes(IntegerType(), 5)}, + ) + for expression in expressions: + should_read = _StrictMetricsEvaluator(schema, expression).eval(data_file) + assert should_read == expected, f"null_value_counts={null_value_counts}, expr: {expression}" + + def test_strict_zero_record_file_stats(strict_data_file_schema: Schema) -> None: zero_record_data_file = DataFile.from_args( file_path="file_1.parquet", file_format=FileFormat.PARQUET, partition=Record(), record_count=0