-
Notifications
You must be signed in to change notification settings - Fork 125
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
Fix datetime encoding overflow #1020
Merged
Merged
Conversation
This file contains 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
Before we were always converting first to a microsecond-based representation then to the final representation. The intermediate conversion is unecessary and risks overflows when trying to convert to a different time unit later. This approach converts directly to i64 from the Elixir struct and time unit. The drawback is that we can no longer build i64 datetimes directly from Elixir structs.
* Restrict nanosecond datetimes to a set which is representable by i64 * Make datetime generation funs public
I'm surprised this worked...
* `format!("...")` to `"...".to_string()` * `.map_or(...)` to `.ok_or` * `x.clone()` to `*x` * `.map(|x| Some(x))` to `.map(Some)`
billylanchantin
commented
Nov 20, 2024
Turns out this branch fixes more than I thought. MIX_ENV=test iex -S mix # test env to get access to non-UTC timezones iex> dt = %DateTime{year: 2017, month: 11, day: 7, zone_abbr: "CET",
...> hour: 11, minute: 45, second: 18, microsecond: {123456, 6},
...> utc_offset: 3600, std_offset: 0, time_zone: "Europe/Paris"}
#DateTime<2017-11-07 11:45:18.123456+01:00 CET Europe/Paris>
# This branch (correct)
iex> [dt] |> Explorer.Series.from_list(dtype: {:datetime, :nanosecond, dt.time_zone})
#Explorer.Series<
Polars[1]
datetime[ns, Europe/Paris] [2017-11-07 11:45:18.123456+01:00 CET Europe/Paris]
>
# main (way off)
iex> [dt] |> Explorer.Series.from_list(dtype: {:datetime, :nanosecond, dt.time_zone})
#Explorer.Series<
Polars[1]
datetime[ns, Europe/Paris] [1970-01-18 11:27:35.118123+01:00 CET Europe/Paris]
>
|
This change reverts that. It also refactors the change in `series.rs` to be more readable.
josevalim
approved these changes
Nov 21, 2024
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.
Fixes one of the #1014 issues with printing dataframes.
This one stems from how we encode datetimes. Example:
Panics with:
The culprit is how we encode datetimes: we always turn them into microseconds first. This is usually fine. But with certain operations like printing, especially when the datetime is far from the unix epoch, it can result in overflow.
My proposed fix is to build the
i64
representation directly from the(%DateTime{}, time_unit)
pair using (mostly) Polars functions. This works -- and I added some properties to be confident -- but it has the drawback of us no longer being able to supportimpl From<ExNaiveDateTime> for i64
since we need to know which time unit the user wants to represent the datetime with. We didn't use thatimpl
much so I think the drawback is acceptable.We're also handling the case where nanosecond precision datetimes can't be represented more explicitly.