Repository navigation
Description
Expected Behavior
get_historical_features returns, for each entity row, the latest feature value at or before the row's event timestamp, and returns the entity dataframe's own columns unchanged, whatever they are called. The DuckDB offline store does this.
Current Behavior
With the file (Dask) offline store, if the entity dataframe has a column with the same name as a feature view's source timestamp_field (other than the event timestamp column itself), the point-in-time join reads the entity dataframe's column instead of the source's:
- The TTL filter and the latest-row selection use the request's value, so the result can be a value from after the event timestamp, or missing. In the example below, the expected value is 20 for every row; the file store returns
NA,NAand30(a value from the day after the request). - The request's own column is dropped from the result, and the source column comes back under an internal name,
ts__. - If the request's column is not a timestamp (a string, integer or float), the call fails with
AttributeError: 'str' object has no attribute 'tzinfo'. - If the entity dataframe has both
tsandts__, the call fails withValueError: Duplicate column names found: ['driver_id', 'event_timestamp', 'ts__', 'ts__', ...]. With two feature views whose timestamp field ists, memory use keeps growing instead (past 2.4 GB within about 20 seconds for a one-row entity dataframe, when I stopped it).
#7071 fixed the same problem for the source's created_timestamp_column. The event timestamp_field is still affected. The existing rename in _field_mapping only covers a timestamp_field equal to the entity dataframe's event timestamp column, and its __ alias can itself collide with request data: with a source whose timestamp_field is event_timestamp, a request column named __event_timestamp makes the feature come back missing.
Steps to reproduce
import tempfile
from datetime import timedelta
from pathlib import Path
import pandas as pd
from feast import Entity, FeatureStore, FeatureView, Field, FileSource, RepoConfig
from feast.types import Int64
from feast.value_type import ValueType
repo = Path(tempfile.mkdtemp())
pd.DataFrame(
{
"driver_id": [1, 1, 1, 2, 2, 2, 3, 3, 3],
"ts": pd.to_datetime(["2024-01-01", "2024-01-02", "2024-01-03"] * 3, utc=True),
"trips": [10, 20, 30] * 3,
}
).to_parquet(repo / "trips.parquet")
driver = Entity(name="driver", join_keys=["driver_id"], value_type=ValueType.INT64)
trips = FeatureView(
name="driver_trips",
entities=[driver],
schema=[Field(name="trips", dtype=Int64)],
source=FileSource(path=str(repo / "trips.parquet"), timestamp_field="ts"),
ttl=timedelta(days=7),
)
# Every driver has trips 10, 20 and 30 on 2024-01-01, -02 and -03. The requests are
# at 2024-01-02 12:00, so the expected value is 20. The entity dataframe also has
# its own column called "ts" (any request data that happens to use that name).
entity_df = pd.DataFrame(
{
"driver_id": [1, 2, 3],
"event_timestamp": pd.to_datetime(["2024-01-02 12:00"] * 3, utc=True),
"ts": [
pd.Timestamp("2023-12-25", tz="UTC"),
pd.Timestamp("2024-01-10", tz="UTC"),
pd.Timestamp("2024-01-02 01:00", tz="UTC"),
],
}
)
for offline_store in ["file", "duckdb"]:
store = FeatureStore(
config=RepoConfig(
project="repro",
provider="local",
registry=str(repo / f"registry_{offline_store}.db"),
offline_store={"type": offline_store},
online_store={"type": "sqlite", "path": str(repo / "online.db")},
entity_key_serialization_version=3,
repo_path=str(repo),
)
)
store.apply([driver, trips])
df = store.get_historical_features(
entity_df=entity_df, features=["driver_trips:trips"]
).to_df()
print(f"# {offline_store}")
print(df.sort_values("driver_id").to_string(index=False))Output on master (69ac7b0) and on 0.68.0 from PyPI:
# file
driver_id event_timestamp ts__ trips
1 2024-01-02 12:00:00+00:00 NaT <NA>
2 2024-01-02 12:00:00+00:00 NaT <NA>
3 2024-01-02 12:00:00+00:00 2024-01-03 00:00:00+00:00 30
# duckdb
driver_id event_timestamp ts trips
1 2024-01-02 12:00:00+00:00 2023-12-25 00:00:00+00:00 20
2 2024-01-02 12:00:00+00:00 2024-01-10 00:00:00+00:00 20
3 2024-01-02 12:00:00+00:00 2024-01-02 01:00:00+00:00 20
Specifications
- Version: master (69ac7b0) and 0.68.0
- Platform: macOS, Python 3.12
- Subsystem: file (Dask) offline store,
get_historical_features
Possible Solution
In DaskOfflineStore.get_historical_features, _merge joins the source data to the entity dataframe with suffixes=("", "__"). When both have a column called timestamp_field, the entity dataframe's column keeps the name and the source's becomes ts__, but _normalize_timestamp, _filter_ttl, _drop_duplicates and _drop_columns all still use timestamp_field.
The fix: rename the source's timestamp column to an unused temporary name before the merge whenever the entity dataframe already has that name, as #7071 does for the created timestamp column. I have a PR ready with regression tests.
Activity