mirror of
https://github.com/lancedb/lancedb.git
synced 2026-10-12 14:52:26 +00:00
fix(python): use exact integer math for list_versions timestamps (#4426)
Refs #4404. AsyncTable.list_versions split the nanosecond timestamp with float division/modulo (`ts // 1e9`, `ts % 1e9`). At ~1.8e18 a float can't hold every integer, so the microseconds drifted (.274000 came back as .273999). Use `divmod(ts, 1_000_000_000)` and integer // 1000 instead. Only the precision fix is included here. Timezone-aware UTC (option 1 in #4404) is on hold for now, and I can add it here or in a follow-up depending on what they prefer. Added test_list_versions_timestamp_precision using the value from the issue (1790989429274000000), expecting microsecond == 274000.
This commit is contained in:
@@ -6925,9 +6925,11 @@ class AsyncTable:
|
||||
"""
|
||||
versions = await self._inner.list_versions()
|
||||
for v in versions:
|
||||
ts_nanos = v["timestamp"]
|
||||
v["timestamp"] = datetime.fromtimestamp(ts_nanos // 1e9) + timedelta(
|
||||
microseconds=(ts_nanos % 1e9) // 1e3
|
||||
# Use integer math: float division on ~1e18 nanosecond
|
||||
# values loses sub-millisecond precision.
|
||||
seconds, nanos = divmod(v["timestamp"], 1_000_000_000)
|
||||
v["timestamp"] = datetime.fromtimestamp(seconds) + timedelta(
|
||||
microseconds=nanos // 1000
|
||||
)
|
||||
|
||||
return versions
|
||||
|
||||
@@ -993,6 +993,63 @@ def test_polars(mem_db: DBConnection):
|
||||
assert len(filtered_result) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_versions_timestamp_precision():
|
||||
# 2026-10-03T01:03:49.274Z in nanoseconds. Float math turns .274000 into
|
||||
# .273999, so make sure the conversion is exact.
|
||||
ts_nanos = 1790989429274000000
|
||||
|
||||
class FakeInner:
|
||||
async def list_versions(self):
|
||||
return [{"version": 1, "timestamp": ts_nanos, "metadata": {}}]
|
||||
|
||||
table = table_module.AsyncTable(FakeInner())
|
||||
versions = await table.list_versions()
|
||||
|
||||
expected = datetime.fromtimestamp(ts_nanos // 1_000_000_000)
|
||||
assert versions[0]["timestamp"] == expected + timedelta(microseconds=274000)
|
||||
assert versions[0]["timestamp"].microsecond == 274000
|
||||
|
||||
|
||||
def test_list_versions_timestamp_precision_sync(mem_db: DBConnection):
|
||||
# The sync LanceTable.list_versions delegates to AsyncTable.list_versions,
|
||||
# which is the path the parity report hit.
|
||||
ts_nanos = 1790989429274000000
|
||||
|
||||
class FakeInner:
|
||||
async def list_versions(self):
|
||||
return [{"version": 1, "timestamp": ts_nanos, "metadata": {}}]
|
||||
|
||||
table = mem_db.create_table("ts_precision", data=[{"id": 1}])
|
||||
table._table = table_module.AsyncTable(FakeInner())
|
||||
versions = table.list_versions()
|
||||
|
||||
expected = datetime.fromtimestamp(ts_nanos // 1_000_000_000)
|
||||
assert versions[0]["timestamp"] == expected + timedelta(microseconds=274000)
|
||||
assert versions[0]["timestamp"].microsecond == 274000
|
||||
|
||||
|
||||
def test_list_versions_timestamp_precision_remote():
|
||||
# RemoteTable.list_versions also delegates to AsyncTable.list_versions.
|
||||
from lancedb.remote.table import RemoteTable
|
||||
|
||||
ts_nanos = 1790989429274000000
|
||||
|
||||
class FakeInner:
|
||||
def name(self):
|
||||
return "ts_precision"
|
||||
|
||||
async def list_versions(self):
|
||||
return [{"version": 1, "timestamp": ts_nanos, "metadata": {}}]
|
||||
|
||||
table = RemoteTable(table_module.AsyncTable(FakeInner()), "dev")
|
||||
versions = table.list_versions()
|
||||
|
||||
expected = datetime.fromtimestamp(ts_nanos // 1_000_000_000)
|
||||
assert versions[0]["timestamp"] == expected + timedelta(microseconds=274000)
|
||||
assert versions[0]["timestamp"].microsecond == 274000
|
||||
|
||||
|
||||
def test_versioning(mem_db: DBConnection):
|
||||
table = mem_db.create_table(
|
||||
"test",
|
||||
|
||||
Reference in New Issue
Block a user