forked from apache/cassandra-python-driver
-
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
Draft
Copilot
wants to merge
3
commits into
master
Choose a base branch
from
copilot/fix-losing-precision-timestamp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.