Add JSON single-value serialization for timestamp_ns and timestamptz_ns - #3855
Open
takayoshi-makabe wants to merge 1 commit into
Open
Add JSON single-value serialization for timestamp_ns and timestamptz_ns#3855takayoshi-makabe wants to merge 1 commit into
takayoshi-makabe wants to merge 1 commit into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Rationale for this change
TimestampNanoTypeandTimestamptzNanoTypeare registered for byte conversion but not forto_json/from_json, so the JSON single-value serialization path raises for them. A nanosecond timestamp field with a default cannot even be constructed:TypeError: Cannot deserialize bytes, type timestamp_ns not supported: 1510871468123456789NestedFieldrunsfrom_jsononinitial-default/write-defaultduring validation, so this fails on construction as well as onmodel_dump_json()andmodel_validate_json().Implementation notes
Per Appendix D both types serialize as ISO-8601 strings with nanosecond precision, matching Java's
SingleValueParser:from_jsonreturns anint(epoch nanoseconds) rather than adatetime, because Python'sdatetimeonly holds microseconds and would silently drop the last three digits.from_bytesalready returns anintfor these two types.to_human_timestamp_ns/to_human_timestamptz_nsare new — no existing helper formats at nanosecond precision.Two things left out of scope: the
TypeErrorabove says "Cannot deserialize bytes" even fromto_json, a pre-existing copy of from_bytes's message that #3856 fixes separately. And this is independent of #3853, which refactors the*_to_nanosparsers while this adds the reverse direction plus the JSON dispatch — they touch different functions.Are these changes tested?
Yes.
tests/test_conversions.py: both types added totest_json_single_serialization(int anddatetimeinputs) andtest_json_serialize_roundtrip.tests/utils/test_datetime.py: the new formatters, covering the epoch, zero-padded sub-second digits, and a pre-epoch value — each asserted to round-trip back throughtimestamp_to_nanos/timestamptz_to_nanos.tests/test_types.py: end-to-endNestedFieldround-trip forinitial-default/write-default, which is the path in the report above.Are there any user-facing changes?
Yes. Nanosecond timestamp fields with an
initial-defaultorwrite-defaultnow serialize and deserialize instead of raisingTypeError.