Skip to content

Commit 8c17033

Browse files
Ambient Code Botjyejare
authored andcommitted
Fix ODFV output projection in offline retrieval (feast-dev#6099)
This commit fixes issue feast-dev#6099 where offline retrieval (get_historical_features) was returning ALL OnDemandFeatureView output features, even when only a subset was requested, while online retrieval correctly returned only requested features. Changes: - Modified RetrievalJob.to_arrow() to filter ODFV outputs based on requested features from metadata, matching online retrieval behavior - Added test_odfv_projection to verify the fix and prevent regression Before this fix: - Online: features=['odfv:feature_a'] -> returns feature_a only ✓ - Offline: features=['odfv:feature_a'] -> returns feature_a, feature_b, feature_c ✗ After this fix: - Both online and offline return only the requested features ✓ This ensures schema consistency between training (offline) and serving (online) pipelines, preventing downstream issues in ML workflows. Fixes feast-dev#6099
1 parent 65cf03c commit 8c17033

2 files changed

Lines changed: 144 additions & 3 deletions

File tree

‎sdk/python/feast/infra/offline_stores/offline_store.py‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,44 @@ def to_arrow(
154154
"""
155155
features_table = self._to_arrow_internal(timeout=timeout)
156156
if self.on_demand_feature_views:
157+
# Build a mapping of ODFV name to requested feature names
158+
# This ensures we only return the features that were explicitly requested
159+
odfv_feature_refs = {}
160+
if self.metadata and self.metadata.features:
161+
for feature_ref in self.metadata.features:
162+
if ":" in feature_ref:
163+
view_name, feature_name = feature_ref.split(":", 1)
164+
# Check if this view_name matches any of the ODFVs
165+
for odfv in self.on_demand_feature_views:
166+
if odfv.name == view_name or odfv.projection.name_to_use() == view_name:
167+
if view_name not in odfv_feature_refs:
168+
odfv_feature_refs[view_name] = []
169+
# Store the feature name in the format that will appear in transformed_arrow
170+
expected_col_name = (
171+
f"{odfv.projection.name_to_use()}__{feature_name}"
172+
if self.full_feature_names
173+
else feature_name
174+
)
175+
odfv_feature_refs[view_name].append(expected_col_name)
176+
157177
for odfv in self.on_demand_feature_views:
158178
transformed_arrow = odfv.transform_arrow(
159179
features_table, self.full_feature_names
160180
)
161181

182+
# Determine which columns to include from this ODFV
183+
# If we have metadata with requested features, filter to only those
184+
# Otherwise, include all columns (backward compatibility)
185+
requested_features_for_odfv = odfv_feature_refs.get(odfv.name) or odfv_feature_refs.get(odfv.projection.name_to_use())
186+
162187
for col in transformed_arrow.column_names:
163188
if col.startswith("__index"):
164189
continue
165-
features_table = features_table.append_column(
166-
col, transformed_arrow[col]
167-
)
190+
# Only append the column if it was requested, or if we don't have feature metadata
191+
if requested_features_for_odfv is None or col in requested_features_for_odfv:
192+
features_table = features_table.append_column(
193+
col, transformed_arrow[col]
194+
)
168195

169196
if validation_reference:
170197
if not flags_helper.is_test():

‎sdk/python/tests/integration/offline_store/test_universal_historical_retrieval.py‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,3 +840,117 @@ def test_historical_features_non_entity_retrieval(environment):
840840
assert 300 in actual_trips, (
841841
"Latest trip value 300 for driver 1002 should be present"
842842
)
843+
844+
845+
@pytest.mark.integration
846+
@pytest.mark.universal_offline_stores
847+
@pytest.mark.parametrize("full_feature_names", [True, False], ids=lambda v: f"full:{v}")
848+
def test_odfv_projection(environment, universal_data_sources, full_feature_names):
849+
"""
850+
Test that requesting a subset of ODFV features only returns those features.
851+
852+
Regression test for issue #6099: OnDemandFeatureViews should honor output
853+
projection in offline retrieval, matching the behavior of online retrieval.
854+
855+
Before the fix, offline retrieval would return ALL ODFV output features even
856+
when only a subset was requested, while online retrieval correctly returned
857+
only the requested features.
858+
"""
859+
store = environment.feature_store
860+
861+
(entities, datasets, data_sources) = universal_data_sources
862+
863+
feature_views = construct_universal_feature_views(data_sources)
864+
865+
# Add request data needed for ODFV
866+
entity_df_with_request_data = datasets.entity_df.copy(deep=True)
867+
entity_df_with_request_data["val_to_add"] = [
868+
i for i in range(len(entity_df_with_request_data))
869+
]
870+
871+
store.apply([driver(), *feature_views.values()])
872+
873+
# The conv_rate_plus_100 ODFV has 3 output features:
874+
# - conv_rate_plus_100
875+
# - conv_rate_plus_val_to_add
876+
# - conv_rate_plus_100_rounded
877+
878+
# Test 1: Request only ONE ODFV feature
879+
job = store.get_historical_features(
880+
entity_df=entity_df_with_request_data,
881+
features=[
882+
"conv_rate_plus_100:conv_rate_plus_100", # Request only this one
883+
],
884+
full_feature_names=full_feature_names,
885+
)
886+
887+
actual_df = job.to_df()
888+
889+
# Determine expected column names based on full_feature_names setting
890+
expected_feature = (
891+
"conv_rate_plus_100__conv_rate_plus_100" if full_feature_names
892+
else "conv_rate_plus_100"
893+
)
894+
unrequested_feature_1 = (
895+
"conv_rate_plus_100__conv_rate_plus_val_to_add" if full_feature_names
896+
else "conv_rate_plus_val_to_add"
897+
)
898+
unrequested_feature_2 = (
899+
"conv_rate_plus_100__conv_rate_plus_100_rounded" if full_feature_names
900+
else "conv_rate_plus_100_rounded"
901+
)
902+
903+
# Verify the requested feature is present
904+
assert expected_feature in actual_df.columns, (
905+
f"Requested feature '{expected_feature}' should be in the result"
906+
)
907+
908+
# Verify unrequested ODFV features are NOT present (this is the key fix)
909+
assert unrequested_feature_1 not in actual_df.columns, (
910+
f"Unrequested ODFV feature '{unrequested_feature_1}' should NOT be in the result. "
911+
f"This indicates the bug from issue #6099 still exists."
912+
)
913+
assert unrequested_feature_2 not in actual_df.columns, (
914+
f"Unrequested ODFV feature '{unrequested_feature_2}' should NOT be in the result. "
915+
f"This indicates the bug from issue #6099 still exists."
916+
)
917+
918+
# Test 2: Request TWO out of THREE ODFV features
919+
job2 = store.get_historical_features(
920+
entity_df=entity_df_with_request_data,
921+
features=[
922+
"conv_rate_plus_100:conv_rate_plus_100",
923+
"conv_rate_plus_100:conv_rate_plus_val_to_add",
924+
# Deliberately NOT requesting conv_rate_plus_100_rounded
925+
],
926+
full_feature_names=full_feature_names,
927+
)
928+
929+
actual_df2 = job2.to_df()
930+
931+
# Verify the two requested features are present
932+
assert expected_feature in actual_df2.columns
933+
assert unrequested_feature_1 in actual_df2.columns
934+
935+
# Verify the unrequested feature is NOT present
936+
assert unrequested_feature_2 not in actual_df2.columns, (
937+
f"Unrequested ODFV feature '{unrequested_feature_2}' should NOT be in the result"
938+
)
939+
940+
# Test 3: Verify consistency with online retrieval
941+
online_response = store.get_online_features(
942+
features=["conv_rate_plus_100:conv_rate_plus_100"],
943+
entity_rows=[{"driver_id": 1001, "val_to_add": 1}],
944+
full_feature_names=full_feature_names,
945+
).to_dict()
946+
947+
# Online retrieval should also NOT include unrequested ODFV features
948+
assert expected_feature in online_response, (
949+
f"Online retrieval should return requested feature '{expected_feature}'"
950+
)
951+
assert unrequested_feature_1 not in online_response, (
952+
f"Online retrieval should NOT return unrequested feature '{unrequested_feature_1}'"
953+
)
954+
assert unrequested_feature_2 not in online_response, (
955+
f"Online retrieval should NOT return unrequested feature '{unrequested_feature_2}'"
956+
)

0 commit comments

Comments
 (0)