Skip to content

Commit b6a3f3c

Browse files
authored
fix: Naive local default for end_date in get_historical_features (#7041)
* Fix naive local default for end_date in get_historical_features When entity_df is None, end_date defaulted to datetime.now(), a naive host-local wall-clock timestamp. Offline stores interpret naive datetimes as UTC, so the retrieval window shifts by the host's UTC offset (rows missing west of UTC, future rows returned east of it). Default to datetime.now(tz=_timezone.utc), matching the existing usage of the same expression in this file. Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com> * test: cover UTC default end date for get_historical_features without entity_df Co-authored-by: luc-pimentel <luc-pimentel@users.noreply.github.com> Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com> Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com> * style: apply ruff format to test_unit_feature_store.py Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com> --------- Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
1 parent 30507c3 commit b6a3f3c

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎sdk/python/feast/feature_store.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ def get_historical_features(
20882088
)
20892089

20902090
if entity_df is None and end_date is None:
2091-
end_date = datetime.now()
2091+
end_date = datetime.now(tz=_timezone.utc)
20922092

20932093
_feature_refs = utils._get_features(
20942094
self.registry, self.project, features, allow_cache=True
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from datetime import datetime, timedelta, timezone
2+
from tempfile import mkstemp
3+
from unittest.mock import MagicMock, patch
4+
5+
from feast.feature_store import FeatureStore
6+
from feast.feature_view import FeatureView
7+
from feast.field import Field
8+
from feast.infra.offline_stores.file_source import FileSource
9+
from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig
10+
from feast.repo_config import RepoConfig
11+
from feast.types import Float32
12+
13+
14+
def _feature_store() -> FeatureStore:
15+
_, registry_path = mkstemp()
16+
_, online_store_path = mkstemp()
17+
return FeatureStore(
18+
config=RepoConfig(
19+
registry=registry_path,
20+
project="default",
21+
provider="local",
22+
online_store=SqliteOnlineStoreConfig(path=online_store_path),
23+
entity_key_serialization_version=3,
24+
)
25+
)
26+
27+
28+
def _retrieval_end_date(**kwargs) -> datetime:
29+
store = _feature_store()
30+
store.apply(
31+
[
32+
FeatureView(
33+
name="driver_stats",
34+
entities=[],
35+
schema=[Field(name="conv_rate", dtype=Float32)],
36+
source=FileSource(
37+
path="file://feast/*",
38+
timestamp_field="event_timestamp",
39+
),
40+
ttl=timedelta(days=1),
41+
),
42+
]
43+
)
44+
with patch.object(
45+
store._get_provider(), "get_historical_features", return_value=MagicMock()
46+
) as provider:
47+
store.get_historical_features(features=["driver_stats:conv_rate"], **kwargs)
48+
assert provider.call_count == 1
49+
return provider.call_args.kwargs["end_date"]
50+
51+
52+
def test_end_date_defaults_to_utc_when_entity_df_is_omitted():
53+
before = datetime.now(timezone.utc)
54+
end_date = _retrieval_end_date()
55+
after = datetime.now(timezone.utc)
56+
57+
assert end_date.utcoffset() == timedelta(0)
58+
assert before <= end_date <= after
59+
60+
61+
def test_explicit_end_date_reaches_the_offline_store_unchanged():
62+
requested = datetime(2024, 1, 1, tzinfo=timezone.utc)
63+
assert _retrieval_end_date(end_date=requested) == requested

‎sdk/python/tests/unit/test_unit_feature_store.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,49 @@ def test_materialize_odfv_reports_skips_through_the_logger(caplog, capsys):
209209

210210
assert "No source feature views found" in caplog.text
211211
assert capsys.readouterr().out == ""
212+
213+
214+
def test_get_historical_features_non_entity_default_end_date_is_utc(tmp_path):
215+
"""With entity_df=None and no end_date, the end bound handed to the provider
216+
must be the current time in UTC. Offline stores read a naive datetime as UTC
217+
(utils.make_tzaware), so a naive local value shifts the window by the host offset."""
218+
from datetime import datetime, timedelta, timezone
219+
220+
from feast import Entity, FeatureView, Field, FileSource
221+
from feast.infra.offline_stores.dask import DaskOfflineStoreConfig
222+
from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig
223+
from feast.repo_config import RepoConfig
224+
from feast.types import Int64
225+
226+
store = FeatureStore(
227+
config=RepoConfig(
228+
project="non_entity_default_end_date",
229+
registry=str(tmp_path / "registry.db"),
230+
provider="local",
231+
entity_key_serialization_version=3,
232+
offline_store=DaskOfflineStoreConfig(),
233+
online_store=SqliteOnlineStoreConfig(path=str(tmp_path / "online.db")),
234+
)
235+
)
236+
driver = Entity(name="driver", join_keys=["driver_id"])
237+
driver_stats = FeatureView(
238+
name="driver_stats",
239+
entities=[driver],
240+
ttl=timedelta(days=1),
241+
schema=[Field(name="driver_id", dtype=Int64), Field(name="value", dtype=Int64)],
242+
source=FileSource(
243+
path=str(tmp_path / "driver_stats.parquet"),
244+
timestamp_field="event_timestamp",
245+
),
246+
)
247+
store.apply([driver, driver_stats])
248+
249+
provider = MagicMock()
250+
before = datetime.now(timezone.utc)
251+
with patch.object(FeatureStore, "_get_provider", return_value=provider):
252+
store.get_historical_features(entity_df=None, features=["driver_stats:value"])
253+
after = datetime.now(timezone.utc)
254+
255+
end_date = provider.get_historical_features.call_args.kwargs["end_date"]
256+
assert end_date.utcoffset() == timedelta(0)
257+
assert before <= utils.make_tzaware(end_date) <= after

0 commit comments

Comments
 (0)