Repository navigation
Read a naive datetime in older_than as local time - #4220
Conversation
expire_snapshots().older_than() converted its cutoff with datetime_to_millis, which reads a naive datetime as UTC. The docs call it with datetime.now() - timedelta(...), a naive local time, so east of UTC the cutoff landed later and expired snapshots that should be kept, and west of UTC it landed earlier. A naive datetime is now read as local time, as datetime.timestamp() does; an aware one is unchanged. Closes apache#4182
|
This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions. |
Fokko
left a comment
There was a problem hiding this comment.
Nice catch, and thanks @Rahul-cs50 for fixing this, and @RyanLin5967 for the clear repro in the issue 👍 Reading a naive datetime as local time matches datetime.timestamp() and how we already use datetime.now().astimezone() elsewhere, and it makes the older_than(datetime.now() - timedelta(days=3)) example in the docs correct. Verified locally: the new test fails on main and passes with the fix.
|
|
||
| def test_expire_snapshots_older_than_reads_naive_datetime_as_local_time(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Test that older_than reads a naive datetime, as datetime.now() returns it, in the local time zone.""" | ||
| if not hasattr(time, "tzset"): |
There was a problem hiding this comment.
Nit: How about using skipif, so the condition is visible on the test itself?
| def test_expire_snapshots_older_than_reads_naive_datetime_as_local_time(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None: | |
| """Test that older_than reads a naive datetime, as datetime.now() returns it, in the local time zone.""" | |
| if not hasattr(time, "tzset"): | |
| @pytest.mark.skipif(not hasattr(time, "tzset"), reason="time.tzset is not available on Windows") | |
| def test_expire_snapshots_older_than_reads_naive_datetime_as_local_time(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None: | |
| """Test that older_than reads a naive datetime, as datetime.now() returns it, in the local time zone.""" |
Closes #4182
Rationale for this change
expire_snapshots().older_than(dt)converts the cutoff withdatetime_to_millis, which reads a naive datetime as UTC. The docs call it asolder_than(datetime.now() - timedelta(days=3)), anddatetime.now()is a naive local time, so east of UTC the cutoff lands later than asked (in Tokyo,datetime.now() - timedelta(hours=1)expires snapshots that are seconds old) and west of UTC it lands earlier.older_than()now reads a naive datetime as local time, asdatetime.timestamp()does, by callingastimezone()on it before the conversion. An aware datetime is handled as before.datetime_to_millisitself is unchanged, since a naive value there means UTC for timestamp columns.Are these changes tested?
Yes.
test_expire_snapshots_older_than_reads_naive_datetime_as_local_timeintests/table/test_expire_snapshots.pysetsTZtoAsia/Tokyoand checks that a naive cutoff one minute before a snapshot keeps it and one minute after expires it. It fails onmainand passes with this change (it is skipped wheretime.tzsetis unavailable). The issue's script now keeps all three snapshots underAsia/Tokyo,America/Los_AngelesandUTC.tests/tableandtests/utils: 768 passed.ruff checkandruff format --checkare clean.Are there any user-facing changes?
older_than()with a naive datetime now uses the local time zone instead of UTC. Callers who passed naive UTC datetimes on purpose should pass an aware one (datetime.now(timezone.utc)).