Skip to content

fix: Use UTC in SQLite online store timestamp adapters - #7235

Open
PandaAB wants to merge 2 commits into
feast-dev:masterfrom
PandaAB:fix/sqlite-utc-timestamp-adapter
Open

PandaAB wants to merge 2 commits into
feast-dev:masterfrom
PandaAB:fix/sqlite-utc-timestamp-adapter

Conversation

@PandaAB

@PandaAB PandaAB commented Nov 25, 2026 •

Copy link
Copy Markdown

What this PR does / why we need it:

The SQLite online store converted datetimes through the process local timezone. adapt_datetime_epoch called datetime.timestamp() on the naive UTC datetime that online_write_batch normalized with to_naive_utc(), and convert_timestamp called datetime.fromtimestamp() without a timezone. On any non-UTC host (for example, local development in America/New_York or Asia/Shanghai paired with a UTC container), stored event_ts and created_ts epochs are shifted by the writer's UTC offset, and reading the database from a different timezone returns shifted timestamps that can push fresh rows past TTL (OUTSIDE_MAX_AGE).

This PR treats naive datetimes as UTC in adapt_datetime_epoch and returns naive UTC datetimes from convert_timestamp, matching the naive-means-UTC contract documented by to_naive_utc and make_tzaware.

Which issue(s) this PR fixes:

None filed. Reproduced locally by writing and reading the same SQLite online store across different process timezones (UTC, America/New_York, and Asia/Shanghai): raw on-disk epochs were offset by the writer's local UTC offset, online_read returned shifted timestamps, and TTL checks reported fresh rows as OUTSIDE_MAX_AGE.

Checks

  • I've made sure the tests are passing.
  • My commits are signed off (git commit -s)
  • My PR title follows conventional commits format

Testing Strategy

  • Unit tests
  • Integration tests
  • Manual tests
  • Testing is not required for this change

Added sdk/python/tests/unit/infra/online_store/test_sqlite_utc_timestamps.py to test cross-timezone write/read pairs (UTC/UTC, Asia/Shanghai/UTC, UTC/Asia/Shanghai, America/New_York/UTC), verify raw on-disk epochs equal true UTC epochs, and check TTL status via _populate_response_from_feature_data.

Misc

Note on compatibility: existing rows previously written on a non-UTC host retain their stored offset epochs and are not automatically migrated by this change.

adapt_datetime_epoch interpreted naive datetimes in the process local timezone and convert_timestamp returned local wall clock time, so a database written on a non-UTC host stored event_ts and created_ts offset by the local UTC offset, and a reader in another timezone got shifted timestamps that made TTL checks report fresh rows as OUTSIDE_MAX_AGE.

Treat naive datetimes as UTC in adapt_datetime_epoch and return naive UTC datetimes from convert_timestamp, matching the naive means UTC contract of to_naive_utc and make_tzaware. Rows already written by non-UTC hosts keep their shifted epochs and are not migrated automatically.

Add regression tests that flip the process TZ between the write and read sides, assert the on-disk epochs equal the true UTC epochs, and check TTL status through the production TTL path.

Signed-off-by: PandaAB <104823169+PandaAB@users.noreply.github.com>
@PandaAB
PandaAB requested a review from a team as a code owner November 25, 2026 05:38
Copilot AI balanced review requested due to automatic review settings November 25, 2026 05:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +107 to +108
"""Convert a Unix epoch timestamp to a naive UTC datetime."""
return datetime.fromtimestamp(int(val), tz=timezone.utc).replace(tzinfo=None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Incorrect timestamps for existing SQLite features

After upgrading in place, convert_timestamp misreads rows that older versions wrote with a nonzero timezone offset. The old reader reversed that offset on the same host. Now online retrieval can mark fresh features expired or expired features fresh.

Learn more

SQLite keeps the latest feature values and their timestamps in an on-disk database. Previously, online_write_batch supplied naive UTC datetimes to an adapter that interpreted them in the writer's local timezone. The old reader reversed that shift when it ran under the same timezone and the conversion round-tripped. update preserves existing tables, so the new converter reads the same integers as UTC. online_read labels the result UTC, and the freshness check uses it without correction. Incremental materialization starts at the last completed interval and does not automatically replay all existing rows.

Example: A Shanghai deployment writes a feature at 05:34 UTC. Before upgrading, it reads back 05:34 UTC. At 05:39 UTC after upgrading, the same row reads back 21:34 UTC on the previous day and returns OUTSIDE_MAX_AGE for a one-hour TTL.

Recommended fix: Provide an upgrade path that rebuilds the affected online rows from authoritative feature data before the new reader serves them. Document how to rebuild and repopulate the store rather than relying on incremental materialization. Do not infer the original writer's timezone from the current reader's timezone: legacy and corrected epochs have the same integer representation. Add a regression test that writes a row with the legacy adapter and reads it after upgrading.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout, and I agree with not trying to infer the original writer timezone in convert_timestamp since legacy and UTC epochs have the same integer representation.

This is the upgrade trade-off noted in # Misc. Hosts running in UTC are unaffected. For non-UTC hosts upgrading in place with an existing SQLite database, the clean recovery path is to rebuild or run a full feast materialize <start> <end> (rather than feast materialize-incremental, which only processes new intervals) so existing rows are rewritten with true UTC epochs.

Happy to add a short upgrade note in the docs or PR release note if maintainers want to highlight this for non-UTC SQLite deployments.

@franciscojavierarceo franciscojavierarceo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the existing-row compatibility concern needs a shipped upgrade path. older non-UTC writers stored shifted integer epochs that the old local-time reader reversed; the new UTC reader reinterprets those same rows, changing TTL decisions immediately after upgrade. the new tests only write with the new adapter. we need to document the required rebuild or full-range rematerialization in the SQLite docs, explain that incremental materialization leaves old rows unchanged, and add a legacy-epoch regression covering the recovery. the original writer timezone cannot safely be inferred from the integer alone.

Add an upgrade note to the SQLite online store reference explaining that event_ts and created_ts are now stored as UTC, that environments which already ran in UTC are unaffected, and that non-UTC environments need to rebuild the SQLite file or run a full feast materialize backfill over the existing data, while feast materialize-incremental never rewrites rows stored before its interval.

Add a regression test that seeds a row the way the old local-time adapter wrote it, verifies that the UTC adapters read it back shifted and outside the TTL window, verifies that materialize-incremental leaves it untouched, and verifies that a full materialize backfill restores the true UTC epoch and a PRESENT status.

Signed-off-by: PandaAB <104823169+PandaAB@users.noreply.github.com>
@PandaAB

PandaAB commented Nov 25, 2026

Copy link
Copy Markdown
Author

the existing-row compatibility concern needs a shipped upgrade path. older non-UTC writers stored shifted integer epochs that the old local-time reader reversed; the new UTC reader reinterprets those same rows, changing TTL decisions immediately after upgrade. the new tests only write with the new adapter. we need to document the required rebuild or full-range rematerialization in the SQLite docs, explain that incremental materialization leaves old rows unchanged, and add a legacy-epoch regression covering the recovery. the original writer timezone cannot safely be inferred from the integer alone.

Thanks for the review, @franciscojavierarceo. I have pushed an update addressing both points:

  1. Added an upgrade section to docs/reference/online-stores/sqlite.md explaining that UTC deployments are unaffected, non-UTC deployments need to rebuild the SQLite file or run a full feast materialize <start_ts> <end_ts> backfill, and feast materialize-incremental leaves pre-existing rows unchanged.
  2. Added test_legacy_local_time_rows_recovered_by_materialize in test_sqlite_utc_timestamps.py to seed a legacy local-time row, confirm the shifted read and OUTSIDE_MAX_AGE status, verify materialize_incremental does not touch it, and verify a full materialize backfill restores the UTC epoch and PRESENT status.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants