Skip to content

Commit

Permalink
fix: Add adapters for sqlite datetime conversion (feast-dev#4797)
Browse files Browse the repository at this point in the history
* fix: sqlite adapter deprecation warning

Signed-off-by: Matt Green <emgeee@users.noreply.github.com>

* fix: remove duplicate folder definition in ruff config

Signed-off-by: Matt Green <emgeee@users.noreply.github.com>

* update minimum ruff version and format files

Signed-off-by: Matt Green <emgeee@users.noreply.github.com>

---------

Signed-off-by: Matt Green <emgeee@users.noreply.github.com>
  • Loading branch information
emgeee authored Nov 27, 2024
1 parent f09339e commit e198b17
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
42 changes: 41 additions & 1 deletion sdk/python/feast/infra/online_stores/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sqlite3
import struct
import sys
from datetime import datetime
from datetime import date, datetime
from pathlib import Path
from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Tuple, Union

Expand All @@ -39,6 +39,46 @@
from feast.utils import _build_retrieve_online_document_record, to_naive_utc


def adapt_date_iso(val: date):
"""Adapt datetime.date to ISO 8601 date."""
return val.isoformat()


def adapt_datetime_iso(val: datetime):
"""Adapt datetime.datetime to timezone-naive ISO 8601 date."""
return val.isoformat()


def adapt_datetime_epoch(val: datetime):
"""Adapt datetime.datetime to Unix timestamp."""
return int(val.timestamp())


sqlite3.register_adapter(date, adapt_date_iso)
sqlite3.register_adapter(datetime, adapt_datetime_iso)
sqlite3.register_adapter(datetime, adapt_datetime_epoch)


def convert_date(val: bytes):
"""Convert ISO 8601 date to datetime.date object."""
return date.fromisoformat(val.decode())


def convert_datetime(val: bytes):
"""Convert ISO 8601 datetime to datetime.datetime object."""
return datetime.fromisoformat(val.decode())


def convert_timestamp(val: bytes):
"""Convert Unix epoch timestamp to datetime.datetime object."""
return datetime.fromtimestamp(int(val))


sqlite3.register_converter("date", convert_date)
sqlite3.register_converter("datetime", convert_datetime)
sqlite3.register_converter("timestamp", convert_timestamp)


class SqliteOnlineStoreConfig(FeastConfigBaseModel, VectorStoreConfig):
"""Online store config for local (SQLite-based) store"""

Expand Down
2 changes: 1 addition & 1 deletion sdk/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ select = ["E","F","W","I"]
ignore = ["E203", "E266", "E501", "E721"]

[tool.ruff.lint.isort]
known-first-party = ["feast", "feast", "feast_serving_server", "feast_core_server"]
known-first-party = ["feast", "feast_serving_server", "feast_core_server"]
default-section = "third-party"

[tool.mypy]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
"build",
"virtualenv==20.23.0",
"cryptography>=35.0,<43",
"ruff>=0.3.3",
"ruff>=0.8.0",
"mypy-protobuf>=3.1",
"grpcio-tools>=1.56.2,<2",
"grpcio-testing>=1.56.2,<2",
Expand Down

0 comments on commit e198b17

Please sign in to comment.