From 8be6a9912163713aa217250b37b2725b5748eb09 Mon Sep 17 00:00:00 2001 From: Felix Wang Date: Wed, 24 Nov 2021 15:39:55 -0800 Subject: [PATCH 1/3] Rename variables to be more clear Signed-off-by: Felix Wang --- sdk/python/feast/feature_store.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/python/feast/feature_store.py b/sdk/python/feast/feature_store.py index d2c20cd854..898fd00204 100644 --- a/sdk/python/feast/feature_store.py +++ b/sdk/python/feast/feature_store.py @@ -1238,28 +1238,28 @@ def _augment_response_with_on_demand_transforms( self, feature_refs: List[str], requested_result_row_names: Set[str], - odfvs: List[OnDemandFeatureView], + requested_on_demand_feature_views: List[OnDemandFeatureView], full_feature_names: bool, initial_response: OnlineResponse, result_rows: List[GetOnlineFeaturesResponse.FieldValues], ) -> OnlineResponse: - all_on_demand_feature_views = {view.name: view for view in odfvs} - all_odfv_feature_names = all_on_demand_feature_views.keys() + requested_odfv_map = {odfv.name: odfv for odfv in requested_on_demand_feature_views} + requested_odfv_feature_names = requested_odfv_map.keys() - if len(all_on_demand_feature_views) == 0: + if len(requested_odfv_map) == 0: return initial_response initial_response_df = initial_response.to_df() odfv_feature_refs = defaultdict(list) for feature_ref in feature_refs: view_name, feature_name = feature_ref.split(":") - if view_name in all_odfv_feature_names: + if view_name in requested_odfv_feature_names: odfv_feature_refs[view_name].append(feature_name) # Apply on demand transformations odfv_result_names = set() for odfv_name, _feature_refs in odfv_feature_refs.items(): - odfv = all_on_demand_feature_views[odfv_name] + odfv = requested_odfv_map[odfv_name] transformed_features_df = odfv.get_transformed_features_df( initial_response_df ) From 588bd8baad2f18e0d38a6a162968181243056e38 Mon Sep 17 00:00:00 2001 From: Felix Wang Date: Wed, 24 Nov 2021 16:12:52 -0800 Subject: [PATCH 2/3] Refactor on demand logic and add docstring Signed-off-by: Felix Wang --- sdk/python/feast/feature_store.py | 42 +++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/sdk/python/feast/feature_store.py b/sdk/python/feast/feature_store.py index 898fd00204..6b1929087d 100644 --- a/sdk/python/feast/feature_store.py +++ b/sdk/python/feast/feature_store.py @@ -1084,17 +1084,14 @@ def get_online_features( union_of_entity_keys, ) - initial_response = OnlineResponse( - GetOnlineFeaturesResponse(field_values=result_rows) - ) - return self._augment_response_with_on_demand_transforms( + self._augment_response_with_on_demand_transforms( _feature_refs, requested_result_row_names, requested_on_demand_feature_views, full_feature_names, - initial_response, result_rows, ) + return OnlineResponse(GetOnlineFeaturesResponse(field_values=result_rows)) def _get_requested_result_fields( self, @@ -1240,15 +1237,31 @@ def _augment_response_with_on_demand_transforms( requested_result_row_names: Set[str], requested_on_demand_feature_views: List[OnDemandFeatureView], full_feature_names: bool, - initial_response: OnlineResponse, result_rows: List[GetOnlineFeaturesResponse.FieldValues], - ) -> OnlineResponse: - requested_odfv_map = {odfv.name: odfv for odfv in requested_on_demand_feature_views} + ): + """Computes on demand feature values and adds them to the result rows. + + Assumes that 'result_rows' already contains the necessary request data and input feature + views for the on demand feature views. Unneeded feature values such as request data and + unrequested input feature views will be removed from 'result_rows'. + + Args: + feature_refs: List of all feature references to be returned. + requested_result_row_names: Fields from 'result_rows' that have been requested, and + therefore should not be dropped. + requested_on_demand_feature_views: List of all odfvs that have been requested. + full_feature_names: A boolean that provides the option to add the feature view prefixes to the feature names, + changing them from the format "feature" to "feature_view__feature" (e.g., "daily_transactions" changes to + "customer_fv__daily_transactions"). + result_rows: List of result rows to be augmented with on demand feature values. + """ + requested_odfv_map = { + odfv.name: odfv for odfv in requested_on_demand_feature_views + } requested_odfv_feature_names = requested_odfv_map.keys() if len(requested_odfv_map) == 0: - return initial_response - initial_response_df = initial_response.to_df() + return odfv_feature_refs = defaultdict(list) for feature_ref in feature_refs: @@ -1256,7 +1269,12 @@ def _augment_response_with_on_demand_transforms( if view_name in requested_odfv_feature_names: odfv_feature_refs[view_name].append(feature_name) - # Apply on demand transformations + initial_response = OnlineResponse( + GetOnlineFeaturesResponse(field_values=result_rows) + ) + initial_response_df = initial_response.to_df() + + # Apply on demand transformations and augment the result rows odfv_result_names = set() for odfv_name, _feature_refs in odfv_feature_refs.items(): odfv = requested_odfv_map[odfv_name] @@ -1297,8 +1315,6 @@ def _augment_response_with_on_demand_transforms( result_row.fields.pop(unneeded_feature) result_row.statuses.pop(unneeded_feature) - return OnlineResponse(GetOnlineFeaturesResponse(field_values=result_rows)) - def _get_feature_views_to_use( self, features: Optional[Union[List[str], FeatureService]], From 5584f2d6f3999a61813b6fbc9d759cb6f67ccc1a Mon Sep 17 00:00:00 2001 From: Felix Wang Date: Wed, 24 Nov 2021 17:05:55 -0800 Subject: [PATCH 3/3] Small refactor Signed-off-by: Felix Wang --- sdk/python/feast/feature_store.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/python/feast/feature_store.py b/sdk/python/feast/feature_store.py index 6b1929087d..cfdc250d62 100644 --- a/sdk/python/feast/feature_store.py +++ b/sdk/python/feast/feature_store.py @@ -1255,14 +1255,14 @@ def _augment_response_with_on_demand_transforms( "customer_fv__daily_transactions"). result_rows: List of result rows to be augmented with on demand feature values. """ + if len(requested_on_demand_feature_views) == 0: + return + requested_odfv_map = { odfv.name: odfv for odfv in requested_on_demand_feature_views } requested_odfv_feature_names = requested_odfv_map.keys() - if len(requested_odfv_map) == 0: - return - odfv_feature_refs = defaultdict(list) for feature_ref in feature_refs: view_name, feature_name = feature_ref.split(":")