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
5 changes: 5 additions & 0 deletions pyiceberg/expressions/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions tests/expressions/test_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading