Repository navigation
Conversation
…too high A negative timestamp whose only nonzero fraction is sub-microsecond was rendered one full second too high, and could roll the date. For negative values _adjust_fraction_of_nanoseconds borrows a whole second in the fraction (max_fraction - frac), but the integer-seconds part only reflected that borrow when gmtime floored a non-whole microsecond value. When the fraction is entirely sub-microsecond the microsecond part is a whole number, so gmtime did not floor and the borrowed second was dropped: -2208943503.000000001 -> 12:34:57.999999999 (should be 12:34:56.999999999) -0.000000001 -> 1970-01-01 00:00:00... (should be 1969-12-31 ...) Borrow the second in the integer part too when the fraction borrowed and the microsecond part is whole. Cross-checked against the default SnowflakeConverter across TIMESTAMP_NTZ/LTZ and scales 7-9.
Author
|
I have read the CLA Document and I hereby sign the CLA |
This branch has not been deployed
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.
Description
The SnowSQL converter (
SnowflakeConverterSnowSQL, used by thesnowsqlCLI and by anyone passingconverter_class=SnowflakeConverterSnowSQL) renders a negative timestamp whose only nonzero fraction is sub-microsecond one full second too high — and it can roll the date:Root cause
In
_extract_timestamp(converter.py), forscale > 6the integer-seconds part is taken from the value truncated to microseconds and passed totime.gmtime, while_adjust_fraction_of_nanosecondscomputes the fraction asmax_fraction - fracfor negative values — i.e. it borrows a whole second. When the fraction reaches into the microseconds,gmtimefloors the non-whole microsecond value and borrows the same second, so the two agree (this is why the existingtest_more_timestampscases such as-2208943503.012000000are correct). But when the only nonzero fraction is sub-microsecond, the microsecond-truncated value is a whole number,gmtimedoes not floor, and the borrowed second is dropped — leaving the result one second too high.Fix
Mirror the fraction's borrow in the integer-seconds part when the fraction borrowed (negative, nonzero) and the microsecond part is whole. It is a class fix:
TIMESTAMP_NTZ/TZ/LTZat scales 7–9, for any pre-1970 UTC timestamp whose fraction is sub-microsecond.Verification
test_negative_subsecond_timestamps(matching the existingtest_more_timestampsstyle).test_more_timestampsassertions still pass — including the all-zero-fraction-2208943503.000000000→12:34:57.000000000, which thefraction != 0guard leaves untouched.SnowflakeConverter(which is unaffected) acrossTIMESTAMP_NTZ/LTZ, scales 7–9, and positive/negative/whole/µs/ns fractions — 0 second-or-date mismatches.The default
snowflake.connectorconverter is not affected (it doesn't use_extract_timestampfor this path). Happy to add aDESCRIPTION.mdchangelog entry under whatever SNOW ticket you'd like.