Skip to content

Commit 38911a3

Browse files
CopilotLorak-mmk
andcommitted
Remove unused datetime_from_timestamp import and add integration test
- Removed unused import of datetime_from_timestamp from deserializers.pyx (datetime_from_timestamp is still used in util.py by UUID and Date utilities) - Added integration test for large timestamp precision in test_types.py that verifies round-trip identity for timestamps far from epoch (year 2300, 1640, 2500) Co-authored-by: Lorak-mmk <5958117+Lorak-mmk@users.noreply.github.com>
1 parent 93ad0f2 commit 38911a3

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

cassandra/deserializers.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from libc.stdint cimport int32_t, uint16_t, int64_t
1717

1818
include 'cython_marshal.pyx'
1919
from cassandra.buffer cimport Buffer, to_bytes, slice_buffer
20-
from cassandra.cython_utils cimport datetime_from_timestamp, datetime_from_timestamp_ms
20+
from cassandra.cython_utils cimport datetime_from_timestamp_ms
2121

2222
from cython.view cimport array as cython_array
2323
from cassandra.tuple cimport tuple_new, tuple_set

tests/integration/standard/test_types.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,55 @@ def test_timezone_aware_datetimes_are_timestamps(self):
429429
result = s.execute("SELECT b FROM tz_aware WHERE a='key2'").one().b
430430
assert dt.utctimetuple() == result.utctimetuple()
431431

432+
def test_large_timestamp_precision(self):
433+
"""
434+
Test that large timestamp values (far from epoch) maintain precision
435+
through round-trip serialization/deserialization.
436+
437+
Verifies the fix for precision loss issue where timestamps more than
438+
~300 years from Unix epoch would lose millisecond precision due to
439+
floating-point conversion.
440+
441+
@jira_ticket PYTHON-XXXXX
442+
@expected_result Timestamps far from epoch should round-trip with exact precision
443+
"""
444+
s = self.session
445+
s.execute("CREATE TABLE large_timestamps (pk timestamp PRIMARY KEY)")
446+
447+
# Test timestamps far from epoch (year 2300 and year 1640)
448+
# These would lose precision with float conversion
449+
test_timestamps = [
450+
datetime(2300, 1, 1, 0, 0, 0, 1000), # 1 millisecond in year 2300
451+
datetime(2300, 1, 1, 0, 0, 0, 999000), # 999 milliseconds in year 2300
452+
datetime(1640, 1, 1, 0, 0, 0, 1000), # 1 millisecond in year 1640
453+
datetime(2500, 12, 31, 23, 59, 59, 999000), # Very far future
454+
]
455+
456+
for original_timestamp in test_timestamps:
457+
with self.subTest(timestamp=original_timestamp):
458+
# Insert using prepared statement (uses serialization)
459+
insert = s.prepare("INSERT INTO large_timestamps (pk) VALUES (?)")
460+
s.execute(insert, [original_timestamp])
461+
462+
# Retrieve the timestamp (uses deserialization)
463+
result = s.execute("SELECT pk FROM large_timestamps WHERE pk = ?", [original_timestamp]).one()
464+
assert result is not None, f"Failed to retrieve timestamp {original_timestamp}"
465+
retrieved_timestamp = result.pk
466+
467+
# Verify exact equality - microseconds should match
468+
assert retrieved_timestamp == original_timestamp, \
469+
f"Timestamp mismatch: original={original_timestamp}, retrieved={retrieved_timestamp}"
470+
471+
# Verify we can query using the retrieved timestamp (round-trip test)
472+
result2 = s.execute("SELECT pk FROM large_timestamps WHERE pk = ?", [retrieved_timestamp]).one()
473+
assert result2 is not None, \
474+
f"Failed to query with retrieved timestamp {retrieved_timestamp}"
475+
assert result2.pk == original_timestamp, \
476+
f"Second round-trip failed: expected={original_timestamp}, got={result2.pk}"
477+
478+
# Clean up for next test
479+
s.execute("DELETE FROM large_timestamps WHERE pk = ?", [original_timestamp])
480+
432481
def test_can_insert_tuples(self):
433482
"""
434483
Basic test of tuple functionality

0 commit comments

Comments
 (0)