Skip to content

Commit ddfb7dd

Browse files
karubianBurak Ozakinci
andauthored
Order -0.0 below 0.0 in partition field summary bounds (#4324)
Co-authored-by: Burak Ozakinci <burakoz@amazon.com>
1 parent 9cbf09a commit ddfb7dd

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎pyiceberg/manifest.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,12 @@ def update(self, value: Any) -> None:
736736
else:
737737
self._max = max(self._max, value)
738738
self._min = min(self._min, value)
739+
if isinstance(value, float) and value == 0.0:
740+
# min and max keep whichever zero came first, but bounds must order -0.0 below 0.0
741+
if math.copysign(1.0, value) < 0 and self._min == 0.0:
742+
self._min = value
743+
elif math.copysign(1.0, value) > 0 and self._max == 0.0:
744+
self._max = value
739745

740746

741747
def construct_partition_summaries(spec: PartitionSpec, schema: Schema, partitions: list[Record]) -> list[PartitionFieldSummary]:

‎tests/utils/test_manifest.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import pyiceberg.manifest as manifest_module
2828
from pyiceberg.avro.codecs import AvroCompressionCodec
2929
from pyiceberg.avro.file import DEFAULT_SYNC_INTERVAL, AvroFile, AvroOutputFile
30+
from pyiceberg.conversions import to_bytes
3031
from pyiceberg.encryption.key_metadata import StandardKeyMetadata
3132
from pyiceberg.encryption.manager import EncryptionManager
3233
from pyiceberg.encryption.stream import _encrypt_ags1
@@ -47,15 +48,17 @@
4748
_inherit_from_manifest,
4849
_manifests,
4950
clear_manifest_cache,
51+
construct_partition_summaries,
5052
read_manifest_list,
5153
write_manifest,
5254
write_manifest_list,
5355
)
54-
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec
56+
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec
5557
from pyiceberg.schema import Schema
5658
from pyiceberg.table.snapshots import Operation, Snapshot, Summary
59+
from pyiceberg.transforms import IdentityTransform
5760
from pyiceberg.typedef import Record, TableVersion
58-
from pyiceberg.types import IntegerType, ListType, LongType, NestedField, StructType
61+
from pyiceberg.types import DoubleType, FloatType, IntegerType, ListType, LongType, NestedField, StructType
5962
from tests.encryption.memory_kms import MemoryKeyManagementClient
6063

6164

@@ -1365,6 +1368,21 @@ def test_write_manifest_min_sequence_number_zero(format_version: TableVersion) -
13651368
assert manifest_file.min_sequence_number == 0
13661369

13671370

1371+
def test_construct_partition_summaries_orders_negative_zero_below_zero() -> None:
1372+
spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="x"))
1373+
for field_type in (FloatType(), DoubleType()):
1374+
schema = Schema(NestedField(1, "x", field_type, required=False))
1375+
expected = PartitionFieldSummary.from_args(
1376+
contains_null=False,
1377+
contains_nan=False,
1378+
lower_bound=to_bytes(field_type, -0.0),
1379+
upper_bound=to_bytes(field_type, 0.0),
1380+
)
1381+
for values in ([0.0, -0.0], [-0.0, 0.0]):
1382+
summaries = construct_partition_summaries(spec, schema, [Record(value) for value in values])
1383+
assert summaries == [expected], (field_type, values)
1384+
1385+
13681386
def test_inherit_from_manifest_snapshot_id() -> None:
13691387
entry = ManifestEntry.from_args(
13701388
status=ManifestEntryStatus.ADDED,

0 commit comments

Comments
 (0)