Repository navigation
fix: Stop Ray retrieval from using a request column as the point-in-time cutoff #7214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iammanoj807
wants to merge
2
commits into
feast-dev:master
Choose a base branch
from
iammanoj807:fix/ray-timestamp-field-request-column
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
sdk/python/tests/component/ray/test_ray_timestamp_field_collision.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| """Ray offline store: request columns named like the source timestamp field. | ||
|
|
||
| The entity dataframe can hold request data in a column that has the same name | ||
| as a feature view's source timestamp field. That column is not the event | ||
| timestamp, so retrieval must not use it as the point-in-time cutoff, and it | ||
| must come back unchanged, whatever its type. | ||
| """ | ||
|
|
||
| from datetime import timedelta | ||
| from pathlib import Path | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| from feast import Entity, FeatureStore, FeatureView, Field, FileSource, RepoConfig | ||
| from feast.infra.ray_initializer import shutdown_ray | ||
| from feast.types import Int64 | ||
| from feast.value_type import ValueType | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def fresh_ray(): | ||
| shutdown_ray() | ||
| yield | ||
| shutdown_ray() | ||
|
|
||
|
|
||
| def _feature_store(repo: Path, with_driver_2: bool = True) -> FeatureStore: | ||
| # Driver 1 has 10, 20 and 30 on 1, 2 and 3 Jan; driver 2 has 40 on 1 Jan. | ||
| trips_df = pd.DataFrame( | ||
| { | ||
| "driver_id": [1, 1, 1, 2], | ||
| "ts": pd.to_datetime( | ||
| ["2024-01-01", "2024-01-02", "2024-01-03", "2024-01-01"], utc=True | ||
| ), | ||
| "trips": [10, 20, 30, 40], | ||
| } | ||
| ) | ||
| if not with_driver_2: | ||
| # With one driver the source timestamps stay sorted after sorting by | ||
| # driver, so the broadcast join uses merge_asof instead of falling | ||
| # back to its row-by-row join. | ||
| trips_df = trips_df[trips_df["driver_id"] == 1] | ||
| trips_df.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), | ||
| ) | ||
| store = FeatureStore( | ||
| config=RepoConfig( | ||
| project="ray_timestamp_field_collision", | ||
| provider="local", | ||
| registry=str(repo / "registry.db"), | ||
| offline_store={"type": "ray", "storage_path": str(repo / "ray_storage")}, | ||
| online_store={"type": "sqlite", "path": str(repo / "online.db")}, | ||
| entity_key_serialization_version=3, | ||
| repo_path=str(repo), | ||
| ) | ||
| ) | ||
| store.apply([driver, trips]) | ||
| return store | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "request_values", | ||
| [ | ||
| pd.to_datetime( | ||
| ["2024-01-10 00:00", "2023-12-25 00:00", "2024-01-02 06:00"], utc=True | ||
| ), | ||
| pd.Series([1, 2, 3], dtype="int64"), | ||
| pd.Series(["a", "2024-01-05", ""], dtype=object), | ||
| ], | ||
| ids=["datetime", "int", "string"], | ||
| ) | ||
| @pytest.mark.parametrize("full_feature_names", [False, True], ids=lambda v: str(v)) | ||
| def test_request_column_named_like_timestamp_field( | ||
| tmp_path, request_values, full_feature_names | ||
| ): | ||
| store = _feature_store(tmp_path) | ||
| entity_df = pd.DataFrame( | ||
| { | ||
| "driver_id": [1, 1, 2], | ||
| "event_timestamp": pd.to_datetime( | ||
| ["2024-01-02 12:00", "2024-01-02 13:00", "2024-01-02 12:00"], utc=True | ||
| ), | ||
| "ts": list(request_values), | ||
| "request_id": [1, 2, 3], | ||
| } | ||
| ) | ||
|
|
||
| result = store.get_historical_features( | ||
| entity_df=entity_df, | ||
| features=["driver_trips:trips"], | ||
| full_feature_names=full_feature_names, | ||
| ).to_df() | ||
|
|
||
| feature = "driver_trips__trips" if full_feature_names else "trips" | ||
| assert set(result.columns) == set(entity_df.columns) | {feature} | ||
| result = result.sort_values("request_id").reset_index(drop=True) | ||
| # The latest value at or before each event timestamp. | ||
| assert result[feature].tolist() == [20, 20, 40] | ||
| # The request column comes back unchanged. | ||
| assert result["ts"].tolist() == entity_df["ts"].tolist() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "with_colliding_column", [True, False], ids=["colliding-ts", "no-collision"] | ||
| ) | ||
| def test_requests_sharing_entity_and_event_timestamp_are_all_kept( | ||
| tmp_path, with_colliding_column | ||
| ): | ||
| store = _feature_store(tmp_path, with_driver_2=False) | ||
| entity_df = pd.DataFrame( | ||
| { | ||
| "driver_id": [1, 1], | ||
| "event_timestamp": pd.to_datetime( | ||
| ["2024-01-02 12:00", "2024-01-02 12:00"], utc=True | ||
| ), | ||
| "request_id": [101, 102], | ||
| } | ||
| ) | ||
| if with_colliding_column: | ||
| # Different request values in the column named like the source timestamp. | ||
| entity_df["ts"] = pd.to_datetime(["2024-01-02", "2024-01-04"], utc=True) | ||
|
|
||
| result = store.get_historical_features( | ||
| entity_df=entity_df, features=["driver_trips:trips"] | ||
| ).to_df() | ||
|
|
||
| result = result.sort_values("request_id").reset_index(drop=True) | ||
| # Each row is its own request, so both come back, each with the latest value | ||
| # at or before its event timestamp. | ||
| assert result["request_id"].tolist() == [101, 102] | ||
| assert result["trips"].tolist() == [20, 20] | ||
| if with_colliding_column: | ||
| assert result["ts"].tolist() == entity_df["ts"].tolist() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Shared cutoff drops distinct requests
After the cutoff switch,
broadcast_join_featuresdrops all but one same-batch request sharing an entity key and event timestamp. This happens whenmerge_asofsucceeds, even if distinct values in the colliding request column previously kept the rows separate.(Refers to this code)
Learn more
Each entity dataframe row is a separate request for historical features and can carry its own request data. The new collision path copies
event_timestampto the join key instead of using the colliding request column as that key. For small sources, broadcast entity deduplication keeps only the last row per entity key and join timestamp in each batch. A successful merge_asof receives that reduced batch, so the other requests never reach the result. The manual fallback uses the original cleaned batch instead and does not exhibit this particular row loss.Example: Request features for driver 1 twice at Jan 3, with request IDs 101 and 102 and request
tsvalues of Jan 2 and Jan 4. A small source with one row for driver 1 at Jan 1 takes the successful broadcast merge path. Before this change both IDs survived; now only 102 survives. Both IDs need the Jan 1 feature value.Recommended fix: Keep the entity rows intact for the collision path when calling
merge_asof. Leave source-side deduplication in place. Add a regression test with two request IDs, identical entity keys and event timestamps, different colliding request values, and a source ordered so thatmerge_asofsucceeds.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed in ab071f7. The row loss came from the entity-side
drop_duplicatesinbroadcast_join_features. It also dropped rows on master without any collision (two requests for the same entity and event time came back as one; DuckDB returns both), so I removed it instead of narrowing it. The feature-side dedup is unchanged.Added
test_requests_sharing_entity_and_event_timestamp_are_all_kept(separate request ids, same entity and event time, with and without different collidingtsvalues). It fails on the previous head and passes now. Ray component and universal tests pass, and sending every request twice now returns every result twice in 171 random cases (one or two feature views, full feature names, a field mapping and a join-key map); master lost duplicates in 87 of them.