-
Notifications
You must be signed in to change notification settings - Fork 49
Fix precision loss for large CQL timestamp values #636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from libc.stdint cimport int64_t | ||
| cdef datetime_from_timestamp(double timestamp) | ||
| cdef datetime_from_timestamp_ms(int64_t timestamp_ms) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,25 @@ def datetime_from_timestamp(timestamp): | |
| return dt | ||
|
|
||
|
|
||
| def datetime_from_timestamp_ms(timestamp_ms): | ||
| """ | ||
| Creates a timezone-agnostic datetime from timestamp in milliseconds. | ||
| Avoids floating-point conversion to maintain precision for large timestamps. | ||
|
|
||
| Works around precision loss issues with large timestamps (far from epoch) | ||
| by using integer arithmetic throughout. | ||
|
|
||
| :param timestamp_ms: a unix timestamp, in milliseconds (as integer) | ||
| """ | ||
| # Break down milliseconds into components to avoid float conversion | ||
| # Python's % operator always returns non-negative result for positive divisor | ||
| timestamp_seconds = timestamp_ms // 1000 | ||
| remainder_ms = timestamp_ms % 1000 | ||
| microseconds = remainder_ms * 1000 | ||
| dt = DATETIME_EPOC + datetime.timedelta(seconds=timestamp_seconds, microseconds=microseconds) | ||
| return dt | ||
|
|
||
|
|
||
|
Comment on lines
+65
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot ditto, is
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||
| def utc_datetime_from_ms_timestamp(timestamp): | ||
| """ | ||
| Creates a UTC datetime from a timestamp in milliseconds. See | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Copyright DataStax, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
| import datetime | ||
| from cassandra.cqltypes import DateType | ||
| from cassandra.marshal import int64_pack | ||
|
|
||
|
|
||
| class TimestampPrecisionTests(unittest.TestCase): | ||
| """ | ||
| Tests for timestamp precision with large values (far from epoch). | ||
| See: https://github.com/scylladb/python-driver/issues/XXX | ||
| """ | ||
|
|
||
| def test_large_timestamp_roundtrip(self): | ||
| """ | ||
| Test that timestamps far from epoch (> 300 years) maintain precision | ||
| through serialize/deserialize cycle. | ||
| """ | ||
| # Timestamp for "2300-01-01 00:00:00.001" in milliseconds | ||
| # This is far enough from epoch that float precision is lost | ||
| original_ms = 10413792000001 # 2300-01-01 00:00:00.001 | ||
|
|
||
| # Pack as int64 (simulating database storage) | ||
| packed = int64_pack(original_ms) | ||
|
|
||
| # Deserialize back | ||
| dt = DateType.deserialize(packed, 0) | ||
|
|
||
| # Serialize again | ||
| repacked = DateType.serialize(dt, 0) | ||
|
|
||
| # Unpack and compare | ||
| from cassandra.marshal import int64_unpack | ||
| result_ms = int64_unpack(repacked) | ||
|
|
||
| # Should be exactly equal | ||
| assert result_ms == original_ms, \ | ||
| f"Expected {original_ms}, got {result_ms}, difference: {result_ms - original_ms}" | ||
|
|
||
| def test_year_2300_timestamp_precision(self): | ||
| """ | ||
| Test the specific case from the issue report: | ||
| timestamp "2300-01-01 00:00:00.001" should maintain precision. | ||
| """ | ||
| # Create datetime for 2300-01-01 00:00:00.001 | ||
| dt = datetime.datetime(2300, 1, 1, 0, 0, 0, 1000) # 1000 microseconds = 1 millisecond | ||
|
|
||
| # Serialize to bytes | ||
| packed = DateType.serialize(dt, 0) | ||
|
|
||
| # Deserialize back | ||
| dt_restored = DateType.deserialize(packed, 0) | ||
|
|
||
| # Serialize again | ||
| repacked = DateType.serialize(dt_restored, 0) | ||
|
|
||
| # They should be exactly equal | ||
| assert packed == repacked, \ | ||
| f"Serialization not stable: {packed.hex()} != {repacked.hex()}" | ||
|
|
||
| # The microseconds should be preserved | ||
| assert dt_restored.microsecond == 1000, \ | ||
| f"Expected 1000 microseconds, got {dt_restored.microsecond}" | ||
|
|
||
| def test_various_large_timestamps(self): | ||
| """ | ||
| Test multiple timestamps far from epoch to ensure precision is maintained. | ||
| """ | ||
| # Various timestamps > 300 years from epoch (in milliseconds) | ||
| test_timestamps_ms = [ | ||
| 10413792000001, # 2300-01-01 00:00:00.001 | ||
| 10413792000999, # 2300-01-01 00:00:00.999 | ||
| 15768000000000, # 2469-12-31 12:00:00.000 | ||
| 20000000000001, # ~2603 with millisecond precision | ||
| -10413792000001, # ~1640 BCE | ||
| ] | ||
|
|
||
| for original_ms in test_timestamps_ms: | ||
| with self.subTest(timestamp_ms=original_ms): | ||
| # Pack as int64 | ||
| packed = int64_pack(original_ms) | ||
|
|
||
| # Deserialize | ||
| dt = DateType.deserialize(packed, 0) | ||
|
|
||
| # Serialize again | ||
| repacked = DateType.serialize(dt, 0) | ||
|
|
||
| # Unpack and compare | ||
| from cassandra.marshal import int64_unpack | ||
| result_ms = int64_unpack(repacked) | ||
|
|
||
| # Should be exactly equal | ||
| assert result_ms == original_ms, \ | ||
| f"Expected {original_ms}, got {result_ms}, difference: {result_ms - original_ms}" | ||
|
|
||
| def test_small_timestamp_still_works(self): | ||
| """ | ||
| Ensure that timestamps close to epoch still work correctly. | ||
| """ | ||
| # Timestamp close to epoch (well within float precision) | ||
| original_ms = 1000000000000 # 2001-09-09 01:46:40.000 | ||
|
|
||
| packed = int64_pack(original_ms) | ||
| dt = DateType.deserialize(packed, 0) | ||
| repacked = DateType.serialize(dt, 0) | ||
|
|
||
| from cassandra.marshal import int64_unpack | ||
| result_ms = int64_unpack(repacked) | ||
|
|
||
| assert result_ms == original_ms | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Is
datetime_from_timestampused anywhere after you addeddatetime_from_timestamp_ms? Please check, and if it isn't - remove it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
datetime_from_timestampis still used in util.py bydatetime_from_uuid1()and theDateclass methods. It accepts seconds (not milliseconds) and cannot be removed. However, the unused import in deserializers.pyx was removed in commit 38911a3.