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 Ray 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 uses the request's column as the cutoff instead of event_timestamp:
- The result can be a value from after the event timestamp, or missing. In the example below, the expected value is 20 for both rows. Ray returns
30(the row from the day after the request) andNA. - If that column holds integers, rows are lost: with
ts = [1, 2], Ray returns one row instead of two; in that row,tscomes back as a 1970 timestamp andtripsisNA. - If that column holds strings, Ray returns an empty dataframe (no rows and no columns), with no error.
With the same values under a name that doesn't collide (req_value), Ray returns both rows with 20. That case shows a smaller, related difference: Ray adds a ts column to the result (a copy of event_timestamp), which DuckDB doesn't.
This came up in #7177's review: the new universal regression test for the same collision in the file (Dask) offline store (#7176) fails on Ray with [30, nan, 40] instead of [20, 20, 40]. Ray fails the same way on master without that PR, and on 0.68.0.
Steps to reproduce
Needs feast[ray,duckdb] (Ray needs Python 3.11).
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],
"ts": pd.to_datetime(["2024-01-01", "2024-01-02", "2024-01-03"], utc=True),
"trips": [10, 20, 30],
}
).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),
)
# The driver has trips 10, 20 and 30 on 2024-01-01, -02 and -03. Both requests
# are on 2024-01-02, after the 20 row, so the expected value is 20. The entity
# dataframe also has its own column called "ts" (request data that happens to
# use that name).
entity_df = pd.DataFrame(
{
"driver_id": [1, 1],
"event_timestamp": pd.to_datetime(["2024-01-02 12:00", "2024-01-02 13:00"], utc=True),
"ts": pd.to_datetime(["2024-01-10", "2023-12-25"], utc=True),
}
)
offline_stores = {
"ray": {"type": "ray", "storage_path": str(repo / "ray")},
"duckdb": {"type": "duckdb"},
}
for name, offline_store in offline_stores.items():
store = FeatureStore(
config=RepoConfig(
project="repro",
provider="local",
registry=str(repo / f"registry_{name}.db"),
offline_store=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"# {name}")
print(df.sort_values("event_timestamp").to_string(index=False))Output on master (1d6cfb40f) and on 0.68.0, Ray 2.58.0, Python 3.11:
# ray
driver_id event_timestamp ts trips
1 2024-01-02 12:00:00+00:00 2024-01-10 00:00:00+00:00 30
1 2024-01-02 13:00:00+00:00 2023-12-25 00:00:00+00:00 <NA>
# duckdb
driver_id event_timestamp ts trips
1 2024-01-02 12:00:00+00:00 2024-01-10 00:00:00+00:00 20
1 2024-01-02 13:00:00+00:00 2023-12-25 00:00:00+00:00 20
Specifications
- Version: master
1d6cfb40fand 0.68.0 - Platform: macOS (Apple silicon), Python 3.11, Ray 2.58.0
- Subsystem: Ray offline store (
sdk/python/feast/infra/offline_stores/contrib/ray_offline_store/ray.py)
Possible Solution
In RayOfflineStore.get_historical_features (around line 2500), the entity side gets a copy of event_timestamp under the name timestamp_field only when the entity dataframe does not already have a column with that name:
if (
timestamp_field != "event_timestamp"
and timestamp_field not in entity_df_sample.columns
and "event_timestamp" in entity_df_sample.columns
):broadcast_join_features, windowed_temporal_join and _manual_point_in_time_join then use timestamp_field on both sides (merge_asof(..., on=timestamp_field), matching_features[timestamp_field] <= entity_row[timestamp_field]). So when the request already has that column, its values become the cutoff.
One way to fix it: when the entity dataframe has a column named like timestamp_field, rename the source's timestamp column (and the entity side's copy of event_timestamp) to an unused internal name for the join, and leave the request's own column untouched. #7177 does the same for the Dask store. I'm happy to open a PR for this.
Activity
test: Mark the Ray case of the timestamp field collision test as a kn…
test: Mark the Ray case of the timestamp field collision test as a kn…
fix: Keep the source event timestamp separate from request data in Da…