From 40a4ea7621d412c6276215fe1ee917dd49490784 Mon Sep 17 00:00:00 2001 From: "Edgar Y. Walker" Date: Mon, 23 Nov 2026 01:26:47 -0800 Subject: [PATCH] fix(compare): calibration compares raw Arrow values, never Python objects (ITL-1347) Codex on PR #510: a time64[ns] column changing from [1, 9] to [2, 9] in place was labelled row-order variation, because both became the same microsecond Python time. The multiset of a column's values is now taken from each raw value written in the Arrow IPC format (after _bits; a null one fixed key): equal exactly when the values are. Tests: a value changed in place is a change (time64/timestamp/duration ns, decimal256, -0.0, a NaN payload, a long string) and the same raw values reordered are a reorder, for each. Co-Authored-By: Claude Opus 5.5 --- docs/dev/scripts/itl_1347/compare.py | 26 ++++++- .../test_dev_scripts/test_itl_1347_compare.py | 71 +++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/docs/dev/scripts/itl_1347/compare.py b/docs/dev/scripts/itl_1347/compare.py index ec7a77331..484d25b7e 100644 --- a/docs/dev/scripts/itl_1347/compare.py +++ b/docs/dev/scripts/itl_1347/compare.py @@ -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( diff --git a/tests/test_dev_scripts/test_itl_1347_compare.py b/tests/test_dev_scripts/test_itl_1347_compare.py index 05a0de5bb..aafa161ee 100644 --- a/tests/test_dev_scripts/test_itl_1347_compare.py +++ b/tests/test_dev_scripts/test_itl_1347_compare.py @@ -8,6 +8,7 @@ from __future__ import annotations +import decimal import importlib.util from pathlib import Path from typing import Any @@ -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(" 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"]}