Skip to content
Merged
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
26 changes: 23 additions & 3 deletions docs/dev/scripts/itl_1347/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,29 @@ def compare(
return problems


def _as_multiset(array: Any) -> collections.Counter[str]:
"""``array``'s values, bit for bit (``_bits``), counted without their positions."""
return collections.Counter(repr(value) for value in _bits(array).to_pylist())
_NULL_KEY = b"\x00null"


def _value_key(array: Any, index: int) -> bytes:
"""The raw Arrow value at ``index``, as bytes: equal exactly when the values are.

The element is copied to an array of its own (``take``, so no offset or neighbour
enters the bytes) and written in the Arrow IPC format; a null is one fixed key.
Never a Python object: converting to Python loses what Arrow holds (a
``time64[ns]`` becomes a microsecond ``time``, a NaN payload a NaN), which would
make two different values look equal (Codex on PR #510).
"""
if not array[index].is_valid:
return _NULL_KEY
one = pa.RecordBatch.from_arrays([array.take([index])], ["v"])
return one.serialize().to_pybytes()


def _as_multiset(array: Any) -> collections.Counter[bytes]:
"""``array``'s raw values, bit for bit (``_bits``, then ``_value_key``), counted
without their positions."""
bits = _bits(array)
return collections.Counter(_value_key(bits, index) for index in range(len(bits)))


def calibrate_tables(
Expand Down
71 changes: 71 additions & 0 deletions tests/test_dev_scripts/test_itl_1347_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import annotations

import decimal
import importlib.util
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -74,3 +75,73 @@ def test_negative_zero_and_nan_payloads_count_as_values() -> None:
second = pa.table({"v": pa.array([-0.0, 1.0], pa.float64())})
volatile, _ = compare.calibrate_tables({"t": first}, {"t": second})
assert volatile == {"t": ["v"]}


def _float_bits(bits: int, width: int) -> float:
import struct

if width == 64:
return struct.unpack("<d", struct.pack("<Q", bits))[0]
return struct.unpack("<f", struct.pack("<I", bits))[0]


# (id, Arrow type, first run's values, second run's values in place)
_CHANGED_IN_PLACE = [
("time64_ns", pa.time64("ns"), [1, 9], [2, 9]),
("timestamp_ns", pa.timestamp("ns"), [1, 9], [2, 9]),
("timestamp_ns_utc", pa.timestamp("ns", tz="UTC"), [1, 9], [2, 9]),
("duration_ns", pa.duration("ns"), [1, 9], [2, 9]),
(
"decimal256",
pa.decimal256(76, 40),
[decimal.Decimal("1." + "0" * 39 + "1"), None],
[decimal.Decimal("1." + "0" * 39 + "2"), None],
),
("negative_zero", pa.float64(), [0.0, 1.0], [-0.0, 1.0]),
(
"nan_payload",
pa.float64(),
[_float_bits(0x7FF8000000000001, 64), 1.0],
[_float_bits(0x7FF8000000000002, 64), 1.0],
),
(
"long_string",
pa.large_string(),
["x" * 5000 + "a", "b"],
["x" * 5000 + "c", "b"],
),
]


@pytest.mark.parametrize(
("kind", "before", "after"),
[case[1:] for case in _CHANGED_IN_PLACE],
ids=[case[0] for case in _CHANGED_IN_PLACE],
)
def test_a_value_changed_in_place_is_a_change_not_a_reorder(
kind: Any, before: list[Any], after: list[Any]
) -> None:
"""Rows in place, one raw value changed: volatile, never row-order variation, also
for types whose Python form loses precision (nanosecond times, ``decimal256``, a
NaN payload, ``-0.0``) or whose text form is shortened (a long string)."""
first = pa.table({"v": pa.array(before, kind)})
second = pa.table({"v": pa.array(after, kind)})
volatile, reordered = compare.calibrate_tables({"t": first}, {"t": second})
assert volatile == {"t": ["v"]}
assert reordered == {}


@pytest.mark.parametrize(
("kind", "before"),
[(case[1], case[2]) for case in _CHANGED_IN_PLACE],
ids=[case[0] for case in _CHANGED_IN_PLACE],
)
def test_the_same_raw_values_reordered_are_a_reorder(
kind: Any, before: list[Any]
) -> None:
"""The same raw values in another row order: row-order variation, for each type."""
first = pa.table({"v": pa.array(before, kind)})
second = pa.table({"v": pa.array(before[::-1], kind)})
volatile, reordered = compare.calibrate_tables({"t": first}, {"t": second})
assert volatile == {}
assert reordered == {"t": ["v"]}
Loading