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
4 changes: 4 additions & 0 deletions pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3555,6 +3555,10 @@ def _partition_value_filter(partition_field_name: str, value: Any) -> pc.Express
return pc.field(partition_field_name).is_null()
elif isinstance(value, float) and math.isnan(value):
return pc.is_nan(pc.field(partition_field_name))
elif isinstance(value, float) and value == 0.0:
Comment thread
Fokko marked this conversation as resolved.
Resolved
# -0.0 and 0.0 are separate partitions but compare equal, so also match the sign (1 / -0.0 is -inf)
field = pc.field(partition_field_name)
return (field == value) & (pc.divide(pc.scalar(1.0), field) == math.copysign(math.inf, value))
Comment thread
Fokko marked this conversation as resolved.
Resolved
return pc.field(partition_field_name) == value


Expand Down
24 changes: 24 additions & 0 deletions tests/catalog/test_catalog_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,30 @@ def test_append_nan_to_identity_partitioned_table(catalog: Catalog) -> None:
assert sorted(tbl.scan(row_filter="value is nan").to_arrow()["id"].to_pylist()) == [2, 4]


def test_append_signed_zero_to_identity_partitioned_table(catalog: Catalog) -> None:
catalog.create_namespace("default")
identifier = f"default.append_signed_zero_identity_partition_{catalog.name}"
iceberg_schema = Schema(
NestedField(1, "id", IntegerType(), required=False),
NestedField(2, "value", DoubleType(), required=False),
)
partition_spec = PartitionSpec(
PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="value"),
)
tbl = catalog.create_table(identifier=identifier, schema=iceberg_schema, partition_spec=partition_spec)

tbl.append(
pa.Table.from_pydict(
{"id": [1, 2, 3], "value": [-0.0, 0.0, 1.0]},
schema=schema_to_pyarrow(iceberg_schema),
)
)

# Each row is written once, although -0.0 and 0.0 compare equal
assert sorted(tbl.scan().to_arrow()["id"].to_pylist()) == [1, 2, 3]
assert sorted(tbl.scan(row_filter="value = 0").to_arrow()["id"].to_pylist()) == [1, 2]


def _table_with_null_partition(catalog: Catalog, name: str, partition_spec: PartitionSpec) -> Table:
schema = Schema(NestedField(1, "p", StringType(), required=False), NestedField(2, "f", DoubleType(), required=False))
catalog.create_namespace_if_not_exists("default")
Expand Down
21 changes: 21 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3578,6 +3578,27 @@ def test_determine_partitions_identity_nan() -> None:
assert rows_by_partition == {"1.0": [1], "nan": [2, 4], "None": [3]}


def test_determine_partitions_identity_signed_zero() -> None:
for field_type in (DoubleType(), FloatType()):
schema = Schema(
NestedField(id=1, name="id", field_type=IntegerType(), required=False),
NestedField(id=2, name="value", field_type=field_type, required=False),
)
spec = PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="value"))
arrow_table = pa.Table.from_pydict(
{"id": [1, 2, 3, 4], "value": [-0.0, 0.0, 1.0, -0.0]},
schema=schema.as_arrow(),
)

partitions = list(_determine_partitions(spec, schema, arrow_table))

# -0.0 == 0.0, but they are separate partitions, so each row must land in exactly one of them
rows_by_partition = {
repr(p.partition_key.partition[0]): sorted(p.arrow_table_partition["id"].to_pylist()) for p in partitions
}
assert rows_by_partition == {"-0.0": [1, 4], "0.0": [2], "1.0": [3]}, field_type


def test_determine_partitions_with_column_named_like_partition_helper() -> None:
schema = Schema(
NestedField(id=1, name="id", field_type=LongType(), required=False),
Expand Down