Repository navigation
fix: Join entity_df in local compute engine historical retrieval - #7234
Zhuoxi2000 wants to merge 5 commits into
Conversation
LocalFeatureBuilder used the base _build, which never joins entity_df for a source-backed view. get_historical_features therefore returned the latest row per join key across the whole source: unrequested entities came back and the point-in-time/TTL filter never ran. Override _build like the Trino and Flink builders and join entity_df after the source (and transformation). Dedup and aggregate on the entity timestamp as well as the entity keys, so an entity requested at several timestamps keeps one row per entity row. LocalJoinNode no longer joins entity_df a second time for a derived view whose inputs already carry it. Signed-off-by: Edson <zhuoxi2000@gmail.com>
There was a problem hiding this comment.
Devin Review found 3 potential issues.
4 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
| if self._should_join_entity_df(): | ||
| last_node = self.build_join_node(view, [last_node]) |
There was a problem hiding this comment.
🔴 Historical reads overwrite live features
When historical retrieval selects older rows for an online-enabled view, LocalOutputNode writes them to the online store. SQLite replaces newer serving values unconditionally. For offline-enabled Dask views, the added request timestamp makes the offline write reject the result.
Learn more
The local engine executes the historical plan when the caller reads the retrieval job’s result, as shown in LocalRetrievalJob. The builder still ends both historical and materialization plans with build_output_nodes, which calls stores through LocalOutputNode. Before the new join, source-backed retrieval deduplicated source values to the latest per entity; now it feeds historical values into an existing write path. SQLite’s upsert replaces the row without comparing timestamps. Dask’s offline writer requires exactly the source columns, which exclude the request timestamp.
Example: A driver has source values 0.1 at 10:00 and 0.9 at 12:00. SQLite is serving 0.9. Retrieving the driver at 11:00 returns and writes 0.1, so the next online lookup also serves 0.1. If the view also writes to Dask, the historical call raises after the online write.
Recommended fix: End historical plans with a read-only output node; reserve the store-writing node for materialization. Test an online-enabled view with a preloaded newer value and an offline-enabled view with a real source schema.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Fixed in 5b01404. LocalOutputNode now takes a write_to_stores flag, and LocalFeatureBuilder sets it only for a MaterializationTask, as FlinkFeatureBuilder does. Historical retrieval returns its rows and writes nothing.
The write was there before this PR: on master a historical read of an online view already makes one online_write_batch call with the latest row per key, and a read of an offline view raises in offline_write_batch. What this PR changed is which rows reached that write, so the overwrite described here was real.
Tests: test_get_historical_features_does_not_write_to_the_stores (an online and offline view read at an older timestamp, no online_write_batch call) and test_materialization_still_writes_to_the_online_store. It is a separate commit in case it should go in on its own.
| group_keys = list(self.group_keys) | ||
| # Aggregate per entity row after the entity_df join. | ||
| if ENTITY_TS_ALIAS in self.backend.columns(df): | ||
| group_keys.append(ENTITY_TS_ALIAS) | ||
| grouped_df = self.backend.groupby_agg(df, group_keys, self.agg_ops) |
There was a problem hiding this comment.
🔴 Duplicate requests inflate historical aggregates
When requests share a key and timestamp, LocalAggregationNode combines their copied feature rows and inflates sums and counts. LocalDedupNode also keeps only one of those requests, even when they carry different labels.
Learn more
The source join copies each matching feature row for every request with the same key, as in LocalJoinNode. A key and timestamp identify a lookup but do not uniquely identify a request row. The aggregation puts copies from different requests into one group, while LocalDedupNode chooses just one request from such a group. The derived join also uses the lookup pair in place of a request identifier.
Example: Two rides for driver 1 at 12:00 carry different training labels. One eligible source row has value 4. A sum yields one row with 8 instead of two rows with 4; non-aggregated retrieval keeps one ride.
Recommended fix: Assign a stable ID to each request before the join, as Flink’s entity preparation does. Use it to distinguish requests in deduplication, aggregation, and derived joins. Retain the request payload by ID and omit the internal ID from the final result.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Fixed in 32e27d0. LocalJoinNode numbers the entity rows when it joins entity_df, with the ENTITY_ROW_ID column the Flink engine uses. LocalDedupNode dedups on that id, LocalAggregationNode groups by it, the join of already joined inputs uses it, and LocalOutputNode drops it from the result.
Two requests for driver 1 at the same timestamp with labels a and b now come back as two rows, and the sum view gives 0.1 for each instead of one row with 0.2. Both are tested on the pandas and the Polars backend.
One part is not done: an aggregation keeps only its group-by keys and the aggregated columns, so the label is not in an aggregation view's result. The non-aggregated path keeps it. I listed that under "Not covered" in the description.
| if self._should_join_entity_df(): | ||
| last_node = self.build_join_node(view, [last_node]) |
There was a problem hiding this comment.
🔴 Historical lookups fail for global features
For views without entities, LocalFeatureBuilder sends an empty key list to PandasBackend.join. The merge raises before the request timestamps can select any global feature values.
Learn more
FeatureView records a dummy entity for views without keys, and _get_column_names resolves their columns using the registry. The column resolver excludes the dummy key, so the new source join receives no keys. LocalJoinNode still requests a left equality join rather than handling that case explicitly, and pandas rejects on=[].
Example: Request global ride counts at 12:00 with an entity DataFrame containing only event_timestamp. A source row at 11:00 is eligible, but the merge raises an IndexError.
Recommended fix: Give keyless views a global point-in-time lookup instead of an empty-key left merge. Ensure LocalDedupNode selects one eligible value per request even when there are no entity keys.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Fixed in 32e27d0. A view without entities is cross joined with entity_df, and the filter node keeps the feature rows inside the point-in-time and TTL bounds of each entity row. LocalDedupNode then keeps one row per entity row through the row id, so it no longer needs a join key.
test_get_historical_features_for_view_without_entities requests a view with entities=[] at two timestamps and gets 0.1 and 0.9, on the pandas and the Polars backend. FeatureStore.get_historical_features with the Dask offline store returns the same for that input.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7234 +/- ##
==========================================
+ Coverage 54.10% 54.17% +0.06%
==========================================
Files 467 467
Lines 58940 59000 +60
Branches 8688 8701 +13
==========================================
+ Hits 31890 31963 +73
+ Misses 24892 24874 -18
- Partials 2158 2163 +5
... and 5 files with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
Two entity rows can share their join keys and timestamp. The dedup node kept one of them and the aggregation node summed both into one row, and a feature view without entities had no key to join on, so the join raised. Number the entity rows when entity_df is joined, as the Flink engine does, and use that id to dedup, to group and to join already joined inputs. A view without entities is cross joined with entity_df. The id is dropped from the output. Signed-off-by: Edson <zhuoxi2000@gmail.com>
The local engine ended every plan with a node that writes its rows to the online and offline stores of the feature view. For historical retrieval those rows are now as of the entity timestamps, so reading an older value would replace the newer one the online store serves. Write only for materialization, as the Flink engine does. Signed-off-by: Edson <zhuoxi2000@gmail.com>
franciscojavierarceo
left a comment
There was a problem hiding this comment.
we need to preserve every requested entity row when no feature survives the point-in-time filter.
| last_node = self.build_transformation_node(view, [last_node]) | ||
|
|
||
| if self._should_join_entity_df(): | ||
| last_node = self.build_join_node(view, [last_node]) |
There was a problem hiding this comment.
the new source-backed historical path joins each request to candidate feature rows and then LocalFilterNode removes rows outside PIT/TTL bounds, but LocalRetrievalJob returns that filtered table directly. a request with no matching key, only future features, or only expired features therefore disappears instead of returning its entity row and null features; this can silently remove training examples. we need to left-join the selected results back to the original entity rows by ENTITY_ROW_ID before dropping that id, and cover unmatched, future-only, and expired-only requests on both backends.
There was a problem hiding this comment.
Agreed, those requests were dropped. Fixed in 0e1c766 and 962ca9c.
A new LocalEntityRowsNode runs just before the output node for a historical task and left-joins the selected rows back onto the numbered entity rows by ENTITY_ROW_ID, before LocalOutputNode drops the id. The entity columns are taken from entity_df, so a request with no surviving feature row comes back with its own columns and null features, and the result is sorted by the row id, so it is in entity_df order on both backends (962ca9c). LocalJoinNode and the new node share one helper for numbering the rows.
Tests, each on pandas and Polars and for an unmatched key, a future-only key and an expired-only key, always next to a request that does have a value: a source-backed view, a derived view, and the sum aggregation view. One more test covers a request set where no row has a feature. With the previous head's source files these are 20 failed / 18 passed; now 38 passed.
Side effect: the aggregation path now also returns the other entity_df columns (a label, for example), since they come from the join back. I updated the PR description to match.
The filter node drops an entity row when its key has no feature row, or only rows after the entity timestamp or older than the TTL, and the retrieval job returned the filtered table. Left-join the selected rows back onto the entity rows by the entity row id before it is dropped, so that every requested row is returned, with null features when nothing was selected. Signed-off-by: Edson <zhuoxi2000@gmail.com>
A backend's left join does not have to keep the order of the left frame, so sort the joined rows by the entity row id. Signed-off-by: Edson <zhuoxi2000@gmail.com>
What this PR does / why we need it:
LocalComputeEngine.get_historical_featuresignoredentity_dffor source-backed feature views.LocalFeatureBuilderused the baseFeatureBuilder._build, which only adds a join node when a view has upstreaminput_nodes. A source-backed view gotsource -> [transform] -> filter -> dedup, so the result was the latest row per join key across the whole source: unrequested entities came back,entity_dftimestamps were ignored, and the point-in-time/TTL branch ofLocalFilterNodenever ran.LocalDedupNodealso deduped on the join keys only, so an entity requested at two timestamps collapsed to one row.Changes:
LocalFeatureBuilder._should_join_entity_df()and a_buildoverride, mirroringTrinoFeatureBuilder/FlinkFeatureBuilder. After the source node (and the transformation, if any), a source-backed view is joined withentity_dfwhen the task is aHistoricalRetrievalTaskwith a pandasentity_df. Only pandas is accepted becauseLocalJoinNodeconvertsentity_dfwithpa.Table.from_pandas.LocalJoinNodenumbers the entity rows when it joinsentity_df(__feast_entity_row_id, theENTITY_ROW_IDthe Flink engine already uses). Two entity rows can share their join keys and timestamp, so the timestamp alone does not tell them apart.LocalDedupNodededups on that row id when it is present, so there is one row per entity row. Without the id it falls back to the join keys plus__entity_event_timestamp.LocalAggregationNodeadds__entity_event_timestampand the row id to its group-by keys when present. Without this, the join makes an aggregation over an entity requested at two timestamps return one row that sums both entity rows' windows (1.1 instead of 0.1 and 1.0 in the test), and two identical requests would be summed together.LocalJoinNodeno longer joinsentity_dfa second time when its first input already carries__entity_event_timestamp, and it joins such inputs on the join keys plus that column and the row id. Without this, a derived view (source=<another view>) ends up with__entity_event_timestamp_x/_ycolumns.LocalJoinNodecross joins it withentity_dfand the filter node picks the rows inside the point-in-time and TTL bounds.LocalEntityRowsNode(new, last step before the output node for a historical task) left-joins the selected rows back onto the numbered entity rows by the row id.LocalFilterNodedrops an entity row whose key has no feature row, or only rows after its timestamp or older than the TTL; that row now comes back with itsentity_dfcolumns and null features. The entity columns are taken fromentity_df, and the result is sorted by the row id, so it is inentity_dforder on either backend (its own small commit).LocalJoinNodeand this node share one helper for numbering the rows and aliasing the timestamp.LocalOutputNodedrops the row id from the result.Behavior change: historical retrieval through the local engine no longer writes to the stores.
LocalOutputNodeended every plan, and wrote its rows to the online store when the view isonlineand to the offline store when it isoffline. On master a historical read of anonlineview already made oneonline_write_batchcall (with the latest row per key), and a read of anofflineview raised inoffline_write_batch. With the join, the rows are as of the entity timestamps, so reading an older value would have replaced the newer one being served.LocalOutputNodetakes awrite_to_storesflag (defaultTrue), andLocalFeatureBuildersets it only for aMaterializationTask, asFlinkFeatureBuilderdoes. This is its own commit, in case you would rather take it separately.The base
FeatureBuilderis unchanged, so Spark and Ray are not affected by this PR.Not covered here:
filterexpression also comes back with null features, the same as a row outside the point-in-time or TTL bounds.__entity_event_timestampcolumn.LocalEntityRowsNodeneeds the row id column to reach it. A transformation UDF that returns a frame without__feast_entity_row_idleaves the result as the filter produced it, so rows without features are not restored on that path.entity_dfstill skips the join, becauseLocalJoinNodeonly converts pandas input. A pandasentity_dfwith the Polars backend is covered by the new tests.FeatureStore.get_historical_featureswith the Dask offline store returns one row for that input.LocalAggregationNodegroups by entity names, so an aggregation view whose entity name differs from its join key fails with aKeyErroron master. That is unchanged; the new aggregation test uses an entity named after its join key.KeyError: 'event_timestamp'(the parents' timestamp columns collide in the join). That is unchanged.Which issue(s) this PR fixes:
Fixes #7233
Checks
git commit -s)Testing Strategy
New tests in
sdk/python/tests/unit/infra/compute_engines/test_local_compute_engine.py. Driver 1 has feature rows at t0 and t0+2h and is requested at t0+1h and t0+3h; driver 2 is not requested.test_get_historical_features_is_point_in_time_per_entity_row: a source-backed view.test_get_historical_features_for_derived_view: a view whose source is that view.test_get_historical_features_aggregates_per_entity_row: asumaggregation over the same source.The first two expect two rows for driver 1 with
conv_rate[0.1, 0.9]; the aggregation test expectssum_conv_rate[0.1, 1.0]. For the same inputs,FeatureStore.get_historical_featureswith the Dask offline store returns 0.1 and 0.9.Added after the Devin Review comments, the first three on both the pandas and the Polars backend:
test_get_historical_features_keeps_entity_rows_with_the_same_key_and_time: driver 1 requested twice at t0+1h with labelsaandb; both rows come back withconv_rate0.1.test_get_historical_features_aggregates_entity_rows_with_the_same_key_and_time: the same two requests through thesumview give [0.1, 0.1], not one row with 0.2.test_get_historical_features_for_view_without_entities: a view withentities=[]requested at t0+1h and t0+3h gives [0.1, 0.9]. The Dask offline store returns the same for this input.test_get_historical_features_does_not_write_to_the_stores: anonlineandofflineview, read at t0+1h, returns 0.1 and makes noonline_write_batchcall.test_materialization_still_writes_to_the_online_store:_materialize_oneon anonlineview makes oneonline_write_batchcall.Added after the review, each on both backends and for an unmatched key (driver 3), a key whose only feature row is in the future (driver 2 at t0-1h) and a key whose only feature row is past the TTL (driver 2 at t0+2d). Each request is paired with a driver 1 request that does have a value, and the rows are told apart by a label:
test_get_historical_features_keeps_entity_rows_without_features: a source-backed view returns both rows inentity_dforder,conv_rate0.1 and null.test_get_historical_features_for_derived_view_keeps_entity_rows: the same through a view whose source is that view.test_get_historical_features_aggregation_keeps_entity_rows: the same through thesumview (sum_conv_rate0.1 and null, labels kept).test_get_historical_features_keeps_entity_rows_when_none_has_features: two requests, neither with a feature row; both come back with nullconv_rate.The Polars parameters use
pytest.importorskip("polars"). polars is not insdk/python/requirements/py3.1*-ci-requirements.txt, so they run only where it is installed (locally, polars 2.2.0).Related tests:
tests/unit/infra/compute_engineswith the Spark application, Trino and Snowflake modules left out (their optional dependencies aren't installed locally) gives 121 passed, 1 skipped, with polars 2.2.0 installed.tests/unit/test_type_map.pyandtests/unit/test_feature_views.pyfail 29 tests locally, all withNo module named 'pyspark'.ruff checkandruff format --checkpass on the touched files, and mypy reports nothing in them locally apart from the missing pandas stubs.Size: 3 files, +533/-18 (+161/-18 of source, +372 of tests).
Misc
Same gap that was flagged for the Trino engine in #6898 (comment). #5985 touches the timestamp-column guard in
LocalDedupNode, #7004 edits theentity_dfblock ofLocalJoinNode.executea few lines from the guard added here, and #7028 adds incremental methods toLocalJoinNode, so whichever lands later may need a small rebase.